Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxf.c @ 8ce2215b

History | View | Annotate | Download (36.1 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
static const UID mxf_metadata_preface_key                  = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2F,0x00 };
147
static const UID mxf_metadata_content_storage_key          = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 };
148
static const UID mxf_metadata_source_package_key           = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 };
149
static const UID mxf_metadata_material_package_key         = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 };
150
static const UID mxf_metadata_sequence_key                 = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 };
151
static const UID mxf_metadata_source_clip_key              = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 };
152
static const UID mxf_metadata_multiple_descriptor_key      = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 };
153
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 };
154
static const UID mxf_metadata_cdci_descriptor_key          = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
155
static const UID mxf_metadata_rgba_descriptor_key          = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 };
156
static const UID mxf_metadata_mpegvideo_descriptor_key     = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
157
static const UID mxf_metadata_wave_descriptor_key          = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
158
static const UID mxf_metadata_static_track_key             = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 };
159
static const UID mxf_metadata_track_key                    = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3b,0x00 };
160

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

    
165
#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
166

    
167
#define PRINT_KEY(x) dprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", \
168
                             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])
169

    
170
static int64_t klv_decode_ber_length(ByteIOContext *pb)
171
{
172
    int64_t size = 0;
173
    uint8_t length = get_byte(pb);
174
    int type = length >> 7;
175

    
176
    if (type) { /* long form */
177
        int bytes_num = length & 0x7f;
178
        /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
179
        if (bytes_num > 8)
180
            return -1;
181
        while (bytes_num--)
182
            size = size << 8 | get_byte(pb);
183
    } else {
184
        size = length & 0x7f;
185
    }
186
    return size;
187
}
188

    
189
static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
190
{
191
    klv->offset = url_ftell(pb);
192
    get_buffer(pb, klv->key, 16);
193
    klv->length = klv_decode_ber_length(pb);
194
    return klv->length == -1 ? -1 : 0;
195
}
196

    
197
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
198
{
199
    int i;
200

    
201
    for (i = 0; i < s->nb_streams; i++) {
202
        MXFTrack *track = s->streams[i]->priv_data;
203
         /* SMPTE 379M 7.3 */
204
        if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
205
            return i;
206
    }
207
    return -1;
208
}
209

    
210
static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
211
{
212
    KLVPacket klv;
213

    
214
    while (!url_feof(&s->pb)) {
215
        if (klv_read_packet(&klv, &s->pb) < 0) {
216
            av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
217
            return -1;
218
        }
219
#ifdef DEBUG
220
        PRINT_KEY(klv.key);
221
#endif
222
        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
223
            av_get_packet(&s->pb, pkt, klv.length);
224
            pkt->stream_index = mxf_get_stream_index(s, &klv);
225
            return pkt->stream_index == -1 ? -1 : 0;
226
        } else
227
            url_fskip(&s->pb, klv.length);
228
    }
229
    return AVERROR_IO;
