248 static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
249 static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
250 static const uint8_t mxf_avid_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 };
251 static const uint8_t mxf_system_item_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04 };
254 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 };
255 static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
256 static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
257 static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
259 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
265 int bytes_num = size & 0x7f;
271 size = size << 8 |
avio_r8(pb);
279 for (i = 0; i < size && !
url_feof(pb); i++) {
283 else if (b != key[i])
297 return klv->
length == -1 ? -1 : 0;
317 const uint8_t *buf_ptr, *end_ptr;
326 data_ptr = pkt->
data;
328 buf_ptr = pkt->
data + 4;
331 uint32_t
sample = bytestream_get_le32(&buf_ptr);
333 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
335 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
345 static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
351 uint64_t plaintext_size;
378 if (orig_size < plaintext_size)
382 if (size < 32 || size - 32 < orig_size)
388 if (memcmp(tmpbuf, checkv, 16))
394 else if (size < plaintext_size)
396 size -= plaintext_size;
399 &pkt->
data[plaintext_size], size >> 4, ivec, 1);
412 if (item_len != 18) {
416 if (item_num > 65536) {
433 uint64_t footer_partition;
434 uint32_t nb_essence_containers;
456 memset(partition, 0,
sizeof(*partition));
492 if (footer_partition) {
495 "inconsistent FooterPartition value: %"PRIu64
" != %"PRIu64
"\n",
503 "PartitionPack: ThisPartition = 0x%"PRIX64
504 ", PreviousPartition = 0x%"PRIX64
", "
505 "FooterPartition = 0x%"PRIX64
", IndexSID = %i, BodySID = %i\n",
514 "PreviousPartition points to this partition or forward\n");
518 if (op[12] == 1 && op[13] == 1) mxf->
op =
OP1a;
519 else if (op[12] == 1 && op[13] == 2) mxf->
op =
OP1b;
520 else if (op[12] == 1 && op[13] == 3) mxf->
op =
OP1c;
521 else if (op[12] == 2 && op[13] == 1) mxf->
op =
OP2a;
522 else if (op[12] == 2 && op[13] == 2) mxf->
op =
OP2b;
523 else if (op[12] == 2 && op[13] == 3) mxf->
op =
OP2c;
524 else if (op[12] == 3 && op[13] == 1) mxf->
op =
OP3a;
525 else if (op[12] == 3 && op[13] == 2) mxf->
op =
OP3b;
526 else if (op[12] == 3 && op[13] == 3) mxf->
op =
OP3c;
527 else if (op[12] == 64&& op[13] == 1) mxf->
op =
OPSONYOpt;
528 else if (op[12] == 0x10) {
533 if (nb_essence_containers != 1) {
539 nb_essence_containers, op ==
OP1a ?
"OP1a" :
"OPAtom");
545 av_log(mxf->
fc,
AV_LOG_ERROR,
"unknown operational pattern: %02xh %02xh - guessing OP1a\n", op[12], op[13]);
631 package->tracks_refs =
av_calloc(package->tracks_count,
sizeof(
UID));
632 if (!package->tracks_refs)
707 package->tracks_refs =
av_calloc(package->tracks_count,
sizeof(
UID));
708 if (!package->tracks_refs)
719 avio_read(pb, package->descriptor_ref, 16);
765 av_dlog(NULL,
"IndexEntryArray found\n");
787 int code,
value, ofs = 0;
793 av_dlog(NULL,
"pixel layout: code %#x\n", code);
796 layout[ofs++] = code;
797 layout[ofs++] =
value;
893 for (i = 0; i <
len; i++) {
894 if (i != 7 && key[i] != uid[i])
902 while (uls->
uid[0]) {
927 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14,
AV_CODEC_ID_MPEG2VIDEO },
928 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14,
AV_CODEC_ID_DVVIDEO },
929 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14,
AV_CODEC_ID_RAWVIDEO },
930 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
935 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x00,0x00 }, 14,
AV_CODEC_ID_MPEG2VIDEO },
936 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
941 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14,
AV_CODEC_ID_H264 },
942 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
947 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14,
AV_CODEC_ID_PCM_S16LE },
948 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14,
AV_CODEC_ID_MP2 },
949 { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14,
AV_CODEC_ID_PCM_S16LE },
950 { { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0xFF,0x4B,0x46,0x41,0x41,0x00,0x0D,0x4D,0x4F }, 14,
AV_CODEC_ID_PCM_S16LE },
951 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0,
AV_CODEC_ID_NONE },
956 int i, j, nb_segments = 0;
958 int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
968 if (!(unsorted_segments =
av_calloc(nb_segments,
sizeof(*unsorted_segments))) ||
969 !(*sorted_segments =
av_calloc(nb_segments,
sizeof(**sorted_segments)))) {
979 *nb_sorted_segments = 0;
982 for (i = 0; i < nb_segments; i++) {
983 int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
984 uint64_t best_index_duration = 0;
986 for (j = 0; j < nb_segments; j++) {
1008 (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
1009 last_body_sid = best_body_sid;
1010 last_index_sid = best_index_sid;
1011 last_index_start = best_index_start;
1025 int64_t offset_in =
offset;
1042 "failed to find absolute offset of %"PRIX64
" in BodySID %i - partial file?\n",
1043 offset_in, body_sid);
1075 int64_t offset_temp = 0;
1099 av_log(mxf->
fc,
AV_LOG_ERROR,
"IndexSID %i segment at %"PRId64
" missing EditUnitByteCount and IndexEntryArray\n",
1105 *edit_unit_out = edit_unit;
1123 int8_t max_temporal_offset = -128;
1149 for (x = 0; x < index_table->
nb_ptses; x++)
1181 int index_delta = 1;
1190 for (j = 0; j <
n; j += index_delta, x++) {
1196 "x >= nb_ptses - IndexEntryCount %i < IndexDuration %"PRId64
"?\n",
1204 if (index < 0 || index >= index_table->
nb_ptses) {
1206 "index entry %i + TemporalOffset %i = %i, which is out of bounds\n",
1212 max_temporal_offset =
FFMAX(max_temporal_offset, offset);
1216 index_table->
first_dts = -max_temporal_offset;
1227 int i, j, k,
ret, nb_sorted_segments;
1231 nb_sorted_segments <= 0) {
1237 for (i = 0; i < nb_sorted_segments; i++) {
1238 if (i == 0 || sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid)
1240 else if (sorted_segments[i-1]->body_sid != sorted_segments[i]->body_sid) {
1243 goto finish_decoding_index;
1250 goto finish_decoding_index;
1254 for (i = j = 0; i < nb_sorted_segments; i++) {
1255 if (i != 0 && sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid) {
1269 goto finish_decoding_index;
1272 if (sorted_segments[i]->index_start_position)
1273 av_log(mxf->
fc,
AV_LOG_WARNING,
"IndexSID %i starts at EditUnit %"PRId64
" - seeking may not work as expected\n",
1281 goto finish_decoding_index;
1289 av_log(mxf->
fc,
AV_LOG_WARNING,
"IndexSID %i segment %i has zero IndexDuration and there's more than one segment\n",
1306 finish_decoding_index:
1337 if (material_package)
break;
1339 if (!material_package) {
1352 UID *essence_container_ul = NULL;
1402 source_package = temp_package;
1406 if (!source_package) {
1407 av_dlog(mxf->
fc,
"material track %d: no corresponding source package found\n", material_track->
track_id);
1417 source_track = temp_track;
1421 if (!source_track) {
1426 if (!source_track || !component)
1456 "invalid edit rate (%d/%d) found on stream #%d, defaulting to 25/1\n",
1475 if (!sub_descriptor) {
1480 descriptor = sub_descriptor;
1501 essence_container_ul = &((
MXFCryptoContext *)metadata)->source_container_ul;
1512 for (k = 0; k < 16; k++) {
1515 if (!(k+1 & 19) || k == 5)
1521 container_ul =
mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul);
1567 container_ul =
mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
1578 av_log(mxf->
fc,
AV_LOG_WARNING,
"invalid sample rate (%d/%d) found for stream #%d, time base forced to 1/48000\n",
1630 buf_size = size + size/2 + 1;
1650 for (i = 0; i <
sizeof(
UID); i++) {
1651 snprintf(p, 2 + 1,
"%.2x", uid[i]);
1653 if (i == 3 || i == 5 || i == 7 || i == 9) {
1663 struct tm time = {0};
1664 time.tm_year = (timestamp >> 48) - 1900;
1665 time.tm_mon = (timestamp >> 40 & 0xFF) - 1;
1666 time.tm_mday = (timestamp >> 32 & 0xFF);
1667 time.tm_hour = (timestamp >> 24 & 0xFF);
1668 time.tm_min = (timestamp >> 16 & 0xFF);
1669 time.tm_sec = (timestamp >> 8 & 0xFF);
1672 time.tm_mon =
FFMAX(time.tm_mon, 0);
1673 time.tm_mday =
FFMAX(time.tm_mday, 1);
1678 strftime(*str, 32,
"%Y-%m-%d %H:%M:%S", &time);
1683 #define SET_STR_METADATA(pb, name, str) do { \
1684 if ((ret = mxf_read_utf16_string(pb, size, &str)) < 0) \
1686 av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
1689 #define SET_UID_METADATA(pb, name, var, str) do { \
1690 avio_read(pb, var, 16); \
1691 if ((ret = mxf_uid_to_str(var, &str)) < 0) \
1693 av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
1696 #define SET_TS_METADATA(pb, name, var, str) do { \
1697 var = avio_rb64(pb); \
1698 if ((ret = mxf_timestamp_to_str(var, &str)) < 0) \
1700 av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
1741 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 },
mxf_read_primer_pack },
1742 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 },
mxf_read_partition_pack },
1743 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 },
mxf_read_partition_pack },
1744 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 },
mxf_read_partition_pack },
1745 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 },
mxf_read_partition_pack },
1746 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 },
mxf_read_partition_pack },
1747 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 },
mxf_read_partition_pack },
1748 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 },
mxf_read_partition_pack },
1749 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 },
mxf_read_partition_pack },
1750 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 },
mxf_read_partition_pack },
1751 { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 },
mxf_read_partition_pack },
1752 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x30,0x00 },
mxf_read_identification_metadata },
1753 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 },
mxf_read_content_storage, 0,
AnyType },
1754 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 },
mxf_read_source_package,
sizeof(
MXFPackage),
SourcePackage },
1755 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 },
mxf_read_material_package,
sizeof(
MXFPackage),
MaterialPackage },
1756 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 },
mxf_read_sequence,
sizeof(
MXFSequence),
Sequence },
1757 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 },
mxf_read_source_clip,
sizeof(
MXFStructuralComponent),
SourceClip },
1758 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
MultipleDescriptor },
1759 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1760 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1761 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1762 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1763 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1764 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 },
mxf_read_generic_descriptor,
sizeof(
MXFDescriptor),
Descriptor },
1765 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 },
mxf_read_track,
sizeof(
MXFTrack),
Track },
1766 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 },
mxf_read_track,
sizeof(
MXFTrack),
Track },
1767 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x14,0x00 },
mxf_read_timecode_component,
sizeof(
MXFTimecodeComponent),
TimecodeComponent },
1768 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 },
mxf_read_cryptographic_context,
sizeof(
MXFCryptoContext),
CryptoContext },
1769 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 },
mxf_read_index_table_segment,
sizeof(
MXFIndexTableSegment),
IndexTableSegment },
1770 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0,
AnyType },
1788 av_dlog(mxf->
fc,
"local tag %#04x size %d\n", tag, size);
1797 if (local_tag == tag) {
1799 av_dlog(mxf->
fc,
"local tag %#04x\n", local_tag);
1804 if (ctx_size && tag == 0x3C0A)
1806 else if ((ret = read_child(ctx, pb, tag, size, uid, -1)) < 0)
1816 "local tag %#04x extends past end of local set @ %#"PRIx64
"\n",
1822 if (ctx_size) ctx->
type = type;
1842 av_dlog(mxf->
fc,
"seeking to previous partition\n");
1864 av_dlog(mxf->
fc,
"seeking to footer\n");
1876 av_log(mxf->
fc,
AV_LOG_ERROR,
"failed to seek to footer @ 0x%"PRIx64
" (%"PRId64
") - partial file?\n",
1924 "partition %i: bad ThisPartition = %"PRIX64
"\n",
1934 int64_t
ret = (position / kag_size) * kag_size;
1935 return ret == position ? ret : ret + kag_size;
1978 int64_t essence_offset = 0;
2021 int64_t op1a_essence_offset =
2039 if (!essence_offset)
2040 essence_offset = klv.
offset;
2056 for (metadata = mxf_metadata_read_table; metadata->
read; metadata++) {
2059 if (klv.
key[5] == 0x53) {
2081 if (!metadata->
read)
2085 if (!essence_offset) {
2103 av_log(mxf->
fc,
AV_LOG_INFO,
"got %i index tables - only the first one (IndexSID %i) will be used\n",
2126 int64_t last_ofs = -1, next_ofs = -1;
2139 if (next_ofs <= last_ofs) {
2143 "next_ofs didn't change. not deriving packet timestamps\n");
2147 if (next_ofs > current_offset)
2150 last_ofs = next_ofs;
2163 int i, total = 0, size = 0;
2170 if ((sample_rate.
num / sample_rate.
den) == 48000)
2173 int remainder = (sample_rate.
num * time_base.
num) % (time_base.
den * sample_rate.
den);
2175 av_mul_q(sample_rate, time_base)));
2178 "seeking detected on stream #%d with time base (%d/%d) and sample rate (%d/%d), audio pts won't be accurate.\n",
2179 stream_index, time_base.
num, time_base.
den, sample_rate.
num, sample_rate.
den);
2228 int64_t next_ofs, next_klv;
2247 if (next_ofs >= 0 && next_klv > next_ofs) {
2251 "OPAtom misinterpreted as OP1a?"
2252 "KLV for edit unit %i extending into "
2259 if (klv.
key[12] == 0x06 && klv.
key[13] == 0x01 && klv.
key[14] == 0x10) {
2306 int64_t ret64, pos, next_pos;
2335 if ((size = next_pos - pos) <= 0) {
2340 if ((ret64 =
avio_seek(s->
pb, pos, SEEK_SET)) < 0)
2426 for (; bufp <
end; bufp++) {
2453 if (sample_time < 0)
2466 sample_time =
FFMAX(sample_time, 0);
2490 uint64_t current_sample_count = 0;