10l, check container_ul existence
[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 } MXFDescriptor;
106
107 typedef struct MXFPackage {
108     UID uid;
109     enum MXFMetadataSetType type;
110     UID package_uid;
111     UID *tracks_refs;
112     int tracks_count;
113     MXFDescriptor *descriptor; /* only one */
114     UID descriptor_ref;
115 } MXFPackage;
116
117 typedef struct MXFEssenceContainerData {
118     UID uid;
119     enum MXFMetadataSetType type;
120     UID linked_package_uid;
121 } MXFEssenceContainerData;
122
123 typedef struct {
124     UID uid;
125     enum MXFMetadataSetType type;
126 } MXFMetadataSet;
127
128 typedef struct MXFContext {
129     UID *packages_refs;
130     int packages_count;
131     UID *essence_container_data_sets_refs;
132     int essence_container_data_sets_count;
133     UID *essence_containers_uls; /* Universal Labels SMPTE RP224 */
134     int essence_containers_uls_count;
135     UID operational_pattern_ul;
136     UID content_storage_uid;
137     MXFMetadataSet **metadata_sets;
138     int metadata_sets_count;
139     AVFormatContext *fc;
140 } MXFContext;
141
142 typedef struct KLVPacket {
143     UID key;
144     offset_t offset;
145     uint64_t length;
146 } KLVPacket;
147
148 enum MXFWrappingScheme {
149     Frame,
150     Clip,
151 };
152
153 typedef struct MXFCodecUL {
154     UID uid;
155     enum CodecID id;
156     enum MXFWrappingScheme wrapping;
157 } MXFCodecUL;
158
159 typedef struct MXFMetadataReadTableEntry {
160     const UID key;
161     int (*read)(MXFContext *mxf, KLVPacket *klv);
162 } MXFMetadataReadTableEntry;
163
164 /* partial keys to match */
165 static const uint8_t mxf_header_partition_pack_key[]       = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
166 static const uint8_t mxf_essence_element_key[]             = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
167
168 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
169
170 #define PRINT_KEY(x) dprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", \
171                              (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])
172
173 static int64_t klv_decode_ber_length(ByteIOContext *pb)
174 {
175     int64_t size = 0;
176     uint8_t length = get_byte(pb);
177     int type = length >> 7;
178
179     if (type) { /* long form */
180         int bytes_num = length & 0x7f;
181         /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
182         if (bytes_num > 8)
183             return -1;
184         while (bytes_num--)
185             size = size << 8 | get_byte(pb);
186     } else {
187         size = length & 0x7f;
188     }
189     return size;
190 }
191
192 static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
193 {
194     klv->offset = url_ftell(pb);
195     get_buffer(pb, klv->key, 16);
196     klv->length = klv_decode_ber_length(pb);
197     return klv->length == -1 ? -1 : 0;
198 }
199
200 static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
201 {
202     int i;
203
204     for (i = 0; i < s->nb_streams; i++) {
205         MXFTrack *track = s->streams[i]->priv_data;
206          /* SMPTE 379M 7.3 */
207         if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
208             return i;
209     }
210     return -1;
211 }
212
213 static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
214 {
215     KLVPacket klv;
216
217     while (!url_feof(&s->pb)) {
218         if (klv_read_packet(&klv, &s->pb) < 0) {
219             av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
220             return -1;
221         }
222 #ifdef DEBUG
223         PRINT_KEY(klv.key);
224 #endif
225         if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
226             av_get_packet(&s->pb, pkt, klv.length);
227             pkt->stream_index = mxf_get_stream_index(s, &klv);
228             return pkt->stream_index == -1 ? -1 : 0;
229         } else
230             url_fskip(&s->pb, klv.length);
231     }
232     return AVERROR_IO;
233 }
234
235 static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
236 {
237     mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
238     mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
239     mxf->metadata_sets_count++;
240     return 0;
241 }
242
243 static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
244 {
245     ByteIOContext *pb = &mxf->fc->pb;
246     int bytes_read = 0;
247
248     while (bytes_read < klv->length) {
249         int tag = get_be16(pb);
250         int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
251
252         switch (tag) {
253         case 0x3B03:
254             get_buffer(pb, mxf->content_storage_uid, 16);
255             break;
256         case 0x3B09:
257             get_buffer(pb, mxf->operational_pattern_ul, 16);
258             break;
259         case 0x3B0A:
260             mxf->essence_containers_uls_count = get_be32(pb);
261             if (mxf->essence_containers_uls_count >= UINT_MAX / sizeof(UID))
262                 return -1;
263             mxf->essence_containers_uls = av_malloc(mxf->essence_containers_uls_count * sizeof(UID));
264             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
265             get_buffer(pb, mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
266             break;
267         default:
268             url_fskip(pb, size);
269         }
270         bytes_read += size + 4;
271     }
272     return 0;
273 }
274
275 static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
276 {
277     ByteIOContext *pb = &mxf->fc->pb;
278     int bytes_read = 0;
279
280     while (bytes_read < klv->length) {
281         int tag = get_be16(pb);
282         int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
283
284         dprintf("tag 0x%04X, size %d\n", tag, size);
285         switch (tag) {
286         case 0x1901:
287             mxf->packages_count = get_be32(pb);
288             if (mxf->packages_count >= UINT_MAX / sizeof(UID))
289                 return -1;
290             mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
291             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
292             get_buffer(pb, mxf->packages_refs, mxf->packages_count * sizeof(UID));
293             break;
294         case 0x1902:
295             mxf->essence_container_data_sets_count = get_be32(pb);
296             if (mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(UID))
297                 return -1;
298             mxf->essence_container_data_sets_refs = av_malloc(mxf->essence_container_data_sets_count * sizeof(UID));
299             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
300             get_buffer(pb, mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
301             break;
302         default:
303             url_fskip(pb, size);
304         }
305         bytes_read += size + 4;
306     }
307     return 0;
308 }
309
310 static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
311 {
312     ByteIOContext *pb = &mxf->fc->pb;
313     MXFStructuralComponent *source_clip = av_mallocz(sizeof(*source_clip));
314     int bytes_read = 0;
315
316     while (bytes_read < klv->length) {
317         int tag = get_be16(pb);
318         int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
319
320         dprintf("tag 0x%04X, size %d\n", tag, size);
321         switch (tag) {
322         case 0x3C0A:
323             get_buffer(pb, source_clip->uid, 16);
324             break;
325         case 0x0202:
326             source_clip->duration = get_be64(pb);
327             break;
328         case 0x1201:
329             source_clip->start_position = get_be64(pb);
330             break;
331         case 0x1101:
332             /* UMID, only get last 16 bytes */
333             url_fskip(pb, 16);
334             get_buffer(pb, source_clip->source_package_uid, 16);
335             break;
336         case 0x1102:
337             source_clip->source_track_id = get_be32(pb);
338             break;
339         default:
340             url_fskip(pb, size);
341         }
342         bytes_read += size + 4;
343     }
344     source_clip->type = SourceClip;
345     return mxf_add_metadata_set(mxf, source_clip);
346 }
347
348 static int mxf_read_metadata_material_package(MXFContext *mxf, KLVPacket *klv)
349 {
350     ByteIOContext *pb = &mxf->fc->pb;
351     MXFPackage *package = av_mallocz(sizeof(*package));
352     int bytes_read = 0;
353
354     while (bytes_read < klv->length) {
355         int tag = get_be16(pb);
356         int size = get_be16(pb); /* KLV specified by 0x53 */
357
358         switch (tag) {
359         case 0x3C0A:
360             get_buffer(pb, package->uid, 16);
361             break;
362         case 0x4403:
363             package->tracks_count = get_be32(pb);
364             if (package->tracks_count >= UINT_MAX / sizeof(UID))
365                 return -1;
366             package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
367             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
368             get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
369             break;
370         default:
371             url_fskip(pb, size);
372         }
373         bytes_read += size + 4;
374     }
375     package->type = MaterialPackage;
376     return mxf_add_metadata_set(mxf, package);
377 }
378
379 static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
380 {
381     ByteIOContext *pb = &mxf->fc->pb;
382     MXFTrack *track = av_mallocz(sizeof(*track));
383     int bytes_read = 0;
384
385     while (bytes_read < klv->length) {
386         int tag = get_be16(pb);
387         int size = get_be16(pb); /* KLV specified by 0x53 */
388
389         dprintf("tag 0x%04X, size %d\n", tag, size);
390         switch (tag) {
391         case 0x3C0A:
392             get_buffer(pb, track->uid, 16);
393             break;
394         case 0x4801:
395             track->track_id = get_be32(pb);
396             break;
397         case 0x4804:
398             get_buffer(pb, track->track_number, 4);
399             break;
400         case 0x4B01:
401             track->edit_rate.den = get_be32(pb);
402             track->edit_rate.num = get_be32(pb);
403             break;
404         case 0x4803:
405             get_buffer(pb, track->sequence_ref, 16);
406             break;
407         default:
408             url_fskip(pb, size);
409         }
410         bytes_read += size + 4;
411     }
412     track->type = Track;
413     return mxf_add_metadata_set(mxf, track);
414 }
415
416 static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
417 {
418     ByteIOContext *pb = &mxf->fc->pb;
419     MXFSequence *sequence = av_mallocz(sizeof(*sequence));
420     int bytes_read = 0;
421
422     while (bytes_read < klv->length) {
423         int tag = get_be16(pb);
424         int size = get_be16(pb); /* KLV specified by 0x53 */
425
426         dprintf("tag 0x%04X, size %d\n", tag, size);
427         switch (tag) {
428         case 0x3C0A:
429             get_buffer(pb, sequence->uid, 16);
430             break;
431         case 0x0202:
432             sequence->duration = get_be64(pb);
433             break;
434         case 0x0201:
435             get_buffer(pb, sequence->data_definition_ul, 16);
436             break;
437         case 0x1001:
438             sequence->structural_components_count = get_be32(pb);
439             if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
440                 return -1;
441             sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
442             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
443             get_buffer(pb, sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
444             break;
445         default:
446             url_fskip(pb, size);
447         }
448         bytes_read += size + 4;
449     }
450     sequence->type = Sequence;
451     return mxf_add_metadata_set(mxf, sequence);
452 }
453
454 static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
455 {
456     ByteIOContext *pb = &mxf->fc->pb;
457     MXFPackage *package = av_mallocz(sizeof(*package));
458     int bytes_read = 0;
459
460     while (bytes_read < klv->length) {
461         int tag = get_be16(pb);
462         int size = get_be16(pb); /* KLV specified by 0x53 */
463
464         dprintf("tag 0x%04X, size %d\n", tag, size);
465         switch (tag) {
466         case 0x3C0A:
467             get_buffer(pb, package->uid, 16);
468             break;
469         case 0x4403:
470             package->tracks_count = get_be32(pb);
471             if (package->tracks_count >= UINT_MAX / sizeof(UID))
472                 return -1;
473             package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
474             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
475             get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
476             break;
477         case 0x4401:
478             /* UMID, only get last 16 bytes */
479             url_fskip(pb, 16);
480             get_buffer(pb, package->package_uid, 16);
481             break;
482         case 0x4701:
483             get_buffer(pb, package->descriptor_ref, 16);
484             break;
485         default:
486             url_fskip(pb, size);
487         }
488         bytes_read += size + 4;
489     }
490     package->type = SourcePackage;
491     return mxf_add_metadata_set(mxf, package);
492 }
493
494 static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
495 {
496     ByteIOContext *pb = &mxf->fc->pb;
497     MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
498     int bytes_read = 0;
499
500     while (bytes_read < klv->length) {
501         int tag = get_be16(pb);
502         int size = get_be16(pb); /* KLV specified by 0x53 */
503
504         dprintf("tag 0x%04X, size %d\n", tag, size);
505         switch (tag) {
506         case 0x3C0A:
507             get_buffer(pb, descriptor->uid, 16);
508             break;
509         case 0x3F01:
510             descriptor->sub_descriptors_count = get_be32(pb);
511             if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
512                 return -1;
513             descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
514             url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
515             get_buffer(pb, descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
516             break;
517         default:
518             url_fskip(pb, size);
519         }
520         bytes_read += size + 4;
521     }
522     descriptor->type = MultipleDescriptor;
523     return mxf_add_metadata_set(mxf, descriptor);
524 }
525
526 static void mxf_read_metadata_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
527 {
528     int code;
529
530     do {
531         code = get_byte(pb);
532         dprintf("pixel layout: code 0x%x\n", code);
533         switch (code) {
534         case 0x52: /* R */
535             descriptor->bits_per_sample += get_byte(pb);
536             break;
537         case 0x47: /* G */
538             descriptor->bits_per_sample += get_byte(pb);
539             break;
540         case 0x42: /* B */
541             descriptor->bits_per_sample += get_byte(pb);
542             break;
543         default:
544             get_byte(pb);
545         }
546     } while (code != 0); /* SMPTE 377M E.2.46 */
547 }
548
549 static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
550 {
551     ByteIOContext *pb = &mxf->fc->pb;
552     MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
553     int bytes_read = 0;
554
555     while (bytes_read < klv->length) {
556         int tag = get_be16(pb);
557         int size = get_be16(pb); /* KLV specified by 0x53 */
558
559         dprintf("tag 0x%04X, size %d\n", tag, size);
560         switch (tag) {
561         case 0x3C0A:
562             get_buffer(pb, descriptor->uid, 16);
563             break;
564         case 0x3004:
565             get_buffer(pb, descriptor->essence_container_ul, 16);
566             break;
567         case 0x3006:
568             descriptor->linked_track_id = get_be32(pb);
569             break;
570         case 0x3201: /* PictureEssenceCoding */
571             get_buffer(pb, descriptor->essence_codec_ul, 16);
572             break;
573         case 0x3203:
574             descriptor->width = get_be32(pb);
575             break;
576         case 0x3202:
577             descriptor->height = get_be32(pb);
578             break;
579         case 0x320E:
580             descriptor->aspect_ratio.num = get_be32(pb);
581             descriptor->aspect_ratio.den = get_be32(pb);
582             break;
583         case 0x3D03:
584             descriptor->sample_rate.num = get_be32(pb);
585             descriptor->sample_rate.den = get_be32(pb);
586             break;
587         case 0x3D06: /* SoundEssenceCompression */
588             get_buffer(pb, descriptor->essence_codec_ul, 16);
589             break;
590         case 0x3D07:
591             descriptor->channels = get_be32(pb);
592             break;
593         case 0x3D01:
594             descriptor->bits_per_sample = get_be32(pb);
595             break;
596         case 0x3401:
597             mxf_read_metadata_pixel_layout(pb, descriptor);
598             break;
599         default:
600             url_fskip(pb, size);
601         }
602         bytes_read += size + 4;
603     }
604     descriptor->type = Descriptor;
605     return mxf_add_metadata_set(mxf, descriptor);
606 }
607
608 /* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
609 static const UID picture_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 };
610 static const UID sound_essence_track_ul   = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 };
611
612 static const MXFCodecUL mxf_codec_uls[] = {
613     /* PictureEssenceCoding */
614     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@ML I-Frame */
615     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@HL Long GoP */
616     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MP@ML Long GoP */
617     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@ML Long GoP */
618     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MP@HL Long GoP */
619     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x03 },      CODEC_ID_MPEG4, Frame }, /* XDCAM proxy_pal030926.mxf */
620     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x04 },      CODEC_ID_MPEG4, Frame }, /* XDCAM Proxy C0023S01.mxf */
621     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, CODEC_ID_MPEG2VIDEO, Frame }, /* D-10 30Mbps PAL */
622     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, CODEC_ID_MPEG2VIDEO, Frame }, /* D-10 50Mbps PAL */
623     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DVCPRO50 PAL */
624     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DVCPRO25 PAL */
625     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DV25 IEC PAL */
626   //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },     CODEC_ID_JPEG2K, Frame }, /* JPEG2000 Codestream */
627     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 },   CODEC_ID_RAWVIDEO, Frame }, /* Uncompressed */
628     /* SoundEssenceCompression */
629     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* Uncompressed */
630     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE, Frame },
631     { { 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 */
632     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_PCM_ALAW, Frame },
633     { { 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 */
634     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 },        CODEC_ID_AC3, Frame },
635     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 },        CODEC_ID_MP2, Frame }, /* MP2 or MP3 */
636   //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 },    CODEC_ID_DOLBY_E, Frame }, /* Dolby-E */
637     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
638 };
639
640 static const MXFCodecUL mxf_picture_essence_container_uls[] = {
641     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MPEG-ES Frame wrapped */
642     { { 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 */
643     { { 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 */
644     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
645 };
646
647 static const MXFCodecUL mxf_sound_essence_container_uls[] = {
648     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* BWF Frame wrapped */
649     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* AES Frame wrapped */
650     { { 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 */
651     { { 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 */
652     { { 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 */
653   //{ { 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 */
654     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
655 };
656
657 static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
658 {
659     while (uls->id != CODEC_ID_NONE) {
660         if(!memcmp(uls->uid, *uid, 16))
661             break;
662         uls++;
663     }
664     return uls;
665 }
666
667 static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref)
668 {
669     int i;
670
671     if (!strong_ref)
672         return NULL;
673     for (i = 0; i < mxf->metadata_sets_count; i++) {
674         if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16)) {
675             return mxf->metadata_sets[i];
676         }
677     }
678     return NULL;
679 }
680
681 static int mxf_parse_structural_metadata(MXFContext *mxf)
682 {
683     MXFPackage *material_package = NULL;
684     MXFPackage *source_package = NULL;
685     MXFPackage *temp_package = NULL;
686     int i, j, k;
687
688     dprintf("metadata sets count %d\n", mxf->metadata_sets_count);
689     /* TODO: handle multiple material packages (OP3x) */
690     for (i = 0; i < mxf->packages_count; i++) {
691         if (!(temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i]))) {
692             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve package strong ref\n");
693             return -1;
694         }
695         if (temp_package->type == MaterialPackage) {
696             material_package = temp_package;
697             break;
698         }
699     }
700     if (!material_package) {
701         av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
702         return -1;
703     }
704
705     for (i = 0; i < material_package->tracks_count; i++) {
706         MXFTrack *material_track = NULL;
707         MXFTrack *source_track = NULL;
708         MXFTrack *temp_track = NULL;
709         MXFDescriptor *descriptor = NULL;
710         MXFStructuralComponent *component = NULL;
711         const MXFCodecUL *codec_ul = NULL;
712         const MXFCodecUL *container_ul = NULL;
713         AVStream *st;
714
715         if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i]))) {
716             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
717             continue;
718         }
719
720         if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref))) {
721             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
722             return -1;
723         }
724
725         /* TODO: handle multiple source clips */
726         for (j = 0; j < material_track->sequence->structural_components_count; j++) {
727             /* TODO: handle timecode component */
728             component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j]);
729             if (!component || component->type != SourceClip)
730                 continue;
731
732             for (k = 0; k < mxf->packages_count; k++) {
733                 if (!(temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k]))) {
734                     av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
735                     return -1;
736                 }
737                 if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
738                     source_package = temp_package;
739                     break;
740                 }
741             }
742             if (!source_package) {
743                 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
744                 break;
745             }
746             for (k = 0; k < source_package->tracks_count; k++) {
747                 if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k]))) {
748                     av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
749                     return -1;
750                 }
751                 if (temp_track->track_id == component->source_track_id) {
752                     source_track = temp_track;
753                     break;
754                 }
755             }
756             if (!source_track) {
757                 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
758                 break;
759             }
760         }
761         if (!source_track)
762             continue;
763
764         st = av_new_stream(mxf->fc, source_track->track_id);
765         st->priv_data = source_track;
766         st->duration = component->duration;
767         if (st->duration == -1)
768             st->duration = AV_NOPTS_VALUE;
769         st->start_time = component->start_position;
770         av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
771
772         if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref))) {
773             av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
774             return -1;
775         }
776
777 #ifdef DEBUG
778         PRINT_KEY(source_track->sequence->data_definition_ul);
779 #endif
780         if (!memcmp(source_track->sequence->data_definition_ul, picture_essence_track_ul, 16))
781             st->codec->codec_type = CODEC_TYPE_VIDEO;
782         else if (!memcmp(source_track->sequence->data_definition_ul, sound_essence_track_ul, 16))
783             st->codec->codec_type = CODEC_TYPE_AUDIO;
784         else
785             st->codec->codec_type = CODEC_TYPE_DATA;
786
787         source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref);
788         if (source_package->descriptor) {
789             if (source_package->descriptor->type == MultipleDescriptor) {
790                 for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
791                     MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j]);
792
793                     if (!sub_descriptor) {
794                         av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
795                         continue;
796                     }
797                     if (sub_descriptor->linked_track_id == source_track->track_id) {
798                         descriptor = sub_descriptor;
799                         break;
800                     }
801                 }
802             } else
803                 descriptor = source_package->descriptor;
804         }
805         if (!descriptor) {
806             av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
807             continue;
808         }
809 #ifdef DEBUG
810         PRINT_KEY(descriptor->essence_codec_ul);
811         PRINT_KEY(descriptor->essence_container_ul);
812 #endif
813         /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
814         codec_ul = mxf_get_codec_ul(mxf_codec_uls, &descriptor->essence_codec_ul);
815         st->codec->codec_id = codec_ul->id;
816         if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
817             container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, &descriptor->essence_container_ul);
818             if (st->codec->codec_id == CODEC_ID_NONE)
819                 st->codec->codec_id = container_ul->id;
820             st->codec->width = descriptor->width;
821             st->codec->height = descriptor->height;
822             st->codec->bits_per_sample = descriptor->bits_per_sample; /* Uncompressed */
823         } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
824             container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, &descriptor->essence_container_ul);
825             if (st->codec->codec_id == CODEC_ID_NONE)
826                 st->codec->codec_id = container_ul->id;
827             st->codec->channels = descriptor->channels;
828             st->codec->bits_per_sample = descriptor->bits_per_sample;
829             st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
830             /* TODO: implement CODEC_ID_RAWAUDIO */
831             if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
832                 if (descriptor->bits_per_sample == 24)
833                     st->codec->codec_id = CODEC_ID_PCM_S24LE;
834                 else if (descriptor->bits_per_sample == 32)
835                     st->codec->codec_id = CODEC_ID_PCM_S32LE;
836             } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
837                 if (descriptor->bits_per_sample == 24)
838                     st->codec->codec_id = CODEC_ID_PCM_S24BE;
839                 else if (descriptor->bits_per_sample == 32)
840                     st->codec->codec_id = CODEC_ID_PCM_S32BE;
841             }
842         }
843         if (container_ul && container_ul->wrapping == Clip) {
844             dprintf("stream %d: clip wrapped essence\n", st->index);
845             st->need_parsing = 1;
846         }
847     }
848     return 0;
849 }
850
851 static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
852     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2F,0x00 }, mxf_read_metadata_preface },
853     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_metadata_content_storage },
854     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_metadata_source_package },
855     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_metadata_material_package },
856     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_metadata_sequence },
857     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_metadata_source_clip },
858     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_metadata_multiple_descriptor },
859     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_metadata_generic_descriptor }, /* Generic Sound */
860     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_metadata_generic_descriptor }, /* CDCI */
861     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_metadata_generic_descriptor }, /* RGBA */
862     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_metadata_generic_descriptor }, /* MPEG 2 Video */
863     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_metadata_generic_descriptor }, /* Wave */
864     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_metadata_generic_descriptor }, /* AES3 */
865     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_metadata_track }, /* Static Track */
866     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_metadata_track }, /* Generic Track */
867     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL },
868 };
869
870 static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
871 {
872     MXFContext *mxf = s->priv_data;
873     KLVPacket klv;
874
875     mxf->fc = s;
876     while (!url_feof(&s->pb)) {
877         const MXFMetadataReadTableEntry *function;
878
879         if (klv_read_packet(&klv, &s->pb) < 0) {
880             av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
881             return -1;
882         }
883 #ifdef DEBUG
884         PRINT_KEY(klv.key);
885 #endif
886         if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
887             /* FIXME avoid seek */
888             url_fseek(&s->pb, klv.offset, SEEK_SET);
889             break;
890         }
891
892         for (function = mxf_metadata_read_table; function->read; function++) {
893             if (IS_KLV_KEY(klv.key, function->key)) {
894                 if (function->read(mxf, &klv) < 0) {
895                     av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
896                     return -1;
897                 }
898                 break;
899             }
900         }
901         if (!function->read)
902             url_fskip(&s->pb, klv.length);
903     }
904     return mxf_parse_structural_metadata(mxf);
905 }
906
907 static int mxf_read_close(AVFormatContext *s)
908 {
909     MXFContext *mxf = s->priv_data;
910     int i;
911
912     av_freep(&mxf->packages_refs);
913     av_freep(&mxf->essence_container_data_sets_refs);
914     av_freep(&mxf->essence_containers_uls);
915     for (i = 0; i < mxf->metadata_sets_count; i++) {
916         switch (mxf->metadata_sets[i]->type) {
917         case MultipleDescriptor:
918             av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
919             break;
920         case Sequence:
921             av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
922             break;
923         case SourcePackage:
924         case MaterialPackage:
925             av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
926             break;
927         default:
928             break;
929         }
930         av_freep(&mxf->metadata_sets[i]);
931     }
932     av_freep(&mxf->metadata_sets);
933     return 0;
934 }
935
936 static int mxf_probe(AVProbeData *p) {
937     uint8_t *bufp = p->buf;
938     uint8_t *end = p->buf + p->buf_size;
939
940     if (p->buf_size < sizeof(mxf_header_partition_pack_key))
941         return 0;
942
943     /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
944     end -= sizeof(mxf_header_partition_pack_key);
945     for (; bufp < end; bufp++) {
946         if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
947             return AVPROBE_SCORE_MAX;
948     }
949     return 0;
950 }
951
952
953 AVInputFormat mxf_demuxer = {
954     "mxf",
955     "MXF format",
956     sizeof(MXFContext),
957     mxf_probe,
958     mxf_read_header,
959     mxf_read_packet,
960     mxf_read_close,
961     NULL,
962 };