Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxf.c @ ba32c8df

History | View | Annotate | Download (36.3 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) and then resolve Strong Reference from another object.
38
 * Returns -1 if Strong Reference could not be resolved.
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
 * Only descriptors with EssenceSoundCompression or PictureEssenceCoding will be taken into account. "D/req" SMPTE 377M
43
 */
44

    
45
//#define DEBUG
46

    
47
#include "avformat.h"
48

    
49
typedef uint8_t UID[16];
50

    
51
enum MXFPackageType {
52
    MaterialPackage,
53
    SourcePackage,
54
};
55

    
56
enum MXFStructuralComponentType {
57
    Timecode,
58
    SourceClip,
59
};
60

    
61
typedef struct MXFStructuralComponent {
62
    UID uid;
63
    UID source_package_uid;
64
    UID data_definition_ul;
65
    int64_t duration;
66
    int64_t start_position;
67
    int source_track_id;
68
    enum MXFStructuralComponentType type;
69
} MXFStructuralComponent;
70

    
71
typedef struct MXFSequence {
72
    UID uid;
73
    UID data_definition_ul;
74
    MXFStructuralComponent **structural_components;
75
    UID *structural_components_refs;
76
    int structural_components_count;
77
    int64_t duration;
78
} MXFSequence;
79

    
80
typedef struct MXFTrack {
81
    UID uid;
82
    MXFSequence *sequence; /* mandatory, and only one */
83
    UID sequence_ref;
84
    int track_id;
85
    uint8_t track_number[4];
86
    AVRational edit_rate;
87
} MXFTrack;
88

    
89
typedef struct MXFDescriptor {
90
    UID uid;
91
    UID essence_container_ul;
92
    UID essence_codec_ul;
93
    AVRational sample_rate;
94
    AVRational aspect_ratio;
95
    int width;
96
    int height;
97
    int channels;
98
    int bits_per_sample;
99
    struct MXFDescriptor **sub_descriptors;
100
    UID *sub_descriptors_refs;
101
    int sub_descriptors_count;
102
    int linked_track_id;
103
} MXFDescriptor;
104

    
105
typedef struct MXFPackage {
106
    UID uid;
107
    UID package_uid;
108
    MXFTrack **tracks;
109
    UID *tracks_refs;
110
    int tracks_count;
111
    MXFDescriptor *descriptor; /* only one */
112
    UID descriptor_ref;
113
    enum MXFPackageType type;
114
} MXFPackage;
115

    
116
typedef struct MXFEssenceContainerData {
117
    UID uid;
118
    UID linked_package_uid;
119
} MXFEssenceContainerData;
120

    
121
typedef struct MXFContext {
122
    MXFPackage **packages;
123
    UID *packages_refs;
124
    int packages_count;
125
    MXFEssenceContainerData **essence_container_data_sets;
126
    UID *essence_container_data_sets_refs;
127
    int essence_container_data_sets_count;
128
    UID *essence_containers_uls; /* Universal Labels SMPTE RP224 */
129
    int essence_containers_uls_count;
130
    UID operational_pattern_ul;
131
    UID content_storage_uid;
132
    AVFormatContext *fc;
133
} MXFContext;
134

    
135
typedef struct KLVPacket {
136
    UID key;
137
    offset_t offset;
138
    uint64_t length;
139
} KLVPacket;
140

    
141
typedef struct MXFCodecUL {
142
    UID uid;
143
    enum CodecID id;
144
} MXFCodecUL;
145

    
146
typedef struct MXFMetadataReadTableEntry {
147
    const UID key;
148
    int (*read)(MXFContext *mxf, KLVPacket *klv);
149
} MXFMetadataReadTableEntry;
150

    
151
/* partial keys to match */
152
static const uint8_t mxf_header_partition_pack_key[]       = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
153
static const uint8_t mxf_essence_element_key[]             = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
154

    
155
#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
156

    
157
#define PRINT_KEY(x) dprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", \
158
                             (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])
