Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxf.c @ 1159f634

History | View | Annotate | Download (38.7 KB)

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 -1;
218
}
219

    
220
static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
221
{
222
    KLVPacket klv;
223

    
224
    while (!url_feof(&s->pb)) {
225
        if (klv_read_packet(&klv, &s->pb) < 0) {
226
            av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
227
            return -1;
228
        }
229
#ifdef DEBUG
230
        PRINT_KEY("read packet", klv.key);
231
#endif
232
        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
233
            av_get_packet(&s->pb, pkt, klv.length);
234
            pkt->stream_index = mxf_get_stream_index(s, &klv);
235
            return pkt->stream_index == -1 ? -1 : 0;
236
        } else
237
            url_fskip(&s->pb, klv.length);
238
    }
239
    return AVERROR_IO;
240
}
241

    
242
static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
243
{
244
    mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
245
    mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
246
    mxf->metadata_sets_count++;
247
    return 0;
248
}
249

    
250
static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
251
{
252
    ByteIOContext *pb = &mxf->fc->pb;
253
    int bytes_read = 0;
254

    
255
    while (bytes_read < klv->length) {
256
        int tag = get_be16(pb);
257
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
258

    
259
        switch (tag) {
260
        case 0x3B03:
261
            get_buffer(pb, mxf->content_storage_uid, 16);
262
            break;
263
        case 0x3B09:
264
            get_buffer(pb, mxf->operational_pattern_ul, 16);
265
            break;
266
        case 0x3B0A:
267
            mxf->essence_containers_uls_count = get_be32(pb);
268
            if (mxf->essence_containers_uls_count >= UINT_MAX / sizeof(UID))
269
                return -1;
270
            mxf->essence_containers_uls = av_malloc(mxf->essence_containers_uls_count * sizeof(UID));
271
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
272
            get_buffer(pb, (uint8_t *)mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
273
            break;
274
        default:
275
            url_fskip(pb, size);
276
        }
277
        bytes_read += size + 4;
278
    }
279
    return 0;
280
}
281

    
282
static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
283
{
284
    ByteIOContext *pb = &mxf->fc->pb;
285
    int bytes_read = 0;
286

    
287
    while (bytes_read < klv->length) {
288
        int tag = get_be16(pb);
289
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
290

    
291
        dprintf("tag 0x%04X, size %d\n", tag, size);
292
        switch (tag) {
293
        case 0x1901:
294
            mxf->packages_count = get_be32(pb);
295
            if (mxf->packages_count >= UINT_MAX / sizeof(UID))
296
                return -1;
297
            mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
298
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
299
            get_buffer(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
300
            break;
301
        case 0x1902:
302
            mxf->essence_container_data_sets_count = get_be32(pb);
303
            if (mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(UID))
304
                return -1;
305
            mxf->essence_container_data_sets_refs = av_malloc(mxf->essence_container_data_sets_count * sizeof(UID));
306
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
307
            get_buffer(pb, (uint8_t *)mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
308
            break;
309
        default:
310
            url_fskip(pb, size);
311
        }
312
        bytes_read += size + 4;
313
    }
314
    return 0;
315
}
316

    
317
static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
318
{
319
    ByteIOContext *pb = &mxf->fc->pb;
320
    MXFStructuralComponent *source_clip = av_mallocz(sizeof(*source_clip));
321
    int bytes_read = 0;
322

    
323
    while (bytes_read < klv->length) {
324
        int tag = get_be16(pb);
325
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
326

    
327
        bytes_read += size + 4;
328
        dprintf("tag 0x%04X, size %d\n", tag, size);
329
        if (!size) /* ignore empty tag, needed for some files with empty UMID tag */
330
            continue;
331
        switch (tag) {
332
        case 0x3C0A:
333
            get_buffer(pb, source_clip->uid, 16);
334
            break;
335
        case 0x0202:
336
            source_clip->duration = get_be64(pb);
337
            break;
338
        case 0x1201:
339
            source_clip->start_position = get_be64(pb);
340
            break;
341
        case 0x1101:
342
            /* UMID, only get last 16 bytes */
343
            url_fskip(pb, 16);
344
            get_buffer(pb, source_clip->source_package_uid, 16);
345
            break;
346
        case 0x1102:
347
            source_clip->source_track_id = get_be32(pb);
348
            break;
349
        default:
350
            url_fskip(pb, size);
351
        }
352
    }
353
    source_clip->type = SourceClip;
354
    return mxf_add_metadata_set(mxf, source_clip);
355
}
356

    
357
static int mxf_read_metadata_material_package(MXFContext *mxf, KLVPacket *klv)
358
{
359
    ByteIOContext *pb = &mxf->fc->pb;
360
    MXFPackage *package = av_mallocz(sizeof(*package));
361
    int bytes_read = 0;
362

    
363
    while (bytes_read < klv->length) {
364
        int tag = get_be16(pb);
365
        int size = get_be16(pb); /* KLV specified by 0x53 */
366

    
367
        switch (tag) {
368
        case 0x3C0A:
369
            get_buffer(pb, package->uid, 16);
370
            break;
371
        case 0x4403:
372
            package->tracks_count = get_be32(pb);
373
            if (package->tracks_count >= UINT_MAX / sizeof(UID))
374
                return -1;
375
            package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
376
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
377
            get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
378
            break;
379
        default:
380
            url_fskip(pb, size);
381
        }
382
        bytes_read += size + 4;
383
    }
384
    package->type = MaterialPackage;
385
    return mxf_add_metadata_set(mxf, package);
386
}
387

    
388
static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
389
{
390
    ByteIOContext *pb = &mxf->fc->pb;
391
    MXFTrack *track = av_mallocz(sizeof(*track));
392
    int bytes_read = 0;
393

    
394
    while (bytes_read < klv->length) {
395
        int tag = get_be16(pb);
396
        int size = get_be16(pb); /* KLV specified by 0x53 */
397

    
398
        dprintf("tag 0x%04X, size %d\n", tag, size);
399
        switch (tag) {
400
        case 0x3C0A:
401
            get_buffer(pb, track->uid, 16);
402
            break;
403
        case 0x4801:
404
            track->track_id = get_be32(pb);
405
            break;
406
        case 0x4804:
407
            get_buffer(pb, track->track_number, 4);
408
            break;
409
        case 0x4B01:
410
            track->edit_rate.den = get_be32(pb);
411
            track->edit_rate.num = get_be32(pb);
412
            break;
413
        case 0x4803:
414
            get_buffer(pb, track->sequence_ref, 16);
415
            break;
416
        default:
417
            url_fskip(pb, size);
418
        }
419
        bytes_read += size + 4;
420
    }
421
    track->type = Track;
422
    return mxf_add_metadata_set(mxf, track);
423
}
424

    
425
static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
426
{
427
    ByteIOContext *pb = &mxf->fc->pb;
428
    MXFSequence *sequence = av_mallocz(sizeof(*sequence));
429
    int bytes_read = 0;
430

    
431
    while (bytes_read < klv->length) {
432
        int tag = get_be16(pb);
433
        int size = get_be16(pb); /* KLV specified by 0x53 */
434

    
435
        dprintf("tag 0x%04X, size %d\n", tag, size);
436
        switch (tag) {
437
        case 0x3C0A:
438
            get_buffer(pb, sequence->uid, 16);
439
            break;
440
        case 0x0202:
441
            sequence->duration = get_be64(pb);
442
            break;
443
        case 0x0201:
444
            get_buffer(pb, sequence->data_definition_ul, 16);
445
            break;
446
        case 0x1001:
447
            sequence->structural_components_count = get_be32(pb);
448
            if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
449
                return -1;
450
            sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
451
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
452
            get_buffer(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
453
            break;
454
        default:
455
            url_fskip(pb, size);
456
        }
457
        bytes_read += size + 4;
458
    }
459
    sequence->type = Sequence;
460
    return mxf_add_metadata_set(mxf, sequence);
461
}
462

    
463
static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
464
{
465
    ByteIOContext *pb = &mxf->fc->pb;
466
    MXFPackage *package = av_mallocz(sizeof(*package));
467
    int bytes_read = 0;
468

    
469
    while (bytes_read < klv->length) {
470
        int tag = get_be16(pb);
471
        int size = get_be16(pb); /* KLV specified by 0x53 */
472

    
473
        dprintf("tag 0x%04X, size %d\n", tag, size);
474
        switch (tag) {
475
        case 0x3C0A:
476
            get_buffer(pb, package->uid, 16);
477
            break;
478
        case 0x4403:
479
            package->tracks_count = get_be32(pb);
480
            if (package->tracks_count >= UINT_MAX / sizeof(UID))
481
                return -1;
482
            package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
483
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
484
            get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
485
            break;
486
        case 0x4401:
487
            /* UMID, only get last 16 bytes */
488
            url_fskip(pb, 16);
489
            get_buffer(pb, package->package_uid, 16);
490
            break;
491
        case 0x4701:
492
            get_buffer(pb, package->descriptor_ref, 16);
493
            break;
494
        default:
495
            url_fskip(pb, size);
496
        }
497
        bytes_read += size + 4;
498
    }
499
    package->type = SourcePackage;
500
    return mxf_add_metadata_set(mxf, package);
501
}
502

    
503
static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
504
{
505
    ByteIOContext *pb = &mxf->fc->pb;
506
    MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
507
    int bytes_read = 0;
508

    
509
    while (bytes_read < klv->length) {
510
        int tag = get_be16(pb);
511
        int size = get_be16(pb); /* KLV specified by 0x53 */
512

    
513
        dprintf("tag 0x%04X, size %d\n", tag, size);
514
        switch (tag) {
515
        case 0x3C0A:
516
            get_buffer(pb, descriptor->uid, 16);
517
            break;
518
        case 0x3F01:
519
            descriptor->sub_descriptors_count = get_be32(pb);
520
            if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
521
                return -1;
522
            descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
523
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
524
            get_buffer(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
525
            break;
526
        default:
527
            url_fskip(pb, size);
528
        }
529
        bytes_read += size + 4;
530
    }
531
    descriptor->type = MultipleDescriptor;
532
    return mxf_add_metadata_set(mxf, descriptor);
533
}
534

    
535
static void mxf_read_metadata_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
536
{
537
    int code;
538

    
539
    do {
540
        code = get_byte(pb);
541
        dprintf("pixel layout: code 0x%x\n", code);
542
        switch (code) {
543
        case 0x52: /* R */
544
            descriptor->bits_per_sample += get_byte(pb);
545
            break;
546
        case 0x47: /* G */
547
            descriptor->bits_per_sample += get_byte(pb);
548
            break;
549
        case 0x42: /* B */
550
            descriptor->bits_per_sample += get_byte(pb);
551
            break;
552
        default:
553
            get_byte(pb);
554
        }
555
    } while (code != 0); /* SMPTE 377M E.2.46 */
556
}
557

    
558
static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
559
{
560
    ByteIOContext *pb = &mxf->fc->pb;
561
    MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
562
    int bytes_read = 0;
563

    
564
    while (bytes_read < klv->length) {
565
        int tag = get_be16(pb);
566
        int size = get_be16(pb); /* KLV specified by 0x53 */
567

    
568
        dprintf("tag 0x%04X, size %d\n", tag, size);
569
        switch (tag) {
570
        case 0x3C0A:
571
            get_buffer(pb, descriptor->uid, 16);
572
            break;
573
        case 0x3004:
574
            get_buffer(pb, descriptor->essence_container_ul, 16);
575
            break;
576
        case 0x3006:
577
            descriptor->linked_track_id = get_be32(pb);
578
            break;
579
        case 0x3201: /* PictureEssenceCoding */
580
            get_buffer(pb, descriptor->essence_codec_ul, 16);
581
            break;
582
        case 0x3203:
583
            descriptor->width = get_be32(pb);
584
            break;
585
        case 0x3202:
586
            descriptor->height = get_be32(pb);
587
            break;
588
        case 0x320E:
589
            descriptor->aspect_ratio.num = get_be32(pb);
590
            descriptor->aspect_ratio.den = get_be32(pb);
591
            break;
592
        case 0x3D03:
593
            descriptor->sample_rate.num = get_be32(pb);
594
            descriptor->sample_rate.den = get_be32(pb);
595
            break;
596
        case 0x3D06: /* SoundEssenceCompression */
597
            get_buffer(pb, descriptor->essence_codec_ul, 16);
598
            break;
599
        case 0x3D07:
600
            descriptor->channels = get_be32(pb);
601
            break;
602
        case 0x3D01:
603
            descriptor->bits_per_sample = get_be32(pb);
604
            break;
605
        case 0x3401:
606
            mxf_read_metadata_pixel_layout(pb, descriptor);
607
            break;
608
        case 0x8201: /* Private tag used by SONY C0023S01.mxf */
609
            descriptor->extradata = av_malloc(size);
610
            descriptor->extradata_size = size;
611
            get_buffer(pb, descriptor->extradata, size);
612
            break;
613
        default:
614
            url_fskip(pb, size);
615
        }
616
        bytes_read += size + 4;
617
    }
618
    descriptor->type = Descriptor;
619
    return mxf_add_metadata_set(mxf, descriptor);
620
}
621

    
622
/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
623
static const MXFDataDefinitionUL mxf_data_definition_uls[] = {
624
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 }, CODEC_TYPE_VIDEO },
625
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 }, CODEC_TYPE_AUDIO },
626
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x05,0x01,0x03,0x02,0x02,0x02,0x02,0x00,0x00 }, CODEC_TYPE_AUDIO },
627
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  CODEC_TYPE_DATA },
628
};
629

    
630
static const MXFCodecUL mxf_codec_uls[] = {
631
    /* PictureEssenceCoding */
632
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@ML I-Frame */
633
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@HL Long GoP */
634
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MP@ML Long GoP */
635
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* 422P@ML Long GoP */
636
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MP@HL Long GoP */
637
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x03 },      CODEC_ID_MPEG4, Frame }, /* XDCAM proxy_pal030926.mxf */
638
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x04 },      CODEC_ID_MPEG4, Frame }, /* XDCAM Proxy C0023S01.mxf */
639
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, CODEC_ID_MPEG2VIDEO, Frame }, /* D-10 30Mbps PAL */
640
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, CODEC_ID_MPEG2VIDEO, Frame }, /* D-10 50Mbps PAL */
641
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DVCPRO50 PAL */
642
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DVCPRO25 PAL */
643
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },    CODEC_ID_DVVIDEO, Frame }, /* DV25 IEC PAL */
644
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_JPEG2000, Frame }, /* JPEG2000 Codestream */
645
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 },   CODEC_ID_RAWVIDEO, Frame }, /* Uncompressed */
646
    /* SoundEssenceCompression */
