init buffer
[libav.git] / libavformat / gxfenc.c
1 /*
2 * GXF muxer.
3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "avformat.h"
21 #include "gxf.h"
22 #include "riff.h"
23
24 #define GXF_AUDIO_PACKET_SIZE 65536
25
26 typedef struct GXFStreamContext {
27 AVCodecContext *codec;
28 FifoBuffer audio_buffer;
29 uint32_t track_type;
30 uint32_t sample_size;
31 uint32_t sample_rate;
32 uint16_t media_type;
33 uint16_t media_info;
34 uint8_t index;
35 int frame_rate_index;
36 int lines_index;
37 int fields;
38 int iframes;
39 int pframes;
40 int bframes;
41 int p_per_gop;
42 int b_per_gop;
43 int closed_gop;
44 int64_t current_dts;
45 } GXFStreamContext;
46
47 typedef struct GXFContext {
48 uint32_t nb_frames;
49 uint32_t material_flags;
50 uint16_t audio_tracks;
51 uint16_t mpeg_tracks;
52 int64_t creation_time;
53 uint32_t umf_start_offset;
54 uint32_t umf_track_offset;
55 uint32_t umf_media_offset;
56 uint32_t umf_user_data_offset;
57 uint32_t umf_user_data_size;
58 uint32_t umf_length;
59 uint16_t umf_track_size;
60 uint16_t umf_media_size;
61 int audio_written;
62 int sample_rate;
63 int flags;
64 AVFormatContext *fc;
65 GXFStreamContext streams[48];
66 } GXFContext;
67
68 typedef struct GXF_Lines {
69 int height;
70 int index;
71 } GXF_Lines;
72
73
74 /* FIXME check if it is relevant */
75 static const GXF_Lines gxf_lines_tab[] = {
76 { 480, 1 }, /* NTSC */
77 { 512, 1 }, /* NTSC + VBI */
78 { 576, 2 }, /* PAL */
79 { 608, 2 }, /* PAL + VBI */
80 { 1080, 4 },
81 { 720, 6 },
82 };
83
84 static const CodecTag gxf_media_types[] = {
85 { CODEC_ID_MJPEG , 3 }, /* NTSC */
86 { CODEC_ID_MJPEG , 4 }, /* PAL */
87 { CODEC_ID_PCM_S24LE , 9 },
88 { CODEC_ID_PCM_S16LE , 10 },
89 { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
90 { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
91 { CODEC_ID_DVVIDEO , 13 }, /* NTSC */
92 { CODEC_ID_DVVIDEO , 14 }, /* PAL */
93 { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
94 { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
95 { CODEC_ID_AC3 , 17 },
96 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
97 { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
98 { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
99 { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
100 { 0, 0 },
101 };
102
103 #define SERVER_PATH "/space/"
104 #define ES_NAME_PATTERN "ES."
105
106 static int gxf_find_lines_index(GXFStreamContext *ctx)
107 {
108 int i;
109
110 for (i = 0; i < 6; ++i) {
111 if (ctx->codec->height == gxf_lines_tab[i].height) {
112 ctx->lines_index = gxf_lines_tab[i].index;
113 return 0;
114 }
115 }
116 return -1;
117 }
118
119 static void gxf_write_padding(ByteIOContext *pb, offset_t to_pad)
120 {
121 for (; to_pad > 0; to_pad--) {
122 put_byte(pb, 0);
123 }
124 }
125
126 static offset_t updatePacketSize(ByteIOContext *pb, offset_t pos)
127 {
128 offset_t curpos;
129 int size;
130
131 size = url_ftell(pb) - pos;
132 if (size % 4) {
133 gxf_write_padding(pb, 4 - size % 4);
134 size = url_ftell(pb) - pos;
135 }
136 curpos = url_ftell(pb);
137 url_fseek(pb, pos + 6, SEEK_SET);
138 put_be32(pb, size);
139 url_fseek(pb, curpos, SEEK_SET);
140 return curpos - pos;
141 }
142
143 static offset_t updateSize(ByteIOContext *pb, offset_t pos)
144 {
145 offset_t curpos;
146
147 curpos = url_ftell(pb);
148 url_fseek(pb, pos, SEEK_SET);
149 put_be16(pb, curpos - pos - 2);
150 url_fseek(pb, curpos, SEEK_SET);
151 return curpos - pos;
152 }
153
154 static void gxf_write_packet_header(ByteIOContext *pb, pkt_type_t type)
155 {
156 put_be32(pb, 0); /* packet leader for synchro */
157 put_byte(pb, 1);
158 put_byte(pb, type); /* map packet */
159 put_be32(pb, 0); /* size */
160 put_be32(pb, 0); /* reserved */
161 put_byte(pb, 0xE1); /* trailer 1 */
162 put_byte(pb, 0xE2); /* trailer 2 */
163 }
164
165 static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
166 {
167 char buffer[1024];
168 int size;
169
170 if (ctx->iframes) {
171 ctx->p_per_gop = ctx->pframes / ctx->iframes;
172 if (ctx->pframes % ctx->iframes)
173 ctx->p_per_gop++;
174 if (ctx->pframes)
175 ctx->b_per_gop = ctx->bframes / ctx->pframes;
176 if (ctx->p_per_gop > 9)
177 ctx->p_per_gop = 9; /* ensure value won't take more than one char */
178 if (ctx->b_per_gop > 9)
179 ctx->b_per_gop = 9; /* ensure value won't take more than one char */
180 }
181 size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
182 "Pix 0\nCf %d\nCg %d\nSl 7\nnl16 %d\nVi 1\nf1 1\n",
183 (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop,
184 ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, ctx->closed_gop,
185 ctx->codec->height / 16);
186 put_byte(pb, 0x4F);
187 put_byte(pb, size + 1);
188 put_buffer(pb, (uint8_t *)buffer, size + 1);
189 return size + 3;
190 }
191
192 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
193 {
194 /* FIXME implement that */
195 put_byte(pb, 0); /* fields */
196 put_byte(pb, 0); /* seconds */
197 put_byte(pb, 0); /* minutes */
198 put_byte(pb, 0); /* flags + hours */
199 /* reserved */
200 put_be32(pb, 0);
201 return 8;
202 }
203
204 static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stream)
205 {
206 offset_t pos;
207
208 /* track description section */
209 put_byte(pb, stream->media_type + 0x80);
210 put_byte(pb, stream->index + 0xC0);
211
212 pos = url_ftell(pb);
213 put_be16(pb, 0); /* size */
214
215 /* media file name */
216 put_byte(pb, 0x4C);
217 put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
218 put_tag(pb, ES_NAME_PATTERN);
219 put_be16(pb, stream->media_info);
220 put_byte(pb, 0);
221
222 if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
223 /* auxiliary information */
224 put_byte(pb, 0x4D);
225 put_byte(pb, 8);
226 if (stream->codec->codec_id == CODEC_ID_NONE)
227 gxf_write_timecode_auxiliary(pb, stream);
228 else
229 put_le64(pb, 0);
230 }
231
232 /* file system version */
233 put_byte(pb, 0x4E);
234 put_byte(pb, 4);
235 put_be32(pb, 0);
236
237 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
238 gxf_write_mpeg_auxiliary(pb, stream);
239
240 /* frame rate */
241 put_byte(pb, 0x50);
242 put_byte(pb, 4);
243 put_be32(pb, stream->frame_rate_index);
244
245 /* lines per frame */
246 put_byte(pb, 0x51);
247 put_byte(pb, 4);
248 put_be32(pb, stream->lines_index);
249
250 /* fields per frame */
251 put_byte(pb, 0x52);
252 put_byte(pb, 4);
253 put_be32(pb, stream->fields);
254
255 return updateSize(pb, pos);
256 }
257
258 static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
259 {
260 offset_t pos;
261 const char *filename = strrchr(ctx->fc->filename, '/');
262
263 pos = url_ftell(pb);
264 put_be16(pb, 0); /* size */
265
266 /* name */
267 if (filename)
268 filename++;
269 else
270 filename = ctx->fc->filename;
271 put_byte(pb, 0x40);
272 put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
273 put_tag(pb, SERVER_PATH);
274 put_tag(pb, filename);
275 put_byte(pb, 0);
276
277 /* first field */
278 put_byte(pb, 0x41);
279 put_byte(pb, 4);
280 put_be32(pb, 0);
281
282 /* last field */
283 put_byte(pb, 0x42);
284 put_byte(pb, 4);
285 put_be32(pb, ctx->nb_frames);
286
287 /* reserved */
288 put_byte(pb, 0x43);
289 put_byte(pb, 4);
290 put_be32(pb, 0);
291
292 put_byte(pb, 0x44);
293 put_byte(pb, 4);
294 put_be32(pb, ctx->nb_frames);
295
296 /* estimated size */
297 put_byte(pb, 0x45);
298 put_byte(pb, 4);
299 put_be32(pb, url_fsize(pb) / 1024);
300
301 return updateSize(pb, pos);
302 }
303
304 static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx)
305 {
306 offset_t pos;
307 int i;
308
309 pos = url_ftell(pb);
310 put_be16(pb, 0); /* size */
311 for (i = 0; i < ctx->fc->nb_streams; ++i)
312 gxf_write_track_description(pb, &ctx->streams[i]);
313 return updateSize(pb, pos);
314 }
315
316 static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
317 {
318 offset_t pos = url_ftell(pb);
319
320 gxf_write_packet_header(pb, PKT_MAP);
321
322 /* preamble */
323 put_byte(pb, 0xE0); /* version */
324 put_byte(pb, 0xFF); /* reserved */
325
326 gxf_write_material_data_section(pb, ctx);
327 gxf_write_track_description_section(pb, ctx);
328
329 return updatePacketSize(pb, pos);
330 }
331
332 #if 0
333 static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx)
334 {
335 offset_t pos = url_ftell(pb);
336 int i;
337
338 gxf_write_packet_header(pb, PKT_FLT);
339
340 put_le32(pb, 1000); /* number of fields */
341 put_le32(pb, 0); /* number of active flt entries */
342
343 for (i = 0; i < 1000; ++i) {
344 put_le32(pb, 0);
345 }
346 return updatePacketSize(pb, pos);
347 }
348 #endif
349
350 static int gxf_write_umf_material_description(ByteIOContext *pb, GXFContext *ctx)
351 {
352 put_le32(pb, ctx->flags);
353 put_le32(pb, ctx->nb_frames); /* length of the longest track */
354 put_le32(pb, ctx->nb_frames); /* length of the shortest track */
355 put_le32(pb, 0); /* mark in */
356 put_le32(pb, ctx->nb_frames); /* mark out */
357 put_le32(pb, 0); /* timecode mark in */
358 put_le32(pb, ctx->nb_frames); /* timecode mark out */
359 put_le64(pb, ctx->fc->timestamp); /* modification time */
360 put_le64(pb, ctx->fc->timestamp); /* creation time */
361 put_le16(pb, 0); /* reserved */
362 put_le16(pb, 0); /* reserved */
363 put_le16(pb, ctx->audio_tracks);
364 put_le16(pb, 0); /* timecode track count */
365 put_le16(pb, 0); /* reserved */
366 put_le16(pb, ctx->mpeg_tracks);
367 return 48;
368 }
369
370 static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx)
371 {
372 put_le32(pb, ctx->umf_length); /* total length of the umf data */
373 put_le32(pb, 3); /* version */
374 put_le32(pb, ctx->fc->nb_streams);
375 put_le32(pb, ctx->umf_track_offset); /* umf track section offset */
376 put_le32(pb, ctx->umf_track_size);
377 put_le32(pb, ctx->fc->nb_streams);
378 put_le32(pb, ctx->umf_media_offset);
379 put_le32(pb, ctx->umf_media_size);
380 put_le32(pb, ctx->umf_user_data_offset); /* user data offset */
381 put_le32(pb, ctx->umf_user_data_size); /* user data size */
382 put_le32(pb, 0); /* reserved */
383 put_le32(pb, 0); /* reserved */
384 return 48;
385 }
386
387 static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
388 {
389 offset_t pos = url_ftell(pb);
390 int tracks[255]={0};
391 int i;
392
393 ctx->umf_track_offset = pos - ctx->umf_start_offset;
394 for (i = 0; i < ctx->fc->nb_streams; ++i) {
395 AVStream *st = ctx->fc->streams[i];
396 GXFStreamContext *sc = &ctx->streams[i];
397 int id = 0;
398
399 switch (st->codec->codec_id) {
400 case CODEC_ID_MPEG1VIDEO: id= 'L'; break;
401 case CODEC_ID_MPEG2VIDEO: id= 'M'; break;
402 case CODEC_ID_PCM_S16LE: id= 'A'; break;
403 case CODEC_ID_DVVIDEO: id= sc->track_type == 6 ? 'E' : 'D'; break;
404 case CODEC_ID_MJPEG: id= 'V'; break;
405 }
406 sc->media_info= id << 8;
407 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
408 sc->media_info |= '0' + (tracks[id]++);
409 put_le16(pb, sc->media_info);
410 put_le16(pb, 1);
411 }
412 return url_ftell(pb) - pos;
413 }
414
415 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, GXFStreamContext *stream)
416 {
417 if (stream->codec->pix_fmt == PIX_FMT_YUV422P)
418 put_le32(pb, 2);
419 else
420 put_le32(pb, 1); /* default to 420 */
421 put_le32(pb, stream->closed_gop); /* closed = 1, open = 0, unknown = 255 */
422 put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
423 put_le32(pb, 1); /* I picture per GOP */
424 put_le32(pb, stream->p_per_gop);
425 put_le32(pb, stream->b_per_gop);
426 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
427 put_le32(pb, 2);
428 else if (stream->codec->codec_id == CODEC_ID_MPEG1VIDEO)
429 put_le32(pb, 1);
430 else
431 put_le32(pb, 0);
432 put_le32(pb, 0); /* reserved */
433 return 32;
434 }
435
436 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *track)
437 {
438 /* FIXME implement */
439 put_be32(pb, 0); /* drop frame flag */
440 put_be32(pb, 0); /* reserved */
441 put_be32(pb, 0); /* reserved */
442 put_be32(pb, 0); /* reserved */
443 put_be32(pb, 0); /* reserved */
444 put_be32(pb, 0); /* reserved */
445 put_be32(pb, 0); /* reserved */
446 put_be32(pb, 0); /* reserved */
447 return 32;
448 }
449
450 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *track)
451 {
452 int i;
453
454 for (i = 0; i < 8; i++) {
455 put_be32(pb, 0);
456 }
457 return 32;
458 }
459
460 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *track)
461 {
462 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
463 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
464 put_le32(pb, 0); /* number of fields over which to ramp up sound level */
465 put_le32(pb, 0); /* number of fields over which to ramp down sound level */
466 put_le32(pb, 0); /* reserved */
467 put_le32(pb, 0); /* reserved */
468 return 32;
469 }
470
471 #if 0
472 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track)
473 {
474 put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
475 put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
476 put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
477 put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
478 return 32;
479 }
480 #endif
481
482 static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
483 {
484 offset_t pos;
485 int i;
486
487 pos = url_ftell(pb);
488 ctx->umf_media_offset = pos - ctx->umf_start_offset;
489 for (i = 0; i < ctx->fc->nb_streams; ++i) {
490 GXFStreamContext *sc = &ctx->streams[i];
491 char buffer[88];
492 offset_t startpos, curpos;
493 int path_size = strlen(ES_NAME_PATTERN);
494
495 memset(buffer, 0, 88);
496 startpos = url_ftell(pb);
497 put_le16(pb, 0); /* length */
498 put_le16(pb, sc->media_info);
499 put_le16(pb, 0); /* reserved */
500 put_le16(pb, 0); /* reserved */
501 put_le32(pb, ctx->nb_frames);
502 put_le32(pb, 0); /* attributes rw, ro */
503 put_le32(pb, 0); /* mark in */
504 put_le32(pb, ctx->nb_frames); /* mark out */
505 strncpy(buffer, ES_NAME_PATTERN, path_size);
506 put_buffer(pb, (uint8_t *)buffer, path_size);
507 put_be16(pb, sc->media_info);
508 put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
509 put_le32(pb, sc->track_type);
510 put_le32(pb, sc->sample_rate);
511 put_le32(pb, sc->sample_size);
512 put_le32(pb, 0); /* reserved */
513 switch (sc->codec->codec_id) {
514 case CODEC_ID_MPEG2VIDEO:
515 gxf_write_umf_media_mpeg(pb, sc);
516 break;
517 case CODEC_ID_PCM_S16LE:
518 gxf_write_umf_media_audio(pb, sc);
519 break;
520 case CODEC_ID_DVVIDEO:
521 gxf_write_umf_media_dv(pb, sc);
522 break;
523 default:
524 gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
525 }
526 curpos = url_ftell(pb);
527 url_fseek(pb, startpos, SEEK_SET);
528 put_le16(pb, curpos - startpos);
529 url_fseek(pb, curpos, SEEK_SET);
530 }
531 return url_ftell(pb) - pos;
532 }
533
534 static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
535 {
536 offset_t pos = url_ftell(pb);
537 ctx->umf_user_data_offset = pos - ctx->umf_start_offset;
538 put_le32(pb, 20);
539 put_le32(pb, 0);
540 put_le16(pb, 0);
541 put_le16(pb, 0);
542 put_le32(pb, 0);
543 put_byte(pb, 0);
544 put_byte(pb, 0);
545 put_byte(pb, 0);
546 put_byte(pb, 0);
547 return 20;
548 }
549
550 static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
551 {
552 offset_t pos = url_ftell(pb);
553
554 gxf_write_packet_header(pb, PKT_UMF);
555
556 /* preamble */
557 put_byte(pb, 3); /* first and last (only) packet */
558 put_be32(pb, ctx->umf_length); /* data length */
559
560 ctx->umf_start_offset = url_ftell(pb);
561 gxf_write_umf_payload(pb, ctx);
562 gxf_write_umf_material_description(pb, ctx);
563 ctx->umf_track_size = gxf_write_umf_track_description(pb, ctx);
564 ctx->umf_media_size = gxf_write_umf_media_description(pb, ctx);
565 ctx->umf_user_data_size = gxf_write_umf_user_data(pb, ctx);
566 ctx->umf_length = url_ftell(pb) - ctx->umf_start_offset;
567 return updatePacketSize(pb, pos);
568 }
569
570 static int gxf_write_header(AVFormatContext *s)
571 {
572 ByteIOContext *pb = &s->pb;
573 GXFContext *gxf = s->priv_data;
574 int i;
575
576 gxf->fc = s;
577 gxf->flags |= 0x00080000; /* material is simple clip */
578 for (i = 0; i < s->nb_streams; ++i) {
579 AVStream *st = s->streams[i];
580 GXFStreamContext *sc = &gxf->streams[i];
581
582 sc->codec = st->codec;
583 sc->index = i;
584 sc->media_type = codec_get_tag(gxf_media_types, sc->codec->codec_id);
585 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
586 if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
587 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
588 return -1;
589 }
590 if (st->codec->sample_rate != 48000) {
591 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
592 return -1;
593 }
594 if (st->codec->channels != 1) {
595 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
596 return -1;
597 }
598 sc->track_type = 2;
599 sc->sample_rate = st->codec->sample_rate;
600 av_set_pts_info(st, 64, 1, sc->sample_rate);
601 sc->sample_size = 16;
602 sc->frame_rate_index = -2;
603 sc->lines_index = -2;
604 sc->fields = -2;
605 gxf->audio_tracks++;
606 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
607 fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
608 } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) {
609 /* FIXME check from time_base ? */
610 if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */
611 sc->frame_rate_index = 5;
612 sc->sample_rate = 60;
613 gxf->flags |= 0x00000080;
614 } else { /* assume PAL */
615 sc->frame_rate_index = 6;
616 sc->media_type++;
617 sc->sample_rate = 50;
618 gxf->flags |= 0x00000040;
619 }
620 gxf->sample_rate = sc->sample_rate;
621 av_set_pts_info(st, 64, 1, sc->sample_rate);
622 if (gxf_find_lines_index(sc) < 0)
623 sc->lines_index = -1;
624 sc->sample_size = st->codec->bit_rate;
625 sc->fields = 2; /* interlaced */
626 switch (sc->codec->codec_id) {
627 case CODEC_ID_MPEG2VIDEO:
628 sc->track_type = 4;
629 gxf->mpeg_tracks++;
630 gxf->flags |= 0x00008000;
631 break;
632 case CODEC_ID_DVVIDEO:
633 if (sc->codec->pix_fmt == PIX_FMT_YUV422P) {
634 sc->media_type += 2;
635 sc->track_type = 6;
636 gxf->flags |= 0x00002000;
637 } else {
638 sc->track_type = 5;
639 gxf->flags |= 0x00001000;
640 }
641 break;
642 default:
643 av_log(NULL, AV_LOG_ERROR, "video codec not supported\n");
644 return -1;
645 }
646 }
647 }
648 gxf_write_map_packet(pb, gxf);
649 //gxf_write_flt_packet(pb, gxf);
650 gxf_write_umf_packet(pb, gxf);
651 put_flush_packet(pb);
652 return 0;
653 }
654
655 static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
656 {
657 offset_t pos = url_ftell(pb);
658
659 gxf_write_packet_header(pb, PKT_EOS);
660 return updatePacketSize(pb, pos);
661 }
662
663 static int gxf_write_trailer(AVFormatContext *s)
664 {
665 ByteIOContext *pb = &s->pb;
666 GXFContext *gxf = s->priv_data;
667 offset_t end;
668 int i;
669
670 for (i = 0; i < s->nb_streams; ++i) {
671 if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
672 fifo_free(&gxf->streams[i].audio_buffer);
673 }
674 if (s->streams[i]->codec->frame_number > gxf->nb_frames)
675 gxf->nb_frames = 2 * s->streams[i]->codec->frame_number;
676 }
677
678 gxf_write_eos_packet(pb, gxf);
679 end = url_ftell(pb);
680 url_fseek(pb, 0, SEEK_SET);
681 /* overwrite map and umf packets with new values */
682 gxf_write_map_packet(pb, gxf);
683 //gxf_write_flt_packet(pb, gxf);
684 gxf_write_umf_packet(pb, gxf);
685 url_fseek(pb, end, SEEK_SET);
686 return 0;
687 }
688
689 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
690 {
691 uint32_t c=-1;
692 int i;
693 for(i=0; i<size-4 && c!=0x100; i++){
694 c = (c<<8) + buf[i];
695 if(c == 0x1B8) /* GOP start code */
696 sc->closed_gop= (buf[i+4]>>6)&1;
697 }
698 return (buf[i+1]>>3)&7;
699 }
700
701 static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size)
702 {
703 GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
704 int64_t dts = av_rescale(pkt->dts, ctx->sample_rate, sc->sample_rate);
705
706 put_byte(pb, sc->media_type);
707 put_byte(pb, sc->index);
708 put_be32(pb, dts);
709 if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
710 put_be16(pb, 0);
711 put_be16(pb, size / 2);
712 } else if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
713 int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
714 if (frame_type == FF_I_TYPE) {
715 put_byte(pb, 0x0d);
716 sc->iframes++;
717 } else if (frame_type == FF_B_TYPE) {
718 put_byte(pb, 0x0f);
719 sc->bframes++;
720 } else {
721 put_byte(pb, 0x0e);
722 sc->pframes++;
723 }
724 put_be24(pb, size);
725 } else if (sc->codec->codec_id == CODEC_ID_DVVIDEO) {
726 put_byte(pb, size / 4096);
727 put_be24(pb, 0);
728 } else
729 put_be32(pb, size);
730 put_be32(pb, dts);
731 put_byte(pb, 1); /* flags */
732 put_byte(pb, 0); /* reserved */
733 return 16;
734 }
735
736 static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt)
737 {
738 GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
739 offset_t pos = url_ftell(pb);
740 int padding = 0;
741
742 gxf_write_packet_header(pb, PKT_MEDIA);
743 if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
744 padding = 4 - pkt->size % 4;
745 else if (sc->codec->codec_type == CODEC_TYPE_AUDIO)
746 padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
747 gxf_write_media_preamble(pb, ctx, pkt, pkt->size + padding);
748 put_buffer(pb, pkt->data, pkt->size);
749 gxf_write_padding(pb, padding);
750 return updatePacketSize(pb, pos);
751 }
752
753 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
754 {
755 GXFContext *gxf = s->priv_data;
756
757 gxf_write_media_packet(&s->pb, gxf, pkt);
758 put_flush_packet(&s->pb);
759 return 0;
760 }
761
762 static int gxf_new_audio_packet(GXFContext *gxf, GXFStreamContext *sc, AVPacket *pkt, int flush)
763 {
764 int size = flush ? fifo_size(&sc->audio_buffer, NULL) : GXF_AUDIO_PACKET_SIZE;
765
766 if (!size)
767 return 0;
768 av_new_packet(pkt, size);
769 fifo_read(&sc->audio_buffer, pkt->data, size, NULL);
770 pkt->stream_index = sc->index;
771 pkt->dts = sc->current_dts;
772 sc->current_dts += size / 2; /* we only support 16 bit pcm mono for now */
773 return size;
774 }
775
776 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
777 {
778 GXFContext *gxf = s->priv_data;
779 AVPacket new_pkt;
780 int i;
781
782 for (i = 0; i < s->nb_streams; i++) {
783 if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
784 GXFStreamContext *sc = &gxf->streams[i];
785 if (pkt && pkt->stream_index == i) {
786 fifo_write(&sc->audio_buffer, pkt->data, pkt->size, NULL);
787 pkt = NULL;
788 }
789 if (flush || fifo_size(&sc->audio_buffer, NULL) >= GXF_AUDIO_PACKET_SIZE) {
790 if (gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) {
791 pkt = &new_pkt;
792 break; /* add pkt right now into list */
793 }
794 }
795 }
796 }
797 return av_interleave_packet_per_dts(s, out, pkt, flush);
798 }
799
800 AVOutputFormat gxf_muxer = {
801 "gxf",
802 "GXF format",
803 NULL,
804 "gxf",
805 sizeof(GXFContext),
806 CODEC_ID_PCM_S16LE,
807 CODEC_ID_MPEG2VIDEO,
808 gxf_write_header,
809 gxf_write_packet,
810 gxf_write_trailer,
811 0,
812 NULL,
813 gxf_interleave_packet,
814 };