some time ago I was requested to fix strange ffmpeg bug. by customer words they had op1b files from Panasonic camera ffmpeg doesnt read audio correct (first couple seconds were looped).
First thoughts was “easy money” so I signed up.
the problem was trivial basically op1b allows more 1 essence containers (smpte 319) and each essence stored in its own essence container as result each track is unique in essence container and all audio tracks have same track number so when ffmpeg assign new read essence packet it check track number and always assigned int to first audio track
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv) { int i; for (i = 0; i < s->nb_streams; i++) { MXFTrack *track = s->streams[i]->priv_data; /* SMPTE 379M 7.3 */ if (track && !memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number))) { return i; } } /* return 0 if only one stream, for OP Atom files with 0 as track number */ return s->nb_streams == 1 ? 0 : -1; }
as file had 4 tracks it made loop effect because of all 4 tracks had same audio.
to resolve we need:
1) in MXFContentStorage read field which contains list of all EssenceContainerData (0x1902)
static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset) { MXFContext *mxf = arg; switch (tag) { case 0x1901: if (mxf->packages_refs) av_log(mxf->fc, AV_LOG_VERBOSE, "Multiple packages_refs\n"); av_free(mxf->packages_refs); return mxf_read_strong_ref_array(pb, &mxf->packages_refs, &mxf->packages_count); case 0x1902: av_free(mxf->essence_container_data_refs); return mxf_read_strong_ref_array(pb, &mxf->essence_container_data_refs, &mxf->essence_container_data_count); } return 0; }
2) read each EssenceContainerData (ffmpeg didnt read it at all).
3) each EssenceContainerData has reference to SourcePackage and also holds index sid and body sid
typedef struct MXFEssenceContainerData { UID uid; enum MXFMetadataSetType type; UID package_uid; UID package_ul; int index_sid; int body_sid; } MXFEssenceContainerData; static int mxf_read_essence_container_data(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset) { MXFEssenceContainerData * essence_data = arg; switch(tag) { case 0x2701: /* linked package umid UMID */ avio_read(pb, essence_data->package_ul, 16); avio_read(pb, essence_data->package_uid, 16); break; case 0x3f06: essence_data->index_sid = avio_rb32(pb); break; case 0x3f07: essence_data->body_sid = avio_rb32(pb); break; } return 0; } static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = { //removed to not post too many code { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x23,0x00 }, mxf_read_essence_container_data, sizeof(MXFEssenceContainerData), EssenceContainerData }, { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType }, };
4) go through all tracks from each SourcePackage and assign to each track index and body sid we found in corresponding EssenceContainerData
for (k = 0; k < mxf->essence_container_data_count; k++) { if (!(essence_data = mxf_resolve_strong_ref(mxf, &mxf->essence_container_data_refs[k], EssenceContainerData))) { av_log(mxf, AV_LOG_TRACE, "could not resolve essence container data strong ref\n"); continue; } if (memcmp(component->source_package_ul, essence_data->package_ul, sizeof(UID)) || memcmp(component->source_package_uid, essence_data->package_uid, sizeof(UID))) { continue; } source_track->body_sid = essence_data->body_sid; source_track->index_sid = essence_data->index_sid; }
5) when we read next KLV triplet we look for partition this triplet belongs to and each partition has body sid
static int find_body_sid_by_offset(MXFContext *mxf, int64_t offset) { //we basically look for partition where current klv triplet placed int i; MXFPartition * prev = 0; for (i = 0; i < mxf->partitions_count; ++i) { MXFPartition * partition = &mxf->partitions[i]; if (partition->body_sid) { if (partition->this_partition < offset) { prev = partition; } else { break; } } } if (prev) { return prev->body_sid; } return 0; }
6) when we look for which track to assign this triplet we compare track number and body sid (before was only track number compared)
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv, int body_sid) { int i; for (i = 0; i < s->nb_streams; i++) { MXFTrack *track = s->streams[i]->priv_data; /* SMPTE 379M 7.3 */ //we check body_sid and track->body_sid equal to zero just just to be compatible with old where no body_sid assigned to track if (track && (body_sid == 0 || track->body_sid == 0 || track->body_sid == body_sid) && !memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number))) { return i; } } /* return 0 if only one stream, for OP Atom files with 0 as track number */ return s->nb_streams == 1 ? 0 : -1; }
Those changes fixed audio read 🙂
Unfortunately that wasnt it for me… as seek issues occurred and audio packets were too big (audio was custom wrapped e.g. clip wrapped but split on 2 seconds chunk each chunk in new partition) but this is topic for the next post
P.S. changes described in this post could be found by link:
https://github.com/da8eat/FFmpeg/blob/master/libavformat/mxfdec.c
Â
This post seems not relevant anymore as most of the code posted here already part of FFmpeg and seems for a long time even though I didnt know about it 🙂
Im not sure though if other issues fixed (issues I faced and fixed and promised to describe in next post which still in progress)