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