Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxf.c @ 743d772c

History | View | Annotate | Download (35.2 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
#include "dsputil.h"
49

    
50
typedef DECLARE_ALIGNED_16(uint8_t, UID[16]);
51

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

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

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

    
72
typedef struct MXFSequence {
73
    UID uid;
74
    UID data_definition_ul;
75
    MXFStructuralComponent **structural_components;
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
    MXFSequence *sequence; /* mandatory, and only one */
84
    UID sequence_ref;
85
    int track_id;
86
    int track_number;
87
    AVRational edit_rate;
88
} MXFTrack;
89

    
90
typedef struct MXFDescriptor {
91
    UID uid;
92
    UID essence_container;
93
    UID essence_compression;
94
    enum CodecType codec_type;
95
    AVRational sample_rate;
96
    AVRational aspect_ratio;
97
    int width;
98
    int height;
99
    int channels;
100
    int bits_per_sample;
101
    struct MXFDescriptor **sub_descriptors;
102
    UID *sub_descriptors_refs;
103
    int sub_descriptors_count;
104
    int block_align;
105
    int linked_track_id;
106
} MXFDescriptor;
107

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

    
119
typedef struct MXFEssenceContainerData {
120
    UID uid;
121
    UID linked_package_uid;
122
} MXFEssenceContainerData;
123

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

    
138
typedef struct KLVPacket {
139
    UID key;
140
    offset_t offset;
141
    uint64_t length;
142
} KLVPacket;
143

    
144
typedef struct MXFCodecUL {
145
    UID uid;
146
    enum CodecID id;
147
} MXFCodecUL;
148

    
149
static const UID mxf_metadata_preface_key                  = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x2F, 0x00 };
150
static const UID mxf_metadata_content_storage_key          = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x18, 0x00 };
151
static const UID mxf_metadata_source_package_key           = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x37, 0x00 };
152
static const UID mxf_metadata_material_package_key         = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x36, 0x00 };
153
static const UID mxf_metadata_sequence_key                 = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F, 0x00 };
154
static const UID mxf_metadata_source_clip_key              = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x11, 0x00 };
155
static const UID mxf_metadata_multiple_descriptor_key      = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x44, 0x00 };
156
static const UID mxf_metadata_generic_sound_descriptor_key = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x42, 0x00 };
157
static const UID mxf_metadata_cdci_descriptor_key          = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x28, 0x00 };
158
static const UID mxf_metadata_mpegvideo_descriptor_key     = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x51, 0x00 };
159
static const UID mxf_metadata_wave_descriptor_key          = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x48, 0x00 };
160
static const UID mxf_metadata_static_track_key             = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x3A, 0x00 };
161
static const UID mxf_metadata_track_key                    = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x3b, 0x00 };
162

    
163
/* partial keys to match */
164
static const uint8_t mxf_header_partition_pack_key[]       = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01, 0x01, 0x02 };
165
static const uint8_t mxf_essence_element_key[]             = { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x02, 0x01 };
166

    
167
#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
168

    
169
#define PRINT_KEY(x) \
170
do { \
171
    int iterpk; \
172
    for (iterpk = 0; iterpk < 16; iterpk++) { \
173
        av_log(NULL, AV_LOG_DEBUG, "%02X ", x[iterpk]); \
174
    } \
175
    av_log(NULL, AV_LOG_DEBUG, "\n"); \
176
} while (0); \
177

    
178
static int64_t klv_decode_ber_length(ByteIOContext *pb)
179
{
180
    int64_t size = 0;
181
    uint8_t length = get_byte(pb);
182
    int type = length >> 7;
183

    
184
    if (type) { /* long form */
185
        int bytes_num = length & 0x7f;
186
        /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
187
        if (bytes_num > 8)
188
            return -1;
189
        while (bytes_num--)
190
            size = size << 8 | get_byte(pb);
191
    } else {
192
        size = length & 0x7f;
193
    }
194
    return size;
195
}
196

    
197
static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
198
{
199
    klv->offset = url_ftell(pb);
200
    get_buffer(pb, klv->key, 16);
201
    klv->length = klv_decode_ber_length(pb);
202
    if (klv->length == -1)
203
        return -1;
204
    else
205
        return 0;
206
}
207

    
208
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
209
{
210
    int id = BE_32(klv->key + 12); /* SMPTE 379M 7.3 */
211
    int i;
212

    
213
    for (i = 0; i < s->nb_streams; i++) {
214
        if (s->streams[i]->id == id)
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
            return -1;
227
        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
228
            av_get_packet(&s->pb, pkt, klv.length);
229
            pkt->stream_index = mxf_get_stream_index(s, &klv);
230
            if (pkt->stream_index == -1)
231
                return -1;
232
            return 0;
233
        } else
234
            url_fskip(&s->pb, klv.length);
235
    }