230
}
231

    
232
static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
233
{
234
    ByteIOContext *pb = &mxf->fc->pb;
235
    int bytes_read = 0;
236

    
237
    while (bytes_read < klv->length) {
238
        int tag = get_be16(pb);
239
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
240

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

    
264
static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
265
{
266
    ByteIOContext *pb = &mxf->fc->pb;
267
    int bytes_read = 0;
268

    
269
    while (bytes_read < klv->length) {
270
        int tag = get_be16(pb);
271
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
272

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

    
303
static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
304
{
305
    ByteIOContext *pb = &mxf->fc->pb;
306
    MXFStructuralComponent *source_clip = av_mallocz(sizeof(*source_clip));
307
    int bytes_read = 0;
308
    int i, j, k;
309

    
310
    source_clip->type = SourceClip;
311
    while (bytes_read < klv->length) {
312
        int tag = get_be16(pb);
313
        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
314

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

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

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

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

    
396
static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
397
{
398
    ByteIOContext *pb = &mxf->fc->pb;
399
    MXFTrack *track = av_mallocz(sizeof(*track));
400
    int bytes_read = 0;
401
    int i, j;
402

    
403
    while (bytes_read < klv->length) {
404
        int tag = get_be16(pb);
405
        int size = get_be16(pb); /* KLV specified by 0x53 */
406

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

    
443
static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
444
{
445
    ByteIOContext *pb = &mxf->fc->pb;
446
    MXFSequence *sequence = av_mallocz(sizeof(*sequence));
447
    int bytes_read = 0;
448
    int i, j;
449

    
450
    while (bytes_read < klv->length) {
451
        int tag = get_be16(pb);
452
        int size = get_be16(pb); /* KLV specified by 0x53 */
453

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

    
495
static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
496
{
497
    ByteIOContext *pb = &mxf->fc->pb;
498
    MXFPackage *package = av_mallocz(sizeof(*package));
499
    int bytes_read = 0;
500
    int i;
501

    
502
    package->type = SourcePackage;
503
    while (bytes_read < klv->length) {
504
        int tag = get_be16(pb);
505
        int size = get_be16(pb); /* KLV specified by 0x53 */
506

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

    
544
static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
545
{
546
    ByteIOContext *pb = &mxf->fc->pb;
547
    MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
548
    int bytes_read = 0;
549
    int i;
550

    
551
    while (bytes_read < klv->length) {
552
        int tag = get_be16(pb);
553
        int size = get_be16(pb); /* KLV specified by 0x53 */
554

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

    
586
static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
587
{
588
    ByteIOContext *pb = &mxf->fc->pb;
589
    MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
590
    int bytes_read = 0;
591
    int i, j;
592

    
593
    while (bytes_read < klv->length) {
594
        int tag = get_be16(pb);
595
        int size = get_be16(pb); /* KLV specified by 0x53 */
596

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

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

    
661
static const MXFCodecUL mxf_codec_uls[] = {
662
    /* PictureEssenceCoding */
663
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, CODEC_ID_MPEG2VIDEO }, /* I-Frame */
664
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* Long GoP */
665
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* Long GoP */
666
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, CODEC_ID_MPEG2VIDEO }, /* D-10 30Mbps PAL */
667
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, CODEC_ID_MPEG2VIDEO }, /* D-10 50Mbps PAL */
668
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 },    CODEC_ID_DVVIDEO }, /* DVCPRO50 PAL */
669
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 },    CODEC_ID_DVVIDEO }, /* DVCPRO25 PAL */
670
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 },    CODEC_ID_DVVIDEO }, /* DV25 IEC PAL */
671
  //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },     CODEC_ID_JPEG2K }, /* JPEG2000 Codestream */
672
    /* SoundEssenceCompression */
673
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 },  CODEC_ID_PCM_S16LE },
674
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x02,0x02,0x01,0x7E,0x00,0x00,0x00 },  CODEC_ID_PCM_S16BE }, /* From Omneon MXF file */
675
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 },   CODEC_ID_PCM_ALAW },
676
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 },        CODEC_ID_AC3 },
677
    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 },        CODEC_ID_MP2 }, /* MP2 or MP3 */
678
  //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 },    CODEC_ID_DOLBY_E }, /* Dolby-E */
