libavformat/mxfdec.c
Go to the documentation of this file.
00001 /*
00002  * MXF demuxer.
00003  * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /*
00023  * References
00024  * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
00025  * SMPTE 377M MXF File Format Specifications
00026  * SMPTE 378M Operational Pattern 1a
00027  * SMPTE 379M MXF Generic Container
00028  * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
00029  * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container
00030  * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container
00031  *
00032  * Principle
00033  * Search for Track numbers which will identify essence element KLV packets.
00034  * Search for SourcePackage which define tracks which contains Track numbers.
00035  * Material Package contains tracks with reference to SourcePackage tracks.
00036  * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
00037  * Assign Descriptors to correct Tracks.
00038  *
00039  * Metadata reading functions read Local Tags, get InstanceUID(0x3C0A) then add MetaDataSet to MXFContext.
00040  * Metadata parsing resolves Strong References to objects.
00041  *
00042  * Simple demuxer, only OP1A supported and some files might not work at all.
00043  * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
00044  */
00045 
00046 //#define DEBUG
00047 
00048 #include "libavutil/aes.h"
00049 #include "libavutil/mathematics.h"
00050 #include "libavcodec/bytestream.h"
00051 #include "avformat.h"
00052 #include "internal.h"
00053 #include "mxf.h"
00054 
00055 typedef struct {
00056     UID uid;
00057     enum MXFMetadataSetType type;
00058     UID source_container_ul;
00059 } MXFCryptoContext;
00060 
00061 typedef struct {
00062     UID uid;
00063     enum MXFMetadataSetType type;
00064     UID source_package_uid;
00065     UID data_definition_ul;
00066     int64_t duration;
00067     int64_t start_position;
00068     int source_track_id;
00069 } MXFStructuralComponent;
00070 
00071 typedef struct {
00072     UID uid;
00073     enum MXFMetadataSetType type;
00074     UID data_definition_ul;
00075     UID *structural_components_refs;
00076     int structural_components_count;
00077     int64_t duration;
00078 } MXFSequence;
00079 
00080 typedef struct {
00081     UID uid;
00082     enum MXFMetadataSetType type;
00083     MXFSequence *sequence; /* mandatory, and only one */
00084     UID sequence_ref;
00085     int track_id;
00086     uint8_t track_number[4];
00087     AVRational edit_rate;
00088 } MXFTrack;
00089 
00090 typedef struct {
00091     UID uid;
00092     enum MXFMetadataSetType type;
00093     UID essence_container_ul;
00094     UID essence_codec_ul;
00095     AVRational sample_rate;
00096     AVRational aspect_ratio;
00097     int width;
00098     int height;
00099     int channels;
00100     int bits_per_sample;
00101     UID *sub_descriptors_refs;
00102     int sub_descriptors_count;
00103     int linked_track_id;
00104     uint8_t *extradata;
00105     int extradata_size;
00106     enum PixelFormat pix_fmt;
00107 } MXFDescriptor;
00108 
00109 typedef struct {
00110     UID uid;
00111     enum MXFMetadataSetType type;
00112 } MXFIndexTableSegment;
00113 
00114 typedef struct {
00115     UID uid;
00116     enum MXFMetadataSetType type;
00117     UID package_uid;
00118     UID *tracks_refs;
00119     int tracks_count;
00120     MXFDescriptor *descriptor; /* only one */
00121     UID descriptor_ref;
00122 } MXFPackage;
00123 
00124 typedef struct {
00125     UID uid;
00126     enum MXFMetadataSetType type;
00127 } MXFMetadataSet;
00128 
00129 typedef struct {
00130     UID *packages_refs;
00131     int packages_count;
00132     MXFMetadataSet **metadata_sets;
00133     int metadata_sets_count;
00134     AVFormatContext *fc;
00135     struct AVAES *aesc;
00136     uint8_t *local_tags;
00137     int local_tags_count;
00138 } MXFContext;
00139 
00140 enum MXFWrappingScheme {
00141     Frame,
00142     Clip,
00143 };
00144 
00145 typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid);
00146 
00147 typedef struct {
00148     const UID key;
00149     MXFMetadataReadFunc *read;
00150     int ctx_size;
00151     enum MXFMetadataSetType type;
00152 } MXFMetadataReadTableEntry;
00153 
00154 /* partial keys to match */
00155 static const uint8_t mxf_header_partition_pack_key[]       = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
00156 static const uint8_t mxf_essence_element_key[]             = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
00157 static const uint8_t mxf_klv_key[]                         = { 0x06,0x0e,0x2b,0x34 };
00158 /* complete keys to match */
00159 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 };
00160 static const uint8_t mxf_encrypted_triplet_key[]           = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
00161 static const uint8_t mxf_encrypted_essence_container[]     = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
00162 static const uint8_t mxf_sony_mpeg4_extradata[]            = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
00163 
00164 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
00165 
00166 static int64_t klv_decode_ber_length(AVIOContext *pb)
00167 {
00168     uint64_t size = avio_r8(pb);
00169     if (size & 0x80) { /* long form */
00170         int bytes_num = size & 0x7f;
00171         /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
00172         if (bytes_num > 8)
00173             return AVERROR_INVALIDDATA;
00174         size = 0;
00175         while (bytes_num--)
00176             size = size << 8 | avio_r8(pb);
00177     }
00178     return size;
00179 }
00180 
00181 static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
00182 {
00183     int i, b;
00184     for (i = 0; i < size && !pb->eof_reached; i++) {
00185         b = avio_r8(pb);
00186         if (b == key[0])
00187             i = 0;
00188         else if (b != key[i])
00189             i = -1;
00190     }
00191     return i == size;
00192 }
00193 
00194 static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
00195 {
00196     if (!mxf_read_sync(pb, mxf_klv_key, 4))
00197         return AVERROR_INVALIDDATA;
00198     klv->offset = avio_tell(pb) - 4;
00199     memcpy(klv->key, mxf_klv_key, 4);
00200     avio_read(pb, klv->key + 4, 12);
00201     klv->length = klv_decode_ber_length(pb);
00202     return klv->length == -1 ? -1 : 0;
00203 }
00204 
00205 static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
00206 {
00207     int i;
00208 
00209     for (i = 0; i < s->nb_streams; i++) {
00210         MXFTrack *track = s->streams[i]->priv_data;
00211         /* SMPTE 379M 7.3 */
00212         if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
00213             return i;
00214     }
00215     /* return 0 if only one stream, for OP Atom files with 0 as track number */
00216     return s->nb_streams == 1 ? 0 : -1;
00217 }
00218 
00219 /* XXX: use AVBitStreamFilter */
00220 static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
00221 {
00222     const uint8_t *buf_ptr, *end_ptr;
00223     uint8_t *data_ptr;
00224     int i;
00225 
00226     if (length > 61444) /* worst case PAL 1920 samples 8 channels */
00227         return AVERROR_INVALIDDATA;
00228     length = av_get_packet(pb, pkt, length);
00229     if (length < 0)
00230         return length;
00231     data_ptr = pkt->data;
00232     end_ptr = pkt->data + length;
00233     buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
00234     for (; buf_ptr + st->codec->channels*4 < end_ptr; ) {
00235         for (i = 0; i < st->codec->channels; i++) {
00236             uint32_t sample = bytestream_get_le32(&buf_ptr);
00237             if (st->codec->bits_per_coded_sample == 24)
00238                 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
00239             else
00240                 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
00241         }
00242         buf_ptr += 32 - st->codec->channels*4; // always 8 channels stored SMPTE 331M
00243     }
00244     av_shrink_packet(pkt, data_ptr - pkt->data);
00245     return 0;
00246 }
00247 
00248 static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
00249 {
00250     static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
00251     MXFContext *mxf = s->priv_data;
00252     AVIOContext *pb = s->pb;
00253     int64_t end = avio_tell(pb) + klv->length;
00254     int64_t size;
00255     uint64_t orig_size;
00256     uint64_t plaintext_size;
00257     uint8_t ivec[16];
00258     uint8_t tmpbuf[16];
00259     int index;
00260 
00261     if (!mxf->aesc && s->key && s->keylen == 16) {
00262         mxf->aesc = av_malloc(av_aes_size);
00263         if (!mxf->aesc)
00264             return AVERROR(ENOMEM);
00265         av_aes_init(mxf->aesc, s->key, 128, 1);
00266     }
00267     // crypto context
00268     avio_skip(pb, klv_decode_ber_length(pb));
00269     // plaintext offset
00270     klv_decode_ber_length(pb);
00271     plaintext_size = avio_rb64(pb);
00272     // source klv key
00273     klv_decode_ber_length(pb);
00274     avio_read(pb, klv->key, 16);
00275     if (!IS_KLV_KEY(klv, mxf_essence_element_key))
00276         return AVERROR_INVALIDDATA;
00277     index = mxf_get_stream_index(s, klv);
00278     if (index < 0)
00279         return AVERROR_INVALIDDATA;
00280     // source size
00281     klv_decode_ber_length(pb);
00282     orig_size = avio_rb64(pb);
00283     if (orig_size < plaintext_size)
00284         return AVERROR_INVALIDDATA;
00285     // enc. code
00286     size = klv_decode_ber_length(pb);
00287     if (size < 32 || size - 32 < orig_size)
00288         return AVERROR_INVALIDDATA;
00289     avio_read(pb, ivec, 16);
00290     avio_read(pb, tmpbuf, 16);
00291     if (mxf->aesc)
00292         av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
00293     if (memcmp(tmpbuf, checkv, 16))
00294         av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
00295     size -= 32;
00296     size = av_get_packet(pb, pkt, size);
00297     if (size < 0)
00298         return size;
00299     else if (size < plaintext_size)
00300         return AVERROR_INVALIDDATA;
00301     size -= plaintext_size;
00302     if (mxf->aesc)
00303         av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
00304                      &pkt->data[plaintext_size], size >> 4, ivec, 1);
00305     av_shrink_packet(pkt, orig_size);
00306     pkt->stream_index = index;
00307     avio_skip(pb, end - avio_tell(pb));
00308     return 0;
00309 }
00310 
00311 static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
00312 {
00313     KLVPacket klv;
00314 
00315     while (!s->pb->eof_reached) {
00316         int ret;
00317         if (klv_read_packet(&klv, s->pb) < 0)
00318             return -1;
00319         PRINT_KEY(s, "read packet", klv.key);
00320         av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
00321         if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
00322             ret = mxf_decrypt_triplet(s, pkt, &klv);
00323             if (ret < 0) {
00324                 av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
00325                 return AVERROR_INVALIDDATA;
00326             }
00327             return 0;
00328         }
00329         if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
00330             int index = mxf_get_stream_index(s, &klv);
00331             if (index < 0) {
00332                 av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12));
00333                 goto skip;
00334             }
00335             if (s->streams[index]->discard == AVDISCARD_ALL)
00336                 goto skip;
00337             /* check for 8 channels AES3 element */
00338             if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
00339                 if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
00340                     av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
00341                     return AVERROR_INVALIDDATA;
00342                 }
00343             } else {
00344                 ret = av_get_packet(s->pb, pkt, klv.length);
00345                 if (ret < 0)
00346                     return ret;
00347             }
00348             pkt->stream_index = index;
00349             pkt->pos = klv.offset;
00350             return 0;
00351         } else
00352         skip:
00353             avio_skip(s->pb, klv.length);
00354     }
00355     return AVERROR_EOF;
00356 }
00357 
00358 static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00359 {
00360     MXFContext *mxf = arg;
00361     int item_num = avio_rb32(pb);
00362     int item_len = avio_rb32(pb);
00363 
00364     if (item_len != 18) {
00365         av_log_ask_for_sample(pb, "unsupported primer pack item length %d\n",
00366                               item_len);
00367         return AVERROR_PATCHWELCOME;
00368     }
00369     if (item_num > UINT_MAX / item_len)
00370         return AVERROR_INVALIDDATA;
00371     mxf->local_tags_count = item_num;
00372     mxf->local_tags = av_malloc(item_num*item_len);
00373     if (!mxf->local_tags)
00374         return AVERROR(ENOMEM);
00375     avio_read(pb, mxf->local_tags, item_num*item_len);
00376     return 0;
00377 }
00378 
00379 static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
00380 {
00381     if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets))
00382         return AVERROR(ENOMEM);
00383     mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
00384     if (!mxf->metadata_sets)
00385         return AVERROR(ENOMEM);
00386     mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
00387     mxf->metadata_sets_count++;
00388     return 0;
00389 }
00390 
00391 static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00392 {
00393     MXFCryptoContext *cryptocontext = arg;
00394     if (size != 16)
00395         return AVERROR_INVALIDDATA;
00396     if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
00397         avio_read(pb, cryptocontext->source_container_ul, 16);
00398     return 0;
00399 }
00400 
00401 static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00402 {
00403     MXFContext *mxf = arg;
00404     switch (tag) {
00405     case 0x1901:
00406         mxf->packages_count = avio_rb32(pb);
00407         if (mxf->packages_count >= UINT_MAX / sizeof(UID))
00408             return AVERROR_INVALIDDATA;
00409         mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
00410         if (!mxf->packages_refs)
00411             return AVERROR(ENOMEM);
00412         avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
00413         avio_read(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
00414         break;
00415     }
00416     return 0;
00417 }
00418 
00419 static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00420 {
00421     MXFStructuralComponent *source_clip = arg;
00422     switch(tag) {
00423     case 0x0202:
00424         source_clip->duration = avio_rb64(pb);
00425         break;
00426     case 0x1201:
00427         source_clip->start_position = avio_rb64(pb);
00428         break;
00429     case 0x1101:
00430         /* UMID, only get last 16 bytes */
00431         avio_skip(pb, 16);
00432         avio_read(pb, source_clip->source_package_uid, 16);
00433         break;
00434     case 0x1102:
00435         source_clip->source_track_id = avio_rb32(pb);
00436         break;
00437     }
00438     return 0;
00439 }
00440 
00441 static int mxf_read_material_package(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00442 {
00443     MXFPackage *package = arg;
00444     switch(tag) {
00445     case 0x4403:
00446         package->tracks_count = avio_rb32(pb);
00447         if (package->tracks_count >= UINT_MAX / sizeof(UID))
00448             return AVERROR_INVALIDDATA;
00449         package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00450         if (!package->tracks_refs)
00451             return AVERROR(ENOMEM);
00452         avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
00453         avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00454         break;
00455     }
00456     return 0;
00457 }
00458 
00459 static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00460 {
00461     MXFTrack *track = arg;
00462     switch(tag) {
00463     case 0x4801:
00464         track->track_id = avio_rb32(pb);
00465         break;
00466     case 0x4804:
00467         avio_read(pb, track->track_number, 4);
00468         break;
00469     case 0x4B01:
00470         track->edit_rate.den = avio_rb32(pb);
00471         track->edit_rate.num = avio_rb32(pb);
00472         break;
00473     case 0x4803:
00474         avio_read(pb, track->sequence_ref, 16);
00475         break;
00476     }
00477     return 0;
00478 }
00479 
00480 static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00481 {
00482     MXFSequence *sequence = arg;
00483     switch(tag) {
00484     case 0x0202:
00485         sequence->duration = avio_rb64(pb);
00486         break;
00487     case 0x0201:
00488         avio_read(pb, sequence->data_definition_ul, 16);
00489         break;
00490     case 0x1001:
00491         sequence->structural_components_count = avio_rb32(pb);
00492         if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
00493             return AVERROR_INVALIDDATA;
00494         sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
00495         if (!sequence->structural_components_refs)
00496             return AVERROR(ENOMEM);
00497         avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
00498         avio_read(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
00499         break;
00500     }
00501     return 0;
00502 }
00503 
00504 static int mxf_read_source_package(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00505 {
00506     MXFPackage *package = arg;
00507     switch(tag) {
00508     case 0x4403:
00509         package->tracks_count = avio_rb32(pb);
00510         if (package->tracks_count >= UINT_MAX / sizeof(UID))
00511             return AVERROR_INVALIDDATA;
00512         package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00513         if (!package->tracks_refs)
00514             return AVERROR(ENOMEM);
00515         avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
00516         avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00517         break;
00518     case 0x4401:
00519         /* UMID, only get last 16 bytes */
00520         avio_skip(pb, 16);
00521         avio_read(pb, package->package_uid, 16);
00522         break;
00523     case 0x4701:
00524         avio_read(pb, package->descriptor_ref, 16);
00525         break;
00526     }
00527     return 0;
00528 }
00529 
00530 static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00531 {
00532     switch(tag) {
00533     case 0x3F05: av_dlog(NULL, "EditUnitByteCount %d\n", avio_rb32(pb)); break;
00534     case 0x3F06: av_dlog(NULL, "IndexSID %d\n", avio_rb32(pb)); break;
00535     case 0x3F07: av_dlog(NULL, "BodySID %d\n", avio_rb32(pb)); break;
00536     case 0x3F0B: av_dlog(NULL, "IndexEditRate %d/%d\n", avio_rb32(pb), avio_rb32(pb)); break;
00537     case 0x3F0C: av_dlog(NULL, "IndexStartPosition %"PRIu64"\n", avio_rb64(pb)); break;
00538     case 0x3F0D: av_dlog(NULL, "IndexDuration %"PRIu64"\n", avio_rb64(pb)); break;
00539     }
00540     return 0;
00541 }
00542 
00543 static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
00544 {
00545     int code, value, ofs = 0;
00546     char layout[16] = {0};
00547 
00548     do {
00549         code = avio_r8(pb);
00550         value = avio_r8(pb);
00551         av_dlog(NULL, "pixel layout: code %#x\n", code);
00552 
00553         if (ofs < 16) {
00554             layout[ofs++] = code;
00555             layout[ofs++] = value;
00556         }
00557     } while (code != 0); /* SMPTE 377M E.2.46 */
00558 
00559     ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
00560 }
00561 
00562 static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid)
00563 {
00564     MXFDescriptor *descriptor = arg;
00565     switch(tag) {
00566     case 0x3F01:
00567         descriptor->sub_descriptors_count = avio_rb32(pb);
00568         if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
00569             return AVERROR_INVALIDDATA;
00570         descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
00571         if (!descriptor->sub_descriptors_refs)
00572             return AVERROR(ENOMEM);
00573         avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
00574         avio_read(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
00575         break;
00576     case 0x3004:
00577         avio_read(pb, descriptor->essence_container_ul, 16);
00578         break;
00579     case 0x3006:
00580         descriptor->linked_track_id = avio_rb32(pb);
00581         break;
00582     case 0x3201: /* PictureEssenceCoding */
00583         avio_read(pb, descriptor->essence_codec_ul, 16);
00584         break;
00585     case 0x3203:
00586         descriptor->width = avio_rb32(pb);
00587         break;
00588     case 0x3202:
00589         descriptor->height = avio_rb32(pb);
00590         break;
00591     case 0x320E:
00592         descriptor->aspect_ratio.num = avio_rb32(pb);
00593         descriptor->aspect_ratio.den = avio_rb32(pb);
00594         break;
00595     case 0x3D03:
00596         descriptor->sample_rate.num = avio_rb32(pb);
00597         descriptor->sample_rate.den = avio_rb32(pb);
00598         break;
00599     case 0x3D06: /* SoundEssenceCompression */
00600         avio_read(pb, descriptor->essence_codec_ul, 16);
00601         break;
00602     case 0x3D07:
00603         descriptor->channels = avio_rb32(pb);
00604         break;
00605     case 0x3D01:
00606         descriptor->bits_per_sample = avio_rb32(pb);
00607         break;
00608     case 0x3401:
00609         mxf_read_pixel_layout(pb, descriptor);
00610         break;
00611     default:
00612         /* Private uid used by SONY C0023S01.mxf */
00613         if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
00614             descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00615             if (!descriptor->extradata)
00616                 return AVERROR(ENOMEM);
00617             descriptor->extradata_size = size;
00618             avio_read(pb, descriptor->extradata, size);
00619         }
00620         break;
00621     }
00622     return 0;
00623 }
00624 
00625 /*
00626  * Match an uid independently of the version byte and up to len common bytes
00627  * Returns: boolean
00628  */
00629 static int mxf_match_uid(const UID key, const UID uid, int len)
00630 {
00631     int i;
00632     for (i = 0; i < len; i++) {
00633         if (i != 7 && key[i] != uid[i])
00634             return 0;
00635     }
00636     return 1;
00637 }
00638 
00639 static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
00640 {
00641     while (uls->uid[0]) {
00642         if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
00643             break;
00644         uls++;
00645     }
00646     return uls;
00647 }
00648 
00649 static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
00650 {
00651     int i;
00652 
00653     if (!strong_ref)
00654         return NULL;
00655     for (i = 0; i < mxf->metadata_sets_count; i++) {
00656         if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
00657             (type == AnyType || mxf->metadata_sets[i]->type == type)) {
00658             return mxf->metadata_sets[i];
00659         }
00660     }
00661     return NULL;
00662 }
00663 
00664 static const MXFCodecUL mxf_essence_container_uls[] = {
00665     // video essence container uls
00666     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO }, /* MPEG-ES Frame wrapped */
00667     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14,    CODEC_ID_DVVIDEO }, /* DV 625 25mbps */
00668     // sound essence container uls
00669     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */
00670     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14,       CODEC_ID_MP2 }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */
00671     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, CODEC_ID_PCM_S16LE }, /* D-10 Mapping 50Mbps PAL Extended Template */
00672     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,      CODEC_ID_NONE },
00673 };
00674 
00675 static int mxf_parse_structural_metadata(MXFContext *mxf)
00676 {
00677     MXFPackage *material_package = NULL;
00678     MXFPackage *temp_package = NULL;
00679     int i, j, k;
00680 
00681     av_dlog(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
00682     /* TODO: handle multiple material packages (OP3x) */
00683     for (i = 0; i < mxf->packages_count; i++) {
00684         material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
00685         if (material_package) break;
00686     }
00687     if (!material_package) {
00688         av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
00689         return AVERROR_INVALIDDATA;
00690     }
00691 
00692     for (i = 0; i < material_package->tracks_count; i++) {
00693         MXFPackage *source_package = NULL;
00694         MXFTrack *material_track = NULL;
00695         MXFTrack *source_track = NULL;
00696         MXFTrack *temp_track = NULL;
00697         MXFDescriptor *descriptor = NULL;
00698         MXFStructuralComponent *component = NULL;
00699         UID *essence_container_ul = NULL;
00700         const MXFCodecUL *codec_ul = NULL;
00701         const MXFCodecUL *container_ul = NULL;
00702         AVStream *st;
00703 
00704         if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
00705             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
00706             continue;
00707         }
00708 
00709         if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
00710             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
00711             continue;
00712         }
00713 
00714         /* TODO: handle multiple source clips */
00715         for (j = 0; j < material_track->sequence->structural_components_count; j++) {
00716             /* TODO: handle timecode component */
00717             component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
00718             if (!component)
00719                 continue;
00720 
00721             for (k = 0; k < mxf->packages_count; k++) {
00722                 temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
00723                 if (!temp_package)
00724                     continue;
00725                 if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
00726                     source_package = temp_package;
00727                     break;
00728                 }
00729             }
00730             if (!source_package) {
00731                 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
00732                 break;
00733             }
00734             for (k = 0; k < source_package->tracks_count; k++) {
00735                 if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
00736                     av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
00737                     return AVERROR_INVALIDDATA;
00738                 }
00739                 if (temp_track->track_id == component->source_track_id) {
00740                     source_track = temp_track;
00741                     break;
00742                 }
00743             }
00744             if (!source_track) {
00745                 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
00746                 break;
00747             }
00748         }
00749         if (!source_track)
00750             continue;
00751 
00752         st = avformat_new_stream(mxf->fc, NULL);
00753         if (!st) {
00754             av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
00755             return AVERROR(ENOMEM);
00756         }
00757         st->id = source_track->track_id;
00758         st->priv_data = source_track;
00759         st->duration = component->duration;
00760         if (st->duration == -1)
00761             st->duration = AV_NOPTS_VALUE;
00762         st->start_time = component->start_position;
00763         avpriv_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
00764 
00765         if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
00766             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
00767             return AVERROR_INVALIDDATA;
00768         }
00769 
00770         PRINT_KEY(mxf->fc, "data definition   ul", source_track->sequence->data_definition_ul);
00771         codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
00772         st->codec->codec_type = codec_ul->id;
00773 
00774         source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
00775         if (source_package->descriptor) {
00776             if (source_package->descriptor->type == MultipleDescriptor) {
00777                 for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
00778                     MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
00779 
00780                     if (!sub_descriptor) {
00781                         av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
00782                         continue;
00783                     }
00784                     if (sub_descriptor->linked_track_id == source_track->track_id) {
00785                         descriptor = sub_descriptor;
00786                         break;
00787                     }
00788                 }
00789             } else if (source_package->descriptor->type == Descriptor)
00790                 descriptor = source_package->descriptor;
00791         }
00792         if (!descriptor) {
00793             av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
00794             continue;
00795         }
00796         PRINT_KEY(mxf->fc, "essence codec     ul", descriptor->essence_codec_ul);
00797         PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
00798         essence_container_ul = &descriptor->essence_container_ul;
00799         /* HACK: replacing the original key with mxf_encrypted_essence_container
00800          * is not allowed according to s429-6, try to find correct information anyway */
00801         if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
00802             av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
00803             for (k = 0; k < mxf->metadata_sets_count; k++) {
00804                 MXFMetadataSet *metadata = mxf->metadata_sets[k];
00805                 if (metadata->type == CryptoContext) {
00806                     essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
00807                     break;
00808                 }
00809             }
00810         }
00811         /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
00812         codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
00813         st->codec->codec_id = codec_ul->id;
00814         if (descriptor->extradata) {
00815             st->codec->extradata = descriptor->extradata;
00816             st->codec->extradata_size = descriptor->extradata_size;
00817         }
00818         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00819             container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
00820             if (st->codec->codec_id == CODEC_ID_NONE)
00821                 st->codec->codec_id = container_ul->id;
00822             st->codec->width = descriptor->width;
00823             st->codec->height = descriptor->height;
00824             if (st->codec->codec_id == CODEC_ID_RAWVIDEO)
00825                 st->codec->pix_fmt = descriptor->pix_fmt;
00826             st->need_parsing = AVSTREAM_PARSE_HEADERS;
00827         } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00828             container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
00829             if (st->codec->codec_id == CODEC_ID_NONE)
00830                 st->codec->codec_id = container_ul->id;
00831             st->codec->channels = descriptor->channels;
00832             st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
00833             if (descriptor->sample_rate.den > 0) {
00834                 st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
00835                 avpriv_set_pts_info(st, 64, descriptor->sample_rate.den, descriptor->sample_rate.num);
00836             } else {
00837                 av_log(mxf->fc, AV_LOG_WARNING, "invalid sample rate (%d/%d) "
00838                        "found for stream #%d, time base forced to 1/48000\n",
00839                        descriptor->sample_rate.num, descriptor->sample_rate.den,
00840                        st->index);
00841                 avpriv_set_pts_info(st, 64, 1, 48000);
00842             }
00843 
00844             /* TODO: implement CODEC_ID_RAWAUDIO */
00845             if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
00846                 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
00847                     st->codec->codec_id = CODEC_ID_PCM_S24LE;
00848                 else if (descriptor->bits_per_sample == 32)
00849                     st->codec->codec_id = CODEC_ID_PCM_S32LE;
00850             } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
00851                 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
00852                     st->codec->codec_id = CODEC_ID_PCM_S24BE;
00853                 else if (descriptor->bits_per_sample == 32)
00854                     st->codec->codec_id = CODEC_ID_PCM_S32BE;
00855             } else if (st->codec->codec_id == CODEC_ID_MP2) {
00856                 st->need_parsing = AVSTREAM_PARSE_FULL;
00857             }
00858         }
00859         if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
00860             av_log(mxf->fc, AV_LOG_WARNING, "only frame wrapped mappings are correctly supported\n");
00861             st->need_parsing = AVSTREAM_PARSE_FULL;
00862         }
00863     }
00864     return 0;
00865 }
00866 
00867 static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
00868     { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
00869     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
00870     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
00871     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
00872     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
00873     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
00874     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
00875     { { 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 */
00876     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
00877     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
00878     { { 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 */
00879     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
00880     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
00881     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
00882     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
00883     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
00884     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
00885     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
00886 };
00887 
00888 static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
00889 {
00890     AVIOContext *pb = mxf->fc->pb;
00891     MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
00892     uint64_t klv_end = avio_tell(pb) + klv->length;
00893 
00894     if (!ctx)
00895         return AVERROR(ENOMEM);
00896     while (avio_tell(pb) + 4 < klv_end) {
00897         int ret;
00898         int tag = avio_rb16(pb);
00899         int size = avio_rb16(pb); /* KLV specified by 0x53 */
00900         uint64_t next = avio_tell(pb) + size;
00901         UID uid = {0};
00902 
00903         av_dlog(mxf->fc, "local tag %#04x size %d\n", tag, size);
00904         if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
00905             av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
00906             continue;
00907         }
00908         if (tag > 0x7FFF) { /* dynamic tag */
00909             int i;
00910             for (i = 0; i < mxf->local_tags_count; i++) {
00911                 int local_tag = AV_RB16(mxf->local_tags+i*18);
00912                 if (local_tag == tag) {
00913                     memcpy(uid, mxf->local_tags+i*18+2, 16);
00914                     av_dlog(mxf->fc, "local tag %#04x\n", local_tag);
00915                     PRINT_KEY(mxf->fc, "uid", uid);
00916                 }
00917             }
00918         }
00919         if (ctx_size && tag == 0x3C0A)
00920             avio_read(pb, ctx->uid, 16);
00921         else if ((ret = read_child(ctx, pb, tag, size, uid)) < 0)
00922             return ret;
00923 
00924         avio_seek(pb, next, SEEK_SET);
00925     }
00926     if (ctx_size) ctx->type = type;
00927     return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
00928 }
00929 
00930 static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00931 {
00932     MXFContext *mxf = s->priv_data;
00933     KLVPacket klv;
00934 
00935     if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
00936         av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
00937         return AVERROR_INVALIDDATA;
00938     }
00939     avio_seek(s->pb, -14, SEEK_CUR);
00940     mxf->fc = s;
00941     while (!s->pb->eof_reached) {
00942         int ret;
00943         const MXFMetadataReadTableEntry *metadata;
00944 
00945         if ((ret = klv_read_packet(&klv, s->pb)) < 0)
00946             return ret;
00947         PRINT_KEY(s, "read header", klv.key);
00948         av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
00949         if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
00950             IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
00951             /* FIXME avoid seek */
00952             avio_seek(s->pb, klv.offset, SEEK_SET);
00953             break;
00954         }
00955 
00956         for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
00957             if (IS_KLV_KEY(klv.key, metadata->key)) {
00958                 int res;
00959                 if (klv.key[5] == 0x53) {
00960                     res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);
00961                 } else
00962                     res = metadata->read(mxf, s->pb, 0, 0, NULL);
00963                 if (res < 0) {
00964                     av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
00965                     return res;
00966                 }
00967                 break;
00968             }
00969         }
00970         if (!metadata->read)
00971             avio_skip(s->pb, klv.length);
00972     }
00973     return mxf_parse_structural_metadata(mxf);
00974 }
00975 
00976 static int mxf_read_close(AVFormatContext *s)
00977 {
00978     MXFContext *mxf = s->priv_data;
00979     int i;
00980 
00981     av_freep(&mxf->packages_refs);
00982 
00983     for (i = 0; i < s->nb_streams; i++)
00984         s->streams[i]->priv_data = NULL;
00985 
00986     for (i = 0; i < mxf->metadata_sets_count; i++) {
00987         switch (mxf->metadata_sets[i]->type) {
00988         case MultipleDescriptor:
00989             av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
00990             break;
00991         case Sequence:
00992             av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
00993             break;
00994         case SourcePackage:
00995         case MaterialPackage:
00996             av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
00997             break;
00998         default:
00999             break;
01000         }
01001         av_freep(&mxf->metadata_sets[i]);
01002     }
01003     av_freep(&mxf->metadata_sets);
01004     av_freep(&mxf->aesc);
01005     av_freep(&mxf->local_tags);
01006     return 0;
01007 }
01008 
01009 static int mxf_probe(AVProbeData *p) {
01010     uint8_t *bufp = p->buf;
01011     uint8_t *end = p->buf + p->buf_size;
01012 
01013     if (p->buf_size < sizeof(mxf_header_partition_pack_key))
01014         return 0;
01015 
01016     /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
01017     end -= sizeof(mxf_header_partition_pack_key);
01018     for (; bufp < end; bufp++) {
01019         if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
01020             return AVPROBE_SCORE_MAX;
01021     }
01022     return 0;
01023 }
01024 
01025 /* rudimentary byte seek */
01026 /* XXX: use MXF Index */
01027 static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
01028 {
01029     AVStream *st = s->streams[stream_index];
01030     int64_t seconds;
01031 
01032     if (!s->bit_rate)
01033         return AVERROR_INVALIDDATA;
01034     if (sample_time < 0)
01035         sample_time = 0;
01036     seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
01037     avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
01038     ff_update_cur_dts(s, st, sample_time);
01039     return 0;
01040 }
01041 
01042 AVInputFormat ff_mxf_demuxer = {
01043     .name           = "mxf",
01044     .long_name      = NULL_IF_CONFIG_SMALL("Material eXchange Format"),
01045     .priv_data_size = sizeof(MXFContext),
01046     .read_probe     = mxf_probe,
01047     .read_header    = mxf_read_header,
01048     .read_packet    = mxf_read_packet,
01049     .read_close     = mxf_read_close,
01050     .read_seek      = mxf_read_seek,
01051 };