236
    return AVERROR_IO;
237
}
238

    
239
static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
240
{
241
    ByteIOContext *pb = &mxf->fc->pb;
242
    int bytes_read = 0;
243

    
244
    while (bytes_read < klv->length) {
245
        int tag = get_be16(pb);
246
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
247

    
248
        switch (tag) {
249
        case 0x3B03:
250
            get_buffer(pb, mxf->content_storage_uid, 16);
251
            break;
252
        case 0x3B09:
253
            get_buffer(pb, mxf->operational_pattern_ul, 16);
254
            break;
255
        case 0x3B0A:
256
            mxf->essence_containers_uls_count = get_be32(pb);
257
            if (mxf->essence_containers_uls_count >= UINT_MAX / sizeof(UID))
258
                return -1;
259
            mxf->essence_containers_uls = av_malloc(mxf->essence_containers_uls_count * sizeof(UID));
260
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
261
            get_buffer(pb, mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
262
            break;
263
        default:
264
            url_fskip(pb, size);
265
        }
266
        bytes_read += size + 4;
267
    }
268
    return 0;
269
}
270

    
271
static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
272
{
273
    ByteIOContext *pb = &mxf->fc->pb;
274
    int bytes_read = 0;
275

    
276
    while (bytes_read < klv->length) {
277
        int tag = get_be16(pb);
278
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
279

    
280
        dprintf("tag 0x%04X, size %d\n", tag, size);
281
        switch (tag) {
282
        case 0x1901:
283
            mxf->packages_count = get_be32(pb);
284
            if (mxf->packages_count >= UINT_MAX / sizeof(UID) ||
285
                mxf->packages_count >= UINT_MAX / sizeof(*mxf->packages))
286
                return -1;
287
            mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
288
            mxf->packages = av_mallocz(mxf->packages_count * sizeof(*mxf->packages));
289
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
290
            get_buffer(pb, mxf->packages_refs, mxf->packages_count * sizeof(UID));
291
            break;
292
        case 0x1902:
293
            mxf->essence_container_data_sets_count = get_be32(pb);
294
            if (mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(UID) ||
295
                mxf->essence_container_data_sets_count >= UINT_MAX / sizeof(*mxf->essence_container_data_sets))
296
                return -1;
297
            mxf->essence_container_data_sets_refs = av_malloc(mxf->essence_container_data_sets_count * sizeof(UID));
298
            mxf->essence_container_data_sets = av_mallocz(mxf->essence_container_data_sets_count * sizeof(*mxf->essence_container_data_sets));
299
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
300
            get_buffer(pb, mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
301
            break;
302
        default:
303
            url_fskip(pb, size);
304
        }
305
        bytes_read += size + 4;
306
    }
307
    return 0;
308
}
309

    
310
static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
311
{
312
    ByteIOContext *pb = &mxf->fc->pb;
313
    MXFStructuralComponent *source_clip = av_mallocz(sizeof(*source_clip));
314
    int bytes_read = 0;
315
    int i, j, k;
316

    
317
    source_clip->type = SourceClip;
318
    while (bytes_read < klv->length) {
319
        int tag = get_be16(pb);
320
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
321

    
322
        dprintf("tag 0x%04X, size %d\n", tag, size);
323
        switch (tag) {
324
        case 0x3C0A:
325
            get_buffer(pb, source_clip->uid, 16);
326
            break;
327
        case 0x0202:
328
            source_clip->duration = get_be64(pb);
329
            break;
330
        case 0x1201:
331
            source_clip->start_position = get_be64(pb);
332
            break;
333
        case 0x1101:
334
            /* UMID, only get last 16 bytes */
335
            url_fskip(pb, 16);
336
            get_buffer(pb, source_clip->source_package_uid, 16);
337
            break;
338
        case 0x1102:
339
            source_clip->source_track_id = get_be32(pb);
340
            break;
341
        default:
342
            url_fskip(pb, size);
343
        }
344
        bytes_read += size + 4;
345
    }
346
    for (i = 0; i < mxf->packages_count; i++) {
347
        if (mxf->packages[i]) {
348
            for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
349
                if (mxf->packages[i]->tracks[j] && mxf->packages[i]->tracks[j]->sequence) {
350
                    for (k = 0; k < mxf->packages[i]->tracks[j]->sequence->structural_components_count; k++) {
351
                        if (!memcmp(mxf->packages[i]->tracks[j]->sequence->structural_components_refs[k], source_clip->uid, 16)) {
352
                            mxf->packages[i]->tracks[j]->sequence->structural_components[k] = source_clip;
353
                            return 0;
354
                        }
355
                    }
356
                }
357
            }
358
        }
359
    }
360
    return -1;
361
}
362

    
363
static int mxf_read_metadata_material_package(MXFContext *mxf, KLVPacket *klv)
364
{
365
    ByteIOContext *pb = &mxf->fc->pb;
366
    MXFPackage *package = av_mallocz(sizeof(*package));
367
    int bytes_read = 0;
368
    int i;
369

    
370
    package->type = MaterialPackage;
371
    while (bytes_read < klv->length) {
372
        int tag = get_be16(pb);
373
        int size = get_be16(pb); /* KLV specified by 0x53 */
374

    
375
        switch (tag) {
376
        case 0x3C0A:
377
            get_buffer(pb, package->uid, 16);
378
            break;
379
        case 0x4403:
380
            package->tracks_count = get_be32(pb);
381
            if (package->tracks_count >= UINT_MAX / sizeof(UID) ||
382
                package->tracks_count >= UINT_MAX / sizeof(*package->tracks))
383
                return -1;
384
            package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
385
            package->tracks = av_mallocz(package->tracks_count * sizeof(*package->tracks));
386
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
387
            get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
388
            break;
389
        default:
390
            url_fskip(pb, size);
391
        }
392
        bytes_read += size + 4;
393
    }
394
    for (i = 0; i < mxf->packages_count; i++) {
395
        if (!memcmp(mxf->packages_refs[i], package->uid, 16)) {
396
            mxf->packages[i] = package;
397
            return 0;
398
        }
399
    }
400
    return -1;
401
}
402

    
403
static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
404
{
405
    ByteIOContext *pb = &mxf->fc->pb;
406
    MXFTrack *track = av_mallocz(sizeof(*track));
407
    int bytes_read = 0;
408
    int i, j;
409

    
410
    while (bytes_read < klv->length) {
411
        int tag = get_be16(pb);
412
        int size = get_be16(pb); /* KLV specified by 0x53 */
413

    
414
        dprintf("tag 0x%04X, size %d\n", tag, size);
415
        switch (tag) {
416
        case 0x3C0A:
417
            get_buffer(pb, track->uid, 16);
418
            break;
419
        case 0x4801:
420
            track->track_id = get_be32(pb);
421
            break;
422
        case 0x4804:
423
            track->track_number = get_be32(pb);
424
            dprintf("track number 0x%04X\n", track->track_number);
425
            break;
426
        case 0x4B01:
427
            track->edit_rate.den = get_be32(pb);
428
            track->edit_rate.num = get_be32(pb);
429
            break;
430
        case 0x4803:
431
            get_buffer(pb, track->sequence_ref, 16);
432
            break;
433
        default:
434
            url_fskip(pb, size);
435
        }
436
        bytes_read += size + 4;
437
    }
438
    for (i = 0; i < mxf->packages_count; i++) {
439
        if (mxf->packages[i]) {
440
            for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
441
                if (!memcmp(mxf->packages[i]->tracks_refs[j], track->uid, 16)) {
442
                    mxf->packages[i]->tracks[j] = track;
443
                    return 0;
444
                }
445
            }
446
        }
447
    }
448
    return -1;
449
}
450

    
451
static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
452
{
453
    ByteIOContext *pb = &mxf->fc->pb;
454
    MXFSequence *sequence = av_mallocz(sizeof(*sequence));
455
    int bytes_read = 0;
456
    int i, j;
457

    
458
    while (bytes_read < klv->length) {
459
        int tag = get_be16(pb);
460
        int size = get_be16(pb); /* KLV specified by 0x53 */
461

    
462
        dprintf("tag 0x%04X, size %d\n", tag, size);
463
        switch (tag) {
464
        case 0x3C0A:
465
            get_buffer(pb, sequence->uid, 16);
466
            break;
467
        case 0x0202:
468
            sequence->duration = get_be64(pb);
469
            break;
470
        case 0x0201:
471
            get_buffer(pb, sequence->data_definition_ul, 16);
472
            break;
473
        case 0x1001:
474
            sequence->structural_components_count = get_be32(pb);
475
            if (sequence->structural_components_count >= UINT_MAX / sizeof(UID) ||
476
                sequence->structural_components_count >= UINT_MAX / sizeof(*sequence->structural_components))
477
                return -1;
478
            sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
479
            sequence->structural_components = av_mallocz(sequence->structural_components_count * sizeof(*sequence->structural_components));
480
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
481
            get_buffer(pb, sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
482
            break;
483
        default:
484
            url_fskip(pb, size);
485
        }
486
        bytes_read += size + 4;
487
    }
488
    for (i = 0; i < mxf->packages_count; i++) {
489
        if (mxf->packages[i]) {
490
            for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
491
                if (mxf->packages[i]->tracks[j]) {
492
                    if (!memcmp(mxf->packages[i]->tracks[j]->sequence_ref, sequence->uid, 16)) {
493
                        mxf->packages[i]->tracks[j]->sequence = sequence;
494
                        return 0;
495
                    }
496
                }
497
            }
498
        }
499
    }
500
    return -1;
501
}
502

    
503
static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
504
{
505
    ByteIOContext *pb = &mxf->fc->pb;
506
    MXFPackage *package = av_mallocz(sizeof(*package));
507
    int bytes_read = 0;
508
    int i;
509

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

    
515
        dprintf("tag 0x%04X, size %d\n", tag, size);
516
        switch (tag) {
517
        case 0x3C0A:
518
            get_buffer(pb, package->uid, 16);
519
            break;
520
        case 0x4403:
521
            package->tracks_count = get_be32(pb);
522
            if (package->tracks_count >= UINT_MAX / sizeof(UID) ||
523
                package->tracks_count >= UINT_MAX / sizeof(*package->tracks))
524
                return -1;
525
            package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
526
            package->tracks = av_mallocz(package->tracks_count * sizeof(*package->tracks));
527
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
528
            get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
529
            break;
530
        case 0x4401:
531
            /* UMID, only get last 16 bytes */
532
            url_fskip(pb, 16);
533
            get_buffer(pb, package->package_uid, 16);
534
            break;
535
        case 0x4701:
536
            get_buffer(pb, package->descriptor_ref, 16);
537
            break;
538
        default:
539
            url_fskip(pb, size);
540
        }
541
        bytes_read += size + 4;
542
    }
