diff options
Diffstat (limited to 'ffmpeg/libavformat/mxfdec.c')
| -rw-r--r-- | ffmpeg/libavformat/mxfdec.c | 239 |
1 files changed, 203 insertions, 36 deletions
diff --git a/ffmpeg/libavformat/mxfdec.c b/ffmpeg/libavformat/mxfdec.c index 4580e1b..61c0cb2 100644 --- a/ffmpeg/libavformat/mxfdec.c +++ b/ffmpeg/libavformat/mxfdec.c @@ -43,12 +43,13 @@ * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1 */ -//#define DEBUG +#include <stdint.h> #include "libavutil/aes.h" #include "libavutil/avassert.h" #include "libavutil/mathematics.h" #include "libavcodec/bytestream.h" +#include "libavutil/intreadwrite.h" #include "libavutil/timecode.h" #include "avformat.h" #include "internal.h" @@ -215,6 +216,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; @@ -256,6 +258,7 @@ static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x 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))) @@ -435,10 +438,7 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size uint64_t footer_partition; uint32_t nb_essence_containers; - if (mxf->partitions_count+1 >= UINT_MAX / sizeof(*mxf->partitions)) - return AVERROR(ENOMEM); - - tmp_part = av_realloc(mxf->partitions, (mxf->partitions_count + 1) * sizeof(*mxf->partitions)); + tmp_part = av_realloc_array(mxf->partitions, mxf->partitions_count + 1, sizeof(*mxf->partitions)); if (!tmp_part) return AVERROR(ENOMEM); mxf->partitions = tmp_part; @@ -565,9 +565,8 @@ static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set) { MXFMetadataSet **tmp; - if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets)) - return AVERROR(ENOMEM); - tmp = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets)); + + tmp = av_realloc_array(mxf->metadata_sets, mxf->metadata_sets_count + 1, sizeof(*mxf->metadata_sets)); if (!tmp) return AVERROR(ENOMEM); mxf->metadata_sets = tmp; @@ -941,6 +940,7 @@ static const MXFCodecUL mxf_intra_only_essence_container_uls[] = { /* 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 */ { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE }, }; @@ -1518,6 +1518,7 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) av_log(mxf->fc, AV_LOG_VERBOSE, "."); } av_log(mxf->fc, AV_LOG_VERBOSE, "\n"); + if (st->codec->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); @@ -1574,11 +1575,13 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) st->codec->bits_per_coded_sample = descriptor->bits_per_sample; if (descriptor->sample_rate.den > 0) { - avpriv_set_pts_info(st, 64, descriptor->sample_rate.den, descriptor->sample_rate.num); st->codec->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) found for stream #%d, time base forced to 1/48000\n", - descriptor->sample_rate.num, descriptor->sample_rate.den, st->index); + av_log(mxf->fc, AV_LOG_WARNING, "invalid sample rate (%d/%d) " + "found for stream #%d, time base forced to 1/48000\n", + descriptor->sample_rate.num, descriptor->sample_rate.den, + st->index); avpriv_set_pts_info(st, 64, 1, 48000); } @@ -1602,11 +1605,13 @@ static int mxf_parse_structural_metadata(MXFContext *mxf) } } if (descriptor->extradata) { - st->codec->extradata = av_mallocz(descriptor->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); - if (st->codec->extradata) + if (!ff_alloc_extradata(st->codec, descriptor->extradata_size)) { memcpy(st->codec->extradata, descriptor->extradata, descriptor->extradata_size); - } else if(st->codec->codec_id == AV_CODEC_ID_H264) { - ff_generate_avci_extradata(st); + } + } else if (st->codec->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) { /* TODO: decode timestamps */ @@ -1619,6 +1624,124 @@ 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); + + /* ensure month/day are valid */ + time.tm_mon = FFMAX(time.tm_mon, 0); + time.tm_mday = FFMAX(time.tm_mday, 1); + + *str = av_mallocz(32); + if (!*str) + return AVERROR(ENOMEM); + strftime(*str, 32, "%Y-%m-%d %H:%M:%S", &time); + + 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 }, @@ -1631,6 +1754,7 @@ static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = { { { 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 }, @@ -1736,31 +1860,33 @@ 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"); - return 0; - } + } else if (mxf->footer_partition || mxf->last_partition){ + uint64_t offset; - av_dlog(mxf->fc, "seeking to footer\n"); + offset = mxf->footer_partition ? mxf->footer_partition : mxf->last_partition; + + av_dlog(mxf->fc, "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"); + 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; } mxf->current_partition = NULL; mxf->parsing_backward = 1; + } else { + av_dlog(mxf->fc, "can't find last partition\n"); + return 0; } return 1; @@ -1852,6 +1978,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) + 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; @@ -1870,6 +2024,8 @@ static int mxf_read_header(AVFormatContext *s) mxf->fc = s; mxf->run_in = avio_tell(s->pb); + mxf_read_random_index_pack(s); + while (!url_feof(s->pb)) { const MXFMetadataReadTableEntry *metadata; @@ -2080,7 +2236,9 @@ static int mxf_set_audio_pts(MXFContext *mxf, AVCodecContext *codec, AVPacket *p { MXFTrack *track = mxf->fc->streams[pkt->stream_index]->priv_data; pkt->pts = track->sample_count; - if (codec->channels <= 0 || av_get_bits_per_sample(codec->codec_id) <= 0) + if ( codec->channels <= 0 + || av_get_bits_per_sample(codec->codec_id) <= 0 + || codec->channels * (int64_t)av_get_bits_per_sample(codec->codec_id) < 8) return AVERROR(EINVAL); track->sample_count += pkt->size / (codec->channels * (int64_t)av_get_bits_per_sample(codec->codec_id) / 8); return 0; @@ -2091,10 +2249,8 @@ static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt) KLVPacket klv; MXFContext *mxf = s->priv_data; - while (!url_feof(s->pb)) { + while (klv_read_packet(&klv, s->pb) == 0) { int ret; - if (klv_read_packet(&klv, s->pb) < 0) - return -1; PRINT_KEY(s, "read packet", klv.key); av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) { @@ -2179,7 +2335,7 @@ static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt) skip: avio_skip(s->pb, klv.length); } - return AVERROR_EOF; + return url_feof(s->pb) ? AVERROR_EOF : -1; } static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) @@ -2306,10 +2462,19 @@ static int mxf_probe(AVProbeData *p) { /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */ end -= sizeof(mxf_header_partition_pack_key); - for (; bufp < end; bufp++) { - if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key)) - return AVPROBE_SCORE_MAX; + + for (; bufp < end;) { + if (!((bufp[13] - 1) & 0xF2)){ + if (AV_RN32(bufp ) == AV_RN32(mxf_header_partition_pack_key ) && + 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; + bufp ++; + } else + bufp += 10; } + return 0; } @@ -2322,6 +2487,7 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti MXFContext* mxf = s->priv_data; int64_t seekpos; int i, ret; + int64_t ret64; MXFIndexTable *t; MXFTrack *source_track = st->priv_data; @@ -2336,9 +2502,10 @@ static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti sample_time = 0; seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den); - if ((ret = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET)) < 0) - return ret; + if ((ret64 = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET)) < 0) + return ret64; ff_update_cur_dts(s, st, sample_time); + mxf->current_edit_unit = sample_time; } else { t = &mxf->index_tables[0]; |