647
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* Uncompressed */
648
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE, Frame },
649
    { { 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 */
650
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_PCM_ALAW, Frame },
651
    { { 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 */
652
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 },        CODEC_ID_AC3, Frame },
653
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 },        CODEC_ID_MP2, Frame }, /* MP2 or MP3 */
654
  //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 },    CODEC_ID_DOLBY_E, Frame }, /* Dolby-E */
655
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
656
};
657

    
658
static const MXFCodecUL mxf_picture_essence_container_uls[] = {
659
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, CODEC_ID_MPEG2VIDEO, Frame }, /* MPEG-ES Frame wrapped */
660
    { { 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 */
661
    { { 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 */
662
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
663
};
664

    
665
static const MXFCodecUL mxf_sound_essence_container_uls[] = {
666
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* BWF Frame wrapped */
667
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },  CODEC_ID_PCM_S16LE, Frame }, /* AES Frame wrapped */
668
    { { 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 */
669
    { { 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 */
670
    { { 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 */
671
  //{ { 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 */
672
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },       CODEC_ID_NONE, Frame },
673
};
674

    
675
static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
676
{
677
    while (uls->id != CODEC_ID_NONE) {
678
        if(!memcmp(uls->uid, *uid, 16))
679
            break;
680
        uls++;
681
    }
682
    return uls;
683
}
684

    
685
static enum CodecType mxf_get_codec_type(const MXFDataDefinitionUL *uls, UID *uid)
686
{
687
    while (uls->type != CODEC_TYPE_DATA) {
688
        if(!memcmp(uls->uid, *uid, 16))
689
            break;
690
        uls++;
691
    }
692
    return uls->type;
693
}
694

    
695
static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref)
696
{
697
    int i;
698

    
699
    if (!strong_ref)
700
        return NULL;
701
    for (i = 0; i < mxf->metadata_sets_count; i++) {
702
        if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16)) {
703
            return mxf->metadata_sets[i];
704
        }
705
    }
706
    return NULL;
707
}
708

    
709
static int mxf_parse_structural_metadata(MXFContext *mxf)
710
{
711
    MXFPackage *material_package = NULL;
712
    MXFPackage *source_package = NULL;
713
    MXFPackage *temp_package = NULL;
714
    int i, j, k;
715

    
716
    dprintf("metadata sets count %d\n", mxf->metadata_sets_count);
717
    /* TODO: handle multiple material packages (OP3x) */
718
    for (i = 0; i < mxf->packages_count; i++) {
719
        if (!(temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i]))) {
720
            av_log(mxf->fc, AV_LOG_ERROR, "could not resolve package strong ref\n");
721
            return -1;
722
        }
723
        if (temp_package->type == MaterialPackage) {
724
            material_package = temp_package;
725
            break;
726
        }
727
    }
728
    if (!material_package) {
729
        av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
730
        return -1;
731
    }
732

    
733
    for (i = 0; i < material_package->tracks_count; i++) {
734
        MXFTrack *material_track = NULL;
735
        MXFTrack *source_track = NULL;
736
        MXFTrack *temp_track = NULL;
737
        MXFDescriptor *descriptor = NULL;
738
        MXFStructuralComponent *component = NULL;
739
        const MXFCodecUL *codec_ul = NULL;
740
        const MXFCodecUL *container_ul = NULL;
741
        AVStream *st;
742

    
743
        if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i]))) {
744
            av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
745
            continue;
746
        }
747

    
748
        if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref))) {
749
            av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
750
            return -1;
751
        }
752

    
753
        /* TODO: handle multiple source clips */
754
        for (j = 0; j < material_track->sequence->structural_components_count; j++) {
755
            /* TODO: handle timecode component */
756
            component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j]);
757
            if (!component || component->type != SourceClip)
758
                continue;
759

    
760
            for (k = 0; k < mxf->packages_count; k++) {
761
                if (!(temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k]))) {
762
                    av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
763
                    return -1;
764
                }
765
                if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
766
                    source_package = temp_package;
767
                    break;
768
                }
769
            }
770
            if (!source_package) {
771
                av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
772
                break;
773
            }
774
            for (k = 0; k < source_package->tracks_count; k++) {
775
                if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k]))) {
776
                    av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
777
                    return -1;
778
                }
779
                if (temp_track->track_id == component->source_track_id) {
780
                    source_track = temp_track;
781
                    break;
782
                }
783
            }
784
            if (!source_track) {
785
                av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
786
                break;
787
            }
788
        }
789
        if (!source_track)
790
            continue;
791

    
792
        st = av_new_stream(mxf->fc, source_track->track_id);
793
        st->priv_data = source_track;
794
        st->duration = component->duration;
795
        if (st->duration == -1)
