X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fmxfdec.c;h=1f79f3d3cdb0e3ee3a1f000ea3fdad4a9938767f;hb=7ab5192260c387a10998481fe1468f1304f6ebd2;hp=134f27784bdf318ae6327530066b37ad12782e4a;hpb=a12063b118ad05409ff775ba30fab00265ad3031;p=ffmpeg diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c index 134f27784bd..1f79f3d3cdb 100644 --- a/libavformat/mxfdec.c +++ b/libavformat/mxfdec.c @@ -28,6 +28,7 @@ * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container + * SMPTE 2067-21 Interoperable Master Format — Application #2E * * Principle * Search for Track numbers which will identify essence element KLV packets. @@ -47,11 +48,13 @@ #include "libavutil/aes.h" #include "libavutil/avassert.h" +#include "libavutil/mastering_display_metadata.h" #include "libavutil/mathematics.h" #include "libavcodec/bytestream.h" #include "libavutil/intreadwrite.h" #include "libavutil/parseutils.h" #include "libavutil/timecode.h" +#include "libavutil/opt.h" #include "avformat.h" #include "internal.h" #include "mxf.h" @@ -130,7 +133,7 @@ typedef struct MXFSequence { uint8_t origin; } MXFSequence; -typedef struct MXFTrack { +typedef struct MXFTimecodeComponent { UID uid; enum MXFMetadataSetType type; int drop_frame; @@ -198,6 +201,9 @@ typedef struct MXFDescriptor { int bits_per_sample; int64_t duration; /* ContainerDuration optional property */ unsigned int component_depth; + unsigned int black_ref_level; + unsigned int white_ref_level; + unsigned int color_range; unsigned int horiz_subsampling; unsigned int vert_subsampling; UID *sub_descriptors_refs; @@ -206,6 +212,12 @@ typedef struct MXFDescriptor { uint8_t *extradata; int extradata_size; enum AVPixelFormat pix_fmt; + UID color_primaries_ul; + UID color_trc_ul; + UID color_space_ul; + AVMasteringDisplayMetadata *mastering; + AVContentLightMetadata *coll; + size_t coll_size; } MXFDescriptor; typedef struct MXFIndexTableSegment { @@ -265,6 +277,7 @@ typedef struct MXFIndexTable { } MXFIndexTable; typedef struct MXFContext { + const AVClass *class; /**< Class for private options. */ MXFPartition *partitions; unsigned partitions_count; MXFOP op; @@ -287,6 +300,7 @@ typedef struct MXFContext { int last_forward_partition; int nb_index_tables; MXFIndexTable *index_tables; + int eia608_extract; } MXFContext; /* NOTE: klv_offset is not set (-1) for local keys */ @@ -309,6 +323,7 @@ static const uint8_t mxf_canopus_essence_element_key[] = { 0x06,0x0e,0x2b,0x static const uint8_t mxf_system_item_key_cp[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x03,0x01,0x04 }; static const uint8_t mxf_system_item_key_gc[] = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x03,0x01,0x14 }; static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 }; +static const uint8_t mxf_apple_coll_prefix[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x0e,0x20,0x04,0x01,0x05,0x03,0x01 }; /* 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 }; @@ -319,6 +334,8 @@ static const uint8_t mxf_avid_project_name[] = { 0xa5,0xfb,0x7b,0x static const uint8_t mxf_jp2k_rsiz[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }; static const uint8_t mxf_indirect_value_utf16le[] = { 0x4c,0x00,0x02,0x10,0x01,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 }; static const uint8_t mxf_indirect_value_utf16be[] = { 0x42,0x01,0x10,0x02,0x00,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 }; +static const uint8_t mxf_apple_coll_max_cll[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x0e,0x20,0x04,0x01,0x05,0x03,0x01,0x01 }; +static const uint8_t mxf_apple_coll_max_fall[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x0e,0x20,0x04,0x01,0x05,0x03,0x01,0x02 }; #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y))) @@ -328,6 +345,8 @@ static void mxf_free_metadataset(MXFMetadataSet **ctx, int freectx) switch ((*ctx)->type) { case Descriptor: av_freep(&((MXFDescriptor *)*ctx)->extradata); + av_freep(&((MXFDescriptor *)*ctx)->mastering); + av_freep(&((MXFDescriptor *)*ctx)->coll); break; case MultipleDescriptor: av_freep(&((MXFDescriptor *)*ctx)->sub_descriptors_refs); @@ -359,8 +378,9 @@ static void mxf_free_metadataset(MXFMetadataSet **ctx, int freectx) default: break; } - if (freectx) - av_freep(ctx); + if (freectx) { + av_freep(ctx); + } } static int64_t klv_decode_ber_length(AVIOContext *pb) @@ -426,19 +446,19 @@ static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv, int body_sid return s->nb_streams == 1 && s->streams[0]->priv_data ? 0 : -1; } -static int find_body_sid_by_offset(MXFContext *mxf, int64_t offset) +static int find_body_sid_by_absolute_offset(MXFContext *mxf, int64_t offset) { // we look for partition where the offset is placed int a, b, m; - int64_t this_partition; + int64_t pack_ofs; a = -1; b = mxf->partitions_count; while (b - a > 1) { - m = (a + b) >> 1; - this_partition = mxf->partitions[m].this_partition; - if (this_partition <= offset) + m = (a + b) >> 1; + pack_ofs = mxf->partitions[m].pack_ofs; + if (pack_ofs <= offset) a = m; else b = m; @@ -449,6 +469,81 @@ static int find_body_sid_by_offset(MXFContext *mxf, int64_t offset) return mxf->partitions[a].body_sid; } +static int mxf_get_eia608_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt, int64_t length) +{ + int count = avio_rb16(s->pb); + int cdp_identifier, cdp_length, cdp_footer_id, ccdata_id, cc_count; + int line_num, sample_coding, sample_count; + int did, sdid, data_length; + int i, ret; + + if (count != 1) + av_log(s, AV_LOG_WARNING, "unsupported multiple ANC packets (%d) per KLV packet\n", count); + + for (i = 0; i < count; i++) { + if (length < 6) { + av_log(s, AV_LOG_ERROR, "error reading s436m packet %"PRId64"\n", length); + return AVERROR_INVALIDDATA; + } + line_num = avio_rb16(s->pb); + avio_r8(s->pb); // wrapping type + sample_coding = avio_r8(s->pb); + sample_count = avio_rb16(s->pb); + length -= 6 + 8 + sample_count; + if (line_num != 9 && line_num != 11) + continue; + if (sample_coding == 7 || sample_coding == 8 || sample_coding == 9) { + av_log(s, AV_LOG_WARNING, "unsupported s436m 10 bit sample coding\n"); + continue; + } + if (length < 0) + return AVERROR_INVALIDDATA; + + avio_rb32(s->pb); // array count + avio_rb32(s->pb); // array elem size + did = avio_r8(s->pb); + sdid = avio_r8(s->pb); + data_length = avio_r8(s->pb); + if (did != 0x61 || sdid != 1) { + av_log(s, AV_LOG_WARNING, "unsupported did or sdid: %x %x\n", did, sdid); + continue; + } + cdp_identifier = avio_rb16(s->pb); // cdp id + if (cdp_identifier != 0x9669) { + av_log(s, AV_LOG_ERROR, "wrong cdp identifier %x\n", cdp_identifier); + return AVERROR_INVALIDDATA; + } + cdp_length = avio_r8(s->pb); + avio_r8(s->pb); // cdp_frame_rate + avio_r8(s->pb); // cdp_flags + avio_rb16(s->pb); // cdp_hdr_sequence_cntr + ccdata_id = avio_r8(s->pb); // ccdata_id + if (ccdata_id != 0x72) { + av_log(s, AV_LOG_ERROR, "wrong cdp data section %x\n", ccdata_id); + return AVERROR_INVALIDDATA; + } + cc_count = avio_r8(s->pb) & 0x1f; + ret = av_get_packet(s->pb, pkt, cc_count * 3); + if (ret < 0) + return ret; + if (cdp_length - 9 - 4 < cc_count * 3) { + av_log(s, AV_LOG_ERROR, "wrong cdp size %d cc count %d\n", cdp_length, cc_count); + return AVERROR_INVALIDDATA; + } + avio_skip(s->pb, data_length - 9 - 4 - cc_count * 3); + cdp_footer_id = avio_r8(s->pb); + if (cdp_footer_id != 0x74) { + av_log(s, AV_LOG_ERROR, "wrong cdp footer section %x\n", cdp_footer_id); + return AVERROR_INVALIDDATA; + } + avio_rb16(s->pb); // cdp_ftr_sequence_cntr + avio_r8(s->pb); // packet_checksum + break; + } + + return 0; +} + /* XXX: use AVBitStreamFilter */ static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length) { @@ -512,7 +607,7 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv if (!IS_KLV_KEY(klv, mxf_essence_element_key)) return AVERROR_INVALIDDATA; - body_sid = find_body_sid_by_offset(mxf, klv->offset); + body_sid = find_body_sid_by_absolute_offset(mxf, klv->offset); index = mxf_get_stream_index(s, klv, body_sid); if (index < 0) return AVERROR_INVALIDDATA; @@ -576,6 +671,7 @@ static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, U static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset) { MXFContext *mxf = arg; + AVFormatContext *s = mxf->fc; MXFPartition *partition, *tmp_part; UID op; uint64_t footer_partition; @@ -640,6 +736,12 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size } nb_essence_containers = avio_rb32(pb); + if (partition->type == Header) { + char str[36]; + snprintf(str, sizeof(str), "%08x.%08x.%08x.%08x", AV_RB32(&op[0]), AV_RB32(&op[4]), AV_RB32(&op[8]), AV_RB32(&op[12])); + av_dict_set(&s->metadata, "operational_pattern_ul", str, 0); + } + if (partition->this_partition && partition->previous_partition == partition->this_partition) { av_log(mxf->fc, AV_LOG_ERROR, @@ -736,15 +838,17 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size return 0; } -static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set) +static int mxf_add_metadata_set(MXFContext *mxf, MXFMetadataSet **metadata_set) { MXFMetadataSet **tmp; tmp = av_realloc_array(mxf->metadata_sets, mxf->metadata_sets_count + 1, sizeof(*mxf->metadata_sets)); - if (!tmp) + if (!tmp) { + mxf_free_metadataset(metadata_set, 1); return AVERROR(ENOMEM); + } mxf->metadata_sets = tmp; - mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set; + mxf->metadata_sets[mxf->metadata_sets_count] = *metadata_set; mxf->metadata_sets_count++; return 0; } @@ -762,6 +866,7 @@ static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, i static int mxf_read_strong_ref_array(AVIOContext *pb, UID **refs, int *count) { *count = avio_rb32(pb); + av_free(*refs); *refs = av_calloc(*count, sizeof(UID)); if (!*refs) { *count = 0; @@ -781,6 +886,7 @@ static inline int mxf_read_utf16_string(AVIOContext *pb, int size, char** str, i return AVERROR(EINVAL); buf_size = size + size / 2 + 1; + av_free(*str); *str = av_malloc(buf_size); if (!*str) return AVERROR(ENOMEM); @@ -814,10 +920,8 @@ static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int siz 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; @@ -1113,15 +1217,33 @@ static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int descriptor->aspect_ratio.num = avio_rb32(pb); descriptor->aspect_ratio.den = avio_rb32(pb); break; + case 0x3210: + avio_read(pb, descriptor->color_trc_ul, 16); + break; case 0x3212: descriptor->field_dominance = avio_r8(pb); break; + case 0x3219: + avio_read(pb, descriptor->color_primaries_ul, 16); + break; + case 0x321A: + avio_read(pb, descriptor->color_space_ul, 16); + break; case 0x3301: descriptor->component_depth = avio_rb32(pb); break; case 0x3302: descriptor->horiz_subsampling = avio_rb32(pb); break; + case 0x3304: + descriptor->black_ref_level = avio_rb32(pb); + break; + case 0x3305: + descriptor->white_ref_level = avio_rb32(pb); + break; + case 0x3306: + descriptor->color_range = avio_rb32(pb); + break; case 0x3308: descriptor->vert_subsampling = avio_rb32(pb); break; @@ -1160,6 +1282,55 @@ static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int rsiz == FF_PROFILE_JPEG2000_DCINEMA_4K) descriptor->pix_fmt = AV_PIX_FMT_XYZ12; } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_prefix)) { + if (!descriptor->mastering) { + descriptor->mastering = av_mastering_display_metadata_alloc(); + if (!descriptor->mastering) + return AVERROR(ENOMEM); + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[0].uid)) { + for (int i = 0; i < 3; i++) { + /* Order: large x, large y, other (i.e. RGB) */ + descriptor->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + descriptor->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + } + /* Check we have seen mxf_mastering_display_white_point_chromaticity */ + if (descriptor->mastering->white_point[0].den != 0) + descriptor->mastering->has_primaries = 1; + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[1].uid)) { + descriptor->mastering->white_point[0] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + descriptor->mastering->white_point[1] = av_make_q(avio_rb16(pb), FF_MXF_MASTERING_CHROMA_DEN); + /* Check we have seen mxf_mastering_display_primaries */ + if (descriptor->mastering->display_primaries[0][0].den != 0) + descriptor->mastering->has_primaries = 1; + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[2].uid)) { + descriptor->mastering->max_luminance = av_make_q(avio_rb32(pb), FF_MXF_MASTERING_LUMA_DEN); + /* Check we have seen mxf_mastering_display_minimum_luminance */ + if (descriptor->mastering->min_luminance.den != 0) + descriptor->mastering->has_luminance = 1; + } + if (IS_KLV_KEY(uid, ff_mxf_mastering_display_local_tags[3].uid)) { + descriptor->mastering->min_luminance = av_make_q(avio_rb32(pb), FF_MXF_MASTERING_LUMA_DEN); + /* Check we have seen mxf_mastering_display_maximum_luminance */ + if (descriptor->mastering->max_luminance.den != 0) + descriptor->mastering->has_luminance = 1; + } + } + if (IS_KLV_KEY(uid, mxf_apple_coll_prefix)) { + if (!descriptor->coll) { + descriptor->coll = av_content_light_metadata_alloc(&descriptor->coll_size); + if (!descriptor->coll) + return AVERROR(ENOMEM); + } + if (IS_KLV_KEY(uid, mxf_apple_coll_max_cll)) { + descriptor->coll->MaxCLL = avio_rb16(pb); + } + if (IS_KLV_KEY(uid, mxf_apple_coll_max_fall)) { + descriptor->coll->MaxFALL = avio_rb16(pb); + } + } break; } return 0; @@ -1250,6 +1421,7 @@ static const MXFCodecUL mxf_picture_essence_container_uls[] = { { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14, AV_CODEC_ID_RAWVIDEO, NULL, 15, RawVWrap }, /* uncompressed picture */ { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x01,0x01 }, 15, AV_CODEC_ID_HQ_HQA }, { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x02,0x01 }, 15, AV_CODEC_ID_HQX }, + { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x15,0x00,0x04,0x02,0x10,0x00,0x01 }, 16, AV_CODEC_ID_HEVC, NULL, 15 }, /* Canon XF-HEVC */ { { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0xff,0x4b,0x46,0x41,0x41,0x00,0x0d,0x4d,0x4f }, 14, AV_CODEC_ID_RAWVIDEO }, /* Legacy ?? Uncompressed Picture */ { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE }, }; @@ -1459,10 +1631,7 @@ static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t of */ static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid) { - int x; - int64_t ret = 0; - - for (x = 0; x < mxf->partitions_count; x++) { + for (int x = mxf->partitions_count - 1; x >= 0; x--) { MXFPartition *p = &mxf->partitions[x]; if (p->body_sid != body_sid) @@ -1471,10 +1640,10 @@ static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid) if (!p->essence_length) return 0; - ret = p->essence_offset + p->essence_length; + return p->essence_offset + p->essence_length; } - return ret; + return 0; } /* EditUnit -> absolute offset */ @@ -1934,7 +2103,7 @@ static MXFStructuralComponent* mxf_resolve_sourceclip(MXFContext *mxf, UID *stro static int mxf_parse_package_comments(MXFContext *mxf, AVDictionary **pm, MXFPackage *package) { MXFTaggedValue *tag; - int size, i; + int i; char *key = NULL; for (i = 0; i < package->comment_count; i++) { @@ -1942,12 +2111,10 @@ static int mxf_parse_package_comments(MXFContext *mxf, AVDictionary **pm, MXFPac if (!tag || !tag->name || !tag->value) continue; - size = strlen(tag->name) + 8 + 1; - key = av_mallocz(size); + key = av_asprintf("comment_%s", tag->name); if (!key) return AVERROR(ENOMEM); - snprintf(key, size, "comment_%s", tag->name); av_dict_set(pm, key, tag->value, AV_DICT_DONT_STRDUP_KEY); } return 0; @@ -1992,15 +2159,15 @@ static int mxf_parse_physical_source_package(MXFContext *mxf, MXFTrack *source_t continue; } - if (physical_track->edit_rate.num <= 0 || - physical_track->edit_rate.den <= 0) { - av_log(mxf->fc, AV_LOG_WARNING, - "Invalid edit rate (%d/%d) found on structural" - " component #%d, defaulting to 25/1\n", - physical_track->edit_rate.num, - physical_track->edit_rate.den, i); - physical_track->edit_rate = (AVRational){25, 1}; - } + if (physical_track->edit_rate.num <= 0 || + physical_track->edit_rate.den <= 0) { + av_log(mxf->fc, AV_LOG_WARNING, + "Invalid edit rate (%d/%d) found on structural" + " component #%d, defaulting to 25/1\n", + physical_track->edit_rate.num, + physical_track->edit_rate.den, i); + physical_track->edit_rate = (AVRational){25, 1}; + } for (k = 0; k < physical_track->sequence->structural_components_count; k++) { if (!(mxf_tc = mxf_resolve_timecode_component(mxf, &physical_track->sequence->structural_components_refs[k]))) @@ -2061,6 +2228,30 @@ static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track) return 0; } +static enum AVColorRange mxf_get_color_range(MXFContext *mxf, MXFDescriptor *descriptor) +{ + if (descriptor->black_ref_level || descriptor->white_ref_level || descriptor->color_range) { + /* CDCI range metadata */ + if (!descriptor->component_depth) + return AVCOL_RANGE_UNSPECIFIED; + if (descriptor->black_ref_level == 0 && + descriptor->white_ref_level == ((1<component_depth) - 1) && + (descriptor->color_range == (1<component_depth) || + descriptor->color_range == ((1<component_depth) - 1))) + return AVCOL_RANGE_JPEG; + if (descriptor->component_depth >= 8 && + descriptor->black_ref_level == (1 <<(descriptor->component_depth - 4)) && + descriptor->white_ref_level == (235<<(descriptor->component_depth - 8)) && + descriptor->color_range == ((14<<(descriptor->component_depth - 4)) + 1)) + return AVCOL_RANGE_MPEG; + avpriv_request_sample(mxf->fc, "Unrecognized CDCI color range (color diff range %d, b %d, w %d, depth %d)", + descriptor->color_range, descriptor->black_ref_level, + descriptor->white_ref_level, descriptor->component_depth); + } + + return AVCOL_RANGE_UNSPECIFIED; +} + static int mxf_parse_structural_metadata(MXFContext *mxf) { MXFPackage *material_package = NULL; @@ -2354,6 +2545,18 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) default: av_log(mxf->fc, AV_LOG_INFO, "Unknown frame layout type: %d\n", descriptor->frame_layout); } + + if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) { + switch (descriptor->essence_codec_ul[14]) { + case 1: st->codecpar->codec_tag = MKTAG('a','p','c','o'); break; + case 2: st->codecpar->codec_tag = MKTAG('a','p','c','s'); break; + case 3: st->codecpar->codec_tag = MKTAG('a','p','c','n'); break; + case 4: st->codecpar->codec_tag = MKTAG('a','p','c','h'); break; + case 5: st->codecpar->codec_tag = MKTAG('a','p','4','h'); break; + case 6: st->codecpar->codec_tag = MKTAG('a','p','4','x'); break; + } + } + if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) { st->codecpar->format = descriptor->pix_fmt; if (st->codecpar->format == AV_PIX_FMT_NONE) { @@ -2383,7 +2586,27 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) av_dict_set_int(&st->metadata, "source_track_origin", source_track->sequence->origin, 0); } if (descriptor->aspect_ratio.num && descriptor->aspect_ratio.den) - st->display_aspect_ratio = descriptor->aspect_ratio; + st->internal->display_aspect_ratio = descriptor->aspect_ratio; + st->codecpar->color_range = mxf_get_color_range(mxf, descriptor); + st->codecpar->color_primaries = mxf_get_codec_ul(ff_mxf_color_primaries_uls, &descriptor->color_primaries_ul)->id; + st->codecpar->color_trc = mxf_get_codec_ul(ff_mxf_color_trc_uls, &descriptor->color_trc_ul)->id; + st->codecpar->color_space = mxf_get_codec_ul(ff_mxf_color_space_uls, &descriptor->color_space_ul)->id; + if (descriptor->mastering) { + ret = av_stream_add_side_data(st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, + (uint8_t *)descriptor->mastering, + sizeof(*descriptor->mastering)); + if (ret < 0) + goto fail_and_free; + descriptor->mastering = NULL; + } + if (descriptor->coll) { + ret = av_stream_add_side_data(st, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, + (uint8_t *)descriptor->coll, + descriptor->coll_size); + if (ret < 0) + goto fail_and_free; + descriptor->coll = NULL; + } } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul); /* Only overwrite existing codec ID if it is unset or A-law, which is the default according to SMPTE RP 224. */ @@ -2433,6 +2656,11 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) st->codecpar->codec_type = type; if (container_ul->desc) av_dict_set(&st->metadata, "data_type", container_ul->desc, 0); + if (mxf->eia608_extract && + !strcmp(container_ul->desc, "vbi_vanc_smpte_436M")) { + st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; + st->codecpar->codec_id = AV_CODEC_ID_EIA_608; + } } if (descriptor->extradata) { if (!ff_alloc_extradata(st->codecpar, descriptor->extradata_size)) { @@ -2453,6 +2681,24 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) } } + for (int i = 0; i < mxf->fc->nb_streams; i++) { + MXFTrack *track1 = mxf->fc->streams[i]->priv_data; + if (track1 && track1->body_sid) { + for (int j = i + 1; j < mxf->fc->nb_streams; j++) { + MXFTrack *track2 = mxf->fc->streams[j]->priv_data; + if (track2 && track1->body_sid == track2->body_sid && track1->wrapping != track2->wrapping) { + if (track1->wrapping == UnknownWrapped) + track1->wrapping = track2->wrapping; + else if (track2->wrapping == UnknownWrapped) + track2->wrapping = track1->wrapping; + else + av_log(mxf->fc, AV_LOG_ERROR, "stream %d and stream %d have the same BodySID (%d) " + "with different wrapping\n", i, j, track1->body_sid); + } + } + } + } + ret = 0; fail_and_free: return ret; @@ -2461,23 +2707,24 @@ fail_and_free: static int64_t mxf_timestamp_to_int64(uint64_t timestamp) { struct tm time = { 0 }; + int msecs; 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); + msecs = (timestamp & 0xFF) * 4; - /* msvcrt versions of strftime calls the invalid parameter handler - * (aborting the process if one isn't set) if the parameters are out - * of range. */ + /* Clip values for legacy reasons. Maybe we should return error instead? */ 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); + msecs = av_clip(msecs, 0, 999); - return (int64_t)av_timegm(&time) * 1000000; + return (int64_t)av_timegm(&time) * 1000000 + msecs * 1000; } #define SET_STR_METADATA(pb, name, str) do { \ @@ -2495,7 +2742,7 @@ static int64_t mxf_timestamp_to_int64(uint64_t timestamp) #define SET_TS_METADATA(pb, name, var, str) do { \ var = avio_rb64(pb); \ - if ((ret = avpriv_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var)) < 0)) \ + if (var && (ret = avpriv_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var))) < 0) \ return ret; \ } while (0) @@ -2593,6 +2840,7 @@ static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = { static int mxf_metadataset_init(MXFMetadataSet *ctx, enum MXFMetadataSetType type) { + ctx->type = type; switch (type){ case MultipleDescriptor: case Descriptor: @@ -2613,7 +2861,8 @@ static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadF if (!ctx) return AVERROR(ENOMEM); - mxf_metadataset_init(ctx, type); + if (ctx_size) + mxf_metadataset_init(ctx, type); while (avio_tell(pb) + 4 < klv_end && !avio_feof(pb)) { int ret; int tag = avio_rb16(pb); @@ -2649,7 +2898,6 @@ static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadF * it extending past the end of the KLV though (zzuf5.mxf). */ if (avio_tell(pb) > klv_end) { if (ctx_size) { - ctx->type = type; mxf_free_metadataset(&ctx, 1); } @@ -2660,8 +2908,7 @@ static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadF } else if (avio_tell(pb) <= next) /* only seek forward, else this can loop for a long time */ avio_seek(pb, next, SEEK_SET); } - if (ctx_size) ctx->type = type; - return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0; + return ctx_size ? mxf_add_metadata_set(mxf, &ctx) : 0; } /** @@ -2964,10 +3211,8 @@ static int mxf_handle_missing_index_segment(MXFContext *mxf, AVStream *st) if (!(segment = av_mallocz(sizeof(*segment)))) return AVERROR(ENOMEM); - if ((ret = mxf_add_metadata_set(mxf, segment))) { - mxf_free_metadataset((MXFMetadataSet**)&segment, 1); + if ((ret = mxf_add_metadata_set(mxf, (MXFMetadataSet**)&segment))) return ret; - } /* Make sure we have nonzero unique index_sid, body_sid will be ok, because * using the same SID for index is forbidden in MXF. */ @@ -3019,9 +3264,12 @@ static void mxf_read_random_index_pack(AVFormatContext *s) 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) + !IS_KLV_KEY(klv.key, mxf_random_index_pack_key)) + goto end; + if (klv.next_klv != file_size || klv.length <= 4 || (klv.length - 4) % 12) { + av_log(s, AV_LOG_WARNING, "Invalid RIP KLV length\n"); goto end; + } avio_skip(s->pb, klv.length - 12); mxf->footer_partition = avio_rb64(s->pb); @@ -3047,6 +3295,7 @@ static int mxf_read_header(AVFormatContext *s) 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"); + //goto fail should not be needed as no metadata sets will have been parsed yet return AVERROR_INVALIDDATA; } avio_seek(s->pb, -14, SEEK_CUR); @@ -3077,7 +3326,8 @@ static int mxf_read_header(AVFormatContext *s) if (!mxf->current_partition) { av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to first PartitionPack\n"); - return AVERROR_INVALIDDATA; + ret = AVERROR_INVALIDDATA; + goto fail; } if (!mxf->current_partition->first_essence_klv.offset) @@ -3183,20 +3433,17 @@ static int mxf_get_next_track_edit_unit(MXFContext *mxf, MXFTrack *track, int64_ static int64_t mxf_compute_sample_count(MXFContext *mxf, AVStream *st, int64_t edit_unit) { - int i, total = 0, size = 0; MXFTrack *track = st->priv_data; AVRational time_base = av_inv_q(track->edit_rate); AVRational sample_rate = av_inv_q(st->time_base); - const MXFSamplesPerFrame *spf = NULL; - int64_t sample_count; // For non-audio sample_count equals current edit unit if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) return edit_unit; - if ((sample_rate.num / sample_rate.den) == 48000) - spf = ff_mxf_get_samples_per_frame(mxf->fc, time_base); - if (!spf) { + if ((sample_rate.num / sample_rate.den) == 48000) { + return av_rescale_q(edit_unit, sample_rate, track->edit_rate); + } else { int remainder = (sample_rate.num * time_base.num) % (time_base.den * sample_rate.den); if (remainder) @@ -3207,20 +3454,6 @@ static int64_t mxf_compute_sample_count(MXFContext *mxf, AVStream *st, sample_rate.num, sample_rate.den); return av_rescale_q(edit_unit, sample_rate, track->edit_rate); } - - while (spf->samples_per_frame[size]) { - total += spf->samples_per_frame[size]; - size++; - } - - av_assert2(size); - - sample_count = (edit_unit / size) * (uint64_t)total; - for (i = 0; i < edit_unit % size; i++) { - sample_count += spf->samples_per_frame[i]; - } - - return sample_count; } /** @@ -3311,6 +3544,8 @@ static int mxf_set_pts(MXFContext *mxf, AVStream *st, AVPacket *pkt) if (ret < 0) return ret; } else if (track) { + pkt->dts = pkt->pts = track->sample_count; + pkt->duration = 1; track->sample_count++; } return 0; @@ -3350,7 +3585,7 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) if (IS_KLV_KEY(klv.key, mxf_essence_element_key) || IS_KLV_KEY(klv.key, mxf_canopus_essence_element_key) || IS_KLV_KEY(klv.key, mxf_avid_essence_element_key)) { - int body_sid = find_body_sid_by_offset(mxf, klv.offset); + int body_sid = find_body_sid_by_absolute_offset(mxf, klv.offset); int index = mxf_get_stream_index(s, &klv, body_sid); int64_t next_ofs; AVStream *st; @@ -3384,8 +3619,8 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) } else { if ((size = next_ofs - pos) <= 0) { av_log(s, AV_LOG_ERROR, "bad size: %"PRId64"\n", size); - ret = AVERROR_INVALIDDATA; - goto skip; + mxf->current_klv_data = (KLVPacket){{0}}; + return AVERROR_INVALIDDATA; } // We must not overread, because the next edit unit might be in another KLV if (size > max_data_size) @@ -3407,6 +3642,13 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) mxf->current_klv_data = (KLVPacket){{0}}; return ret; } + } else if (mxf->eia608_extract && + s->streams[index]->codecpar->codec_id == AV_CODEC_ID_EIA_608) { + ret = mxf_get_eia608_packet(s, s->streams[index], pkt, klv.length); + if (ret < 0) { + mxf->current_klv_data = (KLVPacket){{0}}; + return ret; + } } else { ret = av_get_packet(s->pb, pkt, klv.length); if (ret < 0) { @@ -3450,6 +3692,7 @@ static int mxf_read_close(AVFormatContext *s) for (i = 0; i < mxf->metadata_sets_count; i++) { mxf_free_metadataset(mxf->metadata_sets + i, 1); } + mxf->metadata_sets_count = 0; av_freep(&mxf->partitions); av_freep(&mxf->metadata_sets); av_freep(&mxf->aesc); @@ -3468,7 +3711,7 @@ static int mxf_read_close(AVFormatContext *s) return 0; } -static int mxf_probe(AVProbeData *p) { +static int mxf_probe(const AVProbeData *p) { const uint8_t *bufp = p->buf; const uint8_t *end = p->buf + p->buf_size; @@ -3484,7 +3727,7 @@ static int mxf_probe(AVProbeData *p) { AV_RN32(bufp+ 4) == AV_RN32(mxf_header_partition_pack_key+ 4) && AV_RN32(bufp+ 8) == AV_RN32(mxf_header_partition_pack_key+ 8) && AV_RN16(bufp+12) == AV_RN16(mxf_header_partition_pack_key+12)) - return AVPROBE_SCORE_MAX; + return bufp == p->buf ? AVPROBE_SCORE_MAX : AVPROBE_SCORE_MAX - 1; bufp ++; } else bufp += 10; @@ -3605,6 +3848,21 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti return 0; } +static const AVOption options[] = { + { "eia608_extract", "extract eia 608 captions from s436m track", + offsetof(MXFContext, eia608_extract), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, + AV_OPT_FLAG_DECODING_PARAM }, + { NULL }, +}; + +static const AVClass demuxer_class = { + .class_name = "mxf", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, + .category = AV_CLASS_CATEGORY_DEMUXER, +}; + AVInputFormat ff_mxf_demuxer = { .name = "mxf", .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"), @@ -3615,4 +3873,5 @@ AVInputFormat ff_mxf_demuxer = { .read_packet = mxf_read_packet, .read_close = mxf_read_close, .read_seek = mxf_read_seek, + .priv_class = &demuxer_class, };