159

    
160
static int64_t klv_decode_ber_length(ByteIOContext *pb)
161
{
162
    int64_t size = 0;
163
    uint8_t length = get_byte(pb);
164
    int type = length >> 7;
165

    
166
    if (type) { /* long form */
167
        int bytes_num = length & 0x7f;
168
        /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
169
        if (bytes_num > 8)
170
            return -1;
171
        while (bytes_num--)
172
            size = size << 8 | get_byte(pb);
173
    } else {
174
        size = length & 0x7f;
175
    }
176
    return size;
177
}
178

    
179
static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
180
{
181
    klv->offset = url_ftell(pb);
182
    get_buffer(pb, klv->key, 16);
183
    klv->length = klv_decode_ber_length(pb);
184
    return klv->length == -1 ? -1 : 0;
185
}
186

    
187
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
188
{
189
    int i;
190

    
191
    for (i = 0; i < s->nb_streams; i++) {
192
        MXFTrack *track = s->streams[i]->priv_data;
193
         /* SMPTE 379M 7.3 */
194
        if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
195
            return i;
196
    }
197
    return -1;
198
}
199

    
200
static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
201
{
202
    KLVPacket klv;
203

    
204
    while (!url_feof(&s->pb)) {
205
        if (klv_read_packet(&klv, &s->pb) < 0) {
206
            av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
207
            return -1;
208
        }
209
#ifdef DEBUG
210
        PRINT_KEY(klv.key);
211
#endif
212
        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
213
            av_get_packet(&s->pb, pkt, klv.length);
214
            pkt->stream_index = mxf_get_stream_index(s, &klv);
215
            return pkt->stream_index == -1 ? -1 : 0;
216
        } else
217
            url_fskip(&s->pb, klv.length);
218
    }
219
    return AVERROR_IO;