543
    for (i = 0; i < mxf->packages_count; i++) {
544
        if (!memcmp(mxf->packages_refs[i], package->uid, 16)) {
545
            mxf->packages[i] = package;
546
            return 0;
547
        }
548
    }
549
    return -1;
550
}
551

    
552
static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
553
{
554
    ByteIOContext *pb = &mxf->fc->pb;
555
    MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
556
    int bytes_read = 0;
557
    int i;
558

    
559
    while (bytes_read < klv->length) {
560
        int tag = get_be16(pb);
561
        int size = get_be16(pb); /* KLV specified by 0x53 */
562

    
563
        dprintf("tag 0x%04X, size %d\n", tag, size);
564
        switch (tag) {
565
        case 0x3C0A:
566
            get_buffer(pb, descriptor->uid, 16);
567
            break;
568
        case 0x3F01:
569
            descriptor->sub_descriptors_count = get_be32(pb);
570
            if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID) ||
571
                descriptor->sub_descriptors_count >= UINT_MAX / sizeof(*descriptor->sub_descriptors))
572
                return -1;
573
            descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
574
            descriptor->sub_descriptors = av_mallocz(descriptor->sub_descriptors_count * sizeof(*descriptor->sub_descriptors));
575
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
576
            get_buffer(pb, descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
577
            break;
578
        default:
579
            url_fskip(pb, size);
580
        }
581
        bytes_read += size + 4;
582
    }
