X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fmxfdec.c;h=cf4931e7318d64e2e966d03d78235e4fe7d1ed73;hb=b43b95f4789b6e60f9684918fd3c0a5f3f18aef6;hp=8591f6951a59d92041ac343b577c44dceb2a820a;hpb=5b930092c3afd2ae01f1c8aa7fb405911d6ad416;p=ffmpeg diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 8591f6951a5..cf4931e7318 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -43,7 +43,7 @@ * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1 */ -#include +#include #include "libavutil/aes.h" #include "libavutil/mathematics.h" @@ -72,7 +72,7 @@ typedef enum { OPSonyOpt, /* FATE sample, violates the spec in places */ } MXFOP; -typedef struct { +typedef struct MXFPartition { int closed; int complete; MXFPartitionType type; @@ -88,13 +88,13 @@ typedef struct { int pack_length; } MXFPartition; -typedef struct { +typedef struct MXFCryptoContext { UID uid; enum MXFMetadataSetType type; UID source_container_ul; } MXFCryptoContext; -typedef struct { +typedef struct MXFStructuralComponent { UID uid; enum MXFMetadataSetType type; UID source_package_uid; @@ -104,16 +104,17 @@ typedef struct { int source_track_id; } MXFStructuralComponent; -typedef struct { +typedef struct MXFSequence { UID uid; enum MXFMetadataSetType type; UID data_definition_ul; UID *structural_components_refs; int structural_components_count; int64_t duration; + uint8_t origin; } MXFSequence; -typedef struct { +typedef struct MXFTrack { UID uid; enum MXFMetadataSetType type; MXFSequence *sequence; /* mandatory, and only one */ @@ -126,7 +127,7 @@ typedef struct { int64_t original_duration; /* st->duration in SampleRate/EditRate units */ } MXFTrack; -typedef struct { +typedef struct MXFDescriptor { UID uid; enum MXFMetadataSetType type; UID essence_container_ul; @@ -152,7 +153,7 @@ typedef struct { enum AVPixelFormat pix_fmt; } MXFDescriptor; -typedef struct { +typedef struct MXFIndexTableSegment { UID uid; enum MXFMetadataSetType type; int edit_unit_byte_count; @@ -167,7 +168,7 @@ typedef struct { int nb_index_entries; } MXFIndexTableSegment; -typedef struct { +typedef struct MXFPackage { UID uid; enum MXFMetadataSetType type; UID package_uid; @@ -177,13 +178,13 @@ typedef struct { UID descriptor_ref; } MXFPackage; -typedef struct { +typedef struct MXFMetadataSet { UID uid; enum MXFMetadataSetType type; } MXFMetadataSet; /* decoded index table */ -typedef struct { +typedef struct MXFIndexTable { int index_sid; int body_sid; int nb_ptses; /* number of PTSes or total duration of index */ @@ -194,7 +195,7 @@ typedef struct { AVIndexEntry *fake_index; /* used for calling ff_index_search_timestamp() */ } MXFIndexTable; -typedef struct { +typedef struct MXFContext { MXFPartition *partitions; unsigned partitions_count; MXFOP op; @@ -206,6 +207,7 @@ typedef struct { struct AVAES *aesc; uint8_t *local_tags; int local_tags_count; + uint64_t last_partition; uint64_t footer_partition; KLVPacket current_klv_data; int current_klv_index; @@ -228,7 +230,7 @@ enum MXFWrappingScheme { /* NOTE: klv_offset is not set (-1) for local keys */ typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset); -typedef struct { +typedef struct MXFMetadataReadTableEntry { const UID key; MXFMetadataReadFunc *read; int ctx_size; @@ -239,12 +241,13 @@ typedef struct { static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 }; static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 }; static const uint8_t mxf_avid_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 }; -static const uint8_t mxf_system_item_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04 }; +static const uint8_t mxf_system_item_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x03,0x01,0x04 }; static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 }; /* complete keys to match */ static const uint8_t mxf_crypto_source_container_ul[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x02,0x02,0x00,0x00,0x00 }; static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 }; static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 }; +static const uint8_t mxf_random_index_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x11,0x01,0x00 }; static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 }; #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y))) @@ -317,15 +320,15 @@ static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, data_ptr = pkt->data; end_ptr = pkt->data + length; buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */ - for (; end_ptr - buf_ptr >= st->codec->channels * 4; ) { - for (i = 0; i < st->codec->channels; i++) { + for (; end_ptr - buf_ptr >= st->codecpar->channels * 4; ) { + for (i = 0; i < st->codecpar->channels; i++) { uint32_t sample = bytestream_get_le32(&buf_ptr); - if (st->codec->bits_per_coded_sample == 24) + if (st->codecpar->bits_per_coded_sample == 24) bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff); else bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff); } - buf_ptr += 32 - st->codec->channels*4; // always 8 channels stored SMPTE 331M + buf_ptr += 32 - st->codecpar->channels*4; // always 8 channels stored SMPTE 331M } av_shrink_packet(pkt, data_ptr - pkt->data); return 0; @@ -476,7 +479,27 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size avio_read(pb, op, sizeof(UID)); nb_essence_containers = avio_rb32(pb); - /* some files don'thave FooterPartition set in every partition */ + if (partition->this_partition && + partition->previous_partition == partition->this_partition) { + av_log(mxf->fc, AV_LOG_ERROR, + "PreviousPartition equal to ThisPartition %"PRIx64"\n", + partition->previous_partition); + /* override with the actual previous partition offset */ + if (!mxf->parsing_backward && mxf->last_forward_partition > 1) { + MXFPartition *prev = + mxf->partitions + mxf->last_forward_partition - 2; + partition->previous_partition = prev->this_partition; + } + /* if no previous body partition are found point to the header + * partition */ + if (partition->previous_partition == partition->this_partition) + partition->previous_partition = 0; + av_log(mxf->fc, AV_LOG_ERROR, + "Overriding PreviousPartition with %"PRIx64"\n", + partition->previous_partition); + } + + /* some files don't have FooterPartition set in every partition */ if (footer_partition) { if (mxf->footer_partition && mxf->footer_partition != footer_partition) { av_log(mxf->fc, AV_LOG_ERROR, @@ -487,7 +510,7 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size } } - av_dlog(mxf->fc, + av_log(mxf->fc, AV_LOG_TRACE, "PartitionPack: ThisPartition = 0x%"PRIX64 ", PreviousPartition = 0x%"PRIX64", " "FooterPartition = 0x%"PRIX64", IndexSID = %i, BodySID = %i\n", @@ -524,7 +547,7 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size /* only nag once */ if (!mxf->op) av_log(mxf->fc, AV_LOG_WARNING, - "\"OPAtom\" with %u ECs - assuming %s\n", + "\"OPAtom\" with %"PRIu32" ECs - assuming %s\n", nb_essence_containers, op == OP1a ? "OP1a" : "OPAtom"); @@ -537,14 +560,15 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size } if (partition->kag_size <= 0 || partition->kag_size > (1 << 20)) { - av_log(mxf->fc, AV_LOG_WARNING, "invalid KAGSize %i - guessing ", partition->kag_size); + av_log(mxf->fc, AV_LOG_WARNING, "invalid KAGSize %"PRId32" - guessing ", + partition->kag_size); if (mxf->op == OPSonyOpt) partition->kag_size = 512; else partition->kag_size = 1; - av_log(mxf->fc, AV_LOG_WARNING, "%i\n", partition->kag_size); + av_log(mxf->fc, AV_LOG_WARNING, "%"PRId32"\n", partition->kag_size); } return 0; @@ -642,7 +666,7 @@ static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid case 0x4804: avio_read(pb, track->track_number, 4); break; - case 0x4B01: + case 0x4b01: track->edit_rate.num = avio_rb32(pb); track->edit_rate.den = avio_rb32(pb); break; @@ -663,6 +687,9 @@ static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID case 0x0201: avio_read(pb, sequence->data_definition_ul, 16); break; + case 0x4b02: + sequence->origin = avio_r8(pb); + break; case 0x1001: sequence->structural_components_count = avio_rb32(pb); if (sequence->structural_components_count >= UINT_MAX / sizeof(UID)) @@ -748,32 +775,32 @@ static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int switch(tag) { case 0x3F05: segment->edit_unit_byte_count = avio_rb32(pb); - av_dlog(NULL, "EditUnitByteCount %d\n", segment->edit_unit_byte_count); + av_log(NULL, AV_LOG_TRACE, "EditUnitByteCount %d\n", segment->edit_unit_byte_count); break; case 0x3F06: segment->index_sid = avio_rb32(pb); - av_dlog(NULL, "IndexSID %d\n", segment->index_sid); + av_log(NULL, AV_LOG_TRACE, "IndexSID %d\n", segment->index_sid); break; case 0x3F07: segment->body_sid = avio_rb32(pb); - av_dlog(NULL, "BodySID %d\n", segment->body_sid); + av_log(NULL, AV_LOG_TRACE, "BodySID %d\n", segment->body_sid); break; case 0x3F0A: - av_dlog(NULL, "IndexEntryArray found\n"); + av_log(NULL, AV_LOG_TRACE, "IndexEntryArray found\n"); return mxf_read_index_entry_array(pb, segment); case 0x3F0B: segment->index_edit_rate.num = avio_rb32(pb); segment->index_edit_rate.den = avio_rb32(pb); - av_dlog(NULL, "IndexEditRate %d/%d\n", segment->index_edit_rate.num, + av_log(NULL, AV_LOG_TRACE, "IndexEditRate %d/%d\n", segment->index_edit_rate.num, segment->index_edit_rate.den); break; case 0x3F0C: segment->index_start_position = avio_rb64(pb); - av_dlog(NULL, "IndexStartPosition %"PRId64"\n", segment->index_start_position); + av_log(NULL, AV_LOG_TRACE, "IndexStartPosition %"PRId64"\n", segment->index_start_position); break; case 0x3F0D: segment->index_duration = avio_rb64(pb); - av_dlog(NULL, "IndexDuration %"PRId64"\n", segment->index_duration); + av_log(NULL, AV_LOG_TRACE, "IndexDuration %"PRId64"\n", segment->index_duration); break; } return 0; @@ -787,9 +814,9 @@ static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor) do { code = avio_r8(pb); value = avio_r8(pb); - av_dlog(NULL, "pixel layout: code %#x\n", code); + av_log(NULL, AV_LOG_TRACE, "pixel layout: code %#x\n", code); - if (ofs < 16) { + if (ofs <= 14) { layout[ofs++] = code; layout[ofs++] = value; } @@ -868,7 +895,7 @@ static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) { av_free(descriptor->extradata); descriptor->extradata_size = 0; - descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); + descriptor->extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE); if (!descriptor->extradata) return AVERROR(ENOMEM); descriptor->extradata_size = size; @@ -920,34 +947,44 @@ static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMe static const MXFCodecUL mxf_picture_essence_container_uls[] = { // video essence container uls - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MPEG-ES Frame wrapped */ - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, AV_CODEC_ID_DVVIDEO }, /* DV 625 25mbps */ - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14, AV_CODEC_ID_RAWVIDEO }, /* Uncompressed Picture */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MPEG-ES frame-wrapped */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, AV_CODEC_ID_DVVIDEO }, /* DV 625 25mbps */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14, AV_CODEC_ID_RAWVIDEO }, /* uncompressed picture */ { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE }, }; /* EC ULs for intra-only formats */ static const MXFCodecUL mxf_intra_only_essence_container_uls[] = { - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x00,0x00 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MXF-GC SMPTE D-10 Mappings */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x00,0x00 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MXF-GC SMPTE D-10 mappings */ { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE }, }; /* intra-only PictureEssenceCoding ULs, where no corresponding EC UL exists */ static const MXFCodecUL mxf_intra_only_picture_essence_coding_uls[] = { - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, AV_CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra Profiles */ - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, 14, AV_CODEC_ID_JPEG2000 }, /* JPEG2000 Codestream */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, AV_CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra Profiles */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, 14, AV_CODEC_ID_JPEG2000 }, /* JPEG 2000 code stream */ { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE }, }; static const MXFCodecUL mxf_sound_essence_container_uls[] = { // sound essence container uls - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, AV_CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */ - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, AV_CODEC_ID_MP2 }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */ - { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, AV_CODEC_ID_PCM_S16LE }, /* D-10 Mapping 50Mbps PAL Extended Template */ - { { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0xFF,0x4B,0x46,0x41,0x41,0x00,0x0D,0x4D,0x4F }, 14, AV_CODEC_ID_PCM_S16LE }, /* 0001GL00.MXF.A1.mxf_opatom.mxf */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, AV_CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, AV_CODEC_ID_MP2 }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, AV_CODEC_ID_PCM_S16LE }, /* D-10 Mapping 50Mbps PAL Extended Template */ + { { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0xff,0x4b,0x46,0x41,0x41,0x00,0x0d,0x4d,0x4F }, 14, AV_CODEC_ID_PCM_S16LE }, /* 0001GL00.MXF.A1.mxf_opatom.mxf */ + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x03,0x04,0x02,0x02,0x02,0x03,0x03,0x01,0x00 }, 14, AV_CODEC_ID_AAC }, /* MPEG-2 AAC ADTS (legacy) */ { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE }, }; +static const MXFCodecUL mxf_data_essence_container_uls[] = { + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 }, 16, 0 }, + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 }, 16, AV_CODEC_ID_NONE }, +}; + +static const char * const mxf_data_essence_descriptor[] = { + "vbi_vanc_smpte_436M", +}; + static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments) { int i, j, nb_segments = 0; @@ -1332,7 +1369,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) MXFPackage *temp_package = NULL; int i, j, k, ret; - av_dlog(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count); + av_log(mxf->fc, AV_LOG_TRACE, "metadata sets count %d\n", mxf->metadata_sets_count); /* TODO: handle multiple material packages (OP3x) */ for (i = 0; i < mxf->packages_count; i++) { material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage); @@ -1383,7 +1420,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) } } if (!source_package) { - av_dlog(mxf->fc, "material track %d: no corresponding source package found\n", material_track->track_id); + av_log(mxf->fc, AV_LOG_TRACE, "material track %d: no corresponding source package found\n", material_track->track_id); break; } for (k = 0; k < source_package->tracks_count; k++) { @@ -1447,7 +1484,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul); codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul); - st->codec->codec_type = codec_ul->id; + st->codecpar->codec_type = codec_ul->id; source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType); if (source_package->descriptor) { @@ -1489,16 +1526,16 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */ codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul); - st->codec->codec_id = codec_ul->id; + st->codecpar->codec_id = codec_ul->id; - if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { source_track->intra_only = mxf_is_intra_only(descriptor); container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul); - if (st->codec->codec_id == AV_CODEC_ID_NONE) - st->codec->codec_id = container_ul->id; - st->codec->width = descriptor->width; + if (st->codecpar->codec_id == AV_CODEC_ID_NONE) + st->codecpar->codec_id = container_ul->id; + st->codecpar->width = descriptor->width; /* Field height, not frame height */ - st->codec->height = descriptor->height; + st->codecpar->height = descriptor->height; switch (descriptor->frame_layout) { case SegmentedFrame: /* This one is a weird layout I don't fully understand. */ @@ -1506,7 +1543,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) "SegmentedFrame layout isn't currently supported\n"); break; case FullFrame: - st->codec->field_order = AV_FIELD_PROGRESSIVE; + st->codecpar->field_order = AV_FIELD_PROGRESSIVE; break; case OneField: /* Every other line is stored and needs to be duplicated. */ @@ -1521,10 +1558,10 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) case MixedFields: switch (descriptor->field_dominance) { case MXF_TFF: - st->codec->field_order = AV_FIELD_TT; + st->codecpar->field_order = AV_FIELD_TT; break; case MXF_BFF: - st->codec->field_order = AV_FIELD_BB; + st->codecpar->field_order = AV_FIELD_BB; break; default: avpriv_request_sample(mxf->fc, @@ -1533,39 +1570,50 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) break; } /* Turn field height into frame height. */ - st->codec->height *= 2; + st->codecpar->height *= 2; + break; default: av_log(mxf->fc, AV_LOG_INFO, "Unknown frame layout type: %d\n", descriptor->frame_layout); } - if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO) { - st->codec->pix_fmt = descriptor->pix_fmt; - if (st->codec->pix_fmt == AV_PIX_FMT_NONE) { + if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) { + st->codecpar->format = descriptor->pix_fmt; + if (st->codecpar->format == AV_PIX_FMT_NONE) { pix_fmt_ul = mxf_get_codec_ul(ff_mxf_pixel_format_uls, &descriptor->essence_codec_ul); - st->codec->pix_fmt = pix_fmt_ul->id; - if (st->codec->pix_fmt == AV_PIX_FMT_NONE) { + st->codecpar->format = pix_fmt_ul->id; + if (st->codecpar->format == AV_PIX_FMT_NONE) { /* support files created before RP224v10 by defaulting to UYVY422 if subsampling is 4:2:2 and component depth is 8-bit */ if (descriptor->horiz_subsampling == 2 && descriptor->vert_subsampling == 1 && descriptor->component_depth == 8) { - st->codec->pix_fmt = AV_PIX_FMT_UYVY422; + st->codecpar->format = AV_PIX_FMT_UYVY422; } } } } st->need_parsing = AVSTREAM_PARSE_HEADERS; - } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { + if (material_track->sequence->origin) { + char material_origin[3]; + snprintf(material_origin, sizeof(material_origin), "%d", material_track->sequence->origin); + av_dict_set(&st->metadata, "material_track_origin", material_origin, 0); + } + if (source_track->sequence->origin) { + char source_origin[3]; + snprintf(source_origin, sizeof(source_origin), "%d", source_track->sequence->origin); + av_dict_set(&st->metadata, "source_track_origin", source_origin, 0); + } + } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul); - if (st->codec->codec_id == AV_CODEC_ID_NONE) - st->codec->codec_id = container_ul->id; - st->codec->channels = descriptor->channels; - st->codec->bits_per_coded_sample = descriptor->bits_per_sample; + if (st->codecpar->codec_id == AV_CODEC_ID_NONE) + st->codecpar->codec_id = container_ul->id; + st->codecpar->channels = descriptor->channels; + st->codecpar->bits_per_coded_sample = descriptor->bits_per_sample; if (descriptor->sample_rate.den > 0) { - st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den; + st->codecpar->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den; avpriv_set_pts_info(st, 64, descriptor->sample_rate.den, descriptor->sample_rate.num); } else { av_log(mxf->fc, AV_LOG_WARNING, "invalid sample rate (%d/%d) " @@ -1582,32 +1630,40 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) st->time_base); /* TODO: implement AV_CODEC_ID_RAWAUDIO */ - if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) { + if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) { if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24) - st->codec->codec_id = AV_CODEC_ID_PCM_S24LE; + st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE; else if (descriptor->bits_per_sample == 32) - st->codec->codec_id = AV_CODEC_ID_PCM_S32LE; - } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) { + st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE; + } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE) { if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24) - st->codec->codec_id = AV_CODEC_ID_PCM_S24BE; + st->codecpar->codec_id = AV_CODEC_ID_PCM_S24BE; else if (descriptor->bits_per_sample == 32) - st->codec->codec_id = AV_CODEC_ID_PCM_S32BE; - } else if (st->codec->codec_id == AV_CODEC_ID_MP2) { + st->codecpar->codec_id = AV_CODEC_ID_PCM_S32BE; + } else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) { st->need_parsing = AVSTREAM_PARSE_FULL; } + } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) { + int codec_id = mxf_get_codec_ul(mxf_data_essence_container_uls, + essence_container_ul)->id; + if (codec_id >= 0 && + codec_id < FF_ARRAY_ELEMS(mxf_data_essence_descriptor)) { + av_dict_set(&st->metadata, "data_type", + mxf_data_essence_descriptor[codec_id], 0); + } } if (descriptor->extradata) { - st->codec->extradata = av_mallocz(descriptor->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); - if (st->codec->extradata) { - memcpy(st->codec->extradata, descriptor->extradata, descriptor->extradata_size); - st->codec->extradata_size = descriptor->extradata_size; + st->codecpar->extradata = av_mallocz(descriptor->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (st->codecpar->extradata) { + memcpy(st->codecpar->extradata, descriptor->extradata, descriptor->extradata_size); + st->codecpar->extradata_size = descriptor->extradata_size; } - } else if (st->codec->codec_id == AV_CODEC_ID_H264) { + } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) { ret = ff_generate_avci_extradata(st); if (ret < 0) return ret; } - if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) { + if (st->codecpar->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) { /* TODO: decode timestamps */ st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS; } @@ -1618,34 +1674,163 @@ fail_and_free: return ret; } +static int mxf_read_utf16_string(AVIOContext *pb, int size, char** str) +{ + int ret; + size_t buf_size; + + if (size < 0) + return AVERROR(EINVAL); + + buf_size = size + size / 2 + 1; + *str = av_malloc(buf_size); + if (!*str) + return AVERROR(ENOMEM); + + if ((ret = avio_get_str16be(pb, size, *str, buf_size)) < 0) { + av_freep(str); + return ret; + } + + return ret; +} + +static int mxf_uid_to_str(UID uid, char **str) +{ + int i; + char *p; + p = *str = av_mallocz(sizeof(UID) * 2 + 4 + 1); + if (!p) + return AVERROR(ENOMEM); + for (i = 0; i < sizeof(UID); i++) { + snprintf(p, 2 + 1, "%.2x", uid[i]); + p += 2; + if (i == 3 || i == 5 || i == 7 || i == 9) { + snprintf(p, 1 + 1, "-"); + p++; + } + } + return 0; +} + +static int mxf_timestamp_to_str(uint64_t timestamp, char **str) +{ + struct tm time = { 0 }; + time.tm_year = (timestamp >> 48) - 1900; + time.tm_mon = (timestamp >> 40 & 0xFF) - 1; + time.tm_mday = (timestamp >> 32 & 0xFF); + time.tm_hour = (timestamp >> 24 & 0xFF); + time.tm_min = (timestamp >> 16 & 0xFF); + time.tm_sec = (timestamp >> 8 & 0xFF); + + /* msvcrt versions of strftime calls the invalid parameter handler + * (aborting the process if one isn't set) if the parameters are out + * of range. */ + time.tm_mon = av_clip(time.tm_mon, 0, 11); + time.tm_mday = av_clip(time.tm_mday, 1, 31); + time.tm_hour = av_clip(time.tm_hour, 0, 23); + time.tm_min = av_clip(time.tm_min, 0, 59); + time.tm_sec = av_clip(time.tm_sec, 0, 59); + + *str = av_mallocz(32); + if (!*str) + return AVERROR(ENOMEM); + if (!strftime(*str, 32, "%Y-%m-%d %H:%M:%S", &time)) + (*str)[0] = '\0'; + + return 0; +} + +#define SET_STR_METADATA(pb, name, str) do { \ + if ((ret = mxf_read_utf16_string(pb, size, &str)) < 0) \ + return ret; \ + av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \ +} while (0) + +#define SET_UID_METADATA(pb, name, var, str) do { \ + avio_read(pb, var, 16); \ + if ((ret = mxf_uid_to_str(var, &str)) < 0) \ + return ret; \ + av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \ +} while (0) + +#define SET_TS_METADATA(pb, name, var, str) do { \ + var = avio_rb64(pb); \ + if ((ret = mxf_timestamp_to_str(var, &str)) < 0) \ + return ret; \ + av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \ +} while (0) + +static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset) +{ + MXFContext *mxf = arg; + AVFormatContext *s = mxf->fc; + int ret; + UID uid = { 0 }; + char *str = NULL; + uint64_t ts; + switch (tag) { + case 0x3C01: + SET_STR_METADATA(pb, "company_name", str); + break; + case 0x3C02: + SET_STR_METADATA(pb, "product_name", str); + break; + case 0x3C04: + SET_STR_METADATA(pb, "product_version", str); + break; + case 0x3C05: + SET_UID_METADATA(pb, "product_uid", uid, str); + break; + case 0x3C06: + SET_TS_METADATA(pb, "modification_date", ts, str); + break; + case 0x3C08: + SET_STR_METADATA(pb, "application_platform", str); + break; + case 0x3C09: + SET_UID_METADATA(pb, "generation_uid", uid, str); + break; + case 0x3C0A: + SET_UID_METADATA(pb, "uid", uid, str); + break; + } + return 0; +} + 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 }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Generic Sound */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG 2 Video */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */ - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext }, - { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment }, + { { 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,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2f,0x00 }, mxf_read_preface_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 }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x30,0x00 }, mxf_read_identification_metadata }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Generic Sound */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG 2 Video */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2VideoDescriptor */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5c,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* VANC/VBI - SMPTE 436M */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5e,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2AudioDescriptor */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */ + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext }, + { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment }, { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType }, }; @@ -1664,7 +1849,7 @@ static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadF uint64_t next = avio_tell(pb) + size; UID uid = {0}; - av_dlog(mxf->fc, "local tag %#04x size %d\n", tag, size); + av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x size %d\n", tag, size); if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */ av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag); continue; @@ -1675,7 +1860,7 @@ static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadF int local_tag = AV_RB16(mxf->local_tags+i*18); if (local_tag == tag) { memcpy(uid, mxf->local_tags+i*18+2, 16); - av_dlog(mxf->fc, "local tag %#04x\n", local_tag); + av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x\n", local_tag); PRINT_KEY(mxf->fc, "uid", uid); } } @@ -1718,7 +1903,7 @@ static int mxf_seek_to_previous_partition(MXFContext *mxf) avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET); mxf->current_partition = NULL; - av_dlog(mxf->fc, "seeking to previous partition\n"); + av_log(mxf->fc, AV_LOG_TRACE, "seeking to previous partition\n"); return 1; } @@ -1735,25 +1920,30 @@ static int mxf_parse_handle_essence(MXFContext *mxf) if (mxf->parsing_backward) { return mxf_seek_to_previous_partition(mxf); } else { - if (!mxf->footer_partition) { - av_dlog(mxf->fc, "no footer\n"); + uint64_t offset = mxf->footer_partition ? mxf->footer_partition + : mxf->last_partition; + + if (!offset) { + av_log(mxf->fc, AV_LOG_TRACE, "no last partition\n"); return 0; } - av_dlog(mxf->fc, "seeking to footer\n"); + av_log(mxf->fc, AV_LOG_TRACE, "seeking to last partition\n"); /* remember where we were so we don't end up seeking further back than this */ mxf->last_forward_tell = avio_tell(pb); - if (!pb->seekable) { - av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing footer\n"); + if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) { + av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing last partition\n"); return -1; } - /* seek to footer partition and parse backward */ - if ((ret = avio_seek(pb, mxf->run_in + mxf->footer_partition, SEEK_SET)) < 0) { - av_log(mxf->fc, AV_LOG_ERROR, "failed to seek to footer @ 0x%"PRIx64" (%"PRId64") - partial file?\n", - mxf->run_in + mxf->footer_partition, ret); + /* seek to last partition and parse backward */ + if ((ret = avio_seek(pb, mxf->run_in + offset, SEEK_SET)) < 0) { + av_log(mxf->fc, AV_LOG_ERROR, + "failed to seek to last partition @ 0x%" PRIx64 + " (%"PRId64") - partial file?\n", + mxf->run_in + offset, ret); return ret; } @@ -1869,8 +2059,8 @@ static void mxf_handle_small_eubc(AVFormatContext *s) /* expect PCM with exactly one index table segment and a small (< 32) EUBC */ if (s->nb_streams != 1 || - s->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO || - !is_pcm(s->streams[0]->codec->codec_id) || + s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO || + !is_pcm(s->streams[0]->codecpar->codec_id) || mxf->nb_index_tables != 1 || mxf->index_tables[0].nb_segments != 1 || mxf->index_tables[0].segments[0]->edit_unit_byte_count >= 32) @@ -1883,6 +2073,34 @@ static void mxf_handle_small_eubc(AVFormatContext *s) mxf->edit_units_per_packet = 1920; } +static void mxf_read_random_index_pack(AVFormatContext *s) +{ + MXFContext *mxf = s->priv_data; + uint32_t length; + int64_t file_size; + KLVPacket klv; + + if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) + return; + + file_size = avio_size(s->pb); + avio_seek(s->pb, file_size - 4, SEEK_SET); + length = avio_rb32(s->pb); + if (length <= 32 || length >= FFMIN(file_size, INT_MAX)) + goto end; + avio_seek(s->pb, file_size - length, SEEK_SET); + if (klv_read_packet(&klv, s->pb) < 0 || + !IS_KLV_KEY(klv.key, mxf_random_index_pack_key) || + klv.length != length - 20) + goto end; + + avio_skip(s->pb, klv.length - 12); + mxf->last_partition = avio_rb64(s->pb); + +end: + avio_seek(s->pb, mxf->run_in, SEEK_SET); +} + static int mxf_read_header(AVFormatContext *s) { MXFContext *mxf = s->priv_data; @@ -1901,7 +2119,10 @@ static int mxf_read_header(AVFormatContext *s) mxf->fc = s; mxf->run_in = avio_tell(s->pb); + mxf_read_random_index_pack(s); + while (!s->pb->eof_reached) { + const MXFMetadataReadTableEntry *metadata; if (klv_read_packet(&klv, s->pb) < 0) { @@ -1913,7 +2134,7 @@ static int mxf_read_header(AVFormatContext *s) } PRINT_KEY(s, "read header", klv.key); - av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); + av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) || IS_KLV_KEY(klv.key, mxf_essence_element_key) || IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) || @@ -1969,6 +2190,9 @@ static int mxf_read_header(AVFormatContext *s) return res; } break; + } else { + av_log(s, AV_LOG_VERBOSE, "Dark key " PRIxUID "\n", + UID_ARG(klv.key)); } } if (!metadata->read) @@ -2091,18 +2315,21 @@ static int mxf_compute_sample_count(MXFContext *mxf, int stream_index, return 0; } -static int mxf_set_audio_pts(MXFContext *mxf, AVCodecContext *codec, +static int mxf_set_audio_pts(MXFContext *mxf, AVCodecParameters *par, AVPacket *pkt) { MXFTrack *track = mxf->fc->streams[pkt->stream_index]->priv_data; - int64_t bits_per_sample = av_get_bits_per_sample(codec->codec_id); + int64_t bits_per_sample = par->bits_per_coded_sample; + + if (!bits_per_sample) + bits_per_sample = av_get_bits_per_sample(par->codec_id); pkt->pts = track->sample_count; - if (codec->channels <= 0 || codec->channels * bits_per_sample < 8) + if (par->channels <= 0 || par->channels * bits_per_sample < 8) return AVERROR_INVALIDDATA; - track->sample_count += pkt->size / (codec->channels * bits_per_sample / 8); + track->sample_count += pkt->size / (par->channels * bits_per_sample / 8); return 0; } @@ -2110,17 +2337,18 @@ static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt) { KLVPacket klv; MXFContext *mxf = s->priv_data; + int ret; while (!s->pb->eof_reached) { - if (klv_read_packet(&klv, s->pb) < 0) - return -1; + if ((ret = klv_read_packet(&klv, s->pb)) < 0) + return ret; PRINT_KEY(s, "read packet", klv.key); - av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); + av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) { - int res = mxf_decrypt_triplet(s, pkt, &klv); - if (res < 0) { + ret = mxf_decrypt_triplet(s, pkt, &klv); + if (ret < 0) { av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n"); - return -1; + return ret; } return 0; } @@ -2130,10 +2358,12 @@ static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt) int64_t next_ofs, next_klv; AVStream *st; MXFTrack *track; - AVCodecContext *codec; + AVCodecParameters *par; if (index < 0) { - av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12)); + av_log(s, AV_LOG_ERROR, + "error getting stream index %"PRIu32"\n", + AV_RB32(klv.key + 12)); goto skip; } @@ -2160,21 +2390,23 @@ static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt) /* check for 8 channels AES3 element */ if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) { - if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) { + ret = mxf_get_d10_aes3_packet(s->pb, s->streams[index], + pkt, klv.length); + if (ret < 0) { av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n"); - return -1; + return ret; } } else { - int ret = av_get_packet(s->pb, pkt, klv.length); + ret = av_get_packet(s->pb, pkt, klv.length); if (ret < 0) return ret; } pkt->stream_index = index; pkt->pos = klv.offset; - codec = s->streams[index]->codec; + par = s->streams[index]->codecpar; - if (codec->codec_type == AVMEDIA_TYPE_VIDEO && next_ofs >= 0) { + if (par->codec_type == AVMEDIA_TYPE_VIDEO && next_ofs >= 0) { /* mxf->current_edit_unit good - see if we have an * index table to derive timestamps from */ MXFIndexTable *t = &mxf->index_tables[0]; @@ -2189,8 +2421,8 @@ static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt) * < PTS if low_delay = 0 (Sony IMX30) */ pkt->pts = mxf->current_edit_unit; } - } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) { - int ret = mxf_set_audio_pts(mxf, codec, pkt); + } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) { + ret = mxf_set_audio_pts(mxf, par, pkt); if (ret < 0) return ret; } @@ -2247,17 +2479,17 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) if ((ret64 = avio_seek(s->pb, pos, SEEK_SET)) < 0) return ret64; - if ((ret = av_get_packet(s->pb, pkt, size)) != size) - return ret < 0 ? ret : AVERROR_EOF; + if ((ret = av_get_packet(s->pb, pkt, size)) != size) + return ret < 0 ? ret : AVERROR_EOF; pkt->stream_index = 0; - if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && t->ptses && + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && t->ptses && mxf->current_edit_unit >= 0 && mxf->current_edit_unit < t->nb_ptses) { pkt->dts = mxf->current_edit_unit + t->first_dts; pkt->pts = t->ptses[mxf->current_edit_unit]; - } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { - int ret = mxf_set_audio_pts(mxf, st->codec, pkt); + } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + int ret = mxf_set_audio_pts(mxf, st->codecpar, pkt); if (ret < 0) return ret; } @@ -2349,7 +2581,7 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti MXFTrack *source_track = st->priv_data; /* if audio then truncate sample_time to EditRate */ - if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) sample_time = av_rescale_q(sample_time, st->time_base, av_inv_q(source_track->edit_rate)); @@ -2396,7 +2628,7 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti AVStream *cur_st = s->streams[i]; MXFTrack *cur_track = cur_st->priv_data; uint64_t current_sample_count = 0; - if (cur_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { + if (cur_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { ret = mxf_compute_sample_count(mxf, i, ¤t_sample_count); if (ret < 0) return ret;