220
}
221

    
222
static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
223
{
224
    ByteIOContext *pb = &mxf->fc->pb;
225
    int bytes_read = 0;
226

    
227
    while (bytes_read < klv->length) {
228
        int tag = get_be16(pb);
229
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
230

    
231
        switch (tag) {
232
        case 0x3B03:
233
            get_buffer(pb, mxf->content_storage_uid, 16);
234
            break;
235
        case 0x3B09:
236
            get_buffer(pb, mxf->operational_pattern_ul, 16);
237
            break;
238
        case 0x3B0A:
239
            mxf->essence_containers_uls_count = get_be32(pb);
240
            if (mxf->essence_containers_uls_count >= UINT_MAX / sizeof(UID))
241
                return -1;
242
            mxf->essence_containers_uls = av_malloc(mxf->essence_containers_uls_count * sizeof(UID));
243
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
244
            get_buffer(pb, mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
245
            break;
246
        default:
247
            url_fskip(pb, size);
248
        }
249
        bytes_read += size + 4;
250
    }
251
    return 0;
252
}
253

    
254
static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
255
{
256
    ByteIOContext *pb = &mxf->fc->pb;
257
    int bytes_read = 0;
258

    
259
    while (bytes_read < klv->length) {
260
        int tag = get_be16(pb);
261
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
262

    
263
        dprintf("tag 0x%04X, size %d\n", tag, size);
264
        switch (tag) {
265
        case 0x1901:
266
            mxf->packages_count = get_be32(pb);
267
            if (mxf->packages_count >= UINT_MAX / sizeof(UID) ||
268
                mxf->packages_count >= UINT_MAX / sizeof(*mxf->packages))
269
                return -1;
270
            mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
271
            mxf->packages = av_mallocz(mxf->packages_count * sizeof(*mxf->packages));
272
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
273
            get_buffer(pb, mxf->packages_refs, mxf->packages_count * sizeof(UID));
274
            break;
275
        case 0x1902:
276
            mxf->essence_container_data_sets_count = get_be32(pb);
277
            if (mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(UID) ||
278
                mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(*mxf->essence_container_data_sets))
279
                return -1;
280
            mxf->essence_container_data_sets_refs = av_malloc(mxf->essence_container_data_sets_count * sizeof(UID));
281
            mxf->essence_container_data_sets = av_mallocz(mxf->essence_container_data_sets_count * sizeof(*mxf->essence_container_data_sets));
282
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
283
            get_buffer(pb, mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
284
            break;
285
        default:
286
            url_fskip(pb, size);
287
        }
288
        bytes_read += size + 4;
289
    }
290
    return 0;
291
}
292

    
293
static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
294
{
295
    ByteIOContext *pb = &mxf->fc->pb;
296
    MXFStructuralComponent *source_clip = av_mallocz(sizeof(*source_clip));
297
    int bytes_read = 0;
298
    int i, j, k;
299

    
300
    source_clip->type = SourceClip;
301
    while (bytes_read < klv->length) {
302
        int tag = get_be16(pb);
303
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
304

    
305
        dprintf("tag 0x%04X, size %d\n", tag, size);
306
        switch (tag) {
307
        case 0x3C0A:
308
            get_buffer(pb, source_clip->uid, 16);
309
            break;
310
        case 0x0202:
311
            source_clip->duration = get_be64(pb);
312
            break;
313
        case 0x1201:
314
            source_clip->start_position = get_be64(pb);
315
            break;
316
        case 0x1101:
317
            /* UMID, only get last 16 bytes */
318
            url_fskip(pb, 16);
319
            get_buffer(pb, source_clip->source_package_uid, 16);
320
            break;
321
        case 0x1102:
322
            source_clip->source_track_id = get_be32(pb);
323
            break;
324
        default:
325
            url_fskip(pb, size);
326
        }
327
        bytes_read += size + 4;
328
    }
329
    for (i = 0; i < mxf->packages_count; i++) {
330
        if (mxf->packages[i]) {
331
            for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
332
                if (mxf->packages[i]->tracks[j] && mxf->packages[i]->tracks[j]->sequence) {
333
                    for (k = 0; k < mxf->packages[i]->tracks[j]->sequence->structural_components_count; k++) {
334
                        if (!memcmp(mxf->packages[i]->tracks[j]->sequence->structural_components_refs[k], source_clip->uid, 16)) {
335
                            mxf->packages[i]->tracks[j]->sequence->structural_components[k] = source_clip;
336
                            return 0;
337
                        }
338
                    }
339
                }
340
            }
341
        }
342
    }
343
    return -1;
344
}
345

    
346
static int mxf_read_metadata_material_package(MXFContext *mxf, KLVPacket *klv)
347
{
348
    ByteIOContext *pb = &mxf->fc->pb;
349
    MXFPackage *package = av_mallocz(sizeof(*package));
350
    int bytes_read = 0;
351
    int i;
352

    
353
    package->type = MaterialPackage;
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
                package->tracks_count >= UINT_MAX / sizeof(*package->tracks))
366
                return -1;
367
            package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
368
            package->tracks = av_mallocz(package->tracks_count * sizeof(*package->tracks));
369
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
370
            get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
371
            break;
372
        default:
373
            url_fskip(pb, size);
374
        }
375
        bytes_read += size + 4;
376
    }
377
    for (i = 0; i < mxf->packages_count; i++) {
378
        if (!memcmp(mxf->packages_refs[i], package->uid, 16)) {
379
            mxf->packages[i] = package;
380
            return 0;
381
        }
382
    }
383
    return -1;
384
}
385

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

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

    
397
        dprintf("tag 0x%04X, size %d\n", tag, size);
398
        switch (tag) {
399
        case 0x3C0A:
400
            get_buffer(pb, track->uid, 16);
401
            break;
402
        case 0x4801:
403
            track->track_id = get_be32(pb);
404
            break;
405
        case 0x4804:
406
            get_buffer(pb, track->track_number, 4);
407
            break;
408
        case 0x4B01:
409
            track->edit_rate.den = get_be32(pb);
410
            track->edit_rate.num = get_be32(pb);
411
            break;
412
        case 0x4803:
413
            get_buffer(pb, track->sequence_ref, 16);
414
            break;
415
        default:
416
            url_fskip(pb, size);
417
        }
418
        bytes_read += size + 4;
419
    }
