00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "libavutil/aes.h"
00049 #include "libavcodec/bytestream.h"
00050 #include "avformat.h"
00051 #include "mxf.h"
00052
00053 typedef struct {
00054 UID uid;
00055 enum MXFMetadataSetType type;
00056 UID source_container_ul;
00057 } MXFCryptoContext;
00058
00059 typedef struct {
00060 UID uid;
00061 enum MXFMetadataSetType type;
00062 UID source_package_uid;
00063 UID data_definition_ul;
00064 int64_t duration;
00065 int64_t start_position;
00066 int source_track_id;
00067 } MXFStructuralComponent;
00068
00069 typedef struct {
00070 UID uid;
00071 enum MXFMetadataSetType type;
00072 UID data_definition_ul;
00073 UID *structural_components_refs;
00074 int structural_components_count;
00075 int64_t duration;
00076 } MXFSequence;
00077
00078 typedef struct {
00079 UID uid;
00080 enum MXFMetadataSetType type;
00081 MXFSequence *sequence;
00082 UID sequence_ref;
00083 int track_id;
00084 uint8_t track_number[4];
00085 AVRational edit_rate;
00086 } MXFTrack;
00087
00088 typedef struct {
00089 UID uid;
00090 enum MXFMetadataSetType type;
00091 UID essence_container_ul;
00092 UID essence_codec_ul;
00093 AVRational sample_rate;
00094 AVRational aspect_ratio;
00095 int width;
00096 int height;
00097 int channels;
00098 int bits_per_sample;
00099 UID *sub_descriptors_refs;
00100 int sub_descriptors_count;
00101 int linked_track_id;
00102 uint8_t *extradata;
00103 int extradata_size;
00104 } MXFDescriptor;
00105
00106 typedef struct {
00107 UID uid;
00108 enum MXFMetadataSetType type;
00109 } MXFIndexTableSegment;
00110
00111 typedef struct {
00112 UID uid;
00113 enum MXFMetadataSetType type;
00114 UID package_uid;
00115 UID *tracks_refs;
00116 int tracks_count;
00117 MXFDescriptor *descriptor;
00118 UID descriptor_ref;
00119 } MXFPackage;
00120
00121 typedef struct {
00122 UID uid;
00123 enum MXFMetadataSetType type;
00124 } MXFMetadataSet;
00125
00126 typedef struct {
00127 UID *packages_refs;
00128 int packages_count;
00129 MXFMetadataSet **metadata_sets;
00130 int metadata_sets_count;
00131 AVFormatContext *fc;
00132 struct AVAES *aesc;
00133 uint8_t *local_tags;
00134 int local_tags_count;
00135 } MXFContext;
00136
00137 enum MXFWrappingScheme {
00138 Frame,
00139 Clip,
00140 };
00141
00142 typedef struct {
00143 const UID key;
00144 int (*read)();
00145 int ctx_size;
00146 enum MXFMetadataSetType type;
00147 } MXFMetadataReadTableEntry;
00148
00149
00150 static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
00151 static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
00152 static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
00153
00154 static const uint8_t mxf_crypto_source_container_ul[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x02,0x02,0x00,0x00,0x00 };
00155 static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
00156 static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
00157 static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
00158
00159 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
00160
00161 static int64_t klv_decode_ber_length(ByteIOContext *pb)
00162 {
00163 uint64_t size = get_byte(pb);
00164 if (size & 0x80) {
00165 int bytes_num = size & 0x7f;
00166
00167 if (bytes_num > 8)
00168 return -1;
00169 size = 0;
00170 while (bytes_num--)
00171 size = size << 8 | get_byte(pb);
00172 }
00173 return size;
00174 }
00175
00176 static int mxf_read_sync(ByteIOContext *pb, const uint8_t *key, unsigned size)
00177 {
00178 int i, b;
00179 for (i = 0; i < size && !url_feof(pb); i++) {
00180 b = get_byte(pb);
00181 if (b == key[0])
00182 i = 0;
00183 else if (b != key[i])
00184 i = -1;
00185 }
00186 return i == size;
00187 }
00188
00189 static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
00190 {
00191 if (!mxf_read_sync(pb, mxf_klv_key, 4))
00192 return -1;
00193 klv->offset = url_ftell(pb) - 4;
00194 memcpy(klv->key, mxf_klv_key, 4);
00195 get_buffer(pb, klv->key + 4, 12);
00196 klv->length = klv_decode_ber_length(pb);
00197 return klv->length == -1 ? -1 : 0;
00198 }
00199
00200 static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
00201 {
00202 int i;
00203
00204 for (i = 0; i < s->nb_streams; i++) {
00205 MXFTrack *track = s->streams[i]->priv_data;
00206
00207 if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
00208 return i;
00209 }
00210
00211 return s->nb_streams == 1 ? 0 : -1;
00212 }
00213
00214
00215 static int mxf_get_d10_aes3_packet(ByteIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
00216 {
00217 const uint8_t *buf_ptr, *end_ptr;
00218 uint8_t *data_ptr;
00219 int i;
00220
00221 if (length > 61444)
00222 return -1;
00223 av_new_packet(pkt, length);
00224 get_buffer(pb, pkt->data, length);
00225 data_ptr = pkt->data;
00226 end_ptr = pkt->data + length;
00227 buf_ptr = pkt->data + 4;
00228 for (; buf_ptr < end_ptr; ) {
00229 for (i = 0; i < st->codec->channels; i++) {
00230 uint32_t sample = bytestream_get_le32(&buf_ptr);
00231 if (st->codec->bits_per_coded_sample == 24)
00232 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
00233 else
00234 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
00235 }
00236 buf_ptr += 32 - st->codec->channels*4;
00237 }
00238 pkt->size = data_ptr - pkt->data;
00239 return 0;
00240 }
00241
00242 static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
00243 {
00244 static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
00245 MXFContext *mxf = s->priv_data;
00246 ByteIOContext *pb = s->pb;
00247 int64_t end = url_ftell(pb) + klv->length;
00248 uint64_t size;
00249 uint64_t orig_size;
00250 uint64_t plaintext_size;
00251 uint8_t ivec[16];
00252 uint8_t tmpbuf[16];
00253 int index;
00254
00255 if (!mxf->aesc && s->key && s->keylen == 16) {
00256 mxf->aesc = av_malloc(av_aes_size);
00257 if (!mxf->aesc)
00258 return -1;
00259 av_aes_init(mxf->aesc, s->key, 128, 1);
00260 }
00261
00262 url_fskip(pb, klv_decode_ber_length(pb));
00263
00264 klv_decode_ber_length(pb);
00265 plaintext_size = get_be64(pb);
00266
00267 klv_decode_ber_length(pb);
00268 get_buffer(pb, klv->key, 16);
00269 if (!IS_KLV_KEY(klv, mxf_essence_element_key))
00270 return -1;
00271 index = mxf_get_stream_index(s, klv);
00272 if (index < 0)
00273 return -1;
00274
00275 klv_decode_ber_length(pb);
00276 orig_size = get_be64(pb);
00277 if (orig_size < plaintext_size)
00278 return -1;
00279
00280 size = klv_decode_ber_length(pb);
00281 if (size < 32 || size - 32 < orig_size)
00282 return -1;
00283 get_buffer(pb, ivec, 16);
00284 get_buffer(pb, tmpbuf, 16);
00285 if (mxf->aesc)
00286 av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
00287 if (memcmp(tmpbuf, checkv, 16))
00288 av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
00289 size -= 32;
00290 av_get_packet(pb, pkt, size);
00291 size -= plaintext_size;
00292 if (mxf->aesc)
00293 av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
00294 &pkt->data[plaintext_size], size >> 4, ivec, 1);
00295 pkt->size = orig_size;
00296 pkt->stream_index = index;
00297 url_fskip(pb, end - url_ftell(pb));
00298 return 0;
00299 }
00300
00301 static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
00302 {
00303 KLVPacket klv;
00304
00305 while (!url_feof(s->pb)) {
00306 if (klv_read_packet(&klv, s->pb) < 0)
00307 return -1;
00308 PRINT_KEY(s, "read packet", klv.key);
00309 dprintf(s, "size %lld offset %#llx\n", klv.length, klv.offset);
00310 if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
00311 int res = mxf_decrypt_triplet(s, pkt, &klv);
00312 if (res < 0) {
00313 av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
00314 return -1;
00315 }
00316 return 0;
00317 }
00318 if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
00319 int index = mxf_get_stream_index(s, &klv);
00320 if (index < 0) {
00321 av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12));
00322 goto skip;
00323 }
00324 if (s->streams[index]->discard == AVDISCARD_ALL)
00325 goto skip;
00326
00327 if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
00328 if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
00329 av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
00330 return -1;
00331 }
00332 } else
00333 av_get_packet(s->pb, pkt, klv.length);
00334 pkt->stream_index = index;
00335 pkt->pos = klv.offset;
00336 return 0;
00337 } else
00338 skip:
00339 url_fskip(s->pb, klv.length);
00340 }
00341 return AVERROR_EOF;
00342 }
00343
00344 static int mxf_read_primer_pack(MXFContext *mxf)
00345 {
00346 ByteIOContext *pb = mxf->fc->pb;
00347 int item_num = get_be32(pb);
00348 int item_len = get_be32(pb);
00349
00350 if (item_len != 18) {
00351 av_log(mxf->fc, AV_LOG_ERROR, "unsupported primer pack item length\n");
00352 return -1;
00353 }
00354 if (item_num > UINT_MAX / item_len)
00355 return -1;
00356 mxf->local_tags_count = item_num;
00357 mxf->local_tags = av_malloc(item_num*item_len);
00358 if (!mxf->local_tags)
00359 return -1;
00360 get_buffer(pb, mxf->local_tags, item_num*item_len);
00361 return 0;
00362 }
00363
00364 static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
00365 {
00366 if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets))
00367 return AVERROR(ENOMEM);
00368 mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
00369 if (!mxf->metadata_sets)
00370 return -1;
00371 mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
00372 mxf->metadata_sets_count++;
00373 return 0;
00374 }
00375
00376 static int mxf_read_cryptographic_context(MXFCryptoContext *cryptocontext, ByteIOContext *pb, int tag, int size, UID uid)
00377 {
00378 if (size != 16)
00379 return -1;
00380 if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
00381 get_buffer(pb, cryptocontext->source_container_ul, 16);
00382 return 0;
00383 }
00384
00385 static int mxf_read_content_storage(MXFContext *mxf, ByteIOContext *pb, int tag)
00386 {
00387 switch (tag) {
00388 case 0x1901:
00389 mxf->packages_count = get_be32(pb);
00390 if (mxf->packages_count >= UINT_MAX / sizeof(UID))
00391 return -1;
00392 mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
00393 if (!mxf->packages_refs)
00394 return -1;
00395 url_fskip(pb, 4);
00396 get_buffer(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
00397 break;
00398 }
00399 return 0;
00400 }
00401
00402 static int mxf_read_source_clip(MXFStructuralComponent *source_clip, ByteIOContext *pb, int tag)
00403 {
00404 switch(tag) {
00405 case 0x0202:
00406 source_clip->duration = get_be64(pb);
00407 break;
00408 case 0x1201:
00409 source_clip->start_position = get_be64(pb);
00410 break;
00411 case 0x1101:
00412
00413 url_fskip(pb, 16);
00414 get_buffer(pb, source_clip->source_package_uid, 16);
00415 break;
00416 case 0x1102:
00417 source_clip->source_track_id = get_be32(pb);
00418 break;
00419 }
00420 return 0;
00421 }
00422
00423 static int mxf_read_material_package(MXFPackage *package, ByteIOContext *pb, int tag)
00424 {
00425 switch(tag) {
00426 case 0x4403:
00427 package->tracks_count = get_be32(pb);
00428 if (package->tracks_count >= UINT_MAX / sizeof(UID))
00429 return -1;
00430 package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00431 if (!package->tracks_refs)
00432 return -1;
00433 url_fskip(pb, 4);
00434 get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00435 break;
00436 }
00437 return 0;
00438 }
00439
00440 static int mxf_read_track(MXFTrack *track, ByteIOContext *pb, int tag)
00441 {
00442 switch(tag) {
00443 case 0x4801:
00444 track->track_id = get_be32(pb);
00445 break;
00446 case 0x4804:
00447 get_buffer(pb, track->track_number, 4);
00448 break;
00449 case 0x4B01:
00450 track->edit_rate.den = get_be32(pb);
00451 track->edit_rate.num = get_be32(pb);
00452 break;
00453 case 0x4803:
00454 get_buffer(pb, track->sequence_ref, 16);
00455 break;
00456 }
00457 return 0;
00458 }
00459
00460 static int mxf_read_sequence(MXFSequence *sequence, ByteIOContext *pb, int tag)
00461 {
00462 switch(tag) {
00463 case 0x0202:
00464 sequence->duration = get_be64(pb);
00465 break;
00466 case 0x0201:
00467 get_buffer(pb, sequence->data_definition_ul, 16);
00468 break;
00469 case 0x1001:
00470 sequence->structural_components_count = get_be32(pb);
00471 if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
00472 return -1;
00473 sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
00474 if (!sequence->structural_components_refs)
00475 return -1;
00476 url_fskip(pb, 4);
00477 get_buffer(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
00478 break;
00479 }
00480 return 0;
00481 }
00482
00483 static int mxf_read_source_package(MXFPackage *package, ByteIOContext *pb, int tag)
00484 {
00485 switch(tag) {
00486 case 0x4403:
00487 package->tracks_count = get_be32(pb);
00488 if (package->tracks_count >= UINT_MAX / sizeof(UID))
00489 return -1;
00490 package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00491 if (!package->tracks_refs)
00492 return -1;
00493 url_fskip(pb, 4);
00494 get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00495 break;
00496 case 0x4401:
00497
00498 url_fskip(pb, 16);
00499 get_buffer(pb, package->package_uid, 16);
00500 break;
00501 case 0x4701:
00502 get_buffer(pb, package->descriptor_ref, 16);
00503 break;
00504 }
00505 return 0;
00506 }
00507
00508 static int mxf_read_index_table_segment(MXFIndexTableSegment *segment, ByteIOContext *pb, int tag)
00509 {
00510 switch(tag) {
00511 case 0x3F05: dprintf(NULL, "EditUnitByteCount %d\n", get_be32(pb)); break;
00512 case 0x3F06: dprintf(NULL, "IndexSID %d\n", get_be32(pb)); break;
00513 case 0x3F07: dprintf(NULL, "BodySID %d\n", get_be32(pb)); break;
00514 case 0x3F0B: dprintf(NULL, "IndexEditRate %d/%d\n", get_be32(pb), get_be32(pb)); break;
00515 case 0x3F0C: dprintf(NULL, "IndexStartPosition %lld\n", get_be64(pb)); break;
00516 case 0x3F0D: dprintf(NULL, "IndexDuration %lld\n", get_be64(pb)); break;
00517 }
00518 return 0;
00519 }
00520
00521 static void mxf_read_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
00522 {
00523 int code;
00524
00525 do {
00526 code = get_byte(pb);
00527 dprintf(NULL, "pixel layout: code %#x\n", code);
00528 switch (code) {
00529 case 0x52:
00530 descriptor->bits_per_sample += get_byte(pb);
00531 break;
00532 case 0x47:
00533 descriptor->bits_per_sample += get_byte(pb);
00534 break;
00535 case 0x42:
00536 descriptor->bits_per_sample += get_byte(pb);
00537 break;
00538 default:
00539 get_byte(pb);
00540 }
00541 } while (code != 0);
00542 }
00543
00544 static int mxf_read_generic_descriptor(MXFDescriptor *descriptor, ByteIOContext *pb, int tag, int size, UID uid)
00545 {
00546 switch(tag) {
00547 case 0x3F01:
00548 descriptor->sub_descriptors_count = get_be32(pb);
00549 if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
00550 return -1;
00551 descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
00552 if (!descriptor->sub_descriptors_refs)
00553 return -1;
00554 url_fskip(pb, 4);
00555 get_buffer(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
00556 break;
00557 case 0x3004:
00558 get_buffer(pb, descriptor->essence_container_ul, 16);
00559 break;
00560 case 0x3006:
00561 descriptor->linked_track_id = get_be32(pb);
00562 break;
00563 case 0x3201:
00564 get_buffer(pb, descriptor->essence_codec_ul, 16);
00565 break;
00566 case 0x3203:
00567 descriptor->width = get_be32(pb);
00568 break;
00569 case 0x3202:
00570 descriptor->height = get_be32(pb);
00571 break;
00572 case 0x320E:
00573 descriptor->aspect_ratio.num = get_be32(pb);
00574 descriptor->aspect_ratio.den = get_be32(pb);
00575 break;
00576 case 0x3D03:
00577 descriptor->sample_rate.num = get_be32(pb);
00578 descriptor->sample_rate.den = get_be32(pb);
00579 break;
00580 case 0x3D06:
00581 get_buffer(pb, descriptor->essence_codec_ul, 16);
00582 break;
00583 case 0x3D07:
00584 descriptor->channels = get_be32(pb);
00585 break;
00586 case 0x3D01:
00587 descriptor->bits_per_sample = get_be32(pb);
00588 break;
00589 case 0x3401:
00590 mxf_read_pixel_layout(pb, descriptor);
00591 break;
00592 default:
00593
00594 if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
00595 descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00596 if (!descriptor->extradata)
00597 return -1;
00598 descriptor->extradata_size = size;
00599 get_buffer(pb, descriptor->extradata, size);
00600 }
00601 break;
00602 }
00603 return 0;
00604 }
00605
00606
00607
00608
00609
00610 static int mxf_match_uid(const UID key, const UID uid, int len)
00611 {
00612 int i;
00613 for (i = 0; i < len; i++) {
00614 if (i != 7 && key[i] != uid[i])
00615 return 0;
00616 }
00617 return 1;
00618 }
00619
00620 static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
00621 {
00622 while (uls->uid[0]) {
00623 if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
00624 break;
00625 uls++;
00626 }
00627 return uls;
00628 }
00629
00630 static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
00631 {
00632 int i;
00633
00634 if (!strong_ref)
00635 return NULL;
00636 for (i = 0; i < mxf->metadata_sets_count; i++) {
00637 if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
00638 (type == AnyType || mxf->metadata_sets[i]->type == type)) {
00639 return mxf->metadata_sets[i];
00640 }
00641 }
00642 return NULL;
00643 }
00644
00645 static const MXFCodecUL mxf_essence_container_uls[] = {
00646
00647 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO },
00648 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, CODEC_ID_DVVIDEO },
00649
00650 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE },
00651 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, CODEC_ID_MP2 },
00652 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, CODEC_ID_PCM_S16LE },
00653 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
00654 };
00655
00656 static int mxf_parse_structural_metadata(MXFContext *mxf)
00657 {
00658 MXFPackage *material_package = NULL;
00659 MXFPackage *temp_package = NULL;
00660 int i, j, k;
00661
00662 dprintf(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
00663
00664 for (i = 0; i < mxf->packages_count; i++) {
00665 material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
00666 if (material_package) break;
00667 }
00668 if (!material_package) {
00669 av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
00670 return -1;
00671 }
00672
00673 for (i = 0; i < material_package->tracks_count; i++) {
00674 MXFPackage *source_package = NULL;
00675 MXFTrack *material_track = NULL;
00676 MXFTrack *source_track = NULL;
00677 MXFTrack *temp_track = NULL;
00678 MXFDescriptor *descriptor = NULL;
00679 MXFStructuralComponent *component = NULL;
00680 UID *essence_container_ul = NULL;
00681 const MXFCodecUL *codec_ul = NULL;
00682 const MXFCodecUL *container_ul = NULL;
00683 AVStream *st;
00684
00685 if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
00686 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
00687 continue;
00688 }
00689
00690 if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
00691 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
00692 continue;
00693 }
00694
00695
00696 for (j = 0; j < material_track->sequence->structural_components_count; j++) {
00697
00698 component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
00699 if (!component)
00700 continue;
00701
00702 for (k = 0; k < mxf->packages_count; k++) {
00703 temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
00704 if (!temp_package)
00705 continue;
00706 if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
00707 source_package = temp_package;
00708 break;
00709 }
00710 }
00711 if (!source_package) {
00712 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
00713 break;
00714 }
00715 for (k = 0; k < source_package->tracks_count; k++) {
00716 if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
00717 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
00718 return -1;
00719 }
00720 if (temp_track->track_id == component->source_track_id) {
00721 source_track = temp_track;
00722 break;
00723 }
00724 }
00725 if (!source_track) {
00726 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
00727 break;
00728 }
00729 }
00730 if (!source_track)
00731 continue;
00732
00733 st = av_new_stream(mxf->fc, source_track->track_id);
00734 if (!st) {
00735 av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
00736 return -1;
00737 }
00738 st->priv_data = source_track;
00739 st->duration = component->duration;
00740 if (st->duration == -1)
00741 st->duration = AV_NOPTS_VALUE;
00742 st->start_time = component->start_position;
00743 av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
00744
00745 if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
00746 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
00747 return -1;
00748 }
00749
00750 PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
00751 codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
00752 st->codec->codec_type = codec_ul->id;
00753
00754 source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
00755 if (source_package->descriptor) {
00756 if (source_package->descriptor->type == MultipleDescriptor) {
00757 for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
00758 MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
00759
00760 if (!sub_descriptor) {
00761 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
00762 continue;
00763 }
00764 if (sub_descriptor->linked_track_id == source_track->track_id) {
00765 descriptor = sub_descriptor;
00766 break;
00767 }
00768 }
00769 } else if (source_package->descriptor->type == Descriptor)
00770 descriptor = source_package->descriptor;
00771 }
00772 if (!descriptor) {
00773 av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
00774 continue;
00775 }
00776 PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
00777 PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
00778 essence_container_ul = &descriptor->essence_container_ul;
00779
00780
00781 if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
00782 av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
00783 for (k = 0; k < mxf->metadata_sets_count; k++) {
00784 MXFMetadataSet *metadata = mxf->metadata_sets[k];
00785 if (metadata->type == CryptoContext) {
00786 essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
00787 break;
00788 }
00789 }
00790 }
00791
00792 codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
00793 st->codec->codec_id = codec_ul->id;
00794 if (descriptor->extradata) {
00795 st->codec->extradata = descriptor->extradata;
00796 st->codec->extradata_size = descriptor->extradata_size;
00797 }
00798 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00799 container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
00800 if (st->codec->codec_id == CODEC_ID_NONE)
00801 st->codec->codec_id = container_ul->id;
00802 st->codec->width = descriptor->width;
00803 st->codec->height = descriptor->height;
00804 st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
00805 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00806 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00807 container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
00808 if (st->codec->codec_id == CODEC_ID_NONE)
00809 st->codec->codec_id = container_ul->id;
00810 st->codec->channels = descriptor->channels;
00811 st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
00812 st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
00813
00814 if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
00815 if (descriptor->bits_per_sample == 24)
00816 st->codec->codec_id = CODEC_ID_PCM_S24LE;
00817 else if (descriptor->bits_per_sample == 32)
00818 st->codec->codec_id = CODEC_ID_PCM_S32LE;
00819 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
00820 if (descriptor->bits_per_sample == 24)
00821 st->codec->codec_id = CODEC_ID_PCM_S24BE;
00822 else if (descriptor->bits_per_sample == 32)
00823 st->codec->codec_id = CODEC_ID_PCM_S32BE;
00824 } else if (st->codec->codec_id == CODEC_ID_MP2) {
00825 st->need_parsing = AVSTREAM_PARSE_FULL;
00826 }
00827 }
00828 if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
00829 av_log(mxf->fc, AV_LOG_WARNING, "only frame wrapped mappings are correctly supported\n");
00830 st->need_parsing = AVSTREAM_PARSE_FULL;
00831 }
00832 }
00833 return 0;
00834 }
00835
00836 static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
00837 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
00838 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
00839 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
00840 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
00841 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
00842 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
00843 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
00844 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
00845 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
00846 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
00847 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
00848 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
00849 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
00850 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track },
00851 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track },
00852 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
00853 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
00854 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
00855 };
00856
00857 static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, int (*read_child)(), int ctx_size, enum MXFMetadataSetType type)
00858 {
00859 ByteIOContext *pb = mxf->fc->pb;
00860 MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
00861 uint64_t klv_end = url_ftell(pb) + klv->length;
00862
00863 if (!ctx)
00864 return -1;
00865 while (url_ftell(pb) + 4 < klv_end) {
00866 int tag = get_be16(pb);
00867 int size = get_be16(pb);
00868 uint64_t next = url_ftell(pb) + size;
00869 UID uid = {0};
00870
00871 dprintf(mxf->fc, "local tag %#04x size %d\n", tag, size);
00872 if (!size) {
00873 av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
00874 continue;
00875 }
00876 if (tag > 0x7FFF) {
00877 int i;
00878 for (i = 0; i < mxf->local_tags_count; i++) {
00879 int local_tag = AV_RB16(mxf->local_tags+i*18);
00880 if (local_tag == tag) {
00881 memcpy(uid, mxf->local_tags+i*18+2, 16);
00882 dprintf(mxf->fc, "local tag %#04x\n", local_tag);
00883 PRINT_KEY(mxf->fc, "uid", uid);
00884 }
00885 }
00886 }
00887 if (ctx_size && tag == 0x3C0A)
00888 get_buffer(pb, ctx->uid, 16);
00889 else if (read_child(ctx, pb, tag, size, uid) < 0)
00890 return -1;
00891
00892 url_fseek(pb, next, SEEK_SET);
00893 }
00894 if (ctx_size) ctx->type = type;
00895 return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
00896 }
00897
00898 static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00899 {
00900 MXFContext *mxf = s->priv_data;
00901 KLVPacket klv;
00902
00903 if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
00904 av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
00905 return -1;
00906 }
00907 url_fseek(s->pb, -14, SEEK_CUR);
00908 mxf->fc = s;
00909 while (!url_feof(s->pb)) {
00910 const MXFMetadataReadTableEntry *metadata;
00911
00912 if (klv_read_packet(&klv, s->pb) < 0)
00913 return -1;
00914 PRINT_KEY(s, "read header", klv.key);
00915 dprintf(s, "size %lld offset %#llx\n", klv.length, klv.offset);
00916 if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
00917 IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
00918
00919 url_fseek(s->pb, klv.offset, SEEK_SET);
00920 break;
00921 }
00922
00923 for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
00924 if (IS_KLV_KEY(klv.key, metadata->key)) {
00925 int (*read)() = klv.key[5] == 0x53 ? mxf_read_local_tags : metadata->read;
00926 if (read(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type) < 0) {
00927 av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
00928 return -1;
00929 }
00930 break;
00931 }
00932 }
00933 if (!metadata->read)
00934 url_fskip(s->pb, klv.length);
00935 }
00936 return mxf_parse_structural_metadata(mxf);
00937 }
00938
00939 static int mxf_read_close(AVFormatContext *s)
00940 {
00941 MXFContext *mxf = s->priv_data;
00942 int i;
00943
00944 av_freep(&mxf->packages_refs);
00945
00946 for (i = 0; i < s->nb_streams; i++)
00947 s->streams[i]->priv_data = NULL;
00948
00949 for (i = 0; i < mxf->metadata_sets_count; i++) {
00950 switch (mxf->metadata_sets[i]->type) {
00951 case MultipleDescriptor:
00952 av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
00953 break;
00954 case Sequence:
00955 av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
00956 break;
00957 case SourcePackage:
00958 case MaterialPackage:
00959 av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
00960 break;
00961 default:
00962 break;
00963 }
00964 av_freep(&mxf->metadata_sets[i]);
00965 }
00966 av_freep(&mxf->metadata_sets);
00967 av_freep(&mxf->aesc);
00968 av_freep(&mxf->local_tags);
00969 return 0;
00970 }
00971
00972 static int mxf_probe(AVProbeData *p) {
00973 uint8_t *bufp = p->buf;
00974 uint8_t *end = p->buf + p->buf_size;
00975
00976 if (p->buf_size < sizeof(mxf_header_partition_pack_key))
00977 return 0;
00978
00979
00980 end -= sizeof(mxf_header_partition_pack_key);
00981 for (; bufp < end; bufp++) {
00982 if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
00983 return AVPROBE_SCORE_MAX;
00984 }
00985 return 0;
00986 }
00987
00988
00989
00990 static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
00991 {
00992 AVStream *st = s->streams[stream_index];
00993 int64_t seconds;
00994
00995 if (!s->bit_rate)
00996 return -1;
00997 if (sample_time < 0)
00998 sample_time = 0;
00999 seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
01000 url_fseek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
01001 av_update_cur_dts(s, st, sample_time);
01002 return 0;
01003 }
01004
01005 AVInputFormat mxf_demuxer = {
01006 "mxf",
01007 NULL_IF_CONFIG_SMALL("Material eXchange Format"),
01008 sizeof(MXFContext),
01009 mxf_probe,
01010 mxf_read_header,
01011 mxf_read_packet,
01012 mxf_read_close,
01013 mxf_read_seek,
01014 };