Browse code

MXF demuxer

Originally committed as revision 5826 to svn://svn.ffmpeg.org/ffmpeg/trunk

Baptiste Coudurier authored on 2006/07/25 23:30:14
Showing 6 changed files
... ...
@@ -51,6 +51,7 @@ version <next>
51 51
 - GXF demuxer
52 52
 - Chinese AVS decoder
53 53
 - GXF muxer
54
+- MXF demuxer
54 55
 
55 56
 version 0.4.9-pre1:
56 57
 
... ...
@@ -198,6 +198,7 @@ Muxers/Demuxers:
198 198
   img2.c                                Michael Niedermayer
199 199
   mov.c                                 Francois Revol, Michael Niedermayer
200 200
   mpegts*                               Mans Rullgard
201
+  mxf.c                                 Baptiste Coudurier
201 202
   nsvdec.c                              Francois Revol
202 203
   nut.c                                 Alex Beregszaszi
203 204
   nuv.c                                 Reimar Doeffinger
... ...
@@ -75,6 +75,7 @@ OBJS-$(CONFIG_MPEGPS_DEMUXER)            += mpeg.o
75 75
 OBJS-$(CONFIG_MPEGTS_DEMUXER)            += mpegts.o
76 76
 OBJS-$(CONFIG_MPEGTS_MUXER)              += mpegtsenc.o
77 77
 OBJS-$(CONFIG_MPJPEG_MUXER)              += mpjpeg.o
78
+OBJS-$(CONFIG_MXF_DEMUXER)               += mxf.o
78 79
 OBJS-$(CONFIG_NSV_DEMUXER)               += nsvdec.o riff.o
79 80
 OBJS-$(CONFIG_NUT_DEMUXER)               += nut.o riff.o
80 81
 OBJS-$(CONFIG_NUT_MUXER)                 += nut.o riff.o
... ...
@@ -240,6 +240,9 @@ void av_register_all(void)
240 240
 #ifdef CONFIG_MPJPEG_MUXER
241 241
     av_register_output_format(&mpjpeg_muxer);
242 242
 #endif
243
+#ifdef CONFIG_MXF_DEMUXER
244
+    av_register_input_format(&mxf_demuxer);
245
+#endif
243 246
 #ifdef CONFIG_NSV_DEMUXER
244 247
     av_register_input_format(&nsv_demuxer);
245 248
 #endif
... ...
@@ -69,6 +69,7 @@ extern AVInputFormat mpegps_demuxer;
69 69
 extern AVInputFormat mpegts_demuxer;
70 70
 extern AVOutputFormat mpegts_muxer;
71 71
 extern AVOutputFormat mpjpeg_muxer;
72
+extern AVInputFormat mxf_demuxer;
72 73
 extern AVInputFormat nsv_demuxer;
73 74
 extern AVInputFormat nut_demuxer;
74 75
 extern AVOutputFormat nut_muxer;