420
    for (i = 0; i < mxf->packages_count; i++) {
421
        if (mxf->packages[i]) {
422
            for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
423
                if (!memcmp(mxf->packages[i]->tracks_refs[j], track->uid, 16)) {
424
                    mxf->packages[i]->tracks[j] = track;
425
                    return 0;
426
                }
427
            }
428
        }
429
    }
430
    return -1;
431
}
432

    
433
static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
434
{
435
    ByteIOContext *pb = &mxf->fc->pb;
436
    MXFSequence *sequence = av_mallocz(sizeof(*sequence));
437
    int bytes_read = 0;
438
    int i, j;
439

    
440
    while (bytes_read < klv->length) {
441
        int tag = get_be16(pb);
442
        int size = get_be16(pb); /* KLV specified by 0x53 */
443

    
444
        dprintf("tag 0x%04X, size %d\n", tag, size);
445
        switch (tag) {
446
        case 0x3C0A:
447
            get_buffer(pb, sequence->uid, 16);
448
            break;
449
        case 0x0202:
450
            sequence->duration = get_be64(pb);
451
            break;
452
        case 0x0201:
453
            get_buffer(pb, sequence->data_definition_ul, 16);
454
            break;
455
        case 0x1001:
456
            sequence->structural_components_count = get_be32(pb);
457
            if (sequence->structural_components_count >= UINT_MAX / sizeof(UID) ||
458
                sequence->structural_components_count >= UINT_MAX / sizeof(*sequence->structural_components))
459
                return -1;
460
            sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
461
            sequence->structural_components = av_mallocz(sequence->structural_components_count * sizeof(*sequence->structural_components));
462
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
463
            get_buffer(pb, sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
464
            break;
465
        default:
466
            url_fskip(pb, size);
467
        }
468
        bytes_read += size + 4;
469
    }
470
    for (i = 0; i < mxf->packages_count; i++) {
471
        if (mxf->packages[i]) {
472
            for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
473
                if (mxf->packages[i]->tracks[j]) {
474
                    if (!memcmp(mxf->packages[i]->tracks[j]->sequence_ref, sequence->uid, 16)) {
475
                        mxf->packages[i]->tracks[j]->sequence = sequence;
476
                        return 0;
477
                    }
478
                }
479
            }
480
        }
481
    }
482
    return -1;
483
}
484

    
485
static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
486
{
487
    ByteIOContext *pb = &mxf->fc->pb;
488
    MXFPackage *package = av_mallocz(sizeof(*package));
489
    int bytes_read = 0;
490
    int i;
491

    
492
    package->type = SourcePackage;
493
    while (bytes_read < klv->length) {
494
        int tag = get_be16(pb);
495
        int size = get_be16(pb); /* KLV specified by 0x53 */
496

    
497
        dprintf("tag 0x%04X, size %d\n", tag, size);
498
        switch (tag) {
499
        case 0x3C0A:
500
            get_buffer(pb, package->uid, 16);
501
            break;
502
        case 0x4403:
503
            package->tracks_count = get_be32(pb);
504
            if (package->tracks_count >= UINT_MAX / sizeof(UID) ||
505
                package->tracks_count >= UINT_MAX / sizeof(*package->tracks))
506
                return -1;
507
            package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
508
            package->tracks = av_mallocz(package->tracks_count * sizeof(*package->tracks));
509
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
510
            get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
511
            break;
512
        case 0x4401:
513
            /* UMID, only get last 16 bytes */
514
            url_fskip(pb, 16);
515
            get_buffer(pb, package->package_uid, 16);
516
            break;
517
        case 0x4701:
518
            get_buffer(pb, package->descriptor_ref, 16);
519
            break;
520
        default:
521
            url_fskip(pb, size);
522
        }
523
        bytes_read += size + 4;
524
    }
525
    for (i = 0; i < mxf->packages_count; i++) {
526
        if (!memcmp(mxf->packages_refs[i], package->uid, 16)) {
527
            mxf->packages[i] = package;
528
            return 0;
529
        }
530
    }
531
    return -1;
532
}
533

    
534
static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
535
{
536
    ByteIOContext *pb = &mxf->fc->pb;
537
    MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
538
    int bytes_read = 0;
539
    int i;
540

    
541
    while (bytes_read < klv->length) {
542
        int tag = get_be16(pb);
543
        int size = get_be16(pb); /* KLV specified by 0x53 */
544

    
545
        dprintf("tag 0x%04X, size %d\n", tag, size);
546
        switch (tag) {
547
        case 0x3C0A:
548
            get_buffer(pb, descriptor->uid, 16);
549
            break;
550
        case 0x3F01:
551
            descriptor->sub_descriptors_count = get_be32(pb);
552
            if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID) ||
553
                descriptor->sub_descriptors_count >= UINT_MAX / sizeof(*descriptor->sub_descriptors))
554
                return -1;
555
            descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
556
            descriptor->sub_descriptors = av_mallocz(descriptor->sub_descriptors_count * sizeof(*descriptor->sub_descriptors));
557
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
558
            get_buffer(pb, descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
559
            break;
560
        default:
561
            url_fskip(pb, size);
562
        }
563
        bytes_read += size + 4;
564
    }
