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