add information about codec uls
[libav.git] / libavformat / mxf.c
CommitLineData
07bf2af8
BC
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.
743d772c 33 * Material Package contains tracks with reference to SourcePackage tracks.
07bf2af8
BC
34 * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
35 * Assign Descriptors to correct Tracks.
36 *
743d772c
BC
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
07bf2af8
BC
43 */
44
45//#define DEBUG
46
47#include "avformat.h"
07bf2af8 48
f03b6426 49typedef uint8_t UID[16];
4bfff768 50
743d772c
BC
51enum MXFPackageType {
52 MaterialPackage,
53 SourcePackage,
54};
55
56enum MXFStructuralComponentType {
57 Timecode,
58 SourceClip,
59};
60
61typedef 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
71typedef 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
80typedef struct MXFTrack {
81 UID uid;
82 MXFSequence *sequence; /* mandatory, and only one */
83 UID sequence_ref;
07bf2af8 84 int track_id;
f03b6426 85 uint8_t track_number[4];
743d772c 86 AVRational edit_rate;
07bf2af8
BC
87} MXFTrack;
88
743d772c
BC
89typedef struct MXFDescriptor {
90 UID uid;
e076173f
BC
91 UID essence_container_ul;
92 UID essence_codec_ul;
07bf2af8
BC
93 AVRational sample_rate;
94 AVRational aspect_ratio;
95 int width;
96 int height;
97 int channels;
98 int bits_per_sample;
743d772c
BC
99 struct MXFDescriptor **sub_descriptors;
100 UID *sub_descriptors_refs;
101 int sub_descriptors_count;
07bf2af8 102 int linked_track_id;
07bf2af8
BC
103} MXFDescriptor;
104
743d772c
BC
105typedef struct MXFPackage {
106 UID uid;
107 UID package_uid;
108 MXFTrack **tracks;
109 UID *tracks_refs;
07bf2af8 110 int tracks_count;
743d772c
BC
111 MXFDescriptor *descriptor; /* only one */
112 UID descriptor_ref;
113 enum MXFPackageType type;
114} MXFPackage;
115
116typedef struct MXFEssenceContainerData {
117 UID uid;
118 UID linked_package_uid;
119} MXFEssenceContainerData;
120
121typedef 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;
07bf2af8
BC
133} MXFContext;
134
743d772c 135typedef struct KLVPacket {
4bfff768 136 UID key;
07bf2af8
BC
137 offset_t offset;
138 uint64_t length;
139} KLVPacket;
140
743d772c
BC
141typedef struct MXFCodecUL {
142 UID uid;
143 enum CodecID id;
144} MXFCodecUL;
145
dd15f9bf
BC
146static const UID mxf_metadata_preface_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2F,0x00 };
147static const UID mxf_metadata_content_storage_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 };
148static const UID mxf_metadata_source_package_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 };
149static const UID mxf_metadata_material_package_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 };
150static const UID mxf_metadata_sequence_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 };
151static const UID mxf_metadata_source_clip_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 };
152static const UID mxf_metadata_multiple_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 };
153static const UID mxf_metadata_generic_sound_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
154static const UID mxf_metadata_cdci_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
8ce2215b 155static const UID mxf_metadata_rgba_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 };
dd15f9bf
BC
156static const UID mxf_metadata_mpegvideo_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
157static const UID mxf_metadata_wave_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
158static const UID mxf_metadata_static_track_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 };
159static const UID mxf_metadata_track_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3b,0x00 };
4bfff768
BC
160
161/* partial keys to match */
dd15f9bf 162static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
f03b6426 163static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
07bf2af8
BC
164
165#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
166
eec3fec0
BC
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])
07bf2af8
BC
169
170static 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
189static 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);
b7522224 194 return klv->length == -1 ? -1 : 0;
07bf2af8
BC
195}
196
197static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
198{
07bf2af8
BC
199 int i;
200
201 for (i = 0; i < s->nb_streams; i++) {
f03b6426
BC
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)))
07bf2af8
BC
205 return i;
206 }
207 return -1;
208}
209
210static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
211{
212 KLVPacket klv;
213
214 while (!url_feof(&s->pb)) {
5e441e31
BC
215 if (klv_read_packet(&klv, &s->pb) < 0) {
216 av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
07bf2af8 217 return -1;
5e441e31 218 }
6d0c3beb
BC
219#ifdef DEBUG
220 PRINT_KEY(klv.key);
221#endif
07bf2af8
BC
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);
b7522224 225 return pkt->stream_index == -1 ? -1 : 0;
07bf2af8
BC
226 } else
227 url_fskip(&s->pb, klv.length);
228 }
229 return AVERROR_IO;
230}
231
743d772c 232static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
07bf2af8
BC
233{
234 ByteIOContext *pb = &mxf->fc->pb;
07bf2af8 235 int bytes_read = 0;
07bf2af8
BC
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) {
743d772c
BC
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
264static 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
303static 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
356static 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
396static 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;
07bf2af8 412 case 0x4801:
743d772c 413 track->track_id = get_be32(pb);
07bf2af8
BC
414 break;
415 case 0x4804:
f03b6426 416 get_buffer(pb, track->track_number, 4);
07bf2af8
BC
417 break;
418 case 0x4B01:
743d772c
BC
419 track->edit_rate.den = get_be32(pb);
420 track->edit_rate.num = get_be32(pb);
07bf2af8
BC
421 break;
422 case 0x4803:
743d772c 423 get_buffer(pb, track->sequence_ref, 16);
07bf2af8
BC
424 break;
425 default:
426 url_fskip(pb, size);
427 }
428 bytes_read += size + 4;
429 }
743d772c
BC
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 }
07bf2af8 438 }
743d772c
BC
439 }
440 return -1;
07bf2af8
BC
441}
442
443static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
444{
445 ByteIOContext *pb = &mxf->fc->pb;
743d772c 446 MXFSequence *sequence = av_mallocz(sizeof(*sequence));
07bf2af8 447 int bytes_read = 0;
743d772c 448 int i, j;
07bf2af8
BC
449
450 while (bytes_read < klv->length) {
451 int tag = get_be16(pb);
452 int size = get_be16(pb); /* KLV specified by 0x53 */
453
743d772c 454 dprintf("tag 0x%04X, size %d\n", tag, size);
07bf2af8
BC
455 switch (tag) {
456 case 0x3C0A:
743d772c 457 get_buffer(pb, sequence->uid, 16);
07bf2af8
BC
458 break;
459 case 0x0202:
743d772c 460 sequence->duration = get_be64(pb);
07bf2af8
BC
461 break;
462 case 0x0201:
743d772c
BC
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));
07bf2af8
BC
474 break;
475 default:
476 url_fskip(pb, size);
477 }
478 bytes_read += size + 4;
479 }
743d772c
BC
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 }
07bf2af8 490 }
743d772c
BC
491 }
492 return -1;
07bf2af8
BC
493}
494
495static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
496{
497 ByteIOContext *pb = &mxf->fc->pb;
743d772c 498 MXFPackage *package = av_mallocz(sizeof(*package));
07bf2af8
BC
499 int bytes_read = 0;
500 int i;
501
743d772c 502 package->type = SourcePackage;
07bf2af8
BC
503 while (bytes_read < klv->length) {
504 int tag = get_be16(pb);
505 int size = get_be16(pb); /* KLV specified by 0x53 */
506
743d772c 507 dprintf("tag 0x%04X, size %d\n", tag, size);
07bf2af8 508 switch (tag) {
743d772c
BC
509 case 0x3C0A:
510 get_buffer(pb, package->uid, 16);
511 break;
07bf2af8 512 case 0x4403:
743d772c
BC
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))
07bf2af8 516 return -1;
743d772c
BC
517 package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
518 package->tracks = av_mallocz(package->tracks_count * sizeof(*package->tracks));
07bf2af8 519 url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
743d772c
BC
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);
07bf2af8
BC
529 break;
530 default:
531 url_fskip(pb, size);
532 }
533 bytes_read += size + 4;
534 }
743d772c
BC
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;
07bf2af8
BC
542}
543
743d772c 544static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv)
07bf2af8
BC
545{
546 ByteIOContext *pb = &mxf->fc->pb;
743d772c 547 MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
07bf2af8 548 int bytes_read = 0;
743d772c 549 int i;
07bf2af8 550
743d772c
BC
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
59b56738
BC
586static void mxf_read_metadata_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
587{
588 int code;
589
590 do {
591 code = get_byte(pb);
592 dprintf("pixel layout: code 0x%x\n", code);
593 switch (code) {
594 case 0x52: /* R */
595 descriptor->bits_per_sample += get_byte(pb);
596 break;
597 case 0x47: /* G */
598 descriptor->bits_per_sample += get_byte(pb);
599 break;
600 case 0x42: /* B */
601 descriptor->bits_per_sample += get_byte(pb);
602 break;
603 default:
604 get_byte(pb);
605 }
606 } while (code != 0); /* SMPTE 377M E.2.46 */
607}
608
743d772c
BC
609static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
610{
611 ByteIOContext *pb = &mxf->fc->pb;
e076173f 612 MXFDescriptor *descriptor = av_mallocz(sizeof(*descriptor));
743d772c
BC
613 int bytes_read = 0;
614 int i, j;
07bf2af8
BC
615
616 while (bytes_read < klv->length) {
617 int tag = get_be16(pb);
618 int size = get_be16(pb); /* KLV specified by 0x53 */
619
743d772c 620 dprintf("tag 0x%04X, size %d\n", tag, size);
07bf2af8 621 switch (tag) {
743d772c 622 case 0x3C0A:
e076173f 623 get_buffer(pb, descriptor->uid, 16);
743d772c 624 break;
07bf2af8 625 case 0x3004:
e076173f 626 get_buffer(pb, descriptor->essence_container_ul, 16);
07bf2af8
BC
627 break;
628 case 0x3006:
e076173f 629 descriptor->linked_track_id = get_be32(pb);
07bf2af8
BC
630 break;
631 case 0x3201: /* PictureEssenceCoding */
e076173f 632 get_buffer(pb, descriptor->essence_codec_ul, 16);
07bf2af8
BC
633 break;
634 case 0x3203:
e076173f 635 descriptor->width = get_be32(pb);
07bf2af8
BC
636 break;
637 case 0x3202:
e076173f 638 descriptor->height = get_be32(pb);
07bf2af8
BC
639 break;
640 case 0x320E:
e076173f
BC
641 descriptor->aspect_ratio.num = get_be32(pb);
642 descriptor->aspect_ratio.den = get_be32(pb);
07bf2af8 643 break;
07bf2af8 644 case 0x3D03:
e076173f
BC
645 descriptor->sample_rate.num = get_be32(pb);
646 descriptor->sample_rate.den = get_be32(pb);
07bf2af8
BC
647 break;
648 case 0x3D06: /* SoundEssenceCompression */
e076173f 649 get_buffer(pb, descriptor->essence_codec_ul, 16);
07bf2af8
BC
650 break;
651 case 0x3D07:
e076173f 652 descriptor->channels = get_be32(pb);
07bf2af8
BC
653 break;
654 case 0x3D01:
e076173f 655 descriptor->bits_per_sample = get_be32(pb);
07bf2af8 656 break;
59b56738
BC
657 case 0x3401:
658 mxf_read_metadata_pixel_layout(pb, descriptor);
659 break;
07bf2af8
BC
660 default:
661 url_fskip(pb, size);
662 }
663 bytes_read += size + 4;
664 }
743d772c
BC
665 for (i = 0; i < mxf->packages_count; i++) {
666 if (mxf->packages[i]) {
e076173f
BC
667 if (!memcmp(mxf->packages[i]->descriptor_ref, descriptor->uid, 16)) {
668 mxf->packages[i]->descriptor = descriptor;
743d772c
BC
669 return 0;
670 } else if (mxf->packages[i]->descriptor) { /* MultipleDescriptor */
671 for (j = 0; j < mxf->packages[i]->descriptor->sub_descriptors_count; j++) {
e076173f
BC
672 if (!memcmp(mxf->packages[i]->descriptor->sub_descriptors_refs[j], descriptor->uid, 16)) {
673 mxf->packages[i]->descriptor->sub_descriptors[j] = descriptor;
743d772c
BC
674 return 0;
675 }
676 }
677 }
678 }
679 }
680 return -1;
07bf2af8
BC
681}
682
683/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
743d772c
BC
684static const UID picture_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 };
685static const UID sound_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 };
686
687static const MXFCodecUL mxf_codec_uls[] = {
688 /* PictureEssenceCoding */
ea3a790e
BC
689 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, CODEC_ID_MPEG2VIDEO }, /* 422P@ML I-Frame */
690 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* 422P@HL Long GoP */
691 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, CODEC_ID_MPEG2VIDEO }, /* MP@ML Long GoP */
692 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* 422P@ML Long GoP */
693 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, CODEC_ID_MPEG2VIDEO }, /* MP@HL Long GoP */
743d772c
BC
694 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, CODEC_ID_MPEG2VIDEO }, /* D-10 30Mbps PAL */
695 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, CODEC_ID_MPEG2VIDEO }, /* D-10 50Mbps PAL */
696 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 }, CODEC_ID_DVVIDEO }, /* DVCPRO50 PAL */
697 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 }, CODEC_ID_DVVIDEO }, /* DVCPRO25 PAL */
698 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 }, CODEC_ID_DVVIDEO }, /* DV25 IEC PAL */
54a3360b 699 //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, CODEC_ID_JPEG2K }, /* JPEG2000 Codestream */
59b56738 700 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, CODEC_ID_RAWVIDEO }, /* Uncompressed */
743d772c
BC
701 /* SoundEssenceCompression */
702 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 }, CODEC_ID_PCM_S16LE },
703 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x02,0x02,0x01,0x7E,0x00,0x00,0x00 }, CODEC_ID_PCM_S16BE }, /* From Omneon MXF file */
54a3360b
BC
704 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 }, CODEC_ID_PCM_ALAW },
705 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 }, CODEC_ID_AC3 },
706 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 }, CODEC_ID_MP2 }, /* MP2 or MP3 */
707 //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 }, CODEC_ID_DOLBY_E }, /* Dolby-E */
07bf2af8
BC
708};
709
743d772c
BC
710static enum CodecID mxf_get_codec_id(const MXFCodecUL *uls, UID *uid)
711{
712 while (uls->id != CODEC_ID_NONE) {
713 if(!memcmp(uls->uid, *uid, 16))
714 return uls->id;
715 uls++;
716 }
717 return CODEC_ID_NONE;
718}
07bf2af8 719
743d772c 720static int mxf_parse_structural_metadata(MXFContext *mxf)
07bf2af8 721{
743d772c
BC
722 MXFPackage *material_package = NULL;
723 MXFPackage *source_package = NULL;
724 int i, j, k;
725
726 /* TODO: handle multiple material packages (OP3x) */
727 for (i = 0; i < mxf->packages_count; i++) {
728 if (mxf->packages[i]->type == MaterialPackage) {
729 material_package = mxf->packages[i];
730 break;
731 }
732 }
733 if (!material_package) {
734 av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
735 return -1;
736 }
07bf2af8 737
743d772c
BC
738 for (i = 0; i < material_package->tracks_count; i++) {
739 MXFTrack *material_track = material_package->tracks[i];
740 MXFTrack *source_track = NULL;
741 MXFDescriptor *descriptor = NULL;
742 MXFStructuralComponent *component = NULL;
743 AVStream *st;
744
745 /* TODO: handle multiple source clips */
746 for (j = 0; j < material_track->sequence->structural_components_count; j++) {
747 component = material_track->sequence->structural_components[j];
748 /* TODO: handle timecode component */
749 if (!component || component->type != SourceClip)
750 continue;
751
752 for (k = 0; k < mxf->packages_count; k++) {
753 if (!memcmp(mxf->packages[k]->package_uid, component->source_package_uid, 16)) {
754 source_package = mxf->packages[k];
755 break;
756 }
757 }
758 if (!source_package) {
759 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
760 break;
761 }
762 for (k = 0; k < source_package->tracks_count; k++) {
763 if (source_package->tracks[k]->track_id == component->source_track_id) {
764 source_track = source_package->tracks[k];
765 break;
766 }
767 }
768 if (!source_track) {
769 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
770 break;
771 }
772 }
773 if (!source_track)
774 continue;
775
f03b6426
BC
776 st = av_new_stream(mxf->fc, source_track->track_id);
777 st->priv_data = source_track;
743d772c
BC
778 st->duration = component->duration;
779 if (st->duration == -1)
780 st->duration = AV_NOPTS_VALUE;
781 st->start_time = component->start_position;
782 av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
783#ifdef DEBUG
784 PRINT_KEY(source_track->sequence->data_definition_ul);
785#endif
786 if (!memcmp(source_track->sequence->data_definition_ul, picture_essence_track_ul, 16))
787 st->codec->codec_type = CODEC_TYPE_VIDEO;
788 else if (!memcmp(source_track->sequence->data_definition_ul, sound_essence_track_ul, 16))
789 st->codec->codec_type = CODEC_TYPE_AUDIO;
790 else
791 st->codec->codec_type = CODEC_TYPE_DATA;
792
793 if (source_package->descriptor) {
794 if (source_package->descriptor->sub_descriptors_count > 0) { /* SourcePackage has MultipleDescriptor */
795 for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
796 if (source_package->descriptor->sub_descriptors[j]) {
797 if (source_package->descriptor->sub_descriptors[j]->linked_track_id == source_track->track_id) {
798 descriptor = source_package->descriptor->sub_descriptors[j];
799 break;
800 }
07bf2af8 801 }
07bf2af8 802 }
743d772c
BC
803 } else {
804 descriptor = source_package->descriptor;
805 }
806 }
807 if (!descriptor) {
808 av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
809 continue;
810 }
811#ifdef DEBUG
e076173f 812 PRINT_KEY(descriptor->essence_codec_ul);
743d772c 813#endif
e076173f 814 st->codec->codec_id = mxf_get_codec_id(mxf_codec_uls, &descriptor->essence_codec_ul);
743d772c
BC
815 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
816 st->codec->width = descriptor->width;
817 st->codec->height = descriptor->height;
59b56738 818 st->codec->bits_per_sample = descriptor->bits_per_sample; /* Uncompressed */
743d772c
BC
819 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
820 st->codec->channels = descriptor->channels;
821 st->codec->bits_per_sample = descriptor->bits_per_sample;
822 st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
823 /* TODO: implement CODEC_ID_RAWAUDIO */
824 if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
825 if (descriptor->bits_per_sample == 24)
826 st->codec->codec_id = CODEC_ID_PCM_S24LE;
827 else if (descriptor->bits_per_sample == 32)
828 st->codec->codec_id = CODEC_ID_PCM_S32LE;
829 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
830 if (descriptor->bits_per_sample == 24)
831 st->codec->codec_id = CODEC_ID_PCM_S24BE;
832 else if (descriptor->bits_per_sample == 32)
833 st->codec->codec_id = CODEC_ID_PCM_S32BE;
07bf2af8
BC
834 }
835 }
836 }
743d772c 837 return 0;
07bf2af8
BC
838}
839
840static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
841{
842 MXFContext *mxf = s->priv_data;
843 KLVPacket klv;
844 int ret = 0;
845
846 mxf->fc = s;
847 while (!url_feof(&s->pb)) {
5e441e31
BC
848 if (klv_read_packet(&klv, &s->pb) < 0) {
849 av_log(s, AV_LOG_ERROR, "error reading KLV packet\n");
07bf2af8 850 return -1;
5e441e31 851 }
6d0c3beb
BC
852#ifdef DEBUG
853 PRINT_KEY(klv.key);
854#endif
07bf2af8
BC
855 if (IS_KLV_KEY(klv.key, mxf_metadata_track_key))
856 ret = mxf_read_metadata_track(mxf, &klv);
743d772c
BC
857 else if (IS_KLV_KEY(klv.key, mxf_metadata_static_track_key))
858 ret = mxf_read_metadata_track(mxf, &klv);
07bf2af8
BC
859 else if (IS_KLV_KEY(klv.key, mxf_metadata_source_package_key))
860 ret = mxf_read_metadata_source_package(mxf, &klv);
861 else if (IS_KLV_KEY(klv.key, mxf_metadata_sequence_key))
862 ret = mxf_read_metadata_sequence(mxf, &klv);
743d772c
BC
863 else if (IS_KLV_KEY(klv.key, mxf_metadata_material_package_key))
864 ret = mxf_read_metadata_material_package(mxf, &klv);
865 else if (IS_KLV_KEY(klv.key, mxf_metadata_source_clip_key))
866 ret = mxf_read_metadata_source_clip(mxf, &klv);
867 else if (IS_KLV_KEY(klv.key, mxf_metadata_multiple_descriptor_key))
868 ret = mxf_read_metadata_multiple_descriptor(mxf, &klv);
07bf2af8 869 else if (IS_KLV_KEY(klv.key, mxf_metadata_wave_descriptor_key))
743d772c 870 ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
07bf2af8 871 else if (IS_KLV_KEY(klv.key, mxf_metadata_mpegvideo_descriptor_key))
743d772c 872 ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
07bf2af8 873 else if (IS_KLV_KEY(klv.key, mxf_metadata_cdci_descriptor_key))
743d772c 874 ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
8ce2215b
BC
875 else if (IS_KLV_KEY(klv.key, mxf_metadata_rgba_descriptor_key))
876 ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
07bf2af8 877 else if (IS_KLV_KEY(klv.key, mxf_metadata_generic_sound_descriptor_key))
743d772c
BC
878 ret = mxf_read_metadata_generic_descriptor(mxf, &klv);
879 else if (IS_KLV_KEY(klv.key, mxf_metadata_preface_key))
880 ret = mxf_read_metadata_preface(mxf, &klv);
881 else if (IS_KLV_KEY(klv.key, mxf_metadata_content_storage_key))
882 ret = mxf_read_metadata_content_storage(mxf, &klv);
07bf2af8
BC
883 else if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
884 /* FIXME avoid seek */
885 url_fseek(&s->pb, klv.offset, SEEK_SET);
886 break;
887 } else
888 url_fskip(&s->pb, klv.length);
5e441e31
BC
889 if (ret < 0) {
890 av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
07bf2af8 891 return ret;
5e441e31 892 }
07bf2af8 893 }
743d772c 894 return mxf_parse_structural_metadata(mxf);
07bf2af8
BC
895}
896
897static int mxf_read_close(AVFormatContext *s)
898{
899 MXFContext *mxf = s->priv_data;
743d772c
BC
900 int i, j, k;
901
902 for (i = 0; i < mxf->packages_count; i++) {
903 for (j = 0; j < mxf->packages[i]->tracks_count; j++) {
904 for (k = 0; k < mxf->packages[i]->tracks[j]->sequence->structural_components_count; k++)
905 av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components[k]);
906 av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components_refs);
907 av_freep(&mxf->packages[i]->tracks[j]->sequence->structural_components);
908 av_freep(&mxf->packages[i]->tracks[j]->sequence);
909 av_freep(&mxf->packages[i]->tracks[j]);
910 }
911 av_freep(&mxf->packages[i]->tracks_refs);
912 av_freep(&mxf->packages[i]->tracks);
913 if (mxf->packages[i]->descriptor) {
914 for (k = 0; k < mxf->packages[i]->descriptor->sub_descriptors_count; k++)
915 av_freep(&mxf->packages[i]->descriptor->sub_descriptors[k]);
916 av_freep(&mxf->packages[i]->descriptor->sub_descriptors_refs);
917 av_freep(&mxf->packages[i]->descriptor->sub_descriptors);
918 }
919 av_freep(&mxf->packages[i]->descriptor);
920 av_freep(&mxf->packages[i]);
921 }
922 av_freep(&mxf->packages_refs);
923 av_freep(&mxf->packages);
924 for (i = 0; i < mxf->essence_container_data_sets_count; i++)
925 av_freep(&mxf->essence_container_data_sets[i]);
926 av_freep(&mxf->essence_container_data_sets_refs);
927 av_freep(&mxf->essence_container_data_sets);
928 av_freep(&mxf->essence_containers_uls);
07bf2af8
BC
929 return 0;
930}
931
932static int mxf_probe(AVProbeData *p) {
933 /* KLV packet describing MXF header partition pack */
934 if (p->buf_size < sizeof(mxf_header_partition_pack_key))
935 return 0;
936
937 if (IS_KLV_KEY(p->buf, mxf_header_partition_pack_key))
938 return AVPROBE_SCORE_MAX;
939 else
940 return 0;
941}
942
943
944AVInputFormat mxf_demuxer = {
945 "mxf",
946 "MXF format",
947 sizeof(MXFContext),
948 mxf_probe,
949 mxf_read_header,
950 mxf_read_packet,
951 mxf_read_close,
952 NULL,
953};