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