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 "libavutil/timecode.h"
00052 #include "avformat.h"
00053 #include "internal.h"
00054 #include "mxf.h"
00055
00056 typedef enum {
00057 Header,
00058 BodyPartition,
00059 Footer
00060 } MXFPartitionType;
00061
00062 typedef enum {
00063 OP1a = 1,
00064 OP1b,
00065 OP1c,
00066 OP2a,
00067 OP2b,
00068 OP2c,
00069 OP3a,
00070 OP3b,
00071 OP3c,
00072 OPAtom,
00073 OPSONYOpt,
00074 } MXFOP;
00075
00076 typedef struct {
00077 int closed;
00078 int complete;
00079 MXFPartitionType type;
00080 uint64_t previous_partition;
00081 int index_sid;
00082 int body_sid;
00083 int64_t this_partition;
00084 int64_t essence_offset;
00085 int64_t essence_length;
00086 int32_t kag_size;
00087 int64_t header_byte_count;
00088 int64_t index_byte_count;
00089 int pack_length;
00090 } MXFPartition;
00091
00092 typedef struct {
00093 UID uid;
00094 enum MXFMetadataSetType type;
00095 UID source_container_ul;
00096 } MXFCryptoContext;
00097
00098 typedef struct {
00099 UID uid;
00100 enum MXFMetadataSetType type;
00101 UID source_package_uid;
00102 UID data_definition_ul;
00103 int64_t duration;
00104 int64_t start_position;
00105 int source_track_id;
00106 } MXFStructuralComponent;
00107
00108 typedef struct {
00109 UID uid;
00110 enum MXFMetadataSetType type;
00111 UID data_definition_ul;
00112 UID *structural_components_refs;
00113 int structural_components_count;
00114 int64_t duration;
00115 } MXFSequence;
00116
00117 typedef struct {
00118 UID uid;
00119 enum MXFMetadataSetType type;
00120 int drop_frame;
00121 int start_frame;
00122 struct AVRational rate;
00123 AVTimecode tc;
00124 } MXFTimecodeComponent;
00125
00126 typedef struct {
00127 UID uid;
00128 enum MXFMetadataSetType type;
00129 MXFSequence *sequence;
00130 UID sequence_ref;
00131 int track_id;
00132 uint8_t track_number[4];
00133 AVRational edit_rate;
00134 int intra_only;
00135 } MXFTrack;
00136
00137 typedef struct {
00138 UID uid;
00139 enum MXFMetadataSetType type;
00140 UID essence_container_ul;
00141 UID essence_codec_ul;
00142 AVRational sample_rate;
00143 AVRational aspect_ratio;
00144 int width;
00145 int height;
00146 int frame_layout;
00147 int channels;
00148 int bits_per_sample;
00149 unsigned int component_depth;
00150 unsigned int horiz_subsampling;
00151 unsigned int vert_subsampling;
00152 UID *sub_descriptors_refs;
00153 int sub_descriptors_count;
00154 int linked_track_id;
00155 uint8_t *extradata;
00156 int extradata_size;
00157 enum PixelFormat pix_fmt;
00158 } MXFDescriptor;
00159
00160 typedef struct {
00161 UID uid;
00162 enum MXFMetadataSetType type;
00163 int edit_unit_byte_count;
00164 int index_sid;
00165 int body_sid;
00166 AVRational index_edit_rate;
00167 uint64_t index_start_position;
00168 uint64_t index_duration;
00169 int8_t *temporal_offset_entries;
00170 int *flag_entries;
00171 uint64_t *stream_offset_entries;
00172 int nb_index_entries;
00173 } MXFIndexTableSegment;
00174
00175 typedef struct {
00176 UID uid;
00177 enum MXFMetadataSetType type;
00178 UID package_uid;
00179 UID *tracks_refs;
00180 int tracks_count;
00181 MXFDescriptor *descriptor;
00182 UID descriptor_ref;
00183 } MXFPackage;
00184
00185 typedef struct {
00186 UID uid;
00187 enum MXFMetadataSetType type;
00188 } MXFMetadataSet;
00189
00190
00191 typedef struct {
00192 int index_sid;
00193 int body_sid;
00194 int nb_ptses;
00195 int64_t first_dts;
00196 int64_t *ptses;
00197 int nb_segments;
00198 MXFIndexTableSegment **segments;
00199 AVIndexEntry *fake_index;
00200 } MXFIndexTable;
00201
00202 typedef struct {
00203 MXFPartition *partitions;
00204 unsigned partitions_count;
00205 MXFOP op;
00206 UID *packages_refs;
00207 int packages_count;
00208 MXFMetadataSet **metadata_sets;
00209 int metadata_sets_count;
00210 AVFormatContext *fc;
00211 struct AVAES *aesc;
00212 uint8_t *local_tags;
00213 int local_tags_count;
00214 uint64_t footer_partition;
00215 KLVPacket current_klv_data;
00216 int current_klv_index;
00217 int run_in;
00218 MXFPartition *current_partition;
00219 int parsing_backward;
00220 int64_t last_forward_tell;
00221 int last_forward_partition;
00222 int current_edit_unit;
00223 int nb_index_tables;
00224 MXFIndexTable *index_tables;
00225 int edit_units_per_packet;
00226 } MXFContext;
00227
00228 enum MXFWrappingScheme {
00229 Frame,
00230 Clip,
00231 };
00232
00233
00234 typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset);
00235
00236 typedef struct {
00237 const UID key;
00238 MXFMetadataReadFunc *read;
00239 int ctx_size;
00240 enum MXFMetadataSetType type;
00241 } MXFMetadataReadTableEntry;
00242
00243
00244 static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
00245 static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
00246 static const uint8_t mxf_avid_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 };
00247 static const uint8_t mxf_system_item_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04 };
00248 static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
00249
00250 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 };
00251 static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
00252 static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
00253 static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
00254
00255 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
00256
00257 static int64_t klv_decode_ber_length(AVIOContext *pb)
00258 {
00259 uint64_t size = avio_r8(pb);
00260 if (size & 0x80) {
00261 int bytes_num = size & 0x7f;
00262
00263 if (bytes_num > 8)
00264 return AVERROR_INVALIDDATA;
00265 size = 0;
00266 while (bytes_num--)
00267 size = size << 8 | avio_r8(pb);
00268 }
00269 return size;
00270 }
00271
00272 static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
00273 {
00274 int i, b;
00275 for (i = 0; i < size && !url_feof(pb); i++) {
00276 b = avio_r8(pb);
00277 if (b == key[0])
00278 i = 0;
00279 else if (b != key[i])
00280 i = -1;
00281 }
00282 return i == size;
00283 }
00284
00285 static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
00286 {
00287 if (!mxf_read_sync(pb, mxf_klv_key, 4))
00288 return AVERROR_INVALIDDATA;
00289 klv->offset = avio_tell(pb) - 4;
00290 memcpy(klv->key, mxf_klv_key, 4);
00291 avio_read(pb, klv->key + 4, 12);
00292 klv->length = klv_decode_ber_length(pb);
00293 return klv->length == -1 ? -1 : 0;
00294 }
00295
00296 static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
00297 {
00298 int i;
00299
00300 for (i = 0; i < s->nb_streams; i++) {
00301 MXFTrack *track = s->streams[i]->priv_data;
00302
00303 if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
00304 return i;
00305 }
00306
00307 return s->nb_streams == 1 ? 0 : -1;
00308 }
00309
00310
00311 static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
00312 {
00313 const uint8_t *buf_ptr, *end_ptr;
00314 uint8_t *data_ptr;
00315 int i;
00316
00317 if (length > 61444)
00318 return AVERROR_INVALIDDATA;
00319 length = av_get_packet(pb, pkt, length);
00320 if (length < 0)
00321 return length;
00322 data_ptr = pkt->data;
00323 end_ptr = pkt->data + length;
00324 buf_ptr = pkt->data + 4;
00325 for (; buf_ptr + st->codec->channels*4 < end_ptr; ) {
00326 for (i = 0; i < st->codec->channels; i++) {
00327 uint32_t sample = bytestream_get_le32(&buf_ptr);
00328 if (st->codec->bits_per_coded_sample == 24)
00329 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
00330 else
00331 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
00332 }
00333 buf_ptr += 32 - st->codec->channels*4;
00334 }
00335 av_shrink_packet(pkt, data_ptr - pkt->data);
00336 return 0;
00337 }
00338
00339 static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
00340 {
00341 static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
00342 MXFContext *mxf = s->priv_data;
00343 AVIOContext *pb = s->pb;
00344 int64_t end = avio_tell(pb) + klv->length;
00345 int64_t size;
00346 uint64_t orig_size;
00347 uint64_t plaintext_size;
00348 uint8_t ivec[16];
00349 uint8_t tmpbuf[16];
00350 int index;
00351
00352 if (!mxf->aesc && s->key && s->keylen == 16) {
00353 mxf->aesc = av_malloc(av_aes_size);
00354 if (!mxf->aesc)
00355 return AVERROR(ENOMEM);
00356 av_aes_init(mxf->aesc, s->key, 128, 1);
00357 }
00358
00359 avio_skip(pb, klv_decode_ber_length(pb));
00360
00361 klv_decode_ber_length(pb);
00362 plaintext_size = avio_rb64(pb);
00363
00364 klv_decode_ber_length(pb);
00365 avio_read(pb, klv->key, 16);
00366 if (!IS_KLV_KEY(klv, mxf_essence_element_key))
00367 return AVERROR_INVALIDDATA;
00368 index = mxf_get_stream_index(s, klv);
00369 if (index < 0)
00370 return AVERROR_INVALIDDATA;
00371
00372 klv_decode_ber_length(pb);
00373 orig_size = avio_rb64(pb);
00374 if (orig_size < plaintext_size)
00375 return AVERROR_INVALIDDATA;
00376
00377 size = klv_decode_ber_length(pb);
00378 if (size < 32 || size - 32 < orig_size)
00379 return AVERROR_INVALIDDATA;
00380 avio_read(pb, ivec, 16);
00381 avio_read(pb, tmpbuf, 16);
00382 if (mxf->aesc)
00383 av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
00384 if (memcmp(tmpbuf, checkv, 16))
00385 av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
00386 size -= 32;
00387 size = av_get_packet(pb, pkt, size);
00388 if (size < 0)
00389 return size;
00390 else if (size < plaintext_size)
00391 return AVERROR_INVALIDDATA;
00392 size -= plaintext_size;
00393 if (mxf->aesc)
00394 av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
00395 &pkt->data[plaintext_size], size >> 4, ivec, 1);
00396 av_shrink_packet(pkt, orig_size);
00397 pkt->stream_index = index;
00398 avio_skip(pb, end - avio_tell(pb));
00399 return 0;
00400 }
00401
00402 static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00403 {
00404 MXFContext *mxf = arg;
00405 int item_num = avio_rb32(pb);
00406 int item_len = avio_rb32(pb);
00407
00408 if (item_len != 18) {
00409 av_log_ask_for_sample(pb, "unsupported primer pack item length %d\n",
00410 item_len);
00411 return AVERROR_PATCHWELCOME;
00412 }
00413 if (item_num > UINT_MAX / item_len)
00414 return AVERROR_INVALIDDATA;
00415 mxf->local_tags_count = item_num;
00416 mxf->local_tags = av_malloc(item_num*item_len);
00417 if (!mxf->local_tags)
00418 return AVERROR(ENOMEM);
00419 avio_read(pb, mxf->local_tags, item_num*item_len);
00420 return 0;
00421 }
00422
00423 static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00424 {
00425 MXFContext *mxf = arg;
00426 MXFPartition *partition, *tmp_part;
00427 UID op;
00428 uint64_t footer_partition;
00429 uint32_t nb_essence_containers;
00430
00431 if (mxf->partitions_count+1 >= UINT_MAX / sizeof(*mxf->partitions))
00432 return AVERROR(ENOMEM);
00433
00434 tmp_part = av_realloc(mxf->partitions, (mxf->partitions_count + 1) * sizeof(*mxf->partitions));
00435 if (!tmp_part)
00436 return AVERROR(ENOMEM);
00437 mxf->partitions = tmp_part;
00438
00439 if (mxf->parsing_backward) {
00440
00441
00442 memmove(&mxf->partitions[mxf->last_forward_partition+1],
00443 &mxf->partitions[mxf->last_forward_partition],
00444 (mxf->partitions_count - mxf->last_forward_partition)*sizeof(*mxf->partitions));
00445 partition = mxf->current_partition = &mxf->partitions[mxf->last_forward_partition];
00446 } else {
00447 mxf->last_forward_partition++;
00448 partition = mxf->current_partition = &mxf->partitions[mxf->partitions_count];
00449 }
00450
00451 memset(partition, 0, sizeof(*partition));
00452 mxf->partitions_count++;
00453 partition->pack_length = avio_tell(pb) - klv_offset + size;
00454
00455 switch(uid[13]) {
00456 case 2:
00457 partition->type = Header;
00458 break;
00459 case 3:
00460 partition->type = BodyPartition;
00461 break;
00462 case 4:
00463 partition->type = Footer;
00464 break;
00465 default:
00466 av_log(mxf->fc, AV_LOG_ERROR, "unknown partition type %i\n", uid[13]);
00467 return AVERROR_INVALIDDATA;
00468 }
00469
00470
00471 partition->closed = partition->type == Footer || !(uid[14] & 1);
00472 partition->complete = uid[14] > 2;
00473 avio_skip(pb, 4);
00474 partition->kag_size = avio_rb32(pb);
00475 partition->this_partition = avio_rb64(pb);
00476 partition->previous_partition = avio_rb64(pb);
00477 footer_partition = avio_rb64(pb);
00478 partition->header_byte_count = avio_rb64(pb);
00479 partition->index_byte_count = avio_rb64(pb);
00480 partition->index_sid = avio_rb32(pb);
00481 avio_skip(pb, 8);
00482 partition->body_sid = avio_rb32(pb);
00483 avio_read(pb, op, sizeof(UID));
00484 nb_essence_containers = avio_rb32(pb);
00485
00486
00487 if (footer_partition) {
00488 if (mxf->footer_partition && mxf->footer_partition != footer_partition) {
00489 av_log(mxf->fc, AV_LOG_ERROR,
00490 "inconsistent FooterPartition value: %"PRIu64" != %"PRIu64"\n",
00491 mxf->footer_partition, footer_partition);
00492 } else {
00493 mxf->footer_partition = footer_partition;
00494 }
00495 }
00496
00497 av_dlog(mxf->fc,
00498 "PartitionPack: ThisPartition = 0x%"PRIX64
00499 ", PreviousPartition = 0x%"PRIX64", "
00500 "FooterPartition = 0x%"PRIX64", IndexSID = %i, BodySID = %i\n",
00501 partition->this_partition,
00502 partition->previous_partition, footer_partition,
00503 partition->index_sid, partition->body_sid);
00504
00505
00506 if (partition->previous_partition &&
00507 mxf->run_in + partition->previous_partition >= klv_offset) {
00508 av_log(mxf->fc, AV_LOG_ERROR,
00509 "PreviousPartition points to this partition or forward\n");
00510 return AVERROR_INVALIDDATA;
00511 }
00512
00513 if (op[12] == 1 && op[13] == 1) mxf->op = OP1a;
00514 else if (op[12] == 1 && op[13] == 2) mxf->op = OP1b;
00515 else if (op[12] == 1 && op[13] == 3) mxf->op = OP1c;
00516 else if (op[12] == 2 && op[13] == 1) mxf->op = OP2a;
00517 else if (op[12] == 2 && op[13] == 2) mxf->op = OP2b;
00518 else if (op[12] == 2 && op[13] == 3) mxf->op = OP2c;
00519 else if (op[12] == 3 && op[13] == 1) mxf->op = OP3a;
00520 else if (op[12] == 3 && op[13] == 2) mxf->op = OP3b;
00521 else if (op[12] == 3 && op[13] == 3) mxf->op = OP3c;
00522 else if (op[12] == 64&& op[13] == 1) mxf->op = OPSONYOpt;
00523 else if (op[12] == 0x10) {
00524
00525
00526
00527
00528 if (nb_essence_containers != 1) {
00529 MXFOP op = nb_essence_containers ? OP1a : OPAtom;
00530
00531
00532 if (!mxf->op)
00533 av_log(mxf->fc, AV_LOG_WARNING, "\"OPAtom\" with %u ECs - assuming %s\n",
00534 nb_essence_containers, op == OP1a ? "OP1a" : "OPAtom");
00535
00536 mxf->op = op;
00537 } else
00538 mxf->op = OPAtom;
00539 } else {
00540 av_log(mxf->fc, AV_LOG_ERROR, "unknown operational pattern: %02xh %02xh - guessing OP1a\n", op[12], op[13]);
00541 mxf->op = OP1a;
00542 }
00543
00544 if (partition->kag_size <= 0 || partition->kag_size > (1 << 20)) {
00545 av_log(mxf->fc, AV_LOG_WARNING, "invalid KAGSize %i - guessing ", partition->kag_size);
00546
00547 if (mxf->op == OPSONYOpt)
00548 partition->kag_size = 512;
00549 else
00550 partition->kag_size = 1;
00551
00552 av_log(mxf->fc, AV_LOG_WARNING, "%i\n", partition->kag_size);
00553 }
00554
00555 return 0;
00556 }
00557
00558 static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
00559 {
00560 MXFMetadataSet **tmp;
00561 if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets))
00562 return AVERROR(ENOMEM);
00563 tmp = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
00564 if (!tmp)
00565 return AVERROR(ENOMEM);
00566 mxf->metadata_sets = tmp;
00567 mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
00568 mxf->metadata_sets_count++;
00569 return 0;
00570 }
00571
00572 static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00573 {
00574 MXFCryptoContext *cryptocontext = arg;
00575 if (size != 16)
00576 return AVERROR_INVALIDDATA;
00577 if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
00578 avio_read(pb, cryptocontext->source_container_ul, 16);
00579 return 0;
00580 }
00581
00582 static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00583 {
00584 MXFContext *mxf = arg;
00585 switch (tag) {
00586 case 0x1901:
00587 mxf->packages_count = avio_rb32(pb);
00588 if (mxf->packages_count >= UINT_MAX / sizeof(UID))
00589 return AVERROR_INVALIDDATA;
00590 mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
00591 if (!mxf->packages_refs)
00592 return AVERROR(ENOMEM);
00593 avio_skip(pb, 4);
00594 avio_read(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
00595 break;
00596 }
00597 return 0;
00598 }
00599
00600 static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00601 {
00602 MXFStructuralComponent *source_clip = arg;
00603 switch(tag) {
00604 case 0x0202:
00605 source_clip->duration = avio_rb64(pb);
00606 break;
00607 case 0x1201:
00608 source_clip->start_position = avio_rb64(pb);
00609 break;
00610 case 0x1101:
00611
00612 avio_skip(pb, 16);
00613 avio_read(pb, source_clip->source_package_uid, 16);
00614 break;
00615 case 0x1102:
00616 source_clip->source_track_id = avio_rb32(pb);
00617 break;
00618 }
00619 return 0;
00620 }
00621
00622 static int mxf_read_material_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00623 {
00624 MXFPackage *package = arg;
00625 switch(tag) {
00626 case 0x4403:
00627 package->tracks_count = avio_rb32(pb);
00628 if (package->tracks_count >= UINT_MAX / sizeof(UID))
00629 return AVERROR_INVALIDDATA;
00630 package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00631 if (!package->tracks_refs)
00632 return AVERROR(ENOMEM);
00633 avio_skip(pb, 4);
00634 avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00635 break;
00636 }
00637 return 0;
00638 }
00639
00640 static int mxf_read_timecode_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00641 {
00642 MXFTimecodeComponent *mxf_timecode = arg;
00643 switch(tag) {
00644 case 0x1501:
00645 mxf_timecode->start_frame = avio_rb64(pb);
00646 break;
00647 case 0x1502:
00648 mxf_timecode->rate = (AVRational){avio_rb16(pb), 1};
00649 break;
00650 case 0x1503:
00651 mxf_timecode->drop_frame = avio_r8(pb);
00652 break;
00653 }
00654 return 0;
00655 }
00656
00657 static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00658 {
00659 MXFTrack *track = arg;
00660 switch(tag) {
00661 case 0x4801:
00662 track->track_id = avio_rb32(pb);
00663 break;
00664 case 0x4804:
00665 avio_read(pb, track->track_number, 4);
00666 break;
00667 case 0x4B01:
00668 track->edit_rate.num = avio_rb32(pb);
00669 track->edit_rate.den = avio_rb32(pb);
00670 break;
00671 case 0x4803:
00672 avio_read(pb, track->sequence_ref, 16);
00673 break;
00674 }
00675 return 0;
00676 }
00677
00678 static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00679 {
00680 MXFSequence *sequence = arg;
00681 switch(tag) {
00682 case 0x0202:
00683 sequence->duration = avio_rb64(pb);
00684 break;
00685 case 0x0201:
00686 avio_read(pb, sequence->data_definition_ul, 16);
00687 break;
00688 case 0x1001:
00689 sequence->structural_components_count = avio_rb32(pb);
00690 if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
00691 return AVERROR_INVALIDDATA;
00692 sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
00693 if (!sequence->structural_components_refs)
00694 return AVERROR(ENOMEM);
00695 avio_skip(pb, 4);
00696 avio_read(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
00697 break;
00698 }
00699 return 0;
00700 }
00701
00702 static int mxf_read_source_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00703 {
00704 MXFPackage *package = arg;
00705 switch(tag) {
00706 case 0x4403:
00707 package->tracks_count = avio_rb32(pb);
00708 if (package->tracks_count >= UINT_MAX / sizeof(UID))
00709 return AVERROR_INVALIDDATA;
00710 package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
00711 if (!package->tracks_refs)
00712 return AVERROR(ENOMEM);
00713 avio_skip(pb, 4);
00714 avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
00715 break;
00716 case 0x4401:
00717
00718 avio_skip(pb, 16);
00719 avio_read(pb, package->package_uid, 16);
00720 break;
00721 case 0x4701:
00722 avio_read(pb, package->descriptor_ref, 16);
00723 break;
00724 }
00725 return 0;
00726 }
00727
00728 static int mxf_read_index_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
00729 {
00730 int i, length;
00731
00732 segment->nb_index_entries = avio_rb32(pb);
00733 length = avio_rb32(pb);
00734
00735 if (!(segment->temporal_offset_entries=av_calloc(segment->nb_index_entries, sizeof(*segment->temporal_offset_entries))) ||
00736 !(segment->flag_entries = av_calloc(segment->nb_index_entries, sizeof(*segment->flag_entries))) ||
00737 !(segment->stream_offset_entries = av_calloc(segment->nb_index_entries, sizeof(*segment->stream_offset_entries))))
00738 return AVERROR(ENOMEM);
00739
00740 for (i = 0; i < segment->nb_index_entries; i++) {
00741 segment->temporal_offset_entries[i] = avio_r8(pb);
00742 avio_r8(pb);
00743 segment->flag_entries[i] = avio_r8(pb);
00744 segment->stream_offset_entries[i] = avio_rb64(pb);
00745 avio_skip(pb, length - 11);
00746 }
00747 return 0;
00748 }
00749
00750 static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00751 {
00752 MXFIndexTableSegment *segment = arg;
00753 switch(tag) {
00754 case 0x3F05:
00755 segment->edit_unit_byte_count = avio_rb32(pb);
00756 av_dlog(NULL, "EditUnitByteCount %d\n", segment->edit_unit_byte_count);
00757 break;
00758 case 0x3F06:
00759 segment->index_sid = avio_rb32(pb);
00760 av_dlog(NULL, "IndexSID %d\n", segment->index_sid);
00761 break;
00762 case 0x3F07:
00763 segment->body_sid = avio_rb32(pb);
00764 av_dlog(NULL, "BodySID %d\n", segment->body_sid);
00765 break;
00766 case 0x3F0A:
00767 av_dlog(NULL, "IndexEntryArray found\n");
00768 return mxf_read_index_entry_array(pb, segment);
00769 case 0x3F0B:
00770 segment->index_edit_rate.num = avio_rb32(pb);
00771 segment->index_edit_rate.den = avio_rb32(pb);
00772 av_dlog(NULL, "IndexEditRate %d/%d\n", segment->index_edit_rate.num,
00773 segment->index_edit_rate.den);
00774 break;
00775 case 0x3F0C:
00776 segment->index_start_position = avio_rb64(pb);
00777 av_dlog(NULL, "IndexStartPosition %"PRId64"\n", segment->index_start_position);
00778 break;
00779 case 0x3F0D:
00780 segment->index_duration = avio_rb64(pb);
00781 av_dlog(NULL, "IndexDuration %"PRId64"\n", segment->index_duration);
00782 break;
00783 }
00784 return 0;
00785 }
00786
00787 static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
00788 {
00789 int code, value, ofs = 0;
00790 char layout[16] = {0};
00791
00792 do {
00793 code = avio_r8(pb);
00794 value = avio_r8(pb);
00795 av_dlog(NULL, "pixel layout: code %#x\n", code);
00796
00797 if (ofs < 16) {
00798 layout[ofs++] = code;
00799 layout[ofs++] = value;
00800 }
00801 } while (code != 0);
00802
00803 ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
00804 }
00805
00806 static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
00807 {
00808 MXFDescriptor *descriptor = arg;
00809 descriptor->pix_fmt = PIX_FMT_NONE;
00810 switch(tag) {
00811 case 0x3F01:
00812 descriptor->sub_descriptors_count = avio_rb32(pb);
00813 if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
00814 return AVERROR_INVALIDDATA;
00815 descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
00816 if (!descriptor->sub_descriptors_refs)
00817 return AVERROR(ENOMEM);
00818 avio_skip(pb, 4);
00819 avio_read(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
00820 break;
00821 case 0x3004:
00822 avio_read(pb, descriptor->essence_container_ul, 16);
00823 break;
00824 case 0x3006:
00825 descriptor->linked_track_id = avio_rb32(pb);
00826 break;
00827 case 0x3201:
00828 avio_read(pb, descriptor->essence_codec_ul, 16);
00829 break;
00830 case 0x3203:
00831 descriptor->width = avio_rb32(pb);
00832 break;
00833 case 0x3202:
00834 descriptor->height = avio_rb32(pb);
00835 break;
00836 case 0x320C:
00837 descriptor->frame_layout = avio_r8(pb);
00838 break;
00839 case 0x320E:
00840 descriptor->aspect_ratio.num = avio_rb32(pb);
00841 descriptor->aspect_ratio.den = avio_rb32(pb);
00842 break;
00843 case 0x3301:
00844 descriptor->component_depth = avio_rb32(pb);
00845 break;
00846 case 0x3302:
00847 descriptor->horiz_subsampling = avio_rb32(pb);
00848 break;
00849 case 0x3308:
00850 descriptor->vert_subsampling = avio_rb32(pb);
00851 break;
00852 case 0x3D03:
00853 descriptor->sample_rate.num = avio_rb32(pb);
00854 descriptor->sample_rate.den = avio_rb32(pb);
00855 break;
00856 case 0x3D06:
00857 avio_read(pb, descriptor->essence_codec_ul, 16);
00858 break;
00859 case 0x3D07:
00860 descriptor->channels = avio_rb32(pb);
00861 break;
00862 case 0x3D01:
00863 descriptor->bits_per_sample = avio_rb32(pb);
00864 break;
00865 case 0x3401:
00866 mxf_read_pixel_layout(pb, descriptor);
00867 break;
00868 default:
00869
00870 if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
00871 descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00872 if (!descriptor->extradata)
00873 return AVERROR(ENOMEM);
00874 descriptor->extradata_size = size;
00875 avio_read(pb, descriptor->extradata, size);
00876 }
00877 break;
00878 }
00879 return 0;
00880 }
00881
00882
00883
00884
00885
00886 static int mxf_match_uid(const UID key, const UID uid, int len)
00887 {
00888 int i;
00889 for (i = 0; i < len; i++) {
00890 if (i != 7 && key[i] != uid[i])
00891 return 0;
00892 }
00893 return 1;
00894 }
00895
00896 static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
00897 {
00898 while (uls->uid[0]) {
00899 if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
00900 break;
00901 uls++;
00902 }
00903 return uls;
00904 }
00905
00906 static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
00907 {
00908 int i;
00909
00910 if (!strong_ref)
00911 return NULL;
00912 for (i = 0; i < mxf->metadata_sets_count; i++) {
00913 if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
00914 (type == AnyType || mxf->metadata_sets[i]->type == type)) {
00915 return mxf->metadata_sets[i];
00916 }
00917 }
00918 return NULL;
00919 }
00920
00921 static const MXFCodecUL mxf_picture_essence_container_uls[] = {
00922
00923 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO },
00924 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, CODEC_ID_DVVIDEO },
00925 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14, CODEC_ID_RAWVIDEO },
00926 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
00927 };
00928
00929
00930 static const MXFCodecUL mxf_intra_only_essence_container_uls[] = {
00931 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x00,0x00 }, 14, CODEC_ID_MPEG2VIDEO },
00932 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
00933 };
00934
00935
00936 static const MXFCodecUL mxf_intra_only_picture_essence_coding_uls[] = {
00937 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, CODEC_ID_H264 },
00938 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
00939 };
00940
00941 static const MXFCodecUL mxf_sound_essence_container_uls[] = {
00942
00943 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE },
00944 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, CODEC_ID_MP2 },
00945 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, CODEC_ID_PCM_S16LE },
00946 { { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0xFF,0x4B,0x46,0x41,0x41,0x00,0x0D,0x4D,0x4F }, 14, CODEC_ID_PCM_S16LE },
00947 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
00948 };
00949
00950 static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
00951 {
00952 int i, j, nb_segments = 0;
00953 MXFIndexTableSegment **unsorted_segments;
00954 int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
00955 uint64_t last_index_duration = 0;
00956
00957
00958 for (i = 0; i < mxf->metadata_sets_count; i++)
00959 if (mxf->metadata_sets[i]->type == IndexTableSegment)
00960 nb_segments++;
00961
00962 if (!(unsorted_segments = av_calloc(nb_segments, sizeof(*unsorted_segments))) ||
00963 !(*sorted_segments = av_calloc(nb_segments, sizeof(**sorted_segments)))) {
00964 av_freep(sorted_segments);
00965 av_free(unsorted_segments);
00966 return AVERROR(ENOMEM);
00967 }
00968
00969 for (i = j = 0; i < mxf->metadata_sets_count; i++)
00970 if (mxf->metadata_sets[i]->type == IndexTableSegment)
00971 unsorted_segments[j++] = (MXFIndexTableSegment*)mxf->metadata_sets[i];
00972
00973 *nb_sorted_segments = 0;
00974
00975
00976 for (i = 0; i < nb_segments; i++) {
00977 int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
00978 uint64_t best_index_duration = 0;
00979
00980 for (j = 0; j < nb_segments; j++) {
00981 MXFIndexTableSegment *s = unsorted_segments[j];
00982
00983
00984
00985
00986
00987 if ((i == 0 || s->body_sid > last_body_sid || s->index_sid > last_index_sid || s->index_start_position > last_index_start) &&
00988 (best == -1 || s->body_sid < best_body_sid || s->index_sid < best_index_sid || s->index_start_position < best_index_start ||
00989 (s->index_start_position == best_index_start && s->index_duration > best_index_duration))) {
00990 best = j;
00991 best_body_sid = s->body_sid;
00992 best_index_sid = s->index_sid;
00993 best_index_start = s->index_start_position;
00994 best_index_duration = s->index_duration;
00995 }
00996 }
00997
00998
00999 if (best == -1)
01000 break;
01001
01002 (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
01003 last_body_sid = best_body_sid;
01004 last_index_sid = best_index_sid;
01005 last_index_start = best_index_start;
01006 last_index_duration = best_index_duration;
01007 }
01008
01009 av_free(unsorted_segments);
01010
01011 return 0;
01012 }
01013
01017 static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t offset, int64_t *offset_out)
01018 {
01019 int x;
01020 int64_t offset_in = offset;
01021
01022 for (x = 0; x < mxf->partitions_count; x++) {
01023 MXFPartition *p = &mxf->partitions[x];
01024
01025 if (p->body_sid != body_sid)
01026 continue;
01027
01028 if (offset < p->essence_length || !p->essence_length) {
01029 *offset_out = p->essence_offset + offset;
01030 return 0;
01031 }
01032
01033 offset -= p->essence_length;
01034 }
01035
01036 av_log(mxf->fc, AV_LOG_ERROR,
01037 "failed to find absolute offset of %"PRIX64" in BodySID %i - partial file?\n",
01038 offset_in, body_sid);
01039
01040 return AVERROR_INVALIDDATA;
01041 }
01042
01046 static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid)
01047 {
01048 int x;
01049 int64_t ret = 0;
01050
01051 for (x = 0; x < mxf->partitions_count; x++) {
01052 MXFPartition *p = &mxf->partitions[x];
01053
01054 if (p->body_sid != body_sid)
01055 continue;
01056
01057 if (!p->essence_length)
01058 return 0;
01059
01060 ret = p->essence_offset + p->essence_length;
01061 }
01062
01063 return ret;
01064 }
01065
01066
01067 static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_table, int64_t edit_unit, int64_t *edit_unit_out, int64_t *offset_out, int nag)
01068 {
01069 int i;
01070 int64_t offset_temp = 0;
01071
01072 for (i = 0; i < index_table->nb_segments; i++) {
01073 MXFIndexTableSegment *s = index_table->segments[i];
01074
01075 edit_unit = FFMAX(edit_unit, s->index_start_position);
01076
01077 if (edit_unit < s->index_start_position + s->index_duration) {
01078 int64_t index = edit_unit - s->index_start_position;
01079
01080 if (s->edit_unit_byte_count)
01081 offset_temp += s->edit_unit_byte_count * index;
01082 else if (s->nb_index_entries) {
01083 if (s->nb_index_entries == 2 * s->index_duration + 1)
01084 index *= 2;
01085
01086 if (index < 0 || index >= s->nb_index_entries) {
01087 av_log(mxf->fc, AV_LOG_ERROR, "IndexSID %i segment at %"PRId64" IndexEntryArray too small\n",
01088 index_table->index_sid, s->index_start_position);
01089 return AVERROR_INVALIDDATA;
01090 }
01091
01092 offset_temp = s->stream_offset_entries[index];
01093 } else {
01094 av_log(mxf->fc, AV_LOG_ERROR, "IndexSID %i segment at %"PRId64" missing EditUnitByteCount and IndexEntryArray\n",
01095 index_table->index_sid, s->index_start_position);
01096 return AVERROR_INVALIDDATA;
01097 }
01098
01099 if (edit_unit_out)
01100 *edit_unit_out = edit_unit;
01101
01102 return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, offset_temp, offset_out);
01103 } else {
01104
01105 offset_temp += s->edit_unit_byte_count * s->index_duration;
01106 }
01107 }
01108
01109 if (nag)
01110 av_log(mxf->fc, AV_LOG_ERROR, "failed to map EditUnit %"PRId64" in IndexSID %i to an offset\n", edit_unit, index_table->index_sid);
01111
01112 return AVERROR_INVALIDDATA;
01113 }
01114
01115 static int mxf_compute_ptses_fake_index(MXFContext *mxf, MXFIndexTable *index_table)
01116 {
01117 int i, j, x;
01118 int8_t max_temporal_offset = -128;
01119
01120
01121 for (i = 0; i < index_table->nb_segments; i++) {
01122 MXFIndexTableSegment *s = index_table->segments[i];
01123
01124 if (!s->nb_index_entries) {
01125 index_table->nb_ptses = 0;
01126 return 0;
01127 }
01128
01129 index_table->nb_ptses += s->index_duration;
01130 }
01131
01132
01133 if (index_table->nb_ptses <= 0)
01134 return 0;
01135
01136 if (!(index_table->ptses = av_calloc(index_table->nb_ptses, sizeof(int64_t))) ||
01137 !(index_table->fake_index = av_calloc(index_table->nb_ptses, sizeof(AVIndexEntry)))) {
01138 av_freep(&index_table->ptses);
01139 return AVERROR(ENOMEM);
01140 }
01141
01142
01143
01144 for (x = 0; x < index_table->nb_ptses; x++)
01145 index_table->ptses[x] = AV_NOPTS_VALUE;
01146
01174 for (i = x = 0; i < index_table->nb_segments; i++) {
01175 MXFIndexTableSegment *s = index_table->segments[i];
01176 int index_delta = 1;
01177 int n = s->nb_index_entries;
01178
01179 if (s->nb_index_entries == 2 * s->index_duration + 1) {
01180 index_delta = 2;
01181
01182 n--;
01183 }
01184
01185 for (j = 0; j < n; j += index_delta, x++) {
01186 int offset = s->temporal_offset_entries[j] / index_delta;
01187 int index = x + offset;
01188
01189 if (x >= index_table->nb_ptses) {
01190 av_log(mxf->fc, AV_LOG_ERROR,
01191 "x >= nb_ptses - IndexEntryCount %i < IndexDuration %"PRId64"?\n",
01192 s->nb_index_entries, s->index_duration);
01193 break;
01194 }
01195
01196 index_table->fake_index[x].timestamp = x;
01197 index_table->fake_index[x].flags = !(s->flag_entries[j] & 0x30) ? AVINDEX_KEYFRAME : 0;
01198
01199 if (index < 0 || index >= index_table->nb_ptses) {
01200 av_log(mxf->fc, AV_LOG_ERROR,
01201 "index entry %i + TemporalOffset %i = %i, which is out of bounds\n",
01202 x, offset, index);
01203 continue;
01204 }
01205
01206 index_table->ptses[index] = x;
01207 max_temporal_offset = FFMAX(max_temporal_offset, offset);
01208 }
01209 }
01210
01211 index_table->first_dts = -max_temporal_offset;
01212
01213 return 0;
01214 }
01215
01220 static int mxf_compute_index_tables(MXFContext *mxf)
01221 {
01222 int i, j, k, ret, nb_sorted_segments;
01223 MXFIndexTableSegment **sorted_segments = NULL;
01224
01225 if ((ret = mxf_get_sorted_table_segments(mxf, &nb_sorted_segments, &sorted_segments)) ||
01226 nb_sorted_segments <= 0) {
01227 av_log(mxf->fc, AV_LOG_WARNING, "broken or empty index\n");
01228 return 0;
01229 }
01230
01231
01232 for (i = 0; i < nb_sorted_segments; i++) {
01233 if (i == 0 || sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid)
01234 mxf->nb_index_tables++;
01235 else if (sorted_segments[i-1]->body_sid != sorted_segments[i]->body_sid) {
01236 av_log(mxf->fc, AV_LOG_ERROR, "found inconsistent BodySID\n");
01237 ret = AVERROR_INVALIDDATA;
01238 goto finish_decoding_index;
01239 }
01240 }
01241
01242 if (!(mxf->index_tables = av_calloc(mxf->nb_index_tables, sizeof(MXFIndexTable)))) {
01243 av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate index tables\n");
01244 ret = AVERROR(ENOMEM);
01245 goto finish_decoding_index;
01246 }
01247
01248
01249 for (i = j = 0; i < nb_sorted_segments; i++) {
01250 if (i != 0 && sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid) {
01251
01252 j++;
01253 }
01254
01255 mxf->index_tables[j].nb_segments++;
01256 }
01257
01258 for (i = j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) {
01259 MXFIndexTable *t = &mxf->index_tables[j];
01260
01261 if (!(t->segments = av_calloc(t->nb_segments, sizeof(MXFIndexTableSegment*)))) {
01262 av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate IndexTableSegment pointer array\n");
01263 ret = AVERROR(ENOMEM);
01264 goto finish_decoding_index;
01265 }
01266
01267 if (sorted_segments[i]->index_start_position)
01268 av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i starts at EditUnit %"PRId64" - seeking may not work as expected\n",
01269 sorted_segments[i]->index_sid, sorted_segments[i]->index_start_position);
01270
01271 memcpy(t->segments, &sorted_segments[i], t->nb_segments * sizeof(MXFIndexTableSegment*));
01272 t->index_sid = sorted_segments[i]->index_sid;
01273 t->body_sid = sorted_segments[i]->body_sid;
01274
01275 if ((ret = mxf_compute_ptses_fake_index(mxf, t)) < 0)
01276 goto finish_decoding_index;
01277
01278
01279 for (k = 0; k < t->nb_segments; k++) {
01280 if (t->segments[k]->index_duration)
01281 continue;
01282
01283 if (t->nb_segments > 1)
01284 av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has zero IndexDuration and there's more than one segment\n",
01285 t->index_sid, k);
01286
01287 if (mxf->fc->nb_streams <= 0) {
01288 av_log(mxf->fc, AV_LOG_WARNING, "no streams?\n");
01289 break;
01290 }
01291
01292
01293
01294
01295 t->segments[k]->index_duration = mxf->fc->streams[0]->duration;
01296 break;
01297 }
01298 }
01299
01300 ret = 0;
01301 finish_decoding_index:
01302 av_free(sorted_segments);
01303 return ret;
01304 }
01305
01306 static int mxf_is_intra_only(MXFDescriptor *descriptor)
01307 {
01308 return mxf_get_codec_ul(mxf_intra_only_essence_container_uls,
01309 &descriptor->essence_container_ul)->id != CODEC_ID_NONE ||
01310 mxf_get_codec_ul(mxf_intra_only_picture_essence_coding_uls,
01311 &descriptor->essence_codec_ul)->id != CODEC_ID_NONE;
01312 }
01313
01314 static int mxf_add_timecode_metadata(AVDictionary **pm, const char *key, AVTimecode *tc)
01315 {
01316 char buf[AV_TIMECODE_STR_SIZE];
01317 av_dict_set(pm, key, av_timecode_make_string(tc, buf, 0), 0);
01318
01319 return 0;
01320 }
01321
01322 static int mxf_parse_structural_metadata(MXFContext *mxf)
01323 {
01324 MXFPackage *material_package = NULL;
01325 MXFPackage *temp_package = NULL;
01326 int i, j, k, ret;
01327
01328 av_dlog(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
01329
01330 for (i = 0; i < mxf->packages_count; i++) {
01331 material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
01332 if (material_package) break;
01333 }
01334 if (!material_package) {
01335 av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
01336 return AVERROR_INVALIDDATA;
01337 }
01338
01339 for (i = 0; i < material_package->tracks_count; i++) {
01340 MXFPackage *source_package = NULL;
01341 MXFTrack *material_track = NULL;
01342 MXFTrack *source_track = NULL;
01343 MXFTrack *temp_track = NULL;
01344 MXFDescriptor *descriptor = NULL;
01345 MXFStructuralComponent *component = NULL;
01346 MXFTimecodeComponent *mxf_tc = NULL;
01347 UID *essence_container_ul = NULL;
01348 const MXFCodecUL *codec_ul = NULL;
01349 const MXFCodecUL *container_ul = NULL;
01350 const MXFCodecUL *pix_fmt_ul = NULL;
01351 AVStream *st;
01352 AVTimecode tc;
01353 int flags;
01354
01355 if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
01356 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
01357 continue;
01358 }
01359
01360 if ((component = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, TimecodeComponent))) {
01361 mxf_tc = (MXFTimecodeComponent*)component;
01362 flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
01363 if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
01364 mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
01365 }
01366 }
01367
01368 if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
01369 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
01370 continue;
01371 }
01372
01373 for (j = 0; j < material_track->sequence->structural_components_count; j++) {
01374 component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], TimecodeComponent);
01375 if (!component)
01376 continue;
01377
01378 mxf_tc = (MXFTimecodeComponent*)component;
01379 flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
01380 if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
01381 mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
01382 break;
01383 }
01384 }
01385
01386
01387 for (j = 0; j < material_track->sequence->structural_components_count; j++) {
01388 component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
01389 if (!component)
01390 continue;
01391
01392 for (k = 0; k < mxf->packages_count; k++) {
01393 temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
01394 if (!temp_package)
01395 continue;
01396 if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
01397 source_package = temp_package;
01398 break;
01399 }
01400 }
01401 if (!source_package) {
01402 av_dlog(mxf->fc, "material track %d: no corresponding source package found\n", material_track->track_id);
01403 break;
01404 }
01405 for (k = 0; k < source_package->tracks_count; k++) {
01406 if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
01407 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
01408 ret = AVERROR_INVALIDDATA;
01409 goto fail_and_free;
01410 }
01411 if (temp_track->track_id == component->source_track_id) {
01412 source_track = temp_track;
01413 break;
01414 }
01415 }
01416 if (!source_track) {
01417 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
01418 break;
01419 }
01420 }
01421 if (!source_track || !component)
01422 continue;
01423
01424 if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
01425 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
01426 ret = AVERROR_INVALIDDATA;
01427 goto fail_and_free;
01428 }
01429
01430
01431
01432 if (memcmp(material_track->sequence->data_definition_ul, source_track->sequence->data_definition_ul, 16)) {
01433 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: DataDefinition mismatch\n", material_track->track_id);
01434 continue;
01435 }
01436
01437 st = avformat_new_stream(mxf->fc, NULL);
01438 if (!st) {
01439 av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
01440 ret = AVERROR(ENOMEM);
01441 goto fail_and_free;
01442 }
01443 st->id = source_track->track_id;
01444 st->priv_data = source_track;
01445 st->duration = component->duration;
01446 if (st->duration == -1)
01447 st->duration = AV_NOPTS_VALUE;
01448 st->start_time = component->start_position;
01449 avpriv_set_pts_info(st, 64, material_track->edit_rate.den, material_track->edit_rate.num);
01450
01451 PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
01452 codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
01453 st->codec->codec_type = codec_ul->id;
01454
01455 source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
01456 if (source_package->descriptor) {
01457 if (source_package->descriptor->type == MultipleDescriptor) {
01458 for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
01459 MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
01460
01461 if (!sub_descriptor) {
01462 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
01463 continue;
01464 }
01465 if (sub_descriptor->linked_track_id == source_track->track_id) {
01466 descriptor = sub_descriptor;
01467 break;
01468 }
01469 }
01470 } else if (source_package->descriptor->type == Descriptor)
01471 descriptor = source_package->descriptor;
01472 }
01473 if (!descriptor) {
01474 av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
01475 continue;
01476 }
01477 PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
01478 PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
01479 essence_container_ul = &descriptor->essence_container_ul;
01480
01481
01482 if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
01483 av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
01484 for (k = 0; k < mxf->metadata_sets_count; k++) {
01485 MXFMetadataSet *metadata = mxf->metadata_sets[k];
01486 if (metadata->type == CryptoContext) {
01487 essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
01488 break;
01489 }
01490 }
01491 }
01492
01493
01494 codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
01495 st->codec->codec_id = codec_ul->id;
01496 if (descriptor->extradata) {
01497 st->codec->extradata = descriptor->extradata;
01498 st->codec->extradata_size = descriptor->extradata_size;
01499 }
01500 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01501 source_track->intra_only = mxf_is_intra_only(descriptor);
01502 container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul);
01503 if (st->codec->codec_id == CODEC_ID_NONE)
01504 st->codec->codec_id = container_ul->id;
01505 st->codec->width = descriptor->width;
01506 st->codec->height = descriptor->height;
01507 switch (descriptor->frame_layout) {
01508 case SegmentedFrame:
01509
01510 av_log(mxf->fc, AV_LOG_INFO, "SegmentedFrame layout isn't currently supported\n");
01511 break;
01512 case FullFrame:
01513 break;
01514 case OneField:
01515
01516 av_log(mxf->fc, AV_LOG_INFO, "OneField frame layout isn't currently supported\n");
01517 break;
01518
01519
01520 case MixedFields:
01521 break;
01522 case SeparateFields:
01523 st->codec->height *= 2;
01524 break;
01525 default:
01526 av_log(mxf->fc, AV_LOG_INFO, "Unknown frame layout type: %d\n", descriptor->frame_layout);
01527 }
01528 if (st->codec->codec_id == CODEC_ID_RAWVIDEO) {
01529 st->codec->pix_fmt = descriptor->pix_fmt;
01530 if (st->codec->pix_fmt == PIX_FMT_NONE) {
01531 pix_fmt_ul = mxf_get_codec_ul(ff_mxf_pixel_format_uls, &descriptor->essence_codec_ul);
01532 st->codec->pix_fmt = pix_fmt_ul->id;
01533 if (st->codec->pix_fmt == PIX_FMT_NONE) {
01534
01535
01536 if (descriptor->horiz_subsampling == 2 &&
01537 descriptor->vert_subsampling == 1 &&
01538 descriptor->component_depth == 8) {
01539 st->codec->pix_fmt = PIX_FMT_UYVY422;
01540 }
01541 }
01542 }
01543 }
01544 st->need_parsing = AVSTREAM_PARSE_HEADERS;
01545 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
01546 container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
01547 if (st->codec->codec_id == CODEC_ID_NONE)
01548 st->codec->codec_id = container_ul->id;
01549 st->codec->channels = descriptor->channels;
01550 st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
01551
01552 if (descriptor->sample_rate.den > 0)
01553 st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
01554
01555
01556 if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
01557 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
01558 st->codec->codec_id = CODEC_ID_PCM_S24LE;
01559 else if (descriptor->bits_per_sample == 32)
01560 st->codec->codec_id = CODEC_ID_PCM_S32LE;
01561 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
01562 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
01563 st->codec->codec_id = CODEC_ID_PCM_S24BE;
01564 else if (descriptor->bits_per_sample == 32)
01565 st->codec->codec_id = CODEC_ID_PCM_S32BE;
01566 } else if (st->codec->codec_id == CODEC_ID_MP2) {
01567 st->need_parsing = AVSTREAM_PARSE_FULL;
01568 }
01569 }
01570 if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
01571
01572 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
01573 }
01574 }
01575
01576 ret = 0;
01577 fail_and_free:
01578 return ret;
01579 }
01580
01581 static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
01582 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
01583 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
01584 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack },
01585 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack },
01586 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack },
01587 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack },
01588 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack },
01589 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack },
01590 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
01591 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
01592 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
01593 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
01594 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
01595 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
01596 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
01597 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
01598 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
01599 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01600 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01601 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01602 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01603 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01604 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor },
01605 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track },
01606 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track },
01607 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x14,0x00 }, mxf_read_timecode_component, sizeof(MXFTimecodeComponent), TimecodeComponent },
01608 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
01609 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
01610 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
01611 };
01612
01613 static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
01614 {
01615 AVIOContext *pb = mxf->fc->pb;
01616 MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
01617 uint64_t klv_end = avio_tell(pb) + klv->length;
01618
01619 if (!ctx)
01620 return AVERROR(ENOMEM);
01621 while (avio_tell(pb) + 4 < klv_end && !url_feof(pb)) {
01622 int ret;
01623 int tag = avio_rb16(pb);
01624 int size = avio_rb16(pb);
01625 uint64_t next = avio_tell(pb) + size;
01626 UID uid = {0};
01627
01628 av_dlog(mxf->fc, "local tag %#04x size %d\n", tag, size);
01629 if (!size) {
01630 av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
01631 continue;
01632 }
01633 if (tag > 0x7FFF) {
01634 int i;
01635 for (i = 0; i < mxf->local_tags_count; i++) {
01636 int local_tag = AV_RB16(mxf->local_tags+i*18);
01637 if (local_tag == tag) {
01638 memcpy(uid, mxf->local_tags+i*18+2, 16);
01639 av_dlog(mxf->fc, "local tag %#04x\n", local_tag);
01640 PRINT_KEY(mxf->fc, "uid", uid);
01641 }
01642 }
01643 }
01644 if (ctx_size && tag == 0x3C0A)
01645 avio_read(pb, ctx->uid, 16);
01646 else if ((ret = read_child(ctx, pb, tag, size, uid, -1)) < 0)
01647 return ret;
01648
01649
01650
01651 if (avio_tell(pb) > klv_end) {
01652 av_log(mxf->fc, AV_LOG_ERROR,
01653 "local tag %#04x extends past end of local set @ %#"PRIx64"\n",
01654 tag, klv->offset);
01655 return AVERROR_INVALIDDATA;
01656 } else if (avio_tell(pb) <= next)
01657 avio_seek(pb, next, SEEK_SET);
01658 }
01659 if (ctx_size) ctx->type = type;
01660 return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
01661 }
01662
01667 static int mxf_seek_to_previous_partition(MXFContext *mxf)
01668 {
01669 AVIOContext *pb = mxf->fc->pb;
01670
01671 if (!mxf->current_partition ||
01672 mxf->run_in + mxf->current_partition->previous_partition <= mxf->last_forward_tell)
01673 return 0;
01674
01675
01676 avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET);
01677 mxf->current_partition = NULL;
01678
01679 av_dlog(mxf->fc, "seeking to previous partition\n");
01680
01681 return 1;
01682 }
01683
01688 static int mxf_parse_handle_essence(MXFContext *mxf)
01689 {
01690 AVIOContext *pb = mxf->fc->pb;
01691 int64_t ret;
01692
01693 if (mxf->parsing_backward) {
01694 return mxf_seek_to_previous_partition(mxf);
01695 } else {
01696 if (!mxf->footer_partition) {
01697 av_dlog(mxf->fc, "no footer\n");
01698 return 0;
01699 }
01700
01701 av_dlog(mxf->fc, "seeking to footer\n");
01702
01703
01704 mxf->last_forward_tell = avio_tell(pb);
01705
01706 if (!pb->seekable) {
01707 av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing footer\n");
01708 return -1;
01709 }
01710
01711
01712 if ((ret = avio_seek(pb, mxf->run_in + mxf->footer_partition, SEEK_SET)) < 0) {
01713 av_log(mxf->fc, AV_LOG_ERROR, "failed to seek to footer @ 0x%"PRIx64" (%"PRId64") - partial file?\n",
01714 mxf->run_in + mxf->footer_partition, ret);
01715 return ret;
01716 }
01717
01718 mxf->current_partition = NULL;
01719 mxf->parsing_backward = 1;
01720 }
01721
01722 return 1;
01723 }
01724
01729 static int mxf_parse_handle_partition_or_eof(MXFContext *mxf)
01730 {
01731 return mxf->parsing_backward ? mxf_seek_to_previous_partition(mxf) : 1;
01732 }
01733
01737 static void mxf_compute_essence_containers(MXFContext *mxf)
01738 {
01739 int x;
01740
01741
01742 if (mxf->op == OPAtom)
01743 return;
01744
01745 for (x = 0; x < mxf->partitions_count; x++) {
01746 MXFPartition *p = &mxf->partitions[x];
01747
01748 if (!p->body_sid)
01749 continue;
01750
01751 if (x >= mxf->partitions_count - 1)
01752 break;
01753
01754
01755 p->essence_length = mxf->partitions[x+1].this_partition - p->essence_offset;
01756
01757 if (p->essence_length < 0) {
01758
01759 p->essence_length = 0;
01760 av_log(mxf->fc, AV_LOG_ERROR,
01761 "partition %i: bad ThisPartition = %"PRIX64"\n",
01762 x+1, mxf->partitions[x+1].this_partition);
01763 }
01764 }
01765 }
01766
01767 static int64_t round_to_kag(int64_t position, int kag_size)
01768 {
01769
01770
01771 int64_t ret = (position / kag_size) * kag_size;
01772 return ret == position ? ret : ret + kag_size;
01773 }
01774
01775 static int is_pcm(enum CodecID codec_id)
01776 {
01777
01778 return codec_id >= CODEC_ID_PCM_S16LE && codec_id < CODEC_ID_PCM_S24DAUD;
01779 }
01780
01786 static void mxf_handle_small_eubc(AVFormatContext *s)
01787 {
01788 MXFContext *mxf = s->priv_data;
01789
01790
01791
01792 if (mxf->op != OPAtom)
01793 return;
01794
01795
01796 if (s->nb_streams != 1 ||
01797 s->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
01798 !is_pcm(s->streams[0]->codec->codec_id) ||
01799 mxf->nb_index_tables != 1 ||
01800 mxf->index_tables[0].nb_segments != 1 ||
01801 mxf->index_tables[0].segments[0]->edit_unit_byte_count >= 32)
01802 return;
01803
01804
01805
01806
01807
01808 mxf->edit_units_per_packet = 1920;
01809 }
01810
01811 static int mxf_read_header(AVFormatContext *s)
01812 {
01813 MXFContext *mxf = s->priv_data;
01814 KLVPacket klv;
01815 int64_t essence_offset = 0;
01816 int ret;
01817
01818 mxf->last_forward_tell = INT64_MAX;
01819 mxf->edit_units_per_packet = 1;
01820
01821 if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
01822 av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
01823 return AVERROR_INVALIDDATA;
01824 }
01825 avio_seek(s->pb, -14, SEEK_CUR);
01826 mxf->fc = s;
01827 mxf->run_in = avio_tell(s->pb);
01828
01829 while (!url_feof(s->pb)) {
01830 const MXFMetadataReadTableEntry *metadata;
01831
01832 if (klv_read_packet(&klv, s->pb) < 0) {
01833
01834 if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
01835 break;
01836 else
01837 continue;
01838 }
01839
01840 PRINT_KEY(s, "read header", klv.key);
01841 av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
01842 if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
01843 IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
01844 IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) ||
01845 IS_KLV_KEY(klv.key, mxf_system_item_key)) {
01846
01847 if (!mxf->current_partition) {
01848 av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to first PartitionPack\n");
01849 return AVERROR_INVALIDDATA;
01850 }
01851
01852 if (!mxf->current_partition->essence_offset) {
01853
01854
01855
01856
01857
01858 int64_t op1a_essence_offset =
01859 round_to_kag(mxf->current_partition->this_partition +
01860 mxf->current_partition->pack_length, mxf->current_partition->kag_size) +
01861 round_to_kag(mxf->current_partition->header_byte_count, mxf->current_partition->kag_size) +
01862 round_to_kag(mxf->current_partition->index_byte_count, mxf->current_partition->kag_size);
01863
01864 if (mxf->op == OPAtom) {
01865
01866
01867
01868 mxf->current_partition->essence_offset = avio_tell(s->pb);
01869 mxf->current_partition->essence_length = klv.length;
01870 } else {
01871
01872 mxf->current_partition->essence_offset = op1a_essence_offset;
01873 }
01874 }
01875
01876 if (!essence_offset)
01877 essence_offset = klv.offset;
01878
01879
01880 if (mxf_parse_handle_essence(mxf) <= 0)
01881 break;
01882 continue;
01883 } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) &&
01884 klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) {
01885
01886 if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
01887 break;
01888 else if (mxf->parsing_backward)
01889 continue;
01890
01891 }
01892
01893 for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
01894 if (IS_KLV_KEY(klv.key, metadata->key)) {
01895 int res;
01896 if (klv.key[5] == 0x53) {
01897 res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);
01898 } else {
01899 uint64_t next = avio_tell(s->pb) + klv.length;
01900 res = metadata->read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);
01901
01902
01903 if (avio_tell(s->pb) > next) {
01904 av_log(s, AV_LOG_ERROR, "read past end of KLV @ %#"PRIx64"\n",
01905 klv.offset);
01906 return AVERROR_INVALIDDATA;
01907 }
01908
01909 avio_seek(s->pb, next, SEEK_SET);
01910 }
01911 if (res < 0) {
01912 av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
01913 return res;
01914 }
01915 break;
01916 }
01917 }
01918 if (!metadata->read)
01919 avio_skip(s->pb, klv.length);
01920 }
01921
01922 if (!essence_offset) {
01923 av_log(s, AV_LOG_ERROR, "no essence\n");
01924 return AVERROR_INVALIDDATA;
01925 }
01926 avio_seek(s->pb, essence_offset, SEEK_SET);
01927
01928 mxf_compute_essence_containers(mxf);
01929
01930
01931
01932 if ((ret = mxf_parse_structural_metadata(mxf)) < 0)
01933 return ret;
01934
01935 if ((ret = mxf_compute_index_tables(mxf)) < 0)
01936 return ret;
01937
01938 if (mxf->nb_index_tables > 1) {
01939
01940 av_log(mxf->fc, AV_LOG_INFO, "got %i index tables - only the first one (IndexSID %i) will be used\n",
01941 mxf->nb_index_tables, mxf->index_tables[0].index_sid);
01942 } else if (mxf->nb_index_tables == 0 && mxf->op == OPAtom) {
01943 av_log(mxf->fc, AV_LOG_ERROR, "cannot demux OPAtom without an index\n");
01944 return AVERROR_INVALIDDATA;
01945 }
01946
01947 mxf_handle_small_eubc(s);
01948
01949 return 0;
01950 }
01951
01956 static int64_t mxf_set_current_edit_unit(MXFContext *mxf, int64_t current_offset)
01957 {
01958 int64_t last_ofs = -1, next_ofs = -1;
01959 MXFIndexTable *t = &mxf->index_tables[0];
01960
01961
01962
01963 if (mxf->nb_index_tables <= 0)
01964 return -1;
01965
01966
01967 while (mxf->current_edit_unit >= 0) {
01968 if (mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit + 1, NULL, &next_ofs, 0) < 0)
01969 return -1;
01970
01971 if (next_ofs <= last_ofs) {
01972
01973
01974 av_log(mxf->fc, AV_LOG_ERROR,
01975 "next_ofs didn't change. not deriving packet timestamps\n");
01976 return - 1;
01977 }
01978
01979 if (next_ofs > current_offset)
01980 break;
01981
01982 last_ofs = next_ofs;
01983 mxf->current_edit_unit++;
01984 }
01985
01986
01987 if (mxf->current_edit_unit < 0)
01988 return -1;
01989
01990 return next_ofs;
01991 }
01992
01993 static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt)
01994 {
01995 KLVPacket klv;
01996 MXFContext *mxf = s->priv_data;
01997
01998 while (!url_feof(s->pb)) {
01999 int ret;
02000 if (klv_read_packet(&klv, s->pb) < 0)
02001 return -1;
02002 PRINT_KEY(s, "read packet", klv.key);
02003 av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
02004 if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
02005 ret = mxf_decrypt_triplet(s, pkt, &klv);
02006 if (ret < 0) {
02007 av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
02008 return AVERROR_INVALIDDATA;
02009 }
02010 return 0;
02011 }
02012 if (IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
02013 IS_KLV_KEY(klv.key, mxf_avid_essence_element_key)) {
02014 int index = mxf_get_stream_index(s, &klv);
02015 int64_t next_ofs, next_klv;
02016 AVStream *st;
02017 MXFTrack *track;
02018
02019 if (index < 0) {
02020 av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12));
02021 goto skip;
02022 }
02023
02024 st = s->streams[index];
02025 track = st->priv_data;
02026
02027 if (s->streams[index]->discard == AVDISCARD_ALL)
02028 goto skip;
02029
02030 next_klv = avio_tell(s->pb) + klv.length;
02031 next_ofs = mxf_set_current_edit_unit(mxf, klv.offset);
02032
02033 if (next_ofs >= 0 && next_klv > next_ofs) {
02034
02035
02036 av_log_ask_for_sample(s,
02037 "KLV for edit unit %i extends into next edit unit - OPAtom misinterpreted as OP1a?\n",
02038 mxf->current_edit_unit);
02039 klv.length = next_ofs - avio_tell(s->pb);
02040 }
02041
02042
02043 if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
02044 if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
02045 av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
02046 return AVERROR_INVALIDDATA;
02047 }
02048 } else {
02049 ret = av_get_packet(s->pb, pkt, klv.length);
02050 if (ret < 0)
02051 return ret;
02052 }
02053 pkt->stream_index = index;
02054 pkt->pos = klv.offset;
02055
02056 if (s->streams[index]->codec->codec_type == AVMEDIA_TYPE_VIDEO && next_ofs >= 0) {
02057
02058 MXFIndexTable *t = &mxf->index_tables[0];
02059
02060 if (mxf->nb_index_tables >= 1 && mxf->current_edit_unit < t->nb_ptses) {
02061 pkt->dts = mxf->current_edit_unit + t->first_dts;
02062 pkt->pts = t->ptses[mxf->current_edit_unit];
02063 } else if (track->intra_only) {
02064
02065
02066 pkt->pts = mxf->current_edit_unit;
02067 }
02068 }
02069
02070
02071 avio_seek(s->pb, next_klv, SEEK_SET);
02072
02073 return 0;
02074 } else
02075 skip:
02076 avio_skip(s->pb, klv.length);
02077 }
02078 return AVERROR_EOF;
02079 }
02080
02081 static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
02082 {
02083 MXFContext *mxf = s->priv_data;
02084 int ret, size;
02085 int64_t ret64, pos, next_pos;
02086 AVStream *st;
02087 MXFIndexTable *t;
02088 int edit_units;
02089
02090 if (mxf->op != OPAtom)
02091 return mxf_read_packet_old(s, pkt);
02092
02093
02094
02095 st = s->streams[0];
02096 t = &mxf->index_tables[0];
02097
02098 if (mxf->current_edit_unit >= st->duration)
02099 return AVERROR_EOF;
02100
02101 edit_units = FFMIN(mxf->edit_units_per_packet, st->duration - mxf->current_edit_unit);
02102
02103 if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit, NULL, &pos, 1)) < 0)
02104 return ret;
02105
02106
02107
02108 if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit + edit_units, NULL, &next_pos, 0)) < 0 &&
02109 (next_pos = mxf_essence_container_end(mxf, t->body_sid)) <= 0) {
02110 av_log(s, AV_LOG_ERROR, "unable to compute the size of the last packet\n");
02111 return AVERROR_INVALIDDATA;
02112 }
02113
02114 if ((size = next_pos - pos) <= 0) {
02115 av_log(s, AV_LOG_ERROR, "bad size: %i\n", size);
02116 return AVERROR_INVALIDDATA;
02117 }
02118
02119 if ((ret64 = avio_seek(s->pb, pos, SEEK_SET)) < 0)
02120 return ret64;
02121
02122 if ((size = av_get_packet(s->pb, pkt, size)) < 0)
02123 return size;
02124
02125 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && t->ptses &&
02126 mxf->current_edit_unit >= 0 && mxf->current_edit_unit < t->nb_ptses) {
02127 pkt->dts = mxf->current_edit_unit + t->first_dts;
02128 pkt->pts = t->ptses[mxf->current_edit_unit];
02129 }
02130
02131 pkt->stream_index = 0;
02132 mxf->current_edit_unit += edit_units;
02133
02134 return 0;
02135 }
02136
02137 static int mxf_read_close(AVFormatContext *s)
02138 {
02139 MXFContext *mxf = s->priv_data;
02140 MXFIndexTableSegment *seg;
02141 int i;
02142
02143 av_freep(&mxf->packages_refs);
02144
02145 for (i = 0; i < s->nb_streams; i++)
02146 s->streams[i]->priv_data = NULL;
02147
02148 for (i = 0; i < mxf->metadata_sets_count; i++) {
02149 switch (mxf->metadata_sets[i]->type) {
02150 case MultipleDescriptor:
02151 av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
02152 break;
02153 case Sequence:
02154 av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
02155 break;
02156 case SourcePackage:
02157 case MaterialPackage:
02158 av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
02159 break;
02160 case IndexTableSegment:
02161 seg = (MXFIndexTableSegment *)mxf->metadata_sets[i];
02162 av_freep(&seg->temporal_offset_entries);
02163 av_freep(&seg->flag_entries);
02164 av_freep(&seg->stream_offset_entries);
02165 break;
02166 default:
02167 break;
02168 }
02169 av_freep(&mxf->metadata_sets[i]);
02170 }
02171 av_freep(&mxf->partitions);
02172 av_freep(&mxf->metadata_sets);
02173 av_freep(&mxf->aesc);
02174 av_freep(&mxf->local_tags);
02175
02176 for (i = 0; i < mxf->nb_index_tables; i++) {
02177 av_freep(&mxf->index_tables[i].segments);
02178 av_freep(&mxf->index_tables[i].ptses);
02179 av_freep(&mxf->index_tables[i].fake_index);
02180 }
02181 av_freep(&mxf->index_tables);
02182
02183 return 0;
02184 }
02185
02186 static int mxf_probe(AVProbeData *p) {
02187 uint8_t *bufp = p->buf;
02188 uint8_t *end = p->buf + p->buf_size;
02189
02190 if (p->buf_size < sizeof(mxf_header_partition_pack_key))
02191 return 0;
02192
02193
02194 end -= sizeof(mxf_header_partition_pack_key);
02195 for (; bufp < end; bufp++) {
02196 if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
02197 return AVPROBE_SCORE_MAX;
02198 }
02199 return 0;
02200 }
02201
02202
02203
02204 static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
02205 {
02206 AVStream *st = s->streams[stream_index];
02207 int64_t seconds;
02208 MXFContext* mxf = s->priv_data;
02209 int64_t seekpos;
02210 int ret;
02211 MXFIndexTable *t;
02212
02213 if (mxf->index_tables <= 0) {
02214 if (!s->bit_rate)
02215 return AVERROR_INVALIDDATA;
02216 if (sample_time < 0)
02217 sample_time = 0;
02218 seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
02219
02220 if ((ret = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET)) < 0)
02221 return ret;
02222 ff_update_cur_dts(s, st, sample_time);
02223 } else {
02224 t = &mxf->index_tables[0];
02225
02226
02227
02228 sample_time = FFMAX(sample_time, 0);
02229
02230 if (t->fake_index) {
02231
02232 if ((sample_time = ff_index_search_timestamp(t->fake_index, t->nb_ptses, sample_time, flags)) < 0)
02233 return sample_time;
02234 } else {
02235
02236
02237 sample_time = FFMIN(sample_time, st->duration - 1);
02238 }
02239
02240 if ((ret = mxf_edit_unit_absolute_offset(mxf, t, sample_time, &sample_time, &seekpos, 1)) << 0)
02241 return ret;
02242
02243 ff_update_cur_dts(s, st, sample_time);
02244 mxf->current_edit_unit = sample_time;
02245 avio_seek(s->pb, seekpos, SEEK_SET);
02246 }
02247 return 0;
02248 }
02249
02250 AVInputFormat ff_mxf_demuxer = {
02251 .name = "mxf",
02252 .long_name = NULL_IF_CONFIG_SMALL("Material eXchange Format"),
02253 .priv_data_size = sizeof(MXFContext),
02254 .read_probe = mxf_probe,
02255 .read_header = mxf_read_header,
02256 .read_packet = mxf_read_packet,
02257 .read_close = mxf_read_close,
02258 .read_seek = mxf_read_seek,
02259 };