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