583
    for (i = 0; i < mxf->packages_count; i++) {
584
        if (mxf->packages[i]) {
585
            if (!memcmp(mxf->packages[i]->descriptor_ref, descriptor->uid, 16)) {
586
                mxf->packages[i]->descriptor = descriptor;
587
                return 0;
588
            }
589
        }
590
    }
591
    return -1;
592
}
593

    
594
static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
595
{
596
    ByteIOContext *pb = &mxf->fc->pb;
597
    MXFDescriptor *desc = av_mallocz(sizeof(*desc));
598
    int bytes_read = 0;
599
    int i, j;
600

    
601
    while (bytes_read < klv->length) {
602
        int tag = get_be16(pb);
603
        int size = get_be16(pb); /* KLV specified by 0x53 */
604

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

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

    
674
static const MXFCodecUL mxf_codec_uls[] = {
675
    /* PictureEssenceCoding */
676
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, CODEC_ID_MPEG2VIDEO }, /* I-Frame */
677
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* Long GoP */
678
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* Long GoP */
679
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, CODEC_ID_MPEG2VIDEO }, /* D-10 30Mbps PAL */
680
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, CODEC_ID_MPEG2VIDEO }, /* D-10 50Mbps PAL */
681
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },    CODEC_ID_DVVIDEO }, /* DVCPRO50 PAL */
682
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },    CODEC_ID_DVVIDEO }, /* DVCPRO25 PAL */
683
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },    CODEC_ID_DVVIDEO }, /* DV25 IEC PAL */
684
    /* SoundEssenceCompression */
