Commit a96b1a90 authored by Baptiste Coudurier's avatar Baptiste Coudurier

correctly interleave audio and video according to specs

Originally committed as revision 17892 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 9885e9a6
...@@ -43,6 +43,7 @@ typedef struct GXFStreamContext { ...@@ -43,6 +43,7 @@ typedef struct GXFStreamContext {
int p_per_gop; int p_per_gop;
int b_per_i_or_p; ///< number of B frames per I frame or P frame int b_per_i_or_p; ///< number of B frames per I frame or P frame
int first_gop_closed; int first_gop_closed;
unsigned order; ///< interleaving order
} GXFStreamContext; } GXFStreamContext;
typedef struct GXFContext { typedef struct GXFContext {
...@@ -56,7 +57,7 @@ typedef struct GXFContext { ...@@ -56,7 +57,7 @@ typedef struct GXFContext {
uint32_t umf_length; uint32_t umf_length;
uint16_t umf_track_size; uint16_t umf_track_size;
uint16_t umf_media_size; uint16_t umf_media_size;
int sample_rate; AVRational time_base;
int flags; int flags;
GXFStreamContext timecode_track; GXFStreamContext timecode_track;
} GXFContext; } GXFContext;
...@@ -370,13 +371,14 @@ static int gxf_write_umf_material_description(AVFormatContext *s) ...@@ -370,13 +371,14 @@ static int gxf_write_umf_material_description(AVFormatContext *s)
{ {
GXFContext *gxf = s->priv_data; GXFContext *gxf = s->priv_data;
ByteIOContext *pb = s->pb; ByteIOContext *pb = s->pb;
int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
// XXX drop frame // XXX drop frame
uint32_t timecode = uint32_t timecode =
gxf->nb_fields / (gxf->sample_rate * 3600) % 24 << 24 | // hours gxf->nb_fields / (timecode_base * 3600) % 24 << 24 | // hours
gxf->nb_fields / (gxf->sample_rate * 60) % 60 << 16 | // minutes gxf->nb_fields / (timecode_base * 60) % 60 << 16 | // minutes
gxf->nb_fields / gxf->sample_rate % 60 << 8 | // seconds gxf->nb_fields / timecode_base % 60 << 8 | // seconds
gxf->nb_fields % gxf->sample_rate; // fields gxf->nb_fields % timecode_base; // fields
put_le32(pb, gxf->flags); put_le32(pb, gxf->flags);
put_le32(pb, gxf->nb_fields); /* length of the longest track */ put_le32(pb, gxf->nb_fields); /* length of the longest track */
...@@ -647,19 +649,24 @@ static int gxf_write_header(AVFormatContext *s) ...@@ -647,19 +649,24 @@ static int gxf_write_header(AVFormatContext *s)
gxf->flags |= 0x04000000; /* audio is 16 bit pcm */ gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
media_info = 'A'; media_info = 'A';
} else if (st->codec->codec_type == CODEC_TYPE_VIDEO) { } else if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
if (i != 0) {
av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
return -1;
}
/* FIXME check from time_base ? */ /* FIXME check from time_base ? */
if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */ if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
sc->frame_rate_index = 5; sc->frame_rate_index = 5;
sc->sample_rate = 60; sc->sample_rate = 60;
gxf->flags |= 0x00000080; gxf->flags |= 0x00000080;
gxf->time_base = (AVRational){ 1001, 60000 };
} else { /* assume PAL */ } else { /* assume PAL */
sc->frame_rate_index = 6; sc->frame_rate_index = 6;
sc->media_type++; sc->media_type++;
sc->sample_rate = 50; sc->sample_rate = 50;
gxf->flags |= 0x00000040; gxf->flags |= 0x00000040;
gxf->time_base = (AVRational){ 1, 50 };
} }
gxf->sample_rate = sc->sample_rate; av_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
av_set_pts_info(st, 64, 1, st->codec->time_base.den);
if (gxf_find_lines_index(st) < 0) if (gxf_find_lines_index(st) < 0)
sc->lines_index = -1; sc->lines_index = -1;
sc->sample_size = st->codec->bit_rate; sc->sample_size = st->codec->bit_rate;
...@@ -704,6 +711,7 @@ static int gxf_write_header(AVFormatContext *s) ...@@ -704,6 +711,7 @@ static int gxf_write_header(AVFormatContext *s)
} }
/* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */ /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
sc->media_info = media_info<<8 | ('0'+tracks[media_info]++); sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
sc->order = s->nb_streams - st->index;
} }
if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0) if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
...@@ -770,7 +778,8 @@ static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size) ...@@ -770,7 +778,8 @@ static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
if (st->codec->codec_type == CODEC_TYPE_VIDEO) { if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
field_nb = gxf->nb_fields; field_nb = gxf->nb_fields;
} else { } else {
field_nb = av_rescale_rnd(pkt->dts, gxf->sample_rate, st->codec->time_base.den, AV_ROUND_UP); field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
(int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
} }
put_byte(pb, sc->media_type); put_byte(pb, sc->media_type);
...@@ -828,10 +837,34 @@ static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -828,10 +837,34 @@ static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
return updatePacketSize(pb, pos); return updatePacketSize(pb, pos);
} }
static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
{
GXFContext *gxf = s->priv_data;
AVPacket *pkt[2] = { cur, next };
int i, field_nb[2];
GXFStreamContext *sc[2];
for (i = 0; i < 2; i++) {
AVStream *st = s->streams[pkt[i]->stream_index];
sc[i] = st->priv_data;
if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
(int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
field_nb[i] &= ~1; // compare against even field number because audio must be before video
} else
field_nb[i] = pkt[i]->dts; // dts are field based
}
return field_nb[1] > field_nb[0] ||
(field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
}
static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
{ {
if (pkt && s->streams[pkt->stream_index]->codec->codec_type == CODEC_TYPE_VIDEO)
pkt->duration = 2; // enforce 2 fields
return ff_audio_rechunk_interleave(s, out, pkt, flush, return ff_audio_rechunk_interleave(s, out, pkt, flush,
av_interleave_packet_per_dts, ff_interleave_compare_dts); av_interleave_packet_per_dts, gxf_compare_field_nb);
} }
AVOutputFormat gxf_muxer = { AVOutputFormat gxf_muxer = {
......
...@@ -27,7 +27,7 @@ af78858062599fcbba049e4a02588a15 *./tests/data/b-libav.mov ...@@ -27,7 +27,7 @@ af78858062599fcbba049e4a02588a15 *./tests/data/b-libav.mov
913b6199765b1dcba196706574621c2f *./tests/data/b-libav.dv 913b6199765b1dcba196706574621c2f *./tests/data/b-libav.dv
3600000 ./tests/data/b-libav.dv 3600000 ./tests/data/b-libav.dv
./tests/data/b-libav.dv CRC=0x7787e173 ./tests/data/b-libav.dv CRC=0x7787e173
d96ed125f8fe64cce77c605f94bf71c7 *./tests/data/b-libav.gxf 8a29994b83b6a00cfc0e0714fcf25705 *./tests/data/b-libav.gxf
806184 ./tests/data/b-libav.gxf 806184 ./tests/data/b-libav.gxf
./tests/data/b-libav.gxf CRC=0x9c06676e ./tests/data/b-libav.gxf CRC=0x9c06676e
66a6584f9e83e8ea3af822a3ba71fbbe *./tests/data/b-libav.nut 66a6584f9e83e8ea3af822a3ba71fbbe *./tests/data/b-libav.nut
......
...@@ -2907,15 +2907,15 @@ ret:-22 st:-1 ts:0.460008 flags:0 ...@@ -2907,15 +2907,15 @@ ret:-22 st:-1 ts:0.460008 flags:0
ret:-22 st:-1 ts:-0.645825 flags:1 ret:-22 st:-1 ts:-0.645825 flags:1
---------------- ----------------
tests/data/b-libav.gxf tests/data/b-libav.gxf
ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:940 size:55076 flags:1 ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:940 size:65536 flags:1
ret: 0 st:-1 ts:-1.000000 flags:0 ret: 0 st:-1 ts:-1.000000 flags:0
ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:56048 size:65536 flags:1 ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:66508 size:55076 flags:1
ret: 0 st:-1 ts:1.894167 flags:1 ret: 0 st:-1 ts:1.894167 flags:1
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st: 0 ts:0.780000 flags:0 ret: 0 st: 0 ts:0.780000 flags:0
ret: 0 st: 0 dts:0.800000 pts:-184467440737095520.000000 pos:661548 size:22568 flags:0 ret: 0 st: 0 dts:0.800000 pts:-184467440737095520.000000 pos:661548 size:22568 flags:0
ret: 0 st: 0 ts:-0.320000 flags:1 ret: 0 st: 0 ts:-0.320000 flags:1
ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:940 size:55076 flags:1 ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:940 size:65536 flags:1
ret: 0 st: 1 ts:2.580000 flags:0 ret: 0 st: 1 ts:2.580000 flags:0
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st: 1 ts:1.480000 flags:1 ret: 0 st: 1 ts:1.480000 flags:1
...@@ -2923,13 +2923,13 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f ...@@ -2923,13 +2923,13 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f
ret: 0 st: 2 ts:0.360000 flags:0 ret: 0 st: 2 ts:0.360000 flags:0
ret: 0 st: 0 dts:0.360000 pts:-184467440737095520.000000 pos:304084 size:23176 flags:0 ret: 0 st: 0 dts:0.360000 pts:-184467440737095520.000000 pos:304084 size:23176 flags:0
ret: 0 st: 2 ts:-0.740000 flags:1 ret: 0 st: 2 ts:-0.740000 flags:1
ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:940 size:55076 flags:1 ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:940 size:65536 flags:1
ret: 0 st:-1 ts:2.153336 flags:0 ret: 0 st:-1 ts:2.153336 flags:0
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st:-1 ts:1.047503 flags:1 ret: 0 st:-1 ts:1.047503 flags:1
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st: 0 ts:-0.060000 flags:0 ret: 0 st: 0 ts:-0.060000 flags:0
ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:56048 size:65536 flags:1 ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:66508 size:55076 flags:1
ret: 0 st: 0 ts:2.840000 flags:1 ret: 0 st: 0 ts:2.840000 flags:1
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st: 1 ts:1.740000 flags:0 ret: 0 st: 1 ts:1.740000 flags:0
...@@ -2937,7 +2937,7 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f ...@@ -2937,7 +2937,7 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f
ret: 0 st: 1 ts:0.620000 flags:1 ret: 0 st: 1 ts:0.620000 flags:1
ret: 0 st: 0 dts:0.600000 pts:-184467440737095520.000000 pos:476360 size:23728 flags:0 ret: 0 st: 0 dts:0.600000 pts:-184467440737095520.000000 pos:476360 size:23728 flags:0
ret: 0 st: 2 ts:-0.480000 flags:0 ret: 0 st: 2 ts:-0.480000 flags:0
ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:56048 size:65536 flags:1 ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:66508 size:55076 flags:1
ret: 0 st: 2 ts:2.420000 flags:1 ret: 0 st: 2 ts:2.420000 flags:1
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st:-1 ts:1.306672 flags:0 ret: 0 st:-1 ts:1.306672 flags:0
...@@ -2945,13 +2945,13 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f ...@@ -2945,13 +2945,13 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f
ret: 0 st:-1 ts:0.200839 flags:1 ret: 0 st:-1 ts:0.200839 flags:1
ret: 0 st: 0 dts:0.200000 pts:-184467440737095520.000000 pos:211244 size:20944 flags:0 ret: 0 st: 0 dts:0.200000 pts:-184467440737095520.000000 pos:211244 size:20944 flags:0
ret: 0 st: 0 ts:-0.900000 flags:0 ret: 0 st: 0 ts:-0.900000 flags:0
ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:56048 size:65536 flags:1 ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:66508 size:55076 flags:1
ret: 0 st: 0 ts:1.980000 flags:1 ret: 0 st: 0 ts:1.980000 flags:1
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st: 1 ts:0.880000 flags:0 ret: 0 st: 1 ts:0.880000 flags:0
ret: 0 st: 0 dts:0.880000 pts:-184467440737095520.000000 pos:706964 size:22456 flags:0 ret: 0 st: 0 dts:0.880000 pts:-184467440737095520.000000 pos:706964 size:22456 flags:0
ret: 0 st: 1 ts:-0.220000 flags:1 ret: 0 st: 1 ts:-0.220000 flags:1
ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:940 size:55076 flags:1 ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:940 size:65536 flags:1
ret: 0 st: 2 ts:2.680000 flags:0 ret: 0 st: 2 ts:2.680000 flags:0
ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1 ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 flags:1
ret: 0 st: 2 ts:1.560000 flags:1 ret: 0 st: 2 ts:1.560000 flags:1
...@@ -2959,7 +2959,7 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f ...@@ -2959,7 +2959,7 @@ ret: 0 st: 0 dts:0.960000 pts:-184467440737095520.000000 pos:751848 size:54320 f
ret: 0 st:-1 ts:0.460008 flags:0 ret: 0 st:-1 ts:0.460008 flags:0
ret: 0 st: 0 dts:0.480000 pts:-184467440737095520.000000 pos:371708 size:53804 flags:1 ret: 0 st: 0 dts:0.480000 pts:-184467440737095520.000000 pos:371708 size:53804 flags:1
ret: 0 st:-1 ts:-0.645825 flags:1 ret: 0 st:-1 ts:-0.645825 flags:1
ret: 0 st: 0 dts:0.000000 pts:-184467440737095520.000000 pos:940 size:55076 flags:1 ret: 0 st: 1 dts:0.000000 pts:0.000000 pos:940 size:65536 flags:1
---------------- ----------------
tests/data/b-libav.mkv tests/data/b-libav.mkv
ret: 0 st: 0 dts:0.000000 pts:0.000000 pos:505 size:28088 flags:1 ret: 0 st: 0 dts:0.000000 pts:0.000000 pos:505 size:28088 flags:1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment