60 #define MXF_MAX_CHUNK_SIZE (32 << 20) 
  194 #define MXF_FIELD_DOMINANCE_DEFAULT 0 
  195 #define MXF_FIELD_DOMINANCE_FF 1  
  196 #define MXF_FIELD_DOMINANCE_FL 2  
  308 static const uint8_t mxf_header_partition_pack_key[]       = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
 
  309 static const uint8_t mxf_essence_element_key[]             = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
 
  310 static const uint8_t mxf_avid_essence_element_key[]        = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 };
 
  311 static const uint8_t mxf_canopus_essence_element_key[]     = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x0a,0x0e,0x0f,0x03,0x01 };
 
  312 static const uint8_t mxf_system_item_key_cp[]              = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x03,0x01,0x04 };
 
  313 static const uint8_t mxf_system_item_key_gc[]              = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x03,0x01,0x14 };
 
  316 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 };
 
  317 static const uint8_t mxf_encrypted_triplet_key[]           = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
 
  318 static const uint8_t mxf_encrypted_essence_container[]     = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
 
  319 static const uint8_t mxf_random_index_pack_key[]           = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
 
  320 static const uint8_t mxf_sony_mpeg4_extradata[]            = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
 
  321 static const uint8_t mxf_avid_project_name[]               = { 0xa5,0xfb,0x7b,0x25,0xf6,0x15,0x94,0xb9,0x62,0xfc,0x37,0x17,0x49,0x2d,0x42,0xbf };
 
  322 static const uint8_t mxf_jp2k_rsiz[]                       = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 };
 
  323 static const uint8_t mxf_indirect_value_utf16le[]          = { 0x4c,0x00,0x02,0x10,0x01,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
 
  324 static const uint8_t mxf_indirect_value_utf16be[]          = { 0x42,0x01,0x10,0x02,0x00,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
 
  326 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y))) 
  331     switch ((*ctx)->type) {
 
  373         int bytes_num = 
size & 0x7f;
 
  381     if (
size > INT64_MAX)
 
  393         else if (
b != 
key[
i])
 
  412     if (
pos > INT64_MAX - length)
 
  422     for (
i = 0; 
i < 
s->nb_streams; 
i++) {
 
  429     return s->nb_streams == 1 && 
s->streams[0]->priv_data ? 0 : -1;
 
  458     int cdp_identifier, cdp_length, cdp_footer_id, ccdata_id, cc_count;
 
  459     int line_num, sample_coding, sample_count;
 
  460     int did, sdid, data_length;
 
  466     for (
i = 0; 
i < count; 
i++) {
 
  475         length -= 6 + 8 + sample_count;
 
  476         if (line_num != 9 && line_num != 11)
 
  478         if (sample_coding == 7 || sample_coding == 8 || sample_coding == 9) {
 
  490         if (did != 0x61 || sdid != 1) {
 
  495         if (cdp_identifier != 0x9669) {
 
  504         if (ccdata_id != 0x72) {
 
  512         if (cdp_length - 9 - 4 <  cc_count * 3) {
 
  516         avio_skip(
s->pb, data_length - 9 - 4 - cc_count * 3);
 
  518         if (cdp_footer_id != 0x74) {
 
  533     const uint8_t *buf_ptr, *end_ptr;
 
  551             uint32_t 
sample = bytestream_get_le32(&buf_ptr);
 
  553                 bytestream_put_le24(&data_ptr, (
sample >> 4) & 0xffffff);
 
  555                 bytestream_put_le16(&data_ptr, (
sample >> 12) & 0xffff);
 
  565     static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
 
  571     uint64_t plaintext_size;
 
  577     if (!mxf->
aesc && 
s->key && 
s->keylen == 16) {
 
  604     if (orig_size < plaintext_size)
 
  608     if (
size < 32 || 
size - 32 < orig_size || (
int)orig_size != orig_size)
 
  614     if (memcmp(tmpbuf, checkv, 16))
 
  620     else if (
size < plaintext_size)
 
  622     size -= plaintext_size;
 
  638     if (item_len != 18) {
 
  642     if (item_num > 65536 || item_num < 0) {
 
  664     uint64_t footer_partition;
 
  665     uint32_t nb_essence_containers;
 
  687     memset(partition, 0, 
sizeof(*partition));
 
  735                "PreviousPartition equal to ThisPartition %"PRIx64
"\n",
 
  748                "Overriding PreviousPartition with %"PRIx64
"\n",
 
  753     if (footer_partition) {
 
  756                    "inconsistent FooterPartition value: %"PRIu64
" != %"PRIu64
"\n",
 
  764             "PartitionPack: ThisPartition = 0x%"PRIX64
 
  765             ", PreviousPartition = 0x%"PRIX64
", " 
  766             "FooterPartition = 0x%"PRIX64
", IndexSID = %i, BodySID = %i\n",
 
  776                "PreviousPartition points to this partition or forward\n");
 
  780     if      (
op[12] == 1 && 
op[13] == 1) mxf->
op = 
OP1a;
 
  781     else if (
op[12] == 1 && 
op[13] == 2) mxf->
op = 
OP1b;
 
  782     else if (
op[12] == 1 && 
op[13] == 3) mxf->
op = 
OP1c;
 
  783     else if (
op[12] == 2 && 
op[13] == 1) mxf->
op = 
OP2a;
 
  784     else if (
op[12] == 2 && 
op[13] == 2) mxf->
op = 
OP2b;
 
  785     else if (
op[12] == 2 && 
op[13] == 3) mxf->
op = 
OP2c;
 
  786     else if (
op[12] == 3 && 
op[13] == 1) mxf->
op = 
OP3a;
 
  787     else if (
op[12] == 3 && 
op[13] == 2) mxf->
op = 
OP3b;
 
  788     else if (
op[12] == 3 && 
op[13] == 3) mxf->
op = 
OP3c;
 
  790     else if (
op[12] == 0x10) {
 
  795         if (nb_essence_containers != 1) {
 
  801                        "\"OPAtom\" with %"PRIu32
" ECs - assuming %s\n",
 
  802                        nb_essence_containers,
 
  803                        op == 
OP1a ? 
"OP1a" : 
"OPAtom");
 
  859     if (
c > INT_MAX / 
sizeof(
UID))
 
  871     if (
ret != *count * 
sizeof(
UID)) {
 
  872         *count = 
ret < 0 ? 0   : 
ret / 
sizeof(
UID);
 
  884     if (size < 0 || size > INT_MAX/2)
 
  906 #define READ_STR16(type, big_endian)                                               \ 
  907 static int mxf_read_utf16 ## type ##_string(AVIOContext *pb, int size, char** str) \ 
  909 return mxf_read_utf16_string(pb, size, str, big_endian);                           \ 
  990         mxf_read_utf16be_string(pb, 
size, &track->
name);
 
 1043                                              &package->tracks_count);
 
 1047         avio_read(pb, package->package_uid, 16);
 
 1050         avio_read(pb, package->descriptor_ref, 16);
 
 1053         return mxf_read_utf16be_string(pb, 
size, &package->name);
 
 1056                                              &package->comment_count);
 
 1084     if (
segment->temporal_offset_entries)
 
 1090     if(
segment->nb_index_entries && length < 11)
 
 1101     for (
i = 0; 
i < 
segment->nb_index_entries; 
i++) {
 
 1136                 segment->index_edit_rate.den);
 
 1165     } 
while (
code != 0); 
 
 1173     int entry_count, entry_size;
 
 1206         if (entry_size == 4) {
 
 1207             if (entry_count > 0)
 
 1211             if (entry_count > 1)
 
 1285         return mxf_read_utf16le_string(pb, 
size - 17, &tagged_value->
value);
 
 1287         return mxf_read_utf16be_string(pb, 
size - 17, &tagged_value->
value);
 
 1297         return mxf_read_utf16be_string(pb, 
size, &tagged_value->
name);
 
 1311     for (
i = 0; 
i < 
len; 
i++) {
 
 1320     while (uls->
uid[0]) {
 
 1345     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 }, 14,   
AV_CODEC_ID_JPEG2000, 
NULL, 14 },
 
 1346     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x10,0x60,0x01 }, 14,       
AV_CODEC_ID_H264, 
NULL, 15 }, 
 
 1347     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, 14,      
AV_CODEC_ID_DNXHD, 
NULL, 14 }, 
 
 1348     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x12,0x01,0x00 }, 14,        
AV_CODEC_ID_VC1, 
NULL, 14 }, 
 
 1349     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x14,0x01,0x00 }, 14,       
AV_CODEC_ID_TIFF, 
NULL, 14 }, 
 
 1350     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x15,0x01,0x00 }, 14,      
AV_CODEC_ID_DIRAC, 
NULL, 14 }, 
 
 1351     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x1b,0x01,0x00 }, 14,       
AV_CODEC_ID_CFHD, 
NULL, 14 }, 
 
 1352     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x1c,0x01,0x00 }, 14,     
AV_CODEC_ID_PRORES, 
NULL, 14 }, 
 
 1353     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, 
AV_CODEC_ID_MPEG2VIDEO, 
NULL, 15 }, 
 
 1354     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, 14, 
AV_CODEC_ID_MPEG2VIDEO, 
NULL, 15, 
D10D11Wrap }, 
 
 1355     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14,    
AV_CODEC_ID_DVVIDEO, 
NULL, 15 }, 
 
 1356     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14,   
AV_CODEC_ID_RAWVIDEO, 
NULL, 15, 
RawVWrap }, 
 
 1357     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x01,0x01 }, 15,     
AV_CODEC_ID_HQ_HQA },
 
 1358     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x02,0x01 }, 15,        
AV_CODEC_ID_HQX },
 
 1359     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x15,0x00,0x04,0x02,0x10,0x00,0x01 }, 16,       
AV_CODEC_ID_HEVC, 
NULL, 15 }, 
 
 1360     { { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0xff,0x4b,0x46,0x41,0x41,0x00,0x0d,0x4d,0x4f }, 14,   
AV_CODEC_ID_RAWVIDEO }, 
 
 1361     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,      
AV_CODEC_ID_NONE },
 
 1366     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x00,0x00 }, 14, 
AV_CODEC_ID_MPEG2VIDEO }, 
 
 1367     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,       
AV_CODEC_ID_NONE },
 
 1372     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14,       
AV_CODEC_ID_H264 }, 
 
 1373     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, 14,   
AV_CODEC_ID_JPEG2000 }, 
 
 1374     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,       
AV_CODEC_ID_NONE },
 
 1379     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 16, 1440 },
 
 1380     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 16, 1440 },
 
 1381     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 16, 1440 },
 
 1382     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 16, 1440 },
 
 1383     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,    0 },
 
 1388     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, 
AV_CODEC_ID_PCM_S16LE, 
NULL, 14, 
RawAWrap }, 
 
 1389     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14,       
AV_CODEC_ID_MP2, 
NULL, 15 }, 
 
 1390     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, 
AV_CODEC_ID_PCM_S16LE, 
NULL, 13 }, 
 
 1391     { { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0xff,0x4b,0x46,0x41,0x41,0x00,0x0d,0x4d,0x4F }, 14, 
AV_CODEC_ID_PCM_S16LE }, 
 
 1392     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x03,0x04,0x02,0x02,0x02,0x03,0x03,0x01,0x00 }, 14,       
AV_CODEC_ID_AAC }, 
 
 1393     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,      
AV_CODEC_ID_NONE },
 
 1397     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0d,0x00,0x00 }, 16, 
AV_CODEC_ID_NONE,      
"vbi_smpte_436M", 11 },
 
 1398     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 }, 16, 
AV_CODEC_ID_NONE, 
"vbi_vanc_smpte_436M", 11 },
 
 1399     { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x13,0x01,0x01 }, 16, 
AV_CODEC_ID_TTML },
 
 1400     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0, 
AV_CODEC_ID_NONE },
 
 1416     val = (*essence_container_ul)[
codec_ul->wrapping_indicator_pos];
 
 1417     switch (
codec_ul->wrapping_indicator_type) {
 
 1422             if (
val == 0x03 || 
val == 0x04)
 
 1439     int i, j, nb_segments = 0;
 
 1441     int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
 
 1451     if (!(unsorted_segments = 
av_calloc(nb_segments, 
sizeof(*unsorted_segments))) ||
 
 1452         !(*sorted_segments  = 
av_calloc(nb_segments, 
sizeof(**sorted_segments)))) {
 
 1461             if (
s->edit_unit_byte_count || 
s->nb_index_entries)
 
 1462                 unsorted_segments[nb_segments++] = 
s;
 
 1464                 av_log(mxf->
fc, 
AV_LOG_WARNING, 
"IndexSID %i segment at %"PRId64
" missing EditUnitByteCount and IndexEntryArray\n",
 
 1465                        s->index_sid, 
s->index_start_position);
 
 1475     *nb_sorted_segments = 0;
 
 1478     for (
i = 0; 
i < nb_segments; 
i++) {
 
 1479         int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
 
 1480         uint64_t best_index_duration = 0;
 
 1482         for (j = 0; j < nb_segments; j++) {
 
 1490                  s->body_sid >  last_body_sid ||
 
 1491                  s->body_sid == last_body_sid && 
s->index_sid >  last_index_sid ||
 
 1492                  s->body_sid == last_body_sid && 
s->index_sid == last_index_sid && 
s->index_start_position > last_index_start) &&
 
 1494                  s->body_sid <  best_body_sid ||
 
 1495                  s->body_sid == best_body_sid && 
s->index_sid <  best_index_sid ||
 
 1496                  s->body_sid == best_body_sid && 
s->index_sid == best_index_sid && 
s->index_start_position <  best_index_start ||
 
 1497                  s->body_sid == best_body_sid && 
s->index_sid == best_index_sid && 
s->index_start_position == best_index_start && 
s->index_duration > best_index_duration)) {
 
 1499                 best_body_sid    = 
s->body_sid;
 
 1500                 best_index_sid   = 
s->index_sid;
 
 1501                 best_index_start = 
s->index_start_position;
 
 1502                 best_index_duration = 
s->index_duration;
 
 1510         (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
 
 1511         last_body_sid    = best_body_sid;
 
 1512         last_index_sid   = best_index_sid;
 
 1513         last_index_start = best_index_start;
 
 1536         m0 = m = (
a + 
b) >> 1;
 
 1538         while (m < b && mxf->partitions[m].body_sid != body_sid)
 
 1541         if (m < b && mxf->partitions[m].body_offset <= 
offset)
 
 1553             *partition_out = last_p;
 
 1558            "failed to find absolute offset of %"PRIX64
" in BodySID %i - partial file?\n",
 
 1588     int64_t offset_temp = 0;
 
 1595         edit_unit = 
FFMAX(edit_unit, 
s->index_start_position);  
 
 1597         if (edit_unit < s->index_start_position + 
s->index_duration) {
 
 1598             int64_t 
index = edit_unit - 
s->index_start_position;
 
 1600             if (
s->edit_unit_byte_count)
 
 1601                 offset_temp += 
s->edit_unit_byte_count * 
index;
 
 1603                 if (
s->nb_index_entries == 2 * 
s->index_duration + 1)
 
 1606                 if (index < 0 || index >= 
s->nb_index_entries) {
 
 1612                 offset_temp = 
s->stream_offset_entries[
index];
 
 1616                 *edit_unit_out = 
av_rescale_q(edit_unit, edit_rate, 
s->index_edit_rate);
 
 1621             offset_temp += 
s->edit_unit_byte_count * 
s->index_duration;
 
 1634     int8_t max_temporal_offset = -128;
 
 1641         if (!
s->nb_index_entries) {
 
 1646         if (
s->index_duration > INT_MAX - 
index_table->nb_ptses) {
 
 1703         int index_delta = 1;
 
 1704         int n = 
s->nb_index_entries;
 
 1706         if (
s->nb_index_entries == 2 * 
s->index_duration + 1) {
 
 1712         for (j = 0; j < n; j += index_delta, x++) {
 
 1713             int offset = 
s->temporal_offset_entries[j] / index_delta;
 
 1718                        "x >= nb_ptses - IndexEntryCount %i < IndexDuration %"PRId64
"?\n",
 
 1719                        s->nb_index_entries, 
s->index_duration);
 
 1725             if (index < 0 || index >= 
index_table->nb_ptses) {
 
 1727                        "index entry %i + TemporalOffset %i = %i, which is out of bounds\n",
 
 1734             max_temporal_offset = 
FFMAX(max_temporal_offset, 
offset);
 
 1757     int i, j, k, 
ret, nb_sorted_segments;
 
 1761         nb_sorted_segments <= 0) {
 
 1767     for (
i = 0; 
i < nb_sorted_segments; 
i++) {
 
 1768         if (
i == 0 || sorted_segments[
i-1]->index_sid != sorted_segments[
i]->index_sid)
 
 1770         else if (sorted_segments[
i-1]->body_sid != sorted_segments[
i]->body_sid) {
 
 1773             goto finish_decoding_index;
 
 1782         goto finish_decoding_index;
 
 1786     for (
i = j = 0; 
i < nb_sorted_segments; 
i++) {
 
 1787         if (
i != 0 && sorted_segments[
i-1]->index_sid != sorted_segments[
i]->index_sid) {
 
 1804                    " pointer array\n");
 
 1806             goto finish_decoding_index;
 
 1809         if (sorted_segments[
i]->index_start_position)
 
 1810             av_log(mxf->
fc, 
AV_LOG_WARNING, 
"IndexSID %i starts at EditUnit %"PRId64
" - seeking may not work as expected\n",
 
 1818             goto finish_decoding_index;
 
 1841                 av_log(mxf->
fc, 
AV_LOG_WARNING, 
"IndexSID %i segment %i has zero IndexDuration and there's more than one segment\n",
 
 1858 finish_decoding_index:
 
 1878     for (
i = 0; 
i < 
sizeof(
UID); 
i++) {
 
 1881         if (
i == 3 || 
i == 5 || 
i == 7 || 
i == 9) {
 
 1898     for (
i = 0; 
i < 
sizeof(
UID); 
i++) {
 
 1903     for (
i = 0; 
i < 
sizeof(
UID); 
i++) {
 
 1939     switch (component->
type) {
 
 1957         package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], SourcePackage);
 
 1961         if (!memcmp(package->package_ul, package_ul, 16) && !memcmp(package->package_uid, package_uid, 16))
 
 1979             if (!sub_descriptor) {
 
 1984                 return sub_descriptor;
 
 2027     switch (component->
type) {
 
 2067     int64_t start_position;
 
 2080         if (physical_package->
name && physical_package->
name[0])
 
 2100                        "Invalid edit rate (%d/%d) found on structural" 
 2101                        " component #%d, defaulting to 25/1\n",
 
 2175         if (material_package) 
break;
 
 2177     if (!material_package) {
 
 2183     if (material_package->
name && material_package->
name[0])
 
 2195         UID *essence_container_ul = 
NULL;
 
 2245             if (!source_package) {
 
 2256                     source_track = temp_track;
 
 2260             if (!source_track) {
 
 2279             if(source_track && component)
 
 2282         if (!source_track || !component || !source_package) {
 
 2326                    "Invalid edit rate (%d/%d) found on stream #%d, " 
 2327                    "defaulting to 25/1\n",
 
 2359                     essence_container_ul = &((
MXFCryptoContext *)metadata)->source_container_ul;
 
 2375         for (k = 0; k < 16; k++) {
 
 2378             if (!(k+1 & 19) || k == 5)
 
 2384         if (source_package->
name && source_package->
name[0])
 
 2386         if (material_track->
name && material_track->
name[0])
 
 2434                                                           "Field dominance %d support",
 
 2448                                                           "Field dominance %d support",
 
 2513                        "found for stream #%d, time base forced to 1/48000\n",
 
 2587                                                       "with different wrapping\n", 
i, j, track1->
body_sid);
 
 2600     struct tm time = { 0 };
 
 2602     time.tm_year = (timestamp >> 48) - 1900;
 
 2603     time.tm_mon  = (timestamp >> 40 & 0xFF) - 1;
 
 2604     time.tm_mday = (timestamp >> 32 & 0xFF);
 
 2605     time.tm_hour = (timestamp >> 24 & 0xFF);
 
 2606     time.tm_min  = (timestamp >> 16 & 0xFF);
 
 2607     time.tm_sec  = (timestamp >> 8  & 0xFF);
 
 2608     msecs        = (timestamp & 0xFF) * 4;
 
 2611     time.tm_mon  = av_clip(time.tm_mon,  0, 11);
 
 2612     time.tm_mday = av_clip(time.tm_mday, 1, 31);
 
 2613     time.tm_hour = av_clip(time.tm_hour, 0, 23);
 
 2614     time.tm_min  = av_clip(time.tm_min,  0, 59);
 
 2615     time.tm_sec  = av_clip(time.tm_sec,  0, 59);
 
 2616     msecs        = av_clip(msecs, 0, 999);
 
 2618     return (int64_t)
av_timegm(&time) * 1000000 + msecs * 1000;
 
 2621 #define SET_STR_METADATA(pb, name, str) do { \ 
 2622     if ((ret = mxf_read_utf16be_string(pb, size, &str)) < 0) \ 
 2624     av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \ 
 2627 #define SET_UID_METADATA(pb, name, var, str) do { \ 
 2628     avio_read(pb, var, 16); \ 
 2629     if ((ret = mxf_uid_to_str(var, &str)) < 0) \ 
 2631     av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \ 
 2634 #define SET_TS_METADATA(pb, name, var, str) do { \ 
 2635     var = avio_rb64(pb); \ 
 2636     if (var && (ret = avpriv_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var))) < 0) \ 
 2691     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, 
mxf_read_primer_pack },
 
 2692     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, 
mxf_read_partition_pack },
 
 2693     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, 
mxf_read_partition_pack },
 
 2694     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, 
mxf_read_partition_pack },
 
 2695     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, 
mxf_read_partition_pack },
 
 2696     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, 
mxf_read_partition_pack },
 
 2697     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, 
mxf_read_partition_pack },
 
 2698     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, 
mxf_read_partition_pack },
 
 2699     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, 
mxf_read_partition_pack },
 
 2700     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, 
mxf_read_partition_pack },
 
 2701     { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, 
mxf_read_partition_pack },
 
 2702     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2f,0x00 }, 
mxf_read_preface_metadata },
 
 2703     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x30,0x00 }, 
mxf_read_identification_metadata },
 
 2704     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, 
mxf_read_content_storage, 0, 
AnyType },
 
 2705     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, 
mxf_read_package, 
sizeof(
MXFPackage), 
SourcePackage },
 
 2706     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, 
mxf_read_package, 
sizeof(
MXFPackage), 
MaterialPackage },
 
 2707     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0f,0x00 }, 
mxf_read_sequence, 
sizeof(
MXFSequence), 
Sequence },
 
 2708     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x05,0x00 }, 
mxf_read_essence_group, 
sizeof(
MXFEssenceGroup), 
EssenceGroup},
 
 2709     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, 
mxf_read_source_clip, 
sizeof(
MXFStructuralComponent), 
SourceClip },
 
 2710     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3f,0x00 }, 
mxf_read_tagged_value, 
sizeof(
MXFTaggedValue), 
TaggedValue },
 
 2711     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
MultipleDescriptor },
 
 2712     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2713     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2714     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2715     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2716     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2717     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2718     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5b,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2719     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5c,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2720     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5e,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2721     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x64,0x00 }, 
mxf_read_generic_descriptor, 
sizeof(
MXFDescriptor), 
Descriptor }, 
 
 2722     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, 
mxf_read_track, 
sizeof(
MXFTrack), 
Track }, 
 
 2723     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, 
mxf_read_track, 
sizeof(
MXFTrack), 
Track }, 
 
 2724     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x14,0x00 }, 
mxf_read_timecode_component, 
sizeof(
MXFTimecodeComponent), 
TimecodeComponent },
 
 2725     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0c,0x00 }, 
mxf_read_pulldown_component, 
sizeof(
MXFPulldownComponent), 
PulldownComponent },
 
 2726     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, 
mxf_read_cryptographic_context, 
sizeof(
MXFCryptoContext), 
CryptoContext },
 
 2727     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, 
mxf_read_index_table_segment, 
sizeof(
MXFIndexTableSegment), 
IndexTableSegment },
 
 2728     { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x23,0x00 }, 
mxf_read_essence_container_data, 
sizeof(
MXFEssenceContainerData), 
EssenceContainerData },
 
 2729     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 
NULL, 0, 
AnyType },
 
 2763         if (next < 0 || next > INT64_MAX - 
size)
 
 2776                 if (local_tag == 
tag) {
 
 2783         if (ctx_size && 
tag == 0x3C0A) {
 
 2799                    "local tag %#04x extends past end of local set @ %#"PRIx64
"\n",
 
 2819             key[13] >= 2 && 
key[13] <= 4;
 
 2831     if (klv.
key[5] == 0x53) {
 
 2861     int64_t current_partition_ofs;
 
 2892     if (klv.
offset >= current_partition_ofs) {
 
 2894                PRIx64 
" indirectly points to itself\n", current_partition_ofs);
 
 2934                    "failed to seek to FooterPartition @ 0x%" PRIx64
 
 2935                    " (%"PRId64
") - partial file?\n",
 
 2958     for (
int i = 0; 
i < 
s->nb_streams; 
i++) {
 
 2994             if (x < mxf->partitions_count - 1)
 
 3001                        "partition %i: bad ThisPartition = %"PRIX64
"\n",
 
 3064     int essence_partition_count = 0;
 
 3065     int edit_unit_byte_count = 0;
 
 3087         essence_partition_count++;
 
 3091     if (essence_partition_count != 1)
 
 3100     if (edit_unit_byte_count <= 0)
 
 3118     segment->edit_unit_byte_count = edit_unit_byte_count;
 
 3119     segment->index_start_position = 0;
 
 3131     int64_t file_size, max_rip_length, min_rip_length;
 
 3147     max_rip_length = ((file_size - mxf->
run_in) / 105) * 12 + 28;
 
 3148     max_rip_length = 
FFMIN(max_rip_length, INT_MAX); 
 
 3151     min_rip_length = 16+1+24+4;
 
 3157     if (length < min_rip_length || length > max_rip_length)
 
 3159     avio_seek(
s->pb, file_size - length, SEEK_SET);
 
 3185     int64_t essence_offset = 0;
 
 3230             if (!essence_offset)
 
 3231                 essence_offset = klv.
offset;
 
 3253         if (!metadata->
read) {
 
 3260     if (!essence_offset)  {
 
 3272     for (
int i = 0; 
i < 
s->nb_streams; 
i++)
 
 3280         av_log(mxf->
fc, 
AV_LOG_INFO, 
"got %i index tables - only the first one (IndexSID %i) will be used\n",
 
 3290     for (
int i = 0; 
i < 
s->nb_streams; 
i++)
 
 3316         if (
offset < current_offset)
 
 3345                    "seeking detected on stream #%d with time base (%d/%d) and " 
 3346                    "sample rate (%d/%d), audio pts won't be accurate.\n",
 
 3360     int64_t next_ofs = -1;
 
 3363     int64_t new_edit_unit;
 
 3376     if (next_ofs > current_offset)
 
 3391     av_log(mxf->
fc, 
AV_LOG_WARNING, 
"edit unit sync lost on stream %d, jumping from %"PRId64
" to %"PRId64
"\n", st->
index, edit_unit, new_edit_unit);
 
 3403     if (!bits_per_sample)
 
 3409         || bits_per_sample <= 0
 
 3410         || par->
channels * (int64_t)bits_per_sample < 8)
 
 3455         int64_t max_data_size;
 
 3463             max_data_size = klv.
length;
 
 3490                        "error getting stream index %"PRIu32
"\n",
 
 3506                 if (next_ofs <= 0) {
 
 3514                     if ((
size = next_ofs - 
pos) <= 0) {
 
 3520                     if (
size > max_data_size)
 
 3521                         size = max_data_size;
 
 3531             if (klv.
key[12] == 0x06 && klv.
key[13] == 0x01 && klv.
key[14] == 0x10) {
 
 3583     for (
i = 0; 
i < 
s->nb_streams; 
i++)
 
 3584         s->streams[
i]->priv_data = 
NULL;
 
 3618     for (; bufp < 
end;) {
 
 3619         if (!((bufp[13] - 1) & 0xF2)){
 
 3637     AVStream *st = 
s->streams[stream_index];
 
 3656         if (sample_time < 0)
 
 3660         seekpos = 
avio_seek(
s->pb, (
s->bit_rate * seconds) >> 3, SEEK_SET);
 
 3672             for (
i = 0; 
i < 
s->nb_streams; 
i++) {
 
 3673                 MXFTrack *new_source_track = 
s->streams[
i]->priv_data;
 
 3676                     source_track = new_source_track;
 
 3681             if (
i == 
s->nb_streams)
 
 3687         sample_time = 
FFMAX(sample_time, 0);
 
 3696                 sample_time < t->ptses[0] &&
 
 3698                 sample_time = t->
ptses[0];
 
 3704             sample_time += t->
offsets[sample_time];
 
 3720             if (seekpos < klv.next_klv - klv.length || seekpos >= klv.
next_klv) {
 
 3732     for (
i = 0; 
i < 
s->nb_streams; 
i++) {
 
 3736             int64_t track_edit_unit = sample_time;
 
 3746     { 
"eia608_extract", 
"extract eia 608 captions from s436m track",