565
    for (i = 0; i < mxf->packages_count; i++) {
566
        if (mxf->packages[i]) {
567
            if (!memcmp(mxf->packages[i]->descriptor_ref, descriptor->uid, 16)) {
568
                mxf->packages[i]->descriptor = descriptor;
569
                return 0;
570
            }
571
        }
572
    }
573
    return -1;
574
}
575

    
576
static void mxf_read_metadata_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
577
{
578
    int code;
579

    
580
    do {
581
        code = get_byte(pb);
582
        dprintf("pixel layout: code 0x%x\n", code);
583
        switch (code) {
584
        case 0x52: /* R */
585
            descriptor->bits_per_sample += get_byte(pb);
586
            break;
587
        case 0x47: /* G */
588
            descriptor->bits_per_sample += get_byte(pb);
589
            break;
590
        case 0x42: /* B */
591
            descriptor->bits_per_sample += get_byte(pb);
592
            break;
593
        default:
594
            get_byte(pb);
595
        }
596
    } while (code != 0); /* SMPTE 377M E.2.46 */
597
}
598

    
599
static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
600
{
601
    ByteIOContext *pb = &mxf->fc->pb;
602
    MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
603
    int bytes_read = 0;
604
    int i, j;
605

    
606
    while (bytes_read < klv->length) {
607
        int tag = get_be16(pb);
608
        int size = get_be16(pb); /* KLV specified by 0x53 */
609

    
610
        dprintf("tag 0x%04X, size %d\n", tag, size);
611
        switch (tag) {
612
        case 0x3C0A:
613
            get_buffer(pb, descriptor->uid, 16);
614
            break;
615
        case 0x3004:
616
            get_buffer(pb, descriptor->essence_container_ul, 16);
617
            break;
618
        case 0x3006:
619
            descriptor->linked_track_id = get_be32(pb);
620
            break;
621
        case 0x3201: /* PictureEssenceCoding */
622
            get_buffer(pb, descriptor->essence_codec_ul, 16);
623
            break;
624
        case 0x3203:
625
            descriptor->width = get_be32(pb);
626
            break;
627
        case 0x3202:
628
            descriptor->height = get_be32(pb);
629
            break;
630
        case 0x320E:
631
            descriptor->aspect_ratio.num = get_be32(pb);
632
            descriptor->aspect_ratio.den = get_be32(pb);
633
            break;
634
        case 0x3D03:
635
            descriptor->sample_rate.num = get_be32(pb);
636
            descriptor->sample_rate.den = get_be32(pb);
637
            break;
638
        case 0x3D06: /* SoundEssenceCompression */
639
            get_buffer(pb, descriptor->essence_codec_ul, 16);
640
            break;
641
        case 0x3D07:
642
            descriptor->channels = get_be32(pb);
643
            break;
644
        case 0x3D01:
645
            descriptor->bits_per_sample = get_be32(pb);
646
            break;
647
        case 0x3401:
648
            mxf_read_metadata_pixel_layout(pb, descriptor);
649
            break;
650
        default:
651
            url_fskip(pb, size);
652
        }
653
        bytes_read += size + 4;
654
    }
655
    for (i = 0; i < mxf->packages_count; i++) {
656
        if (mxf->packages[i]) {
657
            if (!memcmp(mxf->packages[i]->descriptor_ref, descriptor->uid, 16)) {
658
                mxf->packages[i]->descriptor = descriptor;
659
                return 0;
660
            } else if (mxf->packages[i]->descriptor) { /* MultipleDescriptor */
661
                for (j = 0; j < mxf->packages[i]->descriptor->sub_descriptors_count; j++) {
662
                    if (!memcmp(mxf->packages[i]->descriptor->sub_descriptors_refs[j], descriptor->uid, 16)) {
663
                        mxf->packages[i]->descriptor->sub_descriptors[j] = descriptor;
664
                        return 0;
665
                    }
666
                }
667
            }
668
        }
669
    }
670
    return -1;
671
}
672

    
673
/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
674
static const UID picture_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 };
675
static const UID sound_essence_track_ul   = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 };
676

    
677
static const MXFCodecUL mxf_codec_uls[] = {
678
    /* PictureEssenceCoding */
679
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, CODEC_ID_MPEG2VIDEO }, /* 422P@ML I-Frame */
680
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* 422P@HL Long GoP */
681
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, CODEC_ID_MPEG2VIDEO }, /* MP@ML Long GoP */
682
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* 422P@ML Long GoP */
683
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* MP@HL Long GoP */
684
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, CODEC_ID_MPEG2VIDEO }, /* D-10 30Mbps PAL */
685
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, CODEC_ID_MPEG2VIDEO }, /* D-10 50Mbps PAL */
686
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },    CODEC_ID_DVVIDEO }, /* DVCPRO50 PAL */
687
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },    CODEC_ID_DVVIDEO }, /* DVCPRO25 PAL */
688
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },    CODEC_ID_DVVIDEO }, /* DV25 IEC PAL */
689
  //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },     CODEC_ID_JPEG2K }, /* JPEG2000 Codestream */
