3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/intfloat.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/timecode.h"
30 #include "audiointerleave.h"
32 #define GXF_AUDIO_PACKET_SIZE 65536
34 #define GXF_TIMECODE(c, d, h, m, s, f) \
35 ((c) << 30 | (d) << 29 | (h) << 24 | (m) << 16 | (s) << 8 | (f))
37 typedef struct GXFTimecode{
46 typedef struct GXFStreamContext {
47 AudioInterleaveContext aic;
60 int b_per_i_or_p; ///< number of B frames per I frame or P frame
62 unsigned order; ///< interleaving order
65 typedef struct GXFContext {
68 uint16_t audio_tracks;
70 int64_t creation_time;
71 uint32_t umf_start_offset;
72 uint32_t umf_track_offset;
73 uint32_t umf_media_offset;
75 uint16_t umf_track_size;
76 uint16_t umf_media_size;
79 GXFStreamContext timecode_track;
80 unsigned *flt_entries; ///< offsets of packets /1024, starts after 2nd video field
81 unsigned flt_entries_nb;
82 uint64_t *map_offsets; ///< offset of map packets
83 unsigned map_offsets_nb;
84 unsigned packet_count;
91 { 480, 1 }, /* NTSC */
92 { 512, 1 }, /* NTSC + VBI */
94 { 608, 2 }, /* PAL + VBI */
99 static const AVCodecTag gxf_media_types[] = {
100 { AV_CODEC_ID_MJPEG , 3 }, /* NTSC */
101 { AV_CODEC_ID_MJPEG , 4 }, /* PAL */
102 { AV_CODEC_ID_PCM_S24LE , 9 },
103 { AV_CODEC_ID_PCM_S16LE , 10 },
104 { AV_CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
105 { AV_CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
106 { AV_CODEC_ID_DVVIDEO , 13 }, /* NTSC */
107 { AV_CODEC_ID_DVVIDEO , 14 }, /* PAL */
108 { AV_CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
109 { AV_CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
110 { AV_CODEC_ID_AC3 , 17 },
111 //{ AV_CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
112 { AV_CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
113 { AV_CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
114 { AV_CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
115 { AV_CODEC_ID_NONE, 0 },
118 #define SERVER_PATH "EXT:/PDR/default/"
119 #define ES_NAME_PATTERN "EXT:/PDR/default/ES."
121 static int gxf_find_lines_index(AVStream *st)
123 GXFStreamContext *sc = st->priv_data;
126 for (i = 0; i < 6; ++i) {
127 if (st->codec->height == gxf_lines_tab[i].height) {
128 sc->lines_index = gxf_lines_tab[i].index;
135 static void gxf_write_padding(AVIOContext *pb, int64_t to_pad)
137 for (; to_pad > 0; to_pad--) {
142 static int64_t updatePacketSize(AVIOContext *pb, int64_t pos)
147 size = avio_tell(pb) - pos;
149 gxf_write_padding(pb, 4 - size % 4);
150 size = avio_tell(pb) - pos;
152 curpos = avio_tell(pb);
153 avio_seek(pb, pos + 6, SEEK_SET);
155 avio_seek(pb, curpos, SEEK_SET);
159 static int64_t updateSize(AVIOContext *pb, int64_t pos)
163 curpos = avio_tell(pb);
164 avio_seek(pb, pos, SEEK_SET);
165 avio_wb16(pb, curpos - pos - 2);
166 avio_seek(pb, curpos, SEEK_SET);
170 static void gxf_write_packet_header(AVIOContext *pb, GXFPktType type)
172 avio_wb32(pb, 0); /* packet leader for synchro */
174 avio_w8(pb, type); /* map packet */
175 avio_wb32(pb, 0); /* size */
176 avio_wb32(pb, 0); /* reserved */
177 avio_w8(pb, 0xE1); /* trailer 1 */
178 avio_w8(pb, 0xE2); /* trailer 2 */
181 static int gxf_write_mpeg_auxiliary(AVIOContext *pb, AVStream *st)
183 GXFStreamContext *sc = st->priv_data;
185 int size, starting_line;
188 sc->p_per_gop = sc->pframes / sc->iframes;
189 if (sc->pframes % sc->iframes)
192 sc->b_per_i_or_p = sc->bframes / sc->pframes;
193 if (sc->bframes % sc->pframes)
196 if (sc->p_per_gop > 9)
197 sc->p_per_gop = 9; /* ensure value won't take more than one char */
198 if (sc->b_per_i_or_p > 9)
199 sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */
201 if (st->codec->height == 512 || st->codec->height == 608)
202 starting_line = 7; // VBI
203 else if (st->codec->height == 480)
206 starting_line = 23; // default PAL
208 size = snprintf(buffer, sizeof(buffer), "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
209 "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
210 (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
211 st->codec->pix_fmt == AV_PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
212 starting_line, (st->codec->height + 15) / 16);
213 av_assert0(size < sizeof(buffer));
214 avio_w8(pb, TRACK_MPG_AUX);
215 avio_w8(pb, size + 1);
216 avio_write(pb, (uint8_t *)buffer, size + 1);
220 static int gxf_write_dv_auxiliary(AVIOContext *pb, AVStream *st)
222 int64_t track_aux_data = 0;
224 avio_w8(pb, TRACK_AUX);
226 if (st->codec->pix_fmt == PIX_FMT_YUV420P)
227 track_aux_data |= 0x01; /* marks stream as DVCAM instead of DVPRO */
228 track_aux_data |= 0x40000000; /* aux data is valid */
229 avio_wl64(pb, track_aux_data);
233 static int gxf_write_timecode_auxiliary(AVIOContext *pb, GXFContext *gxf)
235 uint32_t timecode = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
236 gxf->tc.hh, gxf->tc.mm,
237 gxf->tc.ss, gxf->tc.ff);
239 avio_w8(pb, TRACK_AUX);
241 avio_wl32(pb, timecode);
247 static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
249 GXFContext *gxf = s->priv_data;
250 AVIOContext *pb = s->pb;
253 /* track description section */
254 avio_w8(pb, sc->media_type + 0x80);
255 avio_w8(pb, index + 0xC0);
258 avio_wb16(pb, 0); /* size */
260 /* media file name */
261 avio_w8(pb, TRACK_NAME);
262 avio_w8(pb, strlen(ES_NAME_PATTERN) + 3);
263 avio_write(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN) - 1);
264 avio_wb16(pb, sc->media_info);
267 switch (sc->track_type) {
268 case 3: /* timecode */
269 gxf_write_timecode_auxiliary(pb, gxf);
273 gxf_write_mpeg_auxiliary(pb, s->streams[index]);
277 gxf_write_dv_auxiliary(pb, s->streams[index]);
280 avio_w8(pb, TRACK_AUX);
285 /* file system version */
286 avio_w8(pb, TRACK_VER);
291 avio_w8(pb, TRACK_FPS);
293 avio_wb32(pb, sc->frame_rate_index);
295 /* lines per frame */
296 avio_w8(pb, TRACK_LINES);
298 avio_wb32(pb, sc->lines_index);
300 /* fields per frame */
301 avio_w8(pb, TRACK_FPF);
303 avio_wb32(pb, sc->fields);
305 return updateSize(pb, pos);
308 static int gxf_write_material_data_section(AVFormatContext *s)
310 GXFContext *gxf = s->priv_data;
311 AVIOContext *pb = s->pb;
314 const char *filename = strrchr(s->filename, '/');
317 avio_wb16(pb, 0); /* size */
323 filename = s->filename;
324 len = strlen(filename);
326 avio_w8(pb, MAT_NAME);
327 avio_w8(pb, strlen(SERVER_PATH) + len + 1);
328 avio_write(pb, SERVER_PATH, sizeof(SERVER_PATH) - 1);
329 avio_write(pb, filename, len);
333 avio_w8(pb, MAT_FIRST_FIELD);
338 avio_w8(pb, MAT_LAST_FIELD);
340 avio_wb32(pb, gxf->nb_fields);
343 avio_w8(pb, MAT_MARK_IN);
347 avio_w8(pb, MAT_MARK_OUT);
349 avio_wb32(pb, gxf->nb_fields);
352 avio_w8(pb, MAT_SIZE);
354 avio_wb32(pb, avio_size(pb) / 1024);
356 return updateSize(pb, pos);
359 static int gxf_write_track_description_section(AVFormatContext *s)
361 GXFContext *gxf = s->priv_data;
362 AVIOContext *pb = s->pb;
367 avio_wb16(pb, 0); /* size */
368 for (i = 0; i < s->nb_streams; ++i)
369 gxf_write_track_description(s, s->streams[i]->priv_data, i);
371 gxf_write_track_description(s, &gxf->timecode_track, s->nb_streams);
373 return updateSize(pb, pos);
376 static int gxf_write_map_packet(AVFormatContext *s, int rewrite)
378 GXFContext *gxf = s->priv_data;
379 AVIOContext *pb = s->pb;
380 int64_t pos = avio_tell(pb);
383 if (!(gxf->map_offsets_nb % 30)) {
384 gxf->map_offsets = av_realloc_f(gxf->map_offsets,
385 sizeof(*gxf->map_offsets),
386 gxf->map_offsets_nb+30);
387 if (!gxf->map_offsets) {
388 av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n");
392 gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here
395 gxf_write_packet_header(pb, PKT_MAP);
398 avio_w8(pb, 0xE0); /* version */
399 avio_w8(pb, 0xFF); /* reserved */
401 gxf_write_material_data_section(s);
402 gxf_write_track_description_section(s);
404 return updatePacketSize(pb, pos);
407 static int gxf_write_flt_packet(AVFormatContext *s)
409 GXFContext *gxf = s->priv_data;
410 AVIOContext *pb = s->pb;
411 int64_t pos = avio_tell(pb);
412 int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
413 int flt_entries = gxf->nb_fields / fields_per_flt;
416 gxf_write_packet_header(pb, PKT_FLT);
418 avio_wl32(pb, fields_per_flt); /* number of fields */
419 avio_wl32(pb, flt_entries); /* number of active flt entries */
421 if (gxf->flt_entries) {
422 for (i = 0; i < flt_entries; i++)
423 avio_wl32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
426 for (; i < 1000; i++)
429 return updatePacketSize(pb, pos);
432 static int gxf_write_umf_material_description(AVFormatContext *s)
434 GXFContext *gxf = s->priv_data;
435 AVIOContext *pb = s->pb;
436 int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
437 int64_t timestamp = 0;
438 AVDictionaryEntry *t;
440 uint32_t timecode_in; // timecode at mark in
441 uint32_t timecode_out; // timecode at mark out
443 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
444 timestamp = ff_iso8601_to_unix_time(t->value);
446 timecode_in = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
447 gxf->tc.hh, gxf->tc.mm,
448 gxf->tc.ss, gxf->tc.ff);
450 nb_fields = gxf->nb_fields +
451 gxf->tc.hh * (timecode_base * 3600) +
452 gxf->tc.mm * (timecode_base * 60) +
453 gxf->tc.ss * timecode_base +
456 timecode_out = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
457 nb_fields / (timecode_base * 3600) % 24,
458 nb_fields / (timecode_base * 60) % 60,
459 nb_fields / timecode_base % 60,
460 nb_fields % timecode_base);
462 avio_wl32(pb, gxf->flags);
463 avio_wl32(pb, gxf->nb_fields); /* length of the longest track */
464 avio_wl32(pb, gxf->nb_fields); /* length of the shortest track */
465 avio_wl32(pb, 0); /* mark in */
466 avio_wl32(pb, gxf->nb_fields); /* mark out */
467 avio_wl32(pb, timecode_in); /* timecode mark in */
468 avio_wl32(pb, timecode_out); /* timecode mark out */
469 avio_wl64(pb, timestamp); /* modification time */
470 avio_wl64(pb, timestamp); /* creation time */
471 avio_wl16(pb, 0); /* reserved */
472 avio_wl16(pb, 0); /* reserved */
473 avio_wl16(pb, gxf->audio_tracks);
474 avio_wl16(pb, 1); /* timecode track count */
475 avio_wl16(pb, 0); /* reserved */
476 avio_wl16(pb, gxf->mpeg_tracks);
480 static int gxf_write_umf_payload(AVFormatContext *s)
482 GXFContext *gxf = s->priv_data;
483 AVIOContext *pb = s->pb;
485 avio_wl32(pb, gxf->umf_length); /* total length of the umf data */
486 avio_wl32(pb, 3); /* version */
487 avio_wl32(pb, s->nb_streams+1);
488 avio_wl32(pb, gxf->umf_track_offset); /* umf track section offset */
489 avio_wl32(pb, gxf->umf_track_size);
490 avio_wl32(pb, s->nb_streams+1);
491 avio_wl32(pb, gxf->umf_media_offset);
492 avio_wl32(pb, gxf->umf_media_size);
493 avio_wl32(pb, gxf->umf_length); /* user data offset */
494 avio_wl32(pb, 0); /* user data size */
495 avio_wl32(pb, 0); /* reserved */
496 avio_wl32(pb, 0); /* reserved */
500 static int gxf_write_umf_track_description(AVFormatContext *s)
502 AVIOContext *pb = s->pb;
503 GXFContext *gxf = s->priv_data;
504 int64_t pos = avio_tell(pb);
507 gxf->umf_track_offset = pos - gxf->umf_start_offset;
508 for (i = 0; i < s->nb_streams; ++i) {
509 GXFStreamContext *sc = s->streams[i]->priv_data;
510 avio_wl16(pb, sc->media_info);
514 avio_wl16(pb, gxf->timecode_track.media_info);
517 return avio_tell(pb) - pos;
520 static int gxf_write_umf_media_mpeg(AVIOContext *pb, AVStream *st)
522 GXFStreamContext *sc = st->priv_data;
524 if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P)
527 avio_wl32(pb, 1); /* default to 420 */
528 avio_wl32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
529 avio_wl32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
530 avio_wl32(pb, 1); /* I picture per GOP */
531 avio_wl32(pb, sc->p_per_gop);
532 avio_wl32(pb, sc->b_per_i_or_p);
533 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
535 else if (st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO)
539 avio_wl32(pb, 0); /* reserved */
543 static int gxf_write_umf_media_timecode(AVIOContext *pb, int drop)
545 avio_wl32(pb, drop); /* drop frame */
546 avio_wl32(pb, 0); /* reserved */
547 avio_wl32(pb, 0); /* reserved */
548 avio_wl32(pb, 0); /* reserved */
549 avio_wl32(pb, 0); /* reserved */
550 avio_wl32(pb, 0); /* reserved */
551 avio_wl32(pb, 0); /* reserved */
552 avio_wl32(pb, 0); /* reserved */
556 static int gxf_write_umf_media_dv(AVIOContext *pb, GXFStreamContext *sc, AVStream *st)
560 if (st->codec->pix_fmt == PIX_FMT_YUV420P)
561 dv_umf_data |= 0x20; /* marks as DVCAM instead of DVPRO */
562 avio_wl32(pb, dv_umf_data);
573 static int gxf_write_umf_media_audio(AVIOContext *pb, GXFStreamContext *sc)
575 avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
576 avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
577 avio_wl32(pb, 0); /* number of fields over which to ramp up sound level */
578 avio_wl32(pb, 0); /* number of fields over which to ramp down sound level */
579 avio_wl32(pb, 0); /* reserved */
580 avio_wl32(pb, 0); /* reserved */
584 static int gxf_write_umf_media_description(AVFormatContext *s)
586 GXFContext *gxf = s->priv_data;
587 AVIOContext *pb = s->pb;
592 gxf->umf_media_offset = pos - gxf->umf_start_offset;
593 for (i = 0; i <= s->nb_streams; ++i) {
594 GXFStreamContext *sc;
595 int64_t startpos, curpos;
597 if (i == s->nb_streams)
598 sc = &gxf->timecode_track;
600 sc = s->streams[i]->priv_data;
602 startpos = avio_tell(pb);
603 avio_wl16(pb, 0); /* length */
604 avio_wl16(pb, sc->media_info);
605 avio_wl16(pb, 0); /* reserved */
606 avio_wl16(pb, 0); /* reserved */
607 avio_wl32(pb, gxf->nb_fields);
608 avio_wl32(pb, 0); /* attributes rw, ro */
609 avio_wl32(pb, 0); /* mark in */
610 avio_wl32(pb, gxf->nb_fields); /* mark out */
611 avio_write(pb, ES_NAME_PATTERN, strlen(ES_NAME_PATTERN));
612 avio_wb16(pb, sc->media_info);
613 for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
615 avio_wl32(pb, sc->track_type);
616 avio_wl32(pb, sc->sample_rate);
617 avio_wl32(pb, sc->sample_size);
618 avio_wl32(pb, 0); /* reserved */
620 if (sc == &gxf->timecode_track)
621 gxf_write_umf_media_timecode(pb, gxf->tc.drop);
623 AVStream *st = s->streams[i];
624 switch (st->codec->codec_id) {
625 case AV_CODEC_ID_MPEG1VIDEO:
626 case AV_CODEC_ID_MPEG2VIDEO:
627 gxf_write_umf_media_mpeg(pb, st);
629 case AV_CODEC_ID_PCM_S16LE:
630 gxf_write_umf_media_audio(pb, sc);
632 case AV_CODEC_ID_DVVIDEO:
633 gxf_write_umf_media_dv(pb, sc, st);
638 curpos = avio_tell(pb);
639 avio_seek(pb, startpos, SEEK_SET);
640 avio_wl16(pb, curpos - startpos);
641 avio_seek(pb, curpos, SEEK_SET);
643 return avio_tell(pb) - pos;
646 static int gxf_write_umf_packet(AVFormatContext *s)
648 GXFContext *gxf = s->priv_data;
649 AVIOContext *pb = s->pb;
650 int64_t pos = avio_tell(pb);
652 gxf_write_packet_header(pb, PKT_UMF);
655 avio_w8(pb, 3); /* first and last (only) packet */
656 avio_wb32(pb, gxf->umf_length); /* data length */
658 gxf->umf_start_offset = avio_tell(pb);
659 gxf_write_umf_payload(s);
660 gxf_write_umf_material_description(s);
661 gxf->umf_track_size = gxf_write_umf_track_description(s);
662 gxf->umf_media_size = gxf_write_umf_media_description(s);
663 gxf->umf_length = avio_tell(pb) - gxf->umf_start_offset;
664 return updatePacketSize(pb, pos);
667 static const int GXF_samples_per_frame[] = { 32768, 0 };
669 static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
674 sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
675 sc->sample_rate = vsc->sample_rate;
676 sc->media_info = ('T'<<8) | '0';
678 sc->frame_rate_index = vsc->frame_rate_index;
679 sc->lines_index = vsc->lines_index;
680 sc->sample_size = 16;
681 sc->fields = vsc->fields;
684 static int gxf_init_timecode(AVFormatContext *s, GXFTimecode *tc, const char *tcstr, int fields)
688 if (sscanf(tcstr, "%d:%d:%d%c%d", &tc->hh, &tc->mm, &tc->ss, &c, &tc->ff) != 5) {
689 av_log(s, AV_LOG_ERROR, "unable to parse timecode, "
690 "syntax: hh:mm:ss[:;.]ff\n");
703 static int gxf_write_header(AVFormatContext *s)
705 AVIOContext *pb = s->pb;
706 GXFContext *gxf = s->priv_data;
707 GXFStreamContext *vsc = NULL;
708 uint8_t tracks[255] = {0};
709 int i, media_info = 0;
710 AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
713 av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome\n");
717 gxf->flags |= 0x00080000; /* material is simple clip */
718 for (i = 0; i < s->nb_streams; ++i) {
719 AVStream *st = s->streams[i];
720 GXFStreamContext *sc = av_mallocz(sizeof(*sc));
722 return AVERROR(ENOMEM);
725 sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
726 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
727 if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE) {
728 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
731 if (st->codec->sample_rate != 48000) {
732 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
735 if (st->codec->channels != 1) {
736 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
740 sc->sample_rate = st->codec->sample_rate;
741 avpriv_set_pts_info(st, 64, 1, sc->sample_rate);
742 sc->sample_size = 16;
743 sc->frame_rate_index = -2;
744 sc->lines_index = -2;
747 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
749 } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
751 av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
754 /* FIXME check from time_base ? */
755 if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
756 sc->frame_rate_index = 5;
757 sc->sample_rate = 60;
758 gxf->flags |= 0x00000080;
759 gxf->time_base = (AVRational){ 1001, 60000 };
760 } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
761 sc->frame_rate_index = 6;
763 sc->sample_rate = 50;
764 gxf->flags |= 0x00000040;
765 gxf->time_base = (AVRational){ 1, 50 };
767 av_log(s, AV_LOG_ERROR, "unsupported video resolution, "
768 "gxf muxer only accepts PAL or NTSC resolutions currently\n");
772 tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
773 avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
774 if (gxf_find_lines_index(st) < 0)
775 sc->lines_index = -1;
776 sc->sample_size = st->codec->bit_rate;
777 sc->fields = 2; /* interlaced */
781 switch (st->codec->codec_id) {
782 case AV_CODEC_ID_MJPEG:
784 gxf->flags |= 0x00004000;
787 case AV_CODEC_ID_MPEG1VIDEO:
792 case AV_CODEC_ID_MPEG2VIDEO:
793 sc->first_gop_closed = -1;
796 gxf->flags |= 0x00008000;
799 case AV_CODEC_ID_DVVIDEO:
800 if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P) {
803 gxf->flags |= 0x00002000;
807 gxf->flags |= 0x00001000;
812 av_log(s, AV_LOG_ERROR, "video codec not supported\n");
816 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
817 sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
818 sc->order = s->nb_streams - st->index;
821 if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
825 gxf_init_timecode(s, &gxf->tc, tcr->value, vsc->fields);
827 gxf_init_timecode_track(&gxf->timecode_track, vsc);
828 gxf->flags |= 0x200000; // time code track is non-drop frame
830 gxf_write_map_packet(s, 0);
831 gxf_write_flt_packet(s);
832 gxf_write_umf_packet(s);
834 gxf->packet_count = 3;
840 static int gxf_write_eos_packet(AVIOContext *pb)
842 int64_t pos = avio_tell(pb);
844 gxf_write_packet_header(pb, PKT_EOS);
845 return updatePacketSize(pb, pos);
848 static int gxf_write_trailer(AVFormatContext *s)
850 GXFContext *gxf = s->priv_data;
851 AVIOContext *pb = s->pb;
855 ff_audio_interleave_close(s);
857 gxf_write_eos_packet(pb);
859 avio_seek(pb, 0, SEEK_SET);
860 /* overwrite map, flt and umf packets with new values */
861 gxf_write_map_packet(s, 1);
862 gxf_write_flt_packet(s);
863 gxf_write_umf_packet(s);
865 /* update duration in all map packets */
866 for (i = 1; i < gxf->map_offsets_nb; i++) {
867 avio_seek(pb, gxf->map_offsets[i], SEEK_SET);
868 gxf_write_map_packet(s, 1);
872 avio_seek(pb, end, SEEK_SET);
874 av_freep(&gxf->flt_entries);
875 av_freep(&gxf->map_offsets);
880 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
884 for(i=0; i<size-4 && c!=0x100; i++){
886 if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
887 sc->first_gop_closed= (buf[i+4]>>6)&1;
889 return (buf[i+1]>>3)&7;
892 static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
894 GXFContext *gxf = s->priv_data;
895 AVIOContext *pb = s->pb;
896 AVStream *st = s->streams[pkt->stream_index];
897 GXFStreamContext *sc = st->priv_data;
899 /* If the video is frame-encoded, the frame numbers shall be represented by
900 * even field numbers.
901 * see SMPTE360M-2004 6.4.2.1.3 Media field number */
902 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
903 field_nb = gxf->nb_fields;
905 field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
906 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
909 avio_w8(pb, sc->media_type);
910 avio_w8(pb, st->index);
911 avio_wb32(pb, field_nb);
912 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
914 avio_wb16(pb, size / 2);
915 } else if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
916 int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
917 if (frame_type == AV_PICTURE_TYPE_I) {
920 } else if (frame_type == AV_PICTURE_TYPE_B) {
928 } else if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) {
929 avio_w8(pb, size / 4096);
933 avio_wb32(pb, field_nb);
934 avio_w8(pb, 1); /* flags */
935 avio_w8(pb, 0); /* reserved */
939 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
941 GXFContext *gxf = s->priv_data;
942 AVIOContext *pb = s->pb;
943 AVStream *st = s->streams[pkt->stream_index];
944 int64_t pos = avio_tell(pb);
946 int packet_start_offset = avio_tell(pb) / 1024;
948 gxf_write_packet_header(pb, PKT_MEDIA);
949 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
950 padding = 4 - pkt->size % 4;
951 else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
952 padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
953 gxf_write_media_preamble(s, pkt, pkt->size + padding);
954 avio_write(pb, pkt->data, pkt->size);
955 gxf_write_padding(pb, padding);
957 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
958 if (!(gxf->flt_entries_nb % 500)) {
959 gxf->flt_entries = av_realloc_f(gxf->flt_entries,
960 sizeof(*gxf->flt_entries),
961 gxf->flt_entries_nb+500);
962 if (!gxf->flt_entries) {
963 av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
967 gxf->flt_entries[gxf->flt_entries_nb++] = packet_start_offset;
968 gxf->nb_fields += 2; // count fields
971 updatePacketSize(pb, pos);
974 if (gxf->packet_count == 100) {
975 gxf_write_map_packet(s, 0);
976 gxf->packet_count = 0;
982 static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
984 GXFContext *gxf = s->priv_data;
985 AVPacket *pkt[2] = { cur, next };
987 GXFStreamContext *sc[2];
989 for (i = 0; i < 2; i++) {
990 AVStream *st = s->streams[pkt[i]->stream_index];
991 sc[i] = st->priv_data;
992 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
993 field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
994 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
995 field_nb[i] &= ~1; // compare against even field number because audio must be before video
997 field_nb[i] = pkt[i]->dts; // dts are field based
1000 return field_nb[1] > field_nb[0] ||
1001 (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
1004 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
1006 if (pkt && s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1007 pkt->duration = 2; // enforce 2 fields
1008 return ff_audio_rechunk_interleave(s, out, pkt, flush,
1009 ff_interleave_packet_per_dts, gxf_compare_field_nb);
1012 AVOutputFormat ff_gxf_muxer = {
1014 .long_name = NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"),
1015 .extensions = "gxf",
1016 .priv_data_size = sizeof(GXFContext),
1017 .audio_codec = AV_CODEC_ID_PCM_S16LE,
1018 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1019 .write_header = gxf_write_header,
1020 .write_packet = gxf_write_packet,
1021 .write_trailer = gxf_write_trailer,
1022 .interleave_packet = gxf_interleave_packet,