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