249 static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
250 static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
251 static const uint8_t mxf_avid_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 };
252 static const uint8_t mxf_system_item_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04 };
255 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 };
256 static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
257 static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
258 static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
260 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
266 int bytes_num = size & 0x7f;
272 size = size << 8 |
avio_r8(pb);
280 for (i = 0; i < size && !
url_feof(pb); i++) {
284 else if (b != key[i])
298 return klv->
length == -1 ? -1 : 0;
318 const uint8_t *buf_ptr, *end_ptr;
327 data_ptr = pkt->
data;
328 end_ptr = pkt->
data + length;
329 buf_ptr = pkt->
data + 4;
332 uint32_t
sample = bytestream_get_le32(&buf_ptr);
334 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
336 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
346 static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
352 uint64_t plaintext_size;
379 if (orig_size < plaintext_size)
383 if (size < 32 || size - 32 < orig_size)
389 if (memcmp(tmpbuf, checkv, 16))
395 else if (size < plaintext_size)
397 size -= plaintext_size;
400 &pkt->
data[plaintext_size], size >> 4, ivec, 1);
413 if (item_len != 18) {
418 if (item_num > 65536) {
435 uint64_t footer_partition;
436 uint32_t nb_essence_containers;
458 memset(partition, 0,
sizeof(*partition));
494 if (footer_partition) {
497 "inconsistent FooterPartition value: %"PRIu64
" != %"PRIu64
"\n",
505 "PartitionPack: ThisPartition = 0x%"PRIX64
506 ", PreviousPartition = 0x%"PRIX64
", "
507 "FooterPartition = 0x%"PRIX64
", IndexSID = %i, BodySID = %i\n",
516 "PreviousPartition points to this partition or forward\n");
520 if (op[12] == 1 && op[13] == 1) mxf->
op =
OP1a;
521 else if (op[12] == 1 && op[13] == 2) mxf->
op =
OP1b;
522 else if (op[12] == 1 && op[13] == 3) mxf->
op =
OP1c;
523 else if (op[12] == 2 && op[13] == 1) mxf->
op =
OP2a;
524 else if (op[12] == 2 && op[13] == 2) mxf->
op =
OP2b;
525 else if (op[12] == 2 && op[13] == 3) mxf->
op =
OP2c;
526 else if (op[12] == 3 && op[13] == 1) mxf->
op =
OP3a;
527 else if (op[12] == 3 && op[13] == 2) mxf->
op =
OP3b;
528 else if (op[12] == 3 && op[13] == 3) mxf->
op =
OP3c;
529 else if (op[12] == 64&& op[13] == 1) mxf->
op =
OPSONYOpt;
530 else if (op[12] == 0x10) {
535 if (nb_essence_containers != 1) {
541 nb_essence_containers, op ==
OP1a ?
"OP1a" :
"OPAtom");
547 av_log(mxf->
fc,
AV_LOG_ERROR,
"unknown operational pattern: %02xh %02xh - guessing OP1a\n", op[12], op[13]);
633 package->tracks_refs =
av_calloc(package->tracks_count,
sizeof(
UID));
634 if (!package->tracks_refs)
709 package->tracks_refs =
av_calloc(package->tracks_count,
sizeof(
UID));
710 if (!package->tracks_refs)
721 avio_read(pb, package->descriptor_ref, 16);
789 int code,
value, ofs = 0;
798 layout[ofs++] = code;
799 layout[ofs++] =
value;
892 for (i = 0; i <
len; i++) {
893 if (i != 7 && key[i] != uid[i])
901 while (uls->
uid[0]) {
926 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14,
AV_CODEC_ID_MPEG2VIDEO },
927 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14,
AV_CODEC_ID_DVVIDEO },
928 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14,
AV_CODEC_ID_RAWVIDEO },
929 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
934 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x00,0x00 }, 14,
AV_CODEC_ID_MPEG2VIDEO },
935 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
940 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14,
AV_CODEC_ID_H264 },
941 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
946 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14,
AV_CODEC_ID_PCM_S16LE },
947 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14,
AV_CODEC_ID_MP2 },
948 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14,
AV_CODEC_ID_PCM_S16LE },
949 { { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0xFF,0x4B,0x46,0x41,0x41,0x00,0x0D,0x4D,0x4F }, 14,
AV_CODEC_ID_PCM_S16LE },
950 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
955 int i, j, nb_segments = 0;
957 int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
967 if (!(unsorted_segments =
av_calloc(nb_segments,
sizeof(*unsorted_segments))) ||
968 !(*sorted_segments =
av_calloc(nb_segments,
sizeof(**sorted_segments)))) {
978 *nb_sorted_segments = 0;
981 for (i = 0; i < nb_segments; i++) {
982 int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
983 uint64_t best_index_duration = 0;
985 for (j = 0; j < nb_segments; j++) {
1007 (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
1008 last_body_sid = best_body_sid;
1009 last_index_sid = best_index_sid;
1010 last_index_start = best_index_start;
1024 int64_t offset_in =
offset;
1041 "failed to find absolute offset of %"PRIX64
" in BodySID %i - partial file?\n",
1042 offset_in, body_sid);
1074 int64_t offset_temp = 0;
1098 av_log(mxf->
fc,
AV_LOG_ERROR,
"IndexSID %i segment at %"PRId64
" missing EditUnitByteCount and IndexEntryArray\n",
1104 *edit_unit_out = edit_unit;
1122 int8_t max_temporal_offset = -128;
1148 for (x = 0; x < index_table->
nb_ptses; x++)
1180 int index_delta = 1;
1189 for (j = 0; j < n; j += index_delta, x++) {
1195 "x >= nb_ptses - IndexEntryCount %i < IndexDuration %"PRId64
"?\n",
1203 if (index < 0 || index >= index_table->
nb_ptses) {
1205 "index entry %i + TemporalOffset %i = %i, which is out of bounds\n",
1211 max_temporal_offset =
FFMAX(max_temporal_offset, offset);
1215 index_table->
first_dts = -max_temporal_offset;
1226 int i, j, k, ret, nb_sorted_segments;
1230 nb_sorted_segments <= 0) {
1236 for (i = 0; i < nb_sorted_segments; i++) {
1237 if (i == 0 || sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid)
1239 else if (sorted_segments[i-1]->body_sid != sorted_segments[i]->body_sid) {
1242 goto finish_decoding_index;
1249 goto finish_decoding_index;
1253 for (i = j = 0; i < nb_sorted_segments; i++) {
1254 if (i != 0 && sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid) {
1268 goto finish_decoding_index;
1271 if (sorted_segments[i]->index_start_position)
1272 av_log(mxf->
fc,
AV_LOG_WARNING,
"IndexSID %i starts at EditUnit %"PRId64
" - seeking may not work as expected\n",
1280 goto finish_decoding_index;
1288 av_log(mxf->
fc,
AV_LOG_WARNING,
"IndexSID %i segment %i has zero IndexDuration and there's more than one segment\n",
1305 finish_decoding_index:
1336 if (material_package)
break;
1338 if (!material_package) {
1351 UID *essence_container_ul =
NULL;
1401 source_package = temp_package;
1405 if (!source_package) {
1406 av_dlog(mxf->
fc,
"material track %d: no corresponding source package found\n", material_track->
track_id);
1416 source_track = temp_track;
1420 if (!source_track) {
1425 if (!source_track || !component)
1455 "invalid edit rate (%d/%d) found on stream #%d, defaulting to 25/1\n",
1474 if (!sub_descriptor) {
1479 descriptor = sub_descriptor;
1500 essence_container_ul = &((
MXFCryptoContext *)metadata)->source_container_ul;
1511 for (k = 0; k < 16; k++) {
1514 if (!(k+1 & 19) || k == 5)
1520 container_ul =
mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul);
1566 container_ul =
mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
1577 av_log(mxf->
fc,
AV_LOG_WARNING,
"invalid sample rate (%d/%d) found for stream #%d, time base forced to 1/48000\n",
1620 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 },
mxf_read_primer_pack },
1621 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 },
mxf_read_partition_pack },
1622 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 },
mxf_read_partition_pack },
1623 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 },
mxf_read_partition_pack },
1624 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 },
mxf_read_partition_pack },
1625 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 },
mxf_read_partition_pack },
1626 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 },
mxf_read_partition_pack },
1627 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 },
mxf_read_partition_pack },
1628 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 },
mxf_read_partition_pack },
1629 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 },
mxf_read_partition_pack },
1630 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 },
mxf_read_partition_pack },
1631 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 },
mxf_read_content_storage, 0,
AnyType },
1632 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 },
mxf_read_source_package,
sizeof(
MXFPackage),
SourcePackage },
1633 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 },
mxf_read_material_package,
sizeof(
MXFPackage),
MaterialPackage },
1634 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 },
mxf_read_sequence,
sizeof(
MXFSequence),
Sequence },
1635 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 },
mxf_read_source_clip,
sizeof(
MXFStructuralComponent),
SourceClip },
1636 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
MultipleDescriptor },
1637 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1638 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1639 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1640 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1641 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1642 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1643 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 },
mxf_read_track,
sizeof(
MXFTrack),
Track },
1644 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 },
mxf_read_track,
sizeof(
MXFTrack),
Track },
1645 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x14,0x00 },
mxf_read_timecode_component,
sizeof(
MXFTimecodeComponent),
TimecodeComponent },
1646 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 },
mxf_read_cryptographic_context,
sizeof(
MXFCryptoContext),
CryptoContext },
1647 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 },
mxf_read_index_table_segment,
sizeof(
MXFIndexTableSegment),
IndexTableSegment },
1648 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
NULL, 0,
AnyType },
1666 av_dlog(mxf->
fc,
"local tag %#04x size %d\n", tag, size);
1675 if (local_tag == tag) {
1677 av_dlog(mxf->
fc,
"local tag %#04x\n", local_tag);
1682 if (ctx_size && tag == 0x3C0A)
1684 else if ((ret = read_child(ctx, pb, tag, size, uid, -1)) < 0)
1694 "local tag %#04x extends past end of local set @ %#"PRIx64
"\n",
1700 if (ctx_size) ctx->
type = type;
1720 av_dlog(mxf->
fc,
"seeking to previous partition\n");
1742 av_dlog(mxf->
fc,
"seeking to footer\n");
1754 av_log(mxf->
fc,
AV_LOG_ERROR,
"failed to seek to footer @ 0x%"PRIx64
" (%"PRId64
") - partial file?\n",
1802 "partition %i: bad ThisPartition = %"PRIX64
"\n",
1812 int64_t ret = (position / kag_size) * kag_size;
1813 return ret == position ? ret : ret + kag_size;
1856 int64_t essence_offset = 0;
1899 int64_t op1a_essence_offset =
1917 if (!essence_offset)
1918 essence_offset = klv.
offset;
1934 for (metadata = mxf_metadata_read_table; metadata->
read; metadata++) {
1937 if (klv.
key[5] == 0x53) {
1959 if (!metadata->
read)
1963 if (!essence_offset) {
1981 av_log(mxf->
fc,
AV_LOG_INFO,
"got %i index tables - only the first one (IndexSID %i) will be used\n",
2004 int64_t last_ofs = -1, next_ofs = -1;
2017 if (next_ofs <= last_ofs) {
2021 "next_ofs didn't change. not deriving packet timestamps\n");
2025 if (next_ofs > current_offset)
2028 last_ofs = next_ofs;
2041 int i, total = 0, size = 0;
2048 if ((sample_rate.
num / sample_rate.
den) == 48000)
2051 int remainder = (sample_rate.
num * time_base.
num) % (time_base.
den * sample_rate.
den);
2053 av_mul_q(sample_rate, time_base)));
2056 "seeking detected on stream #%d with time base (%d/%d) and sample rate (%d/%d), audio pts won't be accurate.\n",
2057 stream_index, time_base.
num, time_base.
den, sample_rate.
num, sample_rate.
den);
2108 int64_t next_ofs, next_klv;
2127 if (next_ofs >= 0 && next_klv > next_ofs) {
2131 "KLV for edit unit %i extends into next edit unit - OPAtom misinterpreted as OP1a?\n",
2137 if (klv.
key[12] == 0x06 && klv.
key[13] == 0x01 && klv.
key[14] == 0x10) {
2184 int64_t ret64, pos, next_pos;
2213 if ((size = next_pos - pos) <= 0) {
2218 if ((ret64 =
avio_seek(s->
pb, pos, SEEK_SET)) < 0)
2304 for (; bufp <
end; bufp++) {
2330 if (sample_time < 0)
2342 sample_time =
FFMAX(sample_time, 0);
2366 uint64_t current_sample_count = 0;