690
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 },   CODEC_ID_RAWVIDEO }, /* Uncompressed */
691
    /* SoundEssenceCompression */
692
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE },
693
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x02,0x02,0x01,0x7E,0x00,0x00,0x00 },  CODEC_ID_PCM_S16BE }, /* From Omneon MXF file */
694
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_PCM_ALAW },
695
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 },        CODEC_ID_AC3 },
696
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 },        CODEC_ID_MP2 }, /* MP2 or MP3 */
697
  //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 },    CODEC_ID_DOLBY_E }, /* Dolby-E */
698
};
699

    
700
static enum CodecID mxf_get_codec_id(const MXFCodecUL *uls, UID *uid)
701
{
702
    while (uls->id != CODEC_ID_NONE) {
703
        if(!memcmp(uls->uid, *uid, 16))
704
            return uls->id;
705
        uls++;
706
    }
707
    return CODEC_ID_NONE;
708
}
709

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

    
716
    /* TODO: handle multiple material packages (OP3x) */
717
    for (i = 0; i < mxf->packages_count; i++) {
718
        if (mxf->packages[i]->type == MaterialPackage) {
719
            material_package = mxf->packages[i];
720
            break;
721
        }
722
    }
723
    if (!material_package) {
724
        av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
725
        return -1;
726
    }