796
            st->duration = AV_NOPTS_VALUE;
797
        st->start_time = component->start_position;
798
        av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
799

    
800
        if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref))) {
801
            av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
802
            return -1;
803
        }
804

    
805
#ifdef DEBUG
806
        PRINT_KEY("data definition   ul", source_track->sequence->data_definition_ul);
807
#endif
808
        st->codec->codec_type = mxf_get_codec_type(mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
809

    
810
        source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref);
811
        if (source_package->descriptor) {
812
            if (source_package->descriptor->type == MultipleDescriptor) {
813
                for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
814
                    MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j]);
815

    
816
                    if (!sub_descriptor) {
817
                        av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
818
                        continue;
819
                    }
820
                    if (sub_descriptor->linked_track_id == source_track->track_id) {
821
                        descriptor = sub_descriptor;
822
                        break;
823
                    }
824
                }
825
            } else
826
                descriptor = source_package->descriptor;
827
        }
828
        if (!descriptor) {
829
            av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
830
            continue;
831
        }
832
#ifdef DEBUG
833
        PRINT_KEY("essence codec     ul", descriptor->essence_codec_ul);
834
        PRINT_KEY("essence container ul", descriptor->essence_container_ul);
835
#endif
836
        /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
837
        codec_ul = mxf_get_codec_ul(mxf_codec_uls, &descriptor->essence_codec_ul);
