Revision 743d772c libavformat/mxf.c

View differences:

libavformat/mxf.c
30 30
 * Principle
31 31
 * Search for Track numbers which will identify essence element KLV packets.
32 32
 * Search for SourcePackage which define tracks which contains Track numbers.
33
 * Material Package tracks does not contain Tracks numbers.
33
 * Material Package contains tracks with reference to SourcePackage tracks.
34 34
 * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
35 35
 * Assign Descriptors to correct Tracks.
36 36
 *
37
 * Preliminary demuxer, only OP1A supported and some files might not work at all.
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
38 43
 */
39 44

  
40 45
//#define DEBUG
41 46

  
42 47
#include "avformat.h"
43 48
#include "dsputil.h"
44
#include "riff.h"
45 49

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

  
48
typedef struct {
49
    AVStream *stream;
50
    UID track_uid;
51
    UID sequence_uid;
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;
52 85
    int track_id;
53 86
    int track_number;
87
    AVRational edit_rate;
54 88
} MXFTrack;
55 89

  
56
typedef struct {
90
typedef struct MXFDescriptor {
91
    UID uid;
57 92
    UID essence_container;
58 93
    UID essence_compression;
59 94
    enum CodecType codec_type;
......
63 98
    int height;
64 99
    int channels;
65 100
    int bits_per_sample;
101
    struct MXFDescriptor **sub_descriptors;
102
    UID *sub_descriptors_refs;
103
    int sub_descriptors_count;
66 104
    int block_align;
67 105
    int linked_track_id;
68
    int kind;
69 106
} MXFDescriptor;
70 107

  
71
typedef struct {
72
    AVFormatContext *fc;
73
    MXFTrack *tracks;
74
    MXFDescriptor *descriptors;
75
    int descriptors_count;
108
typedef struct MXFPackage {
109
    UID uid;
110
    UID package_uid;
111
    MXFTrack **tracks;
112
    UID *tracks_refs;
76 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;
77 136
} MXFContext;
78 137

  
79
typedef struct {
138
typedef struct KLVPacket {
80 139
    UID key;
81 140
    offset_t offset;
82 141
    uint64_t length;
83 142
} KLVPacket;
84 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 };
85 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 };
86 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 };
87 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 };
88 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 };
89 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 };
90 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 };
91 161
static const UID mxf_metadata_track_key                    = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x3b, 0x00 };
92 162

  
93 163
/* partial keys to match */
......
166 236
    return AVERROR_IO;
167 237
}
168 238

  
169
static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
239
static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
170 240
{
171 241
    ByteIOContext *pb = &mxf->fc->pb;
172
    AVRational time_base = (AVRational){0, 0};
173
    uint8_t sequence_uid[16];
174
    uint8_t track_uid[16];
175
    int track_number = 0;
176
    int track_id = 0;
177 242
    int bytes_read = 0;
178
    int i;
179 243

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

  
184 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;
185 419
        case 0x4801:
186
            track_id = get_be32(pb);
420
            track->track_id = get_be32(pb);
187 421
            break;
188 422
        case 0x4804:
189
            track_number = get_be32(pb);
423
            track->track_number = get_be32(pb);
424
            dprintf("track number 0x%04X\n", track->track_number);
190 425
            break;
191 426
        case 0x4B01:
192
            time_base.den = get_be32(pb);
193
            time_base.num = get_be32(pb);
427
            track->edit_rate.den = get_be32(pb);
428
            track->edit_rate.num = get_be32(pb);
194 429
            break;
195 430
        case 0x4803:
196
            get_buffer(pb, sequence_uid, 16);
197
            break;
198
        case 0x3C0A:
199
            get_buffer(pb, track_uid, 16);
431
            get_buffer(pb, track->sequence_ref, 16);
200 432
            break;
201 433
        default:
202 434
            url_fskip(pb, size);
203 435
        }
204 436
        bytes_read += size + 4;
205 437
    }