727

    
728
    for (i = 0; i < material_package->tracks_count; i++) {
729
        MXFTrack *material_track = material_package->tracks[i];
730
        MXFTrack *source_track = NULL;
731
        MXFDescriptor *descriptor = NULL;
732
        MXFStructuralComponent *component = NULL;
733
        AVStream *st;
734

    
735
        /* TODO: handle multiple source clips */
736
        for (j = 0; j < material_track->sequence->structural_components_count; j++) {
737
            component = material_track->sequence->structural_components[j];
738
            /* TODO: handle timecode component */
739
            if (!component || component->type != SourceClip)
740
                continue;
741

    
742
            for (k = 0; k < mxf->packages_count; k++) {
743
                if (!memcmp(mxf->packages[k]->package_uid, component->source_package_uid, 16)) {
744
                    source_package = mxf->packages[k];
745
                    break;
746
                }
747
            }
748
            if (!source_package) {
749
                av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
750
                break;
751
            }
752
            for (k = 0; k < source_package->tracks_count; k++) {
753
                if (source_package->tracks[k]->track_id == component->source_track_id) {
754
                    source_track = source_package->tracks[k];
755
                    break;
756
                }
757
            }
758
            if (!source_track) {
759
                av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
760
                break;
761
            }
762
        }
763
        if (!source_track)
764
            continue;
765

    
766
        st = av_new_stream(mxf->fc, source_track->track_id);
767
        st->priv_data = source_track;
768
        st->duration = component->duration;
769
        if (st->duration == -1)
770
            st->duration = AV_NOPTS_VALUE;
771
        st->start_time = component->start_position;
772
        av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
773
#ifdef DEBUG
774
        PRINT_KEY(source_track->sequence->data_definition_ul);
775
#endif
776
        if (!memcmp(source_track->sequence->data_definition_ul, picture_essence_track_ul, 16))
777
            st->codec->codec_type = CODEC_TYPE_VIDEO;
778
        else if (!memcmp(source_track->sequence->data_definition_ul, sound_essence_track_ul, 16))
779
            st->codec->codec_type = CODEC_TYPE_AUDIO;
780
        else
781
            st->codec->codec_type = CODEC_TYPE_DATA;
782

    
783
        if (source_package->descriptor) {
784
            if (source_package->descriptor->sub_descriptors_count > 0) { /* SourcePackage has MultipleDescriptor */
785
                for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
786
                    if (source_package->descriptor->sub_descriptors[j]) {
787
                        if (source_package->descriptor->sub_descriptors[j]->linked_track_id == source_track->track_id) {
788
                            descriptor = source_package->descriptor->sub_descriptors[j];
789
                            break;
790
                        }
791
                    }
792
                }
793
            } else {
794
                descriptor = source_package->descriptor;
795
            }
796
        }
797
        if (!descriptor) {
798
            av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
799
            continue;
800
        }
801
#ifdef DEBUG
802
        PRINT_KEY(descriptor->essence_codec_ul);
803
#endif
804
        st->codec->codec_id = mxf_get_codec_id(mxf_codec_uls, &descriptor->essence_codec_ul);
805
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
806
            st->codec->width = descriptor->width;
807
            st->codec->height = descriptor->height;
808
            st->codec->bits_per_sample = descriptor->bits_per_sample; /* Uncompressed */
809
        } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
810
            st->codec->channels = descriptor->channels;
811
            st->codec->bits_per_sample = descriptor->bits_per_sample;
812
            st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
813
            /* TODO: implement CODEC_ID_RAWAUDIO */
814
            if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
815
                if (descriptor->bits_per_sample == 24)
816
                    st->codec->codec_id = CODEC_ID_PCM_S24LE;
817
                else if (descriptor->bits_per_sample == 32)
818
                    st->codec->codec_id = CODEC_ID_PCM_S32LE;
819
            } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
820
                if (descriptor->bits_per_sample == 24)
821
                    st->codec->codec_id = CODEC_ID_PCM_S24BE;
822
                else if (descriptor->bits_per_sample == 32)
823
                    st->codec->codec_id = CODEC_ID_PCM_S32BE;
824
            }
825
        }
826
    }
827
    return 0;
828
}
829

    
830
static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
831
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2F,0x00 }, mxf_read_metadata_preface },
832
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_metadata_content_storage },
833
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_metadata_source_package },
834
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_metadata_material_package },
835
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_metadata_sequence },
836
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_metadata_source_clip },
837
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_metadata_multiple_descriptor },
838
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_metadata_generic_descriptor }, /* Generic Sound */
839
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_metadata_generic_descriptor }, /* CDCI */
840
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_metadata_generic_descriptor }, /* RGBA */
841
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_metadata_generic_descriptor }, /* MPEG 2 Video */
842
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_metadata_generic_descriptor }, /* Wave */
843
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_metadata_generic_descriptor }, /* AES3 */
844
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_metadata_track }, /* Static Track */
845
    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_metadata_track }, /* Generic Track */