685
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE },
686
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x02,0x02,0x01,0x7E,0x00,0x00,0x00 },  CODEC_ID_PCM_S16BE }, /* From Omneon MXF file */
687
};
688

    
689
static enum CodecID mxf_get_codec_id(const MXFCodecUL *uls, UID *uid)
690
{
691
    while (uls->id != CODEC_ID_NONE) {
692
        if(!memcmp(uls->uid, *uid, 16))
693
            return uls->id;
694
        uls++;
695
    }
696
    return CODEC_ID_NONE;
697
}
698

    
699
static int mxf_parse_structural_metadata(MXFContext *mxf)
700
{
701
    MXFPackage *material_package = NULL;
702
    MXFPackage *source_package = NULL;
703
    int i, j, k;
704

    
705
    /* TODO: handle multiple material packages (OP3x) */
706
    for (i = 0; i < mxf->packages_count; i++) {
707
        if (mxf->packages[i]->type == MaterialPackage) {
708
            material_package = mxf->packages[i];
709
            break;
710
        }
711
    }
712
    if (!material_package) {
713
        av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
714
        return -1;
715
    }
716

    
717
    for (i = 0; i < material_package->tracks_count; i++) {
718
        MXFTrack *material_track = material_package->tracks[i];
719
        MXFTrack *source_track = NULL;
720
        MXFDescriptor *descriptor = NULL;
721
        MXFStructuralComponent *component = NULL;
722
        AVStream *st;
723

    
724
        /* TODO: handle multiple source clips */
725
        for (j = 0; j < material_track->sequence->structural_components_count; j++) {
726
            component = material_track->sequence->structural_components[j];
727
            /* TODO: handle timecode component */
728
            if (!component || component->type != SourceClip)
729
                continue;
730

    
731
            for (k = 0; k < mxf->packages_count; k++) {
732
                if (!memcmp(mxf->packages[k]->package_uid, component->source_package_uid, 16)) {
733
                    source_package = mxf->packages[k];
734
                    break;
735
                }
736
            }
737
            if (!source_package) {
738
                av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
739
                break;
740
            }
741
            for (k = 0; k < source_package->tracks_count; k++) {
742
                if (source_package->tracks[k]->track_id == component->source_track_id) {
743
                    source_track = source_package->tracks[k];
744
                    break;
745
                }
746
            }
747
            if (!source_track) {
748
                av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
749
                break;
750
            }
751
        }
752
        if (!source_track)
753
            continue;
754

    
755
        st = av_new_stream(mxf->fc, source_track->track_number);
756
        st->duration = component->duration;
757
        if (st->duration == -1)
758
            st->duration = AV_NOPTS_VALUE;
759
        st->start_time = component->start_position;
760
        av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
761
#ifdef DEBUG
762
        PRINT_KEY(source_track->sequence->data_definition_ul);
763
#endif
764
        if (!memcmp(source_track->sequence->data_definition_ul, picture_essence_track_ul, 16))
765
            st->codec->codec_type = CODEC_TYPE_VIDEO;
766
        else if (!memcmp(source_track->sequence->data_definition_ul, sound_essence_track_ul, 16))
767
            st->codec->codec_type = CODEC_TYPE_AUDIO;
768
        else
769
            st->codec->codec_type = CODEC_TYPE_DATA;
770

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

    
817
static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
818
{
819
    MXFContext *mxf = s->priv_data;
820
    KLVPacket klv;
821
    int ret = 0;
822

    
823
    mxf->fc = s;
824
    while (!url_feof(&s->pb)) {
825
        if (klv_read_packet(&klv, &s->pb) < 0)
826
            return -1;
827
        if (IS_KLV_KEY(klv.key, mxf_metadata_track_key))
828
            ret = mxf_read_metadata_track(mxf, &klv);
829
        else if (IS_KLV_KEY(klv.key, mxf_metadata_static_track_key))
830
            ret = mxf_read_metadata_track(mxf, &klv);
831
        else if (IS_KLV_KEY(klv.key, mxf_metadata_source_package_key))
832
            ret = mxf_read_metadata_source_package(mxf, &klv);
833
        else if (IS_KLV_KEY(klv.key, mxf_metadata_sequence_key))
834
            ret = mxf_read_metadata_sequence(mxf, &klv);
835
        else if (IS_KLV_KEY(klv.key, mxf_metadata_material_package_key))
836
            ret = mxf_read_metadata_material_package(mxf, &klv);
837
        else if (IS_KLV_KEY(klv.key, mxf_metadata_source_clip_key))
838
            ret = mxf_read_metadata_source_clip(mxf, &klv);
839
        else if (IS_KLV_KEY(klv.key, mxf_metadata_multiple_descriptor_key))
840
            ret = mxf_read_metadata_multiple_descriptor(mxf, &klv);
841
        else if (IS_KLV_KEY(klv.key, mxf_metadata_wave_descriptor_key))
842
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
843
        else if (IS_KLV_KEY(klv.key, mxf_metadata_mpegvideo_descriptor_key))
844
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
845
        else if (IS_KLV_KEY(klv.key, mxf_metadata_cdci_descriptor_key))
846
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
847
        else if (IS_KLV_KEY(klv.key, mxf_metadata_generic_sound_descriptor_key))
848
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
849
        else if (IS_KLV_KEY(klv.key, mxf_metadata_preface_key))
850
            ret = mxf_read_metadata_preface(mxf, &klv);
851
        else if (IS_KLV_KEY(klv.key, mxf_metadata_content_storage_key))
852
            ret = mxf_read_metadata_content_storage(mxf, &klv);
853
        else if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
854
            /* FIXME avoid seek */
855
            url_fseek(&s->pb, klv.offset, SEEK_SET);
856
            break;
857
        } else
858
            url_fskip(&s->pb, klv.length);
859
        if (ret < 0)
860
            return ret;
861
    }
862
    return mxf_parse_structural_metadata(mxf);
863
}
864

    
865
static int mxf_read_close(AVFormatContext *s)
866
{
867
    MXFContext *mxf = s->priv_data;
868
    int i, j, k;
869

    
870
    for (i = 0; i < mxf->packages_count; i++) {
871
        for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
872
            for (k = 0; k < mxf->packages[i]->tracks[j]->sequence->structural_components_count; k++)
873
                av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components[k]);
874
            av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components_refs);
875
            av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components);
