3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intfloat.h"
23 #include "libavutil/mathematics.h"
27 #include "audiointerleave.h"
29 #define GXF_AUDIO_PACKET_SIZE 65536
31 typedef struct GXFStreamContext
{
32 AudioInterleaveContext aic
;
45 int b_per_i_or_p
; ///< number of B frames per I frame or P frame
47 unsigned order
; ///< interleaving order
50 typedef struct GXFContext
{
52 uint16_t audio_tracks
;
54 int64_t creation_time
;
55 uint32_t umf_start_offset
;
56 uint32_t umf_track_offset
;
57 uint32_t umf_media_offset
;
59 uint16_t umf_track_size
;
60 uint16_t umf_media_size
;
63 GXFStreamContext timecode_track
;
64 unsigned *flt_entries
; ///< offsets of packets /1024, starts after 2nd video field
65 unsigned flt_entries_nb
;
66 uint64_t *map_offsets
; ///< offset of map packets
67 unsigned map_offsets_nb
;
68 unsigned packet_count
;
74 { 480, 1 }, /* NTSC */
75 { 512, 1 }, /* NTSC + VBI */
77 { 608, 2 }, /* PAL + VBI */
82 static const AVCodecTag gxf_media_types
[] = {
83 { AV_CODEC_ID_MJPEG
, 3 }, /* NTSC */
84 { AV_CODEC_ID_MJPEG
, 4 }, /* PAL */
85 { AV_CODEC_ID_PCM_S24LE
, 9 },
86 { AV_CODEC_ID_PCM_S16LE
, 10 },
87 { AV_CODEC_ID_MPEG2VIDEO
, 11 }, /* NTSC */
88 { AV_CODEC_ID_MPEG2VIDEO
, 12 }, /* PAL */
89 { AV_CODEC_ID_DVVIDEO
, 13 }, /* NTSC */
90 { AV_CODEC_ID_DVVIDEO
, 14 }, /* PAL */
91 { AV_CODEC_ID_DVVIDEO
, 15 }, /* 50M NTSC */
92 { AV_CODEC_ID_DVVIDEO
, 16 }, /* 50M PAL */
93 { AV_CODEC_ID_AC3
, 17 },
94 //{ AV_CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
95 { AV_CODEC_ID_MPEG2VIDEO
, 20 }, /* MPEG HD */
96 { AV_CODEC_ID_MPEG1VIDEO
, 22 }, /* NTSC */
97 { AV_CODEC_ID_MPEG1VIDEO
, 23 }, /* PAL */
98 { AV_CODEC_ID_NONE
, 0 },
101 #define SERVER_PATH "EXT:/PDR/default/"
102 #define ES_NAME_PATTERN "EXT:/PDR/default/ES."
104 static int gxf_find_lines_index(AVStream
*st
)
106 GXFStreamContext
*sc
= st
->priv_data
;
109 for (i
= 0; i
< 6; ++i
) {
110 if (st
->codec
->height
== gxf_lines_tab
[i
].height
) {
111 sc
->lines_index
= gxf_lines_tab
[i
].index
;
118 static void gxf_write_padding(AVIOContext
*pb
, int64_t to_pad
)
120 for (; to_pad
> 0; to_pad
--) {
125 static int64_t updatePacketSize(AVIOContext
*pb
, int64_t pos
)
130 size
= avio_tell(pb
) - pos
;
132 gxf_write_padding(pb
, 4 - size
% 4);
133 size
= avio_tell(pb
) - pos
;
135 curpos
= avio_tell(pb
);
136 avio_seek(pb
, pos
+ 6, SEEK_SET
);
138 avio_seek(pb
, curpos
, SEEK_SET
);
142 static int64_t updateSize(AVIOContext
*pb
, int64_t pos
)
146 curpos
= avio_tell(pb
);
147 avio_seek(pb
, pos
, SEEK_SET
);
148 avio_wb16(pb
, curpos
- pos
- 2);
149 avio_seek(pb
, curpos
, SEEK_SET
);
153 static void gxf_write_packet_header(AVIOContext
*pb
, GXFPktType type
)
155 avio_wb32(pb
, 0); /* packet leader for synchro */
157 avio_w8(pb
, type
); /* map packet */
158 avio_wb32(pb
, 0); /* size */
159 avio_wb32(pb
, 0); /* reserved */
160 avio_w8(pb
, 0xE1); /* trailer 1 */
161 avio_w8(pb
, 0xE2); /* trailer 2 */
164 static int gxf_write_mpeg_auxiliary(AVIOContext
*pb
, AVStream
*st
)
166 GXFStreamContext
*sc
= st
->priv_data
;
168 int size
, starting_line
;
171 sc
->p_per_gop
= sc
->pframes
/ sc
->iframes
;
172 if (sc
->pframes
% sc
->iframes
)
175 sc
->b_per_i_or_p
= sc
->bframes
/ sc
->pframes
;
176 if (sc
->bframes
% sc
->pframes
)
179 if (sc
->p_per_gop
> 9)
180 sc
->p_per_gop
= 9; /* ensure value won't take more than one char */
181 if (sc
->b_per_i_or_p
> 9)
182 sc
->b_per_i_or_p
= 9; /* ensure value won't take more than one char */
184 if (st
->codec
->height
== 512 || st
->codec
->height
== 608)
185 starting_line
= 7; // VBI
186 else if (st
->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)st
->codec
->bit_rate
, sc
->p_per_gop
, sc
->b_per_i_or_p
,
194 st
->codec
->pix_fmt
== AV_PIX_FMT_YUV422P
? 2 : 1, sc
->first_gop_closed
== 1,
195 starting_line
, (st
->codec
->height
+ 15) / 16);
196 avio_w8(pb
, TRACK_MPG_AUX
);
197 avio_w8(pb
, size
+ 1);
198 avio_write(pb
, (uint8_t *)buffer
, size
+ 1);
202 static int gxf_write_timecode_auxiliary(AVIOContext
*pb
, GXFStreamContext
*sc
)
204 avio_w8(pb
, 0); /* fields */
205 avio_w8(pb
, 0); /* seconds */
206 avio_w8(pb
, 0); /* minutes */
207 avio_w8(pb
, 0); /* flags + hours */
213 static int gxf_write_track_description(AVFormatContext
*s
, GXFStreamContext
*sc
, int index
)
215 AVIOContext
*pb
= s
->pb
;
217 int mpeg
= sc
->track_type
== 4 || sc
->track_type
== 9;
219 /* track description section */
220 avio_w8(pb
, sc
->media_type
+ 0x80);
221 avio_w8(pb
, index
+ 0xC0);
224 avio_wb16(pb
, 0); /* size */
226 /* media file name */
227 avio_w8(pb
, TRACK_NAME
);
228 avio_w8(pb
, strlen(ES_NAME_PATTERN
) + 3);
229 avio_write(pb
, ES_NAME_PATTERN
, sizeof(ES_NAME_PATTERN
) - 1);
230 avio_wb16(pb
, sc
->media_info
);
234 /* auxiliary information */
235 avio_w8(pb
, TRACK_AUX
);
237 if (sc
->track_type
== 3)
238 gxf_write_timecode_auxiliary(pb
, sc
);
243 /* file system version */
244 avio_w8(pb
, TRACK_VER
);
249 gxf_write_mpeg_auxiliary(pb
, s
->streams
[index
]);
252 avio_w8(pb
, TRACK_FPS
);
254 avio_wb32(pb
, sc
->frame_rate_index
);
256 /* lines per frame */
257 avio_w8(pb
, TRACK_LINES
);
259 avio_wb32(pb
, sc
->lines_index
);
261 /* fields per frame */
262 avio_w8(pb
, TRACK_FPF
);
264 avio_wb32(pb
, sc
->fields
);
266 return updateSize(pb
, pos
);
269 static int gxf_write_material_data_section(AVFormatContext
*s
)
271 GXFContext
*gxf
= s
->priv_data
;
272 AVIOContext
*pb
= s
->pb
;
275 const char *filename
= strrchr(s
->filename
, '/');
278 avio_wb16(pb
, 0); /* size */
284 filename
= s
->filename
;
285 len
= strlen(filename
);
287 avio_w8(pb
, MAT_NAME
);
288 avio_w8(pb
, strlen(SERVER_PATH
) + len
+ 1);
289 avio_write(pb
, SERVER_PATH
, sizeof(SERVER_PATH
) - 1);
290 avio_write(pb
, filename
, len
);
294 avio_w8(pb
, MAT_FIRST_FIELD
);
299 avio_w8(pb
, MAT_LAST_FIELD
);
301 avio_wb32(pb
, gxf
->nb_fields
);
304 avio_w8(pb
, MAT_MARK_IN
);
308 avio_w8(pb
, MAT_MARK_OUT
);
310 avio_wb32(pb
, gxf
->nb_fields
);
313 avio_w8(pb
, MAT_SIZE
);
315 avio_wb32(pb
, avio_size(pb
) / 1024);
317 return updateSize(pb
, pos
);
320 static int gxf_write_track_description_section(AVFormatContext
*s
)
322 GXFContext
*gxf
= s
->priv_data
;
323 AVIOContext
*pb
= s
->pb
;
328 avio_wb16(pb
, 0); /* size */
329 for (i
= 0; i
< s
->nb_streams
; ++i
)
330 gxf_write_track_description(s
, s
->streams
[i
]->priv_data
, i
);
332 gxf_write_track_description(s
, &gxf
->timecode_track
, s
->nb_streams
);
334 return updateSize(pb
, pos
);
337 static int gxf_write_map_packet(AVFormatContext
*s
, int rewrite
)
339 GXFContext
*gxf
= s
->priv_data
;
340 AVIOContext
*pb
= s
->pb
;
341 int64_t pos
= avio_tell(pb
);
344 if (!(gxf
->map_offsets_nb
% 30)) {
345 gxf
->map_offsets
= av_realloc(gxf
->map_offsets
,
346 (gxf
->map_offsets_nb
+30)*sizeof(*gxf
->map_offsets
));
347 if (!gxf
->map_offsets
) {
348 av_log(s
, AV_LOG_ERROR
, "could not realloc map offsets\n");
352 gxf
->map_offsets
[gxf
->map_offsets_nb
++] = pos
; // do not increment here
355 gxf_write_packet_header(pb
, PKT_MAP
);
358 avio_w8(pb
, 0xE0); /* version */
359 avio_w8(pb
, 0xFF); /* reserved */
361 gxf_write_material_data_section(s
);
362 gxf_write_track_description_section(s
);
364 return updatePacketSize(pb
, pos
);
367 static int gxf_write_flt_packet(AVFormatContext
*s
)
369 GXFContext
*gxf
= s
->priv_data
;
370 AVIOContext
*pb
= s
->pb
;
371 int64_t pos
= avio_tell(pb
);
372 int fields_per_flt
= (gxf
->nb_fields
+1) / 1000 + 1;
373 int flt_entries
= gxf
->nb_fields
/ fields_per_flt
;
376 gxf_write_packet_header(pb
, PKT_FLT
);
378 avio_wl32(pb
, fields_per_flt
); /* number of fields */
379 avio_wl32(pb
, flt_entries
); /* number of active flt entries */
381 if (gxf
->flt_entries
) {
382 for (i
= 0; i
< flt_entries
; i
++)
383 avio_wl32(pb
, gxf
->flt_entries
[(i
*fields_per_flt
)>>1]);
386 for (; i
< 1000; i
++)
389 return updatePacketSize(pb
, pos
);
392 static int gxf_write_umf_material_description(AVFormatContext
*s
)
394 GXFContext
*gxf
= s
->priv_data
;
395 AVIOContext
*pb
= s
->pb
;
396 int timecode_base
= gxf
->time_base
.den
== 60000 ? 60 : 50;
397 int64_t timestamp
= 0;
398 AVDictionaryEntry
*t
;
401 if (t
= av_dict_get(s
->metadata
, "creation_time", NULL
, 0))
402 timestamp
= ff_iso8601_to_unix_time(t
->value
);
406 gxf
->nb_fields
/ (timecode_base
* 3600) % 24 << 24 | // hours
407 gxf
->nb_fields
/ (timecode_base
* 60) % 60 << 16 | // minutes
408 gxf
->nb_fields
/ timecode_base
% 60 << 8 | // seconds
409 gxf
->nb_fields
% timecode_base
; // fields
411 avio_wl32(pb
, gxf
->flags
);
412 avio_wl32(pb
, gxf
->nb_fields
); /* length of the longest track */
413 avio_wl32(pb
, gxf
->nb_fields
); /* length of the shortest track */
414 avio_wl32(pb
, 0); /* mark in */
415 avio_wl32(pb
, gxf
->nb_fields
); /* mark out */
416 avio_wl32(pb
, 0); /* timecode mark in */
417 avio_wl32(pb
, timecode
); /* timecode mark out */
418 avio_wl64(pb
, timestamp
); /* modification time */
419 avio_wl64(pb
, timestamp
); /* creation time */
420 avio_wl16(pb
, 0); /* reserved */
421 avio_wl16(pb
, 0); /* reserved */
422 avio_wl16(pb
, gxf
->audio_tracks
);
423 avio_wl16(pb
, 1); /* timecode track count */
424 avio_wl16(pb
, 0); /* reserved */
425 avio_wl16(pb
, gxf
->mpeg_tracks
);
429 static int gxf_write_umf_payload(AVFormatContext
*s
)
431 GXFContext
*gxf
= s
->priv_data
;
432 AVIOContext
*pb
= s
->pb
;
434 avio_wl32(pb
, gxf
->umf_length
); /* total length of the umf data */
435 avio_wl32(pb
, 3); /* version */
436 avio_wl32(pb
, s
->nb_streams
+1);
437 avio_wl32(pb
, gxf
->umf_track_offset
); /* umf track section offset */
438 avio_wl32(pb
, gxf
->umf_track_size
);
439 avio_wl32(pb
, s
->nb_streams
+1);
440 avio_wl32(pb
, gxf
->umf_media_offset
);
441 avio_wl32(pb
, gxf
->umf_media_size
);
442 avio_wl32(pb
, gxf
->umf_length
); /* user data offset */
443 avio_wl32(pb
, 0); /* user data size */
444 avio_wl32(pb
, 0); /* reserved */
445 avio_wl32(pb
, 0); /* reserved */
449 static int gxf_write_umf_track_description(AVFormatContext
*s
)
451 AVIOContext
*pb
= s
->pb
;
452 GXFContext
*gxf
= s
->priv_data
;
453 int64_t pos
= avio_tell(pb
);
456 gxf
->umf_track_offset
= pos
- gxf
->umf_start_offset
;
457 for (i
= 0; i
< s
->nb_streams
; ++i
) {
458 GXFStreamContext
*sc
= s
->streams
[i
]->priv_data
;
459 avio_wl16(pb
, sc
->media_info
);
463 avio_wl16(pb
, gxf
->timecode_track
.media_info
);
466 return avio_tell(pb
) - pos
;
469 static int gxf_write_umf_media_mpeg(AVIOContext
*pb
, AVStream
*st
)
471 GXFStreamContext
*sc
= st
->priv_data
;
473 if (st
->codec
->pix_fmt
== AV_PIX_FMT_YUV422P
)
476 avio_wl32(pb
, 1); /* default to 420 */
477 avio_wl32(pb
, sc
->first_gop_closed
== 1); /* closed = 1, open = 0, unknown = 255 */
478 avio_wl32(pb
, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
479 avio_wl32(pb
, 1); /* I picture per GOP */
480 avio_wl32(pb
, sc
->p_per_gop
);
481 avio_wl32(pb
, sc
->b_per_i_or_p
);
482 if (st
->codec
->codec_id
== AV_CODEC_ID_MPEG2VIDEO
)
484 else if (st
->codec
->codec_id
== AV_CODEC_ID_MPEG1VIDEO
)
488 avio_wl32(pb
, 0); /* reserved */
492 static int gxf_write_umf_media_timecode(AVIOContext
*pb
, GXFStreamContext
*sc
)
494 avio_wl32(pb
, 1); /* non drop frame */
495 avio_wl32(pb
, 0); /* reserved */
496 avio_wl32(pb
, 0); /* reserved */
497 avio_wl32(pb
, 0); /* reserved */
498 avio_wl32(pb
, 0); /* reserved */
499 avio_wl32(pb
, 0); /* reserved */
500 avio_wl32(pb
, 0); /* reserved */
501 avio_wl32(pb
, 0); /* reserved */
505 static int gxf_write_umf_media_dv(AVIOContext
*pb
, GXFStreamContext
*sc
)
509 for (i
= 0; i
< 8; i
++) {
515 static int gxf_write_umf_media_audio(AVIOContext
*pb
, GXFStreamContext
*sc
)
517 avio_wl64(pb
, av_double2int(1)); /* sound level to begin to */
518 avio_wl64(pb
, av_double2int(1)); /* sound level to begin to */
519 avio_wl32(pb
, 0); /* number of fields over which to ramp up sound level */
520 avio_wl32(pb
, 0); /* number of fields over which to ramp down sound level */
521 avio_wl32(pb
, 0); /* reserved */
522 avio_wl32(pb
, 0); /* reserved */
526 static int gxf_write_umf_media_description(AVFormatContext
*s
)
528 GXFContext
*gxf
= s
->priv_data
;
529 AVIOContext
*pb
= s
->pb
;
534 gxf
->umf_media_offset
= pos
- gxf
->umf_start_offset
;
535 for (i
= 0; i
<= s
->nb_streams
; ++i
) {
536 GXFStreamContext
*sc
;
537 int64_t startpos
, curpos
;
539 if (i
== s
->nb_streams
)
540 sc
= &gxf
->timecode_track
;
542 sc
= s
->streams
[i
]->priv_data
;
544 startpos
= avio_tell(pb
);
545 avio_wl16(pb
, 0); /* length */
546 avio_wl16(pb
, sc
->media_info
);
547 avio_wl16(pb
, 0); /* reserved */
548 avio_wl16(pb
, 0); /* reserved */
549 avio_wl32(pb
, gxf
->nb_fields
);
550 avio_wl32(pb
, 0); /* attributes rw, ro */
551 avio_wl32(pb
, 0); /* mark in */
552 avio_wl32(pb
, gxf
->nb_fields
); /* mark out */
553 avio_write(pb
, ES_NAME_PATTERN
, strlen(ES_NAME_PATTERN
));
554 avio_wb16(pb
, sc
->media_info
);
555 for (j
= strlen(ES_NAME_PATTERN
)+2; j
< 88; j
++)
557 avio_wl32(pb
, sc
->track_type
);
558 avio_wl32(pb
, sc
->sample_rate
);
559 avio_wl32(pb
, sc
->sample_size
);
560 avio_wl32(pb
, 0); /* reserved */
562 if (sc
== &gxf
->timecode_track
)
563 gxf_write_umf_media_timecode(pb
, sc
); /* 8 0bytes */
565 AVStream
*st
= s
->streams
[i
];
566 switch (st
->codec
->codec_id
) {
567 case AV_CODEC_ID_MPEG1VIDEO
:
568 case AV_CODEC_ID_MPEG2VIDEO
:
569 gxf_write_umf_media_mpeg(pb
, st
);
571 case AV_CODEC_ID_PCM_S16LE
:
572 gxf_write_umf_media_audio(pb
, sc
);
574 case AV_CODEC_ID_DVVIDEO
:
575 gxf_write_umf_media_dv(pb
, sc
);
580 curpos
= avio_tell(pb
);
581 avio_seek(pb
, startpos
, SEEK_SET
);
582 avio_wl16(pb
, curpos
- startpos
);
583 avio_seek(pb
, curpos
, SEEK_SET
);
585 return avio_tell(pb
) - pos
;
588 static int gxf_write_umf_packet(AVFormatContext
*s
)
590 GXFContext
*gxf
= s
->priv_data
;
591 AVIOContext
*pb
= s
->pb
;
592 int64_t pos
= avio_tell(pb
);
594 gxf_write_packet_header(pb
, PKT_UMF
);
597 avio_w8(pb
, 3); /* first and last (only) packet */
598 avio_wb32(pb
, gxf
->umf_length
); /* data length */
600 gxf
->umf_start_offset
= avio_tell(pb
);
601 gxf_write_umf_payload(s
);
602 gxf_write_umf_material_description(s
);
603 gxf
->umf_track_size
= gxf_write_umf_track_description(s
);
604 gxf
->umf_media_size
= gxf_write_umf_media_description(s
);
605 gxf
->umf_length
= avio_tell(pb
) - gxf
->umf_start_offset
;
606 return updatePacketSize(pb
, pos
);
609 static const int GXF_samples_per_frame
[] = { 32768, 0 };
611 static void gxf_init_timecode_track(GXFStreamContext
*sc
, GXFStreamContext
*vsc
)
616 sc
->media_type
= vsc
->sample_rate
== 60 ? 7 : 8;
617 sc
->sample_rate
= vsc
->sample_rate
;
618 sc
->media_info
= ('T'<<8) | '0';
620 sc
->frame_rate_index
= vsc
->frame_rate_index
;
621 sc
->lines_index
= vsc
->lines_index
;
622 sc
->sample_size
= 16;
623 sc
->fields
= vsc
->fields
;
626 static int gxf_write_header(AVFormatContext
*s
)
628 AVIOContext
*pb
= s
->pb
;
629 GXFContext
*gxf
= s
->priv_data
;
630 GXFStreamContext
*vsc
= NULL
;
631 uint8_t tracks
[255] = {0};
632 int i
, media_info
= 0;
635 av_log(s
, AV_LOG_ERROR
, "gxf muxer does not support streamed output, patch welcome");
639 gxf
->flags
|= 0x00080000; /* material is simple clip */
640 for (i
= 0; i
< s
->nb_streams
; ++i
) {
641 AVStream
*st
= s
->streams
[i
];
642 GXFStreamContext
*sc
= av_mallocz(sizeof(*sc
));
644 return AVERROR(ENOMEM
);
647 sc
->media_type
= ff_codec_get_tag(gxf_media_types
, st
->codec
->codec_id
);
648 if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
649 if (st
->codec
->codec_id
!= AV_CODEC_ID_PCM_S16LE
) {
650 av_log(s
, AV_LOG_ERROR
, "only 16 BIT PCM LE allowed for now\n");
653 if (st
->codec
->sample_rate
!= 48000) {
654 av_log(s
, AV_LOG_ERROR
, "only 48000hz sampling rate is allowed\n");
657 if (st
->codec
->channels
!= 1) {
658 av_log(s
, AV_LOG_ERROR
, "only mono tracks are allowed\n");
662 sc
->sample_rate
= st
->codec
->sample_rate
;
663 avpriv_set_pts_info(st
, 64, 1, sc
->sample_rate
);
664 sc
->sample_size
= 16;
665 sc
->frame_rate_index
= -2;
666 sc
->lines_index
= -2;
669 gxf
->flags
|= 0x04000000; /* audio is 16 bit pcm */
671 } else if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
673 av_log(s
, AV_LOG_ERROR
, "video stream must be the first track\n");
676 /* FIXME check from time_base ? */
677 if (st
->codec
->height
== 480 || st
->codec
->height
== 512) { /* NTSC or NTSC+VBI */
678 sc
->frame_rate_index
= 5;
679 sc
->sample_rate
= 60;
680 gxf
->flags
|= 0x00000080;
681 gxf
->time_base
= (AVRational
){ 1001, 60000 };
682 } else if (st
->codec
->height
== 576 || st
->codec
->height
== 608) { /* PAL or PAL+VBI */
683 sc
->frame_rate_index
= 6;
685 sc
->sample_rate
= 50;
686 gxf
->flags
|= 0x00000040;
687 gxf
->time_base
= (AVRational
){ 1, 50 };
689 av_log(s
, AV_LOG_ERROR
, "unsupported video resolution, "
690 "gxf muxer only accepts PAL or NTSC resolutions currently\n");
693 avpriv_set_pts_info(st
, 64, gxf
->time_base
.num
, gxf
->time_base
.den
);
694 if (gxf_find_lines_index(st
) < 0)
695 sc
->lines_index
= -1;
696 sc
->sample_size
= st
->codec
->bit_rate
;
697 sc
->fields
= 2; /* interlaced */
701 switch (st
->codec
->codec_id
) {
702 case AV_CODEC_ID_MJPEG
:
704 gxf
->flags
|= 0x00004000;
707 case AV_CODEC_ID_MPEG1VIDEO
:
712 case AV_CODEC_ID_MPEG2VIDEO
:
713 sc
->first_gop_closed
= -1;
716 gxf
->flags
|= 0x00008000;
719 case AV_CODEC_ID_DVVIDEO
:
720 if (st
->codec
->pix_fmt
== AV_PIX_FMT_YUV422P
) {
723 gxf
->flags
|= 0x00002000;
727 gxf
->flags
|= 0x00001000;
732 av_log(s
, AV_LOG_ERROR
, "video codec not supported\n");
736 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
737 sc
->media_info
= media_info
<<8 | ('0'+tracks
[media_info
]++);
738 sc
->order
= s
->nb_streams
- st
->index
;
741 if (ff_audio_interleave_init(s
, GXF_samples_per_frame
, (AVRational
){ 1, 48000 }) < 0)
744 gxf_init_timecode_track(&gxf
->timecode_track
, vsc
);
745 gxf
->flags
|= 0x200000; // time code track is non-drop frame
747 gxf_write_map_packet(s
, 0);
748 gxf_write_flt_packet(s
);
749 gxf_write_umf_packet(s
);
751 gxf
->packet_count
= 3;
757 static int gxf_write_eos_packet(AVIOContext
*pb
)
759 int64_t pos
= avio_tell(pb
);
761 gxf_write_packet_header(pb
, PKT_EOS
);
762 return updatePacketSize(pb
, pos
);
765 static int gxf_write_trailer(AVFormatContext
*s
)
767 GXFContext
*gxf
= s
->priv_data
;
768 AVIOContext
*pb
= s
->pb
;
772 ff_audio_interleave_close(s
);
774 gxf_write_eos_packet(pb
);
776 avio_seek(pb
, 0, SEEK_SET
);
777 /* overwrite map, flt and umf packets with new values */
778 gxf_write_map_packet(s
, 1);
779 gxf_write_flt_packet(s
);
780 gxf_write_umf_packet(s
);
782 /* update duration in all map packets */
783 for (i
= 1; i
< gxf
->map_offsets_nb
; i
++) {
784 avio_seek(pb
, gxf
->map_offsets
[i
], SEEK_SET
);
785 gxf_write_map_packet(s
, 1);
789 avio_seek(pb
, end
, SEEK_SET
);
791 av_freep(&gxf
->flt_entries
);
792 av_freep(&gxf
->map_offsets
);
797 static int gxf_parse_mpeg_frame(GXFStreamContext
*sc
, const uint8_t *buf
, int size
)
801 for(i
=0; i
<size
-4 && c
!=0x100; i
++){
803 if(c
== 0x1B8 && sc
->first_gop_closed
== -1) /* GOP start code */
804 sc
->first_gop_closed
= (buf
[i
+4]>>6)&1;
806 return (buf
[i
+1]>>3)&7;
809 static int gxf_write_media_preamble(AVFormatContext
*s
, AVPacket
*pkt
, int size
)
811 GXFContext
*gxf
= s
->priv_data
;
812 AVIOContext
*pb
= s
->pb
;
813 AVStream
*st
= s
->streams
[pkt
->stream_index
];
814 GXFStreamContext
*sc
= st
->priv_data
;
816 /* If the video is frame-encoded, the frame numbers shall be represented by
817 * even field numbers.
818 * see SMPTE360M-2004 6.4.2.1.3 Media field number */
819 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
820 field_nb
= gxf
->nb_fields
;
822 field_nb
= av_rescale_rnd(pkt
->dts
, gxf
->time_base
.den
,
823 (int64_t)48000*gxf
->time_base
.num
, AV_ROUND_UP
);
826 avio_w8(pb
, sc
->media_type
);
827 avio_w8(pb
, st
->index
);
828 avio_wb32(pb
, field_nb
);
829 if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
831 avio_wb16(pb
, size
/ 2);
832 } else if (st
->codec
->codec_id
== AV_CODEC_ID_MPEG2VIDEO
) {
833 int frame_type
= gxf_parse_mpeg_frame(sc
, pkt
->data
, pkt
->size
);
834 if (frame_type
== AV_PICTURE_TYPE_I
) {
837 } else if (frame_type
== AV_PICTURE_TYPE_B
) {
845 } else if (st
->codec
->codec_id
== AV_CODEC_ID_DVVIDEO
) {
846 avio_w8(pb
, size
/ 4096);
850 avio_wb32(pb
, field_nb
);
851 avio_w8(pb
, 1); /* flags */
852 avio_w8(pb
, 0); /* reserved */
856 static int gxf_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
858 GXFContext
*gxf
= s
->priv_data
;
859 AVIOContext
*pb
= s
->pb
;
860 AVStream
*st
= s
->streams
[pkt
->stream_index
];
861 int64_t pos
= avio_tell(pb
);
863 int packet_start_offset
= avio_tell(pb
) / 1024;
865 gxf_write_packet_header(pb
, PKT_MEDIA
);
866 if (st
->codec
->codec_id
== AV_CODEC_ID_MPEG2VIDEO
&& pkt
->size
% 4) /* MPEG-2 frames must be padded */
867 padding
= 4 - pkt
->size
% 4;
868 else if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
)
869 padding
= GXF_AUDIO_PACKET_SIZE
- pkt
->size
;
870 gxf_write_media_preamble(s
, pkt
, pkt
->size
+ padding
);
871 avio_write(pb
, pkt
->data
, pkt
->size
);
872 gxf_write_padding(pb
, padding
);
874 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
875 if (!(gxf
->flt_entries_nb
% 500)) {
876 gxf
->flt_entries
= av_realloc(gxf
->flt_entries
,
877 (gxf
->flt_entries_nb
+500)*sizeof(*gxf
->flt_entries
));
878 if (!gxf
->flt_entries
) {
879 av_log(s
, AV_LOG_ERROR
, "could not reallocate flt entries\n");
883 gxf
->flt_entries
[gxf
->flt_entries_nb
++] = packet_start_offset
;
884 gxf
->nb_fields
+= 2; // count fields
887 updatePacketSize(pb
, pos
);
890 if (gxf
->packet_count
== 100) {
891 gxf_write_map_packet(s
, 0);
892 gxf
->packet_count
= 0;
900 static int gxf_compare_field_nb(AVFormatContext
*s
, AVPacket
*next
, AVPacket
*cur
)
902 GXFContext
*gxf
= s
->priv_data
;
903 AVPacket
*pkt
[2] = { cur
, next
};
905 GXFStreamContext
*sc
[2];
907 for (i
= 0; i
< 2; i
++) {
908 AVStream
*st
= s
->streams
[pkt
[i
]->stream_index
];
909 sc
[i
] = st
->priv_data
;
910 if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
911 field_nb
[i
] = av_rescale_rnd(pkt
[i
]->dts
, gxf
->time_base
.den
,
912 (int64_t)48000*gxf
->time_base
.num
, AV_ROUND_UP
);
913 field_nb
[i
] &= ~1; // compare against even field number because audio must be before video
915 field_nb
[i
] = pkt
[i
]->dts
; // dts are field based
918 return field_nb
[1] > field_nb
[0] ||
919 (field_nb
[1] == field_nb
[0] && sc
[1]->order
> sc
[0]->order
);
922 static int gxf_interleave_packet(AVFormatContext
*s
, AVPacket
*out
, AVPacket
*pkt
, int flush
)
924 if (pkt
&& s
->streams
[pkt
->stream_index
]->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
)
925 pkt
->duration
= 2; // enforce 2 fields
926 return ff_audio_rechunk_interleave(s
, out
, pkt
, flush
,
927 ff_interleave_packet_per_dts
, gxf_compare_field_nb
);
930 AVOutputFormat ff_gxf_muxer
= {
932 .long_name
= NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"),
934 .priv_data_size
= sizeof(GXFContext
),
935 .audio_codec
= AV_CODEC_ID_PCM_S16LE
,
936 .video_codec
= AV_CODEC_ID_MPEG2VIDEO
,
937 .write_header
= gxf_write_header
,
938 .write_packet
= gxf_write_packet
,
939 .write_trailer
= gxf_write_trailer
,
940 .interleave_packet
= gxf_interleave_packet
,