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