Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxf.c @ 5e441e31

History | View | Annotate | Download (35.4 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
            av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
227
            return -1;
228
        }
229
        if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
230
            av_get_packet(&s->pb, pkt, klv.length);
231
            pkt->stream_index = mxf_get_stream_index(s, &klv);
232
            if (pkt->stream_index == -1)
233
                return -1;
234
            return 0;
235
        } else
236
            url_fskip(&s->pb, klv.length);
237
    }
238
    return AVERROR_IO;
239
}
240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
871
static int mxf_read_close(AVFormatContext *s)
872
{
873
    MXFContext *mxf = s->priv_data;
874
    int i, j, k;
875

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

    
906
static int mxf_probe(AVProbeData *p) {
907
    /* KLV packet describing MXF header partition pack */
908
    if (p->buf_size < sizeof(mxf_header_partition_pack_key))
909
        return 0;
910

    
911
    if (IS_KLV_KEY(p->buf, mxf_header_partition_pack_key))
912
        return AVPROBE_SCORE_MAX;
913
    else
914
        return 0;
915
}
916

    
917

    
918
AVInputFormat mxf_demuxer = {
919
    "mxf",
920
    "MXF format",
921
    sizeof(MXFContext),
922
    mxf_probe,
923
    mxf_read_header,
924
    mxf_read_packet,
925
    mxf_read_close,
926
    NULL,
927
};