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"
27 #define GXF_AUDIO_PACKET_SIZE 65536
29 typedef struct GXFStreamContext {
30 AVCodecContext *codec;
31 AVFifoBuffer audio_buffer;
51 typedef struct GXFContext {
53 uint32_t material_flags;
54 uint16_t audio_tracks;
56 int64_t creation_time;
57 uint32_t umf_start_offset;
58 uint32_t umf_track_offset;
59 uint32_t umf_media_offset;
60 uint32_t umf_user_data_offset;
61 uint32_t umf_user_data_size;
63 uint16_t umf_track_size;
64 uint16_t umf_media_size;
71 typedef struct GXF_Lines {
77 /* FIXME check if it is relevant */
78 static const GXF_Lines gxf_lines_tab[] = {
79 { 480, 1 }, /* NTSC */
80 { 512, 1 }, /* NTSC + VBI */
82 { 608, 2 }, /* PAL + VBI */
87 static const AVCodecTag gxf_media_types[] = {
88 { CODEC_ID_MJPEG , 3 }, /* NTSC */
89 { CODEC_ID_MJPEG , 4 }, /* PAL */
90 { CODEC_ID_PCM_S24LE , 9 },
91 { CODEC_ID_PCM_S16LE , 10 },
92 { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
93 { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
94 { CODEC_ID_DVVIDEO , 13 }, /* NTSC */
95 { CODEC_ID_DVVIDEO , 14 }, /* PAL */
96 { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
97 { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
98 { CODEC_ID_AC3 , 17 },
99 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
100 { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
101 { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
102 { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
106 #define SERVER_PATH "/space/"
107 #define ES_NAME_PATTERN "ES."
109 static int gxf_find_lines_index(GXFStreamContext *ctx)
113 for (i = 0; i < 6; ++i) {
114 if (ctx->codec->height == gxf_lines_tab[i].height) {
115 ctx->lines_index = gxf_lines_tab[i].index;
122 static void gxf_write_padding(ByteIOContext *pb, int64_t to_pad)
124 for (; to_pad > 0; to_pad--) {
129 static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos)
134 size = url_ftell(pb) - pos;
136 gxf_write_padding(pb, 4 - size % 4);
137 size = url_ftell(pb) - pos;
139 curpos = url_ftell(pb);
140 url_fseek(pb, pos + 6, SEEK_SET);
142 url_fseek(pb, curpos, SEEK_SET);
146 static int64_t updateSize(ByteIOContext *pb, int64_t pos)
150 curpos = url_ftell(pb);
151 url_fseek(pb, pos, SEEK_SET);
152 put_be16(pb, curpos - pos - 2);
153 url_fseek(pb, curpos, SEEK_SET);
157 static void gxf_write_packet_header(ByteIOContext *pb, GXFPktType type)
159 put_be32(pb, 0); /* packet leader for synchro */
161 put_byte(pb, type); /* map packet */
162 put_be32(pb, 0); /* size */
163 put_be32(pb, 0); /* reserved */
164 put_byte(pb, 0xE1); /* trailer 1 */
165 put_byte(pb, 0xE2); /* trailer 2 */
168 static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
171 int size, starting_line;
174 ctx->p_per_gop = ctx->pframes / ctx->iframes;
175 if (ctx->pframes % ctx->iframes)
178 ctx->b_per_gop = ctx->bframes / ctx->pframes;
179 if (ctx->p_per_gop > 9)
180 ctx->p_per_gop = 9; /* ensure value won't take more than one char */
181 if (ctx->b_per_gop > 9)
182 ctx->b_per_gop = 9; /* ensure value won't take more than one char */
184 if (ctx->codec->height == 512 || ctx->codec->height == 608)
185 starting_line = 7; // VBI
186 else if (ctx->codec->height == 480)
189 starting_line = 23; // default PAL
191 size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
192 "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
193 (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop,
194 ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, ctx->first_gop_closed == 1,
195 starting_line, ctx->codec->height / 16);
196 put_byte(pb, TRACK_MPG_AUX);
197 put_byte(pb, size + 1);
198 put_buffer(pb, (uint8_t *)buffer, size + 1);
202 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
204 /* FIXME implement that */
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(ByteIOContext *pb, GXFStreamContext *stream)
218 /* track description section */
219 put_byte(pb, stream->media_type + 0x80);
220 put_byte(pb, stream->index + 0xC0);
223 put_be16(pb, 0); /* size */
225 /* media file name */
226 put_byte(pb, TRACK_NAME);
227 put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
228 put_tag(pb, ES_NAME_PATTERN);
229 put_be16(pb, stream->media_info);
232 if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
233 /* auxiliary information */
234 put_byte(pb, TRACK_AUX);
236 if (stream->codec->codec_id == CODEC_ID_NONE)
237 gxf_write_timecode_auxiliary(pb, stream);
242 /* file system version */
243 put_byte(pb, TRACK_VER);
247 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
248 gxf_write_mpeg_auxiliary(pb, stream);
251 put_byte(pb, TRACK_FPS);
253 put_be32(pb, stream->frame_rate_index);
255 /* lines per frame */
256 put_byte(pb, TRACK_LINES);
258 put_be32(pb, stream->lines_index);
260 /* fields per frame */
261 put_byte(pb, TRACK_FPF);
263 put_be32(pb, stream->fields);
265 return updateSize(pb, pos);
268 static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
271 const char *filename = strrchr(ctx->fc->filename, '/');
274 put_be16(pb, 0); /* size */
280 filename = ctx->fc->filename;
281 put_byte(pb, MAT_NAME);
282 put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
283 put_tag(pb, SERVER_PATH);
284 put_tag(pb, filename);
288 put_byte(pb, MAT_FIRST_FIELD);
293 put_byte(pb, MAT_LAST_FIELD);
295 put_be32(pb, ctx->nb_frames);
298 put_byte(pb, MAT_MARK_IN);
302 put_byte(pb, MAT_MARK_OUT);
304 put_be32(pb, ctx->nb_frames);
307 put_byte(pb, MAT_SIZE);
309 put_be32(pb, url_fsize(pb) / 1024);
311 return updateSize(pb, pos);
314 static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx)
320 put_be16(pb, 0); /* size */
321 for (i = 0; i < ctx->fc->nb_streams; ++i)
322 gxf_write_track_description(pb, ctx->fc->streams[i]->priv_data);
323 return updateSize(pb, pos);
326 static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
328 int64_t pos = url_ftell(pb);
330 gxf_write_packet_header(pb, PKT_MAP);
333 put_byte(pb, 0xE0); /* version */
334 put_byte(pb, 0xFF); /* reserved */
336 gxf_write_material_data_section(pb, ctx);
337 gxf_write_track_description_section(pb, ctx);
339 return updatePacketSize(pb, pos);
343 static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx)
345 int64_t pos = url_ftell(pb);
348 gxf_write_packet_header(pb, PKT_FLT);
350 put_le32(pb, 1000); /* number of fields */
351 put_le32(pb, 0); /* number of active flt entries */
353 for (i = 0; i < 1000; ++i) {
356 return updatePacketSize(pb, pos);
360 static int gxf_write_umf_material_description(ByteIOContext *pb, GXFContext *ctx)
364 ctx->nb_frames / (ctx->sample_rate * 3600) % 24 << 24 | // hours
365 ctx->nb_frames / (ctx->sample_rate * 60) % 60 << 16 | // minutes
366 ctx->nb_frames / ctx->sample_rate % 60 << 8 | // seconds
367 ctx->nb_frames % ctx->sample_rate; // fields
369 put_le32(pb, ctx->flags);
370 put_le32(pb, ctx->nb_frames); /* length of the longest track */
371 put_le32(pb, ctx->nb_frames); /* length of the shortest track */
372 put_le32(pb, 0); /* mark in */
373 put_le32(pb, ctx->nb_frames); /* mark out */
374 put_le32(pb, 0); /* timecode mark in */
375 put_le32(pb, timecode); /* timecode mark out */
376 put_le64(pb, ctx->fc->timestamp); /* modification time */
377 put_le64(pb, ctx->fc->timestamp); /* creation time */
378 put_le16(pb, 0); /* reserved */
379 put_le16(pb, 0); /* reserved */
380 put_le16(pb, ctx->audio_tracks);
381 put_le16(pb, 0); /* timecode track count */
382 put_le16(pb, 0); /* reserved */
383 put_le16(pb, ctx->mpeg_tracks);
387 static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx)
389 put_le32(pb, ctx->umf_length); /* total length of the umf data */
390 put_le32(pb, 3); /* version */
391 put_le32(pb, ctx->fc->nb_streams);
392 put_le32(pb, ctx->umf_track_offset); /* umf track section offset */
393 put_le32(pb, ctx->umf_track_size);
394 put_le32(pb, ctx->fc->nb_streams);
395 put_le32(pb, ctx->umf_media_offset);
396 put_le32(pb, ctx->umf_media_size);
397 put_le32(pb, ctx->umf_user_data_offset); /* user data offset */
398 put_le32(pb, ctx->umf_user_data_size); /* user data size */
399 put_le32(pb, 0); /* reserved */
400 put_le32(pb, 0); /* reserved */
404 static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
406 int64_t pos = url_ftell(pb);
410 ctx->umf_track_offset = pos - ctx->umf_start_offset;
411 for (i = 0; i < ctx->fc->nb_streams; ++i) {
412 AVStream *st = ctx->fc->streams[i];
413 GXFStreamContext *sc = st->priv_data;
416 switch (st->codec->codec_id) {
417 case CODEC_ID_MPEG1VIDEO: id= 'L'; break;
418 case CODEC_ID_MPEG2VIDEO: id= 'M'; break;
419 case CODEC_ID_PCM_S16LE: id= 'A'; break;
420 case CODEC_ID_DVVIDEO: id= sc->track_type == 6 ? 'E' : 'D'; break;
421 case CODEC_ID_MJPEG: id= 'V'; break;
424 sc->media_info= id << 8;
425 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
426 sc->media_info |= '0' + (tracks[id]++);
427 put_le16(pb, sc->media_info);
430 return url_ftell(pb) - pos;
433 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, GXFStreamContext *stream)
435 if (stream->codec->pix_fmt == PIX_FMT_YUV422P)
438 put_le32(pb, 1); /* default to 420 */
439 put_le32(pb, stream->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
440 put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
441 put_le32(pb, 1); /* I picture per GOP */
442 put_le32(pb, stream->p_per_gop);
443 put_le32(pb, stream->b_per_gop);
444 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO)
446 else if (stream->codec->codec_id == CODEC_ID_MPEG1VIDEO)
450 put_le32(pb, 0); /* reserved */
454 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *track)
456 /* FIXME implement */
457 put_be32(pb, 0); /* drop frame flag */
458 put_be32(pb, 0); /* reserved */
459 put_be32(pb, 0); /* reserved */
460 put_be32(pb, 0); /* reserved */
461 put_be32(pb, 0); /* reserved */
462 put_be32(pb, 0); /* reserved */
463 put_be32(pb, 0); /* reserved */
464 put_be32(pb, 0); /* reserved */
468 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *track)
472 for (i = 0; i < 8; i++) {
478 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *track)
480 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
481 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
482 put_le32(pb, 0); /* number of fields over which to ramp up sound level */
483 put_le32(pb, 0); /* number of fields over which to ramp down sound level */
484 put_le32(pb, 0); /* reserved */
485 put_le32(pb, 0); /* reserved */
490 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track)
492 put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
493 put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
494 put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
495 put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
500 static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
506 ctx->umf_media_offset = pos - ctx->umf_start_offset;
507 for (i = 0; i < ctx->fc->nb_streams; ++i) {
508 GXFStreamContext *sc = ctx->fc->streams[i]->priv_data;
510 int64_t startpos, curpos;
511 int path_size = strlen(ES_NAME_PATTERN);
513 memset(buffer, 0, 88);
514 startpos = url_ftell(pb);
515 put_le16(pb, 0); /* length */
516 put_le16(pb, sc->media_info);
517 put_le16(pb, 0); /* reserved */
518 put_le16(pb, 0); /* reserved */
519 put_le32(pb, ctx->nb_frames);
520 put_le32(pb, 0); /* attributes rw, ro */
521 put_le32(pb, 0); /* mark in */
522 put_le32(pb, ctx->nb_frames); /* mark out */
523 strncpy(buffer, ES_NAME_PATTERN, path_size);
524 put_buffer(pb, (uint8_t *)buffer, path_size);
525 put_be16(pb, sc->media_info);
526 put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2);
527 put_le32(pb, sc->track_type);
528 put_le32(pb, sc->sample_rate);
529 put_le32(pb, sc->sample_size);
530 put_le32(pb, 0); /* reserved */
531 switch (sc->codec->codec_id) {
532 case CODEC_ID_MPEG2VIDEO:
533 gxf_write_umf_media_mpeg(pb, sc);
535 case CODEC_ID_PCM_S16LE:
536 gxf_write_umf_media_audio(pb, sc);
538 case CODEC_ID_DVVIDEO:
539 gxf_write_umf_media_dv(pb, sc);
542 gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
544 curpos = url_ftell(pb);
545 url_fseek(pb, startpos, SEEK_SET);
546 put_le16(pb, curpos - startpos);
547 url_fseek(pb, curpos, SEEK_SET);
549 return url_ftell(pb) - pos;
552 static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
554 int64_t pos = url_ftell(pb);
555 ctx->umf_user_data_offset = pos - ctx->umf_start_offset;
568 static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
570 int64_t pos = url_ftell(pb);
572 gxf_write_packet_header(pb, PKT_UMF);
575 put_byte(pb, 3); /* first and last (only) packet */
576 put_be32(pb, ctx->umf_length); /* data length */
578 ctx->umf_start_offset = url_ftell(pb);
579 gxf_write_umf_payload(pb, ctx);
580 gxf_write_umf_material_description(pb, ctx);
581 ctx->umf_track_size = gxf_write_umf_track_description(pb, ctx);
582 ctx->umf_media_size = gxf_write_umf_media_description(pb, ctx);
583 ctx->umf_user_data_size = gxf_write_umf_user_data(pb, ctx);
584 ctx->umf_length = url_ftell(pb) - ctx->umf_start_offset;
585 return updatePacketSize(pb, pos);
588 #define GXF_NODELAY -5000
590 static int gxf_write_header(AVFormatContext *s)
592 ByteIOContext *pb = s->pb;
593 GXFContext *gxf = s->priv_data;
597 gxf->flags |= 0x00080000; /* material is simple clip */
598 for (i = 0; i < s->nb_streams; ++i) {
599 AVStream *st = s->streams[i];
600 GXFStreamContext *sc = av_mallocz(sizeof(*sc));
602 return AVERROR(ENOMEM);
605 sc->codec = st->codec;
607 sc->media_type = codec_get_tag(gxf_media_types, sc->codec->codec_id);
608 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
609 if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
610 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
613 if (st->codec->sample_rate != 48000) {
614 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
617 if (st->codec->channels != 1) {
618 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
622 sc->sample_rate = st->codec->sample_rate;
623 av_set_pts_info(st, 64, 1, sc->sample_rate);
624 sc->sample_size = 16;
625 sc->frame_rate_index = -2;
626 sc->lines_index = -2;
629 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
630 av_fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE);
631 } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) {
632 /* FIXME check from time_base ? */
633 if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */
634 sc->frame_rate_index = 5;
635 sc->sample_rate = 60;
636 gxf->flags |= 0x00000080;
637 } else { /* assume PAL */
638 sc->frame_rate_index = 6;
640 sc->sample_rate = 50;
641 gxf->flags |= 0x00000040;
643 gxf->sample_rate = sc->sample_rate;
644 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
645 sc->dts_delay = GXF_NODELAY;
646 if (gxf_find_lines_index(sc) < 0)
647 sc->lines_index = -1;
648 sc->sample_size = st->codec->bit_rate;
649 sc->fields = 2; /* interlaced */
650 switch (sc->codec->codec_id) {
651 case CODEC_ID_MPEG2VIDEO:
652 sc->first_gop_closed = -1;
655 gxf->flags |= 0x00008000;
657 case CODEC_ID_DVVIDEO:
658 if (sc->codec->pix_fmt == PIX_FMT_YUV422P) {
661 gxf->flags |= 0x00002000;
664 gxf->flags |= 0x00001000;
668 av_log(s, AV_LOG_ERROR, "video codec not supported\n");
673 gxf_write_map_packet(pb, gxf);
674 //gxf_write_flt_packet(pb, gxf);
675 gxf_write_umf_packet(pb, gxf);
676 put_flush_packet(pb);
680 static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
682 int64_t pos = url_ftell(pb);
684 gxf_write_packet_header(pb, PKT_EOS);
685 return updatePacketSize(pb, pos);
688 static int gxf_write_trailer(AVFormatContext *s)
690 ByteIOContext *pb = s->pb;
691 GXFContext *gxf = s->priv_data;
695 for (i = 0; i < s->nb_streams; ++i) {
696 AVStream *st = s->streams[i];
697 if (st->codec->codec_type == CODEC_TYPE_AUDIO)
698 av_fifo_free(&((GXFStreamContext*)st->priv_data)->audio_buffer);
701 gxf_write_eos_packet(pb, gxf);
703 url_fseek(pb, 0, SEEK_SET);
704 /* overwrite map and umf packets with new values */
705 gxf_write_map_packet(pb, gxf);
706 //gxf_write_flt_packet(pb, gxf);
707 gxf_write_umf_packet(pb, gxf);
708 url_fseek(pb, end, SEEK_SET);
712 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
716 for(i=0; i<size-4 && c!=0x100; i++){
718 if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
719 sc->first_gop_closed= (buf[i+4]>>6)&1;
721 return (buf[i+1]>>3)&7;
724 static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size)
726 GXFStreamContext *sc = ctx->fc->streams[pkt->stream_index]->priv_data;
727 int64_t dts = av_rescale_rnd(pkt->dts, ctx->sample_rate, sc->codec->time_base.den, AV_ROUND_UP);
729 put_byte(pb, sc->media_type);
730 put_byte(pb, sc->index);
732 if (sc->codec->codec_type == CODEC_TYPE_AUDIO) {
734 put_be16(pb, size / 2);
735 } else if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
736 int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
737 if (frame_type == FF_I_TYPE) {
740 } else if (frame_type == FF_B_TYPE) {
748 } else if (sc->codec->codec_id == CODEC_ID_DVVIDEO) {
749 put_byte(pb, size / 4096);
754 put_byte(pb, 1); /* flags */
755 put_byte(pb, 0); /* reserved */
759 static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt)
761 GXFStreamContext *sc = ctx->fc->streams[pkt->stream_index]->priv_data;
762 int64_t pos = url_ftell(pb);
765 gxf_write_packet_header(pb, PKT_MEDIA);
766 if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
767 padding = 4 - pkt->size % 4;
768 else if (sc->codec->codec_type == CODEC_TYPE_AUDIO)
769 padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
770 gxf_write_media_preamble(pb, ctx, pkt, pkt->size + padding);
771 put_buffer(pb, pkt->data, pkt->size);
772 gxf_write_padding(pb, padding);
774 if (sc->codec->codec_type == CODEC_TYPE_VIDEO)
775 ctx->nb_frames += 2; // count fields
777 return updatePacketSize(pb, pos);
780 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
782 GXFContext *gxf = s->priv_data;
784 gxf_write_media_packet(s->pb, gxf, pkt);
785 put_flush_packet(s->pb);
789 static int gxf_new_audio_packet(GXFContext *gxf, GXFStreamContext *sc, AVPacket *pkt, int flush)
791 int size = flush ? av_fifo_size(&sc->audio_buffer) : GXF_AUDIO_PACKET_SIZE;
795 av_new_packet(pkt, size);
796 av_fifo_read(&sc->audio_buffer, pkt->data, size);
797 pkt->stream_index = sc->index;
798 pkt->dts = sc->current_dts;
799 sc->current_dts += size / 2; /* we only support 16 bit pcm mono for now */
803 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
805 GXFContext *gxf = s->priv_data;
809 for (i = 0; i < s->nb_streams; i++) {
810 AVStream *st = s->streams[i];
811 GXFStreamContext *sc = st->priv_data;
812 if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
813 if (pkt && pkt->stream_index == i) {
814 av_fifo_generic_write(&sc->audio_buffer, pkt->data, pkt->size, NULL);
817 if (flush || av_fifo_size(&sc->audio_buffer) >= GXF_AUDIO_PACKET_SIZE) {
818 if (!pkt && gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) {
820 break; /* add pkt right now into list */
823 } else if (pkt && pkt->stream_index == i) {
824 if (sc->dts_delay == GXF_NODELAY) /* adjust dts if needed */
825 sc->dts_delay = pkt->dts;
826 pkt->dts -= sc->dts_delay;
829 return av_interleave_packet_per_dts(s, out, pkt, flush);
832 AVOutputFormat gxf_muxer = {
834 NULL_IF_CONFIG_SMALL("GXF format"),
845 gxf_interleave_packet,