return 0 as stream index if only one stream, this is completely non standard, fix...
[ffmpeg.git] / libavformat / mxf.c
1 /*
2  * MXF demuxer.
3  * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 /*
21  * References
22  * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
23  * SMPTE 377M MXF File Format Specifications
24  * SMPTE 378M Operational Pattern 1a
25  * SMPTE 379M MXF Generic Container
26  * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
27  * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container
28  * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container
29  *
30  * Principle
31  * Search for Track numbers which will identify essence element KLV packets.
32  * Search for SourcePackage which define tracks which contains Track numbers.
33  * Material Package contains tracks with reference to SourcePackage tracks.
34  * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
35  * Assign Descriptors to correct Tracks.
36  *
37  * Metadata reading functions read Local Tags, get InstanceUID(0x3C0A) then add MetaDataSet to MXFContext.
38  * Metadata parsing resolves Strong References to objects.
39  *
40  * Simple demuxer, only OP1A supported and some files might not work at all.
41  * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
42  */
43
44 //#define DEBUG
45
46 #include "avformat.h"
47
48 typedef uint8_t UID[16];
49
50 enum MXFMetadataSetType {
51     MaterialPackage,
52     SourcePackage,
53     SourceClip,
54     TimecodeComponent,
55     Sequence,
56     MultipleDescriptor,
57     Descriptor,
58     Track,
59     EssenceContainerData,
60 };
61
62 typedef struct MXFStructuralComponent {
63     UID uid;
64     enum MXFMetadataSetType type;
65     UID source_package_uid;
66     UID data_definition_ul;
67     int64_t duration;
68     int64_t start_position;
69     int source_track_id;
70 } MXFStructuralComponent;
71
72 typedef struct MXFSequence {
73     UID uid;
74     enum MXFMetadataSetType type;
75     UID data_definition_ul;
76     UID *structural_components_refs;
77     int structural_components_count;
78     int64_t duration;
79 } MXFSequence;
80
81 typedef struct MXFTrack {
82     UID uid;
83     enum MXFMetadataSetType type;
84     MXFSequence *sequence; /* mandatory, and only one */
85     UID sequence_ref;
86     int track_id;
87     uint8_t track_number[4];
88     AVRational edit_rate;
89 } MXFTrack;
90
91 typedef struct MXFDescriptor {
92     UID uid;
93     enum MXFMetadataSetType type;
94     UID essence_container_ul;
95     UID essence_codec_ul;
96     AVRational sample_rate;
97     AVRational aspect_ratio;
98     int width;
99     int height;
100     int channels;
101     int bits_per_sample;
102     UID *sub_descriptors_refs;
103     int sub_descriptors_count;
104     int linked_track_id;
105     uint8_t *extradata;
106     int extradata_size;
107 } MXFDescriptor;
108
109 typedef struct MXFPackage {
110     UID uid;
111     enum MXFMetadataSetType type;
112     UID package_uid;
113     UID *tracks_refs;
114     int tracks_count;
115     MXFDescriptor *descriptor; /* only one */
116     UID descriptor_ref;
117 } MXFPackage;
118
119 typedef struct MXFEssenceContainerData {
120     UID uid;
121     enum MXFMetadataSetType type;
122     UID linked_package_uid;
123 } MXFEssenceContainerData;
124
125 typedef struct {
126     UID uid;
127     enum MXFMetadataSetType type;
128 } MXFMetadataSet;
129
130 typedef struct MXFContext {
131     UID *packages_refs;
132     int packages_count;
133     UID *essence_container_data_sets_refs;
134     int essence_container_data_sets_count;
135     UID *essence_containers_uls; /* Universal Labels SMPTE RP224 */
136     int essence_containers_uls_count;
137     UID operational_pattern_ul;
138     UID content_storage_uid;
139     MXFMetadataSet **metadata_sets;
140     int metadata_sets_count;
141     AVFormatContext *fc;
142 } MXFContext;
143
144 typedef struct KLVPacket {
145     UID key;
146     offset_t offset;
147     uint64_t length;
148 } KLVPacket;
149
150 enum MXFWrappingScheme {
151     Frame,
152     Clip,
153 };
154
155 typedef struct MXFCodecUL {
156     UID uid;
157     enum CodecID id;
158     enum MXFWrappingScheme wrapping;
159 } MXFCodecUL;
160
161 typedef struct MXFDataDefinitionUL {
162     UID uid;
163     enum CodecType type;
164 } MXFDataDefinitionUL;
165
166 typedef struct MXFMetadataReadTableEntry {
167     const UID key;
168     int (*read)(MXFContext *mxf, KLVPacket *klv);
169 } MXFMetadataReadTableEntry;
170
171 /* partial keys to match */
172 static const uint8_t mxf_header_partition_pack_key[]       = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
173 static const uint8_t mxf_essence_element_key[]             = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
174
175 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
176
177 #define PRINT_KEY(s, x) dprintf("%s %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", s, \
178                              (x)[0], (x)[1], (x)[2], (x)[3], (x)[4], (x)[5], (x)[6], (x)[7], (x)[8], (x)[9], (x)[10], (x)[11], (x)[12], (x)[13], (x)[14], (x)[15])
179
180 static int64_t klv_decode_ber_length(ByteIOContext *pb)
181 {
182     int64_t size = 0;
183     uint8_t length = get_byte(pb);
184     int type = length >> 7;
185
186     if (type) { /* long form */
187         int bytes_num = length & 0x7f;
188         /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
189         if (bytes_num > 8)
190             return -1;
191         while (bytes_num--)
192             size = size << 8 | get_byte(pb);
193     } else {
194         size = length & 0x7f;
195     }
196     return size;
197 }
198
199 static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
200 {
201     klv->offset = url_ftell(pb);
202     get_buffer(pb, klv->key, 16);
203     klv->length = klv_decode_ber_length(pb);
204     return klv->length == -1 ? -1 : 0;
205 }
206
207 static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
208 {
209     int i;
210
211     for (i = 0; i < s->nb_streams; i++) {
212         MXFTrack *track = s->streams[i]->priv_data;
213          /* SMPTE 379M 7.3 */
214         if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
215             return i;
216     }
217     /* return 0 if only one stream, for OP Atom files with 0 as track number */
218     return s->nb_streams == 1 ? 0 : -1;
219 }
220
221 static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
222 {
223     KLVPacket klv;
224
225     while (!url_feof(&s->pb)) {
226         if (klv_read_packet(&klv, &s->pb) < 0) {
227             av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
228             return -1;
229         }
230 #ifdef DEBUG
231         PRINT_KEY("read packet", klv.key);
232 #endif
233         if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
234             av_get_packet(&s->pb, pkt, klv.length);
235             pkt->stream_index = mxf_get_stream_index(s, &klv);
236             return pkt->stream_index == -1 ? -1 : 0;
237         } else
238             url_fskip(&s->pb, klv.length);
239     }
240     return AVERROR_IO;
241 }
242
243 static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
244 {
245     mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
246     mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
247     mxf->metadata_sets_count++;
248     return 0;
249 }
250
251 static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
252 {
253     ByteIOContext *pb = &mxf->fc->pb;
254     int bytes_read = 0;
255
256     while (bytes_read < klv->length) {
257         int tag = get_be16(pb);
258         int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
259
260         switch (tag) {
261         case 0x3B03:
262             get_buffer(pb, mxf->content_storage_uid, 16);
263             break;
264         case 0x3B09:
265             get_buffer(pb, mxf->operational_pattern_ul, 16);
266             break;
267         case 0x3B0A:
268             mxf->essence_containers_uls_count = get_be32(pb);
269             if (mxf->essence_containers_uls_count >= UINT_MAX / sizeof(UID))
270                 return -1;
271             mxf->essence_containers_uls = av_malloc(mxf->essence_containers_uls_count * sizeof(UID));
272             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
273             get_buffer(pb, (uint8_t *)mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
274             break;
275         default:
276             url_fskip(pb, size);
277         }
278         bytes_read += size + 4;
279     }
280     return 0;
281 }
282
283 static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
284 {
285     ByteIOContext *pb = &mxf->fc->pb;
286     int bytes_read = 0;
287
288     while (bytes_read < klv->length) {
289         int tag = get_be16(pb);
290         int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
291
292         dprintf("tag 0x%04X, size %d\n", tag, size);
293         switch (tag) {
294         case 0x1901:
295             mxf->packages_count = get_be32(pb);
296             if (mxf->packages_count >= UINT_MAX / sizeof(UID))
297                 return -1;
298             mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
299             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
300             get_buffer(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
301             break;
302         case 0x1902:
303             mxf->essence_container_data_sets_count = get_be32(pb);
304             if (mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(UID))
305                 return -1;
306             mxf->essence_container_data_sets_refs = av_malloc(mxf->essence_container_data_sets_count * sizeof(UID));
307             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
308             get_buffer(pb, (uint8_t *)mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
309             break;
310         default:
311             url_fskip(pb, size);
312         }
313         bytes_read += size + 4;
314     }
315     return 0;
316 }
317
318 static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
319 {
320     ByteIOContext *pb = &mxf->fc->pb;
321     MXFStructuralComponent *source_clip = av_mallocz(sizeof(*source_clip));
322     int bytes_read = 0;
323
324     while (bytes_read < klv->length) {
325         int tag = get_be16(pb);
326         int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
327
328         bytes_read += size + 4;
329         dprintf("tag 0x%04X, size %d\n", tag, size);
330         if (!size) /* ignore empty tag, needed for some files with empty UMID tag */
331             continue;
332         switch (tag) {
333         case 0x3C0A:
334             get_buffer(pb, source_clip->uid, 16);
335             break;
336         case 0x0202:
337             source_clip->duration = get_be64(pb);
338             break;
339         case 0x1201:
340             source_clip->start_position = get_be64(pb);
341             break;
342         case 0x1101:
343             /* UMID, only get last 16 bytes */
344             url_fskip(pb, 16);
345             get_buffer(pb, source_clip->source_package_uid, 16);
346             break;
347         case 0x1102:
348             source_clip->source_track_id = get_be32(pb);
349             break;
350         default:
351             url_fskip(pb, size);
352         }
353     }
354     source_clip->type = SourceClip;
355     return mxf_add_metadata_set(mxf, source_clip);
356 }
357
358 static int mxf_read_metadata_material_package(MXFContext *mxf, KLVPacket *klv)
359 {
360     ByteIOContext *pb = &mxf->fc->pb;
361     MXFPackage *package = av_mallocz(sizeof(*package));
362     int bytes_read = 0;
363
364     while (bytes_read < klv->length) {
365         int tag = get_be16(pb);
366         int size = get_be16(pb); /* KLV specified by 0x53 */
367
368         switch (tag) {
369         case 0x3C0A:
370             get_buffer(pb, package->uid, 16);
371             break;
372         case 0x4403:
373             package->tracks_count = get_be32(pb);
374             if (package->tracks_count >= UINT_MAX / sizeof(UID))
375                 return -1;
376             package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
377             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
378             get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
379             break;
380         default:
381             url_fskip(pb, size);
382         }
383         bytes_read += size + 4;
384     }
385     package->type = MaterialPackage;
386     return mxf_add_metadata_set(mxf, package);
387 }
388
389 static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
390 {
391     ByteIOContext *pb = &mxf->fc->pb;
392     MXFTrack *track = av_mallocz(sizeof(*track));
393     int bytes_read = 0;
394
395     while (bytes_read < klv->length) {
396         int tag = get_be16(pb);
397         int size = get_be16(pb); /* KLV specified by 0x53 */
398
399         dprintf("tag 0x%04X, size %d\n", tag, size);
400         switch (tag) {
401         case 0x3C0A:
402             get_buffer(pb, track->uid, 16);
403             break;
404         case 0x4801:
405             track->track_id = get_be32(pb);
406             break;
407         case 0x4804:
408             get_buffer(pb, track->track_number, 4);
409             break;
410         case 0x4B01:
411             track->edit_rate.den = get_be32(pb);
412             track->edit_rate.num = get_be32(pb);
413             break;
414         case 0x4803:
415             get_buffer(pb, track->sequence_ref, 16);
416             break;
417         default:
418             url_fskip(pb, size);
419         }
420         bytes_read += size + 4;
421     }
422     track->type = Track;
423     return mxf_add_metadata_set(mxf, track);
424 }
425
426 static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
427 {
428     ByteIOContext *pb = &mxf->fc->pb;
429     MXFSequence *sequence = av_mallocz(sizeof(*sequence));
430     int bytes_read = 0;
431
432     while (bytes_read < klv->length) {
433         int tag = get_be16(pb);
434         int size = get_be16(pb); /* KLV specified by 0x53 */
435
436         dprintf("tag 0x%04X, size %d\n", tag, size);
437         switch (tag) {
438         case 0x3C0A:
439             get_buffer(pb, sequence->uid, 16);
440             break;
441         case 0x0202:
442             sequence->duration = get_be64(pb);
443             break;
444         case 0x0201:
445             get_buffer(pb, sequence->data_definition_ul, 16);
446             break;
447         case 0x1001:
448             sequence->structural_components_count = get_be32(pb);
449             if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
450                 return -1;
451             sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
452             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
453             get_buffer(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
454             break;
455         default:
456             url_fskip(pb, size);
457         }
458         bytes_read += size + 4;
459     }
460     sequence->type = Sequence;
461     return mxf_add_metadata_set(mxf, sequence);
462 }
463
464 static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
465 {
466     ByteIOContext *pb = &mxf->fc->pb;
467     MXFPackage *package = av_mallocz(sizeof(*package));
468     int bytes_read = 0;
469
470     while (bytes_read < klv->length) {
471         int tag = get_be16(pb);
472         int size = get_be16(pb); /* KLV specified by 0x53 */
473
474         dprintf("tag 0x%04X, size %d\n", tag, size);
475         switch (tag) {
476         case 0x3C0A:
477             get_buffer(pb, package->uid, 16);
478             break;
479         case 0x4403:
480             package->tracks_count = get_be32(pb);
481             if (package->tracks_count >= UINT_MAX / sizeof(UID))
482                 return -1;
483             package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
484             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
485             get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
486             break;
487         case 0x4401:
488             /* UMID, only get last 16 bytes */
489             url_fskip(pb, 16);
490             get_buffer(pb, package->package_uid, 16);
491             break;
492         case 0x4701:
493             get_buffer(pb, package->descriptor_ref, 16);
494             break;
495         default:
496             url_fskip(pb, size);
497         }
498         bytes_read += size + 4;
499     }
500     package->type = SourcePackage;
501     return mxf_add_metadata_set(mxf, package);
502 }
503
504 static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
505 {
506     ByteIOContext *pb = &mxf->fc->pb;
507     MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
508     int bytes_read = 0;
509
510     while (bytes_read < klv->length) {
511         int tag = get_be16(pb);
512         int size = get_be16(pb); /* KLV specified by 0x53 */
513
514         dprintf("tag 0x%04X, size %d\n", tag, size);
515         switch (tag) {
516         case 0x3C0A:
517             get_buffer(pb, descriptor->uid, 16);
518             break;
519         case 0x3F01:
520             descriptor->sub_descriptors_count = get_be32(pb);
521             if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
522                 return -1;
523             descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
524             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
525             get_buffer(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
526             break;
527         default:
528             url_fskip(pb, size);
529         }
530         bytes_read += size + 4;
531     }
532     descriptor->type = MultipleDescriptor;
533     return mxf_add_metadata_set(mxf, descriptor);
534 }
535
536 static void mxf_read_metadata_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
537 {
538     int code;
539
540     do {
541         code = get_byte(pb);
542         dprintf("pixel layout: code 0x%x\n", code);
543         switch (code) {
544         case 0x52: /* R */
545             descriptor->bits_per_sample += get_byte(pb);
546             break;
547         case 0x47: /* G */
548             descriptor->bits_per_sample += get_byte(pb);
549             break;
550         case 0x42: /* B */
551             descriptor->bits_per_sample += get_byte(pb);
552             break;
553         default:
554             get_byte(pb);
555         }
556     } while (code != 0); /* SMPTE 377M E.2.46 */
557 }
558
559 static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
560 {
561     ByteIOContext *pb = &mxf->fc->pb;
562     MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
563     int bytes_read = 0;
564
565     while (bytes_read < klv->length) {
566         int tag = get_be16(pb);
567         int size = get_be16(pb); /* KLV specified by 0x53 */
568
569         dprintf("tag 0x%04X, size %d\n", tag, size);
570         switch (tag) {
571         case 0x3C0A:
572             get_buffer(pb, descriptor->uid, 16);
573             break;
574         case 0x3004:
575             get_buffer(pb, descriptor->essence_container_ul, 16);
576             break;
577         case 0x3006:
578             descriptor->linked_track_id = get_be32(pb);
579             break;
580         case 0x3201: /* PictureEssenceCoding */
581             get_buffer(pb, descriptor->essence_codec_ul, 16);
582             break;
583         case 0x3203:
584             descriptor->width = get_be32(pb);
585             break;
586         case 0x3202:
587             descriptor->height = get_be32(pb);
588             break;
589         case 0x320E:
590             descriptor->aspect_ratio.num = get_be32(pb);
591             descriptor->aspect_ratio.den = get_be32(pb);
592             break;
593         case 0x3D03:
594             descriptor->sample_rate.num = get_be32(pb);
595             descriptor->sample_rate.den = get_be32(pb);
596             break;
597         case 0x3D06: /* SoundEssenceCompression */
598             get_buffer(pb, descriptor->essence_codec_ul, 16);
599             break;
600         case 0x3D07:
601             descriptor->channels = get_be32(pb);
602             break;
603         case 0x3D01:
604             descriptor->bits_per_sample = get_be32(pb);
605             break;
606         case 0x3401:
607             mxf_read_metadata_pixel_layout(pb, descriptor);
608             break;
609         case 0x8201: /* Private tag used by SONY C0023S01.mxf */
610             descriptor->extradata = av_malloc(size);
611             descriptor->extradata_size = size;
612             get_buffer(pb, descriptor->extradata, size);
613             break;
614         default:
615             url_fskip(pb, size);
616         }
617         bytes_read += size + 4;
618     }
619     descriptor->type = Descriptor;
620     return mxf_add_metadata_set(mxf, descriptor);
621 }
622
623 /* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
624 static const MXFDataDefinitionUL mxf_data_definition_uls[] = {
625     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 }, CODEC_TYPE_VIDEO },
626     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 }, CODEC_TYPE_AUDIO },
627     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x05,0x01,0x03,0x02,0x02,0x02,0x02,0x00,0x00 }, CODEC_TYPE_AUDIO },
628     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  CODEC_TYPE_DATA },
629 };
630
631 static const MXFCodecUL mxf_codec_uls[] = {
632     /* PictureEssenceCoding */
633     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@ML I-Frame */
634     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@HL Long GoP */
635     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MP@ML Long GoP */
636     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@ML Long GoP */
637     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MP@HL Long GoP */
638     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x03 },      CODEC_ID_MPEG4, Frame }, /* XDCAM proxy_pal030926.mxf */
639     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x04 },      CODEC_ID_MPEG4, Frame }, /* XDCAM Proxy C0023S01.mxf */
640     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, CODEC_ID_MPEG2VIDEO, Frame }, /* D-10 30Mbps PAL */
641     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, CODEC_ID_MPEG2VIDEO, Frame }, /* D-10 50Mbps PAL */
642     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DVCPRO50 PAL */
643     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DVCPRO25 PAL */
644     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DV25 IEC PAL */
645     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_JPEG2000, Frame }, /* JPEG2000 Codestream */
646     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 },   CODEC_ID_RAWVIDEO, Frame }, /* Uncompressed */
647     /* SoundEssenceCompression */
648     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* Uncompressed */
649     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE, Frame },
650     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x02,0x02,0x01,0x7E,0x00,0x00,0x00 },  CODEC_ID_PCM_S16BE, Frame }, /* From Omneon MXF file */
651     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_PCM_ALAW, Frame },
652     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x04,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_PCM_ALAW, Frame }, /* XDCAM Proxy C0023S01.mxf */
653     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 },        CODEC_ID_AC3, Frame },
654     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 },        CODEC_ID_MP2, Frame }, /* MP2 or MP3 */
655   //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 },    CODEC_ID_DOLBY_E, Frame }, /* Dolby-E */
656     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
657 };
658
659 static const MXFCodecUL mxf_picture_essence_container_uls[] = {
660     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MPEG-ES Frame wrapped */
661     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0xe0,0x02 }, CODEC_ID_MPEG2VIDEO,  Clip }, /* MPEG-ES Clip wrapped, 0xe0 MPV stream id */
662     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x04,0x61,0x07 }, CODEC_ID_MPEG2VIDEO,  Clip }, /* MPEG-ES Custom wrapped, 0x61 ??? stream id */
663     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
664 };
665
666 static const MXFCodecUL mxf_sound_essence_container_uls[] = {
667     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* BWF Frame wrapped */
668     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* AES Frame wrapped */
669     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 },        CODEC_ID_MP2, Frame }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */
670     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0xc0,0x01 },        CODEC_ID_MP2, Frame }, /* MPEG-ES Frame wrapped, 0xc0 MPA stream id */
671     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0xc0,0x02 },        CODEC_ID_MP2,  Clip }, /* MPEG-ES Clip wrapped, 0xc0 MPA stream id */
672   //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },   CODEC_ID_PCM_AES3, Frame }, /* D-10 Mapping 30Mbps PAL Extended Template */
673     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
674 };
675
676 static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
677 {
678     while (uls->id != CODEC_ID_NONE) {
679         if(!memcmp(uls->uid, *uid, 16))
680             break;
681         uls++;
682     }
683     return uls;
684 }
685
686 static enum CodecType mxf_get_codec_type(const MXFDataDefinitionUL *uls, UID *uid)
687 {
688     while (uls->type != CODEC_TYPE_DATA) {
689         if(!memcmp(uls->uid, *uid, 16))
690             break;
691         uls++;
692     }
693     return uls->type;
694 }
695
696 static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref)
697 {
698     int i;
699
700     if (!strong_ref)
701         return NULL;
702     for (i = 0; i < mxf->metadata_sets_count; i++) {
703         if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16)) {
704             return mxf->metadata_sets[i];
705         }
706     }
707     return NULL;
708 }
709
710 static int mxf_parse_structural_metadata(MXFContext *mxf)
711 {
712     MXFPackage *material_package = NULL;
713     MXFPackage *source_package = NULL;
714     MXFPackage *temp_package = NULL;
715     int i, j, k;
716
717     dprintf("metadata sets count %d\n", mxf->metadata_sets_count);
718     /* TODO: handle multiple material packages (OP3x) */
719     for (i = 0; i < mxf->packages_count; i++) {
720         if (!(temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i]))) {
721             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve package strong ref\n");
722             return -1;
723         }
724         if (temp_package->type == MaterialPackage) {
725             material_package = temp_package;
726             break;
727         }
728     }
729     if (!material_package) {
730         av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
731         return -1;
732     }
733
734     for (i = 0; i < material_package->tracks_count; i++) {
735         MXFTrack *material_track = NULL;
736         MXFTrack *source_track = NULL;
737         MXFTrack *temp_track = NULL;
738         MXFDescriptor *descriptor = NULL;
739         MXFStructuralComponent *component = NULL;
740         const MXFCodecUL *codec_ul = NULL;
741         const MXFCodecUL *container_ul = NULL;
742         AVStream *st;
743
744         if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i]))) {
745             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
746             continue;
747         }
748
749         if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref))) {
750             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
751             return -1;
752         }
753
754         /* TODO: handle multiple source clips */
755         for (j = 0; j < material_track->sequence->structural_components_count; j++) {
756             /* TODO: handle timecode component */
757             component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j]);
758             if (!component || component->type != SourceClip)
759                 continue;
760
761             for (k = 0; k < mxf->packages_count; k++) {
762                 if (!(temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k]))) {
763                     av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
764                     return -1;
765                 }
766                 if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
767                     source_package = temp_package;
768                     break;
769                 }
770             }
771             if (!source_package) {
772                 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
773                 break;
774             }
775             for (k = 0; k < source_package->tracks_count; k++) {
776                 if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k]))) {
777                     av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
778                     return -1;
779                 }
780                 if (temp_track->track_id == component->source_track_id) {
781                     source_track = temp_track;
782                     break;
783                 }
784             }
785             if (!source_track) {
786                 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
787                 break;
788             }
789         }
790         if (!source_track)
791             continue;
792
793         st = av_new_stream(mxf->fc, source_track->track_id);
794         st->priv_data = source_track;
795         st->duration = component->duration;
796         if (st->duration == -1)
797             st->duration = AV_NOPTS_VALUE;
798         st->start_time = component->start_position;
799         av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
800
801         if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref))) {
802             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
803             return -1;
804         }
805
806 #ifdef DEBUG
807         PRINT_KEY("data definition   ul", source_track->sequence->data_definition_ul);
808 #endif
809         st->codec->codec_type = mxf_get_codec_type(mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
810
811         source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref);
812         if (source_package->descriptor) {
813             if (source_package->descriptor->type == MultipleDescriptor) {
814                 for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
815                     MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j]);
816
817                     if (!sub_descriptor) {
818                         av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
819                         continue;
820                     }
821                     if (sub_descriptor->linked_track_id == source_track->track_id) {
822                         descriptor = sub_descriptor;
823                         break;
824                     }
825                 }
826             } else
827                 descriptor = source_package->descriptor;
828         }
829         if (!descriptor) {
830             av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
831             continue;
832         }
833 #ifdef DEBUG
834         PRINT_KEY("essence codec     ul", descriptor->essence_codec_ul);
835         PRINT_KEY("essence container ul", descriptor->essence_container_ul);
836 #endif
837         /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
838         codec_ul = mxf_get_codec_ul(mxf_codec_uls, &descriptor->essence_codec_ul);
839         st->codec->codec_id = codec_ul->id;
840         if (descriptor->extradata) {
841             st->codec->extradata = descriptor->extradata;
842             st->codec->extradata_size = descriptor->extradata_size;
843         }
844         if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
845             container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, &descriptor->essence_container_ul);
846             if (st->codec->codec_id == CODEC_ID_NONE)
847                 st->codec->codec_id = container_ul->id;
848             st->codec->width = descriptor->width;
849             st->codec->height = descriptor->height;
850             st->codec->bits_per_sample = descriptor->bits_per_sample; /* Uncompressed */
851         } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
852             container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, &descriptor->essence_container_ul);
853             if (st->codec->codec_id == CODEC_ID_NONE)
854                 st->codec->codec_id = container_ul->id;
855             st->codec->channels = descriptor->channels;
856             st->codec->bits_per_sample = descriptor->bits_per_sample;
857             st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
858             /* TODO: implement CODEC_ID_RAWAUDIO */
859             if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
860                 if (descriptor->bits_per_sample == 24)
861                     st->codec->codec_id = CODEC_ID_PCM_S24LE;
862                 else if (descriptor->bits_per_sample == 32)
863                     st->codec->codec_id = CODEC_ID_PCM_S32LE;
864             } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
865                 if (descriptor->bits_per_sample == 24)
866                     st->codec->codec_id = CODEC_ID_PCM_S24BE;
867                 else if (descriptor->bits_per_sample == 32)
868                     st->codec->codec_id = CODEC_ID_PCM_S32BE;
869             }
870         }
871         if (container_ul && container_ul->wrapping == Clip) {
872             dprintf("stream %d: clip wrapped essence\n", st->index);
873             st->need_parsing = 1;
874         }
875     }
876     return 0;
877 }
878
879 static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
880     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2F,0x00 }, mxf_read_metadata_preface },
881     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_metadata_content_storage },
882     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_metadata_source_package },
883     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_metadata_material_package },
884     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_metadata_sequence },
885     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_metadata_source_clip },
886     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_metadata_multiple_descriptor },
887     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_metadata_generic_descriptor }, /* Generic Sound */
888     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_metadata_generic_descriptor }, /* CDCI */
889     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_metadata_generic_descriptor }, /* RGBA */
890     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_metadata_generic_descriptor }, /* MPEG 2 Video */
891     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_metadata_generic_descriptor }, /* Wave */
892     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_metadata_generic_descriptor }, /* AES3 */
893     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_metadata_track }, /* Static Track */
894     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_metadata_track }, /* Generic Track */
895     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL },
896 };
897
898 static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
899 {
900     MXFContext *mxf = s->priv_data;
901     KLVPacket klv;
902
903     mxf->fc = s;
904     while (!url_feof(&s->pb)) {
905         const MXFMetadataReadTableEntry *function;
906
907         if (klv_read_packet(&klv, &s->pb) < 0) {
908             av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
909             return -1;
910         }
911 #ifdef DEBUG
912         PRINT_KEY("read header", klv.key);
913 #endif
914         if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
915             /* FIXME avoid seek */
916             url_fseek(&s->pb, klv.offset, SEEK_SET);
917             break;
918         }
919
920         for (function = mxf_metadata_read_table; function->read; function++) {
921             if (IS_KLV_KEY(klv.key, function->key)) {
922                 if (function->read(mxf, &klv) < 0) {
923                     av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
924                     return -1;
925                 }
926                 break;
927             }
928         }
929         if (!function->read)
930             url_fskip(&s->pb, klv.length);
931     }
932     return mxf_parse_structural_metadata(mxf);
933 }
934
935 static int mxf_read_close(AVFormatContext *s)
936 {
937     MXFContext *mxf = s->priv_data;
938     int i;
939
940     av_freep(&mxf->packages_refs);
941     av_freep(&mxf->essence_container_data_sets_refs);
942     av_freep(&mxf->essence_containers_uls);
943     for (i = 0; i < mxf->metadata_sets_count; i++) {
944         switch (mxf->metadata_sets[i]->type) {
945         case MultipleDescriptor:
946             av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
947             break;
948         case Sequence:
949             av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
950             break;
951         case SourcePackage:
952         case MaterialPackage:
953             av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
954             break;
955         default:
956             break;
957         }
958         av_freep(&mxf->metadata_sets[i]);
959     }
960     av_freep(&mxf->metadata_sets);
961     return 0;
962 }
963
964 static int mxf_probe(AVProbeData *p) {
965     uint8_t *bufp = p->buf;
966     uint8_t *end = p->buf + p->buf_size;
967
968     if (p->buf_size < sizeof(mxf_header_partition_pack_key))
969         return 0;
970
971     /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
972     end -= sizeof(mxf_header_partition_pack_key);
973     for (; bufp < end; bufp++) {
974         if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
975             return AVPROBE_SCORE_MAX;
976     }
977     return 0;
978 }
979
980 /* rudimentary binary seek */
981 /* XXX: use MXF Index */
982 static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
983 {
984     AVStream *st = s->streams[stream_index];
985     offset_t pos = url_ftell(&s->pb);
986     int64_t seconds;
987     int i;
988
989     if (!s->bit_rate || sample_time < 0)
990         return -1;
991     seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
992     url_fseek(&s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
993     /* sync on KLV essence element */
994     for (i = 0; i < 12 && url_ftell(&s->pb) < s->file_size; i++) {
995         int b = get_byte(&s->pb);
996         if (b == mxf_essence_element_key[0])
997             i = 0;
998         else if (b != mxf_essence_element_key[i])
999             i = -1;
1000     }
1001     if (i == 12) { /* found KLV key */
1002         url_fseek(&s->pb, -12, SEEK_CUR);
1003         av_update_cur_dts(s, st, sample_time);
1004         return 0;
1005     }
1006     url_fseek(&s->pb, pos, SEEK_SET);
1007     return -1;
1008 }
1009
1010 AVInputFormat mxf_demuxer = {
1011     "mxf",
1012     "MXF format",
1013     sizeof(MXFContext),
1014     mxf_probe,
1015     mxf_read_header,
1016     mxf_read_packet,
1017     mxf_read_close,
1018     mxf_read_seek,
1019 };