206
    for (i = 0; i < mxf->tracks_count; i++)
207
        if (!memcmp(track_uid, mxf->tracks[i].track_uid, 16)) {
208
            mxf->tracks[i].track_id = track_id;
209
            mxf->tracks[i].track_number = track_number;
210
            mxf->tracks[i].stream->time_base = time_base;
211
            mxf->tracks[i].stream->id = track_number;
212
            memcpy(mxf->tracks[i].sequence_uid, sequence_uid, 16);
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
            }
213 446
        }
214
    return bytes_read;
447
    }
448
    return -1;
215 449
}
216 450

  
217 451
static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
218 452
{
219 453
    ByteIOContext *pb = &mxf->fc->pb;
220
    uint8_t sequence_uid[16];
221
    uint8_t data_definition[16];
222
    uint64_t duration = AV_NOPTS_VALUE;
454
    MXFSequence *sequence = av_mallocz(sizeof(*sequence));
223 455
    int bytes_read = 0;
224
    int i;
456
    int i, j;
225 457

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

  
462
        dprintf("tag 0x%04X, size %d\n", tag, size);
230 463
        switch (tag) {
231 464
        case 0x3C0A:
232
            get_buffer(pb, sequence_uid, 16);
465
            get_buffer(pb, sequence->uid, 16);
233 466
            break;
234 467
        case 0x0202:
235
            duration = get_be64(pb);
468
            sequence->duration = get_be64(pb);
236 469
            break;
237 470
        case 0x0201:
238
            get_buffer(pb, data_definition, 16);
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));
239 482
            break;
240 483
        default:
241 484
            url_fskip(pb, size);
242 485
        }
243 486
        bytes_read += size + 4;
244 487
    }
245

  
246
    for (i = 0; i < mxf->tracks_count; i++)
247
        if (!memcmp(sequence_uid, mxf->tracks[i].sequence_uid, 16)) {
248
            mxf->tracks[i].stream->start_time = 0;
249
            mxf->tracks[i].stream->duration = duration;
250
            if (data_definition[11] == 0x02 && data_definition[12] == 0x01)
251
                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_VIDEO;
252
            else if (data_definition[11] == 0x02 && data_definition[12] == 0x02)
253
                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_AUDIO;
254
            else if (data_definition[11] == 0x01) /* SMPTE 12M Time Code track */
255
                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_DATA;
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
            }
256 498
        }
257
    return bytes_read;
499
    }
500
    return -1;
258 501
}
259 502

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

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

  
515
        dprintf("tag 0x%04X, size %d\n", tag, size);
271 516
        switch (tag) {
517
        case 0x3C0A:
518
            get_buffer(pb, package->uid, 16);
519
            break;
272 520
        case 0x4403:
273
            tracks_count = get_be32(pb);
274
            if(tracks_count >= UINT_MAX / sizeof(*mxf->tracks) ||
275
               tracks_count >= UINT_MAX / sizeof(*mxf->descriptors))
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))
276 524
                return -1;
277
            mxf->tracks_count += tracks_count; /* op2a contains multiple source packages */
278
            mxf->tracks = av_realloc(mxf->tracks, mxf->tracks_count * sizeof(*mxf->tracks));
279
            mxf->descriptors = av_realloc(mxf->descriptors, mxf->tracks_count * sizeof(*mxf->descriptors));
525
            package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
526
            package->tracks = av_mallocz(package->tracks_count * sizeof(*package->tracks));
280 527
            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
281
            for (i = mxf->tracks_count - tracks_count; i < mxf->tracks_count; i++) {
282
                mxf->tracks[i].stream = av_new_stream(mxf->fc, 0);
283
                get_buffer(pb, mxf->tracks[i].track_uid, 16);
284
            }
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);
285 537
            break;
286 538
        default:
287 539
            url_fskip(pb, size);
288 540
        }
289 541
        bytes_read += size + 4;
290 542
    }
291
    return bytes_read;
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;
292 550
}
293 551

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

  
300
    if (mxf->descriptors_count == mxf->tracks_count)