876
            av_freep(&mxf->packages[i]->tracks[j]->sequence);
877
            av_freep(&mxf->packages[i]->tracks[j]);
878
        }
879
        av_freep(&mxf->packages[i]->tracks_refs);
880
        av_freep(&mxf->packages[i]->tracks);
881
        if (mxf->packages[i]->descriptor) {
882
            for (k = 0; k < mxf->packages[i]->descriptor->sub_descriptors_count; k++)
883
                av_freep(&mxf->packages[i]->descriptor->sub_descriptors[k]);
884
            av_freep(&mxf->packages[i]->descriptor->sub_descriptors_refs);
885
            av_freep(&mxf->packages[i]->descriptor->sub_descriptors);
886
        }
887
        av_freep(&mxf->packages[i]->descriptor);
888
        av_freep(&mxf->packages[i]);
889
    }
890
    av_freep(&mxf->packages_refs);
891
    av_freep(&mxf->packages);
892
    for (i = 0; i < mxf->essence_container_data_sets_count; i++)
893
        av_freep(&mxf->essence_container_data_sets[i]);
894
    av_freep(&mxf->essence_container_data_sets_refs);
895
    av_freep(&mxf->essence_container_data_sets);
896
    av_freep(&mxf->essence_containers_uls);
897
    return 0;
898
}
899

    
900
static int mxf_probe(AVProbeData *p) {
901
    /* KLV packet describing MXF header partition pack */
902
    if (p->buf_size < sizeof(mxf_header_partition_pack_key))
903
        return 0;
904

    
905
    if (IS_KLV_KEY(p->buf, mxf_header_partition_pack_key))
906
        return AVPROBE_SCORE_MAX;
907
    else
908
        return 0;
909
}
910

    
911

    
912
AVInputFormat mxf_demuxer = {
913
    "mxf",
914
    "MXF format",
915
    sizeof(MXFContext),
916
    mxf_probe,
917
    mxf_read_header,
918
    mxf_read_packet,
919
    mxf_read_close,
920
    NULL,
921
};