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