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 int mxf_uid_to_str(UID uid, char **str)
2122 {
2123  int i;
2124  char *p;
2125  p = *str = av_mallocz(sizeof(UID) * 2 + 4 + 1);
2126  if (!p)
2127  return AVERROR(ENOMEM);
2128  for (i = 0; i < sizeof(UID); i++) {
2129  snprintf(p, 2 + 1, "%.2x", uid[i]);
2130  p += 2;
2131  if (i == 3 || i == 5 || i == 7 || i == 9) {
2132  snprintf(p, 1 + 1, "-");
2133  p++;
2134  }
2135  }
2136  return 0;
2137 }
2138 
2139 static int mxf_umid_to_str(UID ul, UID uid, char **str)
2140 {
2141  int i;
2142  char *p;
2143  p = *str = av_mallocz(sizeof(UID) * 4 + 2 + 1);
2144  if (!p)
2145  return AVERROR(ENOMEM);
2146  snprintf(p, 2 + 1, "0x");
2147  p += 2;
2148  for (i = 0; i < sizeof(UID); i++) {
2149  snprintf(p, 2 + 1, "%.2X", ul[i]);
2150  p += 2;
2151 
2152  }
2153  for (i = 0; i < sizeof(UID); i++) {
2154  snprintf(p, 2 + 1, "%.2X", uid[i]);
2155  p += 2;
2156  }
2157  return 0;
2158 }
2159 
2160 static int mxf_version_to_str(uint16_t major, uint16_t minor, uint16_t tertiary,
2161  uint16_t patch, uint16_t release, char **str)
2162 {
2163  *str = av_asprintf("%d.%d.%d.%d.%d", major, minor, tertiary, patch, release);
2164  if (!*str)
2165  return AVERROR(ENOMEM);
2166  return 0;
2167 }
2168 
2169 static int mxf_add_umid_metadata(AVDictionary **pm, const char *key, MXFPackage* package)
2170 {
2171  char *str;
2172  int ret;
2173  if (!package)
2174  return 0;
2175  if ((ret = mxf_umid_to_str(package->package_ul, package->package_uid, &str)) < 0)
2176  return ret;
2178  return 0;
2179 }
2180 
2181 static int mxf_add_timecode_metadata(AVDictionary **pm, const char *key, AVTimecode *tc)
2182 {
2183  char buf[AV_TIMECODE_STR_SIZE];
2184  av_dict_set(pm, key, av_timecode_make_string(tc, buf, 0), 0);
2185 
2186  return 0;
2187 }
2188 
2190 {
2191  MXFStructuralComponent *component = NULL;
2192  MXFPulldownComponent *pulldown = NULL;
2193 
2194  component = mxf_resolve_strong_ref(mxf, strong_ref, AnyType);
2195  if (!component)
2196  return NULL;
2197 
2198  switch (component->meta.type) {
2199  case TimecodeComponent:
2200  return (MXFTimecodeComponent*)component;
2201  case PulldownComponent: /* timcode component may be located on a pulldown component */
2202  pulldown = (MXFPulldownComponent*)component;
2204  default:
2205  break;
2206  }
2207  return NULL;
2208 }
2209 
2210 static MXFPackage* mxf_resolve_source_package(MXFContext *mxf, UID package_ul, UID package_uid)
2211 {
2212  MXFPackage *package = NULL;
2213  int i;
2214 
2215  for (i = 0; i < mxf->packages_count; i++) {
2216  package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], SourcePackage);
2217  if (!package)
2218  continue;
2219 
2220  if (!memcmp(package->package_ul, package_ul, 16) && !memcmp(package->package_uid, package_uid, 16))
2221  return package;
2222  }
2223  return NULL;
2224 }
2225 
2226 static MXFDescriptor* mxf_resolve_multidescriptor(MXFContext *mxf, MXFDescriptor *descriptor, int track_id)
2227 {
2228  MXFDescriptor *file_descriptor = NULL;
2229  int i;
2230 
2231  if (!descriptor)
2232  return NULL;
2233 
2234  if (descriptor->meta.type == MultipleDescriptor) {
2235  for (i = 0; i < descriptor->file_descriptors_count; i++) {
2236  file_descriptor = mxf_resolve_strong_ref(mxf, &descriptor->file_descriptors_refs[i], Descriptor);
2237 
2238  if (!file_descriptor) {
2239  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve file descriptor strong ref\n");
2240  continue;
2241  }
2242  if (file_descriptor->linked_track_id == track_id) {
2243  return file_descriptor;
2244  }
2245  }
2246  } else if (descriptor->meta.type == Descriptor)
2247  return descriptor;
2248 
2249  return NULL;
2250 }
2251 
2253 {
2254  MXFStructuralComponent *component = NULL;
2255  MXFPackage *package = NULL;
2256  MXFDescriptor *descriptor = NULL;
2257  int i;
2258 
2259  if (!essence_group || !essence_group->structural_components_count)
2260  return NULL;
2261 
2262  /* essence groups contains multiple representations of the same media,
2263  this return the first components with a valid Descriptor typically index 0 */
2264  for (i =0; i < essence_group->structural_components_count; i++){
2265  component = mxf_resolve_strong_ref(mxf, &essence_group->structural_components_refs[i], SourceClip);
2266  if (!component)
2267  continue;
2268 
2269  if (!(package = mxf_resolve_source_package(mxf, component->source_package_ul, component->source_package_uid)))
2270  continue;
2271 
2272  descriptor = mxf_resolve_strong_ref(mxf, &package->descriptor_ref, Descriptor);
2273  if (descriptor)
2274  return component;
2275  }
2276  return NULL;
2277 }
2278 
2280 {
2281  MXFStructuralComponent *component = NULL;
2282 
2283  component = mxf_resolve_strong_ref(mxf, strong_ref, AnyType);
2284  if (!component)
2285  return NULL;
2286  switch (component->meta.type) {
2287  case SourceClip:
2288  return component;
2289  case EssenceGroup:
2290  return mxf_resolve_essence_group_choice(mxf, (MXFEssenceGroup*) component);
2291  default:
2292  break;
2293  }
2294  return NULL;
2295 }
2296 
2298 {
2300  int i;
2301  char *key = NULL;
2302 
2303  for (i = 0; i < package->comment_count; i++) {
2304  tag = mxf_resolve_strong_ref(mxf, &package->comment_refs[i], TaggedValue);
2305  if (!tag || !tag->name || !tag->value)
2306  continue;
2307 
2308  key = av_asprintf("comment_%s", tag->name);
2309  if (!key)
2310  return AVERROR(ENOMEM);
2311 
2313  }
2314  return 0;
2315 }
2316 
2318 {
2319  MXFPackage *physical_package = NULL;
2320  MXFTrack *physical_track = NULL;
2321  MXFStructuralComponent *sourceclip = NULL;
2322  MXFTimecodeComponent *mxf_tc = NULL;
2323  int i, j, k;
2324  AVTimecode tc;
2325  int flags;
2326  int64_t start_position;
2327 
2328  for (i = 0; i < source_track->sequence->structural_components_count; i++) {
2329  sourceclip = mxf_resolve_strong_ref(mxf, &source_track->sequence->structural_components_refs[i], SourceClip);
2330  if (!sourceclip)
2331  continue;
2332 
2333  if (!(physical_package = mxf_resolve_source_package(mxf, sourceclip->source_package_ul, sourceclip->source_package_uid)))
2334  break;
2335 
2336  mxf_add_umid_metadata(&st->metadata, "reel_umid", physical_package);
2337 
2338  /* the name of physical source package is name of the reel or tape */
2339  if (physical_package->name && physical_package->name[0])
2340  av_dict_set(&st->metadata, "reel_name", physical_package->name, 0);
2341 
2342  /* the source timecode is calculated by adding the start_position of the sourceclip from the file source package track
2343  * to the start_frame of the timecode component located on one of the tracks of the physical source package.
2344  */
2345  for (j = 0; j < physical_package->tracks_count; j++) {
2346  if (!(physical_track = mxf_resolve_strong_ref(mxf, &physical_package->tracks_refs[j], Track))) {
2347  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
2348  continue;
2349  }
2350 
2351  if (!(physical_track->sequence = mxf_resolve_strong_ref(mxf, &physical_track->sequence_ref, Sequence))) {
2352  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
2353  continue;
2354  }
2355 
2356  if (physical_track->edit_rate.num <= 0 ||
2357  physical_track->edit_rate.den <= 0) {
2358  av_log(mxf->fc, AV_LOG_WARNING,
2359  "Invalid edit rate (%d/%d) found on structural"
2360  " component #%d, defaulting to 25/1\n",
2361  physical_track->edit_rate.num,
2362  physical_track->edit_rate.den, i);
2363  physical_track->edit_rate = (AVRational){25, 1};
2364  }
2365 
2366  for (k = 0; k < physical_track->sequence->structural_components_count; k++) {
2367  if (!(mxf_tc = mxf_resolve_timecode_component(mxf, &physical_track->sequence->structural_components_refs[k])))
2368  continue;
2369 
2370  flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
2371  /* scale sourceclip start_position to match physical track edit rate */
2372  start_position = av_rescale_q(sourceclip->start_position,
2373  physical_track->edit_rate,
2374  source_track->edit_rate);
2375 
2376  if (av_timecode_init(&tc, mxf_tc->rate, flags, start_position + mxf_tc->start_frame, mxf->fc) == 0) {
2377  mxf_add_timecode_metadata(&st->metadata, "timecode", &tc);
2378  return 0;
2379  }
2380  }
2381  }
2382  }
2383 
2384  return 0;
2385 }
2386 
2388 {
2389  MXFStructuralComponent *component = NULL;
2390  const MXFCodecUL *codec_ul = NULL;
2391  MXFPackage tmp_package;
2392  AVStream *st;
2393  int j;
2394 
2395  for (j = 0; j < track->sequence->structural_components_count; j++) {
2396  component = mxf_resolve_sourceclip(mxf, &track->sequence->structural_components_refs[j]);
2397  if (!component)
2398  continue;
2399  break;
2400  }
2401  if (!component)
2402  return 0;
2403 
2404  st = avformat_new_stream(mxf->fc, NULL);
2405  if (!st) {
2406  av_log(mxf->fc, AV_LOG_ERROR, "could not allocate metadata stream\n");
2407  return AVERROR(ENOMEM);
2408  }
2409 
2412  st->id = track->track_id;
2413 
2414  memcpy(&tmp_package.package_ul, component->source_package_ul, 16);
2415  memcpy(&tmp_package.package_uid, component->source_package_uid, 16);
2416  mxf_add_umid_metadata(&st->metadata, "file_package_umid", &tmp_package);
2417  if (track->name && track->name[0])
2418  av_dict_set(&st->metadata, "track_name", track->name, 0);
2419 
2421  av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
2422  return 0;
2423 }
2424 
2426 {
2427  if (descriptor->black_ref_level || descriptor->white_ref_level || descriptor->color_range) {
2428  /* CDCI range metadata */
2429  if (!descriptor->component_depth)
2430  return AVCOL_RANGE_UNSPECIFIED;
2431  if (descriptor->black_ref_level == 0 && descriptor->component_depth < 31 &&
2432  descriptor->white_ref_level == ((1<<descriptor->component_depth) - 1) &&
2433  (descriptor->color_range == (1<<descriptor->component_depth) ||
2434  descriptor->color_range == ((1<<descriptor->component_depth) - 1)))
2435  return AVCOL_RANGE_JPEG;
2436  if (descriptor->component_depth >= 8 && descriptor->component_depth < 31 &&
2437  descriptor->black_ref_level == (1 <<(descriptor->component_depth - 4)) &&
2438  descriptor->white_ref_level == (235<<(descriptor->component_depth - 8)) &&
2439  descriptor->color_range == ((14<<(descriptor->component_depth - 4)) + 1))
2440  return AVCOL_RANGE_MPEG;
2441  avpriv_request_sample(mxf->fc, "Unrecognized CDCI color range (color diff range %d, b %d, w %d, depth %d)",
2442  descriptor->color_range, descriptor->black_ref_level,
2443  descriptor->white_ref_level, descriptor->component_depth);
2444  }
2445 
2446  return AVCOL_RANGE_UNSPECIFIED;
2447 }
2448 
2449 static int is_pcm(enum AVCodecID codec_id)
2450 {
2451  /* we only care about "normal" PCM codecs until we get samples */
2453 }
2454 
2455 static int set_language(AVFormatContext *s, const char *rfc5646, AVDictionary **met)
2456 {
2457  // language abbr should contain at least 2 chars
2458  if (rfc5646 && strlen(rfc5646) > 1) {
2459  char primary_tag[4] =
2460  {rfc5646[0], rfc5646[1], rfc5646[2] != '-' ? rfc5646[2] : '\0', '\0'};
2461 
2462  const char *iso6392 = ff_convert_lang_to(primary_tag,
2464  if (iso6392)
2465  return(av_dict_set(met, "language", iso6392, 0));
2466  }
2467  return 0;
2468 }
2469 
2471 {
2472  for (int k = 0; k < mxf->metadata_sets_count; k++) {
2474  if (group->meta.type == type && !memcmp(&group->mca_link_id, mca_link_id, 16))
2475  return group;
2476  }
2477  return NULL;
2478 }
2479 
2480 static int parse_mca_labels(MXFContext *mxf, MXFTrack *source_track, MXFDescriptor *descriptor, AVStream *st)
2481 {
2482  uint64_t routing[FF_SANE_NB_CHANNELS] = {0};
2483  char *language = NULL;
2484  int ambigous_language = 0;
2485  enum AVAudioServiceType service_type = AV_AUDIO_SERVICE_TYPE_NB;
2486  int ambigous_service_type = 0;
2487  int has_channel_label = 0;
2488 
2489  for (int i = 0; i < descriptor->sub_descriptors_count; i++) {
2490  char *channel_language;
2491 
2493  if (label == NULL)
2494  continue;
2495 
2496  has_channel_label = 1;
2497  for (const MXFChannelOrderingUL* channel_ordering = mxf_channel_ordering; channel_ordering->uid[0]; channel_ordering++) {
2498  if (IS_KLV_KEY(channel_ordering->uid, label->mca_label_dictionary_id)) {
2499  int target_channel = label->mca_channel_id;
2500  if (target_channel == 0 && descriptor->channels == 1)
2501  target_channel = 1;
2502  if (target_channel <= 0 || target_channel > descriptor->channels) {
2503  av_log(mxf->fc, AV_LOG_ERROR, "AudioChannelLabelSubDescriptor has invalid MCA channel ID %d\n", target_channel);
2504  return AVERROR_INVALIDDATA;
2505  }
2506  routing[target_channel - 1] = channel_ordering->layout_mask;
2507  if (service_type == AV_AUDIO_SERVICE_TYPE_NB)
2508  service_type = channel_ordering->service_type;
2509  else if (service_type != channel_ordering->service_type)
2510  ambigous_service_type = 1;
2511  break;
2512  }
2513  }
2514 
2515  channel_language = label->language;
2516  if (!channel_language) {
2518  if (group) {
2519  channel_language = group->language;
2520  if (!channel_language && group->group_of_soundfield_groups_link_id_count) {
2523  if (supergroup)
2524  channel_language = supergroup->language;
2525  }
2526  }
2527  }
2528  if (channel_language) {
2529  if (language && strcmp(language, channel_language))
2530  ambigous_language = 1;
2531  else
2532  language = channel_language;
2533  }
2534  }
2535 
2536  if (language && !ambigous_language) {
2537  int ret = set_language(mxf->fc, language, &st->metadata);
2538  if (ret < 0)
2539  return ret;
2540  }
2541 
2542  if (service_type != AV_AUDIO_SERVICE_TYPE_NB && service_type != AV_AUDIO_SERVICE_TYPE_MAIN && !ambigous_service_type) {
2543  enum AVAudioServiceType *ast;
2544  uint8_t* side_data = av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE, sizeof(*ast));
2545  if (!side_data)
2546  return AVERROR(ENOMEM);
2547  ast = (enum AVAudioServiceType*)side_data;
2548  *ast = service_type;
2549  }
2550 
2551  if (has_channel_label) {
2552  uint64_t channel_layout = 0;
2553  int ret;
2554 
2555  for (int i = 0; i < descriptor->channels; i++) {
2556  if (!routing[i]) {
2557  av_log(mxf->fc, AV_LOG_WARNING, "Designation of audio channel %d in stream #%d is unknown or unsupported, "
2558  "falling back to unknown channel layout\n", st->index, i);
2559  return 0;
2560  }
2561  if (channel_layout & routing[i]) {
2562  char buf[32];
2563  av_channel_name(buf, sizeof(buf), routing[i]);
2564  av_log(mxf->fc, AV_LOG_WARNING, "%s audio channel is used multiple times in stream #%d, "
2565  "falling back to unknown channel layout\n",
2566  buf, st->index);
2567  return 0;
2568  }
2569  if (routing[i] < channel_layout) {
2570  av_log(mxf->fc, AV_LOG_WARNING, "stream #%d is not in in native channel order, "
2571  "falling back to unknown channel layout\n", st->index);
2572  return 0;
2573  }
2574  channel_layout |= routing[i];
2575  }
2576 
2577  av_assert0(descriptor->channels == av_popcount64(channel_layout));
2578 
2579  ret = av_channel_layout_from_mask(&st->codecpar->ch_layout, channel_layout);
2580  if (ret < 0)
2581  return ret;
2582  }
2583 
2584  return 0;
2585 }
2586 
2588 {
2589  MXFPackage *material_package = NULL;
2590  int i, j, k, ret;
2591 
2592  av_log(mxf->fc, AV_LOG_TRACE, "metadata sets count %d\n", mxf->metadata_sets_count);
2593  /* TODO: handle multiple material packages (OP3x) */
2594  for (i = 0; i < mxf->packages_count; i++) {
2595  material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
2596  if (material_package) break;
2597  }
2598  if (!material_package) {
2599  av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
2600  return AVERROR_INVALIDDATA;
2601  }
2602 
2603  mxf_add_umid_metadata(&mxf->fc->metadata, "material_package_umid", material_package);
2604  if (material_package->name && material_package->name[0])
2605  av_dict_set(&mxf->fc->metadata, "material_package_name", material_package->name, 0);
2606  mxf_parse_package_comments(mxf, &mxf->fc->metadata, material_package);
2607 
2608  for (i = 0; i < material_package->tracks_count; i++) {
2609  MXFPackage *source_package = NULL;
2610  MXFTrack *material_track = NULL;
2611  MXFTrack *source_track = NULL;
2612  MXFTrack *temp_track = NULL;
2613  MXFDescriptor *descriptor = NULL;
2614  MXFStructuralComponent *component = NULL;
2615  MXFTimecodeComponent *mxf_tc = NULL;
2616  UID *essence_container_ul = NULL;
2617  const MXFCodecUL *codec_ul = NULL;
2618  const MXFCodecUL *container_ul = NULL;
2619  const MXFCodecUL *pix_fmt_ul = NULL;
2620  AVStream *st;
2621  FFStream *sti;
2622  AVTimecode tc;
2623  int flags;
2624 
2625  if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
2626  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
2627  continue;
2628  }
2629 
2630  if ((component = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, TimecodeComponent))) {
2631  mxf_tc = (MXFTimecodeComponent*)component;
2632  flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
2633  if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
2634  mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
2635  }
2636  }
2637 
2638  if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
2639  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
2640  continue;
2641  }
2642 
2643  for (j = 0; j < material_track->sequence->structural_components_count; j++) {
2644  component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], TimecodeComponent);
2645  if (!component)
2646  continue;
2647 
2648  mxf_tc = (MXFTimecodeComponent*)component;
2649  flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
2650  if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
2651  mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
2652  break;
2653  }
2654  }
2655 
2656  /* TODO: handle multiple source clips, only finds first valid source clip */
2657  if(material_track->sequence->structural_components_count > 1)
2658  av_log(mxf->fc, AV_LOG_WARNING, "material track %d: has %d components\n",
2659  material_track->track_id, material_track->sequence->structural_components_count);
2660 
2661  for (j = 0; j < material_track->sequence->structural_components_count; j++) {
2662  component = mxf_resolve_sourceclip(mxf, &material_track->sequence->structural_components_refs[j]);
2663  if (!component)
2664  continue;
2665 
2666  source_package = mxf_resolve_source_package(mxf, component->source_package_ul, component->source_package_uid);
2667  if (!source_package) {
2668  av_log(mxf->fc, AV_LOG_TRACE, "material track %d: no corresponding source package found\n", material_track->track_id);
2669  continue;
2670  }
2671  for (k = 0; k < source_package->tracks_count; k++) {
2672  if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
2673  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
2675  goto fail_and_free;
2676  }
2677  if (temp_track->track_id == component->source_track_id) {
2678  source_track = temp_track;
2679  break;
2680  }
2681  }
2682  if (!source_track) {
2683  av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
2684  break;
2685  }
2686 
2687  for (k = 0; k < mxf->essence_container_data_count; k++) {
2688  MXFEssenceContainerData *essence_data;
2689 
2690  if (!(essence_data = mxf_resolve_strong_ref(mxf, &mxf->essence_container_data_refs[k], EssenceContainerData))) {
2691  av_log(mxf->fc, AV_LOG_TRACE, "could not resolve essence container data strong ref\n");
2692  continue;
2693  }
2694  if (!memcmp(component->source_package_ul, essence_data->package_ul, sizeof(UID)) && !memcmp(component->source_package_uid, essence_data->package_uid, sizeof(UID))) {
2695  source_track->body_sid = essence_data->body_sid;
2696  source_track->index_sid = essence_data->index_sid;
2697  break;
2698  }
2699  }
2700 
2701  if(source_track && component)
2702  break;
2703  }
2704  if (!source_track || !component || !source_package) {
2705  if((ret = mxf_add_metadata_stream(mxf, material_track)))
2706  goto fail_and_free;
2707  continue;
2708  }
2709 
2710  if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
2711  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
2713  goto fail_and_free;
2714  }
2715 
2716  /* 0001GL00.MXF.A1.mxf_opatom.mxf has the same SourcePackageID as 0001GL.MXF.V1.mxf_opatom.mxf
2717  * This would result in both files appearing to have two streams. Work around this by sanity checking DataDefinition */
2718  if (memcmp(material_track->sequence->data_definition_ul, source_track->sequence->data_definition_ul, 16)) {
2719  av_log(mxf->fc, AV_LOG_ERROR, "material track %d: DataDefinition mismatch\n", material_track->track_id);
2720  continue;
2721  }
2722 
2723  st = avformat_new_stream(mxf->fc, NULL);
2724  if (!st) {
2725  av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
2726  ret = AVERROR(ENOMEM);
2727  goto fail_and_free;
2728  }
2729  sti = ffstream(st);
2730  st->id = material_track->track_id;
2731  st->priv_data = source_track;
2732 
2733  source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
2734  descriptor = mxf_resolve_multidescriptor(mxf, source_package->descriptor, source_track->track_id);
2735 
2736  /* A SourceClip from a EssenceGroup may only be a single frame of essence data. The clips duration is then how many
2737  * frames its suppose to repeat for. Descriptor->duration, if present, contains the real duration of the essence data */
2738  if (descriptor && descriptor->duration != AV_NOPTS_VALUE)
2739  source_track->original_duration = st->duration = FFMIN(descriptor->duration, component->duration);
2740  else
2741  source_track->original_duration = st->duration = component->duration;
2742 
2743  if (st->duration == -1)
2744  st->duration = AV_NOPTS_VALUE;
2745  st->start_time = component->start_position;
2746  if (material_track->edit_rate.num <= 0 ||
2747  material_track->edit_rate.den <= 0) {
2748  av_log(mxf->fc, AV_LOG_WARNING,
2749  "Invalid edit rate (%d/%d) found on stream #%d, "
2750  "defaulting to 25/1\n",
2751  material_track->edit_rate.num,
2752  material_track->edit_rate.den, st->index);
2753  material_track->edit_rate = (AVRational){25, 1};
2754  }
2755  avpriv_set_pts_info(st, 64, material_track->edit_rate.den, material_track->edit_rate.num);
2756 
2757  /* ensure SourceTrack EditRate == MaterialTrack EditRate since only
2758  * the former is accessible via st->priv_data */
2759  source_track->edit_rate = material_track->edit_rate;
2760 
2761  PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
2763  st->codecpar->codec_type = codec_ul->id;
2764 
2765  if (!descriptor) {
2766  av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
2767  continue;
2768  }
2769  PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
2770  PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
2771  essence_container_ul = &descriptor->essence_container_ul;
2772  source_track->wrapping = (mxf->op == OPAtom) ? ClipWrapped : mxf_get_wrapping_kind(essence_container_ul);
2773  if (source_track->wrapping == UnknownWrapped)
2774  av_log(mxf->fc, AV_LOG_INFO, "wrapping of stream %d is unknown\n", st->index);
2775  /* HACK: replacing the original key with mxf_encrypted_essence_container
2776  * is not allowed according to s429-6, try to find correct information anyway */
2777  if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
2778  av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
2779  for (k = 0; k < mxf->metadata_sets_count; k++) {
2780  MXFMetadataSet *metadata = mxf->metadata_sets[k];
2781  if (metadata->type == CryptoContext) {
2782  essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
2783  break;
2784  }
2785  }
2786  }
2787 
2788  /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
2790  st->codecpar->codec_id = (enum AVCodecID)codec_ul->id;
2791  if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
2793  st->codecpar->codec_id = (enum AVCodecID)codec_ul->id;
2794  }
2795 
2796  av_log(mxf->fc, AV_LOG_VERBOSE, "%s: Universal Label: ",
2798  for (k = 0; k < 16; k++) {
2799  av_log(mxf->fc, AV_LOG_VERBOSE, "%.2x",
2800  descriptor->essence_codec_ul[k]);
2801  if (!(k+1 & 19) || k == 5)
2802  av_log(mxf->fc, AV_LOG_VERBOSE, ".");
2803  }
2804  av_log(mxf->fc, AV_LOG_VERBOSE, "\n");
2805 
2806  mxf_add_umid_metadata(&st->metadata, "file_package_umid", source_package);
2807  if (source_package->name && source_package->name[0])
2808  av_dict_set(&st->metadata, "file_package_name", source_package->name, 0);
2809  if (material_track->name && material_track->name[0])
2810  av_dict_set(&st->metadata, "track_name", material_track->name, 0);
2811 
2812  mxf_parse_physical_source_package(mxf, source_track, st);
2813 
2814  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2815  source_track->intra_only = mxf_is_intra_only(descriptor);
2817  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
2818  st->codecpar->codec_id = container_ul->id;
2819  st->codecpar->width = descriptor->width;
2820  st->codecpar->height = descriptor->height; /* Field height, not frame height */
2821  switch (descriptor->frame_layout) {
2822  case FullFrame:
2824  break;
2825  case OneField:
2826  /* Every other line is stored and needs to be duplicated. */
2827  av_log(mxf->fc, AV_LOG_INFO, "OneField frame layout isn't currently supported\n");
2828  break; /* The correct thing to do here is fall through, but by breaking we might be
2829  able to decode some streams at half the vertical resolution, rather than not al all.
2830  It's also for compatibility with the old behavior. */
2831  case MixedFields:
2832  break;
2833  case SegmentedFrame:
2835  case SeparateFields:
2836  av_log(mxf->fc, AV_LOG_DEBUG, "video_line_map: (%d, %d), field_dominance: %d\n",
2837  descriptor->video_line_map[0], descriptor->video_line_map[1],
2838  descriptor->field_dominance);
2839  if ((descriptor->video_line_map[0] > 0) && (descriptor->video_line_map[1] > 0)) {
2840  /* Detect coded field order from VideoLineMap:
2841  * (even, even) => bottom field coded first
2842  * (even, odd) => top field coded first
2843  * (odd, even) => top field coded first
2844  * (odd, odd) => bottom field coded first
2845  */
2846  if ((descriptor->video_line_map[0] + descriptor->video_line_map[1]) % 2) {
2847  switch (descriptor->field_dominance) {
2851  break;
2854  break;
2855  default:
2857  "Field dominance %d support",
2858  descriptor->field_dominance);
2859  }
2860  } else {
2861  switch (descriptor->field_dominance) {
2865  break;
2868  break;
2869  default:
2871  "Field dominance %d support",
2872  descriptor->field_dominance);
2873  }
2874  }
2875  }
2876  /* Turn field height into frame height. */
2877  st->codecpar->height *= 2;
2878  break;
2879  default:
2880  av_log(mxf->fc, AV_LOG_INFO, "Unknown frame layout type: %d\n", descriptor->frame_layout);
2881  }
2882 
2883  if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
2884  switch (descriptor->essence_codec_ul[14]) {
2885  case 1: st->codecpar->codec_tag = MKTAG('a','p','c','o'); break;
2886  case 2: st->codecpar->codec_tag = MKTAG('a','p','c','s'); break;
2887  case 3: st->codecpar->codec_tag = MKTAG('a','p','c','n'); break;
2888  case 4: st->codecpar->codec_tag = MKTAG('a','p','c','h'); break;
2889  case 5: st->codecpar->codec_tag = MKTAG('a','p','4','h'); break;
2890  case 6: st->codecpar->codec_tag = MKTAG('a','p','4','x'); break;
2891  }
2892  }
2893 
2894  if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
2895  st->codecpar->format = descriptor->pix_fmt;
2896  if (st->codecpar->format == AV_PIX_FMT_NONE) {
2898  &descriptor->essence_codec_ul);
2899  st->codecpar->format = (enum AVPixelFormat)pix_fmt_ul->id;
2900  if (st->codecpar->format== AV_PIX_FMT_NONE) {
2902  &descriptor->essence_codec_ul)->id;
2903  if (!st->codecpar->codec_tag) {
2904  /* support files created before RP224v10 by defaulting to UYVY422
2905  if subsampling is 4:2:2 and component depth is 8-bit */
2906  if (descriptor->horiz_subsampling == 2 &&
2907  descriptor->vert_subsampling == 1 &&
2908  descriptor->component_depth == 8) {
2910  }
2911  }
2912  }
2913  }
2914  }
2916  if (material_track->sequence->origin) {
2917  av_dict_set_int(&st->metadata, "material_track_origin", material_track->sequence->origin, 0);
2918  }
2919  if (source_track->sequence->origin) {
2920  av_dict_set_int(&st->metadata, "source_track_origin", source_track->sequence->origin, 0);
2921  }
2922  if (descriptor->aspect_ratio.num && descriptor->aspect_ratio.den)
2923  sti->display_aspect_ratio = descriptor->aspect_ratio;
2924  st->codecpar->color_range = mxf_get_color_range(mxf, descriptor);
2928  if (descriptor->mastering) {
2930  (uint8_t *)descriptor->mastering,
2931  sizeof(*descriptor->mastering));
2932  if (ret < 0)
2933  goto fail_and_free;
2934  descriptor->mastering = NULL;
2935  }
2936  if (descriptor->coll) {
2938  (uint8_t *)descriptor->coll,
2939  descriptor->coll_size);
2940  if (ret < 0)
2941  goto fail_and_free;
2942  descriptor->coll = NULL;
2943  }
2944  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2946  /* Only overwrite existing codec ID if it is unset or A-law, which is the default according to SMPTE RP 224. */
2948  st->codecpar->codec_id = (enum AVCodecID)container_ul->id;
2949  st->codecpar->ch_layout.nb_channels = descriptor->channels;
2950 
2951  if (descriptor->sample_rate.den > 0) {
2952  st->codecpar->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
2953  avpriv_set_pts_info(st, 64, descriptor->sample_rate.den, descriptor->sample_rate.num);
2954  } else {
2955  av_log(mxf->fc, AV_LOG_WARNING, "invalid sample rate (%d/%d) "
2956  "found for stream #%d, time base forced to 1/48000\n",
2957  descriptor->sample_rate.num, descriptor->sample_rate.den,
2958  st->index);
2959  avpriv_set_pts_info(st, 64, 1, 48000);
2960  }
2961 
2962  /* if duration is set, rescale it from EditRate to SampleRate */
2963  if (st->duration != AV_NOPTS_VALUE)
2964  st->duration = av_rescale_q(st->duration,
2965  av_inv_q(material_track->edit_rate),
2966  st->time_base);
2967 
2968  /* TODO: implement AV_CODEC_ID_RAWAUDIO */
2969  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) {
2970  if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
2972  else if (descriptor->bits_per_sample == 32)
2974  } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE) {
2975  if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
2977  else if (descriptor->bits_per_sample == 32)
2979  } else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) {
2981  }
2983 
2984  if (descriptor->channels <= 0 || descriptor->channels >= FF_SANE_NB_CHANNELS) {
2985  av_log(mxf->fc, AV_LOG_ERROR, "Invalid number of channels %d, must be less than %d\n", descriptor->channels, FF_SANE_NB_CHANNELS);
2986  return AVERROR_INVALIDDATA;
2987  }
2988 
2989  ret = parse_mca_labels(mxf, source_track, descriptor, st);
2990  if (ret < 0)
2991  return ret;
2992  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
2993  enum AVMediaType type;
2995  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
2996  st->codecpar->codec_id = container_ul->id;
2998  if (type == AVMEDIA_TYPE_SUBTITLE)
2999  st->codecpar->codec_type = type;
3000  if (container_ul->desc)
3001  av_dict_set(&st->metadata, "data_type", container_ul->desc, 0);
3002  if (mxf->eia608_extract &&
3003  !strcmp(container_ul->desc, "vbi_vanc_smpte_436M")) {
3006  }
3007  }
3008  if (descriptor->extradata) {
3009  if (!ff_alloc_extradata(st->codecpar, descriptor->extradata_size)) {
3010  memcpy(st->codecpar->extradata, descriptor->extradata, descriptor->extradata_size);
3011  }
3012  } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
3014  &descriptor->essence_codec_ul)->id;
3015  if (coded_width)
3016  st->codecpar->width = coded_width;
3018  if (ret < 0)
3019  return ret;
3020  }
3021  if (st->codecpar->codec_type != AVMEDIA_TYPE_DATA && source_track->wrapping != FrameWrapped) {
3022  /* TODO: decode timestamps */
3024  }
3025  }
3026 
3027  for (int i = 0; i < mxf->fc->nb_streams; i++) {
3028  MXFTrack *track1 = mxf->fc->streams[i]->priv_data;
3029  if (track1 && track1->body_sid) {
3030  for (int j = i + 1; j < mxf->fc->nb_streams; j++) {
3031  MXFTrack *track2 = mxf->fc->streams[j]->priv_data;
3032  if (track2 && track1->body_sid == track2->body_sid && track1->wrapping != track2->wrapping) {
3033  if (track1->wrapping == UnknownWrapped)
3034  track1->wrapping = track2->wrapping;
3035  else if (track2->wrapping == UnknownWrapped)
3036  track2->wrapping = track1->wrapping;
3037  else
3038  av_log(mxf->fc, AV_LOG_ERROR, "stream %d and stream %d have the same BodySID (%d) "
3039  "with different wrapping\n", i, j, track1->body_sid);
3040  }
3041  }
3042  }
3043  }
3044 
3045  ret = 0;
3046 fail_and_free:
3047  return ret;
3048 }
3049 
3050 static int64_t mxf_timestamp_to_int64(uint64_t timestamp)
3051 {
3052  struct tm time = { 0 };
3053  int msecs;
3054  time.tm_year = (timestamp >> 48) - 1900;
3055  time.tm_mon = (timestamp >> 40 & 0xFF) - 1;
3056  time.tm_mday = (timestamp >> 32 & 0xFF);
3057  time.tm_hour = (timestamp >> 24 & 0xFF);
3058  time.tm_min = (timestamp >> 16 & 0xFF);
3059  time.tm_sec = (timestamp >> 8 & 0xFF);
3060  msecs = (timestamp & 0xFF) * 4;
3061 
3062  /* Clip values for legacy reasons. Maybe we should return error instead? */
3063  time.tm_mon = av_clip(time.tm_mon, 0, 11);
3064  time.tm_mday = av_clip(time.tm_mday, 1, 31);
3065  time.tm_hour = av_clip(time.tm_hour, 0, 23);
3066  time.tm_min = av_clip(time.tm_min, 0, 59);
3067  time.tm_sec = av_clip(time.tm_sec, 0, 59);
3068  msecs = av_clip(msecs, 0, 999);
3069 
3070  return (int64_t)av_timegm(&time) * 1000000 + msecs * 1000;
3071 }
3072 
3073 #define SET_STR_METADATA(pb, name, str) do { \
3074  if ((ret = mxf_read_utf16be_string(pb, size, &str)) < 0) \
3075  return ret; \
3076  av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
3077 } while (0)
3078 
3079 #define SET_VERSION_METADATA(pb, name, major, minor, tertiary, patch, release, str) do { \
3080  major = avio_rb16(pb); \
3081  minor = avio_rb16(pb); \
3082  tertiary = avio_rb16(pb); \
3083  patch = avio_rb16(pb); \
3084  release = avio_rb16(pb); \
3085  if ((ret = mxf_version_to_str(major, minor, tertiary, patch, release, &str)) < 0) \
3086  return ret; \
3087  av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
3088 } while (0)
3089 
3090 #define SET_UID_METADATA(pb, name, var, str) do { \
3091  avio_read(pb, var, 16); \
3092  if ((ret = mxf_uid_to_str(var, &str)) < 0) \
3093  return ret; \
3094  av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
3095 } while (0)
3096 
3097 #define SET_TS_METADATA(pb, name, var, str) do { \
3098  var = avio_rb64(pb); \
3099  if (var && (ret = avpriv_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var))) < 0) \
3100  return ret; \
3101 } while (0)
3102 
3103 static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset)
3104 {
3105  MXFContext *mxf = arg;
3106  AVFormatContext *s = mxf->fc;
3107  int ret;
3108  UID uid = { 0 };
3109  char *str = NULL;
3110  uint64_t ts;
3111  uint16_t major, minor, tertiary, patch, release;
3112  switch (tag) {
3113  case 0x3C01:
3114  SET_STR_METADATA(pb, "company_name", str);
3115  break;
3116  case 0x3C02:
3117  SET_STR_METADATA(pb, "product_name", str);
3118  break;
3119  case 0x3C03:
3120  SET_VERSION_METADATA(pb, "product_version_num", major, minor, tertiary, patch, release, str);
3121  break;
3122  case 0x3C04:
3123  SET_STR_METADATA(pb, "product_version", str);
3124  break;
3125  case 0x3C05:
3126  SET_UID_METADATA(pb, "product_uid", uid, str);
3127  break;
3128  case 0x3C06:
3129  SET_TS_METADATA(pb, "modification_date", ts, str);
3130  break;
3131  case 0x3C07:
3132  SET_VERSION_METADATA(pb, "toolkit_version_num", major, minor, tertiary, patch, release, str);
3133  break;
3134  case 0x3C08:
3135  SET_STR_METADATA(pb, "application_platform", str);
3136  break;
3137  case 0x3C09:
3138  SET_UID_METADATA(pb, "generation_uid", uid, str);
3139  break;
3140  case 0x3C0A:
3141  SET_UID_METADATA(pb, "uid", uid, str);
3142  break;
3143  }
3144  return 0;
3145 }
3146 
3147 static int mxf_read_preface_metadata(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
3148 {
3149  MXFContext *mxf = arg;
3150  AVFormatContext *s = mxf->fc;
3151  int ret;
3152  char *str = NULL;
3153 
3154  if (tag >= 0x8000 && (IS_KLV_KEY(uid, mxf_avid_project_name))) {
3155  SET_STR_METADATA(pb, "project_name", str);
3156  }
3157  return 0;
3158 }
3159 
3161  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
3162  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
3163  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack },
3164  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack },
3165  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack },
3166  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack },
3167  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack },
3168  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack },
3169  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
3170  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
3171  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
3172  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2f,0x00 }, mxf_read_preface_metadata },
3173  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x30,0x00 }, mxf_read_identification_metadata },
3174  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
3175  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_package, sizeof(MXFPackage), SourcePackage },
3176  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_package, sizeof(MXFPackage), MaterialPackage },
3177  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0f,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
3178  { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x05,0x00 }, mxf_read_essence_group, sizeof(MXFEssenceGroup), EssenceGroup},
3179  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
3180  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3f,0x00 }, mxf_read_tagged_value, sizeof(MXFTaggedValue), TaggedValue },
3181  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
3182  { { 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 */
3183  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
3184  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
3185  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
3186  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
3187  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2VideoDescriptor */
3188  { { 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 */
3189  { { 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 */
3190  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5e,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2AudioDescriptor */
3191  { { 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 */
3192  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6b,0x00 }, mxf_read_mca_sub_descriptor, sizeof(MXFMCASubDescriptor), AudioChannelLabelSubDescriptor },
3193  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6c,0x00 }, mxf_read_mca_sub_descriptor, sizeof(MXFMCASubDescriptor), SoundfieldGroupLabelSubDescriptor },
3194  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6d,0x00 }, mxf_read_mca_sub_descriptor, sizeof(MXFMCASubDescriptor), GroupOfSoundfieldGroupsLabelSubDescriptor },
3195  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
3196  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
3197  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x14,0x00 }, mxf_read_timecode_component, sizeof(MXFTimecodeComponent), TimecodeComponent },
3198  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0c,0x00 }, mxf_read_pulldown_component, sizeof(MXFPulldownComponent), PulldownComponent },
3199  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
3200  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
3201  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x23,0x00 }, mxf_read_essence_container_data, sizeof(MXFEssenceContainerData), EssenceContainerData },
3202  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
3203 };
3204 
3206 {
3207  ctx->type = type;
3208  ctx->partition_score = partition_score(partition);
3209  switch (type){
3210  case MultipleDescriptor:
3211  case Descriptor:
3212  ((MXFDescriptor*)ctx)->pix_fmt = AV_PIX_FMT_NONE;
3213  ((MXFDescriptor*)ctx)->duration = AV_NOPTS_VALUE;
3214  break;
3215  default:
3216  break;
3217  }
3218  return 0;
3219 }
3220 
3221 static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
3222 {
3223  AVIOContext *pb = mxf->fc->pb;
3224  uint64_t klv_end = avio_tell(pb) + klv->length;
3225  MXFMetadataSet *meta;
3226  void *ctx;
3227 
3228  if (ctx_size) {
3229  meta = av_mallocz(ctx_size);
3230  if (!meta)
3231  return AVERROR(ENOMEM);
3232  ctx = meta;
3234  } else {
3235  meta = NULL;
3236  ctx = mxf;
3237  }
3238  while (avio_tell(pb) + 4ULL < klv_end && !avio_feof(pb)) {
3239  int ret;
3240  int tag = avio_rb16(pb);
3241  int size = avio_rb16(pb); /* KLV specified by 0x53 */
3242  int64_t next = avio_tell(pb);
3243  UID uid = {0};
3244  if (next < 0 || next > INT64_MAX - size) {
3245  if (meta) {
3246  mxf_free_metadataset(&meta, 1);
3247  }
3248  return next < 0 ? next : AVERROR_INVALIDDATA;
3249  }
3250  next += size;
3251 
3252  av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x size %d\n", tag, size);
3253  if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
3254  av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
3255  continue;
3256  }
3257  if (tag > 0x7FFF) { /* dynamic tag */
3258  int i;
3259  for (i = 0; i < mxf->local_tags_count; i++) {
3260  int local_tag = AV_RB16(mxf->local_tags+i*18);
3261  if (local_tag == tag) {
3262  memcpy(uid, mxf->local_tags+i*18+2, 16);
3263  av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x\n", local_tag);
3264  PRINT_KEY(mxf->fc, "uid", uid);
3265  }
3266  }
3267  }
3268  if (meta && tag == 0x3C0A) {
3269  avio_read(pb, meta->uid, 16);
3270  } else if ((ret = read_child(ctx, pb, tag, size, uid, -1)) < 0) {
3271  if (meta) {
3272  mxf_free_metadataset(&meta, 1);
3273  }
3274  return ret;
3275  }
3276 
3277  /* Accept the 64k local set limit being exceeded (Avid). Don't accept
3278  * it extending past the end of the KLV though (zzuf5.mxf). */
3279  if (avio_tell(pb) > klv_end) {
3280  if (meta) {
3281  mxf_free_metadataset(&meta, 1);
3282  }
3283 
3284  av_log(mxf->fc, AV_LOG_ERROR,
3285  "local tag %#04x extends past end of local set @ %#"PRIx64"\n",
3286  tag, klv->offset);
3287  return AVERROR_INVALIDDATA;
3288  } else if (avio_tell(pb) <= next) /* only seek forward, else this can loop for a long time */
3289  avio_seek(pb, next, SEEK_SET);
3290  }
3291  return meta ? mxf_add_metadata_set(mxf, &meta) : 0;
3292 }
3293 
3294 /**
3295  * Matches any partition pack key, in other words:
3296  * - HeaderPartition
3297  * - BodyPartition
3298  * - FooterPartition
3299  * @return non-zero if the key is a partition pack key, zero otherwise
3300  */
3302 {
3303  //NOTE: this is a little lax since it doesn't constraint key[14]
3304  return !memcmp(key, mxf_header_partition_pack_key, 13) &&
3305  key[13] >= 2 && key[13] <= 4;
3306 }
3307 
3308 /**
3309  * Parses a metadata KLV
3310  * @return <0 on error, 0 otherwise
3311  */
3313  int ctx_size, enum MXFMetadataSetType type)
3314 {
3315  AVFormatContext *s = mxf->fc;
3316  int res;
3317  if (klv.key[5] == 0x53) {
3318  res = mxf_read_local_tags(mxf, &klv, read, ctx_size, type);
3319  } else {
3320  uint64_t next = avio_tell(s->pb) + klv.length;
3321  res = read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);
3322 
3323  /* only seek forward, else this can loop for a long time */
3324  if (avio_tell(s->pb) > next) {
3325  av_log(s, AV_LOG_ERROR, "read past end of KLV @ %#"PRIx64"\n",
3326  klv.offset);
3327  return AVERROR_INVALIDDATA;
3328  }
3329 
3330  avio_seek(s->pb, next, SEEK_SET);
3331  }
3332  if (res < 0) {
3333  av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
3334  return res;
3335  }
3336  return 0;
3337 }
3338 
3339 /**
3340  * Seeks to the previous partition and parses it, if possible
3341  * @return <= 0 if we should stop parsing, > 0 if we should keep going
3342  */
3344 {
3345  AVIOContext *pb = mxf->fc->pb;
3346  KLVPacket klv;
3347  int64_t current_partition_ofs;
3348  int ret;
3349 
3350  if (!mxf->current_partition ||
3352  return 0; /* we've parsed all partitions */
3353 
3354  /* seek to previous partition */
3355  current_partition_ofs = mxf->current_partition->pack_ofs; //includes run-in
3356  avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET);
3357  mxf->current_partition = NULL;
3358 
3359  av_log(mxf->fc, AV_LOG_TRACE, "seeking to previous partition\n");
3360 
3361  /* Make sure this is actually a PartitionPack, and if so parse it.
3362  * See deadlock2.mxf
3363  */
3364  if ((ret = klv_read_packet(&klv, pb)) < 0) {
3365  av_log(mxf->fc, AV_LOG_ERROR, "failed to read PartitionPack KLV\n");
3366  return ret;
3367  }
3368 
3369  if (!mxf_is_partition_pack_key(klv.key)) {
3370  av_log(mxf->fc, AV_LOG_ERROR, "PreviousPartition @ %" PRIx64 " isn't a PartitionPack\n", klv.offset);
3371  return AVERROR_INVALIDDATA;
3372  }
3373 
3374  /* We can't just check ofs >= current_partition_ofs because PreviousPartition
3375  * can point to just before the current partition, causing klv_read_packet()
3376  * to sync back up to it. See deadlock3.mxf
3377  */
3378  if (klv.offset >= current_partition_ofs) {
3379  av_log(mxf->fc, AV_LOG_ERROR, "PreviousPartition for PartitionPack @ %"
3380  PRIx64 " indirectly points to itself\n", current_partition_ofs);
3381  return AVERROR_INVALIDDATA;
3382  }
3383 
3384  if ((ret = mxf_parse_klv(mxf, klv, mxf_read_partition_pack, 0, 0)) < 0)
3385  return ret;
3386 
3387  return 1;
3388 }
3389 
3390 /**
3391  * Called when essence is encountered
3392  * @return <= 0 if we should stop parsing, > 0 if we should keep going
3393  */
3395 {
3396  AVIOContext *pb = mxf->fc->pb;
3397  int64_t ret;
3398 
3399  if (mxf->parsing_backward) {
3400  return mxf_seek_to_previous_partition(mxf);
3401  } else {
3402  if (!mxf->footer_partition) {
3403  av_log(mxf->fc, AV_LOG_TRACE, "no FooterPartition\n");
3404  return 0;
3405  }
3406 
3407  av_log(mxf->fc, AV_LOG_TRACE, "seeking to FooterPartition\n");
3408 
3409  /* remember where we were so we don't end up seeking further back than this */
3410  mxf->last_forward_tell = avio_tell(pb);
3411 
3412  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
3413  av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing FooterPartition\n");
3414  return -1;
3415  }
3416 
3417  /* seek to FooterPartition and parse backward */
3418  if ((ret = avio_seek(pb, mxf->run_in + mxf->footer_partition, SEEK_SET)) < 0) {
3419  av_log(mxf->fc, AV_LOG_ERROR,
3420  "failed to seek to FooterPartition @ 0x%" PRIx64
3421  " (%"PRId64") - partial file?\n",
3422  mxf->run_in + mxf->footer_partition, ret);
3423  return ret;
3424  }
3425 
3426  mxf->current_partition = NULL;
3427  mxf->parsing_backward = 1;
3428  }
3429 
3430  return 1;
3431 }
3432 
3433 /**
3434  * Called when the next partition or EOF is encountered
3435  * @return <= 0 if we should stop parsing, > 0 if we should keep going
3436  */
3438 {
3439  return mxf->parsing_backward ? mxf_seek_to_previous_partition(mxf) : 1;
3440 }
3441 
3443 {
3444  for (int i = 0; i < s->nb_streams; i++) {
3445  MXFTrack *track = s->streams[i]->priv_data;
3446  if (track && track->body_sid == body_sid && track->wrapping != UnknownWrapped)
3447  return track->wrapping;
3448  }
3449  return UnknownWrapped;
3450 }
3451 
3452 /**
3453  * Figures out the proper offset and length of the essence container in each partition
3454  */
3456 {
3457  MXFContext *mxf = s->priv_data;
3458  int x;
3459 
3460  for (x = 0; x < mxf->partitions_count; x++) {
3461  MXFPartition *p = &mxf->partitions[x];
3462  MXFWrappingScheme wrapping;
3463 
3464  if (!p->body_sid)
3465  continue; /* BodySID == 0 -> no essence */
3466 
3467  /* for clip wrapped essences we point essence_offset after the KL (usually klv.offset + 20 or 25)
3468  * otherwise we point essence_offset at the key of the first essence KLV.
3469  */
3470 
3471  wrapping = (mxf->op == OPAtom) ? ClipWrapped : mxf_get_wrapping_by_body_sid(s, p->body_sid);
3472 
3473  if (wrapping == ClipWrapped) {
3476  } else {
3478 
3479  /* essence container spans to the next partition */
3480  if (x < mxf->partitions_count - 1)
3482 
3483  if (p->essence_length < 0) {
3484  /* next ThisPartition < essence_offset */
3485  p->essence_length = 0;
3486  av_log(mxf->fc, AV_LOG_ERROR,
3487  "partition %i: bad ThisPartition = %"PRIX64"\n",
3488  x+1, mxf->partitions[x+1].this_partition);
3489  }
3490  }
3491  }
3492 }
3493 
3494 static MXFIndexTable *mxf_find_index_table(MXFContext *mxf, int index_sid)
3495 {
3496  int i;
3497  for (i = 0; i < mxf->nb_index_tables; i++)
3498  if (mxf->index_tables[i].index_sid == index_sid)
3499  return &mxf->index_tables[i];
3500  return NULL;
3501 }
3502 
3503 /**
3504  * Deal with the case where for some audio atoms EditUnitByteCount is
3505  * very small (2, 4..). In those cases we should read more than one
3506  * sample per call to mxf_read_packet().
3507  */
3509 {
3510  MXFTrack *track = st->priv_data;
3511  MXFIndexTable *t;
3512 
3513  if (!track)
3514  return;
3515  track->edit_units_per_packet = 1;
3516  if (track->wrapping != ClipWrapped)
3517  return;
3518 
3519  t = mxf_find_index_table(mxf, track->index_sid);
3520 
3521  /* expect PCM with exactly one index table segment and a small (< 32) EUBC */
3522  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
3523  !is_pcm(st->codecpar->codec_id) ||
3524  !t ||
3525  t->nb_segments != 1 ||
3526  t->segments[0]->edit_unit_byte_count >= 32)
3527  return;
3528 
3529  /* arbitrarily default to 48 kHz PAL audio frame size */
3530  /* TODO: We could compute this from the ratio between the audio
3531  * and video edit rates for 48 kHz NTSC we could use the
3532  * 1802-1802-1802-1802-1801 pattern. */
3533  track->edit_units_per_packet = FFMAX(1, track->edit_rate.num / track->edit_rate.den / 25);
3534 }
3535 
3536 /**
3537  * Deal with the case where ClipWrapped essences does not have any IndexTableSegments.
3538  */
3540 {
3541  MXFTrack *track = st->priv_data;
3543  MXFPartition *p = NULL;
3544  int essence_partition_count = 0;
3545  int edit_unit_byte_count = 0;
3546  int i, ret;
3547 
3548  if (!track || track->wrapping != ClipWrapped)
3549  return 0;
3550 
3551  /* check if track already has an IndexTableSegment */
3552  for (i = 0; i < mxf->metadata_sets_count; i++) {
3553  if (mxf->metadata_sets[i]->type == IndexTableSegment) {
3555  if (s->body_sid == track->body_sid)
3556  return 0;
3557  }
3558  }
3559 
3560  /* find the essence partition */
3561  for (i = 0; i < mxf->partitions_count; i++) {
3562  /* BodySID == 0 -> no essence */
3563  if (mxf->partitions[i].body_sid != track->body_sid)
3564  continue;
3565 
3566  p = &mxf->partitions[i];
3567  essence_partition_count++;
3568  }
3569 
3570  /* only handle files with a single essence partition */
3571  if (essence_partition_count != 1)
3572  return 0;
3573 
3575  edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) *
3576  st->codecpar->ch_layout.nb_channels) >> 3;
3577  } else if (st->duration > 0 && p->first_essence_klv.length > 0 && p->first_essence_klv.length % st->duration == 0) {
3578  edit_unit_byte_count = p->first_essence_klv.length / st->duration;
3579  }
3580 
3581  if (edit_unit_byte_count <= 0)
3582  return 0;
3583 
3584  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);
3585 
3586  if (!(segment = av_mallocz(sizeof(*segment))))
3587  return AVERROR(ENOMEM);
3588 
3589  if ((ret = mxf_add_metadata_set(mxf, (MXFMetadataSet**)&segment)))
3590  return ret;
3591 
3592  /* Make sure we have nonzero unique index_sid, body_sid will be ok, because
3593  * using the same SID for index is forbidden in MXF. */
3594  if (!track->index_sid)
3595  track->index_sid = track->body_sid;
3596 
3597  segment->meta.type = IndexTableSegment;
3598  /* stream will be treated as small EditUnitByteCount */
3599  segment->edit_unit_byte_count = edit_unit_byte_count;
3600  segment->index_start_position = 0;
3601  segment->index_duration = st->duration;
3602  segment->index_edit_rate = av_inv_q(st->time_base);
3603  segment->index_sid = track->index_sid;
3604  segment->body_sid = p->body_sid;
3605  return 0;
3606 }
3607 
3609 {
3610  MXFContext *mxf = s->priv_data;
3611  uint32_t length;
3612  int64_t file_size, max_rip_length, min_rip_length;
3613  KLVPacket klv;
3614 
3615  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
3616  return;
3617 
3618  file_size = avio_size(s->pb);
3619 
3620  /* S377m says to check the RIP length for "silly" values, without defining "silly".
3621  * The limit below assumes a file with nothing but partition packs and a RIP.
3622  * Before changing this, consider that a muxer may place each sample in its own partition.
3623  *
3624  * 105 is the size of the smallest possible PartitionPack
3625  * 12 is the size of each RIP entry
3626  * 28 is the size of the RIP header and footer, assuming an 8-byte BER
3627  */
3628  max_rip_length = ((file_size - mxf->run_in) / 105) * 12 + 28;
3629  max_rip_length = FFMIN(max_rip_length, INT_MAX); //2 GiB and up is also silly
3630 
3631  /* We're only interested in RIPs with at least two entries.. */
3632  min_rip_length = 16+1+24+4;
3633 
3634  /* See S377m section 11 */
3635  avio_seek(s->pb, file_size - 4, SEEK_SET);
3636  length = avio_rb32(s->pb);
3637 
3638  if (length < min_rip_length || length > max_rip_length)
3639  goto end;
3640  avio_seek(s->pb, file_size - length, SEEK_SET);
3641  if (klv_read_packet(&klv, s->pb) < 0 ||
3643  goto end;
3644  if (klv.next_klv != file_size || klv.length <= 4 || (klv.length - 4) % 12) {
3645  av_log(s, AV_LOG_WARNING, "Invalid RIP KLV length\n");
3646  goto end;
3647  }
3648 
3649  avio_skip(s->pb, klv.length - 12);
3650  mxf->footer_partition = avio_rb64(s->pb);
3651 
3652  /* sanity check */
3653  if (mxf->run_in + mxf->footer_partition >= file_size) {
3654  av_log(s, AV_LOG_WARNING, "bad FooterPartition in RIP - ignoring\n");
3655  mxf->footer_partition = 0;
3656  }
3657 
3658 end:
3659  avio_seek(s->pb, mxf->run_in, SEEK_SET);
3660 }
3661 
3663 {
3664  MXFContext *mxf = s->priv_data;
3665  KLVPacket klv;
3666  int64_t essence_offset = 0;
3667  int ret;
3668 
3669  mxf->last_forward_tell = INT64_MAX;
3670 
3672  av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
3673  return AVERROR_INVALIDDATA;
3674  }
3675  avio_seek(s->pb, -14, SEEK_CUR);
3676  mxf->fc = s;
3677  mxf->run_in = avio_tell(s->pb);
3678 
3680 
3681  while (!avio_feof(s->pb)) {
3682  const MXFMetadataReadTableEntry *metadata;
3683 
3684  if (klv_read_packet(&klv, s->pb) < 0) {
3685  /* EOF - seek to previous partition or stop */
3686  if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
3687  break;
3688  else
3689  continue;
3690  }
3691 
3692  PRINT_KEY(s, "read header", klv.key);
3693  av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
3700 
3701  if (!mxf->current_partition) {
3702  av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to first PartitionPack\n");
3703  return AVERROR_INVALIDDATA;
3704  }
3705 
3708 
3709  if (!essence_offset)
3710  essence_offset = klv.offset;
3711 
3712  /* seek to footer, previous partition or stop */
3713  if (mxf_parse_handle_essence(mxf) <= 0)
3714  break;
3715  continue;
3716  } else if (mxf_is_partition_pack_key(klv.key) && mxf->current_partition) {
3717  /* next partition pack - keep going, seek to previous partition or stop */
3718  if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
3719  break;
3720  else if (mxf->parsing_backward)
3721  continue;
3722  /* we're still parsing forward. proceed to parsing this partition pack */
3723  }
3724 
3725  for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
3726  if (IS_KLV_KEY(klv.key, metadata->key)) {
3727  if ((ret = mxf_parse_klv(mxf, klv, metadata->read, metadata->ctx_size, metadata->type)) < 0)
3728  return ret;
3729  break;
3730  }
3731  }
3732  if (!metadata->read) {
3733  av_log(s, AV_LOG_VERBOSE, "Dark key " PRIxUID "\n",
3734  UID_ARG(klv.key));
3735  avio_skip(s->pb, klv.length);
3736  }
3737  }
3738  /* FIXME avoid seek */
3739  if (!essence_offset) {
3740  av_log(s, AV_LOG_ERROR, "no essence\n");
3741  return AVERROR_INVALIDDATA;
3742  }
3743  avio_seek(s->pb, essence_offset, SEEK_SET);
3744 
3745  /* we need to do this before computing the index tables
3746  * to be able to fill in zero IndexDurations with st->duration */
3747  if ((ret = mxf_parse_structural_metadata(mxf)) < 0)
3748  return ret;
3749 
3750  for (int i = 0; i < s->nb_streams; i++)
3751  mxf_handle_missing_index_segment(mxf, s->streams[i]);
3752 
3753  if ((ret = mxf_compute_index_tables(mxf)) < 0)
3754  return ret;
3755 
3756  if (mxf->nb_index_tables > 1) {
3757  /* TODO: look up which IndexSID to use via EssenceContainerData */
3758  av_log(mxf->fc, AV_LOG_INFO, "got %i index tables - only the first one (IndexSID %i) will be used\n",
3759  mxf->nb_index_tables, mxf->index_tables[0].index_sid);
3760  } else if (mxf->nb_index_tables == 0 && mxf->op == OPAtom && (s->error_recognition & AV_EF_EXPLODE)) {
3761  av_log(mxf->fc, AV_LOG_ERROR, "cannot demux OPAtom without an index\n");
3762  return AVERROR_INVALIDDATA;
3763  }
3764 
3766 
3767  for (int i = 0; i < s->nb_streams; i++)
3768  mxf_compute_edit_units_per_packet(mxf, s->streams[i]);
3769 
3770  return 0;
3771 }
3772 
3773 /* Get the edit unit of the next packet from current_offset in a track. The returned edit unit can be original_duration as well! */
3774 static int mxf_get_next_track_edit_unit(MXFContext *mxf, MXFTrack *track, int64_t current_offset, int64_t *edit_unit_out)
3775 {
3776  int64_t a, b, m, offset;
3777  MXFIndexTable *t = mxf_find_index_table(mxf, track->index_sid);
3778 
3779  if (!t || track->original_duration <= 0)
3780  return -1;
3781 
3782  a = -1;
3783  b = track->original_duration;
3784 
3785  while (b - a > 1) {
3786  m = (a + b) >> 1;
3787  if (mxf_edit_unit_absolute_offset(mxf, t, m, track->edit_rate, NULL, &offset, NULL, 0) < 0)
3788  return -1;
3789  if (offset < current_offset)
3790  a = m;
3791  else
3792  b = m;
3793  }
3794 
3795  *edit_unit_out = b;
3796 
3797  return 0;
3798 }
3799 
3801  int64_t edit_unit)
3802 {
3803  MXFTrack *track = st->priv_data;
3804  AVRational time_base = av_inv_q(track->edit_rate);
3806 
3807  // For non-audio sample_count equals current edit unit
3809  return edit_unit;
3810 
3811  if ((sample_rate.num / sample_rate.den) == 48000) {
3812  return av_rescale_q(edit_unit, sample_rate, track->edit_rate);
3813  } else {
3814  int remainder = (sample_rate.num * time_base.num) %
3815  (time_base.den * sample_rate.den);
3816  if (remainder)
3817  av_log(mxf->fc, AV_LOG_WARNING,
3818  "seeking detected on stream #%d with time base (%d/%d) and "
3819  "sample rate (%d/%d), audio pts won't be accurate.\n",
3820  st->index, time_base.num, time_base.den,
3821  sample_rate.num, sample_rate.den);
3822  return av_rescale_q(edit_unit, sample_rate, track->edit_rate);
3823  }
3824 }
3825 
3826 /**
3827  * Make sure track->sample_count is correct based on what offset we're currently at.
3828  * Also determine the next edit unit (or packet) offset.
3829  * @return next_ofs if OK, <0 on error
3830  */
3831 static int64_t mxf_set_current_edit_unit(MXFContext *mxf, AVStream *st, int64_t current_offset, int resync)
3832 {
3833  int64_t next_ofs = -1;
3834  MXFTrack *track = st->priv_data;
3835  int64_t edit_unit = av_rescale_q(track->sample_count, st->time_base, av_inv_q(track->edit_rate));
3836  int64_t new_edit_unit;
3837  MXFIndexTable *t = mxf_find_index_table(mxf, track->index_sid);
3838 
3839  if (!t || track->wrapping == UnknownWrapped)
3840  return -1;
3841 
3842  if (mxf_edit_unit_absolute_offset(mxf, t, edit_unit + track->edit_units_per_packet, track->edit_rate, NULL, &next_ofs, NULL, 0) < 0 &&
3843  (next_ofs = mxf_essence_container_end(mxf, t->body_sid)) <= 0) {
3844  av_log(mxf->fc, AV_LOG_ERROR, "unable to compute the size of the last packet\n");
3845  return -1;
3846  }
3847 
3848  /* check if the next edit unit offset (next_ofs) starts ahead of current_offset */
3849  if (next_ofs > current_offset)
3850  return next_ofs;
3851 
3852  if (!resync) {
3853  av_log(mxf->fc, AV_LOG_ERROR, "cannot find current edit unit for stream %d, invalid index?\n", st->index);
3854  return -1;
3855  }
3856 
3857  if (mxf_get_next_track_edit_unit(mxf, track, current_offset + 1, &new_edit_unit) < 0 || new_edit_unit <= 0) {
3858  av_log(mxf->fc, AV_LOG_ERROR, "failed to find next track edit unit in stream %d\n", st->index);
3859  return -1;
3860  }
3861 
3862  new_edit_unit--;
3863  track->sample_count = mxf_compute_sample_count(mxf, st, new_edit_unit);
3864  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);
3865 
3866  return mxf_set_current_edit_unit(mxf, st, current_offset, 0);
3867 }
3868 
3870  AVPacket *pkt)
3871 {
3872  AVStream *st = mxf->fc->streams[pkt->stream_index];
3873  MXFTrack *track = st->priv_data;
3874  int64_t bits_per_sample = par->bits_per_coded_sample;
3875 
3876  if (!bits_per_sample)
3877  bits_per_sample = av_get_bits_per_sample(par->codec_id);
3878 
3879  pkt->pts = track->sample_count;
3880 
3881  if (par->ch_layout.nb_channels <= 0 ||
3882  bits_per_sample <= 0 ||
3883  par->ch_layout.nb_channels * (int64_t)bits_per_sample < 8)
3884  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);
3885  else
3886  track->sample_count += pkt->size / (par->ch_layout.nb_channels * (int64_t)bits_per_sample / 8);
3887 
3888  return 0;
3889 }
3890 
3891 static int mxf_set_pts(MXFContext *mxf, AVStream *st, AVPacket *pkt)
3892 {
3893  AVCodecParameters *par = st->codecpar;
3894  MXFTrack *track = st->priv_data;
3895 
3896  if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
3897  /* see if we have an index table to derive timestamps from */
3898  MXFIndexTable *t = mxf_find_index_table(mxf, track->index_sid);
3899 
3900  if (t && track->sample_count < t->nb_ptses) {
3901  pkt->dts = track->sample_count + t->first_dts;
3902  pkt->pts = t->ptses[track->sample_count];
3903  } else if (track->intra_only) {
3904  /* intra-only -> PTS = EditUnit.
3905  * let utils.c figure out DTS since it can be < PTS if low_delay = 0 (Sony IMX30) */
3906  pkt->pts = track->sample_count;
3907  }
3908  track->sample_count++;
3909  } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
3910  int ret = mxf_set_audio_pts(mxf, par, pkt);
3911  if (ret < 0)
3912  return ret;
3913  } else if (track) {
3914  pkt->dts = pkt->pts = track->sample_count;
3915  pkt->duration = 1;
3916  track->sample_count++;
3917  }
3918  return 0;
3919 }
3920 
3922 {
3923  KLVPacket klv;
3924  MXFContext *mxf = s->priv_data;
3925  int ret;
3926 
3927  while (1) {
3928  int64_t max_data_size;
3929  int64_t pos = avio_tell(s->pb);
3930 
3931  if (pos < mxf->current_klv_data.next_klv - mxf->current_klv_data.length || pos >= mxf->current_klv_data.next_klv) {
3932  mxf->current_klv_data = (KLVPacket){{0}};
3933  ret = klv_read_packet(&klv, s->pb);
3934  if (ret < 0)
3935  break;
3936  max_data_size = klv.length;
3937  pos = klv.next_klv - klv.length;
3938  PRINT_KEY(s, "read packet", klv.key);
3939  av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
3941  ret = mxf_decrypt_triplet(s, pkt, &klv);
3942  if (ret < 0) {
3943  av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
3944  return ret;
3945  }
3946  return 0;
3947  }
3948  } else {
3949  klv = mxf->current_klv_data;
3950  max_data_size = klv.next_klv - pos;
3951  }
3955  int body_sid = find_body_sid_by_absolute_offset(mxf, klv.offset);
3956  int index = mxf_get_stream_index(s, &klv, body_sid);
3957  int64_t next_ofs;
3958  AVStream *st;
3959  MXFTrack *track;
3960 
3961  if (index < 0) {
3963  "error getting stream index %"PRIu32"\n",
3964  AV_RB32(klv.key + 12));
3965  goto skip;
3966  }
3967 
3968  st = s->streams[index];
3969  track = st->priv_data;
3970 
3971  if (s->streams[index]->discard == AVDISCARD_ALL)
3972  goto skip;
3973 
3974  next_ofs = mxf_set_current_edit_unit(mxf, st, pos, 1);
3975 
3976  if (track->wrapping != FrameWrapped) {
3977  int64_t size;
3978 
3979  if (next_ofs <= 0) {
3980  // If we have no way to packetize the data, then return it in chunks...
3981  if (klv.next_klv - klv.length == pos && max_data_size > MXF_MAX_CHUNK_SIZE) {
3983  avpriv_request_sample(s, "Huge KLV without proper index in non-frame wrapped essence");
3984  }
3985  size = FFMIN(max_data_size, MXF_MAX_CHUNK_SIZE);
3986  } else {
3987  if ((size = next_ofs - pos) <= 0) {
3988  av_log(s, AV_LOG_ERROR, "bad size: %"PRId64"\n", size);
3989  mxf->current_klv_data = (KLVPacket){{0}};
3990  return AVERROR_INVALIDDATA;
3991  }
3992  // We must not overread, because the next edit unit might be in another KLV
3993  if (size > max_data_size)
3994  size = max_data_size;
3995  }
3996 
3997  mxf->current_klv_data = klv;
3998  klv.offset = pos;
3999  klv.length = size;
4000  klv.next_klv = klv.offset + klv.length;
4001  }
4002 
4003  /* check for 8 channels AES3 element */
4004  if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
4005  ret = mxf_get_d10_aes3_packet(s->pb, s->streams[index],
4006  pkt, klv.length);
4007  if (ret < 0) {
4008  av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
4009  mxf->current_klv_data = (KLVPacket){{0}};
4010  return ret;
4011  }
4012  } else if (mxf->eia608_extract &&
4013  s->streams[index]->codecpar->codec_id == AV_CODEC_ID_EIA_608) {
4014  ret = mxf_get_eia608_packet(s, s->streams[index], pkt, klv.length);
4015  if (ret < 0) {
4016  mxf->current_klv_data = (KLVPacket){{0}};
4017  return ret;
4018  }
4019  } else {
4020  ret = av_get_packet(s->pb, pkt, klv.length);
4021  if (ret < 0) {
4022  mxf->current_klv_data = (KLVPacket){{0}};
4023  return ret;
4024  }
4025  }
4026  pkt->stream_index = index;
4027  pkt->pos = klv.offset;
4028 
4029  ret = mxf_set_pts(mxf, st, pkt);
4030  if (ret < 0) {
4031  mxf->current_klv_data = (KLVPacket){{0}};
4032  return ret;
4033  }
4034 
4035  /* seek for truncated packets */
4036  avio_seek(s->pb, klv.next_klv, SEEK_SET);
4037 
4038  return 0;
4039  } else {
4040  skip:
4041  avio_skip(s->pb, max_data_size);
4042  mxf->current_klv_data = (KLVPacket){{0}};
4043  }
4044  }
4045  return avio_feof(s->pb) ? AVERROR_EOF : ret;
4046 }
4047 
4049 {
4050  MXFContext *mxf = s->priv_data;
4051  int i;
4052 
4053  av_freep(&mxf->packages_refs);
4055 
4056  for (i = 0; i < s->nb_streams; i++)
4057  s->streams[i]->priv_data = NULL;
4058 
4059  for (i = 0; i < mxf->metadata_sets_count; i++) {
4061  }
4062  mxf->metadata_sets_count = 0;
4063  av_freep(&mxf->partitions);
4064  av_freep(&mxf->metadata_sets);
4065  av_freep(&mxf->aesc);
4066  av_freep(&mxf->local_tags);
4067 
4068  if (mxf->index_tables) {
4069  for (i = 0; i < mxf->nb_index_tables; i++) {
4070  av_freep(&mxf->index_tables[i].segments);
4071  av_freep(&mxf->index_tables[i].ptses);
4073  av_freep(&mxf->index_tables[i].offsets);
4074  }
4075  }
4076  av_freep(&mxf->index_tables);
4077 
4078  return 0;
4079 }
4080 
4081 static int mxf_probe(const AVProbeData *p) {
4082  const uint8_t *bufp = p->buf;
4083  const uint8_t *end = p->buf + p->buf_size;
4084 
4085  if (p->buf_size < sizeof(mxf_header_partition_pack_key))
4086  return 0;
4087 
4088  /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
4089  end -= sizeof(mxf_header_partition_pack_key);
4090 
4091  for (; bufp < end;) {
4092  if (!((bufp[13] - 1) & 0xF2)){
4093  if (AV_RN32(bufp ) == AV_RN32(mxf_header_partition_pack_key ) &&
4094  AV_RN32(bufp+ 4) == AV_RN32(mxf_header_partition_pack_key+ 4) &&
4095  AV_RN32(bufp+ 8) == AV_RN32(mxf_header_partition_pack_key+ 8) &&
4097  return bufp == p->buf ? AVPROBE_SCORE_MAX : AVPROBE_SCORE_MAX - 1;
4098  bufp ++;
4099  } else
4100  bufp += 10;
4101  }
4102 
4103  return 0;
4104 }
4105 
4106 /* rudimentary byte seek */
4107 /* XXX: use MXF Index */
4108 static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
4109 {
4110  AVStream *st = s->streams[stream_index];
4111  int64_t seconds;
4112  MXFContext* mxf = s->priv_data;
4113  int64_t seekpos;
4114  int i, ret;
4115  MXFIndexTable *t;
4116  MXFTrack *source_track = st->priv_data;
4117 
4118  if (!source_track)
4119  return 0;
4120 
4121  /* if audio then truncate sample_time to EditRate */
4123  sample_time = av_rescale_q(sample_time, st->time_base,
4124  av_inv_q(source_track->edit_rate));
4125 
4126  if (mxf->nb_index_tables <= 0) {
4127  if (!s->bit_rate)
4128  return AVERROR_INVALIDDATA;
4129  if (sample_time < 0)
4130  sample_time = 0;
4131  seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
4132 
4133  seekpos = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
4134  if (seekpos < 0)
4135  return seekpos;
4136 
4137  avpriv_update_cur_dts(s, st, sample_time);
4138  mxf->current_klv_data = (KLVPacket){{0}};
4139  } else {
4140  MXFPartition *partition;
4141 
4142  t = &mxf->index_tables[0];
4143  if (t->index_sid != source_track->index_sid) {
4144  /* If the first index table does not belong to the stream, then find a stream which does belong to the index table */
4145  for (i = 0; i < s->nb_streams; i++) {
4146  MXFTrack *new_source_track = s->streams[i]->priv_data;
4147  if (new_source_track && new_source_track->index_sid == t->index_sid) {
4148  sample_time = av_rescale_q(sample_time, new_source_track->edit_rate, source_track->edit_rate);
4149  source_track = new_source_track;
4150  st = s->streams[i];
4151  break;
4152  }
4153  }
4154  if (i == s->nb_streams)
4155  return AVERROR_INVALIDDATA;
4156  }
4157 
4158  /* clamp above zero, else ff_index_search_timestamp() returns negative
4159  * this also means we allow seeking before the start */
4160  sample_time = FFMAX(sample_time, 0);
4161 
4162  if (t->fake_index) {
4163  /* The first frames may not be keyframes in presentation order, so
4164  * we have to advance the target to be able to find the first
4165  * keyframe backwards... */
4166  if (!(flags & AVSEEK_FLAG_ANY) &&
4168  t->ptses[0] != AV_NOPTS_VALUE &&
4169  sample_time < t->ptses[0] &&
4170  (t->fake_index[t->ptses[0]].flags & AVINDEX_KEYFRAME))
4171  sample_time = t->ptses[0];
4172 
4173  /* behave as if we have a proper index */
4174  if ((sample_time = ff_index_search_timestamp(t->fake_index, t->nb_ptses, sample_time, flags)) < 0)
4175  return sample_time;
4176  /* get the stored order index from the display order index */
4177  sample_time += t->offsets[sample_time];
4178  } else {
4179  /* no IndexEntryArray (one or more CBR segments)
4180  * make sure we don't seek past the end */
4181  sample_time = FFMIN(sample_time, source_track->original_duration - 1);
4182  }
4183 
4184  if (source_track->wrapping == UnknownWrapped)
4185  av_log(mxf->fc, AV_LOG_WARNING, "attempted seek in an UnknownWrapped essence\n");
4186 
4187  if ((ret = mxf_edit_unit_absolute_offset(mxf, t, sample_time, source_track->edit_rate, &sample_time, &seekpos, &partition, 1)) < 0)
4188  return ret;
4189 
4190  avpriv_update_cur_dts(s, st, sample_time);
4191  if (source_track->wrapping == ClipWrapped) {
4192  KLVPacket klv = partition->first_essence_klv;
4193  if (seekpos < klv.next_klv - klv.length || seekpos >= klv.next_klv) {
4194  av_log(mxf->fc, AV_LOG_ERROR, "attempted seek out of clip wrapped KLV\n");
4195  return AVERROR_INVALIDDATA;
4196  }
4197  mxf->current_klv_data = klv;
4198  } else {
4199  mxf->current_klv_data = (KLVPacket){{0}};
4200  }
4201  avio_seek(s->pb, seekpos, SEEK_SET);
4202  }
4203 
4204  // Update all tracks sample count
4205  for (i = 0; i < s->nb_streams; i++) {
4206  AVStream *cur_st = s->streams[i];
4207  MXFTrack *cur_track = cur_st->priv_data;
4208  if (cur_track) {
4209  int64_t track_edit_unit = sample_time;
4210  if (st != cur_st)
4211  mxf_get_next_track_edit_unit(mxf, cur_track, seekpos, &track_edit_unit);
4212  cur_track->sample_count = mxf_compute_sample_count(mxf, cur_st, track_edit_unit);
4213  }
4214  }
4215  return 0;
4216 }
4217 
4218 static const AVOption options[] = {
4219  { "eia608_extract", "extract eia 608 captions from s436m track",
4220  offsetof(MXFContext, eia608_extract), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
4222</