FFmpeg
Loading...
Searching...
No Matches
mxfdec.c
Go to the documentation of this file.
1/*
2 * MXF demuxer.
3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/*
23 * References
24 * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
25 * SMPTE 377M MXF File Format Specifications
26 * SMPTE 378M Operational Pattern 1a
27 * SMPTE 379M MXF Generic Container
28 * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
29 * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container
30 * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container
31 * SMPTE 2067-21 Interoperable Master Format — Application #2E
32 *
33 * Principle
34 * Search for Track numbers which will identify essence element KLV packets.
35 * Search for SourcePackage which define tracks which contains Track numbers.
36 * Material Package contains tracks with reference to SourcePackage tracks.
37 * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
38 * Assign Descriptors to correct Tracks.
39 *
40 * Metadata reading functions read Local Tags, get InstanceUID(0x3C0A) then add MetaDataSet to MXFContext.
41 * Metadata parsing resolves Strong References to objects.
42 *
43 * Simple demuxer, only OP1A supported and some files might not work at all.
44 * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
45 */
46
47#include <inttypes.h>
48#include <time.h>
49
50#include "libavutil/aes.h"
52#include "libavutil/avstring.h"
55#include "libavutil/mem.h"
57#include "libavcodec/defs.h"
58#include "libavcodec/internal.h"
62#include "libavutil/timecode.h"
63#include "libavutil/opt.h"
64#include "avformat.h"
65#include "avlanguage.h"
66#include "avio_internal.h"
67#include "demux.h"
68#include "internal.h"
69#include "mxf.h"
70
71#define MXF_MAX_CHUNK_SIZE (32 << 20)
72#define RUN_IN_MAX (65535+1) // S377m-2004 section 5.5 and S377-1-2009 section 6.5, the +1 is to be slightly more tolerant
73
79
80typedef enum {
81 OP1a = 1,
91 OPSONYOpt, /* FATE sample, violates the spec in places */
92} MXFOP;
93
99
117
122
127
132
142
150
158
163
170
171typedef struct {
173 char *name;
174 char *value;
176
177typedef struct {
179 MXFSequence *sequence; /* mandatory, and only one */
182 char *name;
183 uint8_t track_number[4];
186 uint64_t sample_count;
187 int64_t original_duration; /* st->duration in SampleRate/EditRate units */
191 int edit_units_per_packet; /* how many edit units to read at a time (PCM, ClipWrapped) */
193} MXFTrack;
194
195#define MXF_BLACK_REF_LEVEL_PRESENT (1 << 0)
196#define MXF_WHITE_REF_LEVEL_PRESENT (1 << 1)
197#define MXF_COLOR_RANGE_PRESENT (1 << 2)
198
199typedef struct MXFDescriptor {
206 int width;
207 int height; /* Field height, not frame height */
208 int frame_layout; /* See MXFFrameLayout enum */
210#define MXF_FIELD_DOMINANCE_DEFAULT 0
211#define MXF_FIELD_DOMINANCE_FF 1 /* coded first, displayed first */
212#define MXF_FIELD_DOMINANCE_FL 2 /* coded first, displayed last */
216 int64_t duration; /* ContainerDuration optional property */
217 unsigned int component_depth;
218 unsigned int black_ref_level;
219 unsigned int white_ref_level;
220 unsigned int color_range;
221 unsigned int range_items; ///< MXF_*_PRESENT flags of the range items that were read
223 unsigned int horiz_subsampling;
224 unsigned int vert_subsampling;
230 uint8_t *extradata;
239 size_t coll_size;
241
253
259
274
286
294
295/* decoded index table */
296typedef struct MXFIndexTable {
299 int nb_ptses; /* number of PTSes or total duration of index */
300 int64_t first_dts; /* DTS = EditUnit + first_dts */
301 int64_t *ptses; /* maps EditUnit -> PTS */
303 MXFIndexTableSegment **segments; /* sorted by IndexStartPosition */
304 AVIndexEntry *fake_index; /* used for calling ff_index_search_timestamp() */
305 int8_t *offsets; /* temporal offsets for display order to stored order conversion */
307
333
334/* NOTE: klv_offset is not set (-1) for local keys */
335typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset);
336
343
344/* partial keys to match */
345static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
346static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
347static const uint8_t mxf_avid_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 };
348static const uint8_t mxf_canopus_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x0a,0x0e,0x0f,0x03,0x01 };
349static const uint8_t mxf_system_item_key_cp[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x03,0x01,0x04 };
350static const uint8_t mxf_system_item_key_gc[] = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x03,0x01,0x14 };
351static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
352static const uint8_t mxf_apple_coll_prefix[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x0e,0x20,0x04,0x01,0x05,0x03,0x01 };
353
354/* complete keys to match */
355static const uint8_t mxf_crypto_source_container_ul[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x02,0x02,0x00,0x00,0x00 };
356static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
357static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
358static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
359static const uint8_t mxf_ffv1_extradata[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x04,0x01,0x06,0x0c,0x01,0x00,0x00,0x00 }; // FFV1InitializationMetadata
360static const uint8_t mxf_avid_project_name[] = { 0xa5,0xfb,0x7b,0x25,0xf6,0x15,0x94,0xb9,0x62,0xfc,0x37,0x17,0x49,0x2d,0x42,0xbf };
361static const uint8_t mxf_jp2k_rsiz[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0a,0x04,0x01,0x06,0x03,0x01,0x00,0x00,0x00 };
362static const uint8_t mxf_indirect_value_utf16le[] = { 0x4c,0x00,0x02,0x10,0x01,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
363static const uint8_t mxf_indirect_value_utf16be[] = { 0x42,0x01,0x10,0x02,0x00,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
364static const uint8_t mxf_apple_coll_max_cll[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x0e,0x20,0x04,0x01,0x05,0x03,0x01,0x01 };
365static const uint8_t mxf_apple_coll_max_fall[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x0e,0x20,0x04,0x01,0x05,0x03,0x01,0x02 };
366
367static const uint8_t mxf_mca_label_dictionary_id[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x01,0x03,0x07,0x01,0x01,0x00,0x00,0x00 };
368static const uint8_t mxf_mca_tag_symbol[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x01,0x03,0x07,0x01,0x02,0x00,0x00,0x00 };
369static const uint8_t mxf_mca_tag_name[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x01,0x03,0x07,0x01,0x03,0x00,0x00,0x00 };
370static const uint8_t mxf_group_of_soundfield_groups_link_id[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x01,0x03,0x07,0x01,0x04,0x00,0x00,0x00 };
371static const uint8_t mxf_mca_link_id[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x01,0x03,0x07,0x01,0x05,0x00,0x00,0x00 };
372static const uint8_t mxf_mca_channel_id[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x01,0x03,0x04,0x0a,0x00,0x00,0x00,0x00 };
373static const uint8_t mxf_soundfield_group_link_id[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0e,0x01,0x03,0x07,0x01,0x06,0x00,0x00,0x00 };
374static const uint8_t mxf_mca_rfc5646_spoken_language[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x0d,0x03,0x01,0x01,0x02,0x03,0x15,0x00,0x00 };
375
376static const uint8_t mxf_sub_descriptor[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x04,0x06,0x10,0x00,0x00 };
377
385
386#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
387
389{
391 switch (type) {
392 case Descriptor:
394 av_freep(&((MXFDescriptor *)*ctx)->extradata);
395 av_freep(&((MXFDescriptor *)*ctx)->mastering);
396 av_freep(&((MXFDescriptor *)*ctx)->coll);
397 av_freep(&((MXFDescriptor *)*ctx)->file_descriptors_refs);
398 av_freep(&((MXFDescriptor *)*ctx)->sub_descriptors_refs);
399 break;
401 av_freep(&((MXFFFV1SubDescriptor *)*ctx)->extradata);
402 break;
406 av_freep(&((MXFMCASubDescriptor *)*ctx)->language);
407 av_freep(&((MXFMCASubDescriptor *)*ctx)->group_of_soundfield_groups_link_id_refs);
408 break;
409 case Sequence:
410 av_freep(&((MXFSequence *)*ctx)->structural_components_refs);
411 break;
412 case EssenceGroup:
413 av_freep(&((MXFEssenceGroup *)*ctx)->structural_components_refs);
414 break;
415 case SourcePackage:
416 case MaterialPackage:
417 av_freep(&((MXFPackage *)*ctx)->tracks_refs);
418 av_freep(&((MXFPackage *)*ctx)->name);
419 av_freep(&((MXFPackage *)*ctx)->comment_refs);
420 break;
421 case TaggedValue:
424 break;
425 case Track:
426 av_freep(&((MXFTrack *)*ctx)->name);
427 break;
429 seg = (MXFIndexTableSegment *)*ctx;
431 av_freep(&seg->flag_entries);
433 break;
434 default:
435 break;
436 }
437 av_freep(ctx);
438}
439
441{
442 uint64_t size = avio_r8(pb);
443 if (size & 0x80) { /* long form */
444 int bytes_num = size & 0x7f;
445 /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
446 if (bytes_num > 8)
447 return AVERROR_INVALIDDATA;
448 if (llen)
449 *llen = bytes_num + 1;
450 size = 0;
451 while (bytes_num--)
452 size = size << 8 | avio_r8(pb);
453 } else if (llen) {
454 *llen = 1;
455 }
456 if (size > INT64_MAX)
457 return AVERROR_INVALIDDATA;
458 return size;
459}
460
461static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
462{
463 int i, b;
464 for (i = 0; i < size && !avio_feof(pb); i++) {
465 b = avio_r8(pb);
466 if (b == key[0])
467 i = 0;
468 else if (b != key[i])
469 i = -1;
470 }
471 return i == size;
472}
473
474// special case of mxf_read_sync for mxf_klv_key
476{
477 uint32_t key = avio_rb32(pb);
478 // key will never match mxf_klv_key on EOF
479 if (key == AV_RB32(mxf_klv_key))
480 return 1;
481
482 while (!avio_feof(pb)) {
483 key = (key << 8) | avio_r8(pb);
484 if (key == AV_RB32(mxf_klv_key))
485 return 1;
486 }
487 return 0;
488}
489
491{
492 int64_t length, pos;
493 int llen;
494
495 if (!mxf_read_sync_klv(pb))
496 return AVERROR_INVALIDDATA;
497 klv->offset = avio_tell(pb) - 4;
498 if (klv->offset < mxf->run_in)
499 return AVERROR_INVALIDDATA;
500
501 memcpy(klv->key, mxf_klv_key, 4);
502 int ret = ffio_read_size(pb, klv->key + 4, 12);
503 if (ret < 0)
504 return ret;
505 length = klv_decode_ber_length(pb, &llen);
506 if (length < 0)
507 return length;
508 klv->length = length;
509 if (klv->offset > INT64_MAX - 16 - llen)
510 return AVERROR_INVALIDDATA;
511
512 pos = klv->offset + 16 + llen;
513 if (pos > INT64_MAX - length)
514 return AVERROR_INVALIDDATA;
515 klv->next_klv = pos + length;
516 return 0;
517}
518
519static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv, int body_sid)
520{
521 for (int i = 0; i < s->nb_streams; i++) {
522 MXFTrack *track = s->streams[i]->priv_data;
523 /* SMPTE 379M 7.3 */
524 if (track && (!body_sid || !track->body_sid || track->body_sid == body_sid) && !memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
525 return i;
526 }
527 /* return 0 if only one stream, for OP Atom files with 0 as track number */
528 return s->nb_streams == 1 && s->streams[0]->priv_data ? 0 : -1;
529}
530
532{
533 // we look for partition where the offset is placed
534 int a, b, m;
535 int64_t pack_ofs;
536
537 a = -1;
538 b = mxf->partitions_count;
539
540 while (b - a > 1) {
541 m = (a + b) >> 1;
542 pack_ofs = mxf->partitions[m].pack_ofs;
543 if (pack_ofs <= offset)
544 a = m;
545 else
546 b = m;
547 }
548
549 if (a == -1)
550 return 0;
551 return mxf->partitions[a].body_sid;
552}
553
555{
556 int count = avio_rb16(s->pb);
557 int cdp_identifier, cdp_length, cdp_footer_id, ccdata_id, cc_count;
558 int line_num, sample_coding, sample_count;
559 int did, sdid, data_length;
560 int ret;
561
562 if (count > 1)
563 av_log(s, AV_LOG_WARNING, "unsupported multiple ANC packets (%d) per KLV packet\n", count);
564
565 for (int i = 0; i < count; i++) {
566 if (length < 6) {
567 av_log(s, AV_LOG_ERROR, "error reading s436m packet %"PRId64"\n", length);
568 return AVERROR_INVALIDDATA;
569 }
570 line_num = avio_rb16(s->pb);
571 avio_r8(s->pb); // wrapping type
572 sample_coding = avio_r8(s->pb);
573 sample_count = avio_rb16(s->pb);
574 length -= 6 + 8 + sample_count;
575 if (line_num != 9 && line_num != 11)
576 continue;
577 if (sample_coding == 7 || sample_coding == 8 || sample_coding == 9) {
578 av_log(s, AV_LOG_WARNING, "unsupported s436m 10 bit sample coding\n");
579 continue;
580 }
581 if (length < 0)
582 return AVERROR_INVALIDDATA;
583
584 avio_rb32(s->pb); // array count
585 avio_rb32(s->pb); // array elem size
586 did = avio_r8(s->pb);
587 sdid = avio_r8(s->pb);
588 data_length = avio_r8(s->pb);
589 if (did != 0x61 || sdid != 1) {
590 av_log(s, AV_LOG_WARNING, "unsupported did or sdid: %x %x\n", did, sdid);
591 continue;
592 }
593 cdp_identifier = avio_rb16(s->pb); // cdp id
594 if (cdp_identifier != 0x9669) {
595 av_log(s, AV_LOG_ERROR, "wrong cdp identifier %x\n", cdp_identifier);
596 return AVERROR_INVALIDDATA;
597 }
598 cdp_length = avio_r8(s->pb);
599 avio_r8(s->pb); // cdp_frame_rate
600 avio_r8(s->pb); // cdp_flags
601 avio_rb16(s->pb); // cdp_hdr_sequence_cntr
602 ccdata_id = avio_r8(s->pb); // ccdata_id
603 if (ccdata_id != 0x72) {
604 av_log(s, AV_LOG_ERROR, "wrong cdp data section %x\n", ccdata_id);
605 return AVERROR_INVALIDDATA;
606 }
607 cc_count = avio_r8(s->pb) & 0x1f;
608 ret = av_get_packet(s->pb, pkt, cc_count * 3);
609 if (ret < 0)
610 return ret;
611 if (cdp_length - 9 - 4 < cc_count * 3) {
612 av_log(s, AV_LOG_ERROR, "wrong cdp size %d cc count %d\n", cdp_length, cc_count);
613 return AVERROR_INVALIDDATA;
614 }
615 avio_skip(s->pb, data_length - 9 - 4 - cc_count * 3);
616 cdp_footer_id = avio_r8(s->pb);
617 if (cdp_footer_id != 0x74) {
618 av_log(s, AV_LOG_ERROR, "wrong cdp footer section %x\n", cdp_footer_id);
619 return AVERROR_INVALIDDATA;
620 }
621 avio_rb16(s->pb); // cdp_ftr_sequence_cntr
622 avio_r8(s->pb); // packet_checksum
623 break;
624 }
625
626 return 0;
627}
628
629/* XXX: use AVBitStreamFilter */
631{
632 const uint8_t *buf_ptr, *end_ptr;
633 uint8_t *data_ptr;
634
635 if (length > 61444) /* worst case PAL 1920 samples 8 channels */
636 return AVERROR_INVALIDDATA;
637 length = av_get_packet(pb, pkt, length);
638 if (length < 0)
639 return length;
640 data_ptr = pkt->data;
641 end_ptr = pkt->data + length;
642 buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
643
644 if (st->codecpar->ch_layout.nb_channels > 8)
645 return AVERROR_INVALIDDATA;
646
647 for (; end_ptr - buf_ptr >= st->codecpar->ch_layout.nb_channels * 4; ) {
648 for (int i = 0; i < st->codecpar->ch_layout.nb_channels; i++) {
649 uint32_t sample = bytestream_get_le32(&buf_ptr);
650 if (st->codecpar->bits_per_coded_sample == 24)
651 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
652 else
653 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
654 }
655 // always 8 channels stored SMPTE 331M
656 buf_ptr += 32 - st->codecpar->ch_layout.nb_channels * 4;
657 }
658 av_shrink_packet(pkt, data_ptr - pkt->data);
659 return 0;
660}
661
663{
664 static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
665 MXFContext *mxf = s->priv_data;
666 AVIOContext *pb = s->pb;
667 int64_t end = avio_tell(pb) + klv->length;
669 uint64_t orig_size;
670 uint64_t plaintext_size;
671 uint8_t ivec[16];
672 uint8_t tmpbuf[16];
673 int ret;
674 int index;
675 int body_sid;
676
677 if (!mxf->aesc && s->key && s->keylen == 16) {
678 mxf->aesc = av_aes_alloc();
679 if (!mxf->aesc)
680 return AVERROR(ENOMEM);
681 av_aes_init(mxf->aesc, s->key, 128, 1);
682 }
683 // crypto context
685 if (size < 0)
686 return size;
687 avio_skip(pb, size);
688 // plaintext offset
690 plaintext_size = avio_rb64(pb);
691 // source klv key
693 avio_read(pb, klv->key, 16);
695 return AVERROR_INVALIDDATA;
696
697 body_sid = find_body_sid_by_absolute_offset(mxf, klv->offset);
698 index = mxf_get_stream_index(s, klv, body_sid);
699 if (index < 0)
700 return AVERROR_INVALIDDATA;
701 // source size
703 orig_size = avio_rb64(pb);
704 if (orig_size < plaintext_size)
705 return AVERROR_INVALIDDATA;
706 // enc. code
708 if (size < 32 || size - 32 < orig_size || (int)orig_size != orig_size)
709 return AVERROR_INVALIDDATA;
710 avio_read(pb, ivec, 16);
711 ret = ffio_read_size(pb, tmpbuf, 16);
712 if (ret < 16)
713 return ret;
714 if (mxf->aesc)
715 av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
716 if (memcmp(tmpbuf, checkv, 16))
717 av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
718 size -= 32;
719 size = av_get_packet(pb, pkt, size);
720 if (size < 0)
721 return size;
722 else if (size < plaintext_size)
723 return AVERROR_INVALIDDATA;
724 size -= plaintext_size;
725 if (mxf->aesc)
726 av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
727 &pkt->data[plaintext_size], size >> 4, ivec, 1);
728 av_shrink_packet(pkt, orig_size);
729 pkt->stream_index = index;
730 avio_skip(pb, end - avio_tell(pb));
731 return 0;
732}
733
734static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
735{
736 MXFContext *mxf = arg;
737 int item_num = avio_rb32(pb);
738 int item_len = avio_rb32(pb);
739
740 if (item_len != 18) {
741 avpriv_request_sample(pb, "Primer pack item length %d", item_len);
743 }
744 if (item_num > 65536 || item_num < 0) {
745 av_log(mxf->fc, AV_LOG_ERROR, "item_num %d is too large\n", item_num);
746 return AVERROR_INVALIDDATA;
747 }
748 if (mxf->local_tags)
749 av_log(mxf->fc, AV_LOG_VERBOSE, "Multiple primer packs\n");
750 av_free(mxf->local_tags);
751 mxf->local_tags_count = 0;
752 mxf->local_tags = av_calloc(item_num, item_len);
753 if (!mxf->local_tags)
754 return AVERROR(ENOMEM);
755 mxf->local_tags_count = item_num;
756 avio_read(pb, mxf->local_tags, item_num*item_len);
757 return 0;
758}
759
760static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
761{
762 MXFContext *mxf = arg;
763 AVFormatContext *s = mxf->fc;
764 MXFPartition *partition, *tmp_part;
765 UID op;
766 uint64_t footer_partition;
767 uint32_t nb_essence_containers;
768 uint64_t this_partition;
769 int ret;
770
771 if (mxf->partitions_count >= INT_MAX / 2)
772 return AVERROR_INVALIDDATA;
773
774 av_assert0(klv_offset >= mxf->run_in);
775
776 tmp_part = av_realloc_array(mxf->partitions, mxf->partitions_count + 1, sizeof(*mxf->partitions));
777 if (!tmp_part)
778 return AVERROR(ENOMEM);
779 mxf->partitions = tmp_part;
780
781 if (mxf->parsing_backward) {
782 /* insert the new partition pack in the middle
783 * this makes the entries in mxf->partitions sorted by offset */
784 memmove(&mxf->partitions[mxf->last_forward_partition+1],
786 (mxf->partitions_count - mxf->last_forward_partition)*sizeof(*mxf->partitions));
787 partition = mxf->current_partition = &mxf->partitions[mxf->last_forward_partition];
788 } else {
790 partition = mxf->current_partition = &mxf->partitions[mxf->partitions_count];
791 }
792
793 memset(partition, 0, sizeof(*partition));
794 mxf->partitions_count++;
795 partition->pack_length = avio_tell(pb) - klv_offset + size;
796 partition->pack_ofs = klv_offset;
797
798 switch(uid[13]) {
799 case 2:
800 partition->type = Header;
801 break;
802 case 3:
803 partition->type = BodyPartition;
804 break;
805 case 4:
806 partition->type = Footer;
807 break;
808 default:
809 av_log(mxf->fc, AV_LOG_ERROR, "unknown partition type %i\n", uid[13]);
810 return AVERROR_INVALIDDATA;
811 }
812
813 /* consider both footers to be closed (there is only Footer and CompleteFooter) */
814 partition->closed = partition->type == Footer || !(uid[14] & 1);
815 partition->complete = uid[14] > 2;
816 avio_skip(pb, 4);
817 partition->kag_size = avio_rb32(pb);
818 this_partition = avio_rb64(pb);
819 if (this_partition != klv_offset - mxf->run_in) {
820 av_log(mxf->fc, AV_LOG_ERROR,
821 "this_partition %"PRId64" mismatches %"PRId64"\n",
822 this_partition, klv_offset - mxf->run_in);
823 return AVERROR_INVALIDDATA;
824 }
825 partition->previous_partition = avio_rb64(pb);
826 footer_partition = avio_rb64(pb);
827 partition->header_byte_count = avio_rb64(pb);
828 partition->index_byte_count = avio_rb64(pb);
829 partition->index_sid = avio_rb32(pb);
830 partition->body_offset = avio_rb64(pb);
831 partition->body_sid = avio_rb32(pb);
832 if (partition->body_offset < 0)
833 return AVERROR_INVALIDDATA;
834
835 ret = ffio_read_size(pb, op, sizeof(UID));
836 if (ret < 0) {
837 av_log(mxf->fc, AV_LOG_ERROR, "Failed reading UID\n");
838 return ret;
839 }
840 nb_essence_containers = avio_rb32(pb);
841
842 if (partition->type == Header) {
843 char str[36];
844 snprintf(str, sizeof(str), "%08x.%08x.%08x.%08x", AV_RB32(&op[0]), AV_RB32(&op[4]), AV_RB32(&op[8]), AV_RB32(&op[12]));
845 av_dict_set(&s->metadata, "operational_pattern_ul", str, 0);
846 }
847
848 if (this_partition &&
849 partition->previous_partition == this_partition) {
850 av_log(mxf->fc, AV_LOG_ERROR,
851 "PreviousPartition equal to ThisPartition %"PRIx64"\n",
852 partition->previous_partition);
853 /* override with the actual previous partition offset */
854 if (!mxf->parsing_backward && mxf->last_forward_partition > 1) {
855 MXFPartition *prev =
856 mxf->partitions + mxf->last_forward_partition - 2;
857 partition->previous_partition = prev->pack_ofs - mxf->run_in;
858 }
859 /* if no previous body partition are found point to the header
860 * partition */
861 if (partition->previous_partition == this_partition)
862 partition->previous_partition = 0;
863 av_log(mxf->fc, AV_LOG_ERROR,
864 "Overriding PreviousPartition with %"PRIx64"\n",
865 partition->previous_partition);
866 }
867
868 /* some files don't have FooterPartition set in every partition */
869 if (footer_partition) {
870 if (mxf->footer_partition && mxf->footer_partition != footer_partition) {
871 av_log(mxf->fc, AV_LOG_ERROR,
872 "inconsistent FooterPartition value: %"PRIu64" != %"PRIu64"\n",
873 mxf->footer_partition, footer_partition);
874 } else {
875 mxf->footer_partition = footer_partition;
876 }
877 }
878
879 av_log(mxf->fc, AV_LOG_TRACE,
880 "PartitionPack: ThisPartition = 0x%"PRIX64
881 ", PreviousPartition = 0x%"PRIX64", "
882 "FooterPartition = 0x%"PRIX64", IndexSID = %i, BodySID = %i\n",
883 this_partition,
884 partition->previous_partition, footer_partition,
885 partition->index_sid, partition->body_sid);
886
887 /* sanity check PreviousPartition if set */
888 //NOTE: this isn't actually enough, see mxf_seek_to_previous_partition()
889 if (partition->previous_partition &&
890 mxf->run_in + partition->previous_partition >= klv_offset) {
891 av_log(mxf->fc, AV_LOG_ERROR,
892 "PreviousPartition points to this partition or forward\n");
893 return AVERROR_INVALIDDATA;
894 }
895
896 if (op[12] == 1 && op[13] == 1) mxf->op = OP1a;
897 else if (op[12] == 1 && op[13] == 2) mxf->op = OP1b;
898 else if (op[12] == 1 && op[13] == 3) mxf->op = OP1c;
899 else if (op[12] == 2 && op[13] == 1) mxf->op = OP2a;
900 else if (op[12] == 2 && op[13] == 2) mxf->op = OP2b;
901 else if (op[12] == 2 && op[13] == 3) mxf->op = OP2c;
902 else if (op[12] == 3 && op[13] == 1) mxf->op = OP3a;
903 else if (op[12] == 3 && op[13] == 2) mxf->op = OP3b;
904 else if (op[12] == 3 && op[13] == 3) mxf->op = OP3c;
905 else if (op[12] == 64&& op[13] == 1) mxf->op = OPSONYOpt;
906 else if (op[12] == 0x10) {
907 /* SMPTE 390m: "There shall be exactly one essence container"
908 * The following block deals with files that violate this, namely:
909 * 2011_DCPTEST_24FPS.V.mxf - two ECs, OP1a
910 * abcdefghiv016f56415e.mxf - zero ECs, OPAtom, output by Avid AirSpeed */
911 if (nb_essence_containers != 1) {
912 MXFOP mxfop = nb_essence_containers ? OP1a : OPAtom;
913
914 /* only nag once */
915 if (!mxf->op)
917 "\"OPAtom\" with %"PRIu32" ECs - assuming %s\n",
918 nb_essence_containers,
919 mxfop == OP1a ? "OP1a" : "OPAtom");
920
921 mxf->op = mxfop;
922 } else
923 mxf->op = OPAtom;
924 } else {
925 av_log(mxf->fc, AV_LOG_ERROR, "unknown operational pattern: %02xh %02xh - guessing OP1a\n", op[12], op[13]);
926 mxf->op = OP1a;
927 }
928
929 if (partition->kag_size <= 0 || partition->kag_size > (1 << 20)) {
930 av_log(mxf->fc, AV_LOG_WARNING, "invalid KAGSize %"PRId32" - guessing ",
931 partition->kag_size);
932
933 if (mxf->op == OPSONYOpt)
934 partition->kag_size = 512;
935 else
936 partition->kag_size = 1;
937
938 av_log(mxf->fc, AV_LOG_WARNING, "%"PRId32"\n", partition->kag_size);
939 }
940
941 return 0;
942}
943
944static uint64_t partition_score(MXFPartition *p)
945{
946 uint64_t score;
947 if (!p)
948 return 0;
949 if (p->type == Footer)
950 score = 5;
951 else if (p->complete)
952 score = 4;
953 else if (p->closed)
954 score = 3;
955 else
956 score = 1;
957 return (score << 60) | ((uint64_t)p->pack_ofs >> 4);
958}
959
961{
963 int ret;
964
965 // Index Table is special because it might be added manually without
966 // partition and we iterate through all instances of them. Also some files
967 // use the same Instance UID for different index tables...
968 if (type != IndexTableSegment) {
969 for (int i = 0; i < mg->metadata_sets_count; i++) {
970 if (!memcmp((*metadata_set)->uid, mg->metadata_sets[i]->uid, 16)) {
971 uint64_t old_s = mg->metadata_sets[i]->partition_score;
972 uint64_t new_s = (*metadata_set)->partition_score;
973 if (old_s > new_s) {
974 mxf_free_metadataset(metadata_set, type);
975 return 0;
976 }
977 }
978 }
979 }
980
981 ret = av_dynarray_add_nofree(&mg->metadata_sets, &mg->metadata_sets_count, *metadata_set);
982 if (ret < 0) {
983 mxf_free_metadataset(metadata_set, type);
984 return ret;
985 }
986 return 0;
987}
988
989static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
990{
991 MXFCryptoContext *cryptocontext = arg;
992 if (size != 16)
993 return AVERROR_INVALIDDATA;
995 avio_read(pb, cryptocontext->source_container_ul, 16);
996 return 0;
997}
998
999static int mxf_read_strong_ref_array(AVIOContext *pb, UID **refs, int *count)
1000{
1001 int64_t ret;
1002 unsigned c = avio_rb32(pb);
1003
1004 //avio_read() used int
1005 if (c > INT_MAX / sizeof(UID))
1006 return AVERROR_PATCHWELCOME;
1007 *count = c;
1008
1009 av_free(*refs);
1010 *refs = av_malloc_array(*count, sizeof(UID));
1011 if (!*refs) {
1012 *count = 0;
1013 return AVERROR(ENOMEM);
1014 }
1015 avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
1016 ret = avio_read(pb, (uint8_t *)*refs, *count * sizeof(UID));
1017 if (ret != *count * sizeof(UID)) {
1018 *count = ret < 0 ? 0 : ret / sizeof(UID);
1019 return ret < 0 ? ret : AVERROR_INVALIDDATA;
1020 }
1021
1022 return 0;
1023}
1024
1025static inline int mxf_read_us_ascii_string(AVIOContext *pb, int size, char** str)
1026{
1027 int ret;
1028 size_t buf_size;
1029
1030 if (size < 0 || size > INT_MAX - 1)
1031 return AVERROR(EINVAL);
1032
1033 buf_size = size + 1;
1034 av_free(*str);
1035 *str = av_malloc(buf_size);
1036 if (!*str)
1037 return AVERROR(ENOMEM);
1038
1039 ret = avio_get_str(pb, size, *str, buf_size);
1040
1041 if (ret < 0) {
1042 av_freep(str);
1043 return ret;
1044 }
1045
1046 return ret;
1047}
1048
1049static inline int mxf_read_utf16_string(AVIOContext *pb, int size, char** str, int be)
1050{
1051 int ret;
1052 size_t buf_size;
1053
1054 if (size < 0 || size > INT_MAX/2)
1055 return AVERROR(EINVAL);
1056
1057 buf_size = size + size / 2 + 1;
1058 av_free(*str);
1059 *str = av_malloc(buf_size);
1060 if (!*str)
1061 return AVERROR(ENOMEM);
1062
1063 if (be)
1064 ret = avio_get_str16be(pb, size, *str, buf_size);
1065 else
1066 ret = avio_get_str16le(pb, size, *str, buf_size);
1067
1068 if (ret < 0) {
1069 av_freep(str);
1070 return ret;
1071 }
1072
1073 return ret;
1074}
1075
1076#define READ_STR16(type, big_endian) \
1077static int mxf_read_utf16 ## type ##_string(AVIOContext *pb, int size, char** str) \
1078{ \
1079return mxf_read_utf16_string(pb, size, str, big_endian); \
1080}
1081READ_STR16(be, 1)
1082READ_STR16(le, 0)
1083#undef READ_STR16
1084
1085static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1086{
1087 MXFContext *mxf = arg;
1088 switch (tag) {
1089 case 0x1901:
1090 if (mxf->packages_refs)
1091 av_log(mxf->fc, AV_LOG_VERBOSE, "Multiple packages_refs\n");
1093 case 0x1902:
1095 }
1096 return 0;
1097}
1098
1099static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1100{
1101 MXFStructuralComponent *source_clip = arg;
1102 switch(tag) {
1103 case 0x0202:
1104 source_clip->duration = avio_rb64(pb);
1105 break;
1106 case 0x1201:
1107 source_clip->start_position = avio_rb64(pb);
1108 break;
1109 case 0x1101:
1110 /* UMID, only get last 16 bytes */
1111 avio_read(pb, source_clip->source_package_ul, 16);
1112 avio_read(pb, source_clip->source_package_uid, 16);
1113 break;
1114 case 0x1102:
1115 source_clip->source_track_id = avio_rb32(pb);
1116 break;
1117 }
1118 return 0;
1119}
1120
1121static int mxf_read_timecode_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1122{
1123 MXFTimecodeComponent *mxf_timecode = arg;
1124 switch(tag) {
1125 case 0x1501:
1126 mxf_timecode->start_frame = avio_rb64(pb);
1127 break;
1128 case 0x1502:
1129 mxf_timecode->rate = (AVRational){avio_rb16(pb), 1};
1130 break;
1131 case 0x1503:
1132 mxf_timecode->drop_frame = avio_r8(pb);
1133 break;
1134 }
1135 return 0;
1136}
1137
1138static int mxf_read_pulldown_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1139{
1140 MXFPulldownComponent *mxf_pulldown = arg;
1141 switch(tag) {
1142 case 0x0d01:
1143 avio_read(pb, mxf_pulldown->input_segment_ref, 16);
1144 break;
1145 }
1146 return 0;
1147}
1148
1149static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1150{
1151 MXFTrack *track = arg;
1152 switch(tag) {
1153 case 0x4801:
1154 track->track_id = avio_rb32(pb);
1155 break;
1156 case 0x4804:
1157 avio_read(pb, track->track_number, 4);
1158 break;
1159 case 0x4802:
1160 mxf_read_utf16be_string(pb, size, &track->name);
1161 break;
1162 case 0x4b01:
1163 track->edit_rate.num = avio_rb32(pb);
1164 track->edit_rate.den = avio_rb32(pb);
1165 break;
1166 case 0x4b02:
1167 track->origin = avio_rb64(pb);
1168 break;
1169 case 0x4803:
1170 avio_read(pb, track->sequence_ref, 16);
1171 break;
1172 }
1173 return 0;
1174}
1175
1176static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1177{
1178 MXFSequence *sequence = arg;
1179 switch(tag) {
1180 case 0x0202:
1181 sequence->duration = avio_rb64(pb);
1182 break;
1183 case 0x0201:
1184 avio_read(pb, sequence->data_definition_ul, 16);
1185 break;
1186 case 0x1001:
1188 &sequence->structural_components_count);
1189 }
1190 return 0;
1191}
1192
1193static int mxf_read_essence_group(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1194{
1195 MXFEssenceGroup *essence_group = arg;
1196 switch (tag) {
1197 case 0x0202:
1198 essence_group->duration = avio_rb64(pb);
1199 break;
1200 case 0x0501:
1201 return mxf_read_strong_ref_array(pb, &essence_group->structural_components_refs,
1202 &essence_group->structural_components_count);
1203 }
1204 return 0;
1205}
1206
1207static int mxf_read_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1208{
1209 MXFPackage *package = arg;
1210 switch(tag) {
1211 case 0x4403:
1212 return mxf_read_strong_ref_array(pb, &package->tracks_refs,
1213 &package->tracks_count);
1214 case 0x4401:
1215 /* UMID */
1216 avio_read(pb, package->package_ul, 16);
1217 avio_read(pb, package->package_uid, 16);
1218 break;
1219 case 0x4701:
1220 avio_read(pb, package->descriptor_ref, 16);
1221 break;
1222 case 0x4402:
1223 return mxf_read_utf16be_string(pb, size, &package->name);
1224 case 0x4406:
1225 return mxf_read_strong_ref_array(pb, &package->comment_refs,
1226 &package->comment_count);
1227 }
1228 return 0;
1229}
1230
1231static int mxf_read_essence_container_data(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1232{
1233 MXFEssenceContainerData *essence_data = arg;
1234 switch(tag) {
1235 case 0x2701:
1236 /* linked package umid UMID */
1237 avio_read(pb, essence_data->package_ul, 16);
1238 avio_read(pb, essence_data->package_uid, 16);
1239 break;
1240 case 0x3f06:
1241 essence_data->index_sid = avio_rb32(pb);
1242 break;
1243 case 0x3f07:
1244 essence_data->body_sid = avio_rb32(pb);
1245 break;
1246 }
1247 return 0;
1248}
1249
1251{
1252 int i, length;
1253 uint32_t nb_index_entries;
1254
1255 if (segment->temporal_offset_entries)
1256 return AVERROR_INVALIDDATA;
1257
1258 nb_index_entries = avio_rb32(pb);
1259 if (nb_index_entries > INT_MAX)
1260 return AVERROR_INVALIDDATA;
1261 segment->nb_index_entries = nb_index_entries;
1262
1263 length = avio_rb32(pb);
1264 if(segment->nb_index_entries && length < 11)
1265 return AVERROR_INVALIDDATA;
1266
1267 if (!FF_ALLOC_TYPED_ARRAY(segment->temporal_offset_entries, segment->nb_index_entries) ||
1268 !FF_ALLOC_TYPED_ARRAY(segment->flag_entries , segment->nb_index_entries) ||
1269 !FF_ALLOC_TYPED_ARRAY(segment->stream_offset_entries , segment->nb_index_entries)) {
1270 av_freep(&segment->temporal_offset_entries);
1271 av_freep(&segment->flag_entries);
1272 return AVERROR(ENOMEM);
1273 }
1274
1275 for (i = 0; i < segment->nb_index_entries; i++) {
1276 if(avio_feof(pb))
1277 return AVERROR_INVALIDDATA;
1278 segment->temporal_offset_entries[i] = avio_r8(pb);
1279 avio_r8(pb); /* KeyFrameOffset */
1280 segment->flag_entries[i] = avio_r8(pb);
1281 segment->stream_offset_entries[i] = avio_rb64(pb);
1282 avio_skip(pb, length - 11);
1283 }
1284 return 0;
1285}
1286
1287static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1288{
1290 switch(tag) {
1291 case 0x3F05:
1292 segment->edit_unit_byte_count = avio_rb32(pb);
1293 av_log(NULL, AV_LOG_TRACE, "EditUnitByteCount %d\n", segment->edit_unit_byte_count);
1294 break;
1295 case 0x3F06:
1296 segment->index_sid = avio_rb32(pb);
1297 av_log(NULL, AV_LOG_TRACE, "IndexSID %d\n", segment->index_sid);
1298 break;
1299 case 0x3F07:
1300 segment->body_sid = avio_rb32(pb);
1301 av_log(NULL, AV_LOG_TRACE, "BodySID %d\n", segment->body_sid);
1302 break;
1303 case 0x3F0A:
1304 av_log(NULL, AV_LOG_TRACE, "IndexEntryArray found\n");
1306 case 0x3F0B:
1307 segment->index_edit_rate.num = avio_rb32(pb);
1308 segment->index_edit_rate.den = avio_rb32(pb);
1309 if (segment->index_edit_rate.num <= 0 ||
1310 segment->index_edit_rate.den <= 0)
1311 return AVERROR_INVALIDDATA;
1312 av_log(NULL, AV_LOG_TRACE, "IndexEditRate %d/%d\n", segment->index_edit_rate.num,
1313 segment->index_edit_rate.den);
1314 break;
1315 case 0x3F0C:
1316 segment->index_start_position = avio_rb64(pb);
1317 av_log(NULL, AV_LOG_TRACE, "IndexStartPosition %"PRId64"\n", segment->index_start_position);
1318 break;
1319 case 0x3F0D:
1320 segment->index_duration = avio_rb64(pb);
1321 av_log(NULL, AV_LOG_TRACE, "IndexDuration %"PRId64"\n", segment->index_duration);
1322 break;
1323 }
1324 return 0;
1325}
1326
1328{
1329 int code, value, ofs = 0;
1330 char layout[16] = {0}; /* not for printing, may end up not terminated on purpose */
1331
1332 do {
1333 code = avio_r8(pb);
1334 value = avio_r8(pb);
1335 av_log(NULL, AV_LOG_TRACE, "pixel layout: code %#x\n", code);
1336
1337 if (ofs <= 14) {
1338 layout[ofs++] = code;
1339 layout[ofs++] = value;
1340 } else
1341 break; /* don't read byte by byte on sneaky files filled with lots of non-zeroes */
1342 } while (code != 0); /* SMPTE 377M E.2.46 */
1343
1345}
1346
1347static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1348{
1349 MXFDescriptor *descriptor = arg;
1350 int entry_count, entry_size;
1351
1352 switch(tag) {
1353 case 0x3F01:
1354 return mxf_read_strong_ref_array(pb, &descriptor->file_descriptors_refs,
1355 &descriptor->file_descriptors_count);
1356 case 0x3002: /* ContainerDuration */
1357 descriptor->duration = avio_rb64(pb);
1358 break;
1359 case 0x3004:
1360 avio_read(pb, descriptor->essence_container_ul, 16);
1361 break;
1362 case 0x3005:
1363 avio_read(pb, descriptor->codec_ul, 16);
1364 break;
1365 case 0x3006:
1366 descriptor->linked_track_id = avio_rb32(pb);
1367 break;
1368 case 0x3201: /* PictureEssenceCoding */
1369 avio_read(pb, descriptor->essence_codec_ul, 16);
1370 break;
1371 case 0x3203:
1372 descriptor->width = avio_rb32(pb);
1373 break;
1374 case 0x3202:
1375 descriptor->height = avio_rb32(pb);
1376 break;
1377 case 0x320C:
1378 descriptor->frame_layout = avio_r8(pb);
1379 break;
1380 case 0x320D:
1381 entry_count = avio_rb32(pb);
1382 entry_size = avio_rb32(pb);
1383 if (entry_size == 4) {
1384 if (entry_count > 0)
1385 descriptor->video_line_map[0] = avio_rb32(pb);
1386 else
1387 descriptor->video_line_map[0] = 0;
1388 if (entry_count > 1)
1389 descriptor->video_line_map[1] = avio_rb32(pb);
1390 else
1391 descriptor->video_line_map[1] = 0;
1392 } else
1393 av_log(NULL, AV_LOG_WARNING, "VideoLineMap element size %d currently not supported\n", entry_size);
1394 break;
1395 case 0x320E:
1396 descriptor->aspect_ratio.num = avio_rb32(pb);
1397 descriptor->aspect_ratio.den = avio_rb32(pb);
1398 break;
1399 case 0x3210:
1400 avio_read(pb, descriptor->color_trc_ul, 16);
1401 break;
1402 case 0x3212:
1403 descriptor->field_dominance = avio_r8(pb);
1404 break;
1405 case 0x3219:
1406 avio_read(pb, descriptor->color_primaries_ul, 16);
1407 break;
1408 case 0x321A:
1409 avio_read(pb, descriptor->color_space_ul, 16);
1410 break;
1411 case 0x3301:
1412 descriptor->component_depth = avio_rb32(pb);
1413 break;
1414 case 0x3302:
1415 descriptor->horiz_subsampling = avio_rb32(pb);
1416 break;
1417 case 0x3304:
1418 descriptor->black_ref_level = avio_rb32(pb);
1420 break;
1421 case 0x3305:
1422 descriptor->white_ref_level = avio_rb32(pb);
1424 break;
1425 case 0x3306:
1426 descriptor->color_range = avio_rb32(pb);
1427 descriptor->range_items |= MXF_COLOR_RANGE_PRESENT;
1428 break;
1429 case 0x3215:
1430 descriptor->signal_standard = avio_r8(pb);
1431 break;
1432 case 0x3308:
1433 descriptor->vert_subsampling = avio_rb32(pb);
1434 break;
1435 case 0x3D03:
1436 descriptor->sample_rate.num = avio_rb32(pb);
1437 descriptor->sample_rate.den = avio_rb32(pb);
1438 break;
1439 case 0x3D06: /* SoundEssenceCompression */
1440 avio_read(pb, descriptor->essence_codec_ul, 16);
1441 break;
1442 case 0x3D07:
1443 descriptor->channels = avio_rb32(pb);
1444 break;
1445 case 0x3D01:
1446 descriptor->bits_per_sample = avio_rb32(pb);
1447 break;
1448 case 0x3401:
1449 mxf_read_pixel_layout(pb, descriptor);
1450 break;
1451 default:
1452 /* Private uid used by SONY C0023S01.mxf */
1454 if (descriptor->extradata)
1455 av_log(NULL, AV_LOG_WARNING, "Duplicate sony_mpeg4_extradata\n");
1456 av_free(descriptor->extradata);
1457 descriptor->extradata_size = 0;
1459 if (!descriptor->extradata)
1460 return AVERROR(ENOMEM);
1461 descriptor->extradata_size = size;
1462 avio_read(pb, descriptor->extradata, size);
1463 }
1465 uint32_t rsiz = avio_rb16(pb);
1466 if (rsiz == AV_PROFILE_JPEG2000_DCINEMA_2K ||
1468 descriptor->pix_fmt = AV_PIX_FMT_XYZ12;
1469 }
1471 if (!descriptor->mastering) {
1473 if (!descriptor->mastering)
1474 return AVERROR(ENOMEM);
1475 }
1477 for (int i = 0; i < 3; i++) {
1478 /* Order: large x, large y, other (i.e. RGB) */
1481 }
1482 /* Check we have seen mxf_mastering_display_white_point_chromaticity */
1483 if (descriptor->mastering->white_point[0].den != 0)
1484 descriptor->mastering->has_primaries = 1;
1485 }
1489 /* Check we have seen mxf_mastering_display_primaries */
1490 if (descriptor->mastering->display_primaries[0][0].den != 0)
1491 descriptor->mastering->has_primaries = 1;
1492 }
1495 /* Check we have seen mxf_mastering_display_minimum_luminance */
1496 if (descriptor->mastering->min_luminance.den != 0)
1497 descriptor->mastering->has_luminance = 1;
1498 }
1501 /* Check we have seen mxf_mastering_display_maximum_luminance */
1502 if (descriptor->mastering->max_luminance.den != 0)
1503 descriptor->mastering->has_luminance = 1;
1504 }
1505 }
1507 if (!descriptor->coll) {
1508 descriptor->coll = av_content_light_metadata_alloc(&descriptor->coll_size);
1509 if (!descriptor->coll)
1510 return AVERROR(ENOMEM);
1511 }
1513 descriptor->coll->MaxCLL = avio_rb16(pb);
1514 }
1516 descriptor->coll->MaxFALL = avio_rb16(pb);
1517 }
1518 }
1519
1521 return mxf_read_strong_ref_array(pb, &descriptor->sub_descriptors_refs, &descriptor->sub_descriptors_count);
1522
1523 break;
1524 }
1525 return 0;
1526}
1527
1528static int mxf_read_mca_sub_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1529{
1530 MXFMCASubDescriptor *mca_sub_descriptor = arg;
1531
1533 avio_read(pb, mca_sub_descriptor->mca_label_dictionary_id, 16);
1534
1536 avio_read(pb, mca_sub_descriptor->mca_link_id, 16);
1537
1539 avio_read(pb, mca_sub_descriptor->soundfield_group_link_id, 16);
1540
1543
1545 mca_sub_descriptor->mca_channel_id = avio_rb32(pb);
1546
1548 return mxf_read_us_ascii_string(pb, size, &mca_sub_descriptor->language);
1549
1550 return 0;
1551}
1552
1553static int mxf_read_ffv1_sub_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1554{
1555 MXFFFV1SubDescriptor *ffv1_sub_descriptor = arg;
1556
1558 if (ffv1_sub_descriptor->extradata)
1559 av_log(NULL, AV_LOG_WARNING, "Duplicate ffv1_extradata\n");
1560 av_free(ffv1_sub_descriptor->extradata);
1561 ffv1_sub_descriptor->extradata_size = 0;
1562 ffv1_sub_descriptor->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
1563 if (!ffv1_sub_descriptor->extradata)
1564 return AVERROR(ENOMEM);
1565 ffv1_sub_descriptor->extradata_size = size;
1566 avio_read(pb, ffv1_sub_descriptor->extradata, size);
1567 }
1568
1569 return 0;
1570}
1571
1572static int mxf_read_indirect_value(void *arg, AVIOContext *pb, int size)
1573{
1574 MXFTaggedValue *tagged_value = arg;
1575 uint8_t key[17];
1576 int ret;
1577
1578 if (size <= 17)
1579 return 0;
1580
1581 ret = ffio_read_size(pb, key, 17);
1582 if (ret < 0)
1583 return ret;
1584 /* TODO: handle other types of of indirect values */
1585 if (memcmp(key, mxf_indirect_value_utf16le, 17) == 0) {
1586 return mxf_read_utf16le_string(pb, size - 17, &tagged_value->value);
1587 } else if (memcmp(key, mxf_indirect_value_utf16be, 17) == 0) {
1588 return mxf_read_utf16be_string(pb, size - 17, &tagged_value->value);
1589 }
1590 return 0;
1591}
1592
1593static int mxf_read_tagged_value(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1594{
1595 MXFTaggedValue *tagged_value = arg;
1596 switch (tag){
1597 case 0x5001:
1598 return mxf_read_utf16be_string(pb, size, &tagged_value->name);
1599 case 0x5003:
1600 return mxf_read_indirect_value(tagged_value, pb, size);
1601 }
1602 return 0;
1603}
1604
1605/*
1606 * Match an uid independently of the version byte and up to len common bytes
1607 * Returns: boolean
1608 */
1609static int mxf_match_uid(const UID key, const uint8_t uid_prefix[], int len)
1610{
1611 int i;
1612 for (i = 0; i < len; i++) {
1613 if (i != 7 && key[i] != uid_prefix[i])
1614 return 0;
1615 }
1616 return 1;
1617}
1618
1619static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
1620{
1621 while (uls->uid[0]) {
1622 if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
1623 break;
1624 uls++;
1625 }
1626 return uls;
1627}
1628
1629static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
1630{
1632
1633 if (!strong_ref)
1634 return NULL;
1635 for (int i = mg->metadata_sets_count - 1; i >= 0; i--)
1636 if (!memcmp(*strong_ref, mg->metadata_sets[i]->uid, 16))
1637 return mg->metadata_sets[i];
1638
1639 return NULL;
1640}
1641
1643 // video essence container uls
1644 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 }, 14, AV_CODEC_ID_JPEG2000, NULL, 14, J2KWrap },
1645 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x10,0x60,0x01 }, 14, AV_CODEC_ID_H264, NULL, 15 }, /* H.264 */
1646 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x11,0x01,0x00 }, 14, AV_CODEC_ID_DNXHD, NULL, 14 }, /* VC-3 */
1647 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x1e,0x01,0x00 }, 14, AV_CODEC_ID_DNXUC, NULL, 14 }, /* DNxUncompressed / SMPTE RDD 50 */
1648 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x12,0x01,0x00 }, 14, AV_CODEC_ID_VC1, NULL, 14 }, /* VC-1 */
1649 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x14,0x01,0x00 }, 14, AV_CODEC_ID_TIFF, NULL, 14 }, /* TIFF */
1650 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x15,0x01,0x00 }, 14, AV_CODEC_ID_DIRAC, NULL, 14 }, /* VC-2 */
1651 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x1b,0x01,0x00 }, 14, AV_CODEC_ID_CFHD, NULL, 14 }, /* VC-5 */
1652 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x1c,0x01,0x00 }, 14, AV_CODEC_ID_PRORES, NULL, 14 }, /* ProRes */
1653 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, AV_CODEC_ID_MPEG2VIDEO, NULL, 15 }, /* MPEG-ES */
1654 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, 14, AV_CODEC_ID_MPEG2VIDEO, NULL, 15, D10D11Wrap }, /* SMPTE D-10 mapping */
1655 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x23,0x01,0x00 }, 14, AV_CODEC_ID_FFV1, NULL, 14 },
1656 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, AV_CODEC_ID_DVVIDEO, NULL, 15 }, /* DV 625 25mbps */
1657 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14, AV_CODEC_ID_RAWVIDEO, NULL, 15, RawVWrap }, /* uncompressed picture */
1658 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x01,0x01 }, 15, AV_CODEC_ID_HQ_HQA },
1659 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x02,0x01 }, 15, AV_CODEC_ID_HQX },
1660 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x15,0x00,0x04,0x02,0x10,0x00,0x01 }, 16, AV_CODEC_ID_HEVC, NULL, 15 }, /* Canon XF-HEVC */
1661 { { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0xff,0x4b,0x46,0x41,0x41,0x00,0x0d,0x4d,0x4f }, 14, AV_CODEC_ID_RAWVIDEO }, /* Legacy ?? Uncompressed Picture */
1662 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1663};
1664
1665/* EC ULs for intra-only formats */
1667 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x00,0x00 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MXF-GC SMPTE D-10 mappings */
1668 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1669};
1670
1671/* intra-only PictureEssenceCoding ULs, where no corresponding EC UL exists */
1673 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14, AV_CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra Profiles */
1674 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, 14, AV_CODEC_ID_JPEG2000 }, /* JPEG 2000 code stream */
1675 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1676};
1677
1678/* actual coded width for AVC-Intra to allow selecting correct SPS/PPS */
1680 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 16, 1440 },
1681 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 16, 1440 },
1682 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 16, 1440 },
1683 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 16, 1440 },
1684 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, 0 },
1685};
1686
1688 // sound essence container uls
1689 { { 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 }, /* BWF */
1690 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, AV_CODEC_ID_MP2, NULL, 15 }, /* MPEG-ES */
1691 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, AV_CODEC_ID_PCM_S16LE, NULL, 13 }, /* D-10 Mapping 50Mbps PAL Extended Template */
1692 { { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0xff,0x4b,0x46,0x41,0x41,0x00,0x0d,0x4d,0x4F }, 14, AV_CODEC_ID_PCM_S16LE }, /* 0001GL00.MXF.A1.mxf_opatom.mxf */
1693 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x03,0x04,0x02,0x02,0x02,0x03,0x03,0x01,0x00 }, 14, AV_CODEC_ID_AAC }, /* MPEG-2 AAC ADTS (legacy) */
1694 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x16,0x00,0x00 }, 14, AV_CODEC_ID_AAC, NULL, 14 }, /* AAC ADIF (SMPTE 381-4) */
1695 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x17,0x00,0x00 }, 14, AV_CODEC_ID_AAC, NULL, 14 }, /* AAC ADTS (SMPTE 381-4) */
1696 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x18,0x00,0x00 }, 14, AV_CODEC_ID_AAC, NULL, 14 }, /* AAC LATM/LOAS (SMPTE 381-4) */
1697 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1698};
1699
1701 { { 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 },
1702 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 }, 16, AV_CODEC_ID_SMPTE_436M_ANC, "vbi_vanc_smpte_436M", 11 },
1703 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x13,0x01,0x01 }, 16, AV_CODEC_ID_TTML },
1704 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1705};
1706
1712
1714 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00 }, AV_CHAN_FRONT_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left
1715 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x02,0x00,0x00,0x00,0x00 }, AV_CHAN_FRONT_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right
1716 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x03,0x00,0x00,0x00,0x00 }, AV_CHAN_FRONT_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Center
1717 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x04,0x00,0x00,0x00,0x00 }, AV_CHAN_LOW_FREQUENCY, AV_AUDIO_SERVICE_TYPE_MAIN }, // Low Frequency Effects
1718 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x05,0x00,0x00,0x00,0x00 }, AV_CHAN_SIDE_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Surround
1719 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x06,0x00,0x00,0x00,0x00 }, AV_CHAN_SIDE_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Surround
1720 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x07,0x00,0x00,0x00,0x00 }, AV_CHAN_SIDE_SURROUND_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Side Surround
1721 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x08,0x00,0x00,0x00,0x00 }, AV_CHAN_SIDE_SURROUND_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Side Surround
1722 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x09,0x00,0x00,0x00,0x00 }, AV_CHAN_BACK_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Rear Surround
1723 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x0a,0x00,0x00,0x00,0x00 }, AV_CHAN_BACK_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Rear Surround
1724 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x0b,0x00,0x00,0x00,0x00 }, AV_CHAN_FRONT_LEFT_OF_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Center
1725 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x0c,0x00,0x00,0x00,0x00 }, AV_CHAN_FRONT_RIGHT_OF_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Center
1726 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x0d,0x00,0x00,0x00,0x00 }, AV_CHAN_BACK_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Center Surround
1727 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x0e,0x00,0x00,0x00,0x00 }, AV_CHAN_FRONT_CENTER, AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED }, // Hearing impaired audio channel
1728 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x0f,0x00,0x00,0x00,0x00 }, AV_CHAN_FRONT_CENTER, AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED }, // Visually impaired narrative audio channel
1729 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x20,0x03,0x00,0x00,0x00 }, AV_CHAN_STEREO_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Total
1730 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x20,0x04,0x00,0x00,0x00 }, AV_CHAN_STEREO_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Total
1731 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x01,0x00,0x00 }, AV_CHAN_TOP_FRONT_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Height
1732 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x02,0x00,0x00 }, AV_CHAN_TOP_FRONT_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Height
1733 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x03,0x00,0x00 }, AV_CHAN_TOP_FRONT_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Center Height
1734 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x04,0x00,0x00 }, AV_CHAN_TOP_SURROUND_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Surround Height
1735 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x05,0x00,0x00 }, AV_CHAN_TOP_SURROUND_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Surround Height
1736 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x06,0x00,0x00 }, AV_CHAN_TOP_SIDE_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Side Surround Height
1737 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x07,0x00,0x00 }, AV_CHAN_TOP_SIDE_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Side Surround Height
1738 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x08,0x00,0x00 }, AV_CHAN_TOP_BACK_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Rear Surround Height
1739 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x09,0x00,0x00 }, AV_CHAN_TOP_BACK_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Rear Surround Height
1740 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x0a,0x00,0x00 }, AV_CHAN_TOP_SIDE_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Top Surround
1741 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x0b,0x00,0x00 }, AV_CHAN_TOP_SIDE_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Top Surround
1742 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x0c,0x00,0x00 }, AV_CHAN_TOP_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Top Surround
1743 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x0d,0x00,0x00 }, AV_CHAN_LOW_FREQUENCY, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Front Subwoofer
1744 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x0e,0x00,0x00 }, AV_CHAN_LOW_FREQUENCY_2, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Front Subwoofer
1745 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x0f,0x00,0x00 }, AV_CHAN_TOP_BACK_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Center Rear Height
1746 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x10,0x00,0x00 }, AV_CHAN_BACK_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Center Rear
1747 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x11,0x00,0x00 }, AV_CHAN_BOTTOM_FRONT_LEFT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Left Below
1748 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x12,0x00,0x00 }, AV_CHAN_BOTTOM_FRONT_RIGHT, AV_AUDIO_SERVICE_TYPE_MAIN }, // Right Below
1749 { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0d,0x03,0x02,0x01,0x30,0x01,0x13,0x00,0x00 }, AV_CHAN_BOTTOM_FRONT_CENTER, AV_AUDIO_SERVICE_TYPE_MAIN }, // Center Below
1750 { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_AUDIO_SERVICE_TYPE_NB },
1751};
1752
1753static MXFWrappingScheme mxf_get_wrapping_kind(UID *essence_container_ul)
1754{
1755 int val;
1756 const MXFCodecUL *codec_ul;
1757
1759 if (!codec_ul->uid[0])
1761 if (!codec_ul->uid[0])
1763 if (!codec_ul->uid[0] || !codec_ul->wrapping_indicator_pos)
1764 return UnknownWrapped;
1765
1766 val = (*essence_container_ul)[codec_ul->wrapping_indicator_pos];
1767 switch (codec_ul->wrapping_indicator_type) {
1768 case RawVWrap:
1769 val = val % 4;
1770 break;
1771 case RawAWrap:
1772 if (val == 0x03 || val == 0x04)
1773 val -= 0x02;
1774 break;
1775 case D10D11Wrap:
1776 if (val == 0x02)
1777 val = 0x01;
1778 break;
1779 case J2KWrap:
1780 if (val != 0x02)
1781 val = 0x01;
1782 break;
1783 }
1784 if (val == 0x01)
1785 return FrameWrapped;
1786 if (val == 0x02)
1787 return ClipWrapped;
1788 return UnknownWrapped;
1789}
1790
1791static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
1792{
1793 int i, j, nb_segments = 0;
1794 MXFIndexTableSegment **unsorted_segments;
1795 int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
1797
1798 /* count number of segments, allocate arrays and copy unsorted segments */
1799 nb_segments = mg->metadata_sets_count;
1800 if (!nb_segments)
1801 return AVERROR_INVALIDDATA;
1802
1803 if (!(unsorted_segments = av_calloc(nb_segments, sizeof(*unsorted_segments))) ||
1804 !(*sorted_segments = av_calloc(nb_segments, sizeof(**sorted_segments)))) {
1805 av_freep(sorted_segments);
1806 av_free(unsorted_segments);
1807 return AVERROR(ENOMEM);
1808 }
1809
1810 for (i = nb_segments = 0; i < mg->metadata_sets_count; i++) {
1811 MXFIndexTableSegment *s = (MXFIndexTableSegment*)mg->metadata_sets[i];
1812 if (s->edit_unit_byte_count || s->nb_index_entries)
1813 unsorted_segments[nb_segments++] = s;
1814 else
1815 av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment at %"PRId64" missing EditUnitByteCount and IndexEntryArray\n",
1816 s->index_sid, s->index_start_position);
1817 }
1818
1819 if (!nb_segments) {
1820 av_freep(sorted_segments);
1821 av_free(unsorted_segments);
1822 return AVERROR_INVALIDDATA;
1823 }
1824
1825 *nb_sorted_segments = 0;
1826
1827 /* sort segments by {BodySID, IndexSID, IndexStartPosition}, remove duplicates while we're at it */
1828 for (i = 0; i < nb_segments; i++) {
1829 int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
1830 uint64_t best_index_duration = 0;
1831
1832 for (j = 0; j < nb_segments; j++) {
1833 MXFIndexTableSegment *s = unsorted_segments[j];
1834
1835 /* Require larger BosySID, IndexSID or IndexStartPosition then the previous entry. This removes duplicates.
1836 * We want the smallest values for the keys than what we currently have, unless this is the first such entry this time around.
1837 * If we come across an entry with the same IndexStartPosition but larger IndexDuration, then we'll prefer it over the one we currently have.
1838 */
1839 if ((i == 0 ||
1840 s->body_sid > last_body_sid ||
1841 s->body_sid == last_body_sid && s->index_sid > last_index_sid ||
1842 s->body_sid == last_body_sid && s->index_sid == last_index_sid && s->index_start_position > last_index_start) &&
1843 (best == -1 ||
1844 s->body_sid < best_body_sid ||
1845 s->body_sid == best_body_sid && s->index_sid < best_index_sid ||
1846 s->body_sid == best_body_sid && s->index_sid == best_index_sid && s->index_start_position < best_index_start ||
1847 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)) {
1848 best = j;
1849 best_body_sid = s->body_sid;
1850 best_index_sid = s->index_sid;
1851 best_index_start = s->index_start_position;
1852 best_index_duration = s->index_duration;
1853 }
1854 }
1855
1856 /* no suitable entry found -> we're done */
1857 if (best == -1)
1858 break;
1859
1860 (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
1861 last_body_sid = best_body_sid;
1862 last_index_sid = best_index_sid;
1863 last_index_start = best_index_start;
1864 }
1865
1866 av_free(unsorted_segments);
1867
1868 return 0;
1869}
1870
1871/**
1872 * Computes the absolute file offset of the given essence container offset
1873 */
1874static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t offset, int64_t *offset_out, MXFPartition **partition_out)
1875{
1876 MXFPartition *last_p = NULL;
1877 int a, b, m, m0;
1878
1879 if (offset < 0)
1880 return AVERROR(EINVAL);
1881
1882 a = -1;
1883 b = mxf->partitions_count;
1884
1885 while (b - a > 1) {
1886 m0 = m = (a + b) >> 1;
1887
1888 while (m < b && mxf->partitions[m].body_sid != body_sid)
1889 m++;
1890
1891 if (m < b && mxf->partitions[m].body_offset <= offset)
1892 a = m;
1893 else
1894 b = m0;
1895 }
1896
1897 if (a >= 0)
1898 last_p = &mxf->partitions[a];
1899
1900 if (last_p && (!last_p->essence_length || last_p->essence_length > (offset - last_p->body_offset))) {
1901 *offset_out = last_p->essence_offset + (offset - last_p->body_offset);
1902 if (partition_out)
1903 *partition_out = last_p;
1904 return 0;
1905 }
1906
1907 av_log(mxf->fc, AV_LOG_ERROR,
1908 "failed to find absolute offset of %"PRIX64" in BodySID %i - partial file?\n",
1909 offset, body_sid);
1910
1911 return AVERROR_INVALIDDATA;
1912}
1913
1914/**
1915 * Returns the end position of the essence container with given BodySID, or zero if unknown
1916 */
1918{
1919 for (int x = mxf->partitions_count - 1; x >= 0; x--) {
1920 MXFPartition *p = &mxf->partitions[x];
1921
1922 if (p->body_sid != body_sid)
1923 continue;
1924
1925 if (!p->essence_length)
1926 return 0;
1927
1928 return p->essence_offset + p->essence_length;
1929 }
1930
1931 return 0;
1932}
1933
1934/* EditUnit -> absolute offset */
1935static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_table, int64_t edit_unit, AVRational edit_rate, int64_t *edit_unit_out, int64_t *offset_out, MXFPartition **partition_out, int nag)
1936{
1937 int i = 0, dir = 0;
1938 int64_t index_duration, index_end;
1939 MXFIndexTableSegment *first_segment, *last_segment;
1940
1941 if (!index_table->nb_segments) {
1942 av_log(mxf->fc, AV_LOG_ERROR, "no index table segments\n");
1943 return AVERROR_INVALIDDATA;
1944 }
1945
1946 edit_unit = av_rescale_q(edit_unit, index_table->segments[0]->index_edit_rate, edit_rate);
1947
1948 first_segment = index_table->segments[0];
1949 last_segment = index_table->segments[index_table->nb_segments - 1];
1950
1951 // clamp to actual range of index
1952 index_end = av_sat_add64(last_segment->index_start_position, last_segment->index_duration);
1953 edit_unit = FFMAX(FFMIN(edit_unit, index_end), first_segment->index_start_position);
1954 if (edit_unit < 0)
1955 return AVERROR_PATCHWELCOME;
1956
1957 // guess which table segment this edit unit is in
1958 // saturation is fine since it's just a guess
1959 // if the guess is wrong we revert to a linear search
1960 index_duration = av_sat_sub64(index_end, first_segment->index_start_position);
1961
1962 // compute the guess, taking care not to cause overflow or division by zero
1963 if (index_duration > 0 && edit_unit <= INT64_MAX / index_table->nb_segments) {
1964 // a simple linear guesstimate
1965 // this is accurate to within +-1 when partitions are generated at a constant rate like mxfenc does
1966 int64_t i64 = index_table->nb_segments * edit_unit / index_duration;
1967 // clamp and downcast to 32-bit
1968 i = FFMAX(0, FFMIN(index_table->nb_segments - 1, i64));
1969 }
1970
1971 for (; i >= 0 && i < index_table->nb_segments; i += dir) {
1972 MXFIndexTableSegment *s = index_table->segments[i];
1973
1974 if (s->index_start_position <= edit_unit && edit_unit < s->index_start_position + s->index_duration) {
1975 int64_t index = edit_unit - s->index_start_position;
1976 int64_t offset_temp = s->offset;
1977
1978 if (s->edit_unit_byte_count) {
1979 if (index > INT64_MAX / s->edit_unit_byte_count ||
1980 s->edit_unit_byte_count * index > INT64_MAX - offset_temp)
1981 return AVERROR_INVALIDDATA;
1982
1983 offset_temp += s->edit_unit_byte_count * index;
1984 } else {
1985 if (s->nb_index_entries == 2 * s->index_duration + 1)
1986 index *= 2; /* Avid index */
1987
1988 if (index < 0 || index >= s->nb_index_entries) {
1989 av_log(mxf->fc, AV_LOG_ERROR, "IndexSID %i segment at %"PRId64" IndexEntryArray too small\n",
1990 index_table->index_sid, s->index_start_position);
1991 return AVERROR_INVALIDDATA;
1992 }
1993
1994 offset_temp = s->stream_offset_entries[index];
1995 }
1996
1997 if (edit_unit_out)
1998 *edit_unit_out = av_rescale_q(edit_unit, edit_rate, s->index_edit_rate);
1999
2000 return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, offset_temp, offset_out, partition_out);
2001 } else if (dir == 0) {
2002 // scan backwards if the segment is earlier than the current IndexStartPosition
2003 // else scan forwards
2004 if (edit_unit < s->index_start_position) {
2005 dir = -1;
2006 } else {
2007 dir = 1;
2008 }
2009 }
2010 }
2011
2012 if (nag)
2013 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);
2014
2015 return AVERROR_INVALIDDATA;
2016}
2017
2019{
2020 int i, j, x;
2021 int8_t max_temporal_offset = -128;
2022 uint8_t *flags;
2023
2024 /* first compute how many entries we have */
2025 for (i = 0; i < index_table->nb_segments; i++) {
2026 MXFIndexTableSegment *s = index_table->segments[i];
2027
2028 if (!s->nb_index_entries) {
2029 index_table->nb_ptses = 0;
2030 return 0; /* no TemporalOffsets */
2031 }
2032
2033 if (s->index_duration > INT_MAX - index_table->nb_ptses) {
2034 index_table->nb_ptses = 0;
2035 av_log(mxf->fc, AV_LOG_ERROR, "ignoring IndexSID %d, duration is too large\n", s->index_sid);
2036 return 0;
2037 }
2038
2039 if (s->nb_index_entries != s->index_duration &&
2040 s->nb_index_entries != s->index_duration + 1 && /* Avid index */
2041 s->nb_index_entries != s->index_duration * 2 + 1) {
2042 index_table->nb_ptses = 0;
2043 av_log(mxf->fc, AV_LOG_ERROR, "ignoring IndexSID %d, duration does not match nb_index_entries\n", s->index_sid);
2044 return 0;
2045 }
2046
2047 index_table->nb_ptses += s->index_duration;
2048 }
2049
2050 /* paranoid check */
2051 if (index_table->nb_ptses <= 0)
2052 return 0;
2053
2054 if (!(index_table->ptses = av_malloc_array(index_table->nb_ptses, sizeof(int64_t))) ||
2055 !(index_table->fake_index = av_calloc(index_table->nb_ptses, sizeof(AVIndexEntry))) ||
2056 !(index_table->offsets = av_malloc_array(index_table->nb_ptses, sizeof(int8_t))) ||
2057 !(flags = av_malloc_array(index_table->nb_ptses, sizeof(uint8_t)))) {
2058 av_freep(&index_table->ptses);
2059 av_freep(&index_table->fake_index);
2060 av_freep(&index_table->offsets);
2061 return AVERROR(ENOMEM);
2062 }
2063
2064 /* we may have a few bad TemporalOffsets
2065 * make sure the corresponding PTSes don't have the bogus value 0 */
2066 for (x = 0; x < index_table->nb_ptses; x++)
2067 index_table->ptses[x] = AV_NOPTS_VALUE;
2068
2069 /**
2070 * We have this:
2071 *
2072 * x TemporalOffset
2073 * 0: 0
2074 * 1: 1
2075 * 2: 1
2076 * 3: -2
2077 * 4: 1
2078 * 5: 1
2079 * 6: -2
2080 *
2081 * We want to transform it into this:
2082 *
2083 * x DTS PTS
2084 * 0: -1 0
2085 * 1: 0 3
2086 * 2: 1 1
2087 * 3: 2 2
2088 * 4: 3 6
2089 * 5: 4 4
2090 * 6: 5 5
2091 *
2092 * We do this by bucket sorting x by x+TemporalOffset[x] into mxf->ptses,
2093 * then settings ffstream(mxf)->first_dts = -max(TemporalOffset[x]).
2094 * The latter makes DTS <= PTS.
2095 */
2096 for (i = x = 0; i < index_table->nb_segments; i++) {
2097 MXFIndexTableSegment *s = index_table->segments[i];
2098 int index_delta = 1;
2099 int n = s->nb_index_entries;
2100
2101 if (s->nb_index_entries == 2 * s->index_duration + 1)
2102 index_delta = 2; /* Avid index */
2103 if (s->nb_index_entries == index_delta * s->index_duration + 1)
2104 /* ignore the last entry - it's the size of the essence container in Avid */
2105 n--;
2106
2107 for (j = 0; j < n; j += index_delta, x++) {
2108 int offset = s->temporal_offset_entries[j] / index_delta;
2109 int index = x + offset;
2110
2111 if (x >= index_table->nb_ptses) {
2112 av_log(mxf->fc, AV_LOG_ERROR,
2113 "x >= nb_ptses - IndexEntryCount %i < IndexDuration %"PRId64"?\n",
2114 s->nb_index_entries, s->index_duration);
2115 break;
2116 }
2117
2118 flags[x] = !(s->flag_entries[j] & 0x30) ? AVINDEX_KEYFRAME : 0;
2119
2120 if (index < 0 || index >= index_table->nb_ptses) {
2121 av_log(mxf->fc, AV_LOG_ERROR,
2122 "index entry %i + TemporalOffset %i = %i, which is out of bounds\n",
2123 x, offset, index);
2124 continue;
2125 }
2126
2127 index_table->offsets[x] = offset;
2128 index_table->ptses[index] = x;
2129 max_temporal_offset = FFMAX(max_temporal_offset, offset);
2130 }
2131 }
2132
2133 /* calculate the fake index table in display order */
2134 for (x = 0; x < index_table->nb_ptses; x++) {
2135 index_table->fake_index[x].timestamp = x;
2136 if (index_table->ptses[x] != AV_NOPTS_VALUE)
2137 index_table->fake_index[index_table->ptses[x]].flags = flags[x];
2138 }
2139 av_freep(&flags);
2140
2141 index_table->first_dts = -max_temporal_offset;
2142
2143 return 0;
2144}
2145
2146/**
2147 * Sorts and collects index table segments into index tables.
2148 * Also computes PTSes if possible.
2149 */
2151{
2152 int ret, nb_sorted_segments;
2153 MXFIndexTableSegment **sorted_segments = NULL;
2154
2155 if ((ret = mxf_get_sorted_table_segments(mxf, &nb_sorted_segments, &sorted_segments)) ||
2156 nb_sorted_segments <= 0) {
2157 av_log(mxf->fc, AV_LOG_WARNING, "broken or empty index\n");
2158 return 0;
2159 }
2160
2161 /* sanity check and count unique BodySIDs/IndexSIDs */
2162 for (int i = 0; i < nb_sorted_segments; i++) {
2163 if (i == 0 || sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid)
2164 mxf->nb_index_tables++;
2165 else if (sorted_segments[i-1]->body_sid != sorted_segments[i]->body_sid) {
2166 av_log(mxf->fc, AV_LOG_ERROR, "found inconsistent BodySID\n");
2168 goto finish_decoding_index;
2169 }
2170 }
2171
2173 sizeof(*mxf->index_tables));
2174 if (!mxf->index_tables) {
2175 av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate index tables\n");
2176 ret = AVERROR(ENOMEM);
2177 goto finish_decoding_index;
2178 }
2179
2180 /* distribute sorted segments to index tables */
2181 for (int i = 0, j = 0; i < nb_sorted_segments; i++) {
2182 if (i != 0 && sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid) {
2183 /* next IndexSID */
2184 j++;
2185 }
2186
2187 mxf->index_tables[j].nb_segments++;
2188 }
2189
2190 for (int i = 0, j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) {
2191 MXFIndexTable *t = &mxf->index_tables[j];
2192 MXFTrack *mxf_track = NULL;
2193 int64_t offset_temp = 0;
2194
2195 t->segments = av_calloc(t->nb_segments, sizeof(*t->segments));
2196 if (!t->segments) {
2197 av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate IndexTableSegment"
2198 " pointer array\n");
2199 ret = AVERROR(ENOMEM);
2200 goto finish_decoding_index;
2201 }
2202
2203 if (sorted_segments[i]->index_start_position)
2204 av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i starts at EditUnit %"PRId64" - seeking may not work as expected\n",
2205 sorted_segments[i]->index_sid, sorted_segments[i]->index_start_position);
2206
2207 memcpy(t->segments, &sorted_segments[i], t->nb_segments * sizeof(MXFIndexTableSegment*));
2208 t->index_sid = sorted_segments[i]->index_sid;
2209 t->body_sid = sorted_segments[i]->body_sid;
2210
2211 if ((ret = mxf_compute_ptses_fake_index(mxf, t)) < 0)
2212 goto finish_decoding_index;
2213
2214 for (int k = 0; k < mxf->fc->nb_streams; k++) {
2215 MXFTrack *track = mxf->fc->streams[k]->priv_data;
2216 if (track && track->index_sid == t->index_sid) {
2217 mxf_track = track;
2218 break;
2219 }
2220 }
2221
2222 /* fix zero IndexDurations and compute segment offsets */
2223 for (int k = 0; k < t->nb_segments; k++) {
2225
2226 if (!t->segments[k]->index_edit_rate.num || !t->segments[k]->index_edit_rate.den) {
2227 av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has invalid IndexEditRate\n",
2228 t->index_sid, k);
2229 if (mxf_track)
2230 t->segments[k]->index_edit_rate = mxf_track->edit_rate;
2231 }
2232
2233 s->offset = offset_temp;
2234
2235 /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */
2236 if (s->edit_unit_byte_count && (s->index_duration > INT64_MAX / s->edit_unit_byte_count ||
2237 s->edit_unit_byte_count * s->index_duration > INT64_MAX - offset_temp)) {
2239 goto finish_decoding_index;
2240 }
2241
2242 offset_temp += t->segments[k]->edit_unit_byte_count * t->segments[k]->index_duration;
2243
2244 if (t->segments[k]->index_duration)
2245 continue;
2246
2247 if (t->nb_segments > 1)
2248 av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has zero IndexDuration and there's more than one segment\n",
2249 t->index_sid, k);
2250
2251 if (!mxf_track) {
2252 av_log(mxf->fc, AV_LOG_WARNING, "no streams?\n");
2253 break;
2254 }
2255
2256 /* assume the first stream's duration is reasonable
2257 * leave index_duration = 0 on further segments in case we have any (unlikely)
2258 */
2259 t->segments[k]->index_duration = mxf_track->original_duration;
2260 break;
2261 }
2262 }
2263
2264 ret = 0;
2265finish_decoding_index:
2266 av_free(sorted_segments);
2267 return ret;
2268}
2269
2270static int mxf_is_st_422(const UID *essence_container_ul) {
2271 static const uint8_t st_422_essence_container_ul[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c };
2272
2273 return essence_container_ul && mxf_match_uid(*essence_container_ul, st_422_essence_container_ul,
2274 sizeof(st_422_essence_container_ul));
2275}
2276
2284
2285static void mxf_umid_to_str(const UID ul, const UID uid,
2286 char str[2 + sizeof(UID) * 4 + 1])
2287{
2288 snprintf(str, 2 + sizeof(UID) * 4 + 1, "0x");
2289 ff_data_to_hex(str + 2, ul, sizeof(UID), 0);
2290 ff_data_to_hex(str + 2 + 2 * sizeof(UID), uid, sizeof(UID), 0);
2291}
2292
2293static int mxf_version_to_str(uint16_t major, uint16_t minor, uint16_t tertiary,
2294 uint16_t patch, uint16_t release, char **str)
2295{
2296 *str = av_asprintf("%d.%d.%d.%d.%d", major, minor, tertiary, patch, release);
2297 if (!*str)
2298 return AVERROR(ENOMEM);
2299 return 0;
2300}
2301
2302static int mxf_add_umid_metadata(AVDictionary **pm, const char *key, MXFPackage* package)
2303{
2304 char str[2 + 4 * sizeof(UID) + 1];
2305 if (!package)
2306 return 0;
2307 mxf_umid_to_str(package->package_ul, package->package_uid, str);
2308 av_dict_set(pm, key, str, 0);
2309 return 0;
2310}
2311
2312static int mxf_add_timecode_metadata(AVDictionary **pm, const char *key, AVTimecode *tc)
2313{
2314 char buf[AV_TIMECODE_STR_SIZE];
2315 av_dict_set(pm, key, av_timecode_make_string(tc, buf, 0), 0);
2316
2317 return 0;
2318}
2319
2321{
2322 MXFTimecodeComponent *timecode;
2323 MXFPulldownComponent *pulldown;
2324
2325 timecode = mxf_resolve_strong_ref(mxf, strong_ref, TimecodeComponent);
2326 if (timecode)
2327 return timecode;
2328
2329 pulldown = mxf_resolve_strong_ref(mxf, strong_ref, PulldownComponent);
2330 if (pulldown)
2332
2333 return NULL;
2334}
2335
2336static MXFPackage* mxf_resolve_source_package(MXFContext *mxf, UID package_ul, UID package_uid)
2337{
2338 MXFPackage *package = NULL;
2339 int i;
2340
2341 for (i = 0; i < mxf->packages_count; i++) {
2342 package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], SourcePackage);
2343 if (!package)
2344 continue;
2345
2346 if (!memcmp(package->package_ul, package_ul, 16) && !memcmp(package->package_uid, package_uid, 16))
2347 return package;
2348 }
2349 return NULL;
2350}
2351
2352static MXFDescriptor* mxf_resolve_descriptor(MXFContext *mxf, UID *strong_ref, int track_id)
2353{
2354 MXFDescriptor *descriptor = mxf_resolve_strong_ref(mxf, strong_ref, Descriptor);
2355 if (descriptor)
2356 return descriptor;
2357
2358 descriptor = mxf_resolve_strong_ref(mxf, strong_ref, MultipleDescriptor);
2359 if (descriptor) {
2360 for (int i = 0; i < descriptor->file_descriptors_count; i++) {
2361 MXFDescriptor *file_descriptor = mxf_resolve_strong_ref(mxf, &descriptor->file_descriptors_refs[i], Descriptor);
2362
2363 if (!file_descriptor) {
2364 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve file descriptor strong ref\n");
2365 continue;
2366 }
2367 if (file_descriptor->linked_track_id == track_id) {
2368 return file_descriptor;
2369 }
2370 }
2371 }
2372
2373 return NULL;
2374}
2375
2377{
2378 MXFStructuralComponent *component = NULL;
2379 MXFPackage *package = NULL;
2380 MXFDescriptor *descriptor = NULL;
2381 MXFEssenceGroup *essence_group;
2382 int i;
2383
2384 component = mxf_resolve_strong_ref(mxf, strong_ref, SourceClip);
2385 if (component)
2386 return component;
2387
2388 essence_group = mxf_resolve_strong_ref(mxf, strong_ref, EssenceGroup);
2389 if (!essence_group)
2390 return NULL;
2391
2392 /* essence groups contains multiple representations of the same media,
2393 this return the first components with a valid Descriptor typically index 0 */
2394 for (i =0; i < essence_group->structural_components_count; i++){
2395 component = mxf_resolve_strong_ref(mxf, &essence_group->structural_components_refs[i], SourceClip);
2396 if (!component)
2397 continue;
2398
2399 if (!(package = mxf_resolve_source_package(mxf, component->source_package_ul, component->source_package_uid)))
2400 continue;
2401
2402 descriptor = mxf_resolve_strong_ref(mxf, &package->descriptor_ref, Descriptor);
2403 if (descriptor)
2404 return component;
2405 }
2406
2407 return NULL;
2408}
2409
2411{
2413 int i;
2414 char *key = NULL;
2415
2416 for (i = 0; i < package->comment_count; i++) {
2418 if (!tag || !tag->name || !tag->value)
2419 continue;
2420
2421 key = av_asprintf("comment_%s", tag->name);
2422 if (!key)
2423 return AVERROR(ENOMEM);
2424
2426 }
2427 return 0;
2428}
2429
2431{
2432 MXFPackage *physical_package = NULL;
2433 MXFTrack *physical_track = NULL;
2434 MXFStructuralComponent *sourceclip = NULL;
2435 MXFTimecodeComponent *mxf_tc = NULL;
2436 int i, j, k;
2437 AVTimecode tc;
2438 int flags;
2439 int64_t start_position;
2440
2441 for (i = 0; i < source_track->sequence->structural_components_count; i++) {
2442 sourceclip = mxf_resolve_strong_ref(mxf, &source_track->sequence->structural_components_refs[i], SourceClip);
2443 if (!sourceclip)
2444 continue;
2445
2446 if (!(physical_package = mxf_resolve_source_package(mxf, sourceclip->source_package_ul, sourceclip->source_package_uid)))
2447 break;
2448
2449 mxf_add_umid_metadata(&st->metadata, "reel_umid", physical_package);
2450
2451 /* the name of physical source package is name of the reel or tape */
2452 if (physical_package->name && physical_package->name[0])
2453 av_dict_set(&st->metadata, "reel_name", physical_package->name, 0);
2454
2455 /* the source timecode is calculated by adding the start_position of the sourceclip from the file source package track
2456 * to the start_frame of the timecode component located on one of the tracks of the physical source package.
2457 */
2458 for (j = 0; j < physical_package->tracks_count; j++) {
2459 if (!(physical_track = mxf_resolve_strong_ref(mxf, &physical_package->tracks_refs[j], Track))) {
2460 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
2461 continue;
2462 }
2463
2464 if (!(physical_track->sequence = mxf_resolve_strong_ref(mxf, &physical_track->sequence_ref, Sequence))) {
2465 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
2466 continue;
2467 }
2468
2469 if (physical_track->edit_rate.num <= 0 ||
2470 physical_track->edit_rate.den <= 0) {
2471 av_log(mxf->fc, AV_LOG_WARNING,
2472 "Invalid edit rate (%d/%d) found on structural"
2473 " component #%d, defaulting to 25/1\n",
2474 physical_track->edit_rate.num,
2475 physical_track->edit_rate.den, i);
2476 physical_track->edit_rate = (AVRational){25, 1};
2477 }
2478
2479 for (k = 0; k < physical_track->sequence->structural_components_count; k++) {
2480 if (!(mxf_tc = mxf_resolve_timecode_component(mxf, &physical_track->sequence->structural_components_refs[k])))
2481 continue;
2482
2483 flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
2484 /* scale sourceclip start_position to match physical track edit rate */
2485 start_position = av_rescale_q(sourceclip->start_position,
2486 physical_track->edit_rate,
2487 source_track->edit_rate);
2488
2489 if (av_sat_add64(start_position, mxf_tc->start_frame) != start_position + (uint64_t)mxf_tc->start_frame)
2490 return AVERROR_INVALIDDATA;
2491
2492 if (av_timecode_init(&tc, mxf_tc->rate, flags, start_position + mxf_tc->start_frame, mxf->fc) == 0) {
2493 mxf_add_timecode_metadata(&st->metadata, "timecode", &tc);
2494 return 0;
2495 }
2496 }
2497 }
2498 }
2499
2500 return 0;
2501}
2502
2504{
2505 MXFStructuralComponent *component = NULL;
2506 const MXFCodecUL *codec_ul = NULL;
2507 MXFPackage tmp_package;
2508 AVStream *st;
2509 int j;
2510
2511 for (j = 0; j < track->sequence->structural_components_count; j++) {
2512 component = mxf_resolve_sourceclip(mxf, &track->sequence->structural_components_refs[j]);
2513 if (!component)
2514 continue;
2515 break;
2516 }
2517 if (!component)
2518 return 0;
2519
2520 st = avformat_new_stream(mxf->fc, NULL);
2521 if (!st) {
2522 av_log(mxf->fc, AV_LOG_ERROR, "could not allocate metadata stream\n");
2523 return AVERROR(ENOMEM);
2524 }
2525
2528 st->id = track->track_id;
2529
2530 memcpy(&tmp_package.package_ul, component->source_package_ul, 16);
2531 memcpy(&tmp_package.package_uid, component->source_package_uid, 16);
2532 mxf_add_umid_metadata(&st->metadata, "file_package_umid", &tmp_package);
2533 if (track->name && track->name[0])
2534 av_dict_set(&st->metadata, "track_name", track->name, 0);
2535
2537 av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
2538 return 0;
2539}
2540
2542{
2543 unsigned depth = descriptor->component_depth;
2544 unsigned black = descriptor->black_ref_level;
2545 unsigned white = descriptor->white_ref_level;
2546 unsigned range = descriptor->color_range;
2547
2548 /* CDCI range metadata */
2549 if (!descriptor->range_items || !depth)
2551
2552 if (depth < 31) {
2553 unsigned def_black, def_white, def_range;
2554
2555 /* defaults for absent items, SMPTE ST 377-1 G.2.32 to G.2.34 */
2556 if (descriptor->signal_standard >= 1 && descriptor->signal_standard <= 6 && depth >= 8) {
2557 /* the interface standards of G.2.3 all use these levels */
2558 def_black = 16 << (depth - 8);
2559 def_white = 235 << (depth - 8);
2560 def_range = (224 << (depth - 8)) + 1;
2561 } else {
2562 def_black = depth == 8 ? 16 : depth == 10 ? 64 : 0;
2563 def_white = depth == 8 ? 235 : depth == 10 ? 940 : (1U << depth) - 1;
2564 def_range = (1U << depth) - 1;
2565 }
2566 if (!(descriptor->range_items & MXF_BLACK_REF_LEVEL_PRESENT))
2567 black = def_black;
2568 if (!(descriptor->range_items & MXF_WHITE_REF_LEVEL_PRESENT))
2569 white = def_white;
2570 if (!(descriptor->range_items & MXF_COLOR_RANGE_PRESENT))
2571 range = def_range;
2572
2573 if (black == 0 && white == (1U << depth) - 1 &&
2574 (range == 1U << depth || range == (1U << depth) - 1))
2575 return AVCOL_RANGE_JPEG;
2576 if (depth >= 8 &&
2577 black == 16 << (depth - 8) &&
2578 white == 235 << (depth - 8) &&
2579 range == (224 << (depth - 8)) + 1)
2580 return AVCOL_RANGE_MPEG;
2581 }
2582 avpriv_request_sample(mxf->fc, "Unrecognized CDCI color range (color diff range %u, b %u, w %u, depth %u)",
2583 range, black, white, depth);
2584
2586}
2587
2588static int is_pcm(enum AVCodecID codec_id)
2589{
2590 /* we only care about "normal" PCM codecs until we get samples */
2592}
2593
2594static int set_language(AVFormatContext *s, const char *rfc5646, AVDictionary **met)
2595{
2596 // language abbr should contain at least 2 chars
2597 if (rfc5646 && strlen(rfc5646) > 1) {
2598 char primary_tag[4] =
2599 {rfc5646[0], rfc5646[1], rfc5646[2] != '-' ? rfc5646[2] : '\0', '\0'};
2600
2601 const char *iso6392 = ff_convert_lang_to(primary_tag,
2603 if (iso6392)
2604 return(av_dict_set(met, "language", iso6392, 0));
2605 }
2606 return 0;
2607}
2608
2610{
2612 for (int k = 0; k < mg->metadata_sets_count; k++) {
2613 MXFMCASubDescriptor *group = (MXFMCASubDescriptor*)mg->metadata_sets[k];
2614 if (!memcmp(&group->mca_link_id, mca_link_id, 16))
2615 return group;
2616 }
2617 return NULL;
2618}
2619
2620static void parse_ffv1_sub_descriptor(MXFContext *mxf, MXFTrack *source_track, MXFDescriptor *descriptor, AVStream *st)
2621{
2622 for (int i = 0; i < descriptor->sub_descriptors_count; i++) {
2623 MXFFFV1SubDescriptor *ffv1_sub_descriptor = mxf_resolve_strong_ref(mxf, &descriptor->sub_descriptors_refs[i], FFV1SubDescriptor);
2624 if (ffv1_sub_descriptor == NULL)
2625 continue;
2626
2627 descriptor->extradata = ffv1_sub_descriptor->extradata;
2628 descriptor->extradata_size = ffv1_sub_descriptor->extradata_size;
2629 ffv1_sub_descriptor->extradata = NULL;
2630 ffv1_sub_descriptor->extradata_size = 0;
2631 break;
2632 }
2633}
2634
2635static int parse_mca_labels(MXFContext *mxf, MXFTrack *source_track, MXFDescriptor *descriptor, AVStream *st)
2636{
2637 AVChannelLayout *ch_layout = &st->codecpar->ch_layout;
2638 char *language = NULL;
2639 int ambigous_language = 0;
2640 enum AVAudioServiceType service_type = AV_AUDIO_SERVICE_TYPE_NB;
2641 int ambigous_service_type = 0;
2642 int ret;
2643
2644 for (int i = 0; i < descriptor->sub_descriptors_count; i++) {
2645 char *channel_language;
2646
2648 if (label == NULL)
2649 continue;
2650
2651 if (ch_layout->order == AV_CHANNEL_ORDER_UNSPEC) {
2652 av_channel_layout_uninit(ch_layout);
2653 ret = av_channel_layout_custom_init(ch_layout, descriptor->channels);
2654 if (ret < 0)
2655 return ret;
2656 }
2657
2658 for (const MXFChannelOrderingUL* channel_ordering = mxf_channel_ordering; channel_ordering->uid[0]; channel_ordering++) {
2659 if (IS_KLV_KEY(channel_ordering->uid, label->mca_label_dictionary_id)) {
2660 int target_channel = label->mca_channel_id;
2661 if (target_channel == 0 && descriptor->channels == 1)
2662 target_channel = 1;
2663 if (target_channel <= 0 || target_channel > descriptor->channels) {
2664 av_log(mxf->fc, AV_LOG_ERROR, "AudioChannelLabelSubDescriptor has invalid MCA channel ID %d\n", target_channel);
2665 return AVERROR_INVALIDDATA;
2666 }
2667 ch_layout->u.map[target_channel - 1].id = channel_ordering->channel;
2668 if (service_type == AV_AUDIO_SERVICE_TYPE_NB)
2669 service_type = channel_ordering->service_type;
2670 else if (service_type != channel_ordering->service_type)
2671 ambigous_service_type = 1;
2672 break;
2673 }
2674 }
2675
2676 channel_language = label->language;
2677 if (!channel_language) {
2678 MXFMCASubDescriptor *group = find_mca_link_id(mxf, SoundfieldGroupLabelSubDescriptor, &label->soundfield_group_link_id);
2679 if (group) {
2680 channel_language = group->language;
2681 if (!channel_language && group->group_of_soundfield_groups_link_id_count) {
2684 if (supergroup)
2685 channel_language = supergroup->language;
2686 }
2687 }
2688 }
2689 if (channel_language) {
2690 if (language && strcmp(language, channel_language))
2691 ambigous_language = 1;
2692 else
2693 language = channel_language;
2694 }
2695 }
2696
2697 if (language && !ambigous_language) {
2698 ret = set_language(mxf->fc, language, &st->metadata);
2699 if (ret < 0)
2700 return ret;
2701 }
2702
2703 if (service_type != AV_AUDIO_SERVICE_TYPE_NB && service_type != AV_AUDIO_SERVICE_TYPE_MAIN && !ambigous_service_type) {
2704 enum AVAudioServiceType *ast;
2708 sizeof(*ast), 0);
2709 if (!side_data)
2710 return AVERROR(ENOMEM);
2711 ast = (enum AVAudioServiceType*)side_data->data;
2712 *ast = service_type;
2713 }
2714
2716 if (ret < 0)
2717 return ret;
2718
2719 return 0;
2720}
2721
2723{
2724 MXFPackage *material_package = NULL;
2725 int k, ret;
2726
2727 /* TODO: handle multiple material packages (OP3x) */
2728 for (int i = 0; i < mxf->packages_count; i++) {
2729 material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
2730 if (material_package) break;
2731 }
2732 if (!material_package) {
2733 av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
2734 return AVERROR_INVALIDDATA;
2735 }
2736
2737 mxf_add_umid_metadata(&mxf->fc->metadata, "material_package_umid", material_package);
2738 if (material_package->name && material_package->name[0])
2739 av_dict_set(&mxf->fc->metadata, "material_package_name", material_package->name, 0);
2740 mxf_parse_package_comments(mxf, &mxf->fc->metadata, material_package);
2741
2742 for (int i = 0; i < material_package->tracks_count; i++) {
2743 MXFPackage *source_package = NULL;
2744 MXFTrack *material_track = NULL;
2745 MXFTrack *source_track = NULL;
2746 MXFTrack *temp_track = NULL;
2747 MXFDescriptor *descriptor = NULL;
2748 MXFStructuralComponent *component = NULL;
2749 MXFTimecodeComponent *mxf_tc = NULL;
2750 UID *essence_container_ul = NULL;
2751 const MXFCodecUL *codec_ul = NULL;
2752 const MXFCodecUL *container_ul = NULL;
2753 const MXFCodecUL *pix_fmt_ul = NULL;
2754 AVStream *st;
2755 FFStream *sti;
2756 AVTimecode tc;
2757 int flags;
2758
2759 if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
2760 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
2761 continue;
2762 }
2763
2764 if ((component = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, TimecodeComponent))) {
2765 mxf_tc = (MXFTimecodeComponent*)component;
2766 flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
2767 if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
2768 mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
2769 }
2770 }
2771
2772 if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
2773 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
2774 continue;
2775 }
2776
2777 for (int j = 0; j < material_track->sequence->structural_components_count; j++) {
2778 component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], TimecodeComponent);
2779 if (!component)
2780 continue;
2781
2782 mxf_tc = (MXFTimecodeComponent*)component;
2783 flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
2784 if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
2785 mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
2786 break;
2787 }
2788 }
2789
2790 /* TODO: handle multiple source clips, only finds first valid source clip */
2791 if(material_track->sequence->structural_components_count > 1)
2792 av_log(mxf->fc, AV_LOG_WARNING, "material track %d: has %d components\n",
2793 material_track->track_id, material_track->sequence->structural_components_count);
2794
2795 for (int j = 0; j < material_track->sequence->structural_components_count; j++) {
2796 component = mxf_resolve_sourceclip(mxf, &material_track->sequence->structural_components_refs[j]);
2797 if (!component)
2798 continue;
2799
2800 source_package = mxf_resolve_source_package(mxf, component->source_package_ul, component->source_package_uid);
2801 if (!source_package) {
2802 av_log(mxf->fc, AV_LOG_TRACE, "material track %d: no corresponding source package found\n", material_track->track_id);
2803 continue;
2804 }
2805 for (k = 0; k < source_package->tracks_count; k++) {
2806 if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
2807 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
2809 goto fail_and_free;
2810 }
2811 if (temp_track->track_id == component->source_track_id) {
2812 source_track = temp_track;
2813 break;
2814 }
2815 }
2816 if (!source_track) {
2817 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
2818 break;
2819 }
2820
2821 for (k = 0; k < mxf->essence_container_data_count; k++) {
2822 MXFEssenceContainerData *essence_data;
2823
2824 if (!(essence_data = mxf_resolve_strong_ref(mxf, &mxf->essence_container_data_refs[k], EssenceContainerData))) {
2825 av_log(mxf->fc, AV_LOG_TRACE, "could not resolve essence container data strong ref\n");
2826 continue;
2827 }
2828 if (!memcmp(component->source_package_ul, essence_data->package_ul, sizeof(UID)) && !memcmp(component->source_package_uid, essence_data->package_uid, sizeof(UID))) {
2829 source_track->body_sid = essence_data->body_sid;
2830 source_track->index_sid = essence_data->index_sid;
2831 break;
2832 }
2833 }
2834
2835 if(source_track && component)
2836 break;
2837 }
2838 if (!source_track || !component || !source_package) {
2839 if((ret = mxf_add_metadata_stream(mxf, material_track)))
2840 goto fail_and_free;
2841 continue;
2842 }
2843
2844 if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
2845 av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
2847 goto fail_and_free;
2848 }
2849
2850 /* 0001GL00.MXF.A1.mxf_opatom.mxf has the same SourcePackageID as 0001GL.MXF.V1.mxf_opatom.mxf
2851 * This would result in both files appearing to have two streams. Work around this by sanity checking DataDefinition */
2852 if (memcmp(material_track->sequence->data_definition_ul, source_track->sequence->data_definition_ul, 16)) {
2853 av_log(mxf->fc, AV_LOG_ERROR, "material track %d: DataDefinition mismatch\n", material_track->track_id);
2854 continue;
2855 }
2856
2857 st = avformat_new_stream(mxf->fc, NULL);
2858 if (!st) {
2859 av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
2860 ret = AVERROR(ENOMEM);
2861 goto fail_and_free;
2862 }
2863 sti = ffstream(st);
2864 st->id = material_track->track_id;
2865 st->priv_data = source_track;
2866
2867 descriptor = mxf_resolve_descriptor(mxf, &source_package->descriptor_ref, source_track->track_id);
2868
2869 /* A SourceClip from a EssenceGroup may only be a single frame of essence data. The clips duration is then how many
2870 * frames its suppose to repeat for. Descriptor->duration, if present, contains the real duration of the essence data */
2871 if (descriptor && descriptor->duration != AV_NOPTS_VALUE)
2872 source_track->original_duration = st->duration = FFMIN(descriptor->duration, component->duration);
2873 else
2874 source_track->original_duration = st->duration = component->duration;
2875
2876 if (st->duration == -1)
2878 st->start_time = component->start_position;
2879 if (material_track->edit_rate.num <= 0 ||
2880 material_track->edit_rate.den <= 0) {
2881 av_log(mxf->fc, AV_LOG_WARNING,
2882 "Invalid edit rate (%d/%d) found on stream #%d, "
2883 "defaulting to 25/1\n",
2884 material_track->edit_rate.num,
2885 material_track->edit_rate.den, st->index);
2886 material_track->edit_rate = (AVRational){25, 1};
2887 }
2888 avpriv_set_pts_info(st, 64, material_track->edit_rate.den, material_track->edit_rate.num);
2889
2890 /* ensure SourceTrack EditRate == MaterialTrack EditRate since only
2891 * the former is accessible via st->priv_data */
2892 source_track->edit_rate = material_track->edit_rate;
2893
2894 PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
2896 st->codecpar->codec_type = codec_ul->id;
2897
2898 if (!descriptor) {
2899 av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
2900 continue;
2901 }
2902 PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
2903 PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
2904 essence_container_ul = &descriptor->essence_container_ul;
2905 source_track->wrapping = (mxf->op == OPAtom) ? ClipWrapped : mxf_get_wrapping_kind(essence_container_ul);
2906 if (source_track->wrapping == UnknownWrapped)
2907 av_log(mxf->fc, AV_LOG_INFO, "wrapping of stream %d is unknown\n", st->index);
2908 /* HACK: replacing the original key with mxf_encrypted_essence_container
2909 * is not allowed according to s429-6, try to find correct information anyway */
2910 if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
2912 av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
2913 if (mg->metadata_sets_count) {
2914 MXFMetadataSet *metadata = mg->metadata_sets[0];
2915 essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
2916 }
2917 }
2918
2919 /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
2921 st->codecpar->codec_id = (enum AVCodecID)codec_ul->id;
2922 if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
2924 st->codecpar->codec_id = (enum AVCodecID)codec_ul->id;
2925 }
2926
2927 av_log(mxf->fc, AV_LOG_VERBOSE, "%s: Universal Label: ",
2929 for (k = 0; k < 16; k++) {
2930 av_log(mxf->fc, AV_LOG_VERBOSE, "%.2x",
2931 descriptor->essence_codec_ul[k]);
2932 if (!(k+1 & 19) || k == 5)
2933 av_log(mxf->fc, AV_LOG_VERBOSE, ".");
2934 }
2935 av_log(mxf->fc, AV_LOG_VERBOSE, "\n");
2936
2937 mxf_add_umid_metadata(&st->metadata, "file_package_umid", source_package);
2938 if (source_package->name && source_package->name[0])
2939 av_dict_set(&st->metadata, "file_package_name", source_package->name, 0);
2940 if (material_track->name && material_track->name[0])
2941 av_dict_set(&st->metadata, "track_name", material_track->name, 0);
2942
2943 mxf_parse_physical_source_package(mxf, source_track, st);
2944
2946 source_track->intra_only = mxf_is_intra_only(descriptor);
2949 st->codecpar->codec_id = container_ul->id;
2950 st->codecpar->width = descriptor->width;
2951 st->codecpar->height = descriptor->height; /* Field height, not frame height */
2952 switch (descriptor->frame_layout) {
2953 case FullFrame:
2955 break;
2956 case OneField:
2957 /* Every other line is stored and needs to be duplicated. */
2958 av_log(mxf->fc, AV_LOG_INFO, "OneField frame layout isn't currently supported\n");
2959 break; /* The correct thing to do here is fall through, but by breaking we might be
2960 able to decode some streams at half the vertical resolution, rather than not al all.
2961 It's also for compatibility with the old behavior. */
2962 case MixedFields:
2963 break;
2964 case SegmentedFrame:
2967 case SeparateFields:
2968 av_log(mxf->fc, AV_LOG_DEBUG, "video_line_map: (%d, %d), field_dominance: %d\n",
2969 descriptor->video_line_map[0], descriptor->video_line_map[1],
2970 descriptor->field_dominance);
2971 if ((descriptor->video_line_map[0] > 0) && (descriptor->video_line_map[1] > 0)) {
2972 /* Detect coded field order from VideoLineMap:
2973 * (even, even) => bottom field coded first
2974 * (even, odd) => top field coded first
2975 * (odd, even) => top field coded first
2976 * (odd, odd) => bottom field coded first
2977 */
2978 if ((descriptor->video_line_map[0] + descriptor->video_line_map[1]) % 2) {
2979 switch (descriptor->field_dominance) {
2983 break;
2986 break;
2987 default:
2989 "Field dominance %d support",
2990 descriptor->field_dominance);
2991 }
2992 } else {
2993 switch (descriptor->field_dominance) {
2997 break;
3000 break;
3001 default:
3003 "Field dominance %d support",
3004 descriptor->field_dominance);
3005 }
3006 }
3007 }
3008 /* Turn field height into frame height. */
3009 st->codecpar->height *= 2;
3010 break;
3011 default:
3012 av_log(mxf->fc, AV_LOG_INFO, "Unknown frame layout type: %d\n", descriptor->frame_layout);
3013 }
3014
3015 if (mxf_is_st_422(essence_container_ul)) {
3016 switch ((*essence_container_ul)[14]) {
3017 case 2: /* Cn: Clip- wrapped Picture Element */
3018 case 3: /* I1: Interlaced Frame, 1 field/KLV */
3019 case 4: /* I2: Interlaced Frame, 2 fields/KLV */
3020 case 6: /* P1: Frame- wrapped Picture Element */
3021 st->avg_frame_rate = source_track->edit_rate;
3022 st->r_frame_rate = st->avg_frame_rate;
3023 break;
3024 case 5: /* F1: Field-wrapped Picture Element */
3025 st->avg_frame_rate = av_mul_q(av_make_q(2, 1), source_track->edit_rate);
3026 st->r_frame_rate = st->avg_frame_rate;
3027 break;
3028 default:
3029 break;
3030 }
3031 }
3032
3033 if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
3034 switch (descriptor->essence_codec_ul[14]) {
3035 case 1: st->codecpar->codec_tag = MKTAG('a','p','c','o'); break;
3036 case 2: st->codecpar->codec_tag = MKTAG('a','p','c','s'); break;
3037 case 3: st->codecpar->codec_tag = MKTAG('a','p','c','n'); break;
3038 case 4: st->codecpar->codec_tag = MKTAG('a','p','c','h'); break;
3039 case 5: st->codecpar->codec_tag = MKTAG('a','p','4','h'); break;
3040 case 6: st->codecpar->codec_tag = MKTAG('a','p','4','x'); break;
3041 }
3042 }
3043
3045 st->codecpar->format = descriptor->pix_fmt;
3046 if (st->codecpar->format == AV_PIX_FMT_NONE) {
3048 &descriptor->essence_codec_ul);
3049 st->codecpar->format = (enum AVPixelFormat)pix_fmt_ul->id;
3050 if (st->codecpar->format== AV_PIX_FMT_NONE) {
3052 &descriptor->essence_codec_ul)->id;
3053 if (!st->codecpar->codec_tag) {
3054 /* support files created before RP224v10 by defaulting to UYVY422
3055 if subsampling is 4:2:2 and component depth is 8-bit */
3056 if (descriptor->horiz_subsampling == 2 &&
3057 descriptor->vert_subsampling == 1 &&
3058 descriptor->component_depth == 8) {
3060 }
3061 }
3062 }
3063 }
3064 }
3066 if (material_track->origin) {
3067 av_dict_set_int(&st->metadata, "material_track_origin", material_track->origin, 0);
3068 }
3069 if (source_track->origin) {
3070 av_dict_set_int(&st->metadata, "source_track_origin", source_track->origin, 0);
3071 }
3072 if (descriptor->aspect_ratio.num && descriptor->aspect_ratio.den)
3073 sti->display_aspect_ratio = descriptor->aspect_ratio;
3074 st->codecpar->color_range = mxf_get_color_range(mxf, descriptor);
3078 if (descriptor->mastering) {
3081 (uint8_t *)descriptor->mastering, descriptor->mastering_size, 0)) {
3082 ret = AVERROR(ENOMEM);
3083 goto fail_and_free;
3084 }
3085 descriptor->mastering = NULL;
3086 }
3087 if (descriptor->coll) {
3090 (uint8_t *)descriptor->coll, descriptor->coll_size, 0)) {
3091 ret = AVERROR(ENOMEM);
3092 goto fail_and_free;
3093 }
3094 descriptor->coll = NULL;
3095 }
3096 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3098 /* Only overwrite existing codec ID if it is unset or A-law, which is the default according to SMPTE RP 224. */
3100 st->codecpar->codec_id = (enum AVCodecID)container_ul->id;
3101 st->codecpar->ch_layout.nb_channels = descriptor->channels;
3102
3103 if (descriptor->sample_rate.den > 0) {
3104 st->codecpar->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
3105 avpriv_set_pts_info(st, 64, descriptor->sample_rate.den, descriptor->sample_rate.num);
3106 } else {
3107 av_log(mxf->fc, AV_LOG_WARNING, "invalid sample rate (%d/%d) "
3108 "found for stream #%d, time base forced to 1/48000\n",
3109 descriptor->sample_rate.num, descriptor->sample_rate.den,
3110 st->index);
3111 avpriv_set_pts_info(st, 64, 1, 48000);
3112 }
3113
3114 /* if duration is set, rescale it from EditRate to SampleRate */
3115 if (st->duration != AV_NOPTS_VALUE)
3116 st->duration = av_rescale_q(st->duration,
3117 av_inv_q(material_track->edit_rate),
3118 st->time_base);
3119
3120 /* TODO: implement AV_CODEC_ID_RAWAUDIO */
3122 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
3124 else if (descriptor->bits_per_sample == 32)
3126 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE) {
3127 if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
3129 else if (descriptor->bits_per_sample == 32)
3131 } else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) {
3133 } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
3135 }
3137
3138 if (descriptor->channels <= 0 || descriptor->channels >= FF_SANE_NB_CHANNELS) {
3139 av_log(mxf->fc, AV_LOG_ERROR, "Invalid number of channels %d, must be less than %d\n", descriptor->channels, FF_SANE_NB_CHANNELS);
3140 return AVERROR_INVALIDDATA;
3141 }
3142
3143 ret = parse_mca_labels(mxf, source_track, descriptor, st);
3144 if (ret < 0)
3145 return ret;
3146 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
3147 enum AVMediaType type;
3150 st->codecpar->codec_id = container_ul->id;
3153 st->codecpar->codec_type = type;
3154 if (container_ul->desc)
3155 av_dict_set(&st->metadata, "data_type", container_ul->desc, 0);
3159 }
3160 }
3161 if (!descriptor->extradata)
3162 parse_ffv1_sub_descriptor(mxf, source_track, descriptor, st);
3163 if (descriptor->extradata) {
3164 if (!ff_alloc_extradata(st->codecpar, descriptor->extradata_size)) {
3165 memcpy(st->codecpar->extradata, descriptor->extradata, descriptor->extradata_size);
3166 }
3167 } else if (st->codecpar->codec_id == AV_CODEC_ID_H264 && source_track->intra_only) {
3169 &descriptor->essence_codec_ul)->id;
3170 if (coded_width)
3171 st->codecpar->width = coded_width;
3173 if (ret < 0)
3174 return ret;
3175 }
3176 if (st->codecpar->codec_type != AVMEDIA_TYPE_DATA && source_track->wrapping != FrameWrapped) {
3177 /* TODO: decode timestamps */
3179 }
3180 }
3181
3182 for (int i = 0; i < mxf->fc->nb_streams; i++) {
3183 MXFTrack *track1 = mxf->fc->streams[i]->priv_data;
3184 if (track1 && track1->body_sid) {
3185 for (int j = i + 1; j < mxf->fc->nb_streams; j++) {
3186 MXFTrack *track2 = mxf->fc->streams[j]->priv_data;
3187 if (track2 && track1->body_sid == track2->body_sid && track1->wrapping != track2->wrapping) {
3188 if (track1->wrapping == UnknownWrapped)
3189 track1->wrapping = track2->wrapping;
3190 else if (track2->wrapping == UnknownWrapped)
3191 track2->wrapping = track1->wrapping;
3192 else
3193 av_log(mxf->fc, AV_LOG_ERROR, "stream %d and stream %d have the same BodySID (%d) "
3194 "with different wrapping\n", i, j, track1->body_sid);
3195 }
3196 }
3197 }
3198 }
3199
3200 ret = 0;
3201fail_and_free:
3202 return ret;
3203}
3204
3205static int64_t mxf_timestamp_to_int64(uint64_t timestamp)
3206{
3207 struct tm time = { 0 };
3208 int msecs;
3209 time.tm_year = (timestamp >> 48) - 1900;
3210 time.tm_mon = (timestamp >> 40 & 0xFF) - 1;
3211 time.tm_mday = (timestamp >> 32 & 0xFF);
3212 time.tm_hour = (timestamp >> 24 & 0xFF);
3213 time.tm_min = (timestamp >> 16 & 0xFF);
3214 time.tm_sec = (timestamp >> 8 & 0xFF);
3215 msecs = (timestamp & 0xFF) * 4;
3216
3217 /* Clip values for legacy reasons. Maybe we should return error instead? */
3218 time.tm_mon = av_clip(time.tm_mon, 0, 11);
3219 time.tm_mday = av_clip(time.tm_mday, 1, 31);
3220 time.tm_hour = av_clip(time.tm_hour, 0, 23);
3221 time.tm_min = av_clip(time.tm_min, 0, 59);
3222 time.tm_sec = av_clip(time.tm_sec, 0, 59);
3223 msecs = av_clip(msecs, 0, 999);
3224
3225 return (int64_t)av_timegm(&time) * 1000000 + msecs * 1000;
3226}
3227
3228#define SET_STR_METADATA(pb, name, str) do { \
3229 if ((ret = mxf_read_utf16be_string(pb, size, &str)) < 0) \
3230 return ret; \
3231 av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
3232} while (0)
3233
3234#define SET_VERSION_METADATA(pb, name, major, minor, tertiary, patch, release, str) do { \
3235 major = avio_rb16(pb); \
3236 minor = avio_rb16(pb); \
3237 tertiary = avio_rb16(pb); \
3238 patch = avio_rb16(pb); \
3239 release = avio_rb16(pb); \
3240 if ((ret = mxf_version_to_str(major, minor, tertiary, patch, release, &str)) < 0) \
3241 return ret; \
3242 av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
3243} while (0)
3244
3245#define SET_UID_METADATA(pb, name, var, str) do { \
3246 char uuid_str[2 * AV_UUID_LEN + 4 + 1]; \
3247 avio_read(pb, var, 16); \
3248 av_uuid_unparse(uid, uuid_str); \
3249 av_dict_set(&s->metadata, name, uuid_str, 0); \
3250} while (0)
3251
3252#define SET_TS_METADATA(pb, name, var, str) do { \
3253 var = avio_rb64(pb); \
3254 if (var && (ret = ff_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var))) < 0) \
3255 return ret; \
3256} while (0)
3257
3258static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset)
3259{
3260 MXFContext *mxf = arg;
3261 AVFormatContext *s = mxf->fc;
3262 int ret;
3263 UID uid = { 0 };
3264 char *str = NULL;
3265 uint64_t ts;
3266 uint16_t major, minor, tertiary, patch, release;
3267 switch (tag) {
3268 case 0x3C01:
3269 SET_STR_METADATA(pb, "company_name", str);
3270 break;
3271 case 0x3C02:
3272 SET_STR_METADATA(pb, "product_name", str);
3273 break;
3274 case 0x3C03:
3275 SET_VERSION_METADATA(pb, "product_version_num", major, minor, tertiary, patch, release, str);
3276 break;
3277 case 0x3C04:
3278 SET_STR_METADATA(pb, "product_version", str);
3279 break;
3280 case 0x3C05:
3281 SET_UID_METADATA(pb, "product_uid", uid, str);
3282 break;
3283 case 0x3C06:
3284 SET_TS_METADATA(pb, "modification_date", ts, str);
3285 break;
3286 case 0x3C07:
3287 SET_VERSION_METADATA(pb, "toolkit_version_num", major, minor, tertiary, patch, release, str);
3288 break;
3289 case 0x3C08:
3290 SET_STR_METADATA(pb, "application_platform", str);
3291 break;
3292 case 0x3C09:
3293 SET_UID_METADATA(pb, "generation_uid", uid, str);
3294 break;
3295 case 0x3C0A:
3296 SET_UID_METADATA(pb, "uid", uid, str);
3297 break;
3298 }
3299 return 0;
3300}
3301
3302static int mxf_read_preface_metadata(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
3303{
3304 MXFContext *mxf = arg;
3305 AVFormatContext *s = mxf->fc;
3306 int ret;
3307 char *str = NULL;
3308
3309 if (tag >= 0x8000 && (IS_KLV_KEY(uid, mxf_avid_project_name))) {
3310 SET_STR_METADATA(pb, "project_name", str);
3311 }
3312 return 0;
3313}
3314
3316 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
3317 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
3318 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack },
3319 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack },
3320 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack },
3321 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack },
3322 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack },
3323 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack },
3324 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
3325 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
3326 { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
3327 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2f,0x00 }, mxf_read_preface_metadata },
3328 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x30,0x00 }, mxf_read_identification_metadata },
3329 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage },
3330 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_package, sizeof(MXFPackage), SourcePackage },
3331 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_package, sizeof(MXFPackage), MaterialPackage },
3332 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0f,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
3333 { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x05,0x00 }, mxf_read_essence_group, sizeof(MXFEssenceGroup), EssenceGroup},
3334 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
3335 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3f,0x00 }, mxf_read_tagged_value, sizeof(MXFTaggedValue), TaggedValue },
3336 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
3337 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Generic Sound */
3338 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
3339 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
3340 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
3341 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
3342 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2VideoDescriptor */
3343 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5b,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* VBI - SMPTE 436M */
3344 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5c,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* VANC/VBI - SMPTE 436M */
3345 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5e,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2AudioDescriptor */
3346 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x64,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* DC Timed Text Descriptor */
3347 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6b,0x00 }, mxf_read_mca_sub_descriptor, sizeof(MXFMCASubDescriptor), AudioChannelLabelSubDescriptor },
3348 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6c,0x00 }, mxf_read_mca_sub_descriptor, sizeof(MXFMCASubDescriptor), SoundfieldGroupLabelSubDescriptor },
3349 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6d,0x00 }, mxf_read_mca_sub_descriptor, sizeof(MXFMCASubDescriptor), GroupOfSoundfieldGroupsLabelSubDescriptor },
3350 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x81,0x03 }, mxf_read_ffv1_sub_descriptor, sizeof(MXFFFV1SubDescriptor), FFV1SubDescriptor },
3351 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
3352 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
3353 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x14,0x00 }, mxf_read_timecode_component, sizeof(MXFTimecodeComponent), TimecodeComponent },
3354 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0c,0x00 }, mxf_read_pulldown_component, sizeof(MXFPulldownComponent), PulldownComponent },
3355 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
3356 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
3357 { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x23,0x00 }, mxf_read_essence_container_data, sizeof(MXFEssenceContainerData), EssenceContainerData },
3358 { { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 } }, /* KLV fill, skip */
3359};
3360
3362{
3363 ctx->partition_score = partition_score(partition);
3364 switch (type){
3365 case MultipleDescriptor:
3366 case Descriptor:
3367 ((MXFDescriptor*)ctx)->pix_fmt = AV_PIX_FMT_NONE;
3368 ((MXFDescriptor*)ctx)->duration = AV_NOPTS_VALUE;
3369 break;
3370 default:
3371 break;
3372 }
3373 return 0;
3374}
3375
3376static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
3377{
3378 AVIOContext *pb = mxf->fc->pb;
3379 uint64_t klv_end = avio_tell(pb) + klv->length;
3380 MXFMetadataSet *meta;
3381 void *ctx;
3382
3383 if (ctx_size) {
3384 meta = av_mallocz(ctx_size);
3385 if (!meta)
3386 return AVERROR(ENOMEM);
3387 ctx = meta;
3389 } else {
3390 meta = NULL;
3391 ctx = mxf;
3392 }
3393 while (avio_tell(pb) + 4ULL < klv_end && !avio_feof(pb)) {
3394 int ret;
3395 int tag = avio_rb16(pb);
3396 int size = avio_rb16(pb); /* KLV specified by 0x53 */
3397 int64_t next = avio_tell(pb);
3398 UID uid = {0};
3399 if (next < 0 || next > INT64_MAX - size) {
3400 if (meta) {
3401 mxf_free_metadataset(&meta, type);
3402 }
3403 return next < 0 ? next : AVERROR_INVALIDDATA;
3404 }
3405 next += size;
3406
3407 av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x size %d\n", tag, size);
3408 if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
3409 av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
3410 continue;
3411 }
3412 if (tag > 0x7FFF) { /* dynamic tag */
3413 int i;
3414 for (i = 0; i < mxf->local_tags_count; i++) {
3415 int local_tag = AV_RB16(mxf->local_tags+i*18);
3416 if (local_tag == tag) {
3417 memcpy(uid, mxf->local_tags+i*18+2, 16);
3418 av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x\n", local_tag);
3419 PRINT_KEY(mxf->fc, "uid", uid);
3420 }
3421 }
3422 }
3423 if (meta && tag == 0x3C0A) {
3424 avio_read(pb, meta->uid, 16);
3425 } else if ((ret = read_child(ctx, pb, tag, size, uid, -1)) < 0) {
3426 if (meta) {
3427 mxf_free_metadataset(&meta, type);
3428 }
3429 return ret;
3430 }
3431
3432 /* Accept the 64k local set limit being exceeded (Avid). Don't accept
3433 * it extending past the end of the KLV though (zzuf5.mxf). */
3434 if (avio_tell(pb) > klv_end) {
3435 if (meta) {
3436 mxf_free_metadataset(&meta, type);
3437 }
3438
3439 av_log(mxf->fc, AV_LOG_ERROR,
3440 "local tag %#04x extends past end of local set @ %#"PRIx64"\n",
3441 tag, klv->offset);
3442 return AVERROR_INVALIDDATA;
3443 } else if (avio_tell(pb) <= next) /* only seek forward, else this can loop for a long time */
3444 avio_seek(pb, next, SEEK_SET);
3445 }
3446 return meta ? mxf_add_metadata_set(mxf, &meta, type) : 0;
3447}
3448
3449/**
3450 * Matches any partition pack key, in other words:
3451 * - HeaderPartition
3452 * - BodyPartition
3453 * - FooterPartition
3454 * @return non-zero if the key is a partition pack key, zero otherwise
3455 */
3457{
3458 //NOTE: this is a little lax since it doesn't constraint key[14]
3459 return !memcmp(key, mxf_header_partition_pack_key, 13) &&
3460 key[13] >= 2 && key[13] <= 4;
3461}
3462
3463/**
3464 * Parses a metadata KLV
3465 * @return <0 on error, 0 otherwise
3466 */
3468 int ctx_size, enum MXFMetadataSetType type)
3469{
3470 AVFormatContext *s = mxf->fc;
3471 int res;
3472 if (klv.key[5] == 0x53) {
3473 res = mxf_read_local_tags(mxf, &klv, read, ctx_size, type);
3474 } else {
3475 uint64_t next = avio_tell(s->pb) + klv.length;
3476 res = read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);
3477
3478 /* only seek forward, else this can loop for a long time */
3479 if (avio_tell(s->pb) > next) {
3480 av_log(s, AV_LOG_ERROR, "read past end of KLV @ %#"PRIx64"\n",
3481 klv.offset);
3482 return AVERROR_INVALIDDATA;
3483 }
3484
3485 avio_seek(s->pb, next, SEEK_SET);
3486 }
3487 if (res < 0) {
3488 av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
3489 return res;
3490 }
3491 return 0;
3492}
3493
3494/**
3495 * Seeks to the previous partition and parses it, if possible
3496 * @return <= 0 if we should stop parsing, > 0 if we should keep going
3497 */
3499{
3500 AVIOContext *pb = mxf->fc->pb;
3501 KLVPacket klv;
3502 int64_t current_partition_ofs;
3503 int ret;
3504
3505 if (!mxf->current_partition ||
3507 return 0; /* we've parsed all partitions */
3508
3509 /* seek to previous partition */
3510 current_partition_ofs = mxf->current_partition->pack_ofs; //includes run-in
3511 avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET);
3512 mxf->current_partition = NULL;
3513
3514 av_log(mxf->fc, AV_LOG_TRACE, "seeking to previous partition\n");
3515
3516 /* Make sure this is actually a PartitionPack, and if so parse it.
3517 * See deadlock2.mxf
3518 */
3519 if ((ret = klv_read_packet(mxf, &klv, pb)) < 0) {
3520 av_log(mxf->fc, AV_LOG_ERROR, "failed to read PartitionPack KLV\n");
3521 return ret;
3522 }
3523
3524 if (!mxf_is_partition_pack_key(klv.key)) {
3525 av_log(mxf->fc, AV_LOG_ERROR, "PreviousPartition @ %" PRIx64 " isn't a PartitionPack\n", klv.offset);
3526 return AVERROR_INVALIDDATA;
3527 }
3528
3529 /* We can't just check ofs >= current_partition_ofs because PreviousPartition
3530 * can point to just before the current partition, causing klv_read_packet()
3531 * to sync back up to it. See deadlock3.mxf
3532 */
3533 if (klv.offset >= current_partition_ofs) {
3534 av_log(mxf->fc, AV_LOG_ERROR, "PreviousPartition for PartitionPack @ %"
3535 PRIx64 " indirectly points to itself\n", current_partition_ofs);
3536 return AVERROR_INVALIDDATA;
3537 }
3538
3539 if ((ret = mxf_parse_klv(mxf, klv, mxf_read_partition_pack, 0, 0)) < 0)
3540 return ret;
3541
3542 return 1;
3543}
3544
3545/**
3546 * Called when essence is encountered
3547 * @return <= 0 if we should stop parsing, > 0 if we should keep going
3548 */
3550{
3551 AVIOContext *pb = mxf->fc->pb;
3552 int64_t ret;
3553
3554 if (mxf->parsing_backward) {
3556 } else {
3557 if (!mxf->footer_partition) {
3558 av_log(mxf->fc, AV_LOG_TRACE, "no FooterPartition\n");
3559 return 0;
3560 }
3561
3562 av_log(mxf->fc, AV_LOG_TRACE, "seeking to FooterPartition\n");
3563
3564 /* remember where we were so we don't end up seeking further back than this */
3565 mxf->last_forward_tell = avio_tell(pb);
3566
3567 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
3568 av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing FooterPartition\n");
3569 return -1;
3570 }
3571
3572 /* seek to FooterPartition and parse backward */
3573 if ((ret = avio_seek(pb, mxf->run_in + mxf->footer_partition, SEEK_SET)) < 0) {
3574 av_log(mxf->fc, AV_LOG_ERROR,
3575 "failed to seek to FooterPartition @ 0x%" PRIx64
3576 " (%"PRId64") - partial file?\n",
3577 mxf->run_in + mxf->footer_partition, ret);
3578 return ret;
3579 }
3580
3581 mxf->current_partition = NULL;
3582 mxf->parsing_backward = 1;
3583 }
3584
3585 return 1;
3586}
3587
3588/**
3589 * Called when the next partition or EOF is encountered
3590 * @return <= 0 if we should stop parsing, > 0 if we should keep going
3591 */
3596
3598{
3599 for (int i = 0; i < s->nb_streams; i++) {
3600 MXFTrack *track = s->streams[i]->priv_data;
3601 if (track && track->body_sid == body_sid && track->wrapping != UnknownWrapped)
3602 return track->wrapping;
3603 }
3604 return UnknownWrapped;
3605}
3606
3607/**
3608 * Figures out the proper offset and length of the essence container in each partition
3609 */
3611{
3612 MXFContext *mxf = s->priv_data;
3613 int x;
3614
3615 for (x = 0; x < mxf->partitions_count; x++) {
3616 MXFPartition *p = &mxf->partitions[x];
3617 MXFWrappingScheme wrapping;
3618
3619 if (!p->body_sid)
3620 continue; /* BodySID == 0 -> no essence */
3621
3622 /* for clip wrapped essences we point essence_offset after the KL (usually klv.offset + 20 or 25)
3623 * otherwise we point essence_offset at the key of the first essence KLV.
3624 */
3625
3626 wrapping = (mxf->op == OPAtom) ? ClipWrapped : mxf_get_wrapping_by_body_sid(s, p->body_sid);
3627
3628 if (wrapping == ClipWrapped) {
3629 p->essence_offset = p->first_essence_klv.next_klv - p->first_essence_klv.length;
3630 p->essence_length = p->first_essence_klv.length;
3631 } else {
3632 p->essence_offset = p->first_essence_klv.offset;
3633
3634 /* essence container spans to the next partition */
3635 if (x < mxf->partitions_count - 1)
3636 p->essence_length = mxf->partitions[x+1].pack_ofs - mxf->run_in - p->essence_offset;
3637
3638 if (p->essence_length < 0) {
3639 /* next ThisPartition < essence_offset */
3640 p->essence_length = 0;
3641 av_log(mxf->fc, AV_LOG_ERROR,
3642 "partition %i: bad ThisPartition = %"PRIX64"\n",
3643 x+1, mxf->partitions[x+1].pack_ofs - mxf->run_in);
3644 }
3645 }
3646 }
3647}
3648
3650{
3651 int i;
3652 for (i = 0; i < mxf->nb_index_tables; i++)
3653 if (mxf->index_tables[i].index_sid == index_sid)
3654 return &mxf->index_tables[i];
3655 return NULL;
3656}
3657
3658/**
3659 * Deal with the case where for some audio atoms EditUnitByteCount is
3660 * very small (2, 4..). In those cases we should read more than one
3661 * sample per call to mxf_read_packet().
3662 */
3664{
3665 MXFTrack *track = st->priv_data;
3666 MXFIndexTable *t;
3667
3668 if (!track)
3669 return;
3670 track->edit_units_per_packet = 1;
3671 if (track->wrapping != ClipWrapped)
3672 return;
3673
3674 t = mxf_find_index_table(mxf, track->index_sid);
3675
3676 /* expect PCM with exactly one index table segment and a small (< 32) EUBC */
3678 !is_pcm(st->codecpar->codec_id) ||
3679 !t ||
3680 t->nb_segments != 1 ||
3681 t->segments[0]->edit_unit_byte_count >= 32)
3682 return;
3683
3684 /* arbitrarily default to 48 kHz PAL audio frame size */
3685 /* TODO: We could compute this from the ratio between the audio
3686 * and video edit rates for 48 kHz NTSC we could use the
3687 * 1802-1802-1802-1802-1801 pattern. */
3688 track->edit_units_per_packet = FFMAX(1, track->edit_rate.num / track->edit_rate.den / 25);
3689}
3690
3691/**
3692 * Deal with the case where ClipWrapped essences does not have any IndexTableSegments.
3693 */
3695{
3696 MXFTrack *track = st->priv_data;
3698 MXFPartition *p = NULL;
3699 int essence_partition_count = 0;
3700 int edit_unit_byte_count = 0;
3701 int i, ret;
3703
3704 if (!track || track->wrapping != ClipWrapped)
3705 return 0;
3706
3707 /* check if track already has an IndexTableSegment */
3708 for (i = 0; i < mg->metadata_sets_count; i++) {
3709 MXFIndexTableSegment *s = (MXFIndexTableSegment*)mg->metadata_sets[i];
3710 if (s->body_sid == track->body_sid)
3711 return 0;
3712 }
3713
3714 /* find the essence partition */
3715 for (i = 0; i < mxf->partitions_count; i++) {
3716 /* BodySID == 0 -> no essence */
3717 if (mxf->partitions[i].body_sid != track->body_sid)
3718 continue;
3719
3720 p = &mxf->partitions[i];
3721 essence_partition_count++;
3722 }
3723
3724 /* only handle files with a single essence partition */
3725 if (essence_partition_count != 1)
3726 return 0;
3727
3729 edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) *
3730 st->codecpar->ch_layout.nb_channels) >> 3;
3731 } else if (st->duration > 0 && p->first_essence_klv.length > 0 && p->first_essence_klv.length % st->duration == 0) {
3732 edit_unit_byte_count = p->first_essence_klv.length / st->duration;
3733 }
3734
3735 if (edit_unit_byte_count <= 0)
3736 return 0;
3737
3738 av_log(mxf->fc, AV_LOG_WARNING, "guessing index for stream %d using edit unit byte count %d\n", st->index, edit_unit_byte_count);
3739
3740 if (!(segment = av_mallocz(sizeof(*segment))))
3741 return AVERROR(ENOMEM);
3742
3744 return ret;
3745
3746 /* Make sure we have nonzero unique index_sid, body_sid will be ok, because
3747 * using the same SID for index is forbidden in MXF. */
3748 if (!track->index_sid)
3749 track->index_sid = track->body_sid;
3750
3751 /* stream will be treated as small EditUnitByteCount */
3752 segment->edit_unit_byte_count = edit_unit_byte_count;
3753 segment->index_start_position = 0;
3754 segment->index_duration = st->duration;
3755 segment->index_edit_rate = av_inv_q(st->time_base);
3756 segment->index_sid = track->index_sid;
3757 segment->body_sid = p->body_sid;
3758 return 0;
3759}
3760
3762{
3763 MXFContext *mxf = s->priv_data;
3764 uint32_t length;
3765 int64_t file_size, max_rip_length, min_rip_length;
3766 KLVPacket klv;
3767
3768 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
3769 return;
3770
3771 file_size = avio_size(s->pb);
3772
3773 /* S377m says to check the RIP length for "silly" values, without defining "silly".
3774 * The limit below assumes a file with nothing but partition packs and a RIP.
3775 * Before changing this, consider that a muxer may place each sample in its own partition.
3776 *
3777 * 105 is the size of the smallest possible PartitionPack
3778 * 12 is the size of each RIP entry
3779 * 28 is the size of the RIP header and footer, assuming an 8-byte BER
3780 */
3781 max_rip_length = ((file_size - mxf->run_in) / 105) * 12 + 28;
3782 max_rip_length = FFMIN(max_rip_length, INT_MAX); //2 GiB and up is also silly
3783
3784 /* We're only interested in RIPs with at least two entries.. */
3785 min_rip_length = 16+1+24+4;
3786
3787 /* See S377m section 11 */
3788 avio_seek(s->pb, file_size - 4, SEEK_SET);
3789 length = avio_rb32(s->pb);
3790
3791 if (length < min_rip_length || length > max_rip_length)
3792 goto end;
3793 avio_seek(s->pb, file_size - length, SEEK_SET);
3794 if (klv_read_packet(mxf, &klv, s->pb) < 0 ||
3796 goto end;
3797 if (klv.next_klv != file_size || klv.length <= 4 || (klv.length - 4) % 12) {
3798 av_log(s, AV_LOG_WARNING, "Invalid RIP KLV length\n");
3799 goto end;
3800 }
3801
3802 avio_skip(s->pb, klv.length - 12);
3803 mxf->footer_partition = avio_rb64(s->pb);
3804
3805 /* sanity check */
3806 if (mxf->run_in + mxf->footer_partition >= file_size) {
3807 av_log(s, AV_LOG_WARNING, "bad FooterPartition in RIP - ignoring\n");
3808 mxf->footer_partition = 0;
3809 }
3810
3811end:
3812 avio_seek(s->pb, mxf->run_in, SEEK_SET);
3813}
3814
3816{
3817 MXFContext *mxf = s->priv_data;
3818 KLVPacket klv;
3819 int64_t essence_offset = 0;
3820 int ret;
3821 int64_t run_in;
3822
3823 mxf->last_forward_tell = INT64_MAX;
3824
3826 av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
3827 return AVERROR_INVALIDDATA;
3828 }
3829 avio_seek(s->pb, -14, SEEK_CUR);
3830 mxf->fc = s;
3831 run_in = avio_tell(s->pb);
3832 if (run_in < 0 || run_in > RUN_IN_MAX)
3833 return AVERROR_INVALIDDATA;
3834 mxf->run_in = run_in;
3835
3837
3838 while (!avio_feof(s->pb)) {
3839 size_t x;
3840
3841 ret = klv_read_packet(mxf, &klv, s->pb);
3843 if (ret >= 0 && avio_size(s->pb) > klv.next_klv)
3844 av_log(s, AV_LOG_WARNING, "data after the RandomIndexPack, assuming end of file\n");
3845 /* EOF - seek to previous partition or stop */
3847 break;
3848 else
3849 continue;
3850 }
3851
3852 PRINT_KEY(s, "read header", klv.key);
3853 av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
3860
3861 if (!mxf->current_partition) {
3862 av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to first PartitionPack\n");
3863 return AVERROR_INVALIDDATA;
3864 }
3865
3868
3869 if (!essence_offset)
3870 essence_offset = klv.offset;
3871
3872 /* seek to footer, previous partition or stop */
3873 if (mxf_parse_handle_essence(mxf) <= 0)
3874 break;
3875 continue;
3876 } else if (mxf_is_partition_pack_key(klv.key) && mxf->current_partition) {
3877 /* next partition pack - keep going, seek to previous partition or stop */
3879 break;
3880 else if (mxf->parsing_backward)
3881 continue;
3882 /* we're still parsing forward. proceed to parsing this partition pack */
3883 }
3884
3885 for (x = 0; x < FF_ARRAY_ELEMS(mxf_metadata_read_table); x++) {
3887 if (IS_KLV_KEY(klv.key, metadata->key)) {
3888 if (metadata->read) {
3889 if ((ret = mxf_parse_klv(mxf, klv, metadata->read, metadata->ctx_size, metadata->type)) < 0)
3890 return ret;
3891 } else {
3892 avio_skip(s->pb, klv.length);
3893 }
3894 break;
3895 }
3896 }
3898 av_log(s, AV_LOG_VERBOSE, "Dark key " PRIxUID "\n",
3899 UID_ARG(klv.key));
3900 avio_skip(s->pb, klv.length);
3901 }
3902 }
3903 /* FIXME avoid seek */
3904 if (!essence_offset) {
3905 av_log(s, AV_LOG_ERROR, "no essence\n");
3906 return AVERROR_INVALIDDATA;
3907 }
3908 avio_seek(s->pb, essence_offset, SEEK_SET);
3909
3910 /* we need to do this before computing the index tables
3911 * to be able to fill in zero IndexDurations with st->duration */
3912 if ((ret = mxf_parse_structural_metadata(mxf)) < 0)
3913 return ret;
3914
3915 for (int i = 0; i < s->nb_streams; i++)
3916 mxf_handle_missing_index_segment(mxf, s->streams[i]);
3917
3918 if ((ret = mxf_compute_index_tables(mxf)) < 0)
3919 return ret;
3920
3921 if (mxf->nb_index_tables > 1) {
3922 /* TODO: look up which IndexSID to use via EssenceContainerData */
3923 av_log(mxf->fc, AV_LOG_INFO, "got %i index tables - only the first one (IndexSID %i) will be used\n",
3924 mxf->nb_index_tables, mxf->index_tables[0].index_sid);
3925 } else if (mxf->nb_index_tables == 0 && mxf->op == OPAtom && (s->error_recognition & AV_EF_EXPLODE)) {
3926 av_log(mxf->fc, AV_LOG_ERROR, "cannot demux OPAtom without an index\n");
3927 return AVERROR_INVALIDDATA;
3928 }
3929
3931
3932 for (int i = 0; i < s->nb_streams; i++)
3933 mxf_compute_edit_units_per_packet(mxf, s->streams[i]);
3934
3935 return 0;
3936}
3937
3938/* Get the edit unit of the next packet from current_offset in a track. The returned edit unit can be original_duration as well! */
3939static int mxf_get_next_track_edit_unit(MXFContext *mxf, MXFTrack *track, int64_t current_offset, int64_t *edit_unit_out)
3940{
3941 int64_t a, b, m, offset;
3943
3944 if (!t || track->original_duration <= 0)
3945 return -1;
3946
3947 a = -1;
3948 b = track->original_duration;
3949 while (b - 1 > a) {
3950 m = (a + (uint64_t)b) >> 1;
3951 if (mxf_edit_unit_absolute_offset(mxf, t, m, track->edit_rate, NULL, &offset, NULL, 0) < 0)
3952 return -1;
3953 if (offset < current_offset)
3954 a = m;
3955 else
3956 b = m;
3957 }
3958
3959 *edit_unit_out = b;
3960
3961 return 0;
3962}
3963
3965 int64_t edit_unit)
3966{
3967 MXFTrack *track = st->priv_data;
3968 AVRational time_base = av_inv_q(track->edit_rate);
3969 AVRational sample_rate = av_inv_q(st->time_base);
3970
3971 // For non-audio sample_count equals current edit unit
3973 return edit_unit;
3974
3975 if ((sample_rate.num / sample_rate.den) == 48000) {
3976 return av_rescale_q(edit_unit, sample_rate, track->edit_rate);
3977 } else {
3978 int64_t remainder = (sample_rate.num * (int64_t) time_base.num) %
3979 ( time_base.den * (int64_t)sample_rate.den);
3980 if (remainder)
3981 av_log(mxf->fc, AV_LOG_WARNING,
3982 "seeking detected on stream #%d with time base (%d/%d) and "
3983 "sample rate (%d/%d), audio pts won't be accurate.\n",
3984 st->index, time_base.num, time_base.den,
3985 sample_rate.num, sample_rate.den);
3986 return av_rescale_q(edit_unit, sample_rate, track->edit_rate);
3987 }
3988}
3989
3990/**
3991 * Make sure track->sample_count is correct based on what offset we're currently at.
3992 * Also determine the next edit unit (or packet) offset.
3993 * @return next_ofs if OK, <0 on error
3994 */
3996{
3997 int64_t next_ofs = -1;
3998 MXFTrack *track = st->priv_data;
3999 int64_t edit_unit = av_rescale_q(track->sample_count, st->time_base, av_inv_q(track->edit_rate));
4000 int64_t new_edit_unit;
4002
4003 if (!t || track->wrapping == UnknownWrapped || edit_unit > INT64_MAX - track->edit_units_per_packet)
4004 return -1;
4005
4006 if (mxf_edit_unit_absolute_offset(mxf, t, edit_unit + track->edit_units_per_packet, track->edit_rate, NULL, &next_ofs, NULL, 0) < 0 &&
4007 (next_ofs = mxf_essence_container_end(mxf, t->body_sid)) <= 0) {
4008 av_log(mxf->fc, AV_LOG_ERROR, "unable to compute the size of the last packet\n");
4009 return -1;
4010 }
4011
4012 /* check if the next edit unit offset (next_ofs) starts ahead of current_offset */
4013 if (next_ofs > current_offset)
4014 return next_ofs;
4015
4016 if (!resync) {
4017 av_log(mxf->fc, AV_LOG_ERROR, "cannot find current edit unit for stream %d, invalid index?\n", st->index);
4018 return -1;
4019 }
4020
4021 if (mxf_get_next_track_edit_unit(mxf, track, current_offset + 1, &new_edit_unit) < 0 || new_edit_unit <= 0) {
4022 av_log(mxf->fc, AV_LOG_ERROR, "failed to find next track edit unit in stream %d\n", st->index);
4023 return -1;
4024 }
4025
4026 new_edit_unit--;
4027 track->sample_count = mxf_compute_sample_count(mxf, st, new_edit_unit);
4028 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);
4029
4030 return mxf_set_current_edit_unit(mxf, st, current_offset, 0);
4031}
4032
4034 AVPacket *pkt)
4035{
4036 AVStream *st = mxf->fc->streams[pkt->stream_index];
4037 MXFTrack *track = st->priv_data;
4038 int64_t bits_per_sample = par->bits_per_coded_sample;
4039
4040 if (!bits_per_sample)
4041 bits_per_sample = av_get_bits_per_sample(par->codec_id);
4042
4043 pkt->pts = track->sample_count;
4044
4045 if (par->ch_layout.nb_channels <= 0 ||
4046 bits_per_sample <= 0 ||
4047 par->ch_layout.nb_channels * (int64_t)bits_per_sample < 8)
4048 track->sample_count = mxf_compute_sample_count(mxf, st, av_rescale_q(track->sample_count, st->time_base, av_inv_q(track->edit_rate)) + 1);
4049 else
4050 track->sample_count += pkt->size / (par->ch_layout.nb_channels * (int64_t)bits_per_sample / 8);
4051
4052 return 0;
4053}
4054
4056{
4057 AVCodecParameters *par = st->codecpar;
4058 MXFTrack *track = st->priv_data;
4059
4060 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
4061 /* see if we have an index table to derive timestamps from */
4063
4064 if (t && track->sample_count < t->nb_ptses) {
4065 pkt->dts = track->sample_count + t->first_dts;
4066 pkt->pts = t->ptses[track->sample_count];
4067 } else if (track->intra_only) {
4068 /* intra-only -> PTS = EditUnit.
4069 * let utils.c figure out DTS since it can be < PTS if low_delay = 0 (Sony IMX30) */
4070 pkt->pts = track->sample_count;
4071 }
4072 track->sample_count++;
4073 } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
4074 int ret = mxf_set_audio_pts(mxf, par, pkt);
4075 if (ret < 0)
4076 return ret;
4077 } else if (track) {
4078 pkt->dts = pkt->pts = track->sample_count;
4079 pkt->duration = 1;
4080 track->sample_count++;
4081 }
4082 return 0;
4083}
4084
4086{
4087 KLVPacket klv;
4088 MXFContext *mxf = s->priv_data;
4089 int ret;
4090
4091 while (1) {
4092 int64_t max_data_size;
4093 int64_t pos = avio_tell(s->pb);
4094
4095 if (pos < mxf->current_klv_data.next_klv - mxf->current_klv_data.length || pos >= mxf->current_klv_data.next_klv) {
4096 mxf->current_klv_data = (KLVPacket){{0}};
4097 ret = klv_read_packet(mxf, &klv, s->pb);
4098 if (ret < 0)
4099 break;
4100 // klv.key[0..3] == mxf_klv_key from here forward
4101 max_data_size = klv.length;
4102 pos = klv.next_klv - klv.length;
4103 PRINT_KEY(s, "read packet", klv.key);
4104 av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
4106 ret = mxf_decrypt_triplet(s, pkt, &klv);
4107 if (ret < 0) {
4108 av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
4109 return ret;
4110 }
4111 return 0;
4112 }
4113 } else {
4114 klv = mxf->current_klv_data;
4115 max_data_size = klv.next_klv - pos;
4116 }
4120 int body_sid = find_body_sid_by_absolute_offset(mxf, klv.offset);
4121 int index = mxf_get_stream_index(s, &klv, body_sid);
4122 int64_t next_ofs;
4123 AVStream *st;
4124 MXFTrack *track;
4125
4126 if (index < 0) {
4128 "error getting stream index %"PRIu32"\n",
4129 AV_RB32(klv.key + 12));
4130 goto skip;
4131 }
4132
4133 st = s->streams[index];
4134 track = st->priv_data;
4135
4136 if (s->streams[index]->discard == AVDISCARD_ALL)
4137 goto skip;
4138
4139 next_ofs = mxf_set_current_edit_unit(mxf, st, pos, 1);
4140
4141 if (track->wrapping != FrameWrapped) {
4142 int64_t size;
4143
4144 if (next_ofs <= 0) {
4145 // If we have no way to packetize the data, then return it in chunks...
4146 if (klv.next_klv - klv.length == pos && max_data_size > MXF_MAX_CHUNK_SIZE) {
4148 avpriv_request_sample(s, "Huge KLV without proper index in non-frame wrapped essence");
4149 }
4150 size = FFMIN(max_data_size, MXF_MAX_CHUNK_SIZE);
4151 } else {
4152 if ((size = next_ofs - pos) <= 0) {
4153 av_log(s, AV_LOG_ERROR, "bad size: %"PRId64"\n", size);
4154 mxf->current_klv_data = (KLVPacket){{0}};
4155 return AVERROR_INVALIDDATA;
4156 }
4157 // We must not overread, because the next edit unit might be in another KLV
4158 if (size > max_data_size)
4159 size = max_data_size;
4160 }
4161
4162 mxf->current_klv_data = klv;
4163 klv.offset = pos;
4164 klv.length = size;
4165 klv.next_klv = klv.offset + klv.length;
4166 }
4167
4168 /* check for 8 channels AES3 element */
4169 if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
4170 ret = mxf_get_d10_aes3_packet(s->pb, s->streams[index],
4171 pkt, klv.length);
4172 if (ret < 0) {
4173 av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
4174 mxf->current_klv_data = (KLVPacket){{0}};
4175 return ret;
4176 }
4177 } else if (mxf->eia608_extract &&
4178 s->streams[index]->codecpar->codec_id == AV_CODEC_ID_EIA_608) {
4179 ret = mxf_get_eia608_packet(s, s->streams[index], pkt, klv.length);
4180 if (ret < 0) {
4181 mxf->current_klv_data = (KLVPacket){{0}};
4182 return ret;
4183 }
4184 } else {
4185 ret = av_get_packet(s->pb, pkt, klv.length);
4186 if (ret < 0) {
4187 mxf->current_klv_data = (KLVPacket){{0}};
4188 return ret;
4189 }
4190 }
4191 pkt->stream_index = index;
4192 pkt->pos = klv.offset;
4193
4194 ret = mxf_set_pts(mxf, st, pkt);
4195 if (ret < 0) {
4196 mxf->current_klv_data = (KLVPacket){{0}};
4197 return ret;
4198 }
4199
4200 /* seek for truncated packets */
4201 avio_seek(s->pb, klv.next_klv, SEEK_SET);
4202
4203 return 0;
4204 } else {
4205 skip:
4206 avio_skip(s->pb, max_data_size);
4207 mxf->current_klv_data = (KLVPacket){{0}};
4208 }
4209 }
4210 return avio_feof(s->pb) ? AVERROR_EOF : ret;
4211}
4212
4214{
4215 MXFContext *mxf = s->priv_data;
4216
4217 av_freep(&mxf->packages_refs);
4219
4220 for (int i = 0; i < s->nb_streams; i++)
4221 s->streams[i]->priv_data = NULL;
4222
4223 for (int type = 0; type < FF_ARRAY_ELEMS(mxf->metadata_set_groups); type++) {
4225 for (int i = 0; i < mg->metadata_sets_count; i++)
4226 mxf_free_metadataset(mg->metadata_sets + i, type);
4227 mg->metadata_sets_count = 0;
4228 av_freep(&mg->metadata_sets);
4229 }
4230 av_freep(&mxf->partitions);
4231 av_freep(&mxf->aesc);
4232 av_freep(&mxf->local_tags);
4233
4234 if (mxf->index_tables) {
4235 for (int i = 0; i < mxf->nb_index_tables; i++) {
4237 av_freep(&mxf->index_tables[i].ptses);
4240 }
4241 }
4242 av_freep(&mxf->index_tables);
4243
4244 return 0;
4245}
4246
4247static int mxf_probe(const AVProbeData *p) {
4248 const uint8_t *bufp = p->buf;
4249 const uint8_t *end = p->buf + FFMIN(p->buf_size, RUN_IN_MAX + 1 + sizeof(mxf_header_partition_pack_key));
4250
4251 if (p->buf_size < sizeof(mxf_header_partition_pack_key))
4252 return 0;
4253
4254 /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
4255 end -= sizeof(mxf_header_partition_pack_key);
4256
4257 for (; bufp < end;) {
4258 if (!((bufp[13] - 1) & 0xF2)){
4263 return bufp == p->buf ? AVPROBE_SCORE_MAX : AVPROBE_SCORE_MAX - 1;
4264 bufp ++;
4265 } else
4266 bufp += 10;
4267 }
4268
4269 return 0;
4270}
4271
4272/* rudimentary byte seek */
4273/* XXX: use MXF Index */
4274static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
4275{
4276 AVStream *st = s->streams[stream_index];
4277 int64_t seconds;
4278 MXFContext* mxf = s->priv_data;
4279 int64_t seekpos;
4280 int ret;
4281 MXFIndexTable *t;
4282 MXFTrack *source_track = st->priv_data;
4283
4284 if (!source_track)
4285 return 0;
4286
4287 /* if audio then truncate sample_time to EditRate */
4289 sample_time = av_rescale_q(sample_time, st->time_base,
4290 av_inv_q(source_track->edit_rate));
4291
4292 if (mxf->nb_index_tables <= 0) {
4293 if (!s->bit_rate)
4294 return AVERROR_INVALIDDATA;
4295 if (sample_time < 0)
4296 sample_time = 0;
4297 seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
4298
4299 seekpos = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
4300 if (seekpos < 0)
4301 return seekpos;
4302
4303 avpriv_update_cur_dts(s, st, sample_time);
4304 mxf->current_klv_data = (KLVPacket){{0}};
4305 } else {
4306 MXFPartition *partition;
4307
4308 t = &mxf->index_tables[0];
4309 if (t->index_sid != source_track->index_sid) {
4310 int i;
4311 /* If the first index table does not belong to the stream, then find a stream which does belong to the index table */
4312 for (i = 0; i < s->nb_streams; i++) {
4313 MXFTrack *new_source_track = s->streams[i]->priv_data;
4314 if (new_source_track && new_source_track->index_sid == t->index_sid) {
4315 sample_time = av_rescale_q(sample_time, new_source_track->edit_rate, source_track->edit_rate);
4316 source_track = new_source_track;
4317 st = s->streams[i];
4318 break;
4319 }
4320 }
4321 if (i == s->nb_streams)
4322 return AVERROR_INVALIDDATA;
4323 }
4324
4325 /* clamp above zero, else ff_index_search_timestamp() returns negative
4326 * this also means we allow seeking before the start */
4327 sample_time = FFMAX(sample_time, 0);
4328
4329 if (t->fake_index) {
4330 /* The first frames may not be keyframes in presentation order, so
4331 * we have to advance the target to be able to find the first
4332 * keyframe backwards... */
4333 if (!(flags & AVSEEK_FLAG_ANY) &&
4335 t->ptses[0] != AV_NOPTS_VALUE &&
4336 sample_time < t->ptses[0] &&
4337 (t->fake_index[t->ptses[0]].flags & AVINDEX_KEYFRAME))
4338 sample_time = t->ptses[0];
4339
4340 /* behave as if we have a proper index */
4341 if ((sample_time = ff_index_search_timestamp(t->fake_index, t->nb_ptses, sample_time, flags)) < 0)
4342 return sample_time;
4343 /* get the stored order index from the display order index */
4344 sample_time += t->offsets[sample_time];
4345 } else {
4346 /* no IndexEntryArray (one or more CBR segments)
4347 * make sure we don't seek past the end */
4348 sample_time = FFMIN(sample_time, source_track->original_duration - 1);
4349 }
4350
4351 if (source_track->wrapping == UnknownWrapped)
4352 av_log(mxf->fc, AV_LOG_WARNING, "attempted seek in an UnknownWrapped essence\n");
4353
4354 if ((ret = mxf_edit_unit_absolute_offset(mxf, t, sample_time, source_track->edit_rate, &sample_time, &seekpos, &partition, 1)) < 0)
4355 return ret;
4356
4357 avpriv_update_cur_dts(s, st, sample_time);
4358 if (source_track->wrapping == ClipWrapped) {
4359 KLVPacket klv = partition->first_essence_klv;
4360 if (seekpos < klv.next_klv - klv.length || seekpos >= klv.next_klv) {
4361 av_log(mxf->fc, AV_LOG_ERROR, "attempted seek out of clip wrapped KLV\n");
4362 return AVERROR_INVALIDDATA;
4363 }
4364 mxf->current_klv_data = klv;
4365 } else {
4366 mxf->current_klv_data = (KLVPacket){{0}};
4367 }
4368 avio_seek(s->pb, seekpos, SEEK_SET);
4369 }
4370
4371 // Update all tracks sample count
4372 for (int i = 0; i < s->nb_streams; i++) {
4373 AVStream *cur_st = s->streams[i];
4374 MXFTrack *cur_track = cur_st->priv_data;
4375 if (cur_track) {
4376 int64_t track_edit_unit = sample_time;
4377 if (st != cur_st)
4378 mxf_get_next_track_edit_unit(mxf, cur_track, seekpos, &track_edit_unit);
4379 cur_track->sample_count = mxf_compute_sample_count(mxf, cur_st, track_edit_unit);
4380 }
4381 }
4382 return 0;
4383}
4384
4385static const AVOption options[] = {
4386 { "eia608_extract", "extract eia 608 captions from s436m track",
4387 offsetof(MXFContext, eia608_extract), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
4389 { NULL },
4390};
4391
4392static const AVClass demuxer_class = {
4393 .class_name = "mxf",
4394 .item_name = av_default_item_name,
4395 .option = options,
4396 .version = LIBAVUTIL_VERSION_INT,
4397 .category = AV_CLASS_CATEGORY_DEMUXER,
4398};
4399
4401 .p.name = "mxf",
4402 .p.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
4404 .p.priv_class = &demuxer_class,
4405 .priv_data_size = sizeof(MXFContext),
4406 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
4412};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFInputFormat ff_mxf_demuxer
Definition mxfdec.c:4400
static AVFormatContext * ctx
static const AVClass demuxer_class
Definition apngdec.c:478
int32_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:630
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition avformat.h:507
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition avformat.h:2749
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition avformat.h:522
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2747
@ AVSTREAM_PARSE_TIMESTAMPS
full parsing and interpolation of timestamps for frames not starting on a packet boundary
Definition avformat.h:615
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition avformat.h:614
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:613
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
uint64_t avio_rb64(AVIOContext *s)
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
unsigned int avio_rb16(AVIOContext *s)
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:507
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:620
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition aviobuf.c:845
unsigned int avio_rb32(AVIOContext *s)
int avio_r8(AVIOContext *s)
Definition aviobuf.c:611
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:674
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
const char * ff_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition avlanguage.c:741
@ AV_LANG_ISO639_2_BIBL
Definition avlanguage.h:28
char * av_asprintf(const char *fmt,...)
Definition avstring.c:115
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define av_sat_sub64
Definition common.h:142
#define av_sat_add64
Definition common.h:139
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Misc types and constants that do not belong anywhere else.
#define AV_PROFILE_JPEG2000_DCINEMA_2K
Definition defs.h:165
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition defs.h:230
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:231
@ AV_FIELD_PROGRESSIVE
Definition defs.h:229
@ AV_FIELD_BT
Bottom coded first, top displayed first.
Definition defs.h:233
@ AV_FIELD_TB
Top coded first, bottom displayed first.
Definition defs.h:232
#define AV_PROFILE_JPEG2000_DCINEMA_4K
Definition defs.h:166
AVAudioServiceType
Definition defs.h:251
@ AV_AUDIO_SERVICE_TYPE_MAIN
Definition defs.h:252
@ AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED
Definition defs.h:255
@ AV_AUDIO_SERVICE_TYPE_NB
Not part of ABI.
Definition defs.h:261
@ AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED
Definition defs.h:254
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries, int64_t wanted_timestamp, int flags)
Internal version of av_index_search_timestamp.
Definition seek.c:132
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition seek.c:37
static AVPacket * pkt
double value
Definition eval.c:102
const char * key
int ret
Definition ffplay.c:391
const char * label
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:580
#define sample
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition utils.c:557
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_DIRAC
Definition codec_id.h:166
@ AV_CODEC_ID_PCM_S24BE
Definition codec_id.h:346
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:333
@ AV_CODEC_ID_TTML
Definition codec_id.h:596
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_CFHD
Definition codec_id.h:262
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:334
@ AV_CODEC_ID_PCM_S24LE
Definition codec_id.h:345
@ AV_CODEC_ID_PCM_S24DAUD
Definition codec_id.h:349
@ AV_CODEC_ID_FFV1
Definition codec_id.h:83
@ AV_CODEC_ID_PCM_S32LE
Definition codec_id.h:341
@ AV_CODEC_ID_VC1
Definition codec_id.h:120
@ AV_CODEC_ID_PCM_ALAW
Definition codec_id.h:340
@ AV_CODEC_ID_EIA_608
Definition codec_id.h:582
@ AV_CODEC_ID_MP2
Definition codec_id.h:459
@ AV_CODEC_ID_HQX
Definition codec_id.h:235
@ AV_CODEC_ID_DVVIDEO
Definition codec_id.h:74
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_CODEC_ID_PRORES
Definition codec_id.h:198
@ AV_CODEC_ID_SMPTE_436M_ANC
Definition codec_id.h:616
@ AV_CODEC_ID_TIFF
Definition codec_id.h:146
@ AV_CODEC_ID_PCM_S32BE
Definition codec_id.h:342
@ AV_CODEC_ID_DNXHD
Definition codec_id.h:149
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
@ AV_CODEC_ID_HQ_HQA
Definition codec_id.h:237
@ AV_CODEC_ID_DNXUC
Definition codec_id.h:320
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:248
AVPacketSideData * av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, size_t size, int flags)
Allocate a new packet side data.
Definition packet.c:621
AVPacketSideData * av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, void *data, size_t size, int flags)
Wrap existing data as packet side data.
Definition packet.c:614
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
Definition packet.h:219
@ AV_PKT_DATA_AUDIO_SERVICE_TYPE
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
Definition packet.h:117
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition packet.h:232
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition aes.c:37
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition aes.c:231
void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition aes.c:171
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
Change the AVChannelOrder of a channel layout.
#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL
The specified retype target order is ignored and the simplest possible (canonical) order is used for ...
int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
Initialize a custom channel layout with the specified number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
AVChannel
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
@ AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_BACK_RIGHT
@ AV_CHAN_TOP_SURROUND_RIGHT
-110 degrees, Rvs, TpRS
@ AV_CHAN_FRONT_RIGHT_OF_CENTER
@ AV_CHAN_FRONT_LEFT
@ AV_CHAN_SIDE_SURROUND_RIGHT
-90 degrees, Rss, SiR
@ AV_CHAN_TOP_BACK_LEFT
@ AV_CHAN_TOP_FRONT_RIGHT
@ AV_CHAN_FRONT_RIGHT
@ AV_CHAN_BACK_CENTER
@ AV_CHAN_TOP_SIDE_RIGHT
@ AV_CHAN_FRONT_CENTER
@ AV_CHAN_TOP_SURROUND_LEFT
+110 degrees, Lvs, TpLS
@ AV_CHAN_SIDE_RIGHT
@ AV_CHAN_SIDE_SURROUND_LEFT
+90 degrees, Lss, SiL
@ AV_CHAN_FRONT_LEFT_OF_CENTER
@ AV_CHAN_LOW_FREQUENCY_2
@ AV_CHAN_TOP_BACK_CENTER
@ AV_CHAN_BACK_LEFT
@ AV_CHAN_SIDE_LEFT
@ AV_CHAN_BOTTOM_FRONT_CENTER
@ AV_CHAN_TOP_FRONT_LEFT
@ AV_CHAN_TOP_BACK_RIGHT
@ AV_CHAN_BOTTOM_FRONT_RIGHT
@ AV_CHAN_STEREO_RIGHT
See above.
@ AV_CHAN_TOP_SIDE_LEFT
@ AV_CHAN_BOTTOM_FRONT_LEFT
@ AV_CHAN_TOP_CENTER
@ AV_CHAN_STEREO_LEFT
Stereo downmix.
@ AV_CHAN_TOP_FRONT_CENTER
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function.
Definition dict.h:77
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:180
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition mem.c:419
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition utils.c:28
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
int a
#define b
Definition input.c:43
#define AV_RN32(p)
#define AV_RB32(p)
#define AV_RN16(p)
#define AV_RB16(p)
enum MPFType type
Definition jpegmpfenc.c:55
unsigned offset
Definition libaomenc.c:763
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition anm.c:76
common internal api header.
#define FF_SANE_NB_CHANNELS
Definition internal.h:37
const char * arg
Definition jacosubdec.c:66
static int resync(AVFormatContext *s)
Definition flvdec.c:1169
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:390
char * ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase)
Write hexadecimal string corresponding to given binary data.
Definition utils.c:473
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
#define FF_ALLOC_TYPED_ARRAY(p, nelem)
Definition internal.h:80
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
@ AV_CLASS_CATEGORY_DEMUXER
Definition log.h:33
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
AVContentLightMetadata * av_content_light_metadata_alloc(size_t *size)
Allocate an AVContentLightMetadata structure and set its fields to default values.
AVMasteringDisplayMetadata * av_mastering_display_metadata_alloc_size(size_t *size)
Allocate an AVMasteringDisplayMetadata structure and set its fields to default values.
enum AVColorRange range
uint64_t layout
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
uint32_t tag
Definition movenc.c:2144
const MXFCodecUL ff_mxf_data_definition_uls[]
SMPTE RP224 http://www.smpte-ra.org/mdd/index.html.
Definition mxf.c:31
const uint8_t ff_mxf_random_index_pack_key[16]
Definition mxf.c:26
const MXFCodecUL ff_mxf_pixel_format_uls[]
Definition mxf.c:87
const MXFCodecUL ff_mxf_color_space_uls[]
Definition mxf.c:139
const MXFCodecUL ff_mxf_color_trc_uls[]
Definition mxf.c:114
int ff_mxf_decode_pixel_layout(const char pixel_layout[16], enum AVPixelFormat *pix_fmt)
Definition mxf.c:184
const MXFCodecUL ff_mxf_codec_tag_uls[]
Definition mxf.c:93
const MXFCodecUL ff_mxf_codec_uls[]
Definition mxf.c:40
const MXFCodecUL ff_mxf_color_primaries_uls[]
Definition mxf.c:98
@ J2KWrap
Definition mxf.h:86
@ RawAWrap
Definition mxf.h:84
@ RawVWrap
Definition mxf.h:85
@ D10D11Wrap
Definition mxf.h:83
#define FF_MXF_MasteringDisplayMinimumLuminance
Definition mxf.h:100
#define UID_ARG(x)
Definition mxf.h:132
#define PRINT_KEY(pc, s, x)
Definition mxf.h:155
#define FF_MXF_MasteringDisplayPrimaries
Definition mxf.h:97
#define FF_MXF_MasteringDisplayWhitePointChromaticity
Definition mxf.h:98
#define FF_MXF_MASTERING_CHROMA_DEN
Definition mxf.h:102
#define FF_MXF_MASTERING_LUMA_DEN
Definition mxf.h:103
#define FF_MXF_MasteringDisplay_PREFIX
Definition mxf.h:96
@ MixedFields
Definition mxf.h:65
@ SegmentedFrame
Definition mxf.h:66
@ OneField
Definition mxf.h:64
@ FullFrame
Definition mxf.h:62
@ SeparateFields
Definition mxf.h:63
AVUUID UID
Definition mxf.h:30
#define FF_MXF_MasteringDisplayMaximumLuminance
Definition mxf.h:99
MXFMetadataSetType
Definition mxf.h:32
@ Sequence
Definition mxf.h:38
@ MultipleDescriptor
Definition mxf.h:39
@ SoundfieldGroupLabelSubDescriptor
Definition mxf.h:54
@ MaterialPackage
Definition mxf.h:33
@ SourceClip
Definition mxf.h:35
@ IndexTableSegment
Definition mxf.h:47
@ MetadataSetTypeNB
Definition mxf.h:58
@ FFV1SubDescriptor
Definition mxf.h:56
@ TaggedValue
Definition mxf.h:50
@ AudioChannelLabelSubDescriptor
Definition mxf.h:53
@ EssenceContainerData
Definition mxf.h:48
@ SourcePackage
Definition mxf.h:34
@ EssenceGroup
Definition mxf.h:49
@ Descriptor
Definition mxf.h:40
@ PulldownComponent
Definition mxf.h:37
@ GroupOfSoundfieldGroupsLabelSubDescriptor
Definition mxf.h:55
@ TimecodeComponent
Definition mxf.h:36
#define PRIxUID
Definition mxf.h:126
static int mxf_read_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1207
static const uint8_t mxf_encrypted_essence_container[]
Definition mxfdec.c:357
static int mxf_read_mca_sub_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1528
static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:760
static int64_t klv_decode_ber_length(AVIOContext *pb, int *llen)
Definition mxfdec.c:440
static int mxf_set_pts(MXFContext *mxf, AVStream *st, AVPacket *pkt)
Definition mxfdec.c:4055
static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:989
static void mxf_free_metadataset(MXFMetadataSet **ctx, enum MXFMetadataSetType type)
Definition mxfdec.c:388
static int mxf_compute_ptses_fake_index(MXFContext *mxf, MXFIndexTable *index_table)
Definition mxfdec.c:2018
static const uint8_t mxf_canopus_essence_element_key[]
Definition mxfdec.c:348
static const MXFCodecUL mxf_picture_essence_container_uls[]
Definition mxfdec.c:1642
static int64_t mxf_timestamp_to_int64(uint64_t timestamp)
Definition mxfdec.c:3205
static const MXFCodecUL mxf_intra_only_essence_container_uls[]
Definition mxfdec.c:1666
static int mxf_read_essence_container_data(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1231
static const MXFChannelOrderingUL mxf_channel_ordering[]
Definition mxfdec.c:1713
static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1099
static const uint8_t mxf_soundfield_group_link_id[]
Definition mxfdec.c:373
static MXFMCASubDescriptor * find_mca_link_id(MXFContext *mxf, enum MXFMetadataSetType type, UID *mca_link_id)
Definition mxfdec.c:2609
static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
Definition mxfdec.c:1327
static MXFIndexTable * mxf_find_index_table(MXFContext *mxf, int index_sid)
Definition mxfdec.c:3649
static const uint8_t mxf_sub_descriptor[]
Definition mxfdec.c:376
static void parse_ffv1_sub_descriptor(MXFContext *mxf, MXFTrack *source_track, MXFDescriptor *descriptor, AVStream *st)
Definition mxfdec.c:2620
static const MXFMetadataReadTableEntry mxf_metadata_read_table[]
Definition mxfdec.c:3315
static int mxf_read_preface_metadata(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:3302
static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
Definition mxfdec.c:2503
static const uint8_t mxf_crypto_source_container_ul[]
Definition mxfdec.c:355
static int mxf_read_tagged_value(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1593
static const uint8_t mxf_apple_coll_max_fall[]
Definition mxfdec.c:365
static int mxf_add_metadata_set(MXFContext *mxf, MXFMetadataSet **metadata_set, enum MXFMetadataSetType type)
Definition mxfdec.c:960
static int mxf_parse_physical_source_package(MXFContext *mxf, MXFTrack *source_track, AVStream *st)
Definition mxfdec.c:2430
#define MXF_FIELD_DOMINANCE_FF
Definition mxfdec.c:211
static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
Definition mxfdec.c:662
static int mxf_parse_structural_metadata(MXFContext *mxf)
Definition mxfdec.c:2722
static const uint8_t mxf_mca_tag_name[]
Definition mxfdec.c:369
static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition mxfdec.c:4274
static int64_t mxf_set_current_edit_unit(MXFContext *mxf, AVStream *st, int64_t current_offset, int resync)
Make sure track->sample_count is correct based on what offset we're currently at.
Definition mxfdec.c:3995
static const uint8_t mxf_klv_key[]
Definition mxfdec.c:351
MXFOP
Definition mxfdec.c:80
@ OPSONYOpt
Definition mxfdec.c:91
@ OP1b
Definition mxfdec.c:82
@ OP3c
Definition mxfdec.c:89
@ OP2a
Definition mxfdec.c:84
@ OP2b
Definition mxfdec.c:85
@ OP1c
Definition mxfdec.c:83
@ OPAtom
Definition mxfdec.c:90
@ OP3b
Definition mxfdec.c:88
@ OP3a
Definition mxfdec.c:87
@ OP2c
Definition mxfdec.c:86
@ OP1a
Definition mxfdec.c:81
static const uint8_t mxf_encrypted_triplet_key[]
Definition mxfdec.c:356
static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_table, int64_t edit_unit, AVRational edit_rate, int64_t *edit_unit_out, int64_t *offset_out, MXFPartition **partition_out, int nag)
Definition mxfdec.c:1935
static const uint8_t mxf_header_partition_pack_key[]
Definition mxfdec.c:345
#define MXF_FIELD_DOMINANCE_DEFAULT
Definition mxfdec.c:210
static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
Definition mxfdec.c:1791
static const MXFCodecUL mxf_intra_only_picture_coded_width[]
Definition mxfdec.c:1679
static const MXFCodecUL mxf_data_essence_container_uls[]
Definition mxfdec.c:1700
static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:734
#define SET_TS_METADATA(pb, name, var, str)
Definition mxfdec.c:3252
static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
Definition mxfdec.c:3376
static int mxf_get_next_track_edit_unit(MXFContext *mxf, MXFTrack *track, int64_t current_offset, int64_t *edit_unit_out)
Definition mxfdec.c:3939
#define SET_UID_METADATA(pb, name, var, str)
Definition mxfdec.c:3245
static int mxf_is_st_422(const UID *essence_container_ul)
Definition mxfdec.c:2270
static const uint8_t mxf_sony_mpeg4_extradata[]
Definition mxfdec.c:358
static const uint8_t mxf_mca_link_id[]
Definition mxfdec.c:371
static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset)
Definition mxfdec.c:3258
static const uint8_t mxf_indirect_value_utf16le[]
Definition mxfdec.c:362
static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1176
#define MXF_BLACK_REF_LEVEL_PRESENT
Definition mxfdec.c:195
static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid)
Returns the end position of the essence container with given BodySID, or zero if unknown.
Definition mxfdec.c:1917
static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1347
static const uint8_t mxf_mca_channel_id[]
Definition mxfdec.c:372
static const uint8_t mxf_mastering_display_prefix[13]
Definition mxfdec.c:378
static int klv_read_packet(MXFContext *mxf, KLVPacket *klv, AVIOContext *pb)
Definition mxfdec.c:490
static int mxf_read_timecode_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1121
static const uint8_t mxf_avid_essence_element_key[]
Definition mxfdec.c:347
#define SET_VERSION_METADATA(pb, name, major, minor, tertiary, patch, release, str)
Definition mxfdec.c:3234
static int mxf_add_umid_metadata(AVDictionary **pm, const char *key, MXFPackage *package)
Definition mxfdec.c:2302
#define SET_STR_METADATA(pb, name, str)
Definition mxfdec.c:3228
#define MXF_MAX_CHUNK_SIZE
Definition mxfdec.c:71
static const uint8_t mxf_mastering_display_uls[4][16]
Definition mxfdec.c:379
static int64_t mxf_compute_sample_count(MXFContext *mxf, AVStream *st, int64_t edit_unit)
Definition mxfdec.c:3964
#define READ_STR16(type, big_endian)
Definition mxfdec.c:1076
static int mxf_read_header(AVFormatContext *s)
Definition mxfdec.c:3815
static void mxf_compute_edit_units_per_packet(MXFContext *mxf, AVStream *st)
Deal with the case where for some audio atoms EditUnitByteCount is very small (2, 4....
Definition mxfdec.c:3663
static int mxf_read_index_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
Definition mxfdec.c:1250
static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mxfdec.c:4085
static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t offset, int64_t *offset_out, MXFPartition **partition_out)
Computes the absolute file offset of the given essence container offset.
Definition mxfdec.c:1874
static int mxf_parse_handle_partition_or_eof(MXFContext *mxf)
Called when the next partition or EOF is encountered.
Definition mxfdec.c:3592
static int find_body_sid_by_absolute_offset(MXFContext *mxf, int64_t offset)
Definition mxfdec.c:531
static int mxf_parse_handle_essence(MXFContext *mxf)
Called when essence is encountered.
Definition mxfdec.c:3549
static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1287
static int mxf_read_close(AVFormatContext *s)
Definition mxfdec.c:4213
static int mxf_read_ffv1_sub_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1553
static int mxf_set_audio_pts(MXFContext *mxf, AVCodecParameters *par, AVPacket *pkt)
Definition mxfdec.c:4033
static int is_pcm(enum AVCodecID codec_id)
Definition mxfdec.c:2588
static MXFStructuralComponent * mxf_resolve_sourceclip(MXFContext *mxf, UID *strong_ref)
Definition mxfdec.c:2376
static void * mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
Definition mxfdec.c:1629
static int mxf_read_us_ascii_string(AVIOContext *pb, int size, char **str)
Definition mxfdec.c:1025
static const MXFCodecUL * mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
Definition mxfdec.c:1619
static const uint8_t mxf_mca_label_dictionary_id[]
Definition mxfdec.c:367
static int mxf_seek_to_previous_partition(MXFContext *mxf)
Seeks to the previous partition and parses it, if possible.
Definition mxfdec.c:3498
#define MXF_COLOR_RANGE_PRESENT
Definition mxfdec.c:197
static int set_language(AVFormatContext *s, const char *rfc5646, AVDictionary **met)
Definition mxfdec.c:2594
static int mxf_handle_missing_index_segment(MXFContext *mxf, AVStream *st)
Deal with the case where ClipWrapped essences does not have any IndexTableSegments.
Definition mxfdec.c:3694
static int mxf_is_partition_pack_key(UID key)
Matches any partition pack key, in other words:
Definition mxfdec.c:3456
MXFWrappingScheme
Definition mxfdec.c:94
@ FrameWrapped
Definition mxfdec.c:96
@ UnknownWrapped
Definition mxfdec.c:95
@ ClipWrapped
Definition mxfdec.c:97
static int mxf_parse_klv(MXFContext *mxf, KLVPacket klv, MXFMetadataReadFunc *read, int ctx_size, enum MXFMetadataSetType type)
Parses a metadata KLV.
Definition mxfdec.c:3467
static const uint8_t mxf_mca_rfc5646_spoken_language[]
Definition mxfdec.c:374
MXFPartitionType
Definition mxfdec.c:74
@ Footer
Definition mxfdec.c:77
@ Header
Definition mxfdec.c:75
@ BodyPartition
Definition mxfdec.c:76
int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:335
static const uint8_t mxf_avid_project_name[]
Definition mxfdec.c:360
static int mxf_read_indirect_value(void *arg, AVIOContext *pb, int size)
Definition mxfdec.c:1572
static MXFWrappingScheme mxf_get_wrapping_kind(UID *essence_container_ul)
Definition mxfdec.c:1753
static int mxf_get_eia608_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt, int64_t length)
Definition mxfdec.c:554
static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
Definition mxfdec.c:461
static int mxf_add_timecode_metadata(AVDictionary **pm, const char *key, AVTimecode *tc)
Definition mxfdec.c:2312
static const uint8_t mxf_mca_tag_symbol[]
Definition mxfdec.c:368
static void mxf_read_random_index_pack(AVFormatContext *s)
Definition mxfdec.c:3761
static int mxf_read_pulldown_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1138
static MXFPackage * mxf_resolve_source_package(MXFContext *mxf, UID package_ul, UID package_uid)
Definition mxfdec.c:2336
static enum AVColorRange mxf_get_color_range(MXFContext *mxf, MXFDescriptor *descriptor)
Definition mxfdec.c:2541
static const uint8_t mxf_apple_coll_max_cll[]
Definition mxfdec.c:364
#define IS_KLV_KEY(x, y)
Definition mxfdec.c:386
static int mxf_read_essence_group(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1193
static int mxf_version_to_str(uint16_t major, uint16_t minor, uint16_t tertiary, uint16_t patch, uint16_t release, char **str)
Definition mxfdec.c:2293
static int mxf_read_sync_klv(AVIOContext *pb)
Definition mxfdec.c:475
#define RUN_IN_MAX
Definition mxfdec.c:72
static int mxf_probe(const AVProbeData *p)
Definition mxfdec.c:4247
static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
Definition mxfdec.c:630
static uint64_t partition_score(MXFPartition *p)
Definition mxfdec.c:944
static const uint8_t mxf_essence_element_key[]
Definition mxfdec.c:346
static int mxf_match_uid(const UID key, const uint8_t uid_prefix[], int len)
Definition mxfdec.c:1609
static MXFWrappingScheme mxf_get_wrapping_by_body_sid(AVFormatContext *s, int body_sid)
Definition mxfdec.c:3597
static const MXFCodecUL mxf_intra_only_picture_essence_coding_uls[]
Definition mxfdec.c:1672
static const uint8_t mxf_indirect_value_utf16be[]
Definition mxfdec.c:363
#define MXF_FIELD_DOMINANCE_FL
Definition mxfdec.c:212
static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1085
static MXFDescriptor * mxf_resolve_descriptor(MXFContext *mxf, UID *strong_ref, int track_id)
Definition mxfdec.c:2352
#define MXF_WHITE_REF_LEVEL_PRESENT
Definition mxfdec.c:196
static int mxf_metadataset_init(MXFMetadataSet *ctx, enum MXFMetadataSetType type, MXFPartition *partition)
Definition mxfdec.c:3361
static int mxf_parse_package_comments(MXFContext *mxf, AVDictionary **pm, MXFPackage *package)
Definition mxfdec.c:2410
static const uint8_t mxf_apple_coll_prefix[]
Definition mxfdec.c:352
static const MXFCodecUL mxf_sound_essence_container_uls[]
Definition mxfdec.c:1687
static const uint8_t mxf_jp2k_rsiz[]
Definition mxfdec.c:361
static MXFTimecodeComponent * mxf_resolve_timecode_component(MXFContext *mxf, UID *strong_ref)
Definition mxfdec.c:2320
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv, int body_sid)
Definition mxfdec.c:519
static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition mxfdec.c:1149
static const uint8_t mxf_ffv1_extradata[]
Definition mxfdec.c:359
static int parse_mca_labels(MXFContext *mxf, MXFTrack *source_track, MXFDescriptor *descriptor, AVStream *st)
Definition mxfdec.c:2635
static void mxf_compute_essence_containers(AVFormatContext *s)
Figures out the proper offset and length of the essence container in each partition.
Definition mxfdec.c:3610
static int mxf_read_utf16_string(AVIOContext *pb, int size, char **str, int be)
Definition mxfdec.c:1049
static int mxf_compute_index_tables(MXFContext *mxf)
Sorts and collects index table segments into index tables.
Definition mxfdec.c:2150
static const uint8_t mxf_system_item_key_gc[]
Definition mxfdec.c:350
static int mxf_is_intra_only(MXFDescriptor *descriptor)
Definition mxfdec.c:2277
static const uint8_t mxf_group_of_soundfield_groups_link_id[]
Definition mxfdec.c:370
static void mxf_umid_to_str(const UID ul, const UID uid, char str[2+sizeof(UID) *4+1])
Definition mxfdec.c:2285
static int mxf_read_strong_ref_array(AVIOContext *pb, UID **refs, int *count)
Definition mxfdec.c:999
static const uint8_t mxf_system_item_key_cp[]
Definition mxfdec.c:349
UID codec_ul
Definition mxfenc.c:2339
UID uid
Definition mxfenc.c:2542
const UID container_ul
Definition mxfenc.c:2455
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value, saturated to the range of time_t.
Definition parseutils.c:573
misc parsing utilities
AVColorRange
Visual content value range.
Definition pixfmt.h:757
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:775
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:758
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:792
#define AV_PIX_FMT_XYZ12
Definition pixfmt.h:608
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
static const uint8_t index_table[8]
Definition siren.c:35
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
enum AVChannel id
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
union AVChannelLayout::@162063043056170047076125117143030261346263330336 u
Details about which channels are present in this layout.
int nb_channels
Number of channels in this layout.
AVChannelCustom * map
This member must be used when the channel order is AV_CHANNEL_ORDER_CUSTOM.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
enum AVColorSpace color_space
Definition codec_par.h:192
enum AVFieldOrder field_order
The order of the fields in interlaced video.
Definition codec_par.h:182
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
enum AVColorPrimaries color_primaries
Definition codec_par.h:190
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
enum AVColorTransferCharacteristic color_trc
Definition codec_par.h:191
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
enum AVColorRange color_range
Additional colorspace characteristics.
Definition codec_par.h:189
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
unsigned MaxFALL
Max average light level per frame (cd/m^2).
unsigned MaxCLL
Max content light level (cd/m^2).
Format I/O context.
Definition avformat.h:1401
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1457
AVIOContext * pb
I/O context.
Definition avformat.h:1443
AVDictionary * metadata
Metadata that applies to the whole file.
Definition avformat.h:1649
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1469
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
Mastering display metadata capable of representing the color volume of the display used to master the...
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:431
uint8_t * data
Definition packet.h:432
This structure stores compressed data.
Definition packet.h:587
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
AVDictionary * metadata
Definition avformat.h:848
void * priv_data
Definition avformat.h:793
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:817
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:857
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:902
AVRational display_aspect_ratio
display aspect ratio (0 if unknown)
Definition internal.h:328
enum AVStreamParseType need_parsing
Definition internal.h:346
uint64_t length
Definition mxf.h:77
int64_t offset
Definition mxf.h:76
int64_t next_klv
Definition mxf.h:78
UID key
Definition mxf.h:75
enum AVAudioServiceType service_type
Definition mxfdec.c:1710
enum AVChannel channel
Definition mxfdec.c:1709
UID uid
Definition mxf.h:106
unsigned matching_len
Definition mxf.h:107
int id
Definition mxf.h:108
int eia608_extract
Definition mxfdec.c:331
MXFPartition * partitions
Definition mxfdec.c:310
MXFIndexTable * index_tables
Definition mxfdec.c:330
int64_t last_forward_tell
Definition mxfdec.c:327
int nb_index_tables
Definition mxfdec.c:329
uint64_t footer_partition
Definition mxfdec.c:322
int last_forward_partition
Definition mxfdec.c:328
uint8_t * local_tags
Definition mxfdec.c:320
int local_tags_count
Definition mxfdec.c:321
int run_in
Definition mxfdec.c:324
KLVPacket current_klv_data
Definition mxfdec.c:323
MXFPartition * current_partition
Definition mxfdec.c:325
AVFormatContext * fc
Definition mxfdec.c:318
int parsing_backward
Definition mxfdec.c:326
int essence_container_data_count
Definition mxfdec.c:316
unsigned partitions_count
Definition mxfdec.c:311
struct AVAES * aesc
Definition mxfdec.c:319
MXFMetadataSetGroup metadata_set_groups[MetadataSetTypeNB]
Definition mxfdec.c:317
int packages_count
Definition mxfdec.c:314
UID * packages_refs
Definition mxfdec.c:313
MXFOP op
Definition mxfdec.c:312
UID * essence_container_data_refs
Definition mxfdec.c:315
UID source_container_ul
Definition mxfdec.c:130
MXFMetadataSet meta
Definition mxfdec.c:129
size_t mastering_size
Definition mxfdec.c:237
unsigned int component_depth
Definition mxfdec.c:217
UID color_primaries_ul
Definition mxfdec.c:233
unsigned int color_range
Definition mxfdec.c:220
enum AVPixelFormat pix_fmt
Definition mxfdec.c:232
unsigned int horiz_subsampling
Definition mxfdec.c:223
AVRational sample_rate
Definition mxfdec.c:204
int signal_standard
Definition mxfdec.c:222
int field_dominance
Definition mxfdec.c:213
int bits_per_sample
Definition mxfdec.c:215
unsigned int black_ref_level
Definition mxfdec.c:218
unsigned int vert_subsampling
Definition mxfdec.c:224
int extradata_size
Definition mxfdec.c:231
UID essence_codec_ul
Definition mxfdec.c:202
int video_line_map[2]
Definition mxfdec.c:209
UID * file_descriptors_refs
Definition mxfdec.c:225
unsigned int white_ref_level
Definition mxfdec.c:219
int64_t duration
Definition mxfdec.c:216
int file_descriptors_count
Definition mxfdec.c:226
int sub_descriptors_count
Definition mxfdec.c:228
int linked_track_id
Definition mxfdec.c:229
UID * sub_descriptors_refs
Definition mxfdec.c:227
UID color_trc_ul
Definition mxfdec.c:234
UID color_space_ul
Definition mxfdec.c:235
AVMasteringDisplayMetadata * mastering
Definition mxfdec.c:236
uint8_t * extradata
Definition mxfdec.c:230
size_t coll_size
Definition mxfdec.c:239
unsigned int range_items
MXF_*_PRESENT flags of the range items that were read.
Definition mxfdec.c:221
UID essence_container_ul
Definition mxfdec.c:201
AVContentLightMetadata * coll
Definition mxfdec.c:238
int frame_layout
Definition mxfdec.c:208
MXFMetadataSet meta
Definition mxfdec.c:200
AVRational aspect_ratio
Definition mxfdec.c:205
MXFMetadataSet meta
Definition mxfdec.c:288
MXFMetadataSet meta
Definition mxfdec.c:165
UID * structural_components_refs
Definition mxfdec.c:166
int64_t duration
Definition mxfdec.c:168
int structural_components_count
Definition mxfdec.c:167
MXFMetadataSet meta
Definition mxfdec.c:255
uint8_t * extradata
Definition mxfdec.c:256
uint64_t * stream_offset_entries
Definition mxfdec.c:270
unsigned edit_unit_byte_count
Definition mxfdec.c:262
uint64_t index_start_position
Definition mxfdec.c:266
MXFMetadataSet meta
Definition mxfdec.c:261
uint64_t index_duration
Definition mxfdec.c:267
AVRational index_edit_rate
Definition mxfdec.c:265
int8_t * temporal_offset_entries
Definition mxfdec.c:268
int64_t * ptses
Definition mxfdec.c:301
int index_sid
Definition mxfdec.c:297
AVIndexEntry * fake_index
Definition mxfdec.c:304
int nb_segments
Definition mxfdec.c:302
int64_t first_dts
Definition mxfdec.c:300
int8_t * offsets
Definition mxfdec.c:305
MXFIndexTableSegment ** segments
Definition mxfdec.c:303
UID soundfield_group_link_id
Definition mxfdec.c:246
UID mca_label_dictionary_id
Definition mxfdec.c:249
MXFMetadataSet meta
Definition mxfdec.c:243
UID * group_of_soundfield_groups_link_id_refs
Definition mxfdec.c:247
int group_of_soundfield_groups_link_id_count
Definition mxfdec.c:248
enum MXFMetadataSetType type
Definition mxfdec.c:341
MXFMetadataReadFunc * read
Definition mxfdec.c:339
MXFMetadataSet ** metadata_sets
Definition mxfdec.c:124
uint64_t partition_score
Definition mxfdec.c:120
UID * comment_refs
Definition mxfdec.c:283
UID * tracks_refs
Definition mxfdec.c:279
UID package_ul
Definition mxfdec.c:278
UID package_uid
Definition mxfdec.c:277
int tracks_count
Definition mxfdec.c:280
int comment_count
Definition mxfdec.c:284
char * name
Definition mxfdec.c:282
UID descriptor_ref
Definition mxfdec.c:281
MXFMetadataSet meta
Definition mxfdec.c:276
int pack_length
Definition mxfdec.c:112
uint64_t previous_partition
Definition mxfdec.c:104
int64_t essence_length
Definition mxfdec.c:108
int64_t index_byte_count
Definition mxfdec.c:111
int64_t pack_ofs
absolute offset of pack in file, including run-in
Definition mxfdec.c:113
int index_sid
Definition mxfdec.c:105
int64_t essence_offset
absolute offset of essence
Definition mxfdec.c:107
int body_sid
Definition mxfdec.c:106
int32_t kag_size
Definition mxfdec.c:109
KLVPacket first_essence_klv
Definition mxfdec.c:115
int64_t body_offset
Definition mxfdec.c:114
int64_t header_byte_count
Definition mxfdec.c:110
int complete
Definition mxfdec.c:102
MXFPartitionType type
Definition mxfdec.c:103
MXFMetadataSet meta
Definition mxfdec.c:160
int64_t duration
Definition mxfdec.c:148
UID * structural_components_refs
Definition mxfdec.c:146
MXFMetadataSet meta
Definition mxfdec.c:144
UID data_definition_ul
Definition mxfdec.c:145
int structural_components_count
Definition mxfdec.c:147
MXFMetadataSet meta
Definition mxfdec.c:134
char * value
Definition mxfdec.c:174
char * name
Definition mxfdec.c:173
MXFMetadataSet meta
Definition mxfdec.c:172
struct AVRational rate
Definition mxfdec.c:155
AVTimecode tc
Definition mxfdec.c:156
MXFMetadataSet meta
Definition mxfdec.c:152
int intra_only
Definition mxfdec.c:185
int64_t original_duration
Definition mxfdec.c:187
int edit_units_per_packet
Definition mxfdec.c:191
int body_sid
Definition mxfdec.c:189
MXFSequence * sequence
Definition mxfdec.c:179
uint64_t sample_count
Definition mxfdec.c:186
int64_t origin
Definition mxfdec.c:192
int index_sid
Definition mxfdec.c:188
uint8_t track_number[4]
Definition mxfdec.c:183
MXFWrappingScheme wrapping
Definition mxfdec.c:190
char * name
Definition mxfdec.c:182
AVRational edit_rate
Definition mxfdec.c:184
int track_id
Definition mxfdec.c:181
MXFMetadataSet meta
Definition mxfdec.c:178
UID sequence_ref
Definition mxfdec.c:180
Definition hls.c:80
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
Init a timecode struct with the passed parameters.
Definition timecode.c:201
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum_arg)
Load timecode string in buf.
Definition timecode.c:104
Timecode helpers header.
#define AV_TIMECODE_STR_SIZE
Definition timecode.h:33
@ AV_TIMECODE_FLAG_DROPFRAME
timecode is drop frame
Definition timecode.h:36
int size
enum AVCodecID codec_id
#define mg
int len
static double c[64]