838
        st->codec->codec_id = codec_ul->id;
839
        if (descriptor->extradata) {
840
            st->codec->extradata = descriptor->extradata;
841
            st->codec->extradata_size = descriptor->extradata_size;
842
        }
843
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
844
            container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, &descriptor->essence_container_ul);
845
            if (st->codec->codec_id == CODEC_ID_NONE)
846
                st->codec->codec_id = container_ul->id;
847
            st->codec->width = descriptor->width;
848
            st->codec->height = descriptor->height;
849
            st->codec->bits_per_sample = descriptor->bits_per_sample; /* Uncompressed */
850
        } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
851
            container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, &descriptor->essence_container_ul);
852
            if (st->codec->codec_id == CODEC_ID_NONE)
853
                st->codec->codec_id = container_ul->id;
854
            st->codec->channels = descriptor->channels;
855
            st->codec->bits_per_sample = descriptor->bits_per_sample;
856
            st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
857
            /* TODO: implement CODEC_ID_RAWAUDIO */
858
            if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
859
                if (descriptor->bits_per_sample == 24)
860
                    st->codec->codec_id = CODEC_ID_PCM_S24LE;
861
                else if (descriptor->bits_per_sample == 32)
862
                    st->codec->codec_id = CODEC_ID_PCM_S32LE;