301
        return -1;
302
    desc = &mxf->descriptors[mxf->descriptors_count++];
303
    desc->kind = klv->key[14];
304
    desc->linked_track_id = -1;
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;
305 600

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

  
605
        dprintf("tag 0x%04X, size %d\n", tag, size);
310 606
        switch (tag) {
607
        case 0x3C0A:
608
            get_buffer(pb, desc->uid, 16);
609
            break;
311 610
        case 0x3004:
312 611
            get_buffer(pb, desc->essence_container, 16);
313 612
            break;
......
350 649
        }
351 650
        bytes_read += size + 4;
352 651
    }
353
    return bytes_read;
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;
354 668
}
355 669

  
356 670
/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
357
static const CodecTag mxf_sound_essence_labels[] = {
358
    { CODEC_ID_PCM_S16LE, 0x01000000 },/* Uncompressed Sound Coding */
359
    { CODEC_ID_PCM_S16LE, 0x017F0000 },/* Uncompressed Sound Coding */
360
    { CODEC_ID_PCM_S16BE, 0x017E0000 },/* Uncompressed Sound Coding Big Endian*/
361
    { CODEC_ID_PCM_ALAW,  0x02030101 },
362
    { CODEC_ID_AC3,       0x02030201 },
363
  //{ CODEC_ID_MP1,       0x02030104 },
364
    { CODEC_ID_MP2,       0x02030105 },/* MP2 or MP3 */
365
  //{ CODEC_ID_MP2,       0x02030106 },/* MPEG-2 Layer 1 */
366
  //{ CODEC_ID_???,       0x0203010C },/* Dolby E */
367
  //{ CODEC_ID_???,       0x02030301 },/* MPEG-2 AAC */
368
    { 0, 0 },
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 */
369 687
};
370 688

  
371
static const CodecTag mxf_picture_essence_labels[] = {
372
    { CODEC_ID_RAWVIDEO,   0x0100 },
373
    { CODEC_ID_MPEG2VIDEO, 0x0201 },
374
    { CODEC_ID_DVVIDEO,    0x0202 },
375
  //{ CODEC_ID_???,        0x0207 },/* D-11 HDCAM */
376
    { 0, 0 },
377
};
378

  
379
static const CodecTag mxf_container_picture_labels[] = {
380
    { CODEC_ID_MPEG2VIDEO, 0x0201 }, /* D-10 Mapping */
381
    { CODEC_ID_DVVIDEO,    0x0202 }, /* DV Mapping */
382
  //{ CODEC_ID_???,        0x0203 }, /* HDCAM D-11 Mapping */
383
    { CODEC_ID_MPEG2VIDEO, 0x0204 }, /* MPEG ES Mapping */
384
};
385

  
386
static const CodecTag mxf_container_sound_labels[] = {
387
  //{ CODEC_ID_PCM_S16??,  0x0201 }, /* D-10 Mapping */
388
    { CODEC_ID_MP2,        0x0204 }, /* MPEG ES Mapping */
389
    { CODEC_ID_PCM_S16LE,  0x0206 }, /* AES BWF Mapping */
390
    { CODEC_ID_PCM_ALAW,   0x020A },
391
    { 0, 0 },
392
};
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
}
393 698

  
394
static void mxf_resolve_track_descriptor(MXFContext *mxf)
699
static int mxf_parse_structural_metadata(MXFContext *mxf)
395 700
{
396
    uint32_t container_label;
397
    uint32_t essence_label;
398
    int i, j;
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
    }
