[PATCH] MXF index table based seeking
Here is my first try to add image table based seeking to the MXF demuxer. What the patch basically does is: 1. Parse through the whole file in mxf_read_header (instead of breaking at the first essence container). If an index table is found, it is parsed and stored in an MXFIndexTableSegment. 2. In mxf_parse_structural_metadata, the MXFIndexTableSegments are parsed and an AVIndexEntry array is created from them Seeking is then done based on the AVIndexEntry array. What I didn't really understand is how to link between MXF tracks and the slices in the index table. Sometimes, there is a track for every slice in the index table, sometimes there is not. So this part of the code is probably wrong, but help on this is very appreciated! Regards, Georg
Georg Lippitsch skrev 2011-05-15 15:35:
Here is my first try to add image table based seeking to the MXF demuxer.
What the patch basically does is: 1. Parse through the whole file in mxf_read_header (instead of breaking at the first essence container). If an index table is found, it is parsed and stored in an MXFIndexTableSegment.
This seems OK, but it'll be very slow for large frame wrapped files. What you could do to speed it up is to make it partition aware. Unfortunately the partitions only contain FooterPartition and PreviousPartition offsets, which means one would have to parse the file partly in reverse (you can't rely on there being a Random Index Pack). I've been toying around with doing something like that. The logic is rather tricky though. I parse forward until I run into essence, then I seek to the last partition. After that I parse forward and seek to the previous partition if I run into essence or another partition. This repeats until I get back to where I was before I did the seek to the footer. Rebased patches attached. The first one simply adds some code that parses MXF partitions as it runs across them. The second one does the parsing I explained above. The commit message is due to the exact same problem your patch has - my code didn't handle 11 GiB frame wrapped files very well :o
2. In mxf_parse_structural_metadata, the MXFIndexTableSegments are parsed and an AVIndexEntry array is created from them Seeking is then done based on the AVIndexEntry array.
Sounds OK.
What I didn't really understand is how to link between MXF tracks and the slices in the index table. Sometimes, there is a track for every slice in the index table, sometimes there is not. So this part of the code is probably wrong, but help on this is very appreciated!
The MXF book attempts to explain slices, but I still haven't managed to figure out how they work. What I can say is that they're for handling mixing CBR and VBR media, which is a fairly typical use case (VBR video, CBR audio). I can't comment too much on the index parsing code since I don't quite understand how MXF's indexes work.
@@ -907,6 +1044,7 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) { MXFContext *mxf = s->priv_data; KLVPacket klv; + int64_t file_size = avio_size(s->pb);
if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) { av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n"); @@ -914,18 +1052,24 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) } avio_seek(s->pb, -14, SEEK_CUR); mxf->fc = s; - while (!url_feof(s->pb)) { + while (avio_tell(s->pb) < file_size) {
!s->pb->eof_reached?
const MXFMetadataReadTableEntry *metadata;
if (klv_read_packet(&klv, s->pb) < 0) return -1; PRINT_KEY(s, "read header", klv.key); av_dlog(s, "size %lld offset %#llx\n", klv.length, klv.offset); + if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) || - IS_KLV_KEY(klv.key, mxf_essence_element_key)) { - /* FIXME avoid seek */ - avio_seek(s->pb, klv.offset, SEEK_SET); - break; + IS_KLV_KEY(klv.key, mxf_essence_element_key) || + IS_KLV_KEY(klv.key, mxf_system_item_key)) { + if (!mxf->essence_offset) { + mxf->essence_offset = klv.offset; + if (IS_KLV_KEY(klv.key, mxf_system_item_key)) + mxf->system_item = 1; + } + avio_skip(s->pb, klv.length); + continue;
This is the part that'll be quite slow.
}
for (metadata = mxf_metadata_read_table; metadata->read; metadata++) { @@ -945,13 +1089,15 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) if (!metadata->read) avio_skip(s->pb, klv.length); } + /* FIXME avoid seek */ + avio_seek(s->pb, mxf->essence_offset, SEEK_SET);
You might want to account for not finding any essence. Anyway, hopefully the attached patches prove useful. The first one could probably be applied as-is. Baptiste? /Tomas
Hi Tomas, On 5/16/11 1:27 AM, Tomas Härdin wrote:
Georg Lippitsch skrev 2011-05-15 15:35:
Here is my first try to add image table based seeking to the MXF demuxer.
What the patch basically does is: 1. Parse through the whole file in mxf_read_header (instead of breaking at the first essence container). If an index table is found, it is parsed and stored in an MXFIndexTableSegment.
This seems OK, but it'll be very slow for large frame wrapped files.
What you could do to speed it up is to make it partition aware. Unfortunately the partitions only contain FooterPartition and PreviousPartition offsets, which means one would have to parse the file partly in reverse (you can't rely on there being a Random Index Pack).
I've been toying around with doing something like that. The logic is rather tricky though. I parse forward until I run into essence, then I seek to the last partition. After that I parse forward and seek to the previous partition if I run into essence or another partition. This repeats until I get back to where I was before I did the seek to the footer.
Rebased patches attached. The first one simply adds some code that parses MXF partitions as it runs across them. The second one does the parsing I explained above. The commit message is due to the exact same problem your patch has - my code didn't handle 11 GiB frame wrapped files very well :o
2. In mxf_parse_structural_metadata, the MXFIndexTableSegments are parsed and an AVIndexEntry array is created from them Seeking is then done based on the AVIndexEntry array.
Sounds OK.
What I didn't really understand is how to link between MXF tracks and the slices in the index table. Sometimes, there is a track for every slice in the index table, sometimes there is not. So this part of the code is probably wrong, but help on this is very appreciated!
The MXF book attempts to explain slices, but I still haven't managed to figure out how they work. What I can say is that they're for handling mixing CBR and VBR media, which is a fairly typical use case (VBR video, CBR audio).
I can't comment too much on the index parsing code since I don't quite understand how MXF's indexes work.
@@ -907,6 +1044,7 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) { MXFContext *mxf = s->priv_data; KLVPacket klv; + int64_t file_size = avio_size(s->pb);
if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) { av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n"); @@ -914,18 +1052,24 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) } avio_seek(s->pb, -14, SEEK_CUR); mxf->fc = s; - while (!url_feof(s->pb)) { + while (avio_tell(s->pb) < file_size) {
!s->pb->eof_reached?
const MXFMetadataReadTableEntry *metadata;
if (klv_read_packet(&klv, s->pb) < 0) return -1; PRINT_KEY(s, "read header", klv.key); av_dlog(s, "size %lld offset %#llx\n", klv.length, klv.offset); + if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) || - IS_KLV_KEY(klv.key, mxf_essence_element_key)) { - /* FIXME avoid seek */ - avio_seek(s->pb, klv.offset, SEEK_SET); - break; + IS_KLV_KEY(klv.key, mxf_essence_element_key) || + IS_KLV_KEY(klv.key, mxf_system_item_key)) { + if (!mxf->essence_offset) { + mxf->essence_offset = klv.offset; + if (IS_KLV_KEY(klv.key, mxf_system_item_key)) + mxf->system_item = 1; + } + avio_skip(s->pb, klv.length); + continue;
This is the part that'll be quite slow.
}
for (metadata = mxf_metadata_read_table; metadata->read; metadata++) { @@ -945,13 +1089,15 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) if (!metadata->read) avio_skip(s->pb, klv.length); } + /* FIXME avoid seek */ + avio_seek(s->pb, mxf->essence_offset, SEEK_SET);
You might want to account for not finding any essence.
Anyway, hopefully the attached patches prove useful. The first one could probably be applied as-is. Baptiste?
/Tomas
0001-Parse-MXF-partitions.patch
From d911eeb76e244f4b5d45c0a5e0a76a2bc5dc00c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Mon, 7 Feb 2011 16:07:41 +0100 Subject: [PATCH 1/2] Parse MXF partitions
--- libavformat/mxfdec.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 103 insertions(+), 1 deletions(-)
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 6b44b8f..61820f4 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -50,6 +50,27 @@ #include "avformat.h" #include "mxf.h"
+typedef enum { + Header, + BodyPartition, + Footer +} MXFPartitionType; + +typedef enum { + OPAtom, + OP1a, +} MXFOP; + +typedef struct { + int closed; + int complete; + MXFPartitionType type; + uint64_t previous_partition; + uint64_t footer_partition; + int index_sid; + int body_sid; +} MXFPartition; + typedef struct { UID uid; enum MXFMetadataSetType type; @@ -125,6 +146,9 @@ typedef struct { } MXFMetadataSet;
typedef struct { + MXFPartition **partitions; + unsigned partitions_count;
Any reason you don't use MXFPartition *partitions ? It avoids malloc for each partition.
[...]
+ + /* consider both footers to be closed (there is only Footer and CompleteFooter) */ + partition->closed = partition->type == Footer || !(uid[14] & 1);
Do we really need to workaround here ?
[...]
+ + /* only grab OP from the header */ + if (partition->type != Header) + return 0;
Why only header ? :)
[...]
@@ -843,6 +931,16 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = { { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack }, + { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
Do we need to parse non complete partitions ? Does it happen in the wild ? :/ Anyway, patch ok, nice work :) I'll review the other patch later. -- Baptiste COUDURIER Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA FFmpeg maintainer http://www.ffmpeg.org
Here is my second version of the MXF index table based seeking patch. The changes are: - Support clip-wrapped files. This is based on Maksym Veremeyenkos patch, but instead of parsing ULs for clip-wrapping, it checks if the current klv length is larger than the size of the current index entry. If this is the case, it only reads "size of current index entry"-bytes at once. - Support multiple index table segments Am 16.05.2011, 10:27 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
1. Parse through the whole file in mxf_read_header (instead of breaking at the first essence container). If an index table is found, it is parsed and stored in an MXFIndexTableSegment.
This seems OK, but it'll be very slow for large frame wrapped files.
Yes. I have not yet merged with your parsing code, but your patch certainly provides a decent solution for that.
!s->pb->eof_reached?
It seems that this is not set by avio_skip, so this will fail if avio_skip is called at the end instead of read. Regards, Georg
Hm, nobody interested in commenting on this patch? Anyway, here is another one that fixes the following issues: - Seeking in clip wrapped files only worked if a packet has been read before - Fix building index table in files > 2GB Georg
On Sat, Jun 04, 2011 at 01:39:40PM +0200, Georg Lippitsch wrote:
Hm, nobody interested in commenting on this patch?
ping, baptiste [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Why not whip the teacher when the pupil misbehaves? -- Diogenes of Sinope
Hi, On 06/04/2011 05:32 PM, Michael Niedermayer wrote:
On Sat, Jun 04, 2011 at 01:39:40PM +0200, Georg Lippitsch wrote:
Hm, nobody interested in commenting on this patch?
ping, baptiste
I'm definitely interested, but a little busy, maybe Thomas can review it in the mean time ? -- Baptiste COUDURIER Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA FFmpeg maintainer http://www.ffmpeg.org
Here is a patch that fixes a typo which caused an invalid write. BTW, has still nobody found time to have a look at my patch? I'm willing to work on this until it is in a committable state, but obviously can't do it without getting any feedback. Tomas? Baptiste? Thanks, Georg
Hi Georg, On 6/19/11 7:29 AM, Georg Lippitsch wrote:
Here is a patch that fixes a typo which caused an invalid write.
BTW, has still nobody found time to have a look at my patch? I'm willing to work on this until it is in a committable state, but obviously can't do it without getting any feedback. Tomas? Baptiste?
Sorry about that. I'll try to find some time to review this week :( -- Baptiste COUDURIER Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA FFmpeg maintainer http://www.ffmpeg.org
I took some time today to give these patches a bit more of a review. Partial review follows:
From 912ad3f89b75a6b99a9ffd700757cbd57508ab9d Mon Sep 17 00:00:00 2001 From: Georg Lippitsch <georg@linteum.at> Date: Sun, 15 May 2011 15:15:20 +0200 Subject: [PATCH] MXF index table based seeking
--- snip +static void mxf_read_delta_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment) +{ + int i; + int length; + segment->nb_delta_entries = avio_rb32(pb); + length = avio_rb32(pb); + segment->slice = + av_malloc(segment->nb_delta_entries * sizeof(*segment->slice)); + segment->element_delta = + av_malloc(segment->nb_index_entries * sizeof(*segment->element_delta)); + for (i = 0; i < segment->nb_delta_entries; i++) { + avio_r8(pb); + segment->slice[i] = avio_r8(pb); + segment->element_delta[i] = avio_rb32(pb); + } +} + +static void mxf_read_index_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment) +{ + int i, j; + int length; + segment->nb_index_entries = avio_rb32(pb); + length = avio_rb32(pb); + segment->flag_entries = + av_malloc(segment->nb_index_entries * sizeof(*segment->flag_entries)); + segment->stream_offset_entries = + av_malloc(segment->nb_index_entries * sizeof(*segment->stream_offset_entries)); + if (segment->slice_count) + segment->slice_offset_entries = + av_malloc(segment->nb_index_entries * sizeof(*segment->slice_offset_entries)); + for (i = 0; i < segment->nb_index_entries; i++) { + avio_rb16(pb); + segment->flag_entries[i] = avio_r8(pb); + segment->stream_offset_entries[i] = avio_rb64(pb); + if (segment->slice_count) { + segment->slice_offset_entries[i] = + av_malloc(segment->slice_count * sizeof(**segment->slice_offset_entries)); + for (j = 0; j < segment->slice_count; j++) { + segment->slice_offset_entries[i][j] = avio_rb32(pb); + } + avio_skip(pb, length - 11 - 4 * segment->slice_count); + } else + avio_skip(pb, length - 11); + } +}
You might want to make sure any of the allocations in here failing doesn't cause a crash.
@@ -837,6 +934,46 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
Will review this later.
@@ -907,6 +1044,7 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) { MXFContext *mxf = s->priv_data; KLVPacket klv; + int64_t file_size = avio_size(s->pb);
if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) { av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n"); @@ -914,18 +1052,24 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) } avio_seek(s->pb, -14, SEEK_CUR); mxf->fc = s; - while (!url_feof(s->pb)) { + while (avio_tell(s->pb) < file_size) { const MXFMetadataReadTableEntry *metadata;
if (klv_read_packet(&klv, s->pb) < 0) return -1;
Use break instead of returning and drop file_size/avio_tell in favor of keeping the url_feof().
PRINT_KEY(s, "read header", klv.key); av_dlog(s, "size %lld offset %#llx\n", klv.length, klv.offset); +
Stray newline
if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) || - IS_KLV_KEY(klv.key, mxf_essence_element_key)) { - /* FIXME avoid seek */ - avio_seek(s->pb, klv.offset, SEEK_SET); - break; + IS_KLV_KEY(klv.key, mxf_essence_element_key) || + IS_KLV_KEY(klv.key, mxf_system_item_key)) { + if (!mxf->essence_offset) { + mxf->essence_offset = klv.offset; + if (IS_KLV_KEY(klv.key, mxf_system_item_key)) + mxf->system_item = 1; + } + avio_skip(s->pb, klv.length); + continue; }
for (metadata = mxf_metadata_read_table; metadata->read; metadata++) { @@ -945,13 +1089,15 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) if (!metadata->read) avio_skip(s->pb, klv.length); } + /* FIXME avoid seek */
There need to be an if (!mxf->essence_offset) return here if you go with my "keep using url_feof()" suggestion. I see there's one in the next patch - move it here instead.
+ avio_seek(s->pb, mxf->essence_offset, SEEK_SET); return mxf_parse_structural_metadata(mxf); }
static int mxf_read_close(AVFormatContext *s) { MXFContext *mxf = s->priv_data; - int i; + int i, j;
av_freep(&mxf->packages_refs);
@@ -970,6 +1116,15 @@ static int mxf_read_close(AVFormatContext *s) case MaterialPackage: av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs); break; + case IndexTableSegment: + for (j = 0; j < ((MXFIndexTableSegment *)mxf->metadata_sets[i])->nb_index_entries; j++) + av_freep(&((MXFIndexTableSegment *)mxf->metadata_sets[i])->slice_offset_entries[j]); + av_freep(&((MXFIndexTableSegment *)mxf->metadata_sets[i])->slice); + av_freep(&((MXFIndexTableSegment *)mxf->metadata_sets[i])->element_delta); + av_freep(&((MXFIndexTableSegment *)mxf->metadata_sets[i])->flag_entries); + av_freep(&((MXFIndexTableSegment *)mxf->metadata_sets[i])->stream_offset_entries); + av_freep(&((MXFIndexTableSegment *)mxf->metadata_sets[i])->slice_offset_entries);
Consider simplifying using a MXFIndexTableSegment pointer. That would make this part more readable.
+ break; default: break; } @@ -997,20 +1152,30 @@ static int mxf_probe(AVProbeData *p) { return 0; }
-/* rudimentary byte seek */ -/* XXX: use MXF Index */ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) { AVStream *st = s->streams[stream_index]; - int64_t seconds;
- if (!s->bit_rate) - return -1; - if (sample_time < 0) - sample_time = 0; - seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den); - avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET); - av_update_cur_dts(s, st, sample_time); + if (st->nb_index_entries) { + int index = av_index_search_timestamp(st, sample_time, flags); + av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, sample_time, index); + if (index < 0 && sample_time < st->index_entries[0].timestamp) + index = 0; + if (index < 0) + return -1; + avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET); + av_update_cur_dts(s, st, st->index_entries[index].timestamp); + } else { + int64_t seconds; + if (!s->bit_rate) + return -1; + if (sample_time < 0) + sample_time = 0; + seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den); + avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET); + av_update_cur_dts(s, st, sample_time);
Please avoid mixing functional and cosmetic changes. I can see that this doesn't change the behavior of the else-case though.
From 0661022b30eb8c447a5a3da0b38937253ebb2b45 Mon Sep 17 00:00:00 2001 From: Georg Lippitsch <georg@linteum.at> Date: Mon, 16 May 2011 20:37:28 +0200 Subject: [PATCH 2/2] Index table based handling of clip-wrapped mxf files
--- enum MXFWrappingScheme { @@ -322,8 +327,24 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) @@ -761,6 +792,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
Will review later as well..
@@ -1068,6 +1149,10 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) if (IS_KLV_KEY(klv.key, mxf_system_item_key)) mxf->system_item = 1; } + if (!mxf->first_essence_kl_length && IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
else if maybe?
+ mxf->first_essence_kl_length = avio_tell(s->pb) - klv.offset; + mxf->first_essence_length = klv.length; + } avio_skip(s->pb, klv.length); continue; } @@ -1090,6 +1175,8 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) avio_skip(s->pb, klv.length); } /* FIXME avoid seek */ + if (!mxf->essence_offset) + return -1;
Move this to the first patch as I mentioned earlier. Also perhaps consider a better return value like AVERROR_INVALIDDATA.
avio_seek(s->pb, mxf->essence_offset, SEEK_SET); return mxf_parse_structural_metadata(mxf); } @@ -1155,6 +1242,9 @@ static int mxf_probe(AVProbeData *p) { static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) { AVStream *st = s->streams[stream_index]; + MXFContext* mxf = s->priv_data; + MXFTrack *tr = st->priv_data; + int64_t seekpos;
if (st->nb_index_entries) { int index = av_index_search_timestamp(st, sample_time, flags); @@ -1163,8 +1253,9 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti index = 0; if (index < 0) return -1; - avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET); + seekpos = st->index_entries[index].pos; av_update_cur_dts(s, st, st->index_entries[index].timestamp); + tr->current_sample_index = index; } else { int64_t seconds; if (!s->bit_rate) @@ -1172,10 +1263,23 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti if (sample_time < 0) sample_time = 0; seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den); - avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET); + seekpos = (s->bit_rate * seconds);
3 lost.
av_update_cur_dts(s, st, sample_time); }
+ if (mxf->current_klv_data.length) { + if (mxf->current_klv_index == stream_index) { + int64_t seekoffset = seekpos - avio_tell(s->pb); + mxf->current_klv_data.offset += seekoffset; + if (seekoffset < (int64_t)mxf->current_klv_data.length) + mxf->current_klv_data.length -= seekoffset; + else + mxf->current_klv_data.length = 0; + } + } + + avio_seek(s->pb, seekpos, SEEK_SET); + return 0; }
I need to find some P2 AVC-Intra 50 samples to test this stuff properly. Hopefully the above should keep you a little busy. If you like I can attach an updated set of patches that have been rebased, have some of the things I mentioned above fixed and have your recent fix patches squashed in. /Tomas
Attached is a patch that takes your comments into account. Sorry that it took so long, hopefully I'll have some more time to work on this within the next weeks. Everything is now in a single patch.. The P2 files I've tested (DVCPro and AVC-Intra) seem to work quite ok now. XDCAM HD files also work, but as Tomas mentioned they are horrible slow at building the index table. Georg
Georg Lippitsch <georg.lippitsch <at> gmx.at> writes:
- av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); + av_dlog(s, "size %lld offset %#llx\n", klv.length, klv.offset);
This looks unintended, or am I missing something?
- return 0; + tr->current_sample_index++;
+ return 0;
Trailing whitespace cannot be committed to the FFmpeg repository, please consider using tools/patcheck. Possibly no need to resend before a real review, Carl Eugen
Am 07.08.2011, 19:49 Uhr, schrieb Carl Eugen Hoyos <cehoyos@ag.or.at>:
- av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); + av_dlog(s, "size %lld offset %#llx\n", klv.length, klv.offset);
This looks unintended, or am I missing something?
Indeed.
Trailing whitespace cannot be committed to the FFmpeg repository, please consider using tools/patcheck.
Sorry, but anyway this patch is far from being committable. I will use patcheck next time. Georg
Georg Lippitsch skrev 2011-08-07 21:32:
Am 07.08.2011, 19:49 Uhr, schrieb Carl Eugen Hoyos <cehoyos@ag.or.at>:
- av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); + av_dlog(s, "size %lld offset %#llx\n", klv.length, klv.offset);
This looks unintended, or am I missing something?
Indeed.
Trailing whitespace cannot be committed to the FFmpeg repository, please consider using tools/patcheck.
Sorry, but anyway this patch is far from being committable. I will use patcheck next time.
Any news on this? I'm not sure if 10-bit 4:2:2 H.264 decoding is in FFmpeg yet, but there's a patch for it on libav-devel. With it you should be able to decode the AVC-Intra 100 samples I have at http://titan.codemill.se/~tomhar/samples/ and /multiindex. /Tomas
Tomas Härdin <tomas.hardin <at> codemill.se> writes:
I'm not sure if 10-bit 4:2:2 H.264 decoding is in FFmpeg yet,
It is. It is also tested by fate.
but there's a patch for it on libav-devel.
It was posted there because it's in FFmpeg;-) I don't think the patch should be on hold because it is slow. (Or does it make currently working decoding slower?) Carl Eugen
Am 02.09.2011, 09:56 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
Any news on this?
Sorry, not really for now. I've done a lot of testing with P2 DVCPro files, they seem to work stable. What's most important for me now is merging with your parsing code, so that XDCAM works at reasonable speed. But unfortunately I'm quite busy now, so probably I can do that in around three weeks at earliest ... Georg
On Fri, 2011-09-02 at 15:50 +0200, Georg Lippitsch wrote:
Am 02.09.2011, 09:56 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
Any news on this?
Sorry, not really for now. I've done a lot of testing with P2 DVCPro files, they seem to work stable. What's most important for me now is merging with your parsing code, so that XDCAM works at reasonable speed. But unfortunately I'm quite busy now, so probably I can do that in around three weeks at earliest ...
Georg
I took the liberty of refactoring and fixing a few bugs in your patch, in addition to rebasing my partition parsing code on top of it and adding some patches to support the V210 sample at http://titan.codemill.se/~tomhar/samples/multiindex/ The attached patch, 0001-mxfdec-Index-table-based-seeking.patch, is the *.patch-part* files squashed into one. I included them to make it clear what I changed, and to not clutter up the history making bisecting more difficult (assuming the squashed patch is preferred). Unfortunately fate-seek-lavf_mxf doesn't pass, neither with your original patch nor the updated one. However, considering the old code didn't do proper seeking I suspect the test isn't actually correct. Finally, a few questions about the original patch: What's the deal with n_delta and i in the original patch? Shouldn't at least track_id be used instead of i? The "if (i < mxf->system_item + st->index) n_delta++;" logic also looks strange - is its purpose to offset past the system item if present? /Tomas
This is the magic that makes XDCAM demuxing not take ages. I have a 2 GiB test file demonstrating this if anyone's interested. /Tomas
On Tue, 2011-09-27 at 11:13 +0200, Tomas Härdin wrote:
This is the magic that makes XDCAM demuxing not take ages. I have a 2 GiB test file demonstrating this if anyone's interested.
It turns out this patch doesn't properly handle files where FooterPartition isn't properly set in all partitions. mxfenc's output is an example of this - FooterPartition is only properly set in the header. I opted to deal with this in the demuxer since mxfenc is probably not the only writer to do this. Anyway, the attached patch takes any non-zero FooterPartition value and sticks it in MXFContext for later use. /Tomas
.. and here's the updated version of the initial patch. The intent here is that "[PATCH] mxfdec: Move FooterPartition to MXFContext and make sure it's never set to zero" can be applied on its own. /Tomas
On Mon, Oct 10, 2011 at 04:28:02PM +0200, Tomas Härdin wrote:
On Tue, 2011-09-27 at 11:13 +0200, Tomas Härdin wrote:
This is the magic that makes XDCAM demuxing not take ages. I have a 2 GiB test file demonstrating this if anyone's interested.
It turns out this patch doesn't properly handle files where FooterPartition isn't properly set in all partitions. mxfenc's output is an example of this - FooterPartition is only properly set in the header. I opted to deal with this in the demuxer since mxfenc is probably not the only writer to do this.
Anyway, the attached patch takes any non-zero FooterPartition value and sticks it in MXFContext for later use.
/Tomas
mxfdec.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 355b7dfbae6ede2e3e8f5f25f025608080c46ff9 0001-mxfdec:-Move-FooterPartition-to-MXFContext-and-make-sure-it_s-never-set-to-zero.patch From 889032bf543d02a9fafbc8bcc38e0bab1d45775c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Fri, 7 Oct 2011 15:48:00 +0200 Subject: [PATCH] mxfdec: Move FooterPartition to MXFContext and make sure it's never set to zero
applied [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There seems to be only one solution to NIH syndrom, ... a shooting squad
Hi Thomas, On 9/27/11 2:13 AM, Tomas Härdin wrote:
0003-mxf-Add-two-codec-ULs-for-V210.patch
From 1dabdaea6a1f41d2e210dd6e2ef6bd48c1d96bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Mon, 26 Sep 2011 11:24:11 +0200 Subject: [PATCH 3/4] mxf: Add two codec ULs for V210
--- libavformat/mxf.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/libavformat/mxf.c b/libavformat/mxf.c index 1b85a89..ad384f2 100644 --- a/libavformat/mxf.c +++ b/libavformat/mxf.c @@ -43,6 +43,8 @@ const MXFCodecUL ff_mxf_codec_uls[] = { { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_RAWVIDEO }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x02,0x00,0x00 }, 14, CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x02 }, 16, CODEC_ID_V210 }, /* V210 */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x01 }, 16, CODEC_ID_V210 }, /* V210 */
It seems that only 15 bytes are relevant, and version should be ignored, I think there is a function that does this already. -- Baptiste COUDURIER Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA FFmpeg maintainer http://www.ffmpeg.org
On Tue, 2011-09-27 at 02:23 -0700, Baptiste Coudurier wrote:
Hi Thomas,
On 9/27/11 2:13 AM, Tomas Härdin wrote:
0003-mxf-Add-two-codec-ULs-for-V210.patch
From 1dabdaea6a1f41d2e210dd6e2ef6bd48c1d96bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Mon, 26 Sep 2011 11:24:11 +0200 Subject: [PATCH 3/4] mxf: Add two codec ULs for V210
--- libavformat/mxf.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/libavformat/mxf.c b/libavformat/mxf.c index 1b85a89..ad384f2 100644 --- a/libavformat/mxf.c +++ b/libavformat/mxf.c @@ -43,6 +43,8 @@ const MXFCodecUL ff_mxf_codec_uls[] = { { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_RAWVIDEO }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x02,0x00,0x00 }, 14, CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x02 }, 16, CODEC_ID_V210 }, /* V210 */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x01 }, 16, CODEC_ID_V210 }, /* V210 */
It seems that only 15 bytes are relevant, and version should be ignored, I think there is a function that does this already.
Ah, you're right - mxf_match_uid() ignores the version byte. Updated patch attached. /Tomas
On 9/27/11 2:40 AM, Tomas Härdin wrote:
On Tue, 2011-09-27 at 02:23 -0700, Baptiste Coudurier wrote:
Hi Thomas,
On 9/27/11 2:13 AM, Tomas Härdin wrote:
0003-mxf-Add-two-codec-ULs-for-V210.patch
From 1dabdaea6a1f41d2e210dd6e2ef6bd48c1d96bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Mon, 26 Sep 2011 11:24:11 +0200 Subject: [PATCH 3/4] mxf: Add two codec ULs for V210
--- libavformat/mxf.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/libavformat/mxf.c b/libavformat/mxf.c index 1b85a89..ad384f2 100644 --- a/libavformat/mxf.c +++ b/libavformat/mxf.c @@ -43,6 +43,8 @@ const MXFCodecUL ff_mxf_codec_uls[] = { { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_RAWVIDEO }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x02,0x00,0x00 }, 14, CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x02 }, 16, CODEC_ID_V210 }, /* V210 */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x01 }, 16, CODEC_ID_V210 }, /* V210 */
It seems that only 15 bytes are relevant, and version should be ignored, I think there is a function that does this already.
Ah, you're right - mxf_match_uid() ignores the version byte. Updated patch attached.
/Tomas
0003-mxf-Add-PictureEssenceCoding-UL-for-V210.patch
From 2c0d4d0f44f5e8cf42d0f5faaff465e0b0cdfce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Mon, 26 Sep 2011 11:24:11 +0200 Subject: [PATCH 3/4] mxf: Add PictureEssenceCoding UL for V210
--- libavformat/mxf.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/libavformat/mxf.c b/libavformat/mxf.c index 1b85a89..b39a9b6 100644 --- a/libavformat/mxf.c +++ b/libavformat/mxf.c @@ -43,6 +43,7 @@ const MXFCodecUL ff_mxf_codec_uls[] = { { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_RAWVIDEO }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x02,0x00,0x00 }, 14, CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, 15, CODEC_ID_V210 }, /* V210 */ /* SoundEssenceCompression */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 13, CODEC_ID_PCM_S16LE }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_PCM_S16LE },
Looks good to me. -- Baptiste COUDURIER Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA FFmpeg maintainer http://www.ffmpeg.org
On Tue, Sep 27, 2011 at 01:07:22PM -0700, Baptiste Coudurier wrote:
On 9/27/11 2:40 AM, Tomas Härdin wrote:
On Tue, 2011-09-27 at 02:23 -0700, Baptiste Coudurier wrote:
Hi Thomas,
On 9/27/11 2:13 AM, Tomas Härdin wrote:
0003-mxf-Add-two-codec-ULs-for-V210.patch
From 1dabdaea6a1f41d2e210dd6e2ef6bd48c1d96bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Mon, 26 Sep 2011 11:24:11 +0200 Subject: [PATCH 3/4] mxf: Add two codec ULs for V210
--- libavformat/mxf.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/libavformat/mxf.c b/libavformat/mxf.c index 1b85a89..ad384f2 100644 --- a/libavformat/mxf.c +++ b/libavformat/mxf.c @@ -43,6 +43,8 @@ const MXFCodecUL ff_mxf_codec_uls[] = { { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_RAWVIDEO }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x02,0x00,0x00 }, 14, CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x02 }, 16, CODEC_ID_V210 }, /* V210 */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x01 }, 16, CODEC_ID_V210 }, /* V210 */
It seems that only 15 bytes are relevant, and version should be ignored, I think there is a function that does this already.
Ah, you're right - mxf_match_uid() ignores the version byte. Updated patch attached.
/Tomas
0003-mxf-Add-PictureEssenceCoding-UL-for-V210.patch
From 2c0d4d0f44f5e8cf42d0f5faaff465e0b0cdfce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Mon, 26 Sep 2011 11:24:11 +0200 Subject: [PATCH 3/4] mxf: Add PictureEssenceCoding UL for V210
--- libavformat/mxf.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/libavformat/mxf.c b/libavformat/mxf.c index 1b85a89..b39a9b6 100644 --- a/libavformat/mxf.c +++ b/libavformat/mxf.c @@ -43,6 +43,7 @@ const MXFCodecUL ff_mxf_codec_uls[] = { { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_RAWVIDEO }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x02,0x00,0x00 }, 14, CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra */ + { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, 15, CODEC_ID_V210 }, /* V210 */ /* SoundEssenceCompression */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 13, CODEC_ID_PCM_S16LE }, /* Uncompressed */ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_PCM_S16LE },
Looks good to me.
applied [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Breaking DRM is a little like attempting to break through a door even though the window is wide open and the only thing in the house is a bunch of things you dont want and which you would get tomorrow for free anyway
It's not pretty, but it works. I have no idea how one is supposed to handle shifted PCM in MXF - there's no pretty field like for PixelLayout. /Tomas
Hi Thomas, On 9/27/11 2:14 AM, Tomas Härdin wrote:
It's not pretty, but it works. I have no idea how one is supposed to handle shifted PCM in MXF - there's no pretty field like for PixelLayout.
/Tomas
0004-mxfdec-Consider-QuantizationBits-between-17-and-24-t.patch
From aedba3f931864420853a3bc13071e9633eefffdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Wed, 25 May 2011 17:20:08 +0200 Subject: [PATCH 4/4] mxfdec: Consider QuantizationBits between 17 and 24 to be pcm_s24*
This fixes playback of BRD38772509.mxf --- libavformat/mxfdec.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index a534188..e0c6a12 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -1191,12 +1191,12 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den; /* TODO: implement CODEC_ID_RAWAUDIO */ if (st->codec->codec_id == CODEC_ID_PCM_S16LE) { - if (descriptor->bits_per_sample == 24) + if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24) st->codec->codec_id = CODEC_ID_PCM_S24LE; else if (descriptor->bits_per_sample == 32) st->codec->codec_id = CODEC_ID_PCM_S32LE; } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) { - if (descriptor->bits_per_sample == 24) + if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24) st->codec->codec_id = CODEC_ID_PCM_S24BE; else if (descriptor->bits_per_sample == 32) st->codec->codec_id = CODEC_ID_PCM_S32BE;
Looks ok to me. -- Baptiste COUDURIER Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA FFmpeg maintainer http://www.ffmpeg.org
On Tue, Sep 27, 2011 at 02:33:55AM -0700, Baptiste Coudurier wrote:
Hi Thomas,
On 9/27/11 2:14 AM, Tomas Härdin wrote:
It's not pretty, but it works. I have no idea how one is supposed to handle shifted PCM in MXF - there's no pretty field like for PixelLayout.
/Tomas
0004-mxfdec-Consider-QuantizationBits-between-17-and-24-t.patch
From aedba3f931864420853a3bc13071e9633eefffdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tomas.hardin@codemill.se> Date: Wed, 25 May 2011 17:20:08 +0200 Subject: [PATCH 4/4] mxfdec: Consider QuantizationBits between 17 and 24 to be pcm_s24*
This fixes playback of BRD38772509.mxf --- libavformat/mxfdec.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index a534188..e0c6a12 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -1191,12 +1191,12 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den; /* TODO: implement CODEC_ID_RAWAUDIO */ if (st->codec->codec_id == CODEC_ID_PCM_S16LE) { - if (descriptor->bits_per_sample == 24) + if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24) st->codec->codec_id = CODEC_ID_PCM_S24LE; else if (descriptor->bits_per_sample == 32) st->codec->codec_id = CODEC_ID_PCM_S32LE; } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) { - if (descriptor->bits_per_sample == 24) + if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24) st->codec->codec_id = CODEC_ID_PCM_S24BE; else if (descriptor->bits_per_sample == 32) st->codec->codec_id = CODEC_ID_PCM_S32BE;
Looks ok to me.
applied, thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB If a bugfix only changes things apparently unrelated to the bug with no further explanation, that is a good sign that the bugfix is wrong.
Am 27.09.2011, 11:08 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
I took the liberty of refactoring and fixing a few bugs in your patch, in addition to rebasing my partition parsing code on top of it and adding some patches to support the V210 sample at http://titan.codemill.se/~tomhar/samples/multiindex/
Great news, thank you. I have a bit more time to work on this now, so hopefully I can provide some useful comments within the next days.
Finally, a few questions about the original patch: What's the deal with n_delta and i in the original patch? Shouldn't at least track_id be used instead of i?
Actually I never found out how to correctly link between tracks and n_delta, so this is a hack for doing this. SMPTE377 says "There shall be one Delta Entry for each and every indexed Element within the Essence Container.", but IIUC this does not imply that every track is actually indexed. If a track is not indexed at all, seeking will fail, but I've not figured out how to handle this correctly. Or did I understand this wrong? Probably, simply using track_id is the right way to do it.
The "if (i < mxf->system_item + st->index) n_delta++;" logic also looks strange - is its purpose to offset past the system item if present?
Yes. But as above, I never really understood this track/n_delta logic, so this is probably wrong as well. Georg
Am 27.09.2011, 11:08 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
I took the liberty of refactoring and fixing a few bugs in your patch, in addition to rebasing my partition parsing code on top of it and adding some patches to support the V210 sample at http://titan.codemill.se/~tomhar/samples/multiindex/
Here is a patch that makes the XDCAM HD files from your sample-directory seekable. These files were indeed weird. They had every index entry doubled, and therefore the nb_index_entries value was twice the index_duration. Even mxfdump only parsed the indices for half of the file. I therefore added some code to remove double entries. Also, the sizes of the index entries were not calculated if nb_delta_entries was 0, which I've added now. Regards, Georg
On Wed, 2011-09-28 at 21:59 +0200, Georg Lippitsch wrote:
Am 27.09.2011, 11:08 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
I took the liberty of refactoring and fixing a few bugs in your patch, in addition to rebasing my partition parsing code on top of it and adding some patches to support the V210 sample at http://titan.codemill.se/~tomhar/samples/multiindex/
Here is a patch that makes the XDCAM HD files from your sample-directory seekable.
These files were indeed weird. They had every index entry doubled, and therefore the nb_index_entries value was twice the index_duration. Even mxfdump only parsed the indices for half of the file.
That's Avid for you.. IIRC there's actually 2*n+1 entries - the last entry contains the length of the essence.
I therefore added some code to remove double entries. Also, the sizes of the index entries were not calculated if nb_delta_entries was 0, which I've added now.
Yeah, I think they like the 64k limit of the local set coding. Neither do I TBH.
... + current = 0; for (i = 0; i < segment->nb_index_entries; i++) { + int flag_entry; + uint64_t stream_offset_entry; avio_rb16(pb); /* TemporalOffset and KeyFrameOffset */ - segment->flag_entries[i] = avio_r8(pb); - segment->stream_offset_entries[i] = avio_rb64(pb); - if (segment->slice_count) { - AV_MALLOC_ARR_OR_ENOMEM(segment->slice_offset_entries[i], segment->slice_count); - - for (j = 0; j < segment->slice_count; j++) { - segment->slice_offset_entries[i][j] = avio_rb32(pb); + flag_entry = avio_r8(pb); + stream_offset_entry = avio_rb64(pb); + if (current == 0 || segment->stream_offset_entries[current-1] != stream_offset_entry)
Might want a comment above this "if" explaining why this is needed. Something like /* handle Avid index tables with duplicate entries */
+ { + segment->flag_entries[current] = flag_entry; + segment->stream_offset_entries[current] = stream_offset_entry; + if (segment->slice_count) { + AV_MALLOC_ARR_OR_ENOMEM(segment->slice_offset_entries[current], + segment->slice_count); + + for (j = 0; j < segment->slice_count; j++) { + segment->slice_offset_entries[current][j] = avio_rb32(pb); + } + } + current++; + } else { + if (segment->slice_count) { + for (j = 0; j < segment->slice_count; j++) + avio_rb32(pb);
avio_skip(pb, 4*segment->slice_count);
} }
avio_skip(pb, length - 11 - 4 * segment->slice_count); } + segment->nb_unique_index_entries = current;
If my earlier memory was right, then we might want to do something like: /* account for Avid's index tables having an extra entry for the size */ segment->nb_unique_index_entries = current == segment->nb_index_entries ? current : current - 1;
return 0; }
@@ -982,7 +999,7 @@ static int mxf_parse_index(MXFContext *mxf, int i, AVStream *st) st->index_entries = av_realloc(st->index_entries, st->index_entries_allocated_size); for (k = 0; k < duration; k++) { AVIndexEntry *e = &st->index_entries[st->nb_index_entries]; - if (k < tableseg->nb_index_entries) { + if (k < tableseg->nb_unique_index_entries) { e->pos = tableseg->stream_offset_entries[k]; if (n_delta < tableseg->nb_delta_entries) { if (n_delta < tableseg->nb_delta_entries - 1) { @@ -1002,6 +1019,9 @@ static int mxf_parse_index(MXFContext *mxf, int i, AVStream *st) if (tableseg->slice[n_delta] > 0) e->pos += tableseg->slice_offset_entries[k][tableseg->slice[n_delta]-1]; e->pos += tableseg->element_delta[n_delta]; + } else if (k < duration - 1) { + e->size = tableseg->stream_offset_entries[k+1] - + tableseg->stream_offset_entries[k]; } e->flags = !(tableseg->flag_entries[k] & 0x30) ? AVINDEX_KEYFRAME : 0; } else { @@ -1481,8 +1501,9 @@ static int mxf_read_close(AVFormatContext *s) break; case IndexTableSegment: seg = (MXFIndexTableSegment *)mxf->metadata_sets[i]; - for (j = 0; j < seg->nb_index_entries; j++) - av_freep(&seg->slice_offset_entries[j]); + if (seg->slice_count) + for (j = 0; j < seg->nb_index_entries; j++) + av_freep(&seg->slice_offset_entries[j]);
Unrelated? Also, aren't all of these NULL if !slice_count and thus av_freep() is OK? /Tomas
Am 29.09.2011, 10:04 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
+ if (current == 0 || segment->stream_offset_entries[current-1] != stream_offset_entry)
Might want a comment above this "if" explaining why this is needed. Something like /* handle Avid index tables with duplicate entries */
Indeed. As you have seen I was quite lazy with comments also in the earlier patches, but when reviewing them now some weeks later I find that annoying. I really should care more about comments and documentation ...
/* account for Avid's index tables having an extra entry for the size */ segment->nb_unique_index_entries = current == segment->nb_index_entries ? current : current - 1;
Actually the n+1 doesn't hurt because in the later parsing, we only use index_duration (not nb_index_entries) for the loop. But if a program would not add the +1, one entry is missing, so I'd prefer setting it to current directly.
seg = (MXFIndexTableSegment *)mxf->metadata_sets[i]; - for (j = 0; j < seg->nb_index_entries; j++) - av_freep(&seg->slice_offset_entries[j]); + if (seg->slice_count) + for (j = 0; j < seg->nb_index_entries; j++) + av_freep(&seg->slice_offset_entries[j]);
Unrelated? Also, aren't all of these NULL if !slice_count and thus av_freep() is OK?
No. This caused a segfault. seg->slice_offset_entries itself is not allocated if seg->slice_count is 0, and therefore cannot be accessed with seg->slice_offset_entries[j] Updated patch attached. Georg
Here is another small patch that checks for current_sample_index to be smaller than nb_index_entries in mxf_read_packet. I remember that I had a file where this was not automatically the case and therefore caused an invalid read. Georg
I've split this patch in two parts per Baptiste's request. The first part reads and converts the IndexTableSegments into AVIndexEntry arrays while the second part implements the clip wrapping. First part attached. I has a few fixes, including: * removed useless index_entries_allocated_size from MXFIndexTableSegment * changed allocations to use the recently added av_calloc() * making sure av_realloc():ing st->index_entries works OK * handling audio duration % 8192 != 0 * advancing accumulated_offset by segment_size, meaning we potentially support more than two segments with different EditUnitByteCount (no samples for that yet) On IRC me and Baptiste realized that the index segment sorting needs to be kept since my partition parsing patch reads the file in reverse order, leading to unsorted segments. Just noting it so noone forgets. This patch passes "make fate" /Tomas
Second part. Should be unmodified compared to original patch. Does not pass fate, as previously discussed. /Tomas
Am 06.10.2011, 17:20 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
I've split this patch in two parts per Baptiste's request. The first part reads and converts the IndexTableSegments into AVIndexEntry arrays while the second part implements the clip wrapping.
From my perspective, your patches look fine!
* handling audio duration % 8192 != 0
Actually I didn't like this kind of audio handling very much, so I wrote another patch for this. After all, it avoids parsing the index table and furthermore generating st->index_entries if there is only one table-segment, and if this segments has no entries but only EditUnitByteCount set. It is meant to handle the audio-files with small EditUnitByteCount correctly, and certainly also saves much memory in this case. Besides that, I have no further comments on the patches for now. Regards, Georg
On Sat, 2011-10-08 at 22:33 +0200, Georg Lippitsch wrote:
Am 06.10.2011, 17:20 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
I've split this patch in two parts per Baptiste's request. The first part reads and converts the IndexTableSegments into AVIndexEntry arrays while the second part implements the clip wrapping.
From my perspective, your patches look fine!
* handling audio duration % 8192 != 0
Actually I didn't like this kind of audio handling very much, so I wrote another patch for this. After all, it avoids parsing the index table and furthermore generating st->index_entries if there is only one table-segment, and if this segments has no entries but only EditUnitByteCount set. It is meant to handle the audio-files with small EditUnitByteCount correctly, and certainly also saves much memory in this case.
Sounds reasonable I suppose. While I'm not the biggest fan of having different code paths here, the proposed way is at least easy to understand.
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 1069167..06dda3f 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -187,6 +187,7 @@ typedef struct { int64_t first_essence_length; KLVPacket current_klv_data; int current_klv_index; + int single_eubc; } MXFContext;
enum MXFWrappingScheme { @@ -368,8 +369,11 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) klv = mxf->current_klv_data; index = mxf->current_klv_index; tr = s->streams[index]->priv_data; - klv.length = FFMIN(s->streams[index]->index_entries[tr->current_sample_index].size, - mxf->current_klv_data.length); + if (mxf->single_eubc) + klv.length = FFMAX(mxf->single_eubc, 8192);
You should probably make sure klv.length % samples_per_byte == 0. 24-bit could have a problem with this. For instance, PAL mono 24-bit 48 kHz -> 5760 B, meaning it'd pick 8192 which isn't divisible by 3. Shouldn't this also do FFMIN() with current_klv_data.length just like below?
+ else + klv.length = FFMIN(s->streams[index]->index_entries[tr->current_sample_index].size, + mxf->current_klv_data.length); mxf->current_klv_data.offset += klv.length; mxf->current_klv_data.length -= klv.length; av_dlog(s, "Clip-wrapped reading. klv.length=%"PRId64" current_sample_index=%d\n", @@ -398,9 +402,10 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) } if (s->streams[index]->discard == AVDISCARD_ALL) goto skip; - if (tr->current_sample_index < s->streams[index]->nb_index_entries && + if ((tr->current_sample_index < s->streams[index]->nb_index_entries && klv.length > s->streams[index]->index_entries[tr->current_sample_index].size && - s->streams[index]->index_entries[tr->current_sample_index].size) { + s->streams[index]->index_entries[tr->current_sample_index].size) || + mxf->single_eubc) { mxf->current_klv_data = klv; mxf->current_klv_index = index; return mxf_read_packet(s, pkt); @@ -952,6 +957,14 @@ static int mxf_parse_index(MXFContext *mxf, int i, AVStream *st) if ((ret = mxf_get_sorted_table_segments(mxf, &nb_sorted_segments, &sorted_segments))) return ret;
+ if (nb_sorted_segments == 1 && !sorted_segments[0]->index_duration && + sorted_segments[0]->edit_unit_byte_count > 0) + { + mxf->single_eubc = sorted_segments[0]->edit_unit_byte_count; + av_free(sorted_segments); + return 0; + } + for (j = 0; j < nb_sorted_segments; j++) { int n_delta = i; int duration, sample_duration = 1, last_sample_size = 0; @@ -1447,13 +1460,23 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti index = 0; if (index < 0) return -1; - seekpos = st->index_entries[index].pos; + seekpos = st->index_entries[index].pos;
Not intended?
av_update_cur_dts(s, st, st->index_entries[index].timestamp); tr->current_sample_index = index; if (!index) { mxf->current_klv_data.length = 0; mxf->current_klv_index = 0; } + } else if (mxf->single_eubc) { + seekpos = mxf->single_eubc * sample_time + mxf->essence_offset; + if (sample_time > 0 && seekpos) + seekpos += mxf->first_essence_kl_length; + av_update_cur_dts(s, st, sample_time); + tr->current_sample_index = sample_time; + if (!sample_time) { + mxf->current_klv_data.length = 0; + mxf->current_klv_index = 0; + }
Looks reasonable. /Tomas
Am 10.10.2011, 13:03 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
Actually I didn't like this kind of audio handling very much, so I wrote another patch for this. After all, it avoids parsing the index table and furthermore generating st->index_entries if there is only one table-segment, and if this segments has no entries but only EditUnitByteCount set. It is meant to handle the audio-files with small EditUnitByteCount correctly, and certainly also saves much memory in this case.
Sounds reasonable I suppose. While I'm not the biggest fan of having different code paths here, the proposed way is at least easy to understand.
As discussed on IRC, what I want to avoid is seeking in chunks of 8192 bytes. As long as there is no better idea around on how doing this, a different code path is acceptable IMHO.
You should probably make sure klv.length % samples_per_byte == 0. 24-bit could have a problem with this. For instance, PAL mono 24-bit 48 kHz -> 5760 B, meaning it'd pick 8192 which isn't divisible by 3.
What I've done now is simply multiplying EUBC by 1920 if it is very small (< 32). This should imply klv.length % samples_per_byte == 0 Updated patch attached.
Shouldn't this also do FFMIN() with current_klv_data.length just like below?
Yes, sorry for forgetting.
- seekpos = st->index_entries[index].pos; + seekpos = st->index_entries[index].pos;
Not intended?
Actually a minor cosmetic change, didn't like the double blank. Regards, Georg
Hi! Are there any news regarding review/rework of this MXF patch? IIRC there were some objections against committing, but no one ever told me what was missing or wrong. Thanks, Georg
On Sat, Nov 05, 2011 at 02:28:20PM +0100, Georg Lippitsch wrote:
Hi!
Are there any news regarding review/rework of this MXF patch? IIRC there were some objections against committing, but no one ever told me what was missing or wrong.
Iam also a bit unhappy about the state of the review/rework and ive largely lost track of what is waiting on what. are there any patches or git repositories that are in their authors oppinon ok to be commited? any that are waiting for a review ? any review comments that are waiting to be implemented ? also testcases that show bugfixes or improvemnts would help what about https://github.com/Tjoppen/FFmpeg ? is some of this ready ? what is holding it up ? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Complexity theory is the science of finding the exact solution to an approximation. Benchmarking OTOH is finding an approximation of the exact
On Sat, 2011-11-05 at 17:40 +0100, Michael Niedermayer wrote:
On Sat, Nov 05, 2011 at 02:28:20PM +0100, Georg Lippitsch wrote:
Hi!
Are there any news regarding review/rework of this MXF patch? IIRC there were some objections against committing, but no one ever told me what was missing or wrong.
Iam also a bit unhappy about the state of the review/rework and ive largely lost track of what is waiting on what.
are there any patches or git repositories that are in their authors oppinon ok to be commited? any that are waiting for a review ? any review comments that are waiting to be implemented ?
also testcases that show bugfixes or improvemnts would help
what about https://github.com/Tjoppen/FFmpeg ? is some of this ready ? what is holding it up ?
Sorry, I've been busy with thesis work and work-work. Here's a quick status update: - I have yet to include Georg's single_eubc patch (mxfdec: Avoid index table parsing if only a single segment with no entries is present) - The changes to mxf_read_header() in 4ac9511 (mxfdec: Parse IndexTableSegments and convert them into AVIndexEntry arrays) belong in 44af6ee (mxfdec: Clip wrapping and seeking support) - The seeking only works properly if the file uses one single contiguous essence container (OpAtom does). In all other case the seeking will be off The last point requires quite a bit of logic to fix since MXF allows essence containers to be split over partitions. Seeking didn't work properly before though, so at least it's not worse. I suppose we can save fixing this for later? Basically, we have: Op1a demuxes fine but seeks won't end up exactly where you expect (just like before). OpAtom demuxes fine and seeks work properly (AFAICT). /Tomas
On Wed, Nov 09, 2011 at 11:10:55AM +0100, Tomas Härdin wrote:
On Sat, 2011-11-05 at 17:40 +0100, Michael Niedermayer wrote:
On Sat, Nov 05, 2011 at 02:28:20PM +0100, Georg Lippitsch wrote:
Hi!
Are there any news regarding review/rework of this MXF patch? IIRC there were some objections against committing, but no one ever told me what was missing or wrong.
Iam also a bit unhappy about the state of the review/rework and ive largely lost track of what is waiting on what.
are there any patches or git repositories that are in their authors oppinon ok to be commited? any that are waiting for a review ? any review comments that are waiting to be implemented ?
also testcases that show bugfixes or improvemnts would help
what about https://github.com/Tjoppen/FFmpeg ? is some of this ready ? what is holding it up ?
Sorry, I've been busy with thesis work and work-work. Here's a quick status update:
- I have yet to include Georg's single_eubc patch (mxfdec: Avoid index table parsing if only a single segment with no entries is present) - The changes to mxf_read_header() in 4ac9511 (mxfdec: Parse IndexTableSegments and convert them into AVIndexEntry arrays) belong in 44af6ee (mxfdec: Clip wrapping and seeking support) - The seeking only works properly if the file uses one single contiguous essence container (OpAtom does). In all other case the seeking will be off
The last point requires quite a bit of logic to fix since MXF allows essence containers to be split over partitions. Seeking didn't work properly before though, so at least it's not worse. I suppose we can save fixing this for later?
You and baptiste are our mxf maintainers, what has to be fixed and what not is your twos decission. More precissely you have a git repo and its your decission what you consider ok for it. i will merge it when you say its ready to be merged if you want my oppinon, then yes fixing this later sounds like the better approach [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Let us carefully observe those good qualities wherein our enemies excel us and endeavor to excel them, by avoiding what is faulty, and imitating what is excellent in them. -- Plutarch
Am 09.11.2011, 11:10 Uhr, schrieb Tomas Härdin <tomas.hardin@codemill.se>:
Basically, we have: Op1a demuxes fine but seeks won't end up exactly where you expect (just like before)
It seems that I've also lost track myself: What was the problem with Op1a? I thought these were working the same as OpAtom. Or do you mean only Op1a with multiple partitions? Georg
participants (5)
-
Baptiste Coudurier -
Carl Eugen Hoyos -
Georg Lippitsch -
Michael Niedermayer -
Tomas Härdin