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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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
;
69 GXFStreamContext streams
[48];
72 typedef struct GXF_Lines
{
78 /* FIXME check if it is relevant */
79 static const GXF_Lines gxf_lines_tab
[] = {
80 { 480, 1 }, /* NTSC */
81 { 512, 1 }, /* NTSC + VBI */
83 { 608, 2 }, /* PAL + VBI */
88 static const CodecTag gxf_media_types
[] = {
89 { CODEC_ID_MJPEG
, 3 }, /* NTSC */
90 { CODEC_ID_MJPEG
, 4 }, /* PAL */
91 { CODEC_ID_PCM_S24LE
, 9 },
92 { CODEC_ID_PCM_S16LE
, 10 },
93 { CODEC_ID_MPEG2VIDEO
, 11 }, /* NTSC */
94 { CODEC_ID_MPEG2VIDEO
, 12 }, /* PAL */
95 { CODEC_ID_DVVIDEO
, 13 }, /* NTSC */
96 { CODEC_ID_DVVIDEO
, 14 }, /* PAL */
97 { CODEC_ID_DVVIDEO
, 15 }, /* 50M NTSC */
98 { CODEC_ID_DVVIDEO
, 16 }, /* 50M PAL */
99 { CODEC_ID_AC3
, 17 },
100 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
101 { CODEC_ID_MPEG2VIDEO
, 20 }, /* MPEG HD */
102 { CODEC_ID_MPEG1VIDEO
, 22 }, /* NTSC */
103 { CODEC_ID_MPEG1VIDEO
, 23 }, /* PAL */
107 #define SERVER_PATH "/space/"
108 #define ES_NAME_PATTERN "ES."
110 static int gxf_find_lines_index(GXFStreamContext
*ctx
)
114 for (i
= 0; i
< 6; ++i
) {
115 if (ctx
->codec
->height
== gxf_lines_tab
[i
].height
) {
116 ctx
->lines_index
= gxf_lines_tab
[i
].index
;
123 static void gxf_write_padding(ByteIOContext
*pb
, offset_t to_pad
)
125 for (; to_pad
> 0; to_pad
--) {
130 static offset_t
updatePacketSize(ByteIOContext
*pb
, offset_t pos
)
135 size
= url_ftell(pb
) - pos
;
137 gxf_write_padding(pb
, 4 - size
% 4);
138 size
= url_ftell(pb
) - pos
;
140 curpos
= url_ftell(pb
);
141 url_fseek(pb
, pos
+ 6, SEEK_SET
);
143 url_fseek(pb
, curpos
, SEEK_SET
);
147 static offset_t
updateSize(ByteIOContext
*pb
, offset_t pos
)
151 curpos
= url_ftell(pb
);
152 url_fseek(pb
, pos
, SEEK_SET
);
153 put_be16(pb
, curpos
- pos
- 2);
154 url_fseek(pb
, curpos
, SEEK_SET
);
158 static void gxf_write_packet_header(ByteIOContext
*pb
, pkt_type_t type
)
160 put_be32(pb
, 0); /* packet leader for synchro */
162 put_byte(pb
, type
); /* map packet */
163 put_be32(pb
, 0); /* size */
164 put_be32(pb
, 0); /* reserved */
165 put_byte(pb
, 0xE1); /* trailer 1 */
166 put_byte(pb
, 0xE2); /* trailer 2 */
169 static int gxf_write_mpeg_auxiliary(ByteIOContext
*pb
, GXFStreamContext
*ctx
)
175 ctx
->p_per_gop
= ctx
->pframes
/ ctx
->iframes
;
176 if (ctx
->pframes
% ctx
->iframes
)
179 ctx
->b_per_gop
= ctx
->bframes
/ ctx
->pframes
;
180 if (ctx
->p_per_gop
> 9)
181 ctx
->p_per_gop
= 9; /* ensure value won't take more than one char */
182 if (ctx
->b_per_gop
> 9)
183 ctx
->b_per_gop
= 9; /* ensure value won't take more than one char */
185 size
= snprintf(buffer
, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
186 "Pix 0\nCf %d\nCg %d\nSl 7\nnl16 %d\nVi 1\nf1 1\n",
187 (float)ctx
->codec
->bit_rate
, ctx
->p_per_gop
, ctx
->b_per_gop
,
188 ctx
->codec
->pix_fmt
== PIX_FMT_YUV422P
? 2 : 1, ctx
->first_gop_closed
== 1,
189 ctx
->codec
->height
/ 16);
191 put_byte(pb
, size
+ 1);
192 put_buffer(pb
, (uint8_t *)buffer
, size
+ 1);
196 static int gxf_write_timecode_auxiliary(ByteIOContext
*pb
, GXFStreamContext
*ctx
)
198 /* FIXME implement that */
199 put_byte(pb
, 0); /* fields */
200 put_byte(pb
, 0); /* seconds */
201 put_byte(pb
, 0); /* minutes */
202 put_byte(pb
, 0); /* flags + hours */
208 static int gxf_write_track_description(ByteIOContext
*pb
, GXFStreamContext
*stream
)
212 /* track description section */
213 put_byte(pb
, stream
->media_type
+ 0x80);
214 put_byte(pb
, stream
->index
+ 0xC0);
217 put_be16(pb
, 0); /* size */
219 /* media file name */
221 put_byte(pb
, strlen(ES_NAME_PATTERN
) + 3);
222 put_tag(pb
, ES_NAME_PATTERN
);
223 put_be16(pb
, stream
->media_info
);
226 if (stream
->codec
->codec_id
!= CODEC_ID_MPEG2VIDEO
) {
227 /* auxiliary information */
230 if (stream
->codec
->codec_id
== CODEC_ID_NONE
)
231 gxf_write_timecode_auxiliary(pb
, stream
);
236 /* file system version */
241 if (stream
->codec
->codec_id
== CODEC_ID_MPEG2VIDEO
)
242 gxf_write_mpeg_auxiliary(pb
, stream
);
247 put_be32(pb
, stream
->frame_rate_index
);
249 /* lines per frame */
252 put_be32(pb
, stream
->lines_index
);
254 /* fields per frame */
257 put_be32(pb
, stream
->fields
);
259 return updateSize(pb
, pos
);
262 static int gxf_write_material_data_section(ByteIOContext
*pb
, GXFContext
*ctx
)
265 const char *filename
= strrchr(ctx
->fc
->filename
, '/');
268 put_be16(pb
, 0); /* size */
274 filename
= ctx
->fc
->filename
;
276 put_byte(pb
, strlen(SERVER_PATH
) + strlen(filename
) + 1);
277 put_tag(pb
, SERVER_PATH
);
278 put_tag(pb
, filename
);
289 put_be32(pb
, ctx
->nb_frames
);
298 put_be32(pb
, ctx
->nb_frames
);
303 put_be32(pb
, url_fsize(pb
) / 1024);
305 return updateSize(pb
, pos
);
308 static int gxf_write_track_description_section(ByteIOContext
*pb
, GXFContext
*ctx
)
314 put_be16(pb
, 0); /* size */
315 for (i
= 0; i
< ctx
->fc
->nb_streams
; ++i
)
316 gxf_write_track_description(pb
, &ctx
->streams
[i
]);
317 return updateSize(pb
, pos
);
320 static int gxf_write_map_packet(ByteIOContext
*pb
, GXFContext
*ctx
)
322 offset_t pos
= url_ftell(pb
);
324 gxf_write_packet_header(pb
, PKT_MAP
);
327 put_byte(pb
, 0xE0); /* version */
328 put_byte(pb
, 0xFF); /* reserved */
330 gxf_write_material_data_section(pb
, ctx
);
331 gxf_write_track_description_section(pb
, ctx
);
333 return updatePacketSize(pb
, pos
);
337 static int gxf_write_flt_packet(ByteIOContext
*pb
, GXFContext
*ctx
)
339 offset_t pos
= url_ftell(pb
);
342 gxf_write_packet_header(pb
, PKT_FLT
);
344 put_le32(pb
, 1000); /* number of fields */
345 put_le32(pb
, 0); /* number of active flt entries */
347 for (i
= 0; i
< 1000; ++i
) {
350 return updatePacketSize(pb
, pos
);
354 static int gxf_write_umf_material_description(ByteIOContext
*pb
, GXFContext
*ctx
)
356 put_le32(pb
, ctx
->flags
);
357 put_le32(pb
, ctx
->nb_frames
); /* length of the longest track */
358 put_le32(pb
, ctx
->nb_frames
); /* length of the shortest track */
359 put_le32(pb
, 0); /* mark in */
360 put_le32(pb
, ctx
->nb_frames
); /* mark out */
361 put_le32(pb
, 0); /* timecode mark in */
362 put_le32(pb
, ctx
->nb_frames
); /* timecode mark out */
363 put_le64(pb
, ctx
->fc
->timestamp
); /* modification time */
364 put_le64(pb
, ctx
->fc
->timestamp
); /* creation time */
365 put_le16(pb
, 0); /* reserved */
366 put_le16(pb
, 0); /* reserved */
367 put_le16(pb
, ctx
->audio_tracks
);
368 put_le16(pb
, 0); /* timecode track count */
369 put_le16(pb
, 0); /* reserved */
370 put_le16(pb
, ctx
->mpeg_tracks
);
374 static int gxf_write_umf_payload(ByteIOContext
*pb
, GXFContext
*ctx
)
376 put_le32(pb
, ctx
->umf_length
); /* total length of the umf data */
377 put_le32(pb
, 3); /* version */
378 put_le32(pb
, ctx
->fc
->nb_streams
);
379 put_le32(pb
, ctx
->umf_track_offset
); /* umf track section offset */
380 put_le32(pb
, ctx
->umf_track_size
);
381 put_le32(pb
, ctx
->fc
->nb_streams
);
382 put_le32(pb
, ctx
->umf_media_offset
);
383 put_le32(pb
, ctx
->umf_media_size
);
384 put_le32(pb
, ctx
->umf_user_data_offset
); /* user data offset */
385 put_le32(pb
, ctx
->umf_user_data_size
); /* user data size */
386 put_le32(pb
, 0); /* reserved */
387 put_le32(pb
, 0); /* reserved */
391 static int gxf_write_umf_track_description(ByteIOContext
*pb
, GXFContext
*ctx
)
393 offset_t pos
= url_ftell(pb
);
397 ctx
->umf_track_offset
= pos
- ctx
->umf_start_offset
;
398 for (i
= 0; i
< ctx
->fc
->nb_streams
; ++i
) {
399 AVStream
*st
= ctx
->fc
->streams
[i
];
400 GXFStreamContext
*sc
= &ctx
->streams
[i
];
403 switch (st
->codec
->codec_id
) {
404 case CODEC_ID_MPEG1VIDEO
: id
= 'L'; break;
405 case CODEC_ID_MPEG2VIDEO
: id
= 'M'; break;
406 case CODEC_ID_PCM_S16LE
: id
= 'A'; break;
407 case CODEC_ID_DVVIDEO
: id
= sc
->track_type
== 6 ? 'E' : 'D'; break;
408 case CODEC_ID_MJPEG
: id
= 'V'; break;
411 sc
->media_info
= id
<< 8;
412 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
413 sc
->media_info
|= '0' + (tracks
[id
]++);
414 put_le16(pb
, sc
->media_info
);
417 return url_ftell(pb
) - pos
;
420 static int gxf_write_umf_media_mpeg(ByteIOContext
*pb
, GXFStreamContext
*stream
)
422 if (stream
->codec
->pix_fmt
== PIX_FMT_YUV422P
)
425 put_le32(pb
, 1); /* default to 420 */
426 put_le32(pb
, stream
->first_gop_closed
== 1); /* closed = 1, open = 0, unknown = 255 */
427 put_le32(pb
, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
428 put_le32(pb
, 1); /* I picture per GOP */
429 put_le32(pb
, stream
->p_per_gop
);
430 put_le32(pb
, stream
->b_per_gop
);
431 if (stream
->codec
->codec_id
== CODEC_ID_MPEG2VIDEO
)
433 else if (stream
->codec
->codec_id
== CODEC_ID_MPEG1VIDEO
)
437 put_le32(pb
, 0); /* reserved */
441 static int gxf_write_umf_media_timecode(ByteIOContext
*pb
, GXFStreamContext
*track
)
443 /* FIXME implement */
444 put_be32(pb
, 0); /* drop frame flag */
445 put_be32(pb
, 0); /* reserved */
446 put_be32(pb
, 0); /* reserved */
447 put_be32(pb
, 0); /* reserved */
448 put_be32(pb
, 0); /* reserved */
449 put_be32(pb
, 0); /* reserved */
450 put_be32(pb
, 0); /* reserved */
451 put_be32(pb
, 0); /* reserved */
455 static int gxf_write_umf_media_dv(ByteIOContext
*pb
, GXFStreamContext
*track
)
459 for (i
= 0; i
< 8; i
++) {
465 static int gxf_write_umf_media_audio(ByteIOContext
*pb
, GXFStreamContext
*track
)
467 put_le64(pb
, av_dbl2int(1)); /* sound level to begin to */
468 put_le64(pb
, av_dbl2int(1)); /* sound level to begin to */
469 put_le32(pb
, 0); /* number of fields over which to ramp up sound level */
470 put_le32(pb
, 0); /* number of fields over which to ramp down sound level */
471 put_le32(pb
, 0); /* reserved */
472 put_le32(pb
, 0); /* reserved */
477 static int gxf_write_umf_media_mjpeg(ByteIOContext
*pb
, GXFStreamContext
*track
)
479 put_be64(pb
, 0); /* FIXME FLOAT max chroma quant level */
480 put_be64(pb
, 0); /* FIXME FLOAT max luma quant level */
481 put_be64(pb
, 0); /* FIXME FLOAT min chroma quant level */
482 put_be64(pb
, 0); /* FIXME FLOAT min luma quant level */
487 static int gxf_write_umf_media_description(ByteIOContext
*pb
, GXFContext
*ctx
)
493 ctx
->umf_media_offset
= pos
- ctx
->umf_start_offset
;
494 for (i
= 0; i
< ctx
->fc
->nb_streams
; ++i
) {
495 GXFStreamContext
*sc
= &ctx
->streams
[i
];
497 offset_t startpos
, curpos
;
498 int path_size
= strlen(ES_NAME_PATTERN
);
500 memset(buffer
, 0, 88);
501 startpos
= url_ftell(pb
);
502 put_le16(pb
, 0); /* length */
503 put_le16(pb
, sc
->media_info
);
504 put_le16(pb
, 0); /* reserved */
505 put_le16(pb
, 0); /* reserved */
506 put_le32(pb
, ctx
->nb_frames
);
507 put_le32(pb
, 0); /* attributes rw, ro */
508 put_le32(pb
, 0); /* mark in */
509 put_le32(pb
, ctx
->nb_frames
); /* mark out */
510 strncpy(buffer
, ES_NAME_PATTERN
, path_size
);
511 put_buffer(pb
, (uint8_t *)buffer
, path_size
);
512 put_be16(pb
, sc
->media_info
);
513 put_buffer(pb
, (uint8_t *)buffer
+ path_size
+ 2, 88 - path_size
- 2);
514 put_le32(pb
, sc
->track_type
);
515 put_le32(pb
, sc
->sample_rate
);
516 put_le32(pb
, sc
->sample_size
);
517 put_le32(pb
, 0); /* reserved */
518 switch (sc
->codec
->codec_id
) {
519 case CODEC_ID_MPEG2VIDEO
:
520 gxf_write_umf_media_mpeg(pb
, sc
);
522 case CODEC_ID_PCM_S16LE
:
523 gxf_write_umf_media_audio(pb
, sc
);
525 case CODEC_ID_DVVIDEO
:
526 gxf_write_umf_media_dv(pb
, sc
);
529 gxf_write_umf_media_timecode(pb
, sc
); /* 8 0bytes */
531 curpos
= url_ftell(pb
);
532 url_fseek(pb
, startpos
, SEEK_SET
);
533 put_le16(pb
, curpos
- startpos
);
534 url_fseek(pb
, curpos
, SEEK_SET
);
536 return url_ftell(pb
) - pos
;
539 static int gxf_write_umf_user_data(ByteIOContext
*pb
, GXFContext
*ctx
)
541 offset_t pos
= url_ftell(pb
);
542 ctx
->umf_user_data_offset
= pos
- ctx
->umf_start_offset
;
555 static int gxf_write_umf_packet(ByteIOContext
*pb
, GXFContext
*ctx
)
557 offset_t pos
= url_ftell(pb
);
559 gxf_write_packet_header(pb
, PKT_UMF
);
562 put_byte(pb
, 3); /* first and last (only) packet */
563 put_be32(pb
, ctx
->umf_length
); /* data length */
565 ctx
->umf_start_offset
= url_ftell(pb
);
566 gxf_write_umf_payload(pb
, ctx
);
567 gxf_write_umf_material_description(pb
, ctx
);
568 ctx
->umf_track_size
= gxf_write_umf_track_description(pb
, ctx
);
569 ctx
->umf_media_size
= gxf_write_umf_media_description(pb
, ctx
);
570 ctx
->umf_user_data_size
= gxf_write_umf_user_data(pb
, ctx
);
571 ctx
->umf_length
= url_ftell(pb
) - ctx
->umf_start_offset
;
572 return updatePacketSize(pb
, pos
);
575 static int gxf_write_header(AVFormatContext
*s
)
577 ByteIOContext
*pb
= &s
->pb
;
578 GXFContext
*gxf
= s
->priv_data
;
582 gxf
->flags
|= 0x00080000; /* material is simple clip */
583 for (i
= 0; i
< s
->nb_streams
; ++i
) {
584 AVStream
*st
= s
->streams
[i
];
585 GXFStreamContext
*sc
= &gxf
->streams
[i
];
587 sc
->codec
= st
->codec
;
589 sc
->media_type
= codec_get_tag(gxf_media_types
, sc
->codec
->codec_id
);
590 if (st
->codec
->codec_type
== CODEC_TYPE_AUDIO
) {
591 if (st
->codec
->codec_id
!= CODEC_ID_PCM_S16LE
) {
592 av_log(s
, AV_LOG_ERROR
, "only 16 BIT PCM LE allowed for now\n");
595 if (st
->codec
->sample_rate
!= 48000) {
596 av_log(s
, AV_LOG_ERROR
, "only 48000hz sampling rate is allowed\n");
599 if (st
->codec
->channels
!= 1) {
600 av_log(s
, AV_LOG_ERROR
, "only mono tracks are allowed\n");
604 sc
->sample_rate
= st
->codec
->sample_rate
;
605 av_set_pts_info(st
, 64, 1, sc
->sample_rate
);
606 sc
->sample_size
= 16;
607 sc
->frame_rate_index
= -2;
608 sc
->lines_index
= -2;
611 gxf
->flags
|= 0x04000000; /* audio is 16 bit pcm */
612 av_fifo_init(&sc
->audio_buffer
, 3*GXF_AUDIO_PACKET_SIZE
);
613 } else if (sc
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
614 /* FIXME check from time_base ? */
615 if (sc
->codec
->height
== 480 || sc
->codec
->height
== 512) { /* NTSC or NTSC+VBI */
616 sc
->frame_rate_index
= 5;
617 sc
->sample_rate
= 60;
618 gxf
->flags
|= 0x00000080;
619 } else { /* assume PAL */
620 sc
->frame_rate_index
= 6;
622 sc
->sample_rate
= 50;
623 gxf
->flags
|= 0x00000040;
625 gxf
->sample_rate
= sc
->sample_rate
;
626 av_set_pts_info(st
, 64, 1, sc
->sample_rate
);
627 if (gxf_find_lines_index(sc
) < 0)
628 sc
->lines_index
= -1;
629 sc
->sample_size
= st
->codec
->bit_rate
;
630 sc
->fields
= 2; /* interlaced */
631 switch (sc
->codec
->codec_id
) {
632 case CODEC_ID_MPEG2VIDEO
:
633 sc
->first_gop_closed
= -1;
636 gxf
->flags
|= 0x00008000;
638 case CODEC_ID_DVVIDEO
:
639 if (sc
->codec
->pix_fmt
== PIX_FMT_YUV422P
) {
642 gxf
->flags
|= 0x00002000;
645 gxf
->flags
|= 0x00001000;
649 av_log(s
, AV_LOG_ERROR
, "video codec not supported\n");
654 gxf_write_map_packet(pb
, gxf
);
655 //gxf_write_flt_packet(pb, gxf);
656 gxf_write_umf_packet(pb
, gxf
);
657 put_flush_packet(pb
);
661 static int gxf_write_eos_packet(ByteIOContext
*pb
, GXFContext
*ctx
)
663 offset_t pos
= url_ftell(pb
);
665 gxf_write_packet_header(pb
, PKT_EOS
);
666 return updatePacketSize(pb
, pos
);
669 static int gxf_write_trailer(AVFormatContext
*s
)
671 ByteIOContext
*pb
= &s
->pb
;
672 GXFContext
*gxf
= s
->priv_data
;
676 for (i
= 0; i
< s
->nb_streams
; ++i
) {
677 if (s
->streams
[i
]->codec
->codec_type
== CODEC_TYPE_AUDIO
) {
678 av_fifo_free(&gxf
->streams
[i
].audio_buffer
);
680 if (s
->streams
[i
]->codec
->frame_number
> gxf
->nb_frames
)
681 gxf
->nb_frames
= 2 * s
->streams
[i
]->codec
->frame_number
;
684 gxf_write_eos_packet(pb
, gxf
);
686 url_fseek(pb
, 0, SEEK_SET
);
687 /* overwrite map and umf packets with new values */
688 gxf_write_map_packet(pb
, gxf
);
689 //gxf_write_flt_packet(pb, gxf);
690 gxf_write_umf_packet(pb
, gxf
);
691 url_fseek(pb
, end
, SEEK_SET
);
695 static int gxf_parse_mpeg_frame(GXFStreamContext
*sc
, const uint8_t *buf
, int size
)
699 for(i
=0; i
<size
-4 && c
!=0x100; i
++){
701 if(c
== 0x1B8 && sc
->first_gop_closed
== -1) /* GOP start code */
702 sc
->first_gop_closed
= (buf
[i
+4]>>6)&1;
704 return (buf
[i
+1]>>3)&7;
707 static int gxf_write_media_preamble(ByteIOContext
*pb
, GXFContext
*ctx
, AVPacket
*pkt
, int size
)
709 GXFStreamContext
*sc
= &ctx
->streams
[pkt
->stream_index
];
710 int64_t dts
= av_rescale(pkt
->dts
, ctx
->sample_rate
, sc
->sample_rate
);
712 put_byte(pb
, sc
->media_type
);
713 put_byte(pb
, sc
->index
);
715 if (sc
->codec
->codec_type
== CODEC_TYPE_AUDIO
) {
717 put_be16(pb
, size
/ 2);
718 } else if (sc
->codec
->codec_id
== CODEC_ID_MPEG2VIDEO
) {
719 int frame_type
= gxf_parse_mpeg_frame(sc
, pkt
->data
, pkt
->size
);
720 if (frame_type
== FF_I_TYPE
) {
723 } else if (frame_type
== FF_B_TYPE
) {
731 } else if (sc
->codec
->codec_id
== CODEC_ID_DVVIDEO
) {
732 put_byte(pb
, size
/ 4096);
737 put_byte(pb
, 1); /* flags */
738 put_byte(pb
, 0); /* reserved */
742 static int gxf_write_media_packet(ByteIOContext
*pb
, GXFContext
*ctx
, AVPacket
*pkt
)
744 GXFStreamContext
*sc
= &ctx
->streams
[pkt
->stream_index
];
745 offset_t pos
= url_ftell(pb
);
748 gxf_write_packet_header(pb
, PKT_MEDIA
);
749 if (sc
->codec
->codec_id
== CODEC_ID_MPEG2VIDEO
&& pkt
->size
% 4) /* MPEG-2 frames must be padded */
750 padding
= 4 - pkt
->size
% 4;
751 else if (sc
->codec
->codec_type
== CODEC_TYPE_AUDIO
)
752 padding
= GXF_AUDIO_PACKET_SIZE
- pkt
->size
;
753 gxf_write_media_preamble(pb
, ctx
, pkt
, pkt
->size
+ padding
);
754 put_buffer(pb
, pkt
->data
, pkt
->size
);
755 gxf_write_padding(pb
, padding
);
756 return updatePacketSize(pb
, pos
);
759 static int gxf_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
761 GXFContext
*gxf
= s
->priv_data
;
763 gxf_write_media_packet(&s
->pb
, gxf
, pkt
);
764 put_flush_packet(&s
->pb
);
768 static int gxf_new_audio_packet(GXFContext
*gxf
, GXFStreamContext
*sc
, AVPacket
*pkt
, int flush
)
770 int size
= flush
? av_fifo_size(&sc
->audio_buffer
) : GXF_AUDIO_PACKET_SIZE
;
774 av_new_packet(pkt
, size
);
775 av_fifo_read(&sc
->audio_buffer
, pkt
->data
, size
);
776 pkt
->stream_index
= sc
->index
;
777 pkt
->dts
= sc
->current_dts
;
778 sc
->current_dts
+= size
/ 2; /* we only support 16 bit pcm mono for now */
782 static int gxf_interleave_packet(AVFormatContext
*s
, AVPacket
*out
, AVPacket
*pkt
, int flush
)
784 GXFContext
*gxf
= s
->priv_data
;
788 for (i
= 0; i
< s
->nb_streams
; i
++) {
789 AVStream
*st
= s
->streams
[i
];
790 GXFStreamContext
*sc
= &gxf
->streams
[i
];
791 if (st
->codec
->codec_type
== CODEC_TYPE_AUDIO
) {
792 if (pkt
&& pkt
->stream_index
== i
) {
793 av_fifo_write(&sc
->audio_buffer
, pkt
->data
, pkt
->size
);
796 if (flush
|| av_fifo_size(&sc
->audio_buffer
) >= GXF_AUDIO_PACKET_SIZE
) {
797 if (!pkt
&& gxf_new_audio_packet(gxf
, sc
, &new_pkt
, flush
) > 0) {
799 break; /* add pkt right now into list */
803 /* adjust dts if negative */
804 if (pkt
->dts
< 0 && !sc
->dts_delay
) {
805 /* XXX: rescale if codec time base is different from stream time base */
806 sc
->dts_delay
= av_rescale_q(pkt
->dts
, st
->codec
->time_base
, st
->time_base
);
807 pkt
->dts
= sc
->dts_delay
; /* set to 0 */
809 pkt
->dts
-= sc
->dts_delay
;
812 return av_interleave_packet_per_dts(s
, out
, pkt
, flush
);
815 AVOutputFormat gxf_muxer
= {
828 gxf_interleave_packet
,