679
};
680

    
681
static enum CodecID mxf_get_codec_id(const MXFCodecUL *uls, UID *uid)
682
{
683
    while (uls->id != CODEC_ID_NONE) {
684
        if(!memcmp(uls->uid, *uid, 16))
685
            return uls->id;
686
        uls++;
687
    }
688
    return CODEC_ID_NONE;
689
}
690

    
691
static int mxf_parse_structural_metadata(MXFContext *mxf)
692
{
693
    MXFPackage *material_package = NULL;
694
    MXFPackage *source_package = NULL;
695
    int i, j, k;
696

    
697
    /* TODO: handle multiple material packages (OP3x) */
698
    for (i = 0; i < mxf->packages_count; i++) {
699
        if (mxf->packages[i]->type == MaterialPackage) {
700
            material_package = mxf->packages[i];
701
            break;
702
        }
703
    }
704
    if (!material_package) {
705
        av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
706
        return -1;
707
    }
708

    
709
    for (i = 0; i < material_package->tracks_count; i++) {
710
        MXFTrack *material_track = material_package->tracks[i];
711
        MXFTrack *source_track = NULL;
712
        MXFDescriptor *descriptor = NULL;
713
        MXFStructuralComponent *component = NULL;
714
        AVStream *st;
715

    
716
        /* TODO: handle multiple source clips */
717
        for (j = 0; j < material_track->sequence->structural_components_count; j++) {
718
            component = material_track->sequence->structural_components[j];
719
            /* TODO: handle timecode component */
720
            if (!component || component->type != SourceClip)
721
                continue;
722

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

    
747
        st = av_new_stream(mxf->fc, source_track->track_id);
748
        st->priv_data = source_track;
749
        st->duration = component->duration;
750
        if (st->duration == -1)
751
            st->duration = AV_NOPTS_VALUE;
752
        st->start_time = component->start_position;
753
        av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
754
#ifdef DEBUG
755
        PRINT_KEY(source_track->sequence->data_definition_ul);
756
#endif
757
        if (!memcmp(source_track->sequence->data_definition_ul, picture_essence_track_ul, 16))
758
            st->codec->codec_type = CODEC_TYPE_VIDEO;
759
        else if (!memcmp(source_track->sequence->data_definition_ul, sound_essence_track_ul, 16))
760
            st->codec->codec_type = CODEC_TYPE_AUDIO;
761
        else
762
            st->codec->codec_type = CODEC_TYPE_DATA;
763

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

    
810
static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
811
{
812
    MXFContext *mxf = s->priv_data;
813
    KLVPacket klv;
814
    int ret = 0;
815

    
816
    mxf->fc = s;
817
    while (!url_feof(&s->pb)) {
818
        if (klv_read_packet(&klv, &s->pb) < 0) {
819
            av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
820
            return -1;
821
        }
822
#ifdef DEBUG
823
        PRINT_KEY(klv.key);
824
#endif
825
        if (IS_KLV_KEY(klv.key, mxf_metadata_track_key))
826
            ret = mxf_read_metadata_track(mxf, &klv);
827
        else if (IS_KLV_KEY(klv.key, mxf_metadata_static_track_key))
828
            ret = mxf_read_metadata_track(mxf, &klv);
829
        else if (IS_KLV_KEY(klv.key, mxf_metadata_source_package_key))
830
            ret = mxf_read_metadata_source_package(mxf, &klv);
831
        else if (IS_KLV_KEY(klv.key, mxf_metadata_sequence_key))
832
            ret = mxf_read_metadata_sequence(mxf, &klv);
833
        else if (IS_KLV_KEY(klv.key, mxf_metadata_material_package_key))
834
            ret = mxf_read_metadata_material_package(mxf, &klv);
835
        else if (IS_KLV_KEY(klv.key, mxf_metadata_source_clip_key))
836
            ret = mxf_read_metadata_source_clip(mxf, &klv);
837
        else if (IS_KLV_KEY(klv.key, mxf_metadata_multiple_descriptor_key))
838
            ret = mxf_read_metadata_multiple_descriptor(mxf, &klv);
839
        else if (IS_KLV_KEY(klv.key, mxf_metadata_wave_descriptor_key))
840
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
841
        else if (IS_KLV_KEY(klv.key, mxf_metadata_mpegvideo_descriptor_key))
842
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
843
        else if (IS_KLV_KEY(klv.key, mxf_metadata_cdci_descriptor_key))
844
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
845
        else if (IS_KLV_KEY(klv.key, mxf_metadata_rgba_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
            av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
861
            return ret;
862
        }
863
    }
864
    return mxf_parse_structural_metadata(mxf);
865
}
866

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

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

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

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

    
913

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