-typedef struct MXFCodecUL {
- UID uid;
- enum CodecID id;
- enum MXFWrappingScheme wrapping;
-} MXFCodecUL;
-
-typedef struct MXFMetadataReadTableEntry {
- const UID key;
- int (*read)(MXFContext *mxf, KLVPacket *klv);
-} MXFMetadataReadTableEntry;
-
-/* partial keys to match */
-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 };
-
-#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
-
-#define PRINT_KEY(x) dprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", \
- (x)[0], (x)[1], (x)[2], (x)[3], (x)[4], (x)[5], (x)[6], (x)[7], (x)[8], (x)[9], (x)[10], (x)[11], (x)[12], (x)[13], (x)[14], (x)[15])
-
-static int64_t klv_decode_ber_length(ByteIOContext *pb)
-{
- int64_t size = 0;
- uint8_t length = get_byte(pb);
- int type = length >> 7;
-
- if (type) { /* long form */
- int bytes_num = length & 0x7f;
- /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
- if (bytes_num > 8)
- return -1;
- while (bytes_num--)
- size = size << 8 | get_byte(pb);
- } else {
- size = length & 0x7f;
- }
- return size;
-}
-
-static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
-{
- klv->offset = url_ftell(pb);
- get_buffer(pb, klv->key, 16);
- klv->length = klv_decode_ber_length(pb);
- return klv->length == -1 ? -1 : 0;
-}
-
-static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
-{
- int i;
-
- for (i = 0; i < s->nb_streams; i++) {
- MXFTrack *track = s->streams[i]->priv_data;
- /* SMPTE 379M 7.3 */
- if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
- return i;
- }
- return -1;
-}
-
-static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
-{
- KLVPacket klv;
-
- while (!url_feof(&s->pb)) {
- if (klv_read_packet(&klv, &s->pb) < 0) {
- av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
- return -1;
- }
-#ifdef DEBUG
- PRINT_KEY(klv.key);
-#endif
- if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
- av_get_packet(&s->pb, pkt, klv.length);
- pkt->stream_index = mxf_get_stream_index(s, &klv);
- return pkt->stream_index == -1 ? -1 : 0;
- } else
- url_fskip(&s->pb, klv.length);
- }
- return AVERROR_IO;
-}
-
-static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
-{
- mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
- mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
- mxf->metadata_sets_count++;
- return 0;
-}
-
-static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
-
- switch (tag) {
- case 0x3B03:
- get_buffer(pb, mxf->content_storage_uid, 16);
- break;
- case 0x3B09:
- get_buffer(pb, mxf->operational_pattern_ul, 16);
- break;
- case 0x3B0A:
- mxf->essence_containers_uls_count = get_be32(pb);
- if (mxf->essence_containers_uls_count >= UINT_MAX / sizeof(UID))
- return -1;
- mxf->essence_containers_uls = av_malloc(mxf->essence_containers_uls_count * sizeof(UID));
- url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- return 0;
-}
-
-static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
-
- dprintf("tag 0x%04X, size %d\n", tag, size);
- switch (tag) {
- case 0x1901:
- mxf->packages_count = get_be32(pb);
- if (mxf->packages_count >= UINT_MAX / sizeof(UID))
- return -1;
- mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
- url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, mxf->packages_refs, mxf->packages_count * sizeof(UID));
- break;
- case 0x1902:
- mxf->essence_container_data_sets_count = get_be32(pb);
- if (mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(UID))
- return -1;
- mxf->essence_container_data_sets_refs = av_malloc(mxf->essence_container_data_sets_count * sizeof(UID));
- url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- return 0;
-}
-
-static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- MXFStructuralComponent *source_clip = av_mallocz(sizeof(*source_clip));
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
-
- dprintf("tag 0x%04X, size %d\n", tag, size);
- switch (tag) {
- case 0x3C0A:
- get_buffer(pb, source_clip->uid, 16);
- break;
- case 0x0202:
- source_clip->duration = get_be64(pb);
- break;
- case 0x1201:
- source_clip->start_position = get_be64(pb);
- break;
- case 0x1101:
- /* UMID, only get last 16 bytes */
- url_fskip(pb, 16);
- get_buffer(pb, source_clip->source_package_uid, 16);
- break;
- case 0x1102:
- source_clip->source_track_id = get_be32(pb);
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- source_clip->type = SourceClip;
- return mxf_add_metadata_set(mxf, source_clip);
-}
-
-static int mxf_read_metadata_material_package(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- MXFPackage *package = av_mallocz(sizeof(*package));
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* KLV specified by 0x53 */
-
- switch (tag) {
- case 0x3C0A:
- get_buffer(pb, package->uid, 16);
- break;
- case 0x4403:
- package->tracks_count = get_be32(pb);
- if (package->tracks_count >= UINT_MAX / sizeof(UID))
- return -1;
- package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
- url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- package->type = MaterialPackage;
- return mxf_add_metadata_set(mxf, package);
-}
-
-static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- MXFTrack *track = av_mallocz(sizeof(*track));
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* KLV specified by 0x53 */
-
- dprintf("tag 0x%04X, size %d\n", tag, size);
- switch (tag) {
- case 0x3C0A:
- get_buffer(pb, track->uid, 16);
- break;
- case 0x4801:
- track->track_id = get_be32(pb);
- break;
- case 0x4804:
- get_buffer(pb, track->track_number, 4);
- break;
- case 0x4B01:
- track->edit_rate.den = get_be32(pb);
- track->edit_rate.num = get_be32(pb);
- break;
- case 0x4803:
- get_buffer(pb, track->sequence_ref, 16);
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- track->type = Track;
- return mxf_add_metadata_set(mxf, track);
-}
-
-static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- MXFSequence *sequence = av_mallocz(sizeof(*sequence));
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* KLV specified by 0x53 */
-
- dprintf("tag 0x%04X, size %d\n", tag, size);
- switch (tag) {
- case 0x3C0A:
- get_buffer(pb, sequence->uid, 16);
- break;
- case 0x0202:
- sequence->duration = get_be64(pb);
- break;
- case 0x0201:
- get_buffer(pb, sequence->data_definition_ul, 16);
- break;
- case 0x1001:
- sequence->structural_components_count = get_be32(pb);
- if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
- return -1;
- sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
- url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- sequence->type = Sequence;
- return mxf_add_metadata_set(mxf, sequence);
-}
-
-static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- MXFPackage *package = av_mallocz(sizeof(*package));
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* KLV specified by 0x53 */
-
- dprintf("tag 0x%04X, size %d\n", tag, size);
- switch (tag) {
- case 0x3C0A:
- get_buffer(pb, package->uid, 16);
- break;
- case 0x4403:
- package->tracks_count = get_be32(pb);
- if (package->tracks_count >= UINT_MAX / sizeof(UID))
- return -1;
- package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
- url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
- break;
- case 0x4401:
- /* UMID, only get last 16 bytes */
- url_fskip(pb, 16);
- get_buffer(pb, package->package_uid, 16);
- break;
- case 0x4701:
- get_buffer(pb, package->descriptor_ref, 16);
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- package->type = SourcePackage;
- return mxf_add_metadata_set(mxf, package);
-}
-
-static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* KLV specified by 0x53 */
-
- dprintf("tag 0x%04X, size %d\n", tag, size);
- switch (tag) {
- case 0x3C0A:
- get_buffer(pb, descriptor->uid, 16);
- break;
- case 0x3F01:
- descriptor->sub_descriptors_count = get_be32(pb);
- if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
- return -1;
- descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
- url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- descriptor->type = MultipleDescriptor;
- return mxf_add_metadata_set(mxf, descriptor);
-}
-
-static void mxf_read_metadata_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
-{
- int code;
-
- do {
- code = get_byte(pb);
- dprintf("pixel layout: code 0x%x\n", code);
- switch (code) {
- case 0x52: /* R */
- descriptor->bits_per_sample += get_byte(pb);
- break;
- case 0x47: /* G */
- descriptor->bits_per_sample += get_byte(pb);
- break;
- case 0x42: /* B */
- descriptor->bits_per_sample += get_byte(pb);
- break;
- default:
- get_byte(pb);
- }
- } while (code != 0); /* SMPTE 377M E.2.46 */
-}
-
-static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
-{
- ByteIOContext *pb = &mxf->fc->pb;
- MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
- int bytes_read = 0;
-
- while (bytes_read < klv->length) {
- int tag = get_be16(pb);
- int size = get_be16(pb); /* KLV specified by 0x53 */
-
- dprintf("tag 0x%04X, size %d\n", tag, size);
- switch (tag) {
- case 0x3C0A:
- get_buffer(pb, descriptor->uid, 16);
- break;
- case 0x3004:
- get_buffer(pb, descriptor->essence_container_ul, 16);
- break;
- case 0x3006:
- descriptor->linked_track_id = get_be32(pb);
- break;
- case 0x3201: /* PictureEssenceCoding */
- get_buffer(pb, descriptor->essence_codec_ul, 16);
- break;
- case 0x3203:
- descriptor->width = get_be32(pb);
- break;
- case 0x3202:
- descriptor->height = get_be32(pb);
- break;
- case 0x320E:
- descriptor->aspect_ratio.num = get_be32(pb);
- descriptor->aspect_ratio.den = get_be32(pb);
- break;
- case 0x3D03:
- descriptor->sample_rate.num = get_be32(pb);
- descriptor->sample_rate.den = get_be32(pb);
- break;
- case 0x3D06: /* SoundEssenceCompression */
- get_buffer(pb, descriptor->essence_codec_ul, 16);
- break;
- case 0x3D07:
- descriptor->channels = get_be32(pb);
- break;
- case 0x3D01:
- descriptor->bits_per_sample = get_be32(pb);
- break;
- case 0x3401:
- mxf_read_metadata_pixel_layout(pb, descriptor);
- break;
- default:
- url_fskip(pb, size);
- }
- bytes_read += size + 4;
- }
- descriptor->type = Descriptor;
- return mxf_add_metadata_set(mxf, descriptor);
-}
-
-/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
-static const UID picture_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 };
-static const UID sound_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 };
-
-static const MXFCodecUL mxf_codec_uls[] = {