846
    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL },
847
};
848

    
849
static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
850
{
851
    MXFContext *mxf = s->priv_data;
852
    KLVPacket klv;
853

    
854
    mxf->fc = s;
855
    while (!url_feof(&s->pb)) {
856
        const MXFMetadataReadTableEntry *function;
857

    
858
        if (klv_read_packet(&klv, &s->pb) < 0) {
859
            av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
860
            return -1;
861
        }
862
#ifdef DEBUG
863
        PRINT_KEY(klv.key);
864
#endif
865
        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
866
            /* FIXME avoid seek */
867
            url_fseek(&s->pb, klv.offset, SEEK_SET);
868
            break;
869
        }
870

    
871
        for (function = mxf_metadata_read_table; function->read; function++) {
872
            if (IS_KLV_KEY(klv.key, function->key)) {
873
                if (function->read(mxf, &klv) < 0) {
874
                    av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
875
                    return -1;
876
                }
877
                break;
878
            }
879
        }
880
        if (!function->read)
881
            url_fskip(&s->pb, klv.length);
882
    }
883
    return mxf_parse_structural_metadata(mxf);
884
}
885

    
886
static int mxf_read_close(AVFormatContext *s)
887
{
888
    MXFContext *mxf = s->priv_data;
889
    int i, j, k;
890

    
891
    for (i = 0; i < mxf->packages_count; i++) {
892
        for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
893
            for (k = 0; k < mxf->packages[i]->tracks[j]->sequence->structural_components_count; k++)
894
                av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components[k]);
895
            av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components_refs);
896
            av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components);
897
            av_freep(&mxf->packages[i]->tracks[j]->sequence);
898
            av_freep(&mxf->packages[i]->tracks[j]);
899
        }
900
        av_freep(&mxf->packages[i]->tracks_refs);
901
        av_freep(&mxf->packages[i]->tracks);
902
        if (mxf->packages[i]->descriptor) {
903
            for (k = 0; k < mxf->packages[i]->descriptor->sub_descriptors_count; k++)
904
                av_freep(&mxf->packages[i]->descriptor->sub_descriptors[k]);
905
            av_freep(&mxf->packages[i]->descriptor->sub_descriptors_refs);
906
            av_freep(&mxf->packages[i]->descriptor->sub_descriptors);
907
        }
908
        av_freep(&mxf->packages[i]->descriptor);
909
        av_freep(&mxf->packages[i]);
910
    }
911
    av_freep(&mxf->packages_refs);
912
    av_freep(&mxf->packages);
913
    for (i = 0; i < mxf->essence_container_data_sets_count; i++)
914
        av_freep(&mxf->essence_container_data_sets[i]);
915
    av_freep(&mxf->essence_container_data_sets_refs);
916
    av_freep(&mxf->essence_container_data_sets);
917
    av_freep(&mxf->essence_containers_uls);
918
    return 0;
919
}
920

    
921
static int mxf_probe(AVProbeData *p) {
922
    uint8_t *bufp = p->buf;
923
    uint8_t *end = p->buf + p->buf_size;
924

    
925
    if (p->buf_size < sizeof(mxf_header_partition_pack_key))
926
        return 0;
927

    
928
    /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
929
    end -= sizeof(mxf_header_partition_pack_key);
930
    for (; bufp < end; bufp++) {
931
        if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
932
            return AVPROBE_SCORE_MAX;
933
    }
934
    return 0;
935
}
936

    
937

    
938
AVInputFormat mxf_demuxer = {
939
    "mxf",
940
    "MXF format",
941
    sizeof(MXFContext),
942
    mxf_probe,
943
    mxf_read_header,
944
    mxf_read_packet,
945
    mxf_read_close,
946
    NULL,
947
};