399 716

  
400
    for (i = 0; i < mxf->descriptors_count; i++) {
401
        for (j = 0; j < mxf->tracks_count; j++) {
402
            AVStream *st = mxf->tracks[j].stream;
403
            MXFDescriptor *desc = &mxf->descriptors[i];
404

  
405
            if ((desc->linked_track_id == -1 && st->codec->codec_type == desc->codec_type)
406
                || desc->linked_track_id == mxf->tracks[j].track_id) {
407
                if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
408
                    st->codec->channels = desc->channels;
409
                    st->codec->bits_per_sample = desc->bits_per_sample;
410
                    st->codec->block_align = desc->block_align;
411
                    st->codec->sample_rate = desc->sample_rate.num / desc->sample_rate.den;
412

  
413
                    container_label = BE_16(desc->essence_container + 12);
414
                    essence_label = BE_32(desc->essence_compression + 11);
415
                    st->codec->codec_id = codec_get_id(mxf_sound_essence_labels, essence_label);
416
                    if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
417
                        if (desc->bits_per_sample == 24)
418
                            st->codec->codec_id = CODEC_ID_PCM_S24LE;
419
                        else if (desc->bits_per_sample == 32)
420
                            st->codec->codec_id = CODEC_ID_PCM_S32LE;
421
                    }
422
                    if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
423
                        if (desc->bits_per_sample == 24)
424
                            st->codec->codec_id = CODEC_ID_PCM_S24BE;
425
                        else if (desc->bits_per_sample == 32)
426
                            st->codec->codec_id = CODEC_ID_PCM_S32BE;
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
                        }
427 779
                    }
428
                    if (!st->codec->codec_id)
429
                        st->codec->codec_id = codec_get_id(mxf_container_sound_labels, container_label);
430

  
431
                } else if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
432
                    st->codec->width = desc->width;
433
                    st->codec->height = desc->height;
434

  
435
                    container_label = BE_16(desc->essence_container + 12);
436
                    essence_label = BE_16(desc->essence_compression + 11);
437
                    st->codec->codec_id = codec_get_id(mxf_picture_essence_labels, essence_label);
438
                    if (!st->codec->codec_id)
439
                        st->codec->codec_id = codec_get_id(mxf_container_picture_labels, container_label);
440 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;
441 811
            }
442 812
        }
443 813
    }
814
    return 0;
444 815
}
445 816

  
446 817
static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
......
455 826
            return -1;
456 827
        if (IS_KLV_KEY(klv.key, mxf_metadata_track_key))
457 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);
458 831
        else if (IS_KLV_KEY(klv.key, mxf_metadata_source_package_key))
459 832
            ret = mxf_read_metadata_source_package(mxf, &klv);
460 833
        else if (IS_KLV_KEY(klv.key, mxf_metadata_sequence_key))
461 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);
462 841
        else if (IS_KLV_KEY(klv.key, mxf_metadata_wave_descriptor_key))
463
            ret = mxf_read_metadata_descriptor(mxf, &klv);
842
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
464 843
        else if (IS_KLV_KEY(klv.key, mxf_metadata_mpegvideo_descriptor_key))
465
            ret = mxf_read_metadata_descriptor(mxf, &klv);
844
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
466 845
        else if (IS_KLV_KEY(klv.key, mxf_metadata_cdci_descriptor_key))
467
            ret = mxf_read_metadata_descriptor(mxf, &klv);
846
            ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
468 847
        else if (IS_KLV_KEY(klv.key, mxf_metadata_generic_sound_descriptor_key))
469
            ret = mxf_read_metadata_descriptor(mxf, &klv);
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);
470 853
        else if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
471 854
            /* FIXME avoid seek */
472 855
            url_fseek(&s->pb, klv.offset, SEEK_SET);
......
476 859
        if (ret < 0)
477 860
            return ret;
478 861
    }
479
    mxf_resolve_track_descriptor(mxf);
480
    return 0;
862
    return mxf_parse_structural_metadata(mxf);
481 863
}
482 864

  
483 865
static int mxf_read_close(AVFormatContext *s)
484 866
{
485 867
    MXFContext *mxf = s->priv_data;
486

  
487
    av_freep(&mxf->tracks);
488
    av_freep(&mxf->descriptors);
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);
489 897
    return 0;
490 898
}
491 899

  

Also available in: Unified diff