75 76
new file mode 100644
... ...
@@ -0,0 +1,508 @@
0
+/*
1
+ * MXF demuxer.
2
+ * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+ */
18
+
19
+/*
20
+ * References
21
+ * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
22
+ * SMPTE 377M MXF File Format Specifications
23
+ * SMPTE 378M Operational Pattern 1a
24
+ * SMPTE 379M MXF Generic Container
25
+ * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
26
+ * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container
27
+ * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container
28
+ *
29
+ * Principle
30
+ * Search for Track numbers which will identify essence element KLV packets.
31
+ * Search for SourcePackage which define tracks which contains Track numbers.
32
+ * Material Package tracks does not contain Tracks numbers.
33
+ * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
34
+ * Assign Descriptors to correct Tracks.
35
+ *
36
+ * Preliminary demuxer, only OP1A supported and some files might not work at all.
37
+ */
38
+
39
+//#define DEBUG
40
+
41
+#include "avformat.h"
42
+#include "dsputil.h"
43
+#include "riff.h"
44
+
45
+typedef struct {
46
+    AVStream *stream;
47
+    uint8_t track_uid[16];
48
+    uint8_t sequence_uid[16];
49
+    int track_id;
50
+    int track_number;
51
+} MXFTrack;
52
+
53
+typedef struct {
54
+    DECLARE_ALIGNED_16(uint8_t, essence_container[16]);
55
+    DECLARE_ALIGNED_16(uint8_t, essence_compression[16]);
56
+    enum CodecType codec_type;
57
+    AVRational sample_rate;
58
+    AVRational aspect_ratio;
59
+    int width;
60
+    int height;
61
+    int channels;
62
+    int bits_per_sample;
63
+    int block_align;
64
+    int linked_track_id;
65
+    int kind;
66
+} MXFDescriptor;
67
+
68
+typedef struct {
69
+    AVFormatContext *fc;
70
+    MXFTrack *tracks;
71
+    MXFDescriptor *descriptors;
72
+    int descriptors_count;
73
+    int tracks_count;
74
+} MXFContext;
75
+
76
+typedef struct {
77
+    DECLARE_ALIGNED_16(uint8_t, key[16]);
78
+    offset_t offset;
79
+    uint64_t length;
80
+} KLVPacket;
81
+
82
+static const uint8_t mxf_metadata_source_package_key[]           = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x37, 0x00 };
83
+static const uint8_t mxf_metadata_sequence_key[]                 = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F, 0x00 };
84
+static const uint8_t mxf_metadata_generic_sound_descriptor_key[] = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x42, 0x00 };
85
+static const uint8_t mxf_metadata_cdci_descriptor_key[]          = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x28, 0x00 };
86
+static const uint8_t mxf_metadata_mpegvideo_descriptor_key[]     = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x51, 0x00 };
87
+static const uint8_t mxf_metadata_wave_descriptor_key[]          = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x48, 0x00 };
88
+static const uint8_t mxf_metadata_track_key[]                    = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x3b, 0x00 };
89
+static const uint8_t mxf_header_partition_pack_key[]             = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01, 0x01, 0x02 };
90
+static const uint8_t mxf_essence_element_key[]                   = { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x02, 0x01 };
91
+
92
+#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
93
+
94
+#define PRINT_KEY(x) \
95
+do { \
96
+    int iterpk; \
97
+    for (iterpk = 0; iterpk < 16; iterpk++) { \
98
+        av_log(NULL, AV_LOG_DEBUG, "%02X ", x[iterpk]); \
99
+    } \
100
+    av_log(NULL, AV_LOG_DEBUG, "\n"); \
101
+} while (0); \
102
+
103
+static int64_t klv_decode_ber_length(ByteIOContext *pb)
104
+{
105
+    int64_t size = 0;
106
+    uint8_t length = get_byte(pb);
107
+    int type = length >> 7;
108
+
109
+    if (type) { /* long form */
110
+        int bytes_num = length & 0x7f;
111
+        /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
112
+        if (bytes_num > 8)
113
+            return -1;
114
+        while (bytes_num--)
115
+            size = size << 8 | get_byte(pb);
116
+    } else {
117
+        size = length & 0x7f;
118
+    }
119
+    return size;
120
+}
121
+
122
+static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
123
+{
124
+    klv->offset = url_ftell(pb);
125
+    get_buffer(pb, klv->key, 16);
126
+    klv->length = klv_decode_ber_length(pb);
127
+    if (klv->length == -1)
128
+        return -1;
129
+    else
130
+        return 0;
131
+}
132
+
133
+static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
134
+{
135
+    int id = BE_32(klv->key + 12); /* SMPTE 379M 7.3 */
136
+    int i;
137
+
138
+    for (i = 0; i < s->nb_streams; i++) {
139
+        if (s->streams[i]->id == id)
140
+            return i;
141
+    }
142
+    return -1;
143
+}
144
+
145
+static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
146
+{
147
+    KLVPacket klv;
148
+
149
+    while (!url_feof(&s->pb)) {
150
+        if (klv_read_packet(&klv, &s->pb) < 0)
151
+            return -1;
152
+        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
153
+            av_get_packet(&s->pb, pkt, klv.length);
154
+            pkt->stream_index = mxf_get_stream_index(s, &klv);
155
+            if (pkt->stream_index == -1)
156
+                return -1;
157
+            return 0;
158
+        } else
159
+            url_fskip(&s->pb, klv.length);
160
+    }
161
+    return AVERROR_IO;
162
+}
163
+
164
+static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
165
+{
166
+    ByteIOContext *pb = &mxf->fc->pb;
167
+    AVRational time_base = (AVRational){0, 0};
168
+    uint8_t sequence_uid[16];
169
+    uint8_t track_uid[16];
170
+    int track_number = 0;
171
+    int track_id = 0;
172
+    int bytes_read = 0;
173
+    int i;
174
+
175
+    while (bytes_read < klv->length) {
176
+        int tag = get_be16(pb);
177
+        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
178
+
179
+        switch (tag) {
180
+        case 0x4801:
181
+            track_id = get_be32(pb);
182
+            break;
183
+        case 0x4804:
184
+            track_number = get_be32(pb);
185
+            break;
186
+        case 0x4B01:
187
+            time_base.den = get_be32(pb);
188
+            time_base.num = get_be32(pb);
189
+            break;
190
+        case 0x4803:
191
+            get_buffer(pb, sequence_uid, 16);
192
+            break;
193
+        case 0x3C0A:
194
+            get_buffer(pb, track_uid, 16);
195
+            break;
196
+        default:
197
+            url_fskip(pb, size);
198
+        }
199
+        bytes_read += size + 4;
200
+    }
201
+    for (i = 0; i < mxf->tracks_count; i++)
202
+        if (!memcmp(track_uid, mxf->tracks[i].track_uid, 16)) {
203
+            mxf->tracks[i].track_id = track_id;
204
+            mxf->tracks[i].track_number = track_number;
205
+            mxf->tracks[i].stream->time_base = time_base;
206
+            mxf->tracks[i].stream->id = track_number;
207
+            memcpy(mxf->tracks[i].sequence_uid, sequence_uid, 16);
208
+        }
209
+    return bytes_read;
210
+}
211
+
212
+static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
213
+{
214
+    ByteIOContext *pb = &mxf->fc->pb;
215
+    uint8_t sequence_uid[16];
216
+    uint8_t data_definition[16];
217
+    uint64_t duration = 0;
218
+    int bytes_read = 0;
219
+    int i;
220
+
221
+    while (bytes_read < klv->length) {
222
+        int tag = get_be16(pb);
223
+        int size = get_be16(pb); /* KLV specified by 0x53 */
224
+
225
+        switch (tag) {
226
+        case 0x3C0A:
227
+            get_buffer(pb, sequence_uid, 16);
228
+            break;
229
+        case 0x0202:
230
+            duration = get_be64(pb);
231
+            break;
232
+        case 0x0201:
233
+            get_buffer(pb, data_definition, 16);
234
+            break;
235
+        default:
236
+            url_fskip(pb, size);
237
+        }
238
+        bytes_read += size + 4;
239
+    }
240
+
241
+    for (i = 0; i < mxf->tracks_count; i++)
242
+        if (!memcmp(sequence_uid, mxf->tracks[i].sequence_uid, 16)) {
243
+            mxf->tracks[i].stream->duration = duration;
244
+            if (data_definition[11] == 0x02 && data_definition[12] == 0x01)
245
+                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_VIDEO;
246
+            else if (data_definition[11] == 0x02 && data_definition[12] == 0x02)
247
+                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_AUDIO;
248
+            else if (data_definition[11] == 0x01) /* SMPTE 12M Time Code track */
249
+                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_DATA;
250
+        }
251
+    return bytes_read;
252
+}
253
+
254
+static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
255
+{
256
+    ByteIOContext *pb = &mxf->fc->pb;
257
+    int tracks_count;
258
+    int bytes_read = 0;
259
+    int i;
260
+
261
+    while (bytes_read < klv->length) {
262
+        int tag = get_be16(pb);
263
+        int size = get_be16(pb); /* KLV specified by 0x53 */
264
+
265
+        switch (tag) {
266
+        case 0x4403:
267
+            tracks_count = get_be32(pb);
268
+            if(tracks_count >= UINT_MAX / sizeof(*mxf->tracks) ||
269
+               tracks_count >= UINT_MAX / sizeof(*mxf->descriptors))
270
+                return -1;
271
+            mxf->tracks_count += tracks_count; /* op2a contains multiple source packages */
272
+            mxf->tracks = av_realloc(mxf->tracks, mxf->tracks_count * sizeof(*mxf->tracks));
273
+            mxf->descriptors = av_realloc(mxf->descriptors, mxf->tracks_count * sizeof(*mxf->descriptors));
274
+            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
275
+            for (i = mxf->tracks_count - tracks_count; i < mxf->tracks_count; i++) {
276
+                mxf->tracks[i].stream = av_new_stream(mxf->fc, 0);
277
+                get_buffer(pb, mxf->tracks[i].track_uid, 16);
278
+            }
279
+            break;
280
+        default:
281
+            url_fskip(pb, size);
282
+        }
283
+        bytes_read += size + 4;
284
+    }
285
+    return bytes_read;
286
+}
287
+
288
+static int mxf_read_metadata_descriptor(MXFContext *mxf, KLVPacket *klv)
289
+{
290
+    ByteIOContext *pb = &mxf->fc->pb;
291
+    MXFDescriptor *desc;
292
+    int bytes_read = 0;
293
+
294
+    if (mxf->descriptors_count == mxf->tracks_count)
295
+        return -1;
296
+    desc = &mxf->descriptors[mxf->descriptors_count++];
297
+    desc->kind = klv->key[14];
298
+    desc->linked_track_id = -1;
299
+
300
+    while (bytes_read < klv->length) {
301
+        int tag = get_be16(pb);
302
+        int size = get_be16(pb); /* KLV specified by 0x53 */
303
+
304
+        switch (tag) {
305
+        case 0x3004:
306
+            get_buffer(pb, desc->essence_container, 16);
307
+            break;
308
+        case 0x3006:
309
+            desc->linked_track_id = get_be32(pb);
310
+            break;
311
+        case 0x3201: /* PictureEssenceCoding */
312
+            desc->codec_type = CODEC_TYPE_VIDEO;
313
+            get_buffer(pb, desc->essence_compression, 16);
314
+            break;
315
+        case 0x3203:
316
+            desc->width = get_be32(pb);
317
+            break;
318
+        case 0x3202:
319
+            desc->height = get_be32(pb);
320
+            break;
321
+        case 0x320E:
322
+            desc->aspect_ratio.num = get_be32(pb);
323
+            desc->aspect_ratio.den = get_be32(pb);
324
+            break;
325
+        case 0x3D0A:
326
+            desc->block_align = get_be16(pb);
327
+            break;
328
+        case 0x3D03:
329
+            desc->sample_rate.num = get_be32(pb);
330
+            desc->sample_rate.den = get_be32(pb);
331
+            break;
332
+        case 0x3D06: /* SoundEssenceCompression */
333
+            desc->codec_type = CODEC_TYPE_AUDIO;
334
+            get_buffer(pb, desc->essence_compression, 16);
335
+            break;
336
+        case 0x3D07:
337
+            desc->channels = get_be32(pb);
338
+            break;
339
+        case 0x3D01:
340
+            desc->bits_per_sample = get_be32(pb);
341
+            break;
342
+        default:
343
+            url_fskip(pb, size);
344
+        }
345
+        bytes_read += size + 4;
346
+    }
347
+    return bytes_read;
348
+}
349
+
350
+/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
351
+static const CodecTag mxf_sound_essence_labels[] = {
352
+    { CODEC_ID_PCM_S16LE, 0x01000000 },/* Uncompressed Sound Coding */
353
+    { CODEC_ID_PCM_S16LE, 0x017F0000 },/* Uncompressed Sound Coding */
354
+    { CODEC_ID_PCM_S16BE, 0x017E0000 },/* Uncompressed Sound Coding Big Endian*/
355
+    { CODEC_ID_PCM_ALAW,  0x02030101 },
356
+    { CODEC_ID_AC3,       0x02030201 },
357
+  //{ CODEC_ID_MP1,       0x02030104 },
358
+    { CODEC_ID_MP2,       0x02030105 },/* MP2 or MP3 */
359
+  //{ CODEC_ID_MP2,       0x02030106 },/* MPEG-2 Layer 1 */
360
+  //{ CODEC_ID_???,       0x0203010C },/* Dolby E */
361
+  //{ CODEC_ID_???,       0x02030301 },/* MPEG-2 AAC */
362
+    { 0, 0 },
363
+};
364
+
365
+static const CodecTag mxf_picture_essence_labels[] = {
366
+    { CODEC_ID_RAWVIDEO,   0x0100 },
367
+    { CODEC_ID_MPEG2VIDEO, 0x0201 },
368
+    { CODEC_ID_DVVIDEO,    0x0202 },
369
+  //{ CODEC_ID_???,        0x0207 },/* D-11 HDCAM */
370
+    { 0, 0 },
371
+};
372
+
373
+static const CodecTag mxf_container_picture_labels[] = {
374
+    { CODEC_ID_MPEG2VIDEO, 0x0201 }, /* D-10 Mapping */
375
+    { CODEC_ID_DVVIDEO,    0x0202 }, /* DV Mapping */
376
+  //{ CODEC_ID_???,        0x0203 }, /* HDCAM D-11 Mapping */
377
+    { CODEC_ID_MPEG2VIDEO, 0x0204 }, /* MPEG ES Mapping */
378
+};
379
+
380
+static const CodecTag mxf_container_sound_labels[] = {
381
+  //{ CODEC_ID_PCM_S16??,  0x0201 }, /* D-10 Mapping */
382
+    { CODEC_ID_MP2,        0x0204 }, /* MPEG ES Mapping */
383
+    { CODEC_ID_PCM_S16LE,  0x0206 }, /* AES BWF Mapping */
384
+    { CODEC_ID_PCM_ALAW,   0x020A },
385
+    { 0, 0 },
386
+};
387
+
388
+static void mxf_resolve_track_descriptor(MXFContext *mxf)
389
+{
390
+    uint32_t container_label;
391
+    uint32_t essence_label;
392
+    int i, j;
393
+
394
+    for (i = 0; i < mxf->descriptors_count; i++) {
395
+        for (j = 0; j < mxf->tracks_count; j++) {
396
+            AVStream *st = mxf->tracks[j].stream;
397
+            MXFDescriptor *desc = &mxf->descriptors[i];
398
+
399
+            if ((desc->linked_track_id == -1 && st->codec->codec_type == desc->codec_type)
400
+                || desc->linked_track_id == mxf->tracks[j].track_id) {
401
+                if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
402
+                    st->codec->channels = desc->channels;
403
+                    st->codec->bits_per_sample = desc->bits_per_sample;
404
+                    st->codec->block_align = desc->block_align;
405
+                    st->codec->sample_rate = desc->sample_rate.num / desc->sample_rate.den;
406
+
407
+                    container_label = BE_16(desc->essence_container + 12);
408
+                    essence_label = BE_32(desc->essence_compression + 11);
409
+                    st->codec->codec_id = codec_get_id(mxf_sound_essence_labels, essence_label);
410
+                    if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
411
+                        if (desc->bits_per_sample == 24)
412
+                            st->codec->codec_id = CODEC_ID_PCM_S24LE;
413
+                        else if (desc->bits_per_sample == 32)
414
+                            st->codec->codec_id = CODEC_ID_PCM_S32LE;
415
+                    }
416
+                    if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
417
+                        if (desc->bits_per_sample == 24)
418
+                            st->codec->codec_id = CODEC_ID_PCM_S24BE;
419
+                        else if (desc->bits_per_sample == 32)
420
+                            st->codec->codec_id = CODEC_ID_PCM_S32BE;
421
+                    }
422
+                    if (!st->codec->codec_id)
423
+                        st->codec->codec_id = codec_get_id(mxf_container_sound_labels, container_label);
424
+
425
+                } else if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
426
+                    st->codec->width = desc->width;
427
+                    st->codec->height = desc->height;
428
+
429
+                    container_label = BE_16(desc->essence_container + 12);
430
+                    essence_label = BE_16(desc->essence_compression + 11);
431
+                    st->codec->codec_id = codec_get_id(mxf_picture_essence_labels, essence_label);
432
+                    if (!st->codec->codec_id)
433
+                        st->codec->codec_id = codec_get_id(mxf_container_picture_labels, container_label);
434
+                }
435
+            }
436
+        }
437
+    }
438
+}
439
+
440
+static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
441
+{
442
+    MXFContext *mxf = s->priv_data;
443
+    KLVPacket klv;
444
+    int ret = 0;
445
+
446
+    mxf->fc = s;
447
+    while (!url_feof(&s->pb)) {
448
+        if (klv_read_packet(&klv, &s->pb) < 0)
449
+            return -1;
450
+        if (IS_KLV_KEY(klv.key, mxf_metadata_track_key))
451
+            ret = mxf_read_metadata_track(mxf, &klv);
452
+        else if (IS_KLV_KEY(klv.key, mxf_metadata_source_package_key))
453
+            ret = mxf_read_metadata_source_package(mxf, &klv);
454
+        else if (IS_KLV_KEY(klv.key, mxf_metadata_sequence_key))
455
+            ret = mxf_read_metadata_sequence(mxf, &klv);
456
+        else if (IS_KLV_KEY(klv.key, mxf_metadata_wave_descriptor_key))
457
+            ret = mxf_read_metadata_descriptor(mxf, &klv);
458
+        else if (IS_KLV_KEY(klv.key, mxf_metadata_mpegvideo_descriptor_key))
459
+            ret = mxf_read_metadata_descriptor(mxf, &klv);
460
+        else if (IS_KLV_KEY(klv.key, mxf_metadata_cdci_descriptor_key))
461
+            ret = mxf_read_metadata_descriptor(mxf, &klv);
462
+        else if (IS_KLV_KEY(klv.key, mxf_metadata_generic_sound_descriptor_key))
463
+            ret = mxf_read_metadata_descriptor(mxf, &klv);
464
+        else if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
465
+            /* FIXME avoid seek */
466
+            url_fseek(&s->pb, klv.offset, SEEK_SET);
467
+            break;
468
+        } else
469
+            url_fskip(&s->pb, klv.length);
470
+        if (ret < 0)
471
+            return ret;
472
+    }
473
+    mxf_resolve_track_descriptor(mxf);
474
+    return 0;
475
+}
476
+
477
+static int mxf_read_close(AVFormatContext *s)
478
+{
479
+    MXFContext *mxf = s->priv_data;
480
+
481
+    av_freep(&mxf->tracks);
482
+    av_freep(&mxf->descriptors);
483
+    return 0;
484
+}
485
+
486
+static int mxf_probe(AVProbeData *p) {
487
+    /* KLV packet describing MXF header partition pack */
488
+    if (p->buf_size < sizeof(mxf_header_partition_pack_key))
489
+        return 0;
490
+
491
+    if (IS_KLV_KEY(p->buf, mxf_header_partition_pack_key))
492
+        return AVPROBE_SCORE_MAX;
493
+    else
494
+        return 0;
495
+}
496
+
497
+
498
+AVInputFormat mxf_demuxer = {
499
+    "mxf",
500
+    "MXF format",
501
+    sizeof(MXFContext),
502
+    mxf_probe,
503
+    mxf_read_header,
504
+    mxf_read_packet,
505
+    mxf_read_close,
506
+    NULL,
507
+};