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 "libavutil/mathematics.h"
00050 #include "libavcodec/bytestream.h"
00051 #include "avformat.h"
00052 #include "internal.h"
00053 #include "mxf.h"
00054
00055 typedef enum {
00056 Header,
00057 BodyPartition,
00058 Footer
00059 } MXFPartitionType;
00060
00061 typedef enum {
00062 OP1a,
00063 OP1b,
00064 OP1c,
00065 OP2a,
00066 OP2b,
00067 OP2c,
00068 OP3a,
00069 OP3b,
00070 OP3c,
00071 OPAtom,
00072 OPSONYOpt,
00073 } MXFOP;
00074
00075 typedef struct {
00076 int closed;
00077 int complete;
00078 MXFPartitionType type;
00079 uint64_t previous_partition;
00080 int index_sid;
00081 int body_sid;
00082 int64_t this_partition;
00083 int64_t essence_offset;
00084 int64_t essence_length;
00085 int32_t kag_size;
00086 int64_t header_byte_count;
00087 int64_t index_byte_count;
00088 int pack_length;
00089 } MXFPartition;
00090
00091 typedef struct {
00092 UID uid;
00093 enum MXFMetadataSetType type;
00094 UID source_container_ul;
00095 } MXFCryptoContext;
00096
00097 typedef struct {
00098 UID uid;
00099 enum MXFMetadataSetType type;
00100 UID source_package_uid;
00101 UID data_definition_ul;
00102 int64_t duration;
00103 int64_t start_position;
00104 int source_track_id;
00105 } MXFStructuralComponent;
00106
00107 typedef struct {
00108 UID uid;
00109 enum MXFMetadataSetType type;
00110 UID data_definition_ul;
00111 UID *structural_components_refs;
00112 int structural_components_count;
00113 int64_t duration;
00114 } MXFSequence;
00115
00116 typedef struct {
00117 UID uid;
00118 enum MXFMetadataSetType type;
00119 MXFSequence *sequence;
00120 UID sequence_ref;
00121 int track_id;
00122 uint8_t track_number[4];
00123 AVRational edit_rate;
00124 } MXFTrack;
00125
00126 typedef struct {
00127 UID uid;
00128 enum MXFMetadataSetType type;
00129 UID essence_container_ul;
00130 UID essence_codec_ul;
00131 AVRational sample_rate;
00132 AVRational aspect_ratio;
00133 int width;
00134 int height;
00135 int channels;
00136 int bits_per_sample;
00137 UID *sub_descriptors_refs;
00138 int sub_descriptors_count;
00139 int linked_track_id;
00140 uint8_t *extradata;
00141 int extradata_size;
00142 enum PixelFormat pix_fmt;
00143 } MXFDescriptor;
00144
00145 typedef struct {
00146 UID uid;
00147 enum MXFMetadataSetType type;
00148 int edit_unit_byte_count;
00149 int index_sid;
00150 int body_sid;
00151 int slice_count;
00152 AVRational index_edit_rate;
00153 uint64_t index_start_position;
00154 uint64_t index_duration;
00155 int *slice;
00156 int *element_delta;
00157 int nb_delta_entries;
00158 int8_t *temporal_offset_entries;
00159 int *flag_entries;
00160 uint64_t *stream_offset_entries;
00161 uint32_t **slice_offset_entries;
00162 int nb_index_entries;
00163 } MXFIndexTableSegment;
00164
00165 typedef struct {
00166 UID uid;
00167 enum MXFMetadataSetType type;
00168 UID package_uid;
00169 UID *tracks_refs;
00170 int tracks_count;
00171 MXFDescriptor *descriptor;
00172 UID descriptor_ref;
00173 } MXFPackage;
00174
00175 typedef struct {
00176 UID uid;
00177 enum MXFMetadataSetType type;
00178 } MXFMetadataSet;
00179
00180 typedef struct {
00181 MXFPartition *partitions;
00182 unsigned partitions_count;
00183 MXFOP op;
00184 UID *packages_refs;
00185 int packages_count;
00186 MXFMetadataSet **metadata_sets;
00187 int metadata_sets_count;
00188 AVFormatContext *fc;
00189 struct AVAES *aesc;
00190 uint8_t *local_tags;
00191 int local_tags_count;
00192 uint64_t footer_partition;
00193 KLVPacket current_klv_data;
00194 int current_klv_index;
00195 int run_in;
00196 MXFPartition *current_partition;
00197 int parsing_backward;
00198 int64_t last_forward_tell;
00199 int last_forward_partition;
00200 int current_edit_unit;
00201 int current_stream;
00202 int d10;
00203 int broken_index;
00204 } MXFContext;
00205
00206 enum MXFWrappingScheme {
00207 Frame,
00208 Clip,
00209 };
00210
00211
00212 typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset);
00213
00214 typedef struct {
00215 const UID key;
00216 MXFMetadataReadFunc *read;
00217 int ctx_size;
00218 enum MXFMetadataSetType type;
00219 } MXFMetadataReadTableEntry;
00220
00221
00222 static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
00223 static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
00224 static const uint8_t mxf_system_item_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04 };
00225 static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
00226
00227 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 };
00228 static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
00229 static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
00230 static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
00231
00232 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
00233
00234 static int64_t klv_decode_ber_length(AVIOContext *pb)
00235 {
00236 uint64_t size = avio_r8(pb);
00237 if (size & 0x80) {
00238 int bytes_num = size & 0x7f;
00239
00240 if (bytes_num > 8)
00241 return -1;
00242 size = 0;
00243 while (bytes_num--)
00244 size = size << 8 | avio_r8(pb);
00245 }
00246 return size;
00247 }
00248
00249 static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
00250 {
00251 int i, b;
00252 for (i = 0; i < size && !url_feof(pb); i++) {
00253 b = avio_r8(pb);
00254 if (b == key[0])
00255 i = 0;
00256 else if (b != key[i])
00257 i = -1;
00258 }
00259 return i == size;
00260 }
00261
00262 static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
00263 {
00264 if (!mxf_read_sync(pb, mxf_klv_key, 4))
00265 return -1;
00266 klv->offset = avio_tell(pb) - 4;
00267 memcpy(klv->key, mxf_klv_key, 4);
00268 avio_read(pb, klv->key + 4, 12);
00269 klv->length = klv_decode_ber_length(pb);
00270 return klv->length == -1 ? -1 : 0;
00271 }
00272
00273 static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
00274 {
00275 int i;
00276
00277 for (i = 0; i < s->nb_streams; i++) {
00278 MXFTrack *track = s->streams[i]->priv_data;
00279
00280 if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
00281 return i;
00282 }
00283
00284 return s->nb_streams == 1 ? 0 : -1;
00285 }
00286
00287
00288 static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
00289 {
00290 const uint8_t *buf_ptr, *end_ptr;
00291 uint8_t *data_ptr;
00292 int i;
00293
00294 if (length > 61444)
00295 return -1;
00296 length = av_get_packet(pb, pkt, length);
00297 if (length < 0)
00298 return length;
00299 data_ptr = pkt->data;
00300 end_ptr = pkt->data + length;
00301 buf_ptr = pkt->data + 4;
00302 for (; buf_ptr + st->codec->channels*4 < end_ptr; ) {
00303 for (i = 0; i < st->codec->channels; i++) {
00304 uint32_t sample = bytestream_get_le32(&buf_ptr);
00305 if (st->codec->bits_per_coded_sample == 24)
00306 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
00307 else
00308 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
00309 }
00310 buf_ptr += 32 - st->codec->channels*4;
00311 }
00312 av_shrink_packet(pkt, data_ptr - pkt->data);
00313 return 0;
00314 }
00315
00316 static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
00317 {
00318 static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
00319 MXFContext *mxf = s->priv_data;
00320 AVIOContext *pb = s->pb;
00321 int64_t end = avio_tell(pb) + klv->length;
00322 int64_t size;
00323 uint64_t orig_size;
00324 uint64_t plaintext_size;
00325 uint8_t ivec[16];
00326 uint8_t tmpbuf[16];
00327 int index;
00328
00329 if (!mxf->aesc && s->key && s->keylen == 16) {
00330 mxf->aesc = av_malloc(av_aes_size);
00331 if (!mxf->aesc)
00332 return -1;
00333 av_aes_init(mxf->aesc, s->key, 128, 1);
00334 }
00335
00336 avio_skip(pb, klv_decode_ber_length(pb));
00337
00338 klv_decode_ber_length(pb);
00339 plaintext_size = avio_rb64(pb);
00340
00341 klv_decode_ber_length(pb);
00342 avio_read(pb, klv->key, 16);
00343 if (!IS_KLV_KEY(klv, mxf_essence_element_key))
00344 return -1;
00345 index = mxf_get_stream_index(s, klv);
00346 if (index < 0)
00347 return -1;
00348
00349 klv_decode_ber_length(pb);
00350 orig_size = avio_rb64(pb);
00351 if (orig_size < plaintext_size)
00352 return -1;
00353
00354 size = klv_decode_ber_length(pb);
00355 if (size < 32 || size - 32 < orig_size)
00356 return -1;
00357 avio_read(pb, ivec, 16);
00358 avio_read(pb, tmpbuf, 16);
00359 if (mxf->aesc)
00360 av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
00361 if (memcmp(tmpbuf, checkv, 16))
00362 av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
00363 size -= 32;
00364 size = av_get_packet(pb, pkt, size);
00365 if (size < 0)
00366 return size;
00367 else if (size < plaintext_size)
00368 return AVERROR_INVALIDDATA;
00369 size -= plaintext_size;
00370 if (mxf->aesc)
00371 av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
00372 &pkt->data[plaintext_size], size >> 4, ivec, 1);
00373 av_shrink_packet(pkt, orig_size);
00374 pkt->stream_index = index;
00375 avio_skip(pb, end - avio_tell(pb));
00376 return 0;
00377 }
00378
00379 static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt)
00380 {
00381 KLVPacket klv;
00382
00383 while (!url_feof(s->pb)) {
00384 if (klv_read_packet(&klv, s->pb) < 0)
00385 return -1;
00386 PRINT_KEY(s, "read packet", klv.key);
00387 av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
00388 if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
00389 int res = mxf_decrypt_triplet(s, pkt, &klv);
00390 if (res < 0) {
00391 av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
00392 return -1;
00393 }
00394 return 0;
00395 }
00396 if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
00397 int index = mxf_get_stream_index(s, &klv);
00398 if (index < 0) {
00399 av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12));
00400 goto skip;
00401 }
00402 if (s->streams[index]->discard == AVDISCARD_ALL)
00403 goto skip;
00404
00405 if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
00406 if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
00407 av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
00408 return -1;
00409 }
00410 } else {
00411 int ret = av_get_packet(s->pb, pkt, klv.length);
00412 if (ret < 0)
00413 return ret;
00414 }
00415 pkt->stream_index = index;
00416 pkt->pos = klv.offset;
00417 return 0;
00418 } else
00419 skip:
00420 avio_skip(s->pb, klv.length);
00421 }
00422 return AVERROR_EOF;
00423 }
00424
00425 static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
00426 {
00427 MXFContext *mxf = s->priv_data;
00428 AVIndexEntry *e;
00429 int ret;
00430 int64_t ret64;
00431 KLVPacket klv;
00432 AVStream *st;
00433
00434
00435
00436 if (mxf->op != OPAtom)
00437 return mxf_read_packet_old(s, pkt);
00438
00439 if (mxf->current_stream >= s->nb_streams) {
00440 mxf->current_edit_unit++;
00441 mxf->current_stream = 0;
00442 }
00443
00444 st = s->streams[mxf->current_stream];
00445
00446 if (mxf->current_edit_unit >= st->nb_index_entries)
00447 return AVERROR_EOF;
00448
00449 e = &st->index_entries[mxf->current_edit_unit];
00450
00451 if ((ret64 = avio_seek(s->pb, e->pos, SEEK_SET)) < 0)
00452 return ret64;
00453
00454 if (mxf->op == OPAtom) {
00455
00456 if ((ret = av_get_packet(s->pb, pkt, e->size)) != e->size)
00457 return ret < 0 ? ret : AVERROR_EOF;
00458 } else {
00459
00460 if ((ret = klv_read_packet(&klv, s->pb)) < 0)
00461 return ret;
00462
00463
00464 if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
00465 int res = mxf_decrypt_triplet(s, pkt, &klv);
00466 if (res < 0) {
00467 av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
00468 return -1;
00469 }
00470 return 0;
00471 }
00472
00473 if ((ret = av_get_packet(s->pb, pkt, klv.length)) != klv.length)
00474 return ret < 0 ? ret : AVERROR_EOF;
00475
00476 pkt->pos = e->pos;
00477 }
00478
00479 pkt->stream_index = mxf->current_stream++;
00480
00481 return 0;
00482 }
00483
00484 static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00485 {
00486 MXFContext *mxf = arg;
00487 int item_num = avio_rb32(pb);
00488 int item_len = avio_rb32(pb);
00489
00490 if (item_len != 18) {
00491 av_log(mxf->fc, AV_LOG_ERROR, "unsupported primer pack item length\n");
00492 return -1;
00493 }
00494 if (item_num > UINT_MAX / item_len)
00495 return -1;
00496 mxf->local_tags_count = item_num;
00497 mxf->local_tags = av_malloc(item_num*item_len);
00498 if (!mxf->local_tags)
00499 return -1;
00500 avio_read(pb, mxf->local_tags, item_num*item_len);
00501 return 0;
00502 }
00503
00504 static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00505 {
00506 MXFContext *mxf = arg;
00507 MXFPartition *partition;
00508 UID op;
00509 uint64_t footer_partition;
00510
00511 if (mxf->partitions_count+1 >= UINT_MAX / sizeof(*mxf->partitions))
00512 return AVERROR(ENOMEM);
00513
00514 mxf->partitions = av_realloc(mxf->partitions, (mxf->partitions_count + 1) * sizeof(*mxf->partitions));
00515 if (!mxf->partitions)
00516 return AVERROR(ENOMEM);
00517
00518 if (mxf->parsing_backward) {
00519
00520
00521 memmove(&mxf->partitions[mxf->last_forward_partition+1],
00522 &mxf->partitions[mxf->last_forward_partition],
00523 (mxf->partitions_count - mxf->last_forward_partition)*sizeof(*mxf->partitions));
00524 partition = mxf->current_partition = &mxf->partitions[mxf->last_forward_partition];
00525 } else {
00526 mxf->last_forward_partition++;
00527 partition = mxf->current_partition = &mxf->partitions[mxf->partitions_count];
00528 }
00529
00530 memset(partition, 0, sizeof(*partition));
00531 mxf->partitions_count++;
00532 partition->pack_length = avio_tell(pb) - klv_offset + size;
00533
00534 switch(uid[13]) {
00535 case 2:
00536 partition->type = Header;
00537 break;
00538 case 3:
00539 partition->type = BodyPartition;
00540 break;
00541 case 4:
00542 partition->type = Footer;
00543 break;
00544 default:
00545 av_log(mxf->fc, AV_LOG_ERROR, "unknown partition type %i\n", uid[13]);
00546 return AVERROR_INVALIDDATA;
00547 }
00548
00549
00550 partition->closed = partition->type == Footer || !(uid[14] & 1);
00551 partition->complete = uid[14] > 2;
00552 avio_skip(pb, 4);
00553 partition->kag_size = avio_rb32(pb);
00554 partition->this_partition = avio_rb64(pb);
00555 partition->previous_partition = avio_rb64(pb);
00556 footer_partition = avio_rb64(pb);
00557 partition->header_byte_count = avio_rb64(pb);
00558 partition->index_byte_count = avio_rb64(pb);
00559 partition->index_sid = avio_rb32(pb);
00560 avio_skip(pb, 8);
00561 partition->body_sid = avio_rb32(pb);
00562 avio_read(pb, op, sizeof(UID));
00563
00564
00565 if (footer_partition) {
00566 if (mxf->footer_partition && mxf->footer_partition != footer_partition) {
00567 av_log(mxf->fc, AV_LOG_ERROR, "inconsistent FooterPartition value: %li != %li\n",
00568 mxf->footer_partition, footer_partition);
00569 } else {
00570 mxf->footer_partition = footer_partition;
00571 }
00572 }
00573
00574 av_dlog(mxf->fc, "PartitionPack: ThisPartition = 0x%lx, PreviousPartition = 0x%lx, "
00575 "FooterPartition = 0x%lx, IndexSID = %i, BodySID = %i\n",
00576 partition->this_partition,
00577 partition->previous_partition, footer_partition,
00578 partition->index_sid, partition->body_sid);
00579
00580
00581 if (partition->previous_partition &&
00582 mxf->run_in + partition->previous_partition >= klv_offset) {
00583 av_log(mxf->fc, AV_LOG_ERROR, "PreviousPartition points to this partition or forward\n");
00584 return AVERROR_INVALIDDATA;
00585 }
00586
00587 if (op[12] == 1 && op[13] == 1) mxf->op = OP1a;
00588 else if (op[12] == 1 && op[13] == 2) mxf->op = OP1b;
00589 else if (op[12] == 1 && op[13] == 3) mxf->op = OP1c;
00590 else if (op[12] == 2 && op[13] == 1) mxf->op = OP2a;
00591 else if (op[12] == 2 && op[13] == 2) mxf->op = OP2b;
00592 else if (op[12] == 2 && op[13] == 3) mxf->op = OP2c;
00593 else if (op[12] == 3 && op[13] == 1) mxf->op = OP3a;
00594 else if (op[12] == 3 && op[13] == 2) mxf->op = OP3b;
00595 else if (op[12] == 3 && op[13] == 3) mxf->op = OP3c;
00596 else if (op[12] == 0x10) mxf->op = OPAtom;
00597 else if (op[12] == 64&& op[13] == 1) mxf->op = OPSONYOpt;
00598 else {
00599 av_log(mxf->fc, AV_LOG_ERROR, "unknown operational pattern: %02xh %02xh - guessing OP1a\n", op[12], op[13]);
00600 mxf->op = OP1a;
00601 }
00602
00603 if (partition->kag_size <= 0 || partition->kag_size > (1 << 20)) {
00604 av_log(mxf->fc, AV_LOG_WARNING, "invalid KAGSize %i - guessing ", partition->kag_size);
00605
00606 if (mxf->op == OPSONYOpt)
00607 partition->kag_size = 512;
00608 else
00609 partition->kag_size = 1;
00610
00611 av_log(mxf->fc, AV_LOG_WARNING, "%i\n", partition->kag_size);
00612 }
00613
00614 return 0;
00615 }
00616
00617 static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
00618 {
00619 if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets))
00620 return AVERROR(ENOMEM);
00621 mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
00622 if (!mxf->metadata_sets)
00623 return -1;
00624 mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
00625 mxf->metadata_sets_count++;
00626 return 0;
00627 }
00628
00629 static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00630 {
00631 MXFCryptoContext *cryptocontext = arg;
00632 if (size != 16)
00633 return -1;
00634 if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
00635 avio_read(pb, cryptocontext->source_container_ul, 16);
00636 return 0;
00637 }
00638
00639 static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00640 {
00641 MXFContext *mxf = arg;
00642 switch (tag) {
00643 case 0x1901:
00644 mxf->packages_count = avio_rb32(pb);
00645 if (mxf->packages_count >= UINT_MAX / sizeof(UID))
00646 return -1;
00647 mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
00648 if (!mxf->packages_refs)
00649 return -1;
00650 avio_skip(pb, 4);
00651 avio_read(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
00652 break;
00653 }
00654 return 0;
00655 }
00656
00657 static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00658 {
00659 MXFStructuralComponent *source_clip = arg;
00660 switch(tag) {
00661 case 0x0202:
00662 source_clip->duration = avio_rb64(pb);
00663 break;
00664 case 0x1201:
00665 source_clip->start_position = avio_rb64(pb);
00666 break;
00667 case 0x1101:
00668
00669 avio_skip(pb, 16);
00670 avio_read(pb, source_clip->source_package_uid, 16);
00671 break;
00672 case 0x1102:
00673 source_clip->source_track_id = avio_rb32(pb);
00674 break;
00675 }
00676 return 0;
00677 }
00678
00679 static int mxf_read_material_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00680 {
00681 MXFPackage *package = arg;
00682 switch(tag) {
00683 case 0x4403:
00684 package->tracks_count = avio_rb32(pb);
00685 if (package->tracks_count >= UINT_MAX / sizeof(UID))
00686 return -1;
00687 package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00688 if (!package->tracks_refs)
00689 return -1;
00690 avio_skip(pb, 4);
00691 avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00692 break;
00693 }
00694 return 0;
00695 }
00696
00697 static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00698 {
00699 MXFTrack *track = arg;
00700 switch(tag) {
00701 case 0x4801:
00702 track->track_id = avio_rb32(pb);
00703 break;
00704 case 0x4804:
00705 avio_read(pb, track->track_number, 4);
00706 break;
00707 case 0x4B01:
00708 track->edit_rate.den = avio_rb32(pb);
00709 track->edit_rate.num = avio_rb32(pb);
00710 break;
00711 case 0x4803:
00712 avio_read(pb, track->sequence_ref, 16);
00713 break;
00714 }
00715 return 0;
00716 }
00717
00718 static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00719 {
00720 MXFSequence *sequence = arg;
00721 switch(tag) {
00722 case 0x0202:
00723 sequence->duration = avio_rb64(pb);
00724 break;
00725 case 0x0201:
00726 avio_read(pb, sequence->data_definition_ul, 16);
00727 break;
00728 case 0x1001:
00729 sequence->structural_components_count = avio_rb32(pb);
00730 if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
00731 return -1;
00732 sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
00733 if (!sequence->structural_components_refs)
00734 return -1;
00735 avio_skip(pb, 4);
00736 avio_read(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
00737 break;
00738 }
00739 return 0;
00740 }
00741
00742 static int mxf_read_source_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00743 {
00744 MXFPackage *package = arg;
00745 switch(tag) {
00746 case 0x4403:
00747 package->tracks_count = avio_rb32(pb);
00748 if (package->tracks_count >= UINT_MAX / sizeof(UID))
00749 return -1;
00750 package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00751 if (!package->tracks_refs)
00752 return -1;
00753 avio_skip(pb, 4);
00754 avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00755 break;
00756 case 0x4401:
00757
00758 avio_skip(pb, 16);
00759 avio_read(pb, package->package_uid, 16);
00760 break;
00761 case 0x4701:
00762 avio_read(pb, package->descriptor_ref, 16);
00763 break;
00764 }
00765 return 0;
00766 }
00767
00768 static int mxf_read_delta_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
00769 {
00770 int i, length;
00771
00772 segment->nb_delta_entries = avio_rb32(pb);
00773 length = avio_rb32(pb);
00774
00775 if (!(segment->slice = av_calloc(segment->nb_delta_entries, sizeof(*segment->slice))) ||
00776 !(segment->element_delta = av_calloc(segment->nb_delta_entries, sizeof(*segment->element_delta))))
00777 return AVERROR(ENOMEM);
00778
00779 for (i = 0; i < segment->nb_delta_entries; i++) {
00780 avio_r8(pb);
00781 segment->slice[i] = avio_r8(pb);
00782 segment->element_delta[i] = avio_rb32(pb);
00783 }
00784 return 0;
00785 }
00786
00787 static int mxf_read_index_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
00788 {
00789 int i, j, length;
00790
00791 segment->nb_index_entries = avio_rb32(pb);
00792 length = avio_rb32(pb);
00793
00794 if (!(segment->temporal_offset_entries=av_calloc(segment->nb_index_entries, sizeof(*segment->temporal_offset_entries))) ||
00795 !(segment->flag_entries = av_calloc(segment->nb_index_entries, sizeof(*segment->flag_entries))) ||
00796 !(segment->stream_offset_entries = av_calloc(segment->nb_index_entries, sizeof(*segment->stream_offset_entries))))
00797 return AVERROR(ENOMEM);
00798
00799 if (segment->slice_count &&
00800 !(segment->slice_offset_entries = av_calloc(segment->nb_index_entries, sizeof(*segment->slice_offset_entries))))
00801 return AVERROR(ENOMEM);
00802
00803 for (i = 0; i < segment->nb_index_entries; i++) {
00804 segment->temporal_offset_entries[i] = avio_r8(pb);
00805 avio_r8(pb);
00806 segment->flag_entries[i] = avio_r8(pb);
00807 segment->stream_offset_entries[i] = avio_rb64(pb);
00808 if (segment->slice_count) {
00809 if (!(segment->slice_offset_entries[i] = av_calloc(segment->slice_count, sizeof(**segment->slice_offset_entries))))
00810 return AVERROR(ENOMEM);
00811
00812 for (j = 0; j < segment->slice_count; j++)
00813 segment->slice_offset_entries[i][j] = avio_rb32(pb);
00814 }
00815
00816 avio_skip(pb, length - 11 - 4 * segment->slice_count);
00817 }
00818 return 0;
00819 }
00820
00821 static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00822 {
00823 MXFIndexTableSegment *segment = arg;
00824 switch(tag) {
00825 case 0x3F05:
00826 segment->edit_unit_byte_count = avio_rb32(pb);
00827 av_dlog(NULL, "EditUnitByteCount %d\n", segment->edit_unit_byte_count);
00828 break;
00829 case 0x3F06:
00830 segment->index_sid = avio_rb32(pb);
00831 av_dlog(NULL, "IndexSID %d\n", segment->index_sid);
00832 break;
00833 case 0x3F07:
00834 segment->body_sid = avio_rb32(pb);
00835 av_dlog(NULL, "BodySID %d\n", segment->body_sid);
00836 break;
00837 case 0x3F08:
00838 segment->slice_count = avio_r8(pb);
00839 av_dlog(NULL, "SliceCount %d\n", segment->slice_count);
00840 break;
00841 case 0x3F09:
00842 av_dlog(NULL, "DeltaEntryArray found\n");
00843 return mxf_read_delta_entry_array(pb, segment);
00844 case 0x3F0A:
00845 av_dlog(NULL, "IndexEntryArray found\n");
00846 return mxf_read_index_entry_array(pb, segment);
00847 case 0x3F0B:
00848 segment->index_edit_rate.num = avio_rb32(pb);
00849 segment->index_edit_rate.den = avio_rb32(pb);
00850 av_dlog(NULL, "IndexEditRate %d/%d\n", segment->index_edit_rate.num,
00851 segment->index_edit_rate.den);
00852 break;
00853 case 0x3F0C:
00854 segment->index_start_position = avio_rb64(pb);
00855 av_dlog(NULL, "IndexStartPosition %"PRId64"\n", segment->index_start_position);
00856 break;
00857 case 0x3F0D:
00858 segment->index_duration = avio_rb64(pb);
00859 av_dlog(NULL, "IndexDuration %"PRId64"\n", segment->index_duration);
00860 break;
00861 }
00862 return 0;
00863 }
00864
00865 static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
00866 {
00867 int code, value, ofs = 0;
00868 char layout[16] = {0};
00869
00870 do {
00871 code = avio_r8(pb);
00872 value = avio_r8(pb);
00873 av_dlog(NULL, "pixel layout: code %#x\n", code);
00874
00875 if (ofs < 16) {
00876 layout[ofs++] = code;
00877 layout[ofs++] = value;
00878 }
00879 } while (code != 0);
00880
00881 ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
00882 }
00883
00884 static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00885 {
00886 MXFDescriptor *descriptor = arg;
00887 switch(tag) {
00888 case 0x3F01:
00889 descriptor->sub_descriptors_count = avio_rb32(pb);
00890 if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
00891 return -1;
00892 descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
00893 if (!descriptor->sub_descriptors_refs)
00894 return -1;
00895 avio_skip(pb, 4);
00896 avio_read(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
00897 break;
00898 case 0x3004:
00899 avio_read(pb, descriptor->essence_container_ul, 16);
00900 break;
00901 case 0x3006:
00902 descriptor->linked_track_id = avio_rb32(pb);
00903 break;
00904 case 0x3201:
00905 avio_read(pb, descriptor->essence_codec_ul, 16);
00906 break;
00907 case 0x3203:
00908 descriptor->width = avio_rb32(pb);
00909 break;
00910 case 0x3202:
00911 descriptor->height = avio_rb32(pb);
00912 break;
00913 case 0x320E:
00914 descriptor->aspect_ratio.num = avio_rb32(pb);
00915 descriptor->aspect_ratio.den = avio_rb32(pb);
00916 break;
00917 case 0x3D03:
00918 descriptor->sample_rate.num = avio_rb32(pb);
00919 descriptor->sample_rate.den = avio_rb32(pb);
00920 break;
00921 case 0x3D06:
00922 avio_read(pb, descriptor->essence_codec_ul, 16);
00923 break;
00924 case 0x3D07:
00925 descriptor->channels = avio_rb32(pb);
00926 break;
00927 case 0x3D01:
00928 descriptor->bits_per_sample = avio_rb32(pb);
00929 break;
00930 case 0x3401:
00931 mxf_read_pixel_layout(pb, descriptor);
00932 break;
00933 default:
00934
00935 if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
00936 descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00937 if (!descriptor->extradata)
00938 return -1;
00939 descriptor->extradata_size = size;
00940 avio_read(pb, descriptor->extradata, size);
00941 }
00942 break;
00943 }
00944 return 0;
00945 }
00946
00947
00948
00949
00950
00951 static int mxf_match_uid(const UID key, const UID uid, int len)
00952 {
00953 int i;
00954 for (i = 0; i < len; i++) {
00955 if (i != 7 && key[i] != uid[i])
00956 return 0;
00957 }
00958 return 1;
00959 }
00960
00961 static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
00962 {
00963 while (uls->uid[0]) {
00964 if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
00965 break;
00966 uls++;
00967 }
00968 return uls;
00969 }
00970
00971 static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
00972 {
00973 int i;
00974
00975 if (!strong_ref)
00976 return NULL;
00977 for (i = 0; i < mxf->metadata_sets_count; i++) {
00978 if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
00979 (type == AnyType || mxf->metadata_sets[i]->type == type)) {
00980 return mxf->metadata_sets[i];
00981 }
00982 }
00983 return NULL;
00984 }
00985
00986 static const MXFCodecUL mxf_picture_essence_container_uls[] = {
00987
00988 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO },
00989 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, CODEC_ID_DVVIDEO },
00990 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
00991 };
00992 static const MXFCodecUL mxf_sound_essence_container_uls[] = {
00993
00994 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE },
00995 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, CODEC_ID_MP2 },
00996 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, CODEC_ID_PCM_S16LE },
00997 { { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0xFF,0x4B,0x46,0x41,0x41,0x00,0x0D,0x4D,0x4F }, 14, CODEC_ID_PCM_S16LE },
00998 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
00999 };
01000
01001 static UID mxf_d10_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 };
01002
01003 static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
01004 {
01005 int i, j, nb_segments = 0;
01006 MXFIndexTableSegment **unsorted_segments;
01007 int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
01008
01009
01010 for (i = 0; i < mxf->metadata_sets_count; i++)
01011 if (mxf->metadata_sets[i]->type == IndexTableSegment)
01012 nb_segments++;
01013
01014 if (!(unsorted_segments = av_calloc(nb_segments, sizeof(*unsorted_segments))) ||
01015 !(*sorted_segments = av_calloc(nb_segments, sizeof(**sorted_segments)))) {
01016 av_free(unsorted_segments);
01017 return AVERROR(ENOMEM);
01018 }
01019
01020 for (i = j = 0; i < mxf->metadata_sets_count; i++)
01021 if (mxf->metadata_sets[i]->type == IndexTableSegment)
01022 unsorted_segments[j++] = (MXFIndexTableSegment*)mxf->metadata_sets[i];
01023
01024 *nb_sorted_segments = 0;
01025
01026
01027 for (i = 0; i < nb_segments; i++) {
01028 int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
01029
01030 for (j = 0; j < nb_segments; j++) {
01031 MXFIndexTableSegment *s = unsorted_segments[j];
01032
01033
01034
01035
01036 if ((i == 0 || s->body_sid > last_body_sid || s->index_sid > last_index_sid || s->index_start_position > last_index_start) &&
01037 (best == -1 || s->body_sid < best_body_sid || s->index_sid < best_index_sid || s->index_start_position < best_index_start)) {
01038 best = j;
01039 best_body_sid = s->body_sid;
01040 best_index_sid = s->index_sid;
01041 best_index_start = s->index_start_position;
01042 }
01043 }
01044
01045
01046 if (best == -1)
01047 break;
01048
01049 (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
01050 last_body_sid = best_body_sid;
01051 last_index_sid = best_index_sid;
01052 last_index_start = best_index_start;
01053 }
01054
01055 av_free(unsorted_segments);
01056
01057 return 0;
01058 }
01059
01063 static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t offset, int64_t *offset_out)
01064 {
01065 int x;
01066 int64_t offset_in = offset;
01067
01068 for (x = 0; x < mxf->partitions_count; x++) {
01069 MXFPartition *p = &mxf->partitions[x];
01070
01071 if (p->body_sid != body_sid)
01072 continue;
01073
01074 if (offset < p->essence_length || !p->essence_length) {
01075 *offset_out = p->essence_offset + offset;
01076 return 0;
01077 }
01078
01079 offset -= p->essence_length;
01080 }
01081
01082 av_log(mxf->fc, AV_LOG_ERROR, "failed to find absolute offset of %lx in BodySID %i - partial file?\n",
01083 offset_in, body_sid);
01084
01085 return AVERROR_INVALIDDATA;
01086 }
01087
01088 static int mxf_parse_index(MXFContext *mxf, int track_id, AVStream *st)
01089 {
01090 int64_t accumulated_offset = 0;
01091 int j, k, ret, nb_sorted_segments;
01092 MXFIndexTableSegment **sorted_segments = NULL;
01093 int n_delta = track_id - 1;
01094
01095 if (track_id < 1) {
01096 av_log(mxf->fc, AV_LOG_ERROR, "TrackID not positive: %i\n", track_id);
01097 ret = AVERROR_INVALIDDATA;
01098 goto err_out;
01099 }
01100
01101 if ((ret = mxf_get_sorted_table_segments(mxf, &nb_sorted_segments, &sorted_segments)))
01102 goto err_out;
01103
01104 if (nb_sorted_segments <= 0) {
01105 av_log(mxf->fc, AV_LOG_WARNING, "Empty index for stream %i\n", st->index);
01106 ret = 0;
01107 goto err_out;
01108 }
01109
01110 for (j = 0; j < nb_sorted_segments; j++) {
01111 int duration, sample_duration = 1, last_sample_size = 0;
01112 int64_t segment_size;
01113 MXFIndexTableSegment *tableseg = sorted_segments[j];
01114
01115
01116 if (j > 0 && tableseg->body_sid != sorted_segments[j-1]->body_sid)
01117 accumulated_offset = 0;
01118
01119 if (n_delta >= tableseg->nb_delta_entries && st->index != 0)
01120 continue;
01121 duration = tableseg->index_duration > 0 ? tableseg->index_duration :
01122 st->duration - st->nb_index_entries;
01123 segment_size = tableseg->edit_unit_byte_count * duration;
01124
01125 if (tableseg->edit_unit_byte_count && tableseg->edit_unit_byte_count < 32
01126 && !tableseg->index_duration) {
01127
01128
01129 sample_duration = 8192;
01130 last_sample_size = (duration % sample_duration) * tableseg->edit_unit_byte_count;
01131 tableseg->edit_unit_byte_count *= sample_duration;
01132 duration /= sample_duration;
01133 if (last_sample_size) duration++;
01134 }
01135
01136 if (duration <= 0) {
01137 av_log(mxf->fc, AV_LOG_WARNING, "0 duration in index for stream %i\n", st->index);
01138 ret = 0;
01139 goto err_out;
01140 }
01141
01142 for (k = 0; k < duration; k++) {
01143 int64_t pos;
01144 int size, flags = 0;
01145
01146 if (k < tableseg->nb_index_entries) {
01147 pos = tableseg->stream_offset_entries[k];
01148 if (n_delta < tableseg->nb_delta_entries) {
01149 if (n_delta < tableseg->nb_delta_entries - 1) {
01150 size =
01151 tableseg->slice_offset_entries[k][tableseg->slice[n_delta+1]-1] +
01152 tableseg->element_delta[n_delta+1] -
01153 tableseg->element_delta[n_delta];
01154 if (tableseg->slice[n_delta] > 0)
01155 size -= tableseg->slice_offset_entries[k][tableseg->slice[n_delta]-1];
01156 } else if (k < duration - 1) {
01157 size = tableseg->stream_offset_entries[k+1] -
01158 tableseg->stream_offset_entries[k] -
01159 tableseg->slice_offset_entries[k][tableseg->slice[tableseg->nb_delta_entries-1]-1] -
01160 tableseg->element_delta[tableseg->nb_delta_entries-1];
01161 } else
01162 size = 0;
01163 if (tableseg->slice[n_delta] > 0)
01164 pos += tableseg->slice_offset_entries[k][tableseg->slice[n_delta]-1];
01165 pos += tableseg->element_delta[n_delta];
01166 } else
01167 size = 0;
01168 flags = !(tableseg->flag_entries[k] & 0x30) ? AVINDEX_KEYFRAME : 0;
01169 } else {
01170 pos = (int64_t)k * tableseg->edit_unit_byte_count + accumulated_offset;
01171 if (n_delta < tableseg->nb_delta_entries - 1)
01172 size = tableseg->element_delta[n_delta+1] - tableseg->element_delta[n_delta];
01173 else {
01174
01175 if (last_sample_size && k == duration - 1)
01176 size = last_sample_size;
01177 else
01178 size = tableseg->edit_unit_byte_count;
01179 if (tableseg->nb_delta_entries)
01180 size -= tableseg->element_delta[tableseg->nb_delta_entries-1];
01181 }
01182 if (n_delta < tableseg->nb_delta_entries)
01183 pos += tableseg->element_delta[n_delta];
01184 flags = AVINDEX_KEYFRAME;
01185 }
01186
01187 if (mxf_absolute_bodysid_offset(mxf, tableseg->body_sid, pos, &pos) < 0) {
01188
01189 break;
01190 }
01191
01192 av_dlog(mxf->fc, "Stream %d IndexEntry %d TrackID %d Offset %"PRIx64" Timestamp %"PRId64"\n",
01193 st->index, st->nb_index_entries, track_id, pos, sample_duration * st->nb_index_entries);
01194
01195 if ((ret = av_add_index_entry(st, pos, sample_duration * st->nb_index_entries, size, 0, flags)) < 0)
01196 return ret;
01197 }
01198 accumulated_offset += segment_size;
01199 }
01200
01201 av_free(sorted_segments);
01202
01203 return 0;
01204
01205 err_out:
01206 av_free(sorted_segments);
01207 mxf->broken_index = 1;
01208 return ret;
01209 }
01210
01211 static int mxf_parse_structural_metadata(MXFContext *mxf)
01212 {
01213 MXFPackage *material_package = NULL;
01214 MXFPackage *temp_package = NULL;
01215 int i, j, k, ret;
01216
01217 av_dlog(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
01218
01219 for (i = 0; i < mxf->packages_count; i++) {
01220 material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
01221 if (material_package) break;
01222 }
01223 if (!material_package) {
01224 av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
01225 return -1;
01226 }
01227
01228 for (i = 0; i < material_package->tracks_count; i++) {
01229 MXFPackage *source_package = NULL;
01230 MXFTrack *material_track = NULL;
01231 MXFTrack *source_track = NULL;
01232 MXFTrack *temp_track = NULL;
01233 MXFDescriptor *descriptor = NULL;
01234 MXFStructuralComponent *component = NULL;
01235 UID *essence_container_ul = NULL;
01236 const MXFCodecUL *codec_ul = NULL;
01237 const MXFCodecUL *container_ul = NULL;
01238 AVStream *st;
01239
01240 if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
01241 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
01242 continue;
01243 }
01244
01245 if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
01246 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
01247 continue;
01248 }
01249
01250
01251 for (j = 0; j < material_track->sequence->structural_components_count; j++) {
01252
01253 component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
01254 if (!component)
01255 continue;
01256
01257 for (k = 0; k < mxf->packages_count; k++) {
01258 temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
01259 if (!temp_package)
01260 continue;
01261 if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
01262 source_package = temp_package;
01263 break;
01264 }
01265 }
01266 if (!source_package) {
01267 av_dlog(mxf->fc, "material track %d: no corresponding source package found\n", material_track->track_id);
01268 break;
01269 }
01270 for (k = 0; k < source_package->tracks_count; k++) {
01271 if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
01272 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
01273 return -1;
01274 }
01275 if (temp_track->track_id == component->source_track_id) {
01276 source_track = temp_track;
01277 break;
01278 }
01279 }
01280 if (!source_track) {
01281 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
01282 break;
01283 }
01284 }
01285 if (!source_track || !component)
01286 continue;
01287
01288 if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
01289 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
01290 return -1;
01291 }
01292
01293
01294
01295 if (memcmp(material_track->sequence->data_definition_ul, source_track->sequence->data_definition_ul, 16)) {
01296 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: DataDefinition mismatch\n", material_track->track_id);
01297 continue;
01298 }
01299
01300 st = avformat_new_stream(mxf->fc, NULL);
01301 if (!st) {
01302 av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
01303 return -1;
01304 }
01305 st->id = source_track->track_id;
01306 st->priv_data = source_track;
01307 st->duration = component->duration;
01308 if (st->duration == -1)
01309 st->duration = AV_NOPTS_VALUE;
01310 st->start_time = component->start_position;
01311 avpriv_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
01312
01313 PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
01314 codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
01315 st->codec->codec_type = codec_ul->id;
01316
01317 source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
01318 if (source_package->descriptor) {
01319 if (source_package->descriptor->type == MultipleDescriptor) {
01320 for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
01321 MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
01322
01323 if (!sub_descriptor) {
01324 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
01325 continue;
01326 }
01327 if (sub_descriptor->linked_track_id == source_track->track_id) {
01328 descriptor = sub_descriptor;
01329 break;
01330 }
01331 }
01332 } else if (source_package->descriptor->type == Descriptor)
01333 descriptor = source_package->descriptor;
01334 }
01335 if (!descriptor) {
01336 av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
01337 continue;
01338 }
01339 PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
01340 PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
01341 essence_container_ul = &descriptor->essence_container_ul;
01342
01343
01344 if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
01345 av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
01346 for (k = 0; k < mxf->metadata_sets_count; k++) {
01347 MXFMetadataSet *metadata = mxf->metadata_sets[k];
01348 if (metadata->type == CryptoContext) {
01349 essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
01350 break;
01351 }
01352 }
01353 }
01354
01355
01356 if (mxf_match_uid(essence_container_ul, mxf_d10_ul, 14))
01357 mxf->d10 = 1;
01358
01359
01360 codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
01361 st->codec->codec_id = codec_ul->id;
01362 if (descriptor->extradata) {
01363 st->codec->extradata = descriptor->extradata;
01364 st->codec->extradata_size = descriptor->extradata_size;
01365 }
01366 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01367 container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul);
01368 if (st->codec->codec_id == CODEC_ID_NONE)
01369 st->codec->codec_id = container_ul->id;
01370 st->codec->width = descriptor->width;
01371 st->codec->height = descriptor->height;
01372 if (st->codec->codec_id == CODEC_ID_RAWVIDEO)
01373 st->codec->pix_fmt = descriptor->pix_fmt;
01374 st->need_parsing = AVSTREAM_PARSE_HEADERS;
01375 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
01376 container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
01377 if (st->codec->codec_id == CODEC_ID_NONE)
01378 st->codec->codec_id = container_ul->id;
01379 st->codec->channels = descriptor->channels;
01380 st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
01381 st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
01382
01383 if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
01384 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
01385 st->codec->codec_id = CODEC_ID_PCM_S24LE;
01386 else if (descriptor->bits_per_sample == 32)
01387 st->codec->codec_id = CODEC_ID_PCM_S32LE;
01388 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
01389 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
01390 st->codec->codec_id = CODEC_ID_PCM_S24BE;
01391 else if (descriptor->bits_per_sample == 32)
01392 st->codec->codec_id = CODEC_ID_PCM_S32BE;
01393 } else if (st->codec->codec_id == CODEC_ID_MP2) {
01394 st->need_parsing = AVSTREAM_PARSE_FULL;
01395 }
01396 }
01397 if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
01398
01399 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
01400 }
01401
01402 if ((ret = mxf_parse_index(mxf, material_track->track_id, st)))
01403 return ret;
01404 }
01405 return 0;
01406 }
01407
01408 static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
01409 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
01410 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
01411 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack },
01412 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack },
01413 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack },
01414 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack },
01415 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack },
01416 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack },
01417 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
01418 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
01419 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
01420 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
01421 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
01422 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
01423 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
01424 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
01425 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
01426 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01427 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01428 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01429 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01430 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01431 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01432 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track },
01433 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track },
01434 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
01435 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
01436 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
01437 };
01438
01439 static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
01440 {
01441 AVIOContext *pb = mxf->fc->pb;
01442 MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
01443 uint64_t klv_end = avio_tell(pb) + klv->length;
01444
01445 if (!ctx)
01446 return -1;
01447 while (avio_tell(pb) + 4 < klv_end && !url_feof(pb)) {
01448 int tag = avio_rb16(pb);
01449 int size = avio_rb16(pb);
01450 uint64_t next = avio_tell(pb) + size;
01451 UID uid = {0};
01452
01453 av_dlog(mxf->fc, "local tag %#04x size %d\n", tag, size);
01454 if (!size) {
01455 av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
01456 continue;
01457 }
01458 if (tag > 0x7FFF) {
01459 int i;
01460 for (i = 0; i < mxf->local_tags_count; i++) {
01461 int local_tag = AV_RB16(mxf->local_tags+i*18);
01462 if (local_tag == tag) {
01463 memcpy(uid, mxf->local_tags+i*18+2, 16);
01464 av_dlog(mxf->fc, "local tag %#04x\n", local_tag);
01465 PRINT_KEY(mxf->fc, "uid", uid);
01466 }
01467 }
01468 }
01469 if (ctx_size && tag == 0x3C0A)
01470 avio_read(pb, ctx->uid, 16);
01471 else if (read_child(ctx, pb, tag, size, uid, -1) < 0)
01472 return -1;
01473
01474 avio_seek(pb, next, SEEK_SET);
01475 }
01476 if (ctx_size) ctx->type = type;
01477 return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
01478 }
01479
01484 static int mxf_seek_to_previous_partition(MXFContext *mxf)
01485 {
01486 AVIOContext *pb = mxf->fc->pb;
01487
01488 if (!mxf->current_partition ||
01489 mxf->run_in + mxf->current_partition->previous_partition <= mxf->last_forward_tell)
01490 return 0;
01491
01492
01493 avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET);
01494 mxf->current_partition = NULL;
01495
01496 av_dlog(mxf->fc, "seeking to previous partition\n");
01497
01498 return 1;
01499 }
01500
01505 static int mxf_parse_handle_essence(MXFContext *mxf)
01506 {
01507 AVIOContext *pb = mxf->fc->pb;
01508 int64_t ret;
01509
01510 if (mxf->parsing_backward) {
01511 return mxf_seek_to_previous_partition(mxf);
01512 } else {
01513 if (!mxf->footer_partition) {
01514 av_dlog(mxf->fc, "no footer\n");
01515 return 0;
01516 }
01517
01518 av_dlog(mxf->fc, "seeking to footer\n");
01519
01520
01521 mxf->last_forward_tell = avio_tell(pb);
01522
01523 if (!pb->seekable) {
01524 av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing footer\n");
01525 return -1;
01526 }
01527
01528
01529 if ((ret = avio_seek(pb, mxf->run_in + mxf->footer_partition, SEEK_SET)) < 0) {
01530 av_log(mxf->fc, AV_LOG_ERROR, "failed to seek to footer @ 0x%"PRIx64" (%"PRId64") - partial file?\n",
01531 mxf->run_in + mxf->footer_partition, ret);
01532 return ret;
01533 }
01534
01535 mxf->current_partition = NULL;
01536 mxf->parsing_backward = 1;
01537 }
01538
01539 return 1;
01540 }
01541
01546 static int mxf_parse_handle_partition_or_eof(MXFContext *mxf)
01547 {
01548 return mxf->parsing_backward ? mxf_seek_to_previous_partition(mxf) : 1;
01549 }
01550
01554 static void mxf_compute_essence_containers(MXFContext *mxf)
01555 {
01556 int x;
01557
01558
01559 if (mxf->op == OPAtom)
01560 return;
01561
01562 for (x = 0; x < mxf->partitions_count; x++) {
01563 MXFPartition *p = &mxf->partitions[x];
01564
01565 if (!p->body_sid)
01566 continue;
01567
01568 if (x >= mxf->partitions_count - 1)
01569 break;
01570
01571
01572 p->essence_length = mxf->partitions[x+1].this_partition - p->essence_offset;
01573
01574 if (p->essence_length < 0) {
01575
01576 p->essence_length = 0;
01577 av_log(mxf->fc, AV_LOG_ERROR, "partition %i: bad ThisPartition = %lx\n",
01578 x+1, mxf->partitions[x+1].this_partition);
01579 }
01580 }
01581 }
01582
01583 static int64_t round_to_kag(int64_t position, int kag_size)
01584 {
01585
01586
01587 int64_t ret = (position / kag_size) * kag_size;
01588 return ret == position ? ret : ret + kag_size;
01589 }
01590
01591 static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
01592 {
01593 MXFContext *mxf = s->priv_data;
01594 KLVPacket klv;
01595 int64_t essence_offset = 0;
01596
01597 mxf->last_forward_tell = INT64_MAX;
01598
01599 if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
01600 av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
01601 return -1;
01602 }
01603 avio_seek(s->pb, -14, SEEK_CUR);
01604 mxf->fc = s;
01605 mxf->run_in = avio_tell(s->pb);
01606
01607 while (!url_feof(s->pb)) {
01608 const MXFMetadataReadTableEntry *metadata;
01609
01610 if (klv_read_packet(&klv, s->pb) < 0) {
01611
01612 if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
01613 break;
01614 else
01615 continue;
01616 }
01617
01618 PRINT_KEY(s, "read header", klv.key);
01619 av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
01620 if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
01621 IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
01622 IS_KLV_KEY(klv.key, mxf_system_item_key)) {
01623
01624 if (!mxf->current_partition) {
01625 av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to first PartitionPack\n");
01626 return AVERROR_INVALIDDATA;
01627 }
01628
01629 if (!mxf->current_partition->essence_offset) {
01630
01631
01632
01633
01634
01635 int64_t op1a_essence_offset =
01636 round_to_kag(mxf->current_partition->this_partition +
01637 mxf->current_partition->pack_length, mxf->current_partition->kag_size) +
01638 round_to_kag(mxf->current_partition->header_byte_count, mxf->current_partition->kag_size) +
01639 round_to_kag(mxf->current_partition->index_byte_count, mxf->current_partition->kag_size);
01640
01641 if (mxf->op == OPAtom) {
01642
01643
01644
01645 mxf->current_partition->essence_offset = avio_tell(s->pb);
01646 mxf->current_partition->essence_length = klv.length;
01647 } else {
01648
01649 mxf->current_partition->essence_offset = op1a_essence_offset;
01650 }
01651 }
01652
01653 if (!essence_offset)
01654 essence_offset = klv.offset;
01655
01656
01657 if (mxf_parse_handle_essence(mxf) <= 0)
01658 break;
01659 continue;
01660 } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) &&
01661 klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) {
01662
01663 if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
01664 break;
01665 else if (mxf->parsing_backward)
01666 continue;
01667
01668 }
01669
01670 for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
01671 if (IS_KLV_KEY(klv.key, metadata->key)) {
01672 int res;
01673 if (klv.key[5] == 0x53) {
01674 res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);
01675 } else {
01676 uint64_t next = avio_tell(s->pb) + klv.length;
01677 res = metadata->read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);
01678 avio_seek(s->pb, next, SEEK_SET);
01679 }
01680 if (res < 0) {
01681 av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
01682 return -1;
01683 }
01684 break;
01685 }
01686 }
01687 if (!metadata->read)
01688 avio_skip(s->pb, klv.length);
01689 }
01690
01691 if (!essence_offset) {
01692 av_log(s, AV_LOG_ERROR, "no essence\n");
01693 return AVERROR_INVALIDDATA;
01694 }
01695 avio_seek(s->pb, essence_offset, SEEK_SET);
01696
01697 mxf_compute_essence_containers(mxf);
01698
01699 return mxf_parse_structural_metadata(mxf);
01700 }
01701
01702 static int mxf_read_close(AVFormatContext *s)
01703 {
01704 MXFContext *mxf = s->priv_data;
01705 MXFIndexTableSegment *seg;
01706 int i, j;
01707
01708 av_freep(&mxf->packages_refs);
01709
01710 for (i = 0; i < s->nb_streams; i++)
01711 s->streams[i]->priv_data = NULL;
01712
01713 for (i = 0; i < mxf->metadata_sets_count; i++) {
01714 switch (mxf->metadata_sets[i]->type) {
01715 case MultipleDescriptor:
01716 av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
01717 break;
01718 case Sequence:
01719 av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
01720 break;
01721 case SourcePackage:
01722 case MaterialPackage:
01723 av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
01724 break;
01725 case IndexTableSegment:
01726 seg = (MXFIndexTableSegment *)mxf->metadata_sets[i];
01727 if (seg->slice_count)
01728 for (j = 0; j < seg->nb_index_entries; j++)
01729 av_freep(&seg->slice_offset_entries[j]);
01730 av_freep(&seg->slice);
01731 av_freep(&seg->element_delta);
01732 av_freep(&seg->temporal_offset_entries);
01733 av_freep(&seg->flag_entries);
01734 av_freep(&seg->stream_offset_entries);
01735 av_freep(&seg->slice_offset_entries);
01736 break;
01737 default:
01738 break;
01739 }
01740 av_freep(&mxf->metadata_sets[i]);
01741 }
01742 av_freep(&mxf->partitions);
01743 av_freep(&mxf->metadata_sets);
01744 av_freep(&mxf->aesc);
01745 av_freep(&mxf->local_tags);
01746 return 0;
01747 }
01748
01749 static int mxf_probe(AVProbeData *p) {
01750 uint8_t *bufp = p->buf;
01751 uint8_t *end = p->buf + p->buf_size;
01752
01753 if (p->buf_size < sizeof(mxf_header_partition_pack_key))
01754 return 0;
01755
01756
01757 end -= sizeof(mxf_header_partition_pack_key);
01758 for (; bufp < end; bufp++) {
01759 if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
01760 return AVPROBE_SCORE_MAX;
01761 }
01762 return 0;
01763 }
01764
01765
01766
01767 static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
01768 {
01769 AVStream *st = s->streams[stream_index];
01770 int64_t seconds;
01771 MXFContext* mxf = s->priv_data;
01772 int64_t seekpos;
01773 int index;
01774
01775 if (mxf->op != OPAtom) {
01776 if (!s->bit_rate)
01777 return -1;
01778 if (sample_time < 0)
01779 sample_time = 0;
01780 seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
01781 if (avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET) < 0)
01782 return -1;
01783 ff_update_cur_dts(s, st, sample_time);
01784 } else {
01785 if (st->nb_index_entries <= 0)
01786 return -1;
01787
01788 index = av_index_search_timestamp(st, sample_time, flags);
01789
01790 av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, sample_time, index);
01791
01792 if (index < 0) {
01793 if (sample_time < st->index_entries[0].timestamp)
01794 index = 0;
01795 else
01796 return -1;
01797 }
01798
01799 seekpos = st->index_entries[index].pos;
01800 av_update_cur_dts(s, st, st->index_entries[index].timestamp);
01801 mxf->current_edit_unit = st->index_entries[index].timestamp;
01802 mxf->current_stream = 0;
01803 avio_seek(s->pb, seekpos, SEEK_SET);
01804 }
01805 return 0;
01806 }
01807
01808 AVInputFormat ff_mxf_demuxer = {
01809 .name = "mxf",
01810 .long_name = NULL_IF_CONFIG_SMALL("Material eXchange Format"),
01811 .priv_data_size = sizeof(MXFContext),
01812 .read_probe = mxf_probe,
01813 .read_header = mxf_read_header,
01814 .read_packet = mxf_read_packet,
01815 .read_close = mxf_read_close,
01816 .read_seek = mxf_read_seek,
01817 };