[FFmpeg-devel] [PATCH 02/12] avformat/mxfenc: write index delta entry array needed by sony vegas pro 11
Baptiste Coudurier
baptiste.coudurier at gmail.com
Wed Jul 4 21:35:04 EEST 2018
---
libavformat/mxfenc.c | 208 ++++++++++++++------------------
tests/ref/lavf/mxf | 6 +-
tests/ref/lavf/mxf_d10 | 2 +-
tests/ref/lavf/mxf_dv25 | 2 +-
tests/ref/lavf/mxf_dvcpro50 | 2 +-
tests/ref/lavf/mxf_opatom | 2 +-
tests/ref/lavf/mxf_opatom_audio | 2 +-
7 files changed, 101 insertions(+), 123 deletions(-)
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index b98d234f03..8c1e38353c 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -93,6 +93,8 @@ typedef struct MXFStreamContext {
AVRational aspect_ratio; ///< display aspect ratio
int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
int video_bit_rate;
+ int slice_offset;
+ int frame_size; ///< frame size in bytes
} MXFStreamContext;
typedef struct MXFContainerEssenceEntry {
@@ -389,6 +391,7 @@ typedef struct MXFContext {
AVRational audio_edit_rate;
int store_user_comments;
int track_instance_count; // used to generate MXFTrack uuids
+ int cbr_index; ///< use a constant bitrate index
} MXFContext;
static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
@@ -1696,6 +1699,7 @@ static void mxf_write_index_table_segment(AVFormatContext *s)
AVIOContext *pb = s->pb;
int i, j, temporal_reordering = 0;
int key_index = mxf->last_key_index;
+ int64_t pos;
av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
@@ -1704,12 +1708,8 @@ static void mxf_write_index_table_segment(AVFormatContext *s)
avio_write(pb, index_table_segment_key, 16);
- if (mxf->edit_unit_byte_count) {
- klv_encode_ber_length(pb, 80);
- } else {
- klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1LL)*6 +
- 12+mxf->edit_units_count*(11+mxf->slice_count*4LL));
- }
+ klv_encode_ber4_length(pb, 0);
+ pos = avio_tell(pb);
// instance id
mxf_write_local_tag(pb, 16, 0x3C0A);
@@ -1743,39 +1743,43 @@ static void mxf_write_index_table_segment(AVFormatContext *s)
mxf_write_local_tag(pb, 4, 0x3F07);
avio_wb32(pb, 1);
- if (!mxf->edit_unit_byte_count) {
- // real slice count - 1
- mxf_write_local_tag(pb, 1, 0x3F08);
- avio_w8(pb, mxf->slice_count);
-
- // delta entry array
- mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
- avio_wb32(pb, s->nb_streams+1); // num of entries
- avio_wb32(pb, 6); // size of one entry
- // write system item delta entry
- avio_w8(pb, 0);
- avio_w8(pb, 0); // slice entry
- avio_wb32(pb, 0); // element delta
- for (i = 0; i < s->nb_streams; i++) {
- AVStream *st = s->streams[i];
- MXFStreamContext *sc = st->priv_data;
- avio_w8(pb, sc->temporal_reordering);
- if (sc->temporal_reordering)
- temporal_reordering = 1;
- if (i == 0) { // video track
- avio_w8(pb, 0); // slice number
- avio_wb32(pb, KAG_SIZE); // system item size including klv fill
- } else { // audio track
- unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
- audio_frame_size += klv_fill_size(audio_frame_size);
- avio_w8(pb, 1);
- avio_wb32(pb, (i-1)*audio_frame_size); // element delta
- }
+ // real slice count - 1
+ mxf_write_local_tag(pb, 1, 0x3F08);
+ avio_w8(pb, !mxf->edit_unit_byte_count); // only one slice for CBR
+
+ // delta entry array
+ mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
+ avio_wb32(pb, s->nb_streams+1); // num of entries
+ avio_wb32(pb, 6); // size of one entry
+ // write system item delta entry
+ avio_w8(pb, 0);
+ avio_w8(pb, 0); // slice entry
+ avio_wb32(pb, 0); // element delta
+ // write each stream delta entry
+ for (i = 0; i < s->nb_streams; i++) {
+ AVStream *st = s->streams[i];
+ MXFStreamContext *sc = st->priv_data;
+ avio_w8(pb, sc->temporal_reordering);
+ if (sc->temporal_reordering)
+ temporal_reordering = 1;
+ if (mxf->edit_unit_byte_count) {
+ avio_w8(pb, 0); // slice number
+ avio_wb32(pb, sc->slice_offset);
+ } else if (i == 0) { // video track
+ avio_w8(pb, 0); // slice number
+ avio_wb32(pb, KAG_SIZE); // system item size including klv fill
+ } else { // audio track
+ unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
+ audio_frame_size += klv_fill_size(audio_frame_size);
+ avio_w8(pb, 1);
+ avio_wb32(pb, (i-1)*audio_frame_size); // element delta
}
+ }
- mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
+ if (!mxf->edit_unit_byte_count) {
+ mxf_write_local_tag(pb, 8 + mxf->edit_units_count*15, 0x3F0A);
avio_wb32(pb, mxf->edit_units_count); // num of entries
- avio_wb32(pb, 11+mxf->slice_count*4); // size of one entry
+ avio_wb32(pb, 15); // size of one entry
for (i = 0; i < mxf->edit_units_count; i++) {
int temporal_offset = 0;
@@ -1815,12 +1819,16 @@ static void mxf_write_index_table_segment(AVFormatContext *s)
avio_wb64(pb, mxf->index_entries[i].offset);
if (s->nb_streams > 1)
avio_wb32(pb, mxf->index_entries[i].slice_offset);
+ else
+ avio_wb32(pb, 0);
}
mxf->last_key_index = key_index - mxf->edit_units_count;
mxf->last_indexed_edit_unit += mxf->edit_units_count;
mxf->edit_units_count = 0;
}
+
+ mxf_update_klv_size(pb, pos);
}
static void mxf_write_klv_fill(AVFormatContext *s)
@@ -1848,7 +1856,7 @@ static int mxf_write_partition(AVFormatContext *s, int bodysid,
if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
- 12+mxf->edit_units_count*(11+mxf->slice_count*4);
+ 12+mxf->edit_units_count*15;
else if (mxf->edit_unit_byte_count && indexsid)
index_byte_count = 80;
@@ -2004,24 +2012,7 @@ AVPacket *pkt)
sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
sc->aspect_ratio = (AVRational){ 16, 9 };
-
- if (s->oformat == &ff_mxf_opatom_muxer) {
- mxf->edit_unit_byte_count = frame_size;
- return 1;
- }
-
- mxf->edit_unit_byte_count = KAG_SIZE;
- for (i = 0; i < s->nb_streams; i++) {
- AVStream *st = s->streams[i];
- MXFStreamContext *sc = st->priv_data;
- if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
- mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
- mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
- } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
- mxf->edit_unit_byte_count += 16 + 4 + frame_size;
- mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
- }
- }
+ sc->frame_size = pkt->size;
return 1;
}
@@ -2088,24 +2079,7 @@ static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
sc->index = ul_index;
sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
-
- if(s->oformat == &ff_mxf_opatom_muxer) {
- mxf->edit_unit_byte_count = frame_size;
- return 1;
- }
-
- mxf->edit_unit_byte_count = KAG_SIZE;
- for (i = 0; i < s->nb_streams; i++) {
- AVStream *st = s->streams[i];
- MXFStreamContext *sc = st->priv_data;
- if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
- mxf->edit_unit_byte_count += 16 + 4 + sc->aic.samples[0]*sc->aic.sample_size;
- mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
- } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
- mxf->edit_unit_byte_count += 16 + 4 + frame_size;
- mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
- }
- }
+ sc->frame_size = pkt->size;
return 1;
}
@@ -2410,6 +2384,12 @@ static int mxf_write_header(AVFormatContext *s)
return ret;
sc->video_bit_rate = st->codecpar->bit_rate;
+
+ if (s->oformat == &ff_mxf_d10_muxer ||
+ st->codecpar->codec_id == AV_CODEC_ID_DNXHD ||
+ st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO)
+ mxf->cbr_index = 1;
+
if (s->oformat == &ff_mxf_d10_muxer) {
if (st->codecpar->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
av_log(s, AV_LOG_ERROR, "error MXF D-10 only support MPEG-2 Video\n");
@@ -2430,15 +2410,10 @@ static int mxf_write_header(AVFormatContext *s)
return -1;
}
- mxf->edit_unit_byte_count = KAG_SIZE; // system element
- mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)sc->video_bit_rate *
- mxf->time_base.num / (8*mxf->time_base.den);
- mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
- mxf->edit_unit_byte_count += 16 + 4 + 4 + spf->samples_per_frame[0]*8*4;
- mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
-
sc->signal_standard = 1;
sc->color_siting = 0;
+ sc->frame_size = (int64_t)sc->video_bit_rate *
+ mxf->time_base.num / (8*mxf->time_base.den);
}
if (mxf->signal_standard >= 0)
sc->signal_standard = mxf->signal_standard;
@@ -2458,6 +2433,7 @@ static int mxf_write_header(AVFormatContext *s)
av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
}
sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
+ sc->frame_size = 4 + 8 * spf[0].samples_per_frame[0] * 4;
} else if (s->oformat == &ff_mxf_opatom_muxer) {
AVRational tbc = av_inv_q(mxf->audio_edit_rate);
@@ -2486,6 +2462,8 @@ static int mxf_write_header(AVFormatContext *s)
sc->index = INDEX_WAV;
} else {
mxf->slice_count = 1;
+ sc->frame_size = (st->codecpar->channels * spf[0].samples_per_frame[0] *
+ av_get_bits_per_sample(st->codecpar->codec_id)) / 8;
}
}
@@ -2591,35 +2569,6 @@ static void mxf_write_system_item(AVFormatContext *s)
mxf_write_umid(s, 1);
}
-static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
-{
- MXFContext *mxf = s->priv_data;
- AVIOContext *pb = s->pb;
- MXFStreamContext *sc = st->priv_data;
- int packet_size = (uint64_t)sc->video_bit_rate*mxf->time_base.num /
- (8*mxf->time_base.den); // frame size
- int pad;
-
- packet_size += 16 + 4;
- packet_size += klv_fill_size(packet_size);
-
- klv_encode_ber4_length(pb, pkt->size);
- avio_write(pb, pkt->data, pkt->size);
-
- // ensure CBR muxing by padding to correct video frame size
- pad = packet_size - pkt->size - 16 - 4;
- if (pad > 20) {
- avio_write(s->pb, klv_fill_key, 16);
- pad -= 16 + 4;
- klv_encode_ber4_length(s->pb, pad);
- ffio_fill(s->pb, 0, pad);
- av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
- } else {
- av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
- ffio_fill(s->pb, 0, pad);
- }
-}
-
static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
MXFContext *mxf = s->priv_data;
@@ -2703,6 +2652,27 @@ static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEn
return 0;
}
+static void mxf_compute_edit_unit_byte_count(AVFormatContext *s)
+{
+ MXFContext *mxf = s->priv_data;
+ int i;
+
+ if (s->oformat == &ff_mxf_opatom_muxer) {
+ MXFStreamContext *sc = s->streams[0]->priv_data;
+ mxf->edit_unit_byte_count = sc->frame_size;
+ return;
+ }
+
+ mxf->edit_unit_byte_count = KAG_SIZE; // system element
+ for (i = 0; i < s->nb_streams; i++) {
+ AVStream *st = s->streams[i];
+ MXFStreamContext *sc = st->priv_data;
+ sc->slice_offset = mxf->edit_unit_byte_count;
+ mxf->edit_unit_byte_count += 16 + 4 + sc->frame_size;
+ mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
+ }
+}
+
static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
{
MXFContext *mxf = s->priv_data;
@@ -2712,7 +2682,7 @@ static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
MXFIndexEntry ie = {0};
int err;
- if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
+ if (!mxf->cbr_index && !mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
+ EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
mxf->edit_units_count = 0;
@@ -2743,6 +2713,16 @@ static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
}
}
+ if (mxf->cbr_index) {
+ if (pkt->size != sc->frame_size && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+ av_log(s, AV_LOG_ERROR, "track %d: frame size does not match index unit size, %d != %d\n",
+ st->index, pkt->size, sc->frame_size);
+ return -1;
+ }
+ if (!mxf->header_written)
+ mxf_compute_edit_unit_byte_count(s);
+ }
+
if (s->oformat == &ff_mxf_opatom_muxer)
return mxf_write_opatom_packet(s, pkt, &ie);
@@ -2791,11 +2771,9 @@ static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
mxf_write_klv_fill(s);
avio_write(pb, sc->track_essence_element_key, 16); // write key
- if (s->oformat == &ff_mxf_d10_muxer) {
- if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
- mxf_write_d10_video_packet(s, st, pkt);
- else
- mxf_write_d10_audio_packet(s, st, pkt);
+ if (s->oformat == &ff_mxf_d10_muxer &&
+ st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+ mxf_write_d10_audio_packet(s, st, pkt);
} else {
klv_encode_ber4_length(pb, pkt->size); // write length
avio_write(pb, pkt->data, pkt->size);
diff --git a/tests/ref/lavf/mxf b/tests/ref/lavf/mxf
index 30a7846143..e99f737ed8 100644
--- a/tests/ref/lavf/mxf
+++ b/tests/ref/lavf/mxf
@@ -1,9 +1,9 @@
-93367bc02b8997b84224e7eb359cae8a *./tests/data/lavf/lavf.mxf
+96c0f82082eb420ecbddf7810f3b3943 *./tests/data/lavf/lavf.mxf
526393 ./tests/data/lavf/lavf.mxf
./tests/data/lavf/lavf.mxf CRC=0x8dddfaab
-0087ea968d20cc9192456fcd4c00019e *./tests/data/lavf/lavf.mxf
+4b73c93955f4ae2fcddb2fc99e5e7017 *./tests/data/lavf/lavf.mxf
561721 ./tests/data/lavf/lavf.mxf
./tests/data/lavf/lavf.mxf CRC=0xf21b1b48
-7754550ff6fe5895555610f586a15fc5 *./tests/data/lavf/lavf.mxf
+8376d69f7eccfc268041dffd82db1a61 *./tests/data/lavf/lavf.mxf
526393 ./tests/data/lavf/lavf.mxf
./tests/data/lavf/lavf.mxf CRC=0x8dddfaab
diff --git a/tests/ref/lavf/mxf_d10 b/tests/ref/lavf/mxf_d10
index 4c272048a1..a40a42a2e2 100644
--- a/tests/ref/lavf/mxf_d10
+++ b/tests/ref/lavf/mxf_d10
@@ -1,3 +1,3 @@
-693c6e28c56607aba86e73da3d14cecf *./tests/data/lavf/lavf.mxf_d10
+ad3f0da2f77e5269e896b367f2a5b0b0 *./tests/data/lavf/lavf.mxf_d10
5332013 ./tests/data/lavf/lavf.mxf_d10
./tests/data/lavf/lavf.mxf_d10 CRC=0x6c74d488
diff --git a/tests/ref/lavf/mxf_dv25 b/tests/ref/lavf/mxf_dv25
index ac9147d284..efec757218 100644
--- a/tests/ref/lavf/mxf_dv25
+++ b/tests/ref/lavf/mxf_dv25
@@ -1,3 +1,3 @@
-37ad73c526ecc698dbae5a8995c7dd4d *./tests/data/lavf/lavf.mxf_dv25
+cd1ea9497444b0b43d930b0dc56aecf7 *./tests/data/lavf/lavf.mxf_dv25
3834413 ./tests/data/lavf/lavf.mxf_dv25
./tests/data/lavf/lavf.mxf_dv25 CRC=0xbdaf7f52
diff --git a/tests/ref/lavf/mxf_dvcpro50 b/tests/ref/lavf/mxf_dvcpro50
index 19740a9a30..bbe4562ec4 100644
--- a/tests/ref/lavf/mxf_dvcpro50
+++ b/tests/ref/lavf/mxf_dvcpro50
@@ -1,3 +1,3 @@
-8e989e5eb6f64741c1f3f2a9bfdb6f9a *./tests/data/lavf/lavf.mxf_dvcpro50
+ce601c92f0e2d7f07cbf6263a8a18ea5 *./tests/data/lavf/lavf.mxf_dvcpro50
7431213 ./tests/data/lavf/lavf.mxf_dvcpro50
./tests/data/lavf/lavf.mxf_dvcpro50 CRC=0xe3bbe4b4
diff --git a/tests/ref/lavf/mxf_opatom b/tests/ref/lavf/mxf_opatom
index 29b1b58843..bca8f65154 100644
--- a/tests/ref/lavf/mxf_opatom
+++ b/tests/ref/lavf/mxf_opatom
@@ -1,3 +1,3 @@
-b6e1fde300f68808ead5c316fd9a4543 *./tests/data/lavf/lavf.mxf_opatom
+76093fef1ac42587cc374012dc1945db *./tests/data/lavf/lavf.mxf_opatom
4717625 ./tests/data/lavf/lavf.mxf_opatom
./tests/data/lavf/lavf.mxf_opatom CRC=0xf55aa22a
diff --git a/tests/ref/lavf/mxf_opatom_audio b/tests/ref/lavf/mxf_opatom_audio
index a86aa991e9..380ce4f0a3 100644
--- a/tests/ref/lavf/mxf_opatom_audio
+++ b/tests/ref/lavf/mxf_opatom_audio
@@ -1,3 +1,3 @@
-cc0dc4aacd82f5495456000c4ce38b08 *./tests/data/lavf/lavf.mxf_opatom_audio
+8524f908a6fa8750a4755ab79cc1516c *./tests/data/lavf/lavf.mxf_opatom_audio
102969 ./tests/data/lavf/lavf.mxf_opatom_audio
./tests/data/lavf/lavf.mxf_opatom_audio CRC=0xd155c6ff
--
2.17.0 (Apple Git-106)
More information about the ffmpeg-devel
mailing list