863
            } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
864
                if (descriptor->bits_per_sample == 24)
865
                    st->codec->codec_id = CODEC_ID_PCM_S24BE;
866
                else if (descriptor->bits_per_sample == 32)
867
                    st->codec->codec_id = CODEC_ID_PCM_S32BE;
868
            }
869
        }
870
        if (container_ul && container_ul->wrapping == Clip) {
871
            dprintf("stream %d: clip wrapped essence\n", st->index);
872
            st->need_parsing = 1;
873
        }
874
    }
875
    return 0;
876
}
877

    
878
static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
879
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2F,0x00 }, mxf_read_metadata_preface },
880
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_metadata_content_storage },
881
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_metadata_source_package },
882
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_metadata_material_package },
883
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_metadata_sequence },
884
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_metadata_source_clip },
885
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_metadata_multiple_descriptor },
886
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_metadata_generic_descriptor }, /* Generic Sound */
887
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_metadata_generic_descriptor }, /* CDCI */
888
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_metadata_generic_descriptor }, /* RGBA */
889
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_metadata_generic_descriptor }, /* MPEG 2 Video */
890
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_metadata_generic_descriptor }, /* Wave */
891
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_metadata_generic_descriptor }, /* AES3 */
892
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_metadata_track }, /* Static Track */
893
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_metadata_track }, /* Generic Track */
894
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL },
895
};
896

    
897
static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
898
{
899
    MXFContext *mxf = s->priv_data;
900
    KLVPacket klv;
901

    
902
    mxf->fc = s;
903
    while (!url_feof(&s->pb)) {
904
        const MXFMetadataReadTableEntry *function;
905

    
906
        if (klv_read_packet(&klv, &s->pb) < 0) {
907
            av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
908
            return -1;
909
        }
910
#ifdef DEBUG
911
        PRINT_KEY("read header", klv.key);
912
#endif
913
        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
914
            /* FIXME avoid seek */
915
            url_fseek(&s->pb, klv.offset, SEEK_SET);
916
            break;
917
        }
918

    
919
        for (function = mxf_metadata_read_table; function->read; function++) {
920
            if (IS_KLV_KEY(klv.key, function->key)) {
921
                if (function->read(mxf, &klv) < 0) {
922
                    av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
923
                    return -1;
924
                }
925
                break;
926
            }
927
        }
928
        if (!function->read)
929
            url_fskip(&s->pb, klv.length);
930
    }
931
    return mxf_parse_structural_metadata(mxf);
932
}
933

    
934
static int mxf_read_close(AVFormatContext *s)
935
{
936
    MXFContext *mxf = s->priv_data;
937
    int i;
938

    
939
    av_freep(&mxf->packages_refs);
940
    av_freep(&mxf->essence_container_data_sets_refs);
941
    av_freep(&mxf->essence_containers_uls);
942
    for (i = 0; i < mxf->metadata_sets_count; i++) {
943
        switch (mxf->metadata_sets[i]->type) {
944
        case MultipleDescriptor:
945
            av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
946
            break;
947
        case Sequence:
948
            av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
949
            break;
950
        case SourcePackage:
951
        case MaterialPackage:
952
            av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
953
            break;
954
        default:
955
            break;
956
        }
957
        av_freep(&mxf->metadata_sets[i]);
958
    }
959
    av_freep(&mxf->metadata_sets);
960
    return 0;
961
}
962

    
963
static int mxf_probe(AVProbeData *p) {
964
    uint8_t *bufp = p->buf;
965
    uint8_t *end = p->buf + p->buf_size;
966

    
967
    if (p->buf_size < sizeof(mxf_header_partition_pack_key))
968
        return 0;
969

    
970
    /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
971
    end -= sizeof(mxf_header_partition_pack_key);
972
    for (; bufp < end; bufp++) {
973
        if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
974
            return AVPROBE_SCORE_MAX;
975
    }
976
    return 0;
977
}
978

    
979

    
980
AVInputFormat mxf_demuxer = {
981
    "mxf",
982
    "MXF format",
983
    sizeof(MXFContext),
984
    mxf_probe,
985
    mxf_read_header,
986
    mxf_read_packet,
987
    mxf_read_close,
988
    NULL,
989
};