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/fifo.h"
26 #include "audiointerleave.h"
28 #define GXF_AUDIO_PACKET_SIZE 65536
30 typedef struct GXFStreamContext {
31 AudioInterleaveContext aic;
44 int b_per_i_or_p; ///< number of B frames per I frame or P frame
46 unsigned order; ///< interleaving order
49 typedef struct GXFContext {
51 uint16_t audio_tracks;
53 int64_t creation_time;
54 uint32_t umf_start_offset;
55 uint32_t umf_track_offset;
56 uint32_t umf_media_offset;
58 uint16_t umf_track_size;
59 uint16_t umf_media_size;
62 GXFStreamContext timecode_track;
63 unsigned *flt_entries; ///< offsets of packets /1024, starts after 2nd video field
64 unsigned flt_entries_nb;
67 typedef struct GXF_Lines {
73 /* FIXME check if it is relevant */
74 static const GXF_Lines gxf_lines_tab[] = {
75 { 480, 1 }, /* NTSC */
76 { 512, 1 }, /* NTSC + VBI */
78 { 608, 2 }, /* PAL + VBI */
83 static const AVCodecTag gxf_media_types[] = {
84 { CODEC_ID_MJPEG , 3 }, /* NTSC */
85 { CODEC_ID_MJPEG , 4 }, /* PAL */
86 { CODEC_ID_PCM_S24LE , 9 },
87 { CODEC_ID_PCM_S16LE , 10 },
88 { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
89 { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
90 { CODEC_ID_DVVIDEO , 13 }, /* NTSC */
91 { CODEC_ID_DVVIDEO , 14 }, /* PAL */
92 { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
93 { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
94 { CODEC_ID_AC3 , 17 },
95 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
96 { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
97 { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
98 { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
102 #define SERVER_PATH "/space/"
103 #define ES_NAME_PATTERN "ES."
105 static int gxf_find_lines_index(AVStream *st)
107 GXFStreamContext *sc = st->priv_data;
110 for (i = 0; i < 6; ++i) {
111 if (st->codec->height == gxf_lines_tab[i].height) {
112 sc->lines_index = gxf_lines_tab[i].index;
119 static void gxf_write_padding(ByteIOContext *pb, int64_t to_pad)
121 for (; to_pad > 0; to_pad--) {
126 static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos)
131 size = url_ftell(pb) - pos;
133 gxf_write_padding(pb, 4 - size % 4);
134 size = url_ftell(pb) - pos;
136 curpos = url_ftell(pb);
137 url_fseek(pb, pos + 6, SEEK_SET);
139 url_fseek(pb, curpos, SEEK_SET);
143 static int64_t updateSize(ByteIOContext *pb, int64_t pos)
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);
154 static void gxf_write_packet_header(ByteIOContext *pb, GXFPktType type)
156 put_be32(pb, 0); /* packet leader for synchro */
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 */
165 static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, AVStream *st)
167 GXFStreamContext *sc = st->priv_data;
169 int size, starting_line;
172 sc->p_per_gop = sc->pframes / sc->iframes;
173 if (sc->pframes % sc->iframes)
176 sc->b_per_i_or_p = sc->bframes / sc->pframes;
177 if (sc->bframes % sc->pframes)
180 if (sc->p_per_gop > 9)
181 sc->p_per_gop = 9; /* ensure value won't take more than one char */
182 if (sc->b_per_i_or_p > 9)
183 sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */
185 if (st->codec->height == 512 || st->codec->height == 608)
186 starting_line = 7; // VBI
187 else if (st->codec->height == 480)
190 starting_line = 23; // default PAL
192 size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
193 "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
194 (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
195 st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
196 starting_line, st->codec->height / 16);
197 put_byte(pb, TRACK_MPG_AUX);
198 put_byte(pb, size + 1);
199 put_buffer(pb, (uint8_t *)buffer, size + 1);
203 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *sc)
205 put_byte(pb, 0); /* fields */
206 put_byte(pb, 0); /* seconds */
207 put_byte(pb, 0); /* minutes */
208 put_byte(pb, 0); /* flags + hours */
214 static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
216 ByteIOContext *pb = s->pb;
218 int mpeg = sc->track_type == 4 || sc->track_type == 9;
220 /* track description section */
221 put_byte(pb, sc->media_type + 0x80);
222 put_byte(pb, index + 0xC0);
225 put_be16(pb, 0); /* size */
227 /* media file name */
228 put_byte(pb, TRACK_NAME);
229 put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
230 put_tag(pb, ES_NAME_PATTERN);
231 put_be16(pb, sc->media_info);
235 /* auxiliary information */
236 put_byte(pb, TRACK_AUX);
238 if (sc->track_type == 3)
239 gxf_write_timecode_auxiliary(pb, sc);
244 /* file system version */
245 put_byte(pb, TRACK_VER);
250 gxf_write_mpeg_auxiliary(pb, s->streams[index]);
253 put_byte(pb, TRACK_FPS);
255 put_be32(pb, sc->frame_rate_index);
257 /* lines per frame */
258 put_byte(pb, TRACK_LINES);
260 put_be32(pb, sc->lines_index);
262 /* fields per frame */
263 put_byte(pb, TRACK_FPF);
265 put_be32(pb, sc->fields);
267 return updateSize(pb, pos);
270 static int gxf_write_material_data_section(AVFormatContext *s)
272 GXFContext *gxf = s->priv_data;
273 ByteIOContext *pb = s->pb;
275 const char *filename = strrchr(s->filename, '/');
278 put_be16(pb, 0); /* size */
284 filename = s->filename;
285 put_byte(pb, MAT_NAME);
286 put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
287 put_tag(pb, SERVER_PATH);
288 put_tag(pb, filename);
292 put_byte(pb, MAT_FIRST_FIELD);
297 put_byte(pb, MAT_LAST_FIELD);
299 put_be32(pb, gxf->nb_fields);
302 put_byte(pb, MAT_MARK_IN);
306 put_byte(pb, MAT_MARK_OUT);
308 put_be32(pb, gxf->nb_fields);
311 put_byte(pb, MAT_SIZE);
313 put_be32(pb, url_fsize(pb) / 1024);
315 return updateSize(pb, pos);
318 static int gxf_write_track_description_section(AVFormatContext *s)
320 GXFContext *gxf = s->priv_data;
321 ByteIOContext *pb = s->pb;
326 put_be16(pb, 0); /* size */
327 for (i = 0; i < s->nb_streams; ++i)
328 gxf_write_track_description(s, s->streams[i]->priv_data, i);
330 gxf_write_track_description(s, &gxf->timecode_track, s->nb_streams);
332 return updateSize(pb, pos);
335 static int gxf_write_map_packet(AVFormatContext *s)
337 ByteIOContext *pb = s->pb;
338 int64_t pos = url_ftell(pb);
340 gxf_write_packet_header(pb, PKT_MAP);
343 put_byte(pb, 0xE0); /* version */
344 put_byte(pb, 0xFF); /* reserved */
346 gxf_write_material_data_section(s);
347 gxf_write_track_description_section(s);
349 return updatePacketSize(pb, pos);
352 static int gxf_write_flt_packet(AVFormatContext *s)
354 GXFContext *gxf = s->priv_data;
355 ByteIOContext *pb = s->pb;
356 int64_t pos = url_ftell(pb);
357 int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
358 int flt_entries = gxf->nb_fields / fields_per_flt - 1;
361 gxf_write_packet_header(pb, PKT_FLT);
363 put_le32(pb, fields_per_flt); /* number of fields */
364 put_le32(pb, flt_entries); /* number of active flt entries */
366 if (gxf->flt_entries) {
367 for (i = 0; i < flt_entries; i++)
368 put_le32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
371 for (; i < 1000; i++)
374 return updatePacketSize(pb, pos);
377 static int gxf_write_umf_material_description(AVFormatContext *s)
379 GXFContext *gxf = s->priv_data;
380 ByteIOContext *pb = s->pb;
381 int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
385 gxf->nb_fields / (timecode_base * 3600) % 24 << 24 | // hours
386 gxf->nb_fields / (timecode_base * 60) % 60 << 16 | // minutes
387 gxf->nb_fields / timecode_base % 60 << 8 | // seconds
388 gxf->nb_fields % timecode_base; // fields
390 put_le32(pb, gxf->flags);
391 put_le32(pb, gxf->nb_fields); /* length of the longest track */
392 put_le32(pb, gxf->nb_fields); /* length of the shortest track */
393 put_le32(pb, 0); /* mark in */
394 put_le32(pb, gxf->nb_fields); /* mark out */
395 put_le32(pb, 0); /* timecode mark in */
396 put_le32(pb, timecode); /* timecode mark out */
397 put_le64(pb, s->timestamp); /* modification time */
398 put_le64(pb, s->timestamp); /* creation time */
399 put_le16(pb, 0); /* reserved */
400 put_le16(pb, 0); /* reserved */
401 put_le16(pb, gxf->audio_tracks);
402 put_le16(pb, 1); /* timecode track count */
403 put_le16(pb, 0); /* reserved */
404 put_le16(pb, gxf->mpeg_tracks);
408 static int gxf_write_umf_payload(AVFormatContext *s)
410 GXFContext *gxf = s->priv_data;
411 ByteIOContext *pb = s->pb;
413 put_le32(pb, gxf->umf_length); /* total length of the umf data */
414 put_le32(pb, 3); /* version */
415 put_le32(pb, s->nb_streams+1);
416 put_le32(pb, gxf->umf_track_offset); /* umf track section offset */
417 put_le32(pb, gxf->umf_track_size);
418 put_le32(pb, s->nb_streams+1);
419 put_le32(pb, gxf->umf_media_offset);
420 put_le32(pb, gxf->umf_media_size);
421 put_le32(pb, gxf->umf_length); /* user data offset */
422 put_le32(pb, 0); /* user data size */
423 put_le32(pb, 0); /* reserved */
424 put_le32(pb, 0); /* reserved */
428 static int gxf_write_umf_track_description(AVFormatContext *s)
430 ByteIOContext *pb = s->pb;
431 GXFContext *gxf = s->priv_data;
432 int64_t pos = url_ftell(pb);
435 gxf->umf_track_offset = pos - gxf->umf_start_offset;
436 for (i = 0; i < s->nb_streams; ++i) {
437 GXFStreamContext *sc = s->streams[i]->priv_data;
438 put_le16(pb, sc->media_info);
442 put_le16(pb, gxf->timecode_track.media_info);
445 return url_ftell(pb) - pos;
448 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, AVStream *st)
450 GXFStreamContext *sc = st->priv_data;
452 if (st->codec->pix_fmt == PIX_FMT_YUV422P)
455 put_le32(pb, 1); /* default to 420 */
456 put_le32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
457 put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
458 put_le32(pb, 1); /* I picture per GOP */
459 put_le32(pb, sc->p_per_gop);
460 put_le32(pb, sc->b_per_i_or_p);
461 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
463 else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO)
467 put_le32(pb, 0); /* reserved */
471 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *sc)
473 put_le32(pb, 1); /* non drop frame */
474 put_le32(pb, 0); /* reserved */
475 put_le32(pb, 0); /* reserved */
476 put_le32(pb, 0); /* reserved */
477 put_le32(pb, 0); /* reserved */
478 put_le32(pb, 0); /* reserved */
479 put_le32(pb, 0); /* reserved */
480 put_le32(pb, 0); /* reserved */
484 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *sc)
488 for (i = 0; i < 8; i++) {
494 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *sc)
496 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
497 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
498 put_le32(pb, 0); /* number of fields over which to ramp up sound level */
499 put_le32(pb, 0); /* number of fields over which to ramp down sound level */
500 put_le32(pb, 0); /* reserved */
501 put_le32(pb, 0); /* reserved */
506 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *sc)
508 put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
509 put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
510 put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
511 put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
516 static int gxf_write_umf_media_description(AVFormatContext *s)
518 GXFContext *gxf = s->priv_data;
519 ByteIOContext *pb = s->pb;
524 gxf->umf_media_offset = pos - gxf->umf_start_offset;
525 for (i = 0; i <= s->nb_streams; ++i) {
526 GXFStreamContext *sc;
528 int64_t startpos, curpos;
529 int path_size = strlen(ES_NAME_PATTERN);
531 if (i == s->nb_streams)
532 sc = &gxf->timecode_track;
534 sc = s->streams[i]->priv_data;
536 memset(buffer, 0, 88);
537 startpos = url_ftell(pb);
538 put_le16(pb, 0); /* length */
539 put_le16(pb, sc->media_info);
540 put_le16(pb, 0); /* reserved */
541 put_le16(pb, 0); /* reserved */
542 put_le32(pb, gxf->nb_fields);
543 put_le32(pb, 0); /* attributes rw, ro */
544 put_le32(pb, 0); /* mark in */
545 put_le32(pb, gxf->nb_fields); /* mark out */
546 strncpy(buffer, ES_NAME_PATTERN, path_size);
547 put_buffer(pb, (uint8_t *)buffer, path_size);
548 put_be16(pb, sc->media_info);
549 put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
550 put_le32(pb, sc->track_type);
551 put_le32(pb, sc->sample_rate);
552 put_le32(pb, sc->sample_size);
553 put_le32(pb, 0); /* reserved */
555 if (sc == &gxf->timecode_track)
556 gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
558 AVStream *st = s->streams[i];
559 switch (st->codec->codec_id) {
560 case CODEC_ID_MPEG2VIDEO:
561 gxf_write_umf_media_mpeg(pb, st);
563 case CODEC_ID_PCM_S16LE:
564 gxf_write_umf_media_audio(pb, sc);
566 case CODEC_ID_DVVIDEO:
567 gxf_write_umf_media_dv(pb, sc);
572 curpos = url_ftell(pb);
573 url_fseek(pb, startpos, SEEK_SET);
574 put_le16(pb, curpos - startpos);
575 url_fseek(pb, curpos, SEEK_SET);
577 return url_ftell(pb) - pos;
580 static int gxf_write_umf_packet(AVFormatContext *s)
582 GXFContext *gxf = s->priv_data;
583 ByteIOContext *pb = s->pb;
584 int64_t pos = url_ftell(pb);
586 gxf_write_packet_header(pb, PKT_UMF);
589 put_byte(pb, 3); /* first and last (only) packet */
590 put_be32(pb, gxf->umf_length); /* data length */
592 gxf->umf_start_offset = url_ftell(pb);
593 gxf_write_umf_payload(s);
594 gxf_write_umf_material_description(s);
595 gxf->umf_track_size = gxf_write_umf_track_description(s);
596 gxf->umf_media_size = gxf_write_umf_media_description(s);
597 gxf->umf_length = url_ftell(pb) - gxf->umf_start_offset;
598 return updatePacketSize(pb, pos);
601 static const int GXF_samples_per_frame[] = { 32768, 0 };
603 static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
608 sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
609 sc->sample_rate = vsc->sample_rate;
610 sc->media_info = ('T'<<8) | '0';
612 sc->frame_rate_index = vsc->frame_rate_index;
613 sc->lines_index = vsc->lines_index;
614 sc->sample_size = 16;
615 sc->fields = vsc->fields;
618 static int gxf_write_header(AVFormatContext *s)
620 ByteIOContext *pb = s->pb;
621 GXFContext *gxf = s->priv_data;
622 GXFStreamContext *vsc = NULL;
623 uint8_t tracks[255] = {0};
624 int i, media_info = 0;
626 gxf->flags |= 0x00080000; /* material is simple clip */
627 for (i = 0; i < s->nb_streams; ++i) {
628 AVStream *st = s->streams[i];
629 GXFStreamContext *sc = av_mallocz(sizeof(*sc));
631 return AVERROR(ENOMEM);
634 sc->media_type = codec_get_tag(gxf_media_types, st->codec->codec_id);
635 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
636 if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
637 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
640 if (st->codec->sample_rate != 48000) {
641 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
644 if (st->codec->channels != 1) {
645 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
649 sc->sample_rate = st->codec->sample_rate;
650 av_set_pts_info(st, 64, 1, sc->sample_rate);
651 sc->sample_size = 16;
652 sc->frame_rate_index = -2;
653 sc->lines_index = -2;
656 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
658 } else if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
660 av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
663 /* FIXME check from time_base ? */
664 if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
665 sc->frame_rate_index = 5;
666 sc->sample_rate = 60;
667 gxf->flags |= 0x00000080;
668 gxf->time_base = (AVRational){ 1001, 60000 };
669 } else { /* assume PAL */
670 sc->frame_rate_index = 6;
672 sc->sample_rate = 50;
673 gxf->flags |= 0x00000040;
674 gxf->time_base = (AVRational){ 1, 50 };
676 av_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
677 if (gxf_find_lines_index(st) < 0)
678 sc->lines_index = -1;
679 sc->sample_size = st->codec->bit_rate;
680 sc->fields = 2; /* interlaced */
684 switch (st->codec->codec_id) {
687 gxf->flags |= 0x00004000;
690 case CODEC_ID_MPEG1VIDEO:
695 case CODEC_ID_MPEG2VIDEO:
696 sc->first_gop_closed = -1;
699 gxf->flags |= 0x00008000;
702 case CODEC_ID_DVVIDEO:
703 if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
706 gxf->flags |= 0x00002000;
710 gxf->flags |= 0x00001000;
715 av_log(s, AV_LOG_ERROR, "video codec not supported\n");
719 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
720 sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
721 sc->order = s->nb_streams - st->index;
724 if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
727 gxf_init_timecode_track(&gxf->timecode_track, vsc);
728 gxf->flags |= 0x200000; // time code track is non-drop frame
730 gxf_write_map_packet(s);
731 gxf_write_flt_packet(s);
732 gxf_write_umf_packet(s);
733 put_flush_packet(pb);
737 static int gxf_write_eos_packet(ByteIOContext *pb)
739 int64_t pos = url_ftell(pb);
741 gxf_write_packet_header(pb, PKT_EOS);
742 return updatePacketSize(pb, pos);
745 static int gxf_write_trailer(AVFormatContext *s)
747 GXFContext *gxf = s->priv_data;
748 ByteIOContext *pb = s->pb;
751 ff_audio_interleave_close(s);
753 gxf_write_eos_packet(pb);
755 url_fseek(pb, 0, SEEK_SET);
756 /* overwrite map, flt and umf packets with new values */
757 gxf_write_map_packet(s);
758 gxf_write_flt_packet(s);
759 gxf_write_umf_packet(s);
760 url_fseek(pb, end, SEEK_SET);
762 av_freep(&gxf->flt_entries);
767 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
771 for(i=0; i<size-4 && c!=0x100; i++){
773 if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
774 sc->first_gop_closed= (buf[i+4]>>6)&1;
776 return (buf[i+1]>>3)&7;
779 static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
781 GXFContext *gxf = s->priv_data;
782 ByteIOContext *pb = s->pb;
783 AVStream *st = s->streams[pkt->stream_index];
784 GXFStreamContext *sc = st->priv_data;
786 /* If the video is frame-encoded, the frame numbers shall be represented by
787 * even field numbers.
788 * see SMPTE360M-2004 6.4.2.1.3 Media field number */
789 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
790 field_nb = gxf->nb_fields;
792 field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
793 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
796 put_byte(pb, sc->media_type);
797 put_byte(pb, st->index);
798 put_be32(pb, field_nb);
799 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
801 put_be16(pb, size / 2);
802 } else if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
803 int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
804 if (frame_type == FF_I_TYPE) {
807 } else if (frame_type == FF_B_TYPE) {
815 } else if (st->codec->codec_id == CODEC_ID_DVVIDEO) {
816 put_byte(pb, size / 4096);
820 put_be32(pb, field_nb);
821 put_byte(pb, 1); /* flags */
822 put_byte(pb, 0); /* reserved */
826 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
828 GXFContext *gxf = s->priv_data;
829 ByteIOContext *pb = s->pb;
830 AVStream *st = s->streams[pkt->stream_index];
831 int64_t pos = url_ftell(pb);
834 gxf_write_packet_header(pb, PKT_MEDIA);
835 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
836 padding = 4 - pkt->size % 4;
837 else if (st->codec->codec_type == CODEC_TYPE_AUDIO)
838 padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
839 gxf_write_media_preamble(s, pkt, pkt->size + padding);
840 put_buffer(pb, pkt->data, pkt->size);
841 gxf_write_padding(pb, padding);
843 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
844 if (!(gxf->flt_entries_nb % 500)) {
845 gxf->flt_entries = av_realloc(gxf->flt_entries,
846 (gxf->flt_entries_nb+500)*sizeof(*gxf->flt_entries));
847 if (!gxf->flt_entries) {
848 av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
852 gxf->flt_entries[gxf->flt_entries_nb++] = url_ftell(pb) / 1024;
853 gxf->nb_fields += 2; // count fields
856 put_flush_packet(pb);
858 return updatePacketSize(pb, pos);
861 static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
863 GXFContext *gxf = s->priv_data;
864 AVPacket *pkt[2] = { cur, next };
866 GXFStreamContext *sc[2];
868 for (i = 0; i < 2; i++) {
869 AVStream *st = s->streams[pkt[i]->stream_index];
870 sc[i] = st->priv_data;
871 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
872 field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
873 (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
874 field_nb[i] &= ~1; // compare against even field number because audio must be before video
876 field_nb[i] = pkt[i]->dts; // dts are field based
879 return field_nb[1] > field_nb[0] ||
880 (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
883 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
885 if (pkt && s->streams[pkt->stream_index]->codec->codec_type == CODEC_TYPE_VIDEO)
886 pkt->duration = 2; // enforce 2 fields
887 return ff_audio_rechunk_interleave(s, out, pkt, flush,
888 av_interleave_packet_per_dts, gxf_compare_field_nb);
891 AVOutputFormat gxf_muxer = {
893 NULL_IF_CONFIG_SMALL("GXF format"),
904 gxf_interleave_packet,