FFmpeg  3.4.9
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  *
32  * Principle
33  * Search for Track numbers which will identify essence element KLV packets.
34  * Search for SourcePackage which define tracks which contains Track numbers.
35  * Material Package contains tracks with reference to SourcePackage tracks.
36  * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
37  * Assign Descriptors to correct Tracks.
38  *
39  * Metadata reading functions read Local Tags, get InstanceUID(0x3C0A) then add MetaDataSet to MXFContext.
40  * Metadata parsing resolves Strong References to objects.
41  *
42  * Simple demuxer, only OP1A supported and some files might not work at all.
43  * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
44  */
45 
46 #include <inttypes.h>
47 
48 #include "libavutil/aes.h"
49 #include "libavutil/avassert.h"
50 #include "libavutil/mathematics.h"
51 #include "libavcodec/bytestream.h"
52 #include "libavutil/intreadwrite.h"
53 #include "libavutil/parseutils.h"
54 #include "libavutil/timecode.h"
55 #include "avformat.h"
56 #include "internal.h"
57 #include "mxf.h"
58 
59 typedef enum {
64 
65 typedef enum {
66  OP1a = 1,
76  OPSONYOpt, /* FATE sample, violates the spec in places */
77 } MXFOP;
78 
79 typedef struct MXFPartition {
80  int closed;
81  int complete;
84  int index_sid;
85  int body_sid;
86  int64_t this_partition;
87  int64_t essence_offset; ///< absolute offset of essence
88  int64_t essence_length;
93  int64_t pack_ofs; ///< absolute offset of pack in file, including run-in
94 } MXFPartition;
95 
96 typedef struct MXFCryptoContext {
101 
102 typedef struct MXFStructuralComponent {
108  int64_t duration;
109  int64_t start_position;
112 
113 typedef struct MXFSequence {
119  int64_t duration;
121 } MXFSequence;
122 
123 typedef struct MXFTrack {
128  struct AVRational rate;
131 
132 typedef struct {
137 
138 typedef struct {
143  int64_t duration;
145 
146 typedef struct {
149  char *name;
150  char *value;
152 
153 typedef struct {
156  MXFSequence *sequence; /* mandatory, and only one */
158  int track_id;
159  char *name;
160  uint8_t track_number[4];
163  uint64_t sample_count;
164  int64_t original_duration; /* st->duration in SampleRate/EditRate units */
165 } MXFTrack;
166 
167 typedef struct MXFDescriptor {
175  int width;
176  int height; /* Field height, not frame height */
177  int frame_layout; /* See MXFFrameLayout enum */
178  int video_line_map[2];
179 #define MXF_FIELD_DOMINANCE_DEFAULT 0
180 #define MXF_FIELD_DOMINANCE_FF 1 /* coded first, displayed first */
181 #define MXF_FIELD_DOMINANCE_FL 2 /* coded first, displayed last */
183  int channels;
185  int64_t duration; /* ContainerDuration optional property */
186  unsigned int component_depth;
187  unsigned int horiz_subsampling;
188  unsigned int vert_subsampling;
195 } MXFDescriptor;
196 
197 typedef struct MXFIndexTableSegment {
202  int body_sid;
205  uint64_t index_duration;
211 
212 typedef struct MXFPackage {
219  MXFDescriptor *descriptor; /* only one */
221  char *name;
224 } MXFPackage;
225 
226 typedef struct MXFMetadataSet {
230 
231 /* decoded index table */
232 typedef struct MXFIndexTable {
234  int body_sid;
235  int nb_ptses; /* number of PTSes or total duration of index */
236  int64_t first_dts; /* DTS = EditUnit + first_dts */
237  int64_t *ptses; /* maps EditUnit -> PTS */
239  MXFIndexTableSegment **segments; /* sorted by IndexStartPosition */
240  AVIndexEntry *fake_index; /* used for calling ff_index_search_timestamp() */
241  int8_t *offsets; /* temporal offsets for display order to stored order conversion */
242 } MXFIndexTable;
243 
244 typedef struct MXFContext {
253  struct AVAES *aesc;
259  int run_in;
267  int edit_units_per_packet; ///< how many edit units to read at a time (PCM, OPAtom)
268 } MXFContext;
269 
273 };
274 
275 /* NOTE: klv_offset is not set (-1) for local keys */
276 typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset);
277 
279  const UID key;
281  int ctx_size;
284 
285 static int mxf_read_close(AVFormatContext *s);
286 
287 /* partial keys to match */
288 static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
289 static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
290 static const uint8_t mxf_avid_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 };
291 static const uint8_t mxf_canopus_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x0a,0x0e,0x0f,0x03,0x01 };
292 static const uint8_t mxf_system_item_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x03,0x01,0x04 };
293 static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
294 /* complete keys to match */
295 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 };
296 static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
297 static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
298 static const uint8_t mxf_random_index_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
299 static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
300 static const uint8_t mxf_avid_project_name[] = { 0xa5,0xfb,0x7b,0x25,0xf6,0x15,0x94,0xb9,0x62,0xfc,0x37,0x17,0x49,0x2d,0x42,0xbf };
301 static const uint8_t mxf_jp2k_rsiz[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 };
302 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 };
303 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 };
304 
305 #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
306 
307 static void mxf_free_metadataset(MXFMetadataSet **ctx, int freectx)
308 {
310  switch ((*ctx)->type) {
311  case Descriptor:
312  case MultipleDescriptor:
313  av_freep(&((MXFDescriptor *)*ctx)->extradata);
314  av_freep(&((MXFDescriptor *)*ctx)->sub_descriptors_refs);
315  break;
316  case Sequence:
317  av_freep(&((MXFSequence *)*ctx)->structural_components_refs);
318  break;
319  case EssenceGroup:
320  av_freep(&((MXFEssenceGroup *)*ctx)->structural_components_refs);
321  break;
322  case SourcePackage:
323  case MaterialPackage:
324  av_freep(&((MXFPackage *)*ctx)->tracks_refs);
325  av_freep(&((MXFPackage *)*ctx)->name);
326  av_freep(&((MXFPackage *)*ctx)->comment_refs);
327  break;
328  case TaggedValue:
329  av_freep(&((MXFTaggedValue *)*ctx)->name);
330  av_freep(&((MXFTaggedValue *)*ctx)->value);
331  break;
332  case Track:
333  av_freep(&((MXFTrack *)*ctx)->name);
334  break;
335  case IndexTableSegment:
336  seg = (MXFIndexTableSegment *)*ctx;
338  av_freep(&seg->flag_entries);
340  default:
341  break;
342  }
343  if (freectx)
344  av_freep(ctx);
345 }
346 
348 {
349  uint64_t size = avio_r8(pb);
350  if (size & 0x80) { /* long form */
351  int bytes_num = size & 0x7f;
352  /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
353  if (bytes_num > 8)
354  return AVERROR_INVALIDDATA;
355  size = 0;
356  while (bytes_num--)
357  size = size << 8 | avio_r8(pb);
358  }
359  return size;
360 }
361 
362 static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
363 {
364  int i, b;
365  for (i = 0; i < size && !avio_feof(pb); i++) {
366  b = avio_r8(pb);
367  if (b == key[0])
368  i = 0;
369  else if (b != key[i])
370  i = -1;
371  }
372  return i == size;
373 }
374 
375 static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
376 {
377  if (!mxf_read_sync(pb, mxf_klv_key, 4))
378  return AVERROR_INVALIDDATA;
379  klv->offset = avio_tell(pb) - 4;
380  memcpy(klv->key, mxf_klv_key, 4);
381  avio_read(pb, klv->key + 4, 12);
382  klv->length = klv_decode_ber_length(pb);
383  return klv->length == -1 ? -1 : 0;
384 }
385 
387 {
388  int i;
389 
390  for (i = 0; i < s->nb_streams; i++) {
391  MXFTrack *track = s->streams[i]->priv_data;
392  /* SMPTE 379M 7.3 */
393  if (track && !memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
394  return i;
395  }
396  /* return 0 if only one stream, for OP Atom files with 0 as track number */
397  return s->nb_streams == 1 ? 0 : -1;
398 }
399 
400 /* XXX: use AVBitStreamFilter */
402 {
403  const uint8_t *buf_ptr, *end_ptr;
404  uint8_t *data_ptr;
405  int i;
406 
407  if (length > 61444) /* worst case PAL 1920 samples 8 channels */
408  return AVERROR_INVALIDDATA;
409  length = av_get_packet(pb, pkt, length);
410  if (length < 0)
411  return length;
412  data_ptr = pkt->data;
413  end_ptr = pkt->data + length;
414  buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
415 
416  if (st->codecpar->channels > 8)
417  return AVERROR_INVALIDDATA;
418 
419  for (; end_ptr - buf_ptr >= st->codecpar->channels * 4; ) {
420  for (i = 0; i < st->codecpar->channels; i++) {
421  uint32_t sample = bytestream_get_le32(&buf_ptr);
422  if (st->codecpar->bits_per_coded_sample == 24)
423  bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
424  else
425  bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
426  }
427  buf_ptr += 32 - st->codecpar->channels*4; // always 8 channels stored SMPTE 331M
428  }
429  av_shrink_packet(pkt, data_ptr - pkt->data);
430  return 0;
431 }
432 
434 {
435  static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
436  MXFContext *mxf = s->priv_data;
437  AVIOContext *pb = s->pb;
438  int64_t end = avio_tell(pb) + klv->length;
439  int64_t size;
440  uint64_t orig_size;
441  uint64_t plaintext_size;
442  uint8_t ivec[16];
443  uint8_t tmpbuf[16];
444  int index;
445 
446  if (!mxf->aesc && s->key && s->keylen == 16) {
447  mxf->aesc = av_aes_alloc();
448  if (!mxf->aesc)
449  return AVERROR(ENOMEM);
450  av_aes_init(mxf->aesc, s->key, 128, 1);
451  }
452  // crypto context
454  // plaintext offset
456  plaintext_size = avio_rb64(pb);
457  // source klv key
459  avio_read(pb, klv->key, 16);
461  return AVERROR_INVALIDDATA;
462  index = mxf_get_stream_index(s, klv);
463  if (index < 0)
464  return AVERROR_INVALIDDATA;
465  // source size
467  orig_size = avio_rb64(pb);
468  if (orig_size < plaintext_size)
469  return AVERROR_INVALIDDATA;
470  // enc. code
471  size = klv_decode_ber_length(pb);
472  if (size < 32 || size - 32 < orig_size || (int)orig_size != orig_size)
473  return AVERROR_INVALIDDATA;
474  avio_read(pb, ivec, 16);
475  avio_read(pb, tmpbuf, 16);
476  if (mxf->aesc)
477  av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
478  if (memcmp(tmpbuf, checkv, 16))
479  av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
480  size -= 32;
481  size = av_get_packet(pb, pkt, size);
482  if (size < 0)
483  return size;
484  else if (size < plaintext_size)
485  return AVERROR_INVALIDDATA;
486  size -= plaintext_size;
487  if (mxf->aesc)
488  av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
489  &pkt->data[plaintext_size], size >> 4, ivec, 1);
490  av_shrink_packet(pkt, orig_size);
491  pkt->stream_index = index;
492  avio_skip(pb, end - avio_tell(pb));
493  return 0;
494 }
495 
496 static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
497 {
498  MXFContext *mxf = arg;
499  int item_num = avio_rb32(pb);
500  int item_len = avio_rb32(pb);
501 
502  if (item_len != 18) {
503  avpriv_request_sample(pb, "Primer pack item length %d", item_len);
504  return AVERROR_PATCHWELCOME;
505  }
506  if (item_num > 65536 || item_num < 0) {
507  av_log(mxf->fc, AV_LOG_ERROR, "item_num %d is too large\n", item_num);
508  return AVERROR_INVALIDDATA;
509  }
510  if (mxf->local_tags)
511  av_log(mxf->fc, AV_LOG_VERBOSE, "Multiple primer packs\n");
512  av_free(mxf->local_tags);
513  mxf->local_tags_count = 0;
514  mxf->local_tags = av_calloc(item_num, item_len);
515  if (!mxf->local_tags)
516  return AVERROR(ENOMEM);
517  mxf->local_tags_count = item_num;
518  avio_read(pb, mxf->local_tags, item_num*item_len);
519  return 0;
520 }
521 
522 static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
523 {
524  MXFContext *mxf = arg;
525  MXFPartition *partition, *tmp_part;
526  UID op;
527  uint64_t footer_partition;
528  uint32_t nb_essence_containers;
529 
530  tmp_part = av_realloc_array(mxf->partitions, mxf->partitions_count + 1, sizeof(*mxf->partitions));
531  if (!tmp_part)
532  return AVERROR(ENOMEM);
533  mxf->partitions = tmp_part;
534 
535  if (mxf->parsing_backward) {
536  /* insert the new partition pack in the middle
537  * this makes the entries in mxf->partitions sorted by offset */
538  memmove(&mxf->partitions[mxf->last_forward_partition+1],
540  (mxf->partitions_count - mxf->last_forward_partition)*sizeof(*mxf->partitions));
541  partition = mxf->current_partition = &mxf->partitions[mxf->last_forward_partition];
542  } else {
543  mxf->last_forward_partition++;
544  partition = mxf->current_partition = &mxf->partitions[mxf->partitions_count];
545  }
546 
547  memset(partition, 0, sizeof(*partition));
548  mxf->partitions_count++;
549  partition->pack_length = avio_tell(pb) - klv_offset + size;
550  partition->pack_ofs = klv_offset;
551 
552  switch(uid[13]) {
553  case 2:
554  partition->type = Header;
555  break;
556  case 3:
557  partition->type = BodyPartition;
558  break;
559  case 4:
560  partition->type = Footer;
561  break;
562  default:
563  av_log(mxf->fc, AV_LOG_ERROR, "unknown partition type %i\n", uid[13]);
564  return AVERROR_INVALIDDATA;
565  }
566 
567  /* consider both footers to be closed (there is only Footer and CompleteFooter) */
568  partition->closed = partition->type == Footer || !(uid[14] & 1);
569  partition->complete = uid[14] > 2;
570  avio_skip(pb, 4);
571  partition->kag_size = avio_rb32(pb);
572  partition->this_partition = avio_rb64(pb);
573  partition->previous_partition = avio_rb64(pb);
574  footer_partition = avio_rb64(pb);
575  partition->header_byte_count = avio_rb64(pb);
576  partition->index_byte_count = avio_rb64(pb);
577  partition->index_sid = avio_rb32(pb);
578  avio_skip(pb, 8);
579  partition->body_sid = avio_rb32(pb);
580  if (avio_read(pb, op, sizeof(UID)) != sizeof(UID)) {
581  av_log(mxf->fc, AV_LOG_ERROR, "Failed reading UID\n");
582  return AVERROR_INVALIDDATA;
583  }
584  nb_essence_containers = avio_rb32(pb);
585 
586  if (partition->this_partition &&
587  partition->previous_partition == partition->this_partition) {
588  av_log(mxf->fc, AV_LOG_ERROR,
589  "PreviousPartition equal to ThisPartition %"PRIx64"\n",
590  partition->previous_partition);
591  /* override with the actual previous partition offset */
592  if (!mxf->parsing_backward && mxf->last_forward_partition > 1) {
593  MXFPartition *prev =
594  mxf->partitions + mxf->last_forward_partition - 2;
595  partition->previous_partition = prev->this_partition;
596  }
597  /* if no previous body partition are found point to the header
598  * partition */
599  if (partition->previous_partition == partition->this_partition)
600  partition->previous_partition = 0;
601  av_log(mxf->fc, AV_LOG_ERROR,
602  "Overriding PreviousPartition with %"PRIx64"\n",
603  partition->previous_partition);
604  }
605 
606  /* some files don't have FooterPartition set in every partition */
607  if (footer_partition) {
608  if (mxf->footer_partition && mxf->footer_partition != footer_partition) {
609  av_log(mxf->fc, AV_LOG_ERROR,
610  "inconsistent FooterPartition value: %"PRIu64" != %"PRIu64"\n",
611  mxf->footer_partition, footer_partition);
612  } else {
613  mxf->footer_partition = footer_partition;
614  }
615  }
616 
617  av_log(mxf->fc, AV_LOG_TRACE,
618  "PartitionPack: ThisPartition = 0x%"PRIX64
619  ", PreviousPartition = 0x%"PRIX64", "
620  "FooterPartition = 0x%"PRIX64", IndexSID = %i, BodySID = %i\n",
621  partition->this_partition,
622  partition->previous_partition, footer_partition,
623  partition->index_sid, partition->body_sid);
624 
625  /* sanity check PreviousPartition if set */
626  //NOTE: this isn't actually enough, see mxf_seek_to_previous_partition()
627  if (partition->previous_partition &&
628  mxf->run_in + partition->previous_partition >= klv_offset) {
629  av_log(mxf->fc, AV_LOG_ERROR,
630  "PreviousPartition points to this partition or forward\n");
631  return AVERROR_INVALIDDATA;
632  }
633 
634  if (op[12] == 1 && op[13] == 1) mxf->op = OP1a;
635  else if (op[12] == 1 && op[13] == 2) mxf->op = OP1b;
636  else if (op[12] == 1 && op[13] == 3) mxf->op = OP1c;
637  else if (op[12] == 2 && op[13] == 1) mxf->op = OP2a;
638  else if (op[12] == 2 && op[13] == 2) mxf->op = OP2b;
639  else if (op[12] == 2 && op[13] == 3) mxf->op = OP2c;
640  else if (op[12] == 3 && op[13] == 1) mxf->op = OP3a;
641  else if (op[12] == 3 && op[13] == 2) mxf->op = OP3b;
642  else if (op[12] == 3 && op[13] == 3) mxf->op = OP3c;
643  else if (op[12] == 64&& op[13] == 1) mxf->op = OPSONYOpt;
644  else if (op[12] == 0x10) {
645  /* SMPTE 390m: "There shall be exactly one essence container"
646  * The following block deals with files that violate this, namely:
647  * 2011_DCPTEST_24FPS.V.mxf - two ECs, OP1a
648  * abcdefghiv016f56415e.mxf - zero ECs, OPAtom, output by Avid AirSpeed */
649  if (nb_essence_containers != 1) {
650  MXFOP op = nb_essence_containers ? OP1a : OPAtom;
651 
652  /* only nag once */
653  if (!mxf->op)
654  av_log(mxf->fc, AV_LOG_WARNING,
655  "\"OPAtom\" with %"PRIu32" ECs - assuming %s\n",
656  nb_essence_containers,
657  op == OP1a ? "OP1a" : "OPAtom");
658 
659  mxf->op = op;
660  } else
661  mxf->op = OPAtom;
662  } else {
663  av_log(mxf->fc, AV_LOG_ERROR, "unknown operational pattern: %02xh %02xh - guessing OP1a\n", op[12], op[13]);
664  mxf->op = OP1a;
665  }
666 
667  if (partition->kag_size <= 0 || partition->kag_size > (1 << 20)) {
668  av_log(mxf->fc, AV_LOG_WARNING, "invalid KAGSize %"PRId32" - guessing ",
669  partition->kag_size);
670 
671  if (mxf->op == OPSONYOpt)
672  partition->kag_size = 512;
673  else
674  partition->kag_size = 1;
675 
676  av_log(mxf->fc, AV_LOG_WARNING, "%"PRId32"\n", partition->kag_size);
677  }
678 
679  return 0;
680 }
681 
682 static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
683 {
685 
686  tmp = av_realloc_array(mxf->metadata_sets, mxf->metadata_sets_count + 1, sizeof(*mxf->metadata_sets));
687  if (!tmp)
688  return AVERROR(ENOMEM);
689  mxf->metadata_sets = tmp;
690  mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
691  mxf->metadata_sets_count++;
692  return 0;
693 }
694 
695 static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
696 {
697  MXFCryptoContext *cryptocontext = arg;
698  if (size != 16)
699  return AVERROR_INVALIDDATA;
701  avio_read(pb, cryptocontext->source_container_ul, 16);
702  return 0;
703 }
704 
705 static int mxf_read_strong_ref_array(AVIOContext *pb, UID **refs, int *count)
706 {
707  *count = avio_rb32(pb);
708  *refs = av_calloc(*count, sizeof(UID));
709  if (!*refs) {
710  *count = 0;
711  return AVERROR(ENOMEM);
712  }
713  avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
714  avio_read(pb, (uint8_t *)*refs, *count * sizeof(UID));
715  return 0;
716 }
717 
718 static inline int mxf_read_utf16_string(AVIOContext *pb, int size, char** str, int be)
719 {
720  int ret;
721  size_t buf_size;
722 
723  if (size < 0 || size > INT_MAX/2)
724  return AVERROR(EINVAL);
725 
726  buf_size = size + size / 2 + 1;
727  av_free(*str);
728  *str = av_malloc(buf_size);
729  if (!*str)
730  return AVERROR(ENOMEM);
731 
732  if (be)
733  ret = avio_get_str16be(pb, size, *str, buf_size);
734  else
735  ret = avio_get_str16le(pb, size, *str, buf_size);
736 
737  if (ret < 0) {
738  av_freep(str);
739  return ret;
740  }
741 
742  return ret;
743 }
744 
745 #define READ_STR16(type, big_endian) \
746 static int mxf_read_utf16 ## type ##_string(AVIOContext *pb, int size, char** str) \
747 { \
748 return mxf_read_utf16_string(pb, size, str, big_endian); \
749 }
750 READ_STR16(be, 1)
751 READ_STR16(le, 0)
752 #undef READ_STR16
753 
754 static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
755 {
756  MXFContext *mxf = arg;
757  switch (tag) {
758  case 0x1901:
759  if (mxf->packages_refs)
760  av_log(mxf->fc, AV_LOG_VERBOSE, "Multiple packages_refs\n");
761  av_free(mxf->packages_refs);
762  return mxf_read_strong_ref_array(pb, &mxf->packages_refs, &mxf->packages_count);
763  }
764  return 0;
765 }
766 
767 static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
768 {
769  MXFStructuralComponent *source_clip = arg;
770  switch(tag) {
771  case 0x0202:
772  source_clip->duration = avio_rb64(pb);
773  break;
774  case 0x1201:
775  source_clip->start_position = avio_rb64(pb);
776  break;
777  case 0x1101:
778  /* UMID, only get last 16 bytes */
779  avio_read(pb, source_clip->source_package_ul, 16);
780  avio_read(pb, source_clip->source_package_uid, 16);
781  break;
782  case 0x1102:
783  source_clip->source_track_id = avio_rb32(pb);
784  break;
785  }
786  return 0;
787 }
788 
789 static int mxf_read_timecode_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
790 {
791  MXFTimecodeComponent *mxf_timecode = arg;
792  switch(tag) {
793  case 0x1501:
794  mxf_timecode->start_frame = avio_rb64(pb);
795  break;
796  case 0x1502:
797  mxf_timecode->rate = (AVRational){avio_rb16(pb), 1};
798  break;
799  case 0x1503:
800  mxf_timecode->drop_frame = avio_r8(pb);
801  break;
802  }
803  return 0;
804 }
805 
806 static int mxf_read_pulldown_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
807 {
808  MXFPulldownComponent *mxf_pulldown = arg;
809  switch(tag) {
810  case 0x0d01:
811  avio_read(pb, mxf_pulldown->input_segment_ref, 16);
812  break;
813  }
814  return 0;
815 }
816 
817 static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
818 {
819  MXFTrack *track = arg;
820  switch(tag) {
821  case 0x4801:
822  track->track_id = avio_rb32(pb);
823  break;
824  case 0x4804:
825  avio_read(pb, track->track_number, 4);
826  break;
827  case 0x4802:
828  mxf_read_utf16be_string(pb, size, &track->name);
829  break;
830  case 0x4b01:
831  track->edit_rate.num = avio_rb32(pb);
832  track->edit_rate.den = avio_rb32(pb);
833  break;
834  case 0x4803:
835  avio_read(pb, track->sequence_ref, 16);
836  break;
837  }
838  return 0;
839 }
840 
841 static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
842 {
843  MXFSequence *sequence = arg;
844  switch(tag) {
845  case 0x0202:
846  sequence->duration = avio_rb64(pb);
847  break;
848  case 0x0201:
849  avio_read(pb, sequence->data_definition_ul, 16);
850  break;
851  case 0x4b02:
852  sequence->origin = avio_r8(pb);
853  break;
854  case 0x1001:
856  &sequence->structural_components_count);
857  }
858  return 0;
859 }
860 
861 static int mxf_read_essence_group(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
862 {
863  MXFEssenceGroup *essence_group = arg;
864  switch (tag) {
865  case 0x0202:
866  essence_group->duration = avio_rb64(pb);
867  break;
868  case 0x0501:
869  return mxf_read_strong_ref_array(pb, &essence_group->structural_components_refs,
870  &essence_group->structural_components_count);
871  }
872  return 0;
873 }
874 
875 static int mxf_read_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
876 {
877  MXFPackage *package = arg;
878  switch(tag) {
879  case 0x4403:
880  return mxf_read_strong_ref_array(pb, &package->tracks_refs,
881  &package->tracks_count);
882  case 0x4401:
883  /* UMID */
884  avio_read(pb, package->package_ul, 16);
885  avio_read(pb, package->package_uid, 16);
886  break;
887  case 0x4701:
888  avio_read(pb, package->descriptor_ref, 16);
889  break;
890  case 0x4402:
891  return mxf_read_utf16be_string(pb, size, &package->name);
892  case 0x4406:
893  return mxf_read_strong_ref_array(pb, &package->comment_refs,
894  &package->comment_count);
895  }
896  return 0;
897 }
898 
900 {
901  int i, length;
902 
903  segment->nb_index_entries = avio_rb32(pb);
904 
905  length = avio_rb32(pb);
906  if(segment->nb_index_entries && length < 11)
907  return AVERROR_INVALIDDATA;
908 
909  if (!(segment->temporal_offset_entries=av_calloc(segment->nb_index_entries, sizeof(*segment->temporal_offset_entries))) ||
910  !(segment->flag_entries = av_calloc(segment->nb_index_entries, sizeof(*segment->flag_entries))) ||
911  !(segment->stream_offset_entries = av_calloc(segment->nb_index_entries, sizeof(*segment->stream_offset_entries)))) {
913  av_freep(&segment->flag_entries);
914  return AVERROR(ENOMEM);
915  }
916 
917  for (i = 0; i < segment->nb_index_entries; i++) {
918  if(avio_feof(pb))
919  return AVERROR_INVALIDDATA;
920  segment->temporal_offset_entries[i] = avio_r8(pb);
921  avio_r8(pb); /* KeyFrameOffset */
922  segment->flag_entries[i] = avio_r8(pb);
923  segment->stream_offset_entries[i] = avio_rb64(pb);
924  avio_skip(pb, length - 11);
925  }
926  return 0;
927 }
928 
929 static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
930 {
932  switch(tag) {
933  case 0x3F05:
934  segment->edit_unit_byte_count = avio_rb32(pb);
935  av_log(NULL, AV_LOG_TRACE, "EditUnitByteCount %d\n", segment->edit_unit_byte_count);
936  break;
937  case 0x3F06:
938  segment->index_sid = avio_rb32(pb);
939  av_log(NULL, AV_LOG_TRACE, "IndexSID %d\n", segment->index_sid);
940  break;
941  case 0x3F07:
942  segment->body_sid = avio_rb32(pb);
943  av_log(NULL, AV_LOG_TRACE, "BodySID %d\n", segment->body_sid);
944  break;
945  case 0x3F0A:
946  av_log(NULL, AV_LOG_TRACE, "IndexEntryArray found\n");
947  return mxf_read_index_entry_array(pb, segment);
948  case 0x3F0B:
949  segment->index_edit_rate.num = avio_rb32(pb);
950  segment->index_edit_rate.den = avio_rb32(pb);
951  av_log(NULL, AV_LOG_TRACE, "IndexEditRate %d/%d\n", segment->index_edit_rate.num,
952  segment->index_edit_rate.den);
953  break;
954  case 0x3F0C:
955  segment->index_start_position = avio_rb64(pb);
956  av_log(NULL, AV_LOG_TRACE, "IndexStartPosition %"PRId64"\n", segment->index_start_position);
957  break;
958  case 0x3F0D:
959  segment->index_duration = avio_rb64(pb);
960  av_log(NULL, AV_LOG_TRACE, "IndexDuration %"PRId64"\n", segment->index_duration);
961  break;
962  }
963  return 0;
964 }
965 
966 static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
967 {
968  int code, value, ofs = 0;
969  char layout[16] = {0}; /* not for printing, may end up not terminated on purpose */
970 
971  do {
972  code = avio_r8(pb);
973  value = avio_r8(pb);
974  av_log(NULL, AV_LOG_TRACE, "pixel layout: code %#x\n", code);
975 
976  if (ofs <= 14) {
977  layout[ofs++] = code;
978  layout[ofs++] = value;
979  } else
980  break; /* don't read byte by byte on sneaky files filled with lots of non-zeroes */
981  } while (code != 0); /* SMPTE 377M E.2.46 */
982 
983  ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
984 }
985 
986 static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
987 {
988  MXFDescriptor *descriptor = arg;
989  int entry_count, entry_size;
990 
991  switch(tag) {
992  case 0x3F01:
993  return mxf_read_strong_ref_array(pb, &descriptor->sub_descriptors_refs,
994  &descriptor->sub_descriptors_count);
995  case 0x3002: /* ContainerDuration */
996  descriptor->duration = avio_rb64(pb);
997  break;
998  case 0x3004:
999  avio_read(pb, descriptor->essence_container_ul, 16);
1000  break;
1001  case 0x3005:
1002  avio_read(pb, descriptor->codec_ul, 16);
1003  break;
1004  case 0x3006:
1005  descriptor->linked_track_id = avio_rb32(pb);
1006  break;
1007  case 0x3201: /* PictureEssenceCoding */
1008  avio_read(pb, descriptor->essence_codec_ul, 16);
1009  break;
1010  case 0x3203:
1011  descriptor->width = avio_rb32(pb);
1012  break;
1013  case 0x3202:
1014  descriptor->height = avio_rb32(pb);
1015  break;
1016  case 0x320C:
1017  descriptor->frame_layout = avio_r8(pb);
1018  break;
1019  case 0x320D:
1020  entry_count = avio_rb32(pb);
1021  entry_size = avio_rb32(pb);
1022  if (entry_size == 4) {
1023  if (entry_count > 0)
1024  descriptor->video_line_map[0] = avio_rb32(pb);
1025  else
1026  descriptor->video_line_map[0] = 0;
1027  if (entry_count > 1)
1028  descriptor->video_line_map[1] = avio_rb32(pb);
1029  else
1030  descriptor->video_line_map[1] = 0;
1031  } else
1032  av_log(NULL, AV_LOG_WARNING, "VideoLineMap element size %d currently not supported\n", entry_size);
1033  break;
1034  case 0x320E:
1035  descriptor->aspect_ratio.num = avio_rb32(pb);
1036  descriptor->aspect_ratio.den = avio_rb32(pb);
1037  break;
1038  case 0x3212:
1039  descriptor->field_dominance = avio_r8(pb);
1040  break;
1041  case 0x3301:
1042  descriptor->component_depth = avio_rb32(pb);
1043  break;
1044  case 0x3302:
1045  descriptor->horiz_subsampling = avio_rb32(pb);
1046  break;
1047  case 0x3308:
1048  descriptor->vert_subsampling = avio_rb32(pb);
1049  break;
1050  case 0x3D03:
1051  descriptor->sample_rate.num = avio_rb32(pb);
1052  descriptor->sample_rate.den = avio_rb32(pb);
1053  break;
1054  case 0x3D06: /* SoundEssenceCompression */
1055  avio_read(pb, descriptor->essence_codec_ul, 16);
1056  break;
1057  case 0x3D07:
1058  descriptor->channels = avio_rb32(pb);
1059  break;
1060  case 0x3D01:
1061  descriptor->bits_per_sample = avio_rb32(pb);
1062  break;
1063  case 0x3401:
1064  mxf_read_pixel_layout(pb, descriptor);
1065  break;
1066  default:
1067  /* Private uid used by SONY C0023S01.mxf */
1069  if (descriptor->extradata)
1070  av_log(NULL, AV_LOG_WARNING, "Duplicate sony_mpeg4_extradata\n");
1071  av_free(descriptor->extradata);
1072  descriptor->extradata_size = 0;
1073  descriptor->extradata = av_malloc(size);
1074  if (!descriptor->extradata)
1075  return AVERROR(ENOMEM);
1076  descriptor->extradata_size = size;
1077  avio_read(pb, descriptor->extradata, size);
1078  }
1079  if (IS_KLV_KEY(uid, mxf_jp2k_rsiz)) {
1080  uint32_t rsiz = avio_rb16(pb);
1081  if (rsiz == FF_PROFILE_JPEG2000_DCINEMA_2K ||
1083  descriptor->pix_fmt = AV_PIX_FMT_XYZ12;
1084  }
1085  break;
1086  }
1087  return 0;
1088 }
1089 
1090 static int mxf_read_indirect_value(void *arg, AVIOContext *pb, int size)
1091 {
1092  MXFTaggedValue *tagged_value = arg;
1093  uint8_t key[17];
1094 
1095  if (size <= 17)
1096  return 0;
1097 
1098  avio_read(pb, key, 17);
1099  /* TODO: handle other types of of indirect values */
1100  if (memcmp(key, mxf_indirect_value_utf16le, 17) == 0) {
1101  return mxf_read_utf16le_string(pb, size - 17, &tagged_value->value);
1102  } else if (memcmp(key, mxf_indirect_value_utf16be, 17) == 0) {
1103  return mxf_read_utf16be_string(pb, size - 17, &tagged_value->value);
1104  }
1105  return 0;
1106 }
1107 
1108 static int mxf_read_tagged_value(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
1109 {
1110  MXFTaggedValue *tagged_value = arg;
1111  switch (tag){
1112  case 0x5001:
1113  return mxf_read_utf16be_string(pb, size, &tagged_value->name);
1114  case 0x5003:
1115  return mxf_read_indirect_value(tagged_value, pb, size);
1116  }
1117  return 0;
1118 }
1119 
1120 /*
1121  * Match an uid independently of the version byte and up to len common bytes
1122  * Returns: boolean
1123  */
1124 static int mxf_match_uid(const UID key, const UID uid, int len)
1125 {
1126  int i;
1127  for (i = 0; i < len; i++) {
1128  if (i != 7 && key[i] != uid[i])
1129  return 0;
1130  }
1131  return 1;
1132 }
1133 
1134 static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
1135 {
1136  while (uls->uid[0]) {
1137  if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
1138  break;
1139  uls++;
1140  }
1141  return uls;
1142 }
1143 
1144 static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
1145 {
1146  int i;
1147 
1148  if (!strong_ref)
1149  return NULL;
1150  for (i = 0; i < mxf->metadata_sets_count; i++) {
1151  if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
1152  (type == AnyType || mxf->metadata_sets[i]->type == type)) {
1153  return mxf->metadata_sets[i];
1154  }
1155  }
1156  return NULL;
1157 }
1158 
1160  // video essence container uls
1161  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 }, 14, AV_CODEC_ID_JPEG2000 },
1162  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x10,0x60,0x01 }, 14, AV_CODEC_ID_H264 }, /* H.264 frame wrapped */
1163  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x12,0x01,0x00 }, 14, AV_CODEC_ID_VC1 }, /* VC-1 frame wrapped */
1164  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MPEG-ES frame wrapped */
1165  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* Type D-10 mapping of 40Mbps 525/60-I */
1166  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, AV_CODEC_ID_DVVIDEO }, /* DV 625 25mbps */
1167  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14, AV_CODEC_ID_RAWVIDEO }, /* uncompressed picture */
1168  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x01,0x01 }, 15, AV_CODEC_ID_HQ_HQA },
1169  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0a,0x0e,0x0f,0x03,0x01,0x02,0x20,0x02,0x01 }, 15, AV_CODEC_ID_HQX },
1170  { { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0xff,0x4b,0x46,0x41,0x41,0x00,0x0d,0x4d,0x4f }, 14, AV_CODEC_ID_RAWVIDEO }, /* Legacy ?? Uncompressed Picture */
1171  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1172 };
1173 
1174 /* EC ULs for intra-only formats */
1176  { { 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 */
1177  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1178 };
1179 
1180 /* intra-only PictureEssenceCoding ULs, where no corresponding EC UL exists */
1182  { { 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 */
1183  { { 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 */
1184  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1185 };
1186 
1187 /* actual coded width for AVC-Intra to allow selecting correct SPS/PPS */
1189  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 16, 1440 },
1190  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 16, 1440 },
1191  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 16, 1440 },
1192  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 16, 1440 },
1193  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, 0 },
1194 };
1195 
1197  // sound essence container uls
1198  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, AV_CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */
1199  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x02,0x0d,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, AV_CODEC_ID_MP2 }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */
1200  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, AV_CODEC_ID_PCM_S16LE }, /* D-10 Mapping 50Mbps PAL Extended Template */
1201  { { 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 */
1202  { { 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) */
1203  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1204 };
1205 
1207  { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 }, 16, 0 },
1208  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
1209 };
1210 
1211 static const char * const mxf_data_essence_descriptor[] = {
1212  "vbi_vanc_smpte_436M",
1213 };
1214 
1215 static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
1216 {
1217  int i, j, nb_segments = 0;
1218  MXFIndexTableSegment **unsorted_segments;
1219  int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
1220 
1221  /* count number of segments, allocate arrays and copy unsorted segments */
1222  for (i = 0; i < mxf->metadata_sets_count; i++)
1223  if (mxf->metadata_sets[i]->type == IndexTableSegment)
1224  nb_segments++;
1225 
1226  if (!nb_segments)
1227  return AVERROR_INVALIDDATA;
1228 
1229  if (!(unsorted_segments = av_calloc(nb_segments, sizeof(*unsorted_segments))) ||
1230  !(*sorted_segments = av_calloc(nb_segments, sizeof(**sorted_segments)))) {
1231  av_freep(sorted_segments);
1232  av_free(unsorted_segments);
1233  return AVERROR(ENOMEM);
1234  }
1235 
1236  for (i = j = 0; i < mxf->metadata_sets_count; i++)
1237  if (mxf->metadata_sets[i]->type == IndexTableSegment)
1238  unsorted_segments[j++] = (MXFIndexTableSegment*)mxf->metadata_sets[i];
1239 
1240  *nb_sorted_segments = 0;
1241 
1242  /* sort segments by {BodySID, IndexSID, IndexStartPosition}, remove duplicates while we're at it */
1243  for (i = 0; i < nb_segments; i++) {
1244  int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
1245  uint64_t best_index_duration = 0;
1246 
1247  for (j = 0; j < nb_segments; j++) {
1248  MXFIndexTableSegment *s = unsorted_segments[j];
1249 
1250  /* Require larger BosySID, IndexSID or IndexStartPosition then the previous entry. This removes duplicates.
1251  * We want the smallest values for the keys than what we currently have, unless this is the first such entry this time around.
1252  * If we come across an entry with the same IndexStartPosition but larger IndexDuration, then we'll prefer it over the one we currently have.
1253  */
1254  if ((i == 0 || s->body_sid > last_body_sid || s->index_sid > last_index_sid || s->index_start_position > last_index_start) &&
1255  (best == -1 || s->body_sid < best_body_sid || s->index_sid < best_index_sid || s->index_start_position < best_index_start ||
1256  (s->index_start_position == best_index_start && s->index_duration > best_index_duration))) {
1257  best = j;
1258  best_body_sid = s->body_sid;
1259  best_index_sid = s->index_sid;
1260  best_index_start = s->index_start_position;
1261  best_index_duration = s->index_duration;
1262  }
1263  }
1264 
1265  /* no suitable entry found -> we're done */
1266  if (best == -1)
1267  break;
1268 
1269  (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
1270  last_body_sid = best_body_sid;
1271  last_index_sid = best_index_sid;
1272  last_index_start = best_index_start;
1273  }
1274 
1275  av_free(unsorted_segments);
1276 
1277  return 0;
1278 }
1279 
1280 /**
1281  * Computes the absolute file offset of the given essence container offset
1282  */
1283 static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t offset, int64_t *offset_out)
1284 {
1285  int x;
1286  int64_t offset_in = offset; /* for logging */
1287 
1288  for (x = 0; x < mxf->partitions_count; x++) {
1289  MXFPartition *p = &mxf->partitions[x];
1290 
1291  if (p->body_sid != body_sid)
1292  continue;
1293 
1294  if (offset < p->essence_length || !p->essence_length) {
1295  *offset_out = p->essence_offset + offset;
1296  return 0;
1297  }
1298 
1299  offset -= p->essence_length;
1300  }
1301 
1302  av_log(mxf->fc, AV_LOG_ERROR,
1303  "failed to find absolute offset of %"PRIX64" in BodySID %i - partial file?\n",
1304  offset_in, body_sid);
1305 
1306  return AVERROR_INVALIDDATA;
1307 }
1308 
1309 /**
1310  * Returns the end position of the essence container with given BodySID, or zero if unknown
1311  */
1313 {
1314  int x;
1315  int64_t ret = 0;
1316 
1317  for (x = 0; x < mxf->partitions_count; x++) {
1318  MXFPartition *p = &mxf->partitions[x];
1319 
1320  if (p->body_sid != body_sid)
1321  continue;
1322 
1323  if (!p->essence_length)
1324  return 0;
1325 
1326  ret = p->essence_offset + p->essence_length;
1327  }
1328 
1329  return ret;
1330 }
1331 
1332 /* EditUnit -> absolute offset */
1333 static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_table, int64_t edit_unit, int64_t *edit_unit_out, int64_t *offset_out, int nag)
1334 {
1335  int i;
1336  int64_t offset_temp = 0;
1337 
1338  for (i = 0; i < index_table->nb_segments; i++) {
1339  MXFIndexTableSegment *s = index_table->segments[i];
1340 
1341  edit_unit = FFMAX(edit_unit, s->index_start_position); /* clamp if trying to seek before start */
1342 
1343  if (edit_unit < s->index_start_position + s->index_duration) {
1344  int64_t index = edit_unit - s->index_start_position;
1345 
1346  if (s->edit_unit_byte_count)
1347  offset_temp += s->edit_unit_byte_count * index;
1348  else if (s->nb_index_entries) {
1349  if (s->nb_index_entries == 2 * s->index_duration + 1)
1350  index *= 2; /* Avid index */
1351 
1352  if (index < 0 || index >= s->nb_index_entries) {
1353  av_log(mxf->fc, AV_LOG_ERROR, "IndexSID %i segment at %"PRId64" IndexEntryArray too small\n",
1354  index_table->index_sid, s->index_start_position);
1355  return AVERROR_INVALIDDATA;
1356  }
1357 
1358  offset_temp = s->stream_offset_entries[index];
1359  } else {
1360  av_log(mxf->fc, AV_LOG_ERROR, "IndexSID %i segment at %"PRId64" missing EditUnitByteCount and IndexEntryArray\n",
1361  index_table->index_sid, s->index_start_position);
1362  return AVERROR_INVALIDDATA;
1363  }
1364 
1365  if (edit_unit_out)
1366  *edit_unit_out = edit_unit;
1367 
1368  return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, offset_temp, offset_out);
1369  } else {
1370  /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */
1371  offset_temp += s->edit_unit_byte_count * s->index_duration;
1372  }
1373  }
1374 
1375  if (nag)
1376  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);
1377 
1378  return AVERROR_INVALIDDATA;
1379 }
1380 
1382 {
1383  int i, j, x;
1384  int8_t max_temporal_offset = -128;
1385  uint8_t *flags;
1386 
1387  /* first compute how many entries we have */
1388  for (i = 0; i < index_table->nb_segments; i++) {
1389  MXFIndexTableSegment *s = index_table->segments[i];
1390 
1391  if (!s->nb_index_entries) {
1392  index_table->nb_ptses = 0;
1393  return 0; /* no TemporalOffsets */
1394  }
1395 
1396  index_table->nb_ptses += s->index_duration;
1397  }
1398 
1399  /* paranoid check */
1400  if (index_table->nb_ptses <= 0)
1401  return 0;
1402 
1403  if (!(index_table->ptses = av_calloc(index_table->nb_ptses, sizeof(int64_t))) ||
1404  !(index_table->fake_index = av_calloc(index_table->nb_ptses, sizeof(AVIndexEntry))) ||
1405  !(index_table->offsets = av_calloc(index_table->nb_ptses, sizeof(int8_t))) ||
1406  !(flags = av_calloc(index_table->nb_ptses, sizeof(uint8_t)))) {
1407  av_freep(&index_table->ptses);
1408  av_freep(&index_table->fake_index);
1409  av_freep(&index_table->offsets);
1410  return AVERROR(ENOMEM);
1411  }
1412 
1413  /* we may have a few bad TemporalOffsets
1414  * make sure the corresponding PTSes don't have the bogus value 0 */
1415  for (x = 0; x < index_table->nb_ptses; x++)
1416  index_table->ptses[x] = AV_NOPTS_VALUE;
1417 
1418  /**
1419  * We have this:
1420  *
1421  * x TemporalOffset
1422  * 0: 0
1423  * 1: 1
1424  * 2: 1
1425  * 3: -2
1426  * 4: 1
1427  * 5: 1
1428  * 6: -2
1429  *
1430  * We want to transform it into this:
1431  *
1432  * x DTS PTS
1433  * 0: -1 0
1434  * 1: 0 3
1435  * 2: 1 1
1436  * 3: 2 2
1437  * 4: 3 6
1438  * 5: 4 4
1439  * 6: 5 5
1440  *
1441  * We do this by bucket sorting x by x+TemporalOffset[x] into mxf->ptses,
1442  * then settings mxf->first_dts = -max(TemporalOffset[x]).
1443  * The latter makes DTS <= PTS.
1444  */
1445  for (i = x = 0; i < index_table->nb_segments; i++) {
1446  MXFIndexTableSegment *s = index_table->segments[i];
1447  int index_delta = 1;
1448  int n = s->nb_index_entries;
1449 
1450  if (s->nb_index_entries == 2 * s->index_duration + 1) {
1451  index_delta = 2; /* Avid index */
1452  /* ignore the last entry - it's the size of the essence container */
1453  n--;
1454  }
1455 
1456  for (j = 0; j < n; j += index_delta, x++) {
1457  int offset = s->temporal_offset_entries[j] / index_delta;
1458  int index = x + offset;
1459 
1460  if (x >= index_table->nb_ptses) {
1461  av_log(mxf->fc, AV_LOG_ERROR,
1462  "x >= nb_ptses - IndexEntryCount %i < IndexDuration %"PRId64"?\n",
1464  break;
1465  }
1466 
1467  flags[x] = !(s->flag_entries[j] & 0x30) ? AVINDEX_KEYFRAME : 0;
1468 
1469  if (index < 0 || index >= index_table->nb_ptses) {
1470  av_log(mxf->fc, AV_LOG_ERROR,
1471  "index entry %i + TemporalOffset %i = %i, which is out of bounds\n",
1472  x, offset, index);
1473  continue;
1474  }
1475 
1476  index_table->offsets[x] = offset;
1477  index_table->ptses[index] = x;
1478  max_temporal_offset = FFMAX(max_temporal_offset, offset);
1479  }
1480  }
1481 
1482  /* calculate the fake index table in display order */
1483  for (x = 0; x < index_table->nb_ptses; x++) {
1484  index_table->fake_index[x].timestamp = x;
1485  if (index_table->ptses[x] != AV_NOPTS_VALUE)
1486  index_table->fake_index[index_table->ptses[x]].flags = flags[x];
1487  }
1488  av_freep(&flags);
1489 
1490  index_table->first_dts = -max_temporal_offset;
1491 
1492  return 0;
1493 }
1494 
1495 /**
1496  * Sorts and collects index table segments into index tables.
1497  * Also computes PTSes if possible.
1498  */
1500 {
1501  int i, j, k, ret, nb_sorted_segments;
1502  MXFIndexTableSegment **sorted_segments = NULL;
1503  AVStream *st = NULL;
1504 
1505  for (i = 0; i < mxf->fc->nb_streams; i++) {
1506  if (mxf->fc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1507  continue;
1508  st = mxf->fc->streams[i];
1509  break;
1510  }
1511 
1512  if ((ret = mxf_get_sorted_table_segments(mxf, &nb_sorted_segments, &sorted_segments)) ||
1513  nb_sorted_segments <= 0) {
1514  av_log(mxf->fc, AV_LOG_WARNING, "broken or empty index\n");
1515  return 0;
1516  }
1517 
1518  /* sanity check and count unique BodySIDs/IndexSIDs */
1519  for (i = 0; i < nb_sorted_segments; i++) {
1520  if (i == 0 || sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid)
1521  mxf->nb_index_tables++;
1522  else if (sorted_segments[i-1]->body_sid != sorted_segments[i]->body_sid) {
1523  av_log(mxf->fc, AV_LOG_ERROR, "found inconsistent BodySID\n");
1524  ret = AVERROR_INVALIDDATA;
1525  goto finish_decoding_index;
1526  }
1527  }
1528 
1530  sizeof(*mxf->index_tables));
1531  if (!mxf->index_tables) {
1532  av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate index tables\n");
1533  ret = AVERROR(ENOMEM);
1534  goto finish_decoding_index;
1535  }
1536 
1537  /* distribute sorted segments to index tables */
1538  for (i = j = 0; i < nb_sorted_segments; i++) {
1539  if (i != 0 && sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid) {
1540  /* next IndexSID */
1541  j++;
1542  }
1543 
1544  mxf->index_tables[j].nb_segments++;
1545  }
1546 
1547  for (i = j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) {
1548  MXFIndexTable *t = &mxf->index_tables[j];
1549 
1551  sizeof(*t->segments));
1552 
1553  if (!t->segments) {
1554  av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate IndexTableSegment"
1555  " pointer array\n");
1556  ret = AVERROR(ENOMEM);
1557  goto finish_decoding_index;
1558  }
1559 
1560  if (sorted_segments[i]->index_start_position)
1561  av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i starts at EditUnit %"PRId64" - seeking may not work as expected\n",
1562  sorted_segments[i]->index_sid, sorted_segments[i]->index_start_position);
1563 
1564  memcpy(t->segments, &sorted_segments[i], t->nb_segments * sizeof(MXFIndexTableSegment*));
1565  t->index_sid = sorted_segments[i]->index_sid;
1566  t->body_sid = sorted_segments[i]->body_sid;
1567 
1568  if ((ret = mxf_compute_ptses_fake_index(mxf, t)) < 0)
1569  goto finish_decoding_index;
1570 
1571  /* fix zero IndexDurations */
1572  for (k = 0; k < t->nb_segments; k++) {
1573  if (t->segments[k]->index_duration)
1574  continue;
1575 
1576  if (t->nb_segments > 1)
1577  av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has zero IndexDuration and there's more than one segment\n",
1578  t->index_sid, k);
1579 
1580  if (!st) {
1581  av_log(mxf->fc, AV_LOG_WARNING, "no streams?\n");
1582  break;
1583  }
1584 
1585  /* assume the first stream's duration is reasonable
1586  * leave index_duration = 0 on further segments in case we have any (unlikely)
1587  */
1588  t->segments[k]->index_duration = st->duration;
1589  break;
1590  }
1591  }
1592 
1593  ret = 0;
1594 finish_decoding_index:
1595  av_free(sorted_segments);
1596  return ret;
1597 }
1598 
1599 static int mxf_is_intra_only(MXFDescriptor *descriptor)
1600 {
1601  return mxf_get_codec_ul(mxf_intra_only_essence_container_uls,
1602  &descriptor->essence_container_ul)->id != AV_CODEC_ID_NONE ||
1603  mxf_get_codec_ul(mxf_intra_only_picture_essence_coding_uls,
1604  &descriptor->essence_codec_ul)->id != AV_CODEC_ID_NONE;
1605 }
1606 
1607 static int mxf_uid_to_str(UID uid, char **str)
1608 {
1609  int i;
1610  char *p;
1611  p = *str = av_mallocz(sizeof(UID) * 2 + 4 + 1);
1612  if (!p)
1613  return AVERROR(ENOMEM);
1614  for (i = 0; i < sizeof(UID); i++) {
1615  snprintf(p, 2 + 1, "%.2x", uid[i]);
1616  p += 2;
1617  if (i == 3 || i == 5 || i == 7 || i == 9) {
1618  snprintf(p, 1 + 1, "-");
1619  p++;
1620  }
1621  }
1622  return 0;
1623 }
1624 
1625 static int mxf_umid_to_str(UID ul, UID uid, char **str)
1626 {
1627  int i;
1628  char *p;
1629  p = *str = av_mallocz(sizeof(UID) * 4 + 2 + 1);
1630  if (!p)
1631  return AVERROR(ENOMEM);
1632  snprintf(p, 2 + 1, "0x");
1633  p += 2;
1634  for (i = 0; i < sizeof(UID); i++) {
1635  snprintf(p, 2 + 1, "%.2X", ul[i]);
1636  p += 2;
1637 
1638  }
1639  for (i = 0; i < sizeof(UID); i++) {
1640  snprintf(p, 2 + 1, "%.2X", uid[i]);
1641  p += 2;
1642  }
1643  return 0;
1644 }
1645 
1646 static int mxf_add_umid_metadata(AVDictionary **pm, const char *key, MXFPackage* package)
1647 {
1648  char *str;
1649  int ret;
1650  if (!package)
1651  return 0;
1652  if ((ret = mxf_umid_to_str(package->package_ul, package->package_uid, &str)) < 0)
1653  return ret;
1654  av_dict_set(pm, key, str, AV_DICT_DONT_STRDUP_VAL);
1655  return 0;
1656 }
1657 
1658 static int mxf_add_timecode_metadata(AVDictionary **pm, const char *key, AVTimecode *tc)
1659 {
1660  char buf[AV_TIMECODE_STR_SIZE];
1661  av_dict_set(pm, key, av_timecode_make_string(tc, buf, 0), 0);
1662 
1663  return 0;
1664 }
1665 
1667 {
1668  MXFStructuralComponent *component = NULL;
1669  MXFPulldownComponent *pulldown = NULL;
1670 
1671  component = mxf_resolve_strong_ref(mxf, strong_ref, AnyType);
1672  if (!component)
1673  return NULL;
1674 
1675  switch (component->type) {
1676  case TimecodeComponent:
1677  return (MXFTimecodeComponent*)component;
1678  case PulldownComponent: /* timcode component may be located on a pulldown component */
1679  pulldown = (MXFPulldownComponent*)component;
1681  default:
1682  break;
1683  }
1684  return NULL;
1685 }
1686 
1688 {
1689  MXFPackage *package = NULL;
1690  int i;
1691 
1692  for (i = 0; i < mxf->packages_count; i++) {
1693  package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], SourcePackage);
1694  if (!package)
1695  continue;
1696 
1697  if (!memcmp(package->package_uid, package_uid, 16))
1698  return package;
1699  }
1700  return NULL;
1701 }
1702 
1703 static MXFDescriptor* mxf_resolve_multidescriptor(MXFContext *mxf, MXFDescriptor *descriptor, int track_id)
1704 {
1705  MXFDescriptor *sub_descriptor = NULL;
1706  int i;
1707 
1708  if (!descriptor)
1709  return NULL;
1710 
1711  if (descriptor->type == MultipleDescriptor) {
1712  for (i = 0; i < descriptor->sub_descriptors_count; i++) {
1713  sub_descriptor = mxf_resolve_strong_ref(mxf, &descriptor->sub_descriptors_refs[i], Descriptor);
1714 
1715  if (!sub_descriptor) {
1716  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
1717  continue;
1718  }
1719  if (sub_descriptor->linked_track_id == track_id) {
1720  return sub_descriptor;
1721  }
1722  }
1723  } else if (descriptor->type == Descriptor)
1724  return descriptor;
1725 
1726  return NULL;
1727 }
1728 
1730 {
1731  MXFStructuralComponent *component = NULL;
1732  MXFPackage *package = NULL;
1733  MXFDescriptor *descriptor = NULL;
1734  int i;
1735 
1736  if (!essence_group || !essence_group->structural_components_count)
1737  return NULL;
1738 
1739  /* essence groups contains multiple representations of the same media,
1740  this return the first components with a valid Descriptor typically index 0 */
1741  for (i =0; i < essence_group->structural_components_count; i++){
1742  component = mxf_resolve_strong_ref(mxf, &essence_group->structural_components_refs[i], SourceClip);
1743  if (!component)
1744  continue;
1745 
1746  if (!(package = mxf_resolve_source_package(mxf, component->source_package_uid)))
1747  continue;
1748 
1749  descriptor = mxf_resolve_strong_ref(mxf, &package->descriptor_ref, Descriptor);
1750  if (descriptor)
1751  return component;
1752  }
1753  return NULL;
1754 }
1755 
1757 {
1758  MXFStructuralComponent *component = NULL;
1759 
1760  component = mxf_resolve_strong_ref(mxf, strong_ref, AnyType);
1761  if (!component)
1762  return NULL;
1763  switch (component->type) {
1764  case SourceClip:
1765  return component;
1766  case EssenceGroup:
1767  return mxf_resolve_essence_group_choice(mxf, (MXFEssenceGroup*) component);
1768  default:
1769  break;
1770  }
1771  return NULL;
1772 }
1773 
1775 {
1777  int size, i;
1778  char *key = NULL;
1779 
1780  for (i = 0; i < package->comment_count; i++) {
1781  tag = mxf_resolve_strong_ref(mxf, &package->comment_refs[i], TaggedValue);
1782  if (!tag || !tag->name || !tag->value)
1783  continue;
1784 
1785  size = strlen(tag->name) + 8 + 1;
1786  key = av_mallocz(size);
1787  if (!key)
1788  return AVERROR(ENOMEM);
1789 
1790  snprintf(key, size, "comment_%s", tag->name);
1791  av_dict_set(pm, key, tag->value, AV_DICT_DONT_STRDUP_KEY);
1792  }
1793  return 0;
1794 }
1795 
1797 {
1798  MXFPackage *physical_package = NULL;
1799  MXFTrack *physical_track = NULL;
1800  MXFStructuralComponent *sourceclip = NULL;
1801  MXFTimecodeComponent *mxf_tc = NULL;
1802  int i, j, k;
1803  AVTimecode tc;
1804  int flags;
1805  int64_t start_position;
1806 
1807  for (i = 0; i < source_track->sequence->structural_components_count; i++) {
1808  sourceclip = mxf_resolve_strong_ref(mxf, &source_track->sequence->structural_components_refs[i], SourceClip);
1809  if (!sourceclip)
1810  continue;
1811 
1812  if (!(physical_package = mxf_resolve_source_package(mxf, sourceclip->source_package_uid)))
1813  break;
1814 
1815  mxf_add_umid_metadata(&st->metadata, "reel_umid", physical_package);
1816 
1817  /* the name of physical source package is name of the reel or tape */
1818  if (physical_package->name && physical_package->name[0])
1819  av_dict_set(&st->metadata, "reel_name", physical_package->name, 0);
1820 
1821  /* the source timecode is calculated by adding the start_position of the sourceclip from the file source package track
1822  * to the start_frame of the timecode component located on one of the tracks of the physical source package.
1823  */
1824  for (j = 0; j < physical_package->tracks_count; j++) {
1825  if (!(physical_track = mxf_resolve_strong_ref(mxf, &physical_package->tracks_refs[j], Track))) {
1826  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
1827  continue;
1828  }
1829 
1830  if (!(physical_track->sequence = mxf_resolve_strong_ref(mxf, &physical_track->sequence_ref, Sequence))) {
1831  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
1832  continue;
1833  }
1834 
1835  if (physical_track->edit_rate.num <= 0 ||
1836  physical_track->edit_rate.den <= 0) {
1837  av_log(mxf->fc, AV_LOG_WARNING,
1838  "Invalid edit rate (%d/%d) found on structural"
1839  " component #%d, defaulting to 25/1\n",
1840  physical_track->edit_rate.num,
1841  physical_track->edit_rate.den, i);
1842  physical_track->edit_rate = (AVRational){25, 1};
1843  }
1844 
1845  for (k = 0; k < physical_track->sequence->structural_components_count; k++) {
1846  if (!(mxf_tc = mxf_resolve_timecode_component(mxf, &physical_track->sequence->structural_components_refs[k])))
1847  continue;
1848 
1849  flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
1850  /* scale sourceclip start_position to match physical track edit rate */
1851  start_position = av_rescale_q(sourceclip->start_position,
1852  physical_track->edit_rate,
1853  source_track->edit_rate);
1854 
1855  if (av_timecode_init(&tc, mxf_tc->rate, flags, start_position + mxf_tc->start_frame, mxf->fc) == 0) {
1856  mxf_add_timecode_metadata(&st->metadata, "timecode", &tc);
1857  return 0;
1858  }
1859  }
1860  }
1861  }
1862 
1863  return 0;
1864 }
1865 
1867 {
1868  MXFStructuralComponent *component = NULL;
1869  const MXFCodecUL *codec_ul = NULL;
1870  MXFPackage tmp_package;
1871  AVStream *st;
1872  int j;
1873 
1874  for (j = 0; j < track->sequence->structural_components_count; j++) {
1875  component = mxf_resolve_sourceclip(mxf, &track->sequence->structural_components_refs[j]);
1876  if (!component)
1877  continue;
1878  break;
1879  }
1880  if (!component)
1881  return 0;
1882 
1883  st = avformat_new_stream(mxf->fc, NULL);
1884  if (!st) {
1885  av_log(mxf->fc, AV_LOG_ERROR, "could not allocate metadata stream\n");
1886  return AVERROR(ENOMEM);
1887  }
1888 
1891  st->id = track->track_id;
1892 
1893  memcpy(&tmp_package.package_ul, component->source_package_ul, 16);
1894  memcpy(&tmp_package.package_uid, component->source_package_uid, 16);
1895  mxf_add_umid_metadata(&st->metadata, "file_package_umid", &tmp_package);
1896  if (track->name && track->name[0])
1897  av_dict_set(&st->metadata, "track_name", track->name, 0);
1898 
1900  av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
1901  return 0;
1902 }
1903 
1905 {
1906  MXFPackage *material_package = NULL;
1907  int i, j, k, ret;
1908 
1909  av_log(mxf->fc, AV_LOG_TRACE, "metadata sets count %d\n", mxf->metadata_sets_count);
1910  /* TODO: handle multiple material packages (OP3x) */
1911  for (i = 0; i < mxf->packages_count; i++) {
1912  material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
1913  if (material_package) break;
1914  }
1915  if (!material_package) {
1916  av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
1917  return AVERROR_INVALIDDATA;
1918  }
1919 
1920  mxf_add_umid_metadata(&mxf->fc->metadata, "material_package_umid", material_package);
1921  if (material_package->name && material_package->name[0])
1922  av_dict_set(&mxf->fc->metadata, "material_package_name", material_package->name, 0);
1923  mxf_parse_package_comments(mxf, &mxf->fc->metadata, material_package);
1924 
1925  for (i = 0; i < material_package->tracks_count; i++) {
1926  MXFPackage *source_package = NULL;
1927  MXFTrack *material_track = NULL;
1928  MXFTrack *source_track = NULL;
1929  MXFTrack *temp_track = NULL;
1930  MXFDescriptor *descriptor = NULL;
1931  MXFStructuralComponent *component = NULL;
1932  MXFTimecodeComponent *mxf_tc = NULL;
1933  UID *essence_container_ul = NULL;
1934  const MXFCodecUL *codec_ul = NULL;
1935  const MXFCodecUL *container_ul = NULL;
1936  const MXFCodecUL *pix_fmt_ul = NULL;
1937  AVStream *st;
1938  AVTimecode tc;
1939  int flags;
1940 
1941  if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
1942  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
1943  continue;
1944  }
1945 
1946  if ((component = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, TimecodeComponent))) {
1947  mxf_tc = (MXFTimecodeComponent*)component;
1948  flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
1949  if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
1950  mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
1951  }
1952  }
1953 
1954  if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
1955  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
1956  continue;
1957  }
1958 
1959  for (j = 0; j < material_track->sequence->structural_components_count; j++) {
1960  component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], TimecodeComponent);
1961  if (!component)
1962  continue;
1963 
1964  mxf_tc = (MXFTimecodeComponent*)component;
1965  flags = mxf_tc->drop_frame == 1 ? AV_TIMECODE_FLAG_DROPFRAME : 0;
1966  if (av_timecode_init(&tc, mxf_tc->rate, flags, mxf_tc->start_frame, mxf->fc) == 0) {
1967  mxf_add_timecode_metadata(&mxf->fc->metadata, "timecode", &tc);
1968  break;
1969  }
1970  }
1971 
1972  /* TODO: handle multiple source clips, only finds first valid source clip */
1973  if(material_track->sequence->structural_components_count > 1)
1974  av_log(mxf->fc, AV_LOG_WARNING, "material track %d: has %d components\n",
1975  material_track->track_id, material_track->sequence->structural_components_count);
1976 
1977  for (j = 0; j < material_track->sequence->structural_components_count; j++) {
1978  component = mxf_resolve_sourceclip(mxf, &material_track->sequence->structural_components_refs[j]);
1979  if (!component)
1980  continue;
1981 
1982  source_package = mxf_resolve_source_package(mxf, component->source_package_uid);
1983  if (!source_package) {
1984  av_log(mxf->fc, AV_LOG_TRACE, "material track %d: no corresponding source package found\n", material_track->track_id);
1985  continue;
1986  }
1987  for (k = 0; k < source_package->tracks_count; k++) {
1988  if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
1989  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
1990  ret = AVERROR_INVALIDDATA;
1991  goto fail_and_free;
1992  }
1993  if (temp_track->track_id == component->source_track_id) {
1994  source_track = temp_track;
1995  break;
1996  }
1997  }
1998  if (!source_track) {
1999  av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
2000  break;
2001  }
2002  if(source_track && component)
2003  break;
2004  }
2005  if (!source_track || !component || !source_package) {
2006  if((ret = mxf_add_metadata_stream(mxf, material_track)))
2007  goto fail_and_free;
2008  continue;
2009  }
2010 
2011  if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
2012  av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
2013  ret = AVERROR_INVALIDDATA;
2014  goto fail_and_free;
2015  }
2016 
2017  /* 0001GL00.MXF.A1.mxf_opatom.mxf has the same SourcePackageID as 0001GL.MXF.V1.mxf_opatom.mxf
2018  * This would result in both files appearing to have two streams. Work around this by sanity checking DataDefinition */
2019  if (memcmp(material_track->sequence->data_definition_ul, source_track->sequence->data_definition_ul, 16)) {
2020  av_log(mxf->fc, AV_LOG_ERROR, "material track %d: DataDefinition mismatch\n", material_track->track_id);
2021  continue;
2022  }
2023 
2024  st = avformat_new_stream(mxf->fc, NULL);
2025  if (!st) {
2026  av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
2027  ret = AVERROR(ENOMEM);
2028  goto fail_and_free;
2029  }
2030  st->id = material_track->track_id;
2031  st->priv_data = source_track;
2032 
2033  source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
2034  descriptor = mxf_resolve_multidescriptor(mxf, source_package->descriptor, source_track->track_id);
2035 
2036  /* A SourceClip from a EssenceGroup may only be a single frame of essence data. The clips duration is then how many
2037  * frames its suppose to repeat for. Descriptor->duration, if present, contains the real duration of the essence data */
2038  if (descriptor && descriptor->duration != AV_NOPTS_VALUE)
2039  source_track->original_duration = st->duration = FFMIN(descriptor->duration, component->duration);
2040  else
2041  source_track->original_duration = st->duration = component->duration;
2042 
2043  if (st->duration == -1)
2044  st->duration = AV_NOPTS_VALUE;
2045  st->start_time = component->start_position;
2046  if (material_track->edit_rate.num <= 0 ||
2047  material_track->edit_rate.den <= 0) {
2048  av_log(mxf->fc, AV_LOG_WARNING,
2049  "Invalid edit rate (%d/%d) found on stream #%d, "
2050  "defaulting to 25/1\n",
2051  material_track->edit_rate.num,
2052  material_track->edit_rate.den, st->index);
2053  material_track->edit_rate = (AVRational){25, 1};
2054  }
2055  avpriv_set_pts_info(st, 64, material_track->edit_rate.den, material_track->edit_rate.num);
2056 
2057  /* ensure SourceTrack EditRate == MaterialTrack EditRate since only
2058  * the former is accessible via st->priv_data */
2059  source_track->edit_rate = material_track->edit_rate;
2060 
2061  PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
2063  st->codecpar->codec_type = codec_ul->id;
2064 
2065  if (!descriptor) {
2066  av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
2067  continue;
2068  }
2069  PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
2070  PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
2071  essence_container_ul = &descriptor->essence_container_ul;
2072  /* HACK: replacing the original key with mxf_encrypted_essence_container
2073  * is not allowed according to s429-6, try to find correct information anyway */
2074  if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
2075  av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
2076  for (k = 0; k < mxf->metadata_sets_count; k++) {
2077  MXFMetadataSet *metadata = mxf->metadata_sets[k];
2078  if (metadata->type == CryptoContext) {
2079  essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
2080  break;
2081  }
2082  }
2083  }
2084 
2085  /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
2086  codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
2087  st->codecpar->codec_id = (enum AVCodecID)codec_ul->id;
2088  if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
2089  codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->codec_ul);
2090  st->codecpar->codec_id = (enum AVCodecID)codec_ul->id;
2091  }
2092 
2093  av_log(mxf->fc, AV_LOG_VERBOSE, "%s: Universal Label: ",
2095  for (k = 0; k < 16; k++) {
2096  av_log(mxf->fc, AV_LOG_VERBOSE, "%.2x",
2097  descriptor->essence_codec_ul[k]);
2098  if (!(k+1 & 19) || k == 5)
2099  av_log(mxf->fc, AV_LOG_VERBOSE, ".");
2100  }
2101  av_log(mxf->fc, AV_LOG_VERBOSE, "\n");
2102 
2103  mxf_add_umid_metadata(&st->metadata, "file_package_umid", source_package);
2104  if (source_package->name && source_package->name[0])
2105  av_dict_set(&st->metadata, "file_package_name", source_package->name, 0);
2106  if (material_track->name && material_track->name[0])
2107  av_dict_set(&st->metadata, "track_name", material_track->name, 0);
2108 
2109  mxf_parse_physical_source_package(mxf, source_track, st);
2110 
2111  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2112  source_track->intra_only = mxf_is_intra_only(descriptor);
2113  container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul);
2114  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
2115  st->codecpar->codec_id = container_ul->id;
2116  st->codecpar->width = descriptor->width;
2117  st->codecpar->height = descriptor->height; /* Field height, not frame height */
2118  switch (descriptor->frame_layout) {
2119  case FullFrame:
2121  break;
2122  case OneField:
2123  /* Every other line is stored and needs to be duplicated. */
2124  av_log(mxf->fc, AV_LOG_INFO, "OneField frame layout isn't currently supported\n");
2125  break; /* The correct thing to do here is fall through, but by breaking we might be
2126  able to decode some streams at half the vertical resolution, rather than not al all.
2127  It's also for compatibility with the old behavior. */
2128  case MixedFields:
2129  break;
2130  case SegmentedFrame:
2132  case SeparateFields:
2133  av_log(mxf->fc, AV_LOG_DEBUG, "video_line_map: (%d, %d), field_dominance: %d\n",
2134  descriptor->video_line_map[0], descriptor->video_line_map[1],
2135  descriptor->field_dominance);
2136  if ((descriptor->video_line_map[0] > 0) && (descriptor->video_line_map[1] > 0)) {
2137  /* Detect coded field order from VideoLineMap:
2138  * (even, even) => bottom field coded first
2139  * (even, odd) => top field coded first
2140  * (odd, even) => top field coded first
2141  * (odd, odd) => bottom field coded first
2142  */
2143  if ((descriptor->video_line_map[0] + descriptor->video_line_map[1]) % 2) {
2144  switch (descriptor->field_dominance) {
2148  break;
2151  break;
2152  default:
2154  "Field dominance %d support",
2155  descriptor->field_dominance);
2156  }
2157  } else {
2158  switch (descriptor->field_dominance) {
2162  break;
2165  break;
2166  default:
2168  "Field dominance %d support",
2169  descriptor->field_dominance);
2170  }
2171  }
2172  }
2173  /* Turn field height into frame height. */
2174  st->codecpar->height *= 2;
2175  break;
2176  default:
2177  av_log(mxf->fc, AV_LOG_INFO, "Unknown frame layout type: %d\n", descriptor->frame_layout);
2178  }
2179  if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
2180  st->codecpar->format = descriptor->pix_fmt;
2181  if (st->codecpar->format == AV_PIX_FMT_NONE) {
2183  &descriptor->essence_codec_ul);
2184  st->codecpar->format = (enum AVPixelFormat)pix_fmt_ul->id;
2185  if (st->codecpar->format== AV_PIX_FMT_NONE) {
2187  &descriptor->essence_codec_ul)->id;
2188  if (!st->codecpar->codec_tag) {
2189  /* support files created before RP224v10 by defaulting to UYVY422
2190  if subsampling is 4:2:2 and component depth is 8-bit */
2191  if (descriptor->horiz_subsampling == 2 &&
2192  descriptor->vert_subsampling == 1 &&
2193  descriptor->component_depth == 8) {
2195  }
2196  }
2197  }
2198  }
2199  }
2201  if (material_track->sequence->origin) {
2202  av_dict_set_int(&st->metadata, "material_track_origin", material_track->sequence->origin, 0);
2203  }
2204  if (source_track->sequence->origin) {
2205  av_dict_set_int(&st->metadata, "source_track_origin", source_track->sequence->origin, 0);
2206  }
2207  if (descriptor->aspect_ratio.num && descriptor->aspect_ratio.den)
2208  st->display_aspect_ratio = descriptor->aspect_ratio;
2209  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2210  container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
2211  /* Only overwrite existing codec ID if it is unset or A-law, which is the default according to SMPTE RP 224. */
2212  if (st->codecpar->codec_id == AV_CODEC_ID_NONE || (st->codecpar->codec_id == AV_CODEC_ID_PCM_ALAW && (enum AVCodecID)container_ul->id != AV_CODEC_ID_NONE))
2213  st->codecpar->codec_id = (enum AVCodecID)container_ul->id;
2214  st->codecpar->channels = descriptor->channels;
2215  st->codecpar->bits_per_coded_sample = descriptor->bits_per_sample;
2216 
2217  if (descriptor->sample_rate.den > 0) {
2218  st->codecpar->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
2219  avpriv_set_pts_info(st, 64, descriptor->sample_rate.den, descriptor->sample_rate.num);
2220  } else {
2221  av_log(mxf->fc, AV_LOG_WARNING, "invalid sample rate (%d/%d) "
2222  "found for stream #%d, time base forced to 1/48000\n",
2223  descriptor->sample_rate.num, descriptor->sample_rate.den,
2224  st->index);
2225  avpriv_set_pts_info(st, 64, 1, 48000);
2226  }
2227 
2228  /* if duration is set, rescale it from EditRate to SampleRate */
2229  if (st->duration != AV_NOPTS_VALUE)
2230  st->duration = av_rescale_q(st->duration,
2231  av_inv_q(material_track->edit_rate),
2232  st->time_base);
2233 
2234  /* TODO: implement AV_CODEC_ID_RAWAUDIO */
2235  if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) {
2236  if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
2238  else if (descriptor->bits_per_sample == 32)
2240  } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE) {
2241  if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
2243  else if (descriptor->bits_per_sample == 32)
2245  } else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) {
2247  }
2248  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
2249  int codec_id = mxf_get_codec_ul(mxf_data_essence_container_uls,
2250  essence_container_ul)->id;
2251  if (codec_id >= 0 &&
2252  codec_id < FF_ARRAY_ELEMS(mxf_data_essence_descriptor)) {
2253  av_dict_set(&st->metadata, "data_type",
2254  mxf_data_essence_descriptor[codec_id], 0);
2255  }
2256  }
2257  if (descriptor->extradata) {
2258  if (!ff_alloc_extradata(st->codecpar, descriptor->extradata_size)) {
2259  memcpy(st->codecpar->extradata, descriptor->extradata, descriptor->extradata_size);
2260  }
2261  } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
2262  int coded_width = mxf_get_codec_ul(mxf_intra_only_picture_coded_width,
2263  &descriptor->essence_codec_ul)->id;
2264  if (coded_width)
2265  st->codecpar->width = coded_width;
2266  ret = ff_generate_avci_extradata(st);
2267  if (ret < 0)
2268  return ret;
2269  }
2270  if (st->codecpar->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
2271  /* TODO: decode timestamps */
2273  }
2274  }
2275 
2276  ret = 0;
2277 fail_and_free:
2278  return ret;
2279 }
2280 
2281 static int64_t mxf_timestamp_to_int64(uint64_t timestamp)
2282 {
2283  struct tm time = { 0 };
2284  time.tm_year = (timestamp >> 48) - 1900;
2285  time.tm_mon = (timestamp >> 40 & 0xFF) - 1;
2286  time.tm_mday = (timestamp >> 32 & 0xFF);
2287  time.tm_hour = (timestamp >> 24 & 0xFF);
2288  time.tm_min = (timestamp >> 16 & 0xFF);
2289  time.tm_sec = (timestamp >> 8 & 0xFF);
2290 
2291  /* msvcrt versions of strftime calls the invalid parameter handler
2292  * (aborting the process if one isn't set) if the parameters are out
2293  * of range. */
2294  time.tm_mon = av_clip(time.tm_mon, 0, 11);
2295  time.tm_mday = av_clip(time.tm_mday, 1, 31);
2296  time.tm_hour = av_clip(time.tm_hour, 0, 23);
2297  time.tm_min = av_clip(time.tm_min, 0, 59);
2298  time.tm_sec = av_clip(time.tm_sec, 0, 59);
2299 
2300  return (int64_t)av_timegm(&time) * 1000000;
2301 }
2302 
2303 #define SET_STR_METADATA(pb, name, str) do { \
2304  if ((ret = mxf_read_utf16be_string(pb, size, &str)) < 0) \
2305  return ret; \
2306  av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
2307 } while (0)
2308 
2309 #define SET_UID_METADATA(pb, name, var, str) do { \
2310  avio_read(pb, var, 16); \
2311  if ((ret = mxf_uid_to_str(var, &str)) < 0) \
2312  return ret; \
2313  av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
2314 } while (0)
2315 
2316 #define SET_TS_METADATA(pb, name, var, str) do { \
2317  var = avio_rb64(pb); \
2318  if ((ret = avpriv_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var)) < 0)) \
2319  return ret; \
2320 } while (0)
2321 
2322 static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset)
2323 {
2324  MXFContext *mxf = arg;
2325  AVFormatContext *s = mxf->fc;
2326  int ret;
2327  UID uid = { 0 };
2328  char *str = NULL;
2329  uint64_t ts;
2330  switch (tag) {
2331  case 0x3C01:
2332  SET_STR_METADATA(pb, "company_name", str);
2333  break;
2334  case 0x3C02:
2335  SET_STR_METADATA(pb, "product_name", str);
2336  break;
2337  case 0x3C04:
2338  SET_STR_METADATA(pb, "product_version", str);
2339  break;
2340  case 0x3C05:
2341  SET_UID_METADATA(pb, "product_uid", uid, str);
2342  break;
2343  case 0x3C06:
2344  SET_TS_METADATA(pb, "modification_date", ts, str);
2345  break;
2346  case 0x3C08:
2347  SET_STR_METADATA(pb, "application_platform", str);
2348  break;
2349  case 0x3C09:
2350  SET_UID_METADATA(pb, "generation_uid", uid, str);
2351  break;
2352  case 0x3C0A:
2353  SET_UID_METADATA(pb, "uid", uid, str);
2354  break;
2355  }
2356  return 0;
2357 }
2358 
2359 static int mxf_read_preface_metadata(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
2360 {
2361  MXFContext *mxf = arg;
2362  AVFormatContext *s = mxf->fc;
2363  int ret;
2364  char *str = NULL;
2365 
2366  if (tag >= 0x8000 && (IS_KLV_KEY(uid, mxf_avid_project_name))) {
2367  SET_STR_METADATA(pb, "project_name", str);
2368  }
2369  return 0;
2370 }
2371 
2373  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
2374  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
2375  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack },
2376  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack },
2377  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack },
2378  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack },
2379  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack },
2380  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack },
2381  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
2382  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
2383  { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
2384  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2f,0x00 }, mxf_read_preface_metadata },
2385  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x30,0x00 }, mxf_read_identification_metadata },
2386  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
2387  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_package, sizeof(MXFPackage), SourcePackage },
2388  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_package, sizeof(MXFPackage), MaterialPackage },
2389  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0f,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
2390  { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x05,0x00 }, mxf_read_essence_group, sizeof(MXFEssenceGroup), EssenceGroup},
2391  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
2392  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3f,0x00 }, mxf_read_tagged_value, sizeof(MXFTaggedValue), TaggedValue },
2393  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
2394  { { 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 */
2395  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
2396  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
2397  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
2398  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
2399  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2VideoDescriptor */
2400  { { 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 */
2401  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5e,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG2AudioDescriptor */
2402  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
2403  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
2404  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x14,0x00 }, mxf_read_timecode_component, sizeof(MXFTimecodeComponent), TimecodeComponent },
2405  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0c,0x00 }, mxf_read_pulldown_component, sizeof(MXFPulldownComponent), PulldownComponent },
2406  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
2407  { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
2408  { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
2409 };
2410 
2412 {
2413  switch (type){
2414  case MultipleDescriptor:
2415  case Descriptor:
2416  ((MXFDescriptor*)ctx)->pix_fmt = AV_PIX_FMT_NONE;
2417  ((MXFDescriptor*)ctx)->duration = AV_NOPTS_VALUE;
2418  break;
2419  default:
2420  break;
2421  }
2422  return 0;
2423 }
2424 
2425 static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
2426 {
2427  AVIOContext *pb = mxf->fc->pb;
2428  MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
2429  uint64_t klv_end = avio_tell(pb) + klv->length;
2430 
2431  if (!ctx)
2432  return AVERROR(ENOMEM);
2433  mxf_metadataset_init(ctx, type);
2434  while (avio_tell(pb) + 4 < klv_end && !avio_feof(pb)) {
2435  int ret;
2436  int tag = avio_rb16(pb);
2437  int size = avio_rb16(pb); /* KLV specified by 0x53 */
2438  int64_t next = avio_tell(pb);
2439  UID uid = {0};
2440  if (next < 0 || next > INT64_MAX - size)
2441  return next < 0 ? next : AVERROR_INVALIDDATA;
2442  next += size;
2443 
2444  av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x size %d\n", tag, size);
2445  if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
2446  av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
2447  continue;
2448  }
2449  if (tag > 0x7FFF) { /* dynamic tag */
2450  int i;
2451  for (i = 0; i < mxf->local_tags_count; i++) {
2452  int local_tag = AV_RB16(mxf->local_tags+i*18);
2453  if (local_tag == tag) {
2454  memcpy(uid, mxf->local_tags+i*18+2, 16);
2455  av_log(mxf->fc, AV_LOG_TRACE, "local tag %#04x\n", local_tag);
2456  PRINT_KEY(mxf->fc, "uid", uid);
2457  }
2458  }
2459  }
2460  if (ctx_size && tag == 0x3C0A) {
2461  avio_read(pb, ctx->uid, 16);
2462  } else if ((ret = read_child(ctx, pb, tag, size, uid, -1)) < 0) {
2463  mxf_free_metadataset(&ctx, !!ctx_size);
2464  return ret;
2465  }
2466 
2467  /* Accept the 64k local set limit being exceeded (Avid). Don't accept
2468  * it extending past the end of the KLV though (zzuf5.mxf). */
2469  if (avio_tell(pb) > klv_end) {
2470  if (ctx_size) {
2471  ctx->type = type;
2472  mxf_free_metadataset(&ctx, !!ctx_size);
2473  }
2474 
2475  av_log(mxf->fc, AV_LOG_ERROR,
2476  "local tag %#04x extends past end of local set @ %#"PRIx64"\n",
2477  tag, klv->offset);
2478  return AVERROR_INVALIDDATA;
2479  } else if (avio_tell(pb) <= next) /* only seek forward, else this can loop for a long time */
2480  avio_seek(pb, next, SEEK_SET);
2481  }
2482  if (ctx_size) ctx->type = type;
2483  return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
2484 }
2485 
2486 /**
2487  * Matches any partition pack key, in other words:
2488  * - HeaderPartition
2489  * - BodyPartition
2490  * - FooterPartition
2491  * @return non-zero if the key is a partition pack key, zero otherwise
2492  */
2494 {
2495  //NOTE: this is a little lax since it doesn't constraint key[14]
2496  return !memcmp(key, mxf_header_partition_pack_key, 13) &&
2497  key[13] >= 2 && key[13] <= 4;
2498 }
2499 
2500 /**
2501  * Parses a metadata KLV
2502  * @return <0 on error, 0 otherwise
2503  */
2505  int ctx_size, enum MXFMetadataSetType type)
2506 {
2507  AVFormatContext *s = mxf->fc;
2508  int res;
2509  if (klv.key[5] == 0x53) {
2510  res = mxf_read_local_tags(mxf, &klv, read, ctx_size, type);
2511  } else {
2512  uint64_t next = avio_tell(s->pb) + klv.length;
2513  res = read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);
2514 
2515  /* only seek forward, else this can loop for a long time */
2516  if (avio_tell(s->pb) > next) {
2517  av_log(s, AV_LOG_ERROR, "read past end of KLV @ %#"PRIx64"\n",
2518  klv.offset);
2519  return AVERROR_INVALIDDATA;
2520  }
2521 
2522  avio_seek(s->pb, next, SEEK_SET);
2523  }
2524  if (res < 0) {
2525  av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
2526  return res;
2527  }
2528  return 0;
2529 }
2530 
2531 /**
2532  * Seeks to the previous partition and parses it, if possible
2533  * @return <= 0 if we should stop parsing, > 0 if we should keep going
2534  */
2536 {
2537  AVIOContext *pb = mxf->fc->pb;
2538  KLVPacket klv;
2539  int64_t current_partition_ofs;
2540  int ret;
2541 
2542  if (!mxf->current_partition ||
2544  return 0; /* we've parsed all partitions */
2545 
2546  /* seek to previous partition */
2547  current_partition_ofs = mxf->current_partition->pack_ofs; //includes run-in
2548  avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET);
2549  mxf->current_partition = NULL;
2550 
2551  av_log(mxf->fc, AV_LOG_TRACE, "seeking to previous partition\n");
2552 
2553  /* Make sure this is actually a PartitionPack, and if so parse it.
2554  * See deadlock2.mxf
2555  */
2556  if ((ret = klv_read_packet(&klv, pb)) < 0) {
2557  av_log(mxf->fc, AV_LOG_ERROR, "failed to read PartitionPack KLV\n");
2558  return ret;
2559  }
2560 
2561  if (!mxf_is_partition_pack_key(klv.key)) {
2562  av_log(mxf->fc, AV_LOG_ERROR, "PreviousPartition @ %" PRIx64 " isn't a PartitionPack\n", klv.offset);
2563  return AVERROR_INVALIDDATA;
2564  }
2565 
2566  /* We can't just check ofs >= current_partition_ofs because PreviousPartition
2567  * can point to just before the current partition, causing klv_read_packet()
2568  * to sync back up to it. See deadlock3.mxf
2569  */
2570  if (klv.offset >= current_partition_ofs) {
2571  av_log(mxf->fc, AV_LOG_ERROR, "PreviousPartition for PartitionPack @ %"
2572  PRIx64 " indirectly points to itself\n", current_partition_ofs);
2573  return AVERROR_INVALIDDATA;
2574  }
2575 
2576  if ((ret = mxf_parse_klv(mxf, klv, mxf_read_partition_pack, 0, 0)) < 0)
2577  return ret;
2578 
2579  return 1;
2580 }
2581 
2582 /**
2583  * Called when essence is encountered
2584  * @return <= 0 if we should stop parsing, > 0 if we should keep going
2585  */
2587 {
2588  AVIOContext *pb = mxf->fc->pb;
2589  int64_t ret;
2590 
2591  if (mxf->parsing_backward) {
2592  return mxf_seek_to_previous_partition(mxf);
2593  } else {
2594  if (!mxf->footer_partition) {
2595  av_log(mxf->fc, AV_LOG_TRACE, "no FooterPartition\n");
2596  return 0;
2597  }
2598 
2599  av_log(mxf->fc, AV_LOG_TRACE, "seeking to FooterPartition\n");
2600 
2601  /* remember where we were so we don't end up seeking further back than this */
2602  mxf->last_forward_tell = avio_tell(pb);
2603 
2604  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2605  av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing FooterPartition\n");
2606  return -1;
2607  }
2608 
2609  /* seek to FooterPartition and parse backward */
2610  if ((ret = avio_seek(pb, mxf->run_in + mxf->footer_partition, SEEK_SET)) < 0) {
2611  av_log(mxf->fc, AV_LOG_ERROR,
2612  "failed to seek to FooterPartition @ 0x%" PRIx64
2613  " (%"PRId64") - partial file?\n",
2614  mxf->run_in + mxf->footer_partition, ret);
2615  return ret;
2616  }
2617 
2618  mxf->current_partition = NULL;
2619  mxf->parsing_backward = 1;
2620  }
2621 
2622  return 1;
2623 }
2624 
2625 /**
2626  * Called when the next partition or EOF is encountered
2627  * @return <= 0 if we should stop parsing, > 0 if we should keep going
2628  */
2630 {
2631  return mxf->parsing_backward ? mxf_seek_to_previous_partition(mxf) : 1;
2632 }
2633 
2634 /**
2635  * Figures out the proper offset and length of the essence container in each partition
2636  */
2638 {
2639  int x;
2640 
2641  /* everything is already correct */
2642  if (mxf->op == OPAtom)
2643  return;
2644 
2645  for (x = 0; x < mxf->partitions_count; x++) {
2646  MXFPartition *p = &mxf->partitions[x];
2647 
2648  if (!p->body_sid)
2649  continue; /* BodySID == 0 -> no essence */
2650 
2651  if (x >= mxf->partitions_count - 1)
2652  break; /* FooterPartition - can't compute length (and we don't need to) */
2653 
2654  /* essence container spans to the next partition */
2656 
2657  if (p->essence_length < 0) {
2658  /* next ThisPartition < essence_offset */
2659  p->essence_length = 0;
2660  av_log(mxf->fc, AV_LOG_ERROR,
2661  "partition %i: bad ThisPartition = %"PRIX64"\n",
2662  x+1, mxf->partitions[x+1].this_partition);
2663  }
2664  }
2665 }
2666 
2667 static int64_t round_to_kag(int64_t position, int kag_size)
2668 {
2669  /* TODO: account for run-in? the spec isn't clear whether KAG should account for it */
2670  /* NOTE: kag_size may be any integer between 1 - 2^10 */
2671  int64_t ret = (position / kag_size) * kag_size;
2672  return ret == position ? ret : ret + kag_size;
2673 }
2674 
2675 static int is_pcm(enum AVCodecID codec_id)
2676 {
2677  /* we only care about "normal" PCM codecs until we get samples */
2678  return codec_id >= AV_CODEC_ID_PCM_S16LE && codec_id < AV_CODEC_ID_PCM_S24DAUD;
2679 }
2680 
2682 {
2683  int i;
2684 
2685  if (mxf->op != OPAtom)
2686  return NULL;
2687 
2688  for (i = 0; i < mxf->fc->nb_streams; i++) {
2689  if (mxf->fc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2690  continue;
2691  return mxf->fc->streams[i];
2692  }
2693  return NULL;
2694 }
2695 
2696 /**
2697  * Deal with the case where for some audio atoms EditUnitByteCount is
2698  * very small (2, 4..). In those cases we should read more than one
2699  * sample per call to mxf_read_packet().
2700  */
2702 {
2703  MXFContext *mxf = s->priv_data;
2704 
2705  /* assuming non-OPAtom == frame wrapped
2706  * no sane writer would wrap 2 byte PCM packets with 20 byte headers.. */
2707  AVStream *st = mxf_get_opatom_stream(mxf);
2708  if (!st)
2709  return;
2710 
2711  /* expect PCM with exactly one index table segment and a small (< 32) EUBC */
2712  if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
2713  !is_pcm(st->codecpar->codec_id) ||
2714  mxf->nb_index_tables != 1 ||
2715  mxf->index_tables[0].nb_segments != 1 ||
2716  mxf->index_tables[0].segments[0]->edit_unit_byte_count >= 32)
2717  return;
2718 
2719  /* arbitrarily default to 48 kHz PAL audio frame size */
2720  /* TODO: We could compute this from the ratio between the audio
2721  * and video edit rates for 48 kHz NTSC we could use the
2722  * 1802-1802-1802-1802-1801 pattern. */
2723  mxf->edit_units_per_packet = 1920;
2724 }
2725 
2726 /**
2727  * Deal with the case where OPAtom files does not have any IndexTableSegments.
2728  */
2730 {
2731  AVFormatContext *s = mxf->fc;
2732  AVStream *st = NULL;
2734  MXFPartition *p = NULL;
2735  int essence_partition_count = 0;
2736  int i, ret;
2737 
2738  st = mxf_get_opatom_stream(mxf);
2739  if (!st)
2740  return 0;
2741 
2742  /* TODO: support raw video without an index if they exist */
2744  return 0;
2745 
2746  /* check if file already has a IndexTableSegment */
2747  for (i = 0; i < mxf->metadata_sets_count; i++) {
2748  if (mxf->metadata_sets[i]->type == IndexTableSegment)
2749  return 0;
2750  }
2751 
2752  /* find the essence partition */
2753  for (i = 0; i < mxf->partitions_count; i++) {
2754  /* BodySID == 0 -> no essence */
2755  if (!mxf->partitions[i].body_sid)
2756  continue;
2757 
2758  p = &mxf->partitions[i];
2759  essence_partition_count++;
2760  }
2761 
2762  /* only handle files with a single essence partition */
2763  if (essence_partition_count != 1)
2764  return 0;
2765 
2766  if (!(segment = av_mallocz(sizeof(*segment))))
2767  return AVERROR(ENOMEM);
2768 
2769  if ((ret = mxf_add_metadata_set(mxf, segment))) {
2770  mxf_free_metadataset((MXFMetadataSet**)&segment, 1);
2771  return ret;
2772  }
2773 
2774  segment->type = IndexTableSegment;
2775  /* stream will be treated as small EditUnitByteCount */
2777  segment->index_start_position = 0;
2778  segment->index_duration = s->streams[0]->duration;
2779  segment->index_sid = p->index_sid;
2780  segment->body_sid = p->body_sid;
2781  return 0;
2782 }
2783 
2785 {
2786  MXFContext *mxf = s->priv_data;
2787  uint32_t length;
2788  int64_t file_size, max_rip_length, min_rip_length;
2789  KLVPacket klv;
2790 
2791  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
2792  return;
2793 
2794  file_size = avio_size(s->pb);
2795 
2796  /* S377m says to check the RIP length for "silly" values, without defining "silly".
2797  * The limit below assumes a file with nothing but partition packs and a RIP.
2798  * Before changing this, consider that a muxer may place each sample in its own partition.
2799  *
2800  * 105 is the size of the smallest possible PartitionPack
2801  * 12 is the size of each RIP entry
2802  * 28 is the size of the RIP header and footer, assuming an 8-byte BER
2803  */
2804  max_rip_length = ((file_size - mxf->run_in) / 105) * 12 + 28;
2805  max_rip_length = FFMIN(max_rip_length, INT_MAX); //2 GiB and up is also silly
2806 
2807  /* We're only interested in RIPs with at least two entries.. */
2808  min_rip_length = 16+1+24+4;
2809 
2810  /* See S377m section 11 */
2811  avio_seek(s->pb, file_size - 4, SEEK_SET);
2812  length = avio_rb32(s->pb);
2813 
2814  if (length < min_rip_length || length > max_rip_length)
2815  goto end;
2816  avio_seek(s->pb, file_size - length, SEEK_SET);
2817  if (klv_read_packet(&klv, s->pb) < 0 ||
2819  klv.length != length - 20)
2820  goto end;
2821 
2822  avio_skip(s->pb, klv.length - 12);
2823  mxf->footer_partition = avio_rb64(s->pb);
2824 
2825  /* sanity check */
2826  if (mxf->run_in + mxf->footer_partition >= file_size) {
2827  av_log(s, AV_LOG_WARNING, "bad FooterPartition in RIP - ignoring\n");
2828  mxf->footer_partition = 0;
2829  }
2830 
2831 end:
2832  avio_seek(s->pb, mxf->run_in, SEEK_SET);
2833 }
2834 
2836 {
2837  MXFContext *mxf = s->priv_data;
2838  KLVPacket klv;
2839  int64_t essence_offset = 0;
2840  int ret;
2841 
2842  mxf->last_forward_tell = INT64_MAX;
2843  mxf->edit_units_per_packet = 1;
2844 
2846  av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
2847  return AVERROR_INVALIDDATA;
2848  }
2849  avio_seek(s->pb, -14, SEEK_CUR);
2850  mxf->fc = s;
2851  mxf->run_in = avio_tell(s->pb);
2852 
2854 
2855  while (!avio_feof(s->pb)) {
2856  const MXFMetadataReadTableEntry *metadata;
2857 
2858  if (klv_read_packet(&klv, s->pb) < 0) {
2859  /* EOF - seek to previous partition or stop */
2860  if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
2861  break;
2862  else
2863  continue;
2864  }
2865 
2866  PRINT_KEY(s, "read header", klv.key);
2867  av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
2872 
2873  if (!mxf->current_partition) {
2874  av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to first PartitionPack\n");
2875  return AVERROR_INVALIDDATA;
2876  }
2877 
2878  if (!mxf->current_partition->essence_offset) {
2879  /* for OP1a we compute essence_offset
2880  * for OPAtom we point essence_offset after the KL (usually op1a_essence_offset + 20 or 25)
2881  * TODO: for OP1a we could eliminate this entire if statement, always stopping parsing at op1a_essence_offset
2882  * for OPAtom we still need the actual essence_offset though (the KL's length can vary)
2883  */
2884  int64_t op1a_essence_offset =
2889 
2890  if (mxf->op == OPAtom) {
2891  /* point essence_offset to the actual data
2892  * OPAtom has all the essence in one big KLV
2893  */
2896  } else {
2897  /* NOTE: op1a_essence_offset may be less than to klv.offset (C0023S01.mxf) */
2898  mxf->current_partition->essence_offset = op1a_essence_offset;
2899  }
2900  }
2901 
2902  if (!essence_offset)
2903  essence_offset = klv.offset;
2904 
2905  /* seek to footer, previous partition or stop */
2906  if (mxf_parse_handle_essence(mxf) <= 0)
2907  break;
2908  continue;
2909  } else if (mxf_is_partition_pack_key(klv.key) && mxf->current_partition) {
2910  /* next partition pack - keep going, seek to previous partition or stop */
2911  if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
2912  break;
2913  else if (mxf->parsing_backward)
2914  continue;
2915  /* we're still parsing forward. proceed to parsing this partition pack */
2916  }
2917 
2918  for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
2919  if (IS_KLV_KEY(klv.key, metadata->key)) {
2920  if ((ret = mxf_parse_klv(mxf, klv, metadata->read, metadata->ctx_size, metadata->type)) < 0)
2921  goto fail;
2922  break;
2923  }
2924  }
2925  if (!metadata->read) {
2926  av_log(s, AV_LOG_VERBOSE, "Dark key " PRIxUID "\n",
2927  UID_ARG(klv.key));
2928  avio_skip(s->pb, klv.length);
2929  }
2930  }
2931  /* FIXME avoid seek */
2932  if (!essence_offset) {
2933  av_log(s, AV_LOG_ERROR, "no essence\n");
2934  ret = AVERROR_INVALIDDATA;
2935  goto fail;
2936  }
2937  avio_seek(s->pb, essence_offset, SEEK_SET);
2938 
2940 
2941  /* we need to do this before computing the index tables
2942  * to be able to fill in zero IndexDurations with st->duration */
2943  if ((ret = mxf_parse_structural_metadata(mxf)) < 0)
2944  goto fail;
2945 
2947  if ((ret = mxf_compute_index_tables(mxf)) < 0)
2948  goto fail;
2949 
2950  if (mxf->nb_index_tables > 1) {
2951  /* TODO: look up which IndexSID to use via EssenceContainerData */
2952  av_log(mxf->fc, AV_LOG_INFO, "got %i index tables - only the first one (IndexSID %i) will be used\n",
2953  mxf->nb_index_tables, mxf->index_tables[0].index_sid);
2954  } else if (mxf->nb_index_tables == 0 && mxf->op == OPAtom) {
2955  av_log(mxf->fc, AV_LOG_ERROR, "cannot demux OPAtom without an index\n");
2956  ret = AVERROR_INVALIDDATA;
2957  goto fail;
2958  }
2959 
2961 
2962  return 0;
2963 fail:
2964  mxf_read_close(s);
2965 
2966  return ret;
2967 }
2968 
2969 /**
2970  * Sets mxf->current_edit_unit based on what offset we're currently at.
2971  * @return next_ofs if OK, <0 on error
2972  */
2973 static int64_t mxf_set_current_edit_unit(MXFContext *mxf, int64_t current_offset)
2974 {
2975  int64_t last_ofs = -1, next_ofs = -1;
2976  MXFIndexTable *t = &mxf->index_tables[0];
2977 
2978  /* this is called from the OP1a demuxing logic, which means there
2979  * may be no index tables */
2980  if (mxf->nb_index_tables <= 0)
2981  return -1;
2982 
2983  /* find mxf->current_edit_unit so that the next edit unit starts ahead of current_offset */
2984  while (mxf->current_edit_unit >= 0) {
2985  if (mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit + 1, NULL, &next_ofs, 0) < 0)
2986  return -1;
2987 
2988  if (next_ofs <= last_ofs) {
2989  /* large next_ofs didn't change or current_edit_unit wrapped
2990  * around this fixes the infinite loop on zzuf3.mxf */
2991  av_log(mxf->fc, AV_LOG_ERROR,
2992  "next_ofs didn't change. not deriving packet timestamps\n");
2993  return -1;
2994  }
2995 
2996  if (next_ofs > current_offset)
2997  break;
2998 
2999  last_ofs = next_ofs;
3000  mxf->current_edit_unit++;
3001  }
3002 
3003  /* not checking mxf->current_edit_unit >= t->nb_ptses here since CBR files may lack IndexEntryArrays */
3004  if (mxf->current_edit_unit < 0)
3005  return -1;
3006 
3007  return next_ofs;
3008 }
3009 
3010 static int mxf_compute_sample_count(MXFContext *mxf, int stream_index,
3011  uint64_t *sample_count)
3012 {
3013  int i, total = 0, size = 0;
3014  AVStream *st = mxf->fc->streams[stream_index];
3015  MXFTrack *track = st->priv_data;
3016  AVRational time_base = av_inv_q(track->edit_rate);
3018  const MXFSamplesPerFrame *spf = NULL;
3019 
3020  if ((sample_rate.num / sample_rate.den) == 48000)
3021  spf = ff_mxf_get_samples_per_frame(mxf->fc, time_base);
3022  if (!spf) {
3023  int remainder = (sample_rate.num * time_base.num) %
3024  (time_base.den * sample_rate.den);
3025  *sample_count = av_q2d(av_mul_q((AVRational){mxf->current_edit_unit, 1},
3026  av_mul_q(sample_rate, time_base)));
3027  if (remainder)
3028  av_log(mxf->fc, AV_LOG_WARNING,
3029  "seeking detected on stream #%d with time base (%d/%d) and "
3030  "sample rate (%d/%d), audio pts won't be accurate.\n",
3031  stream_index, time_base.num, time_base.den,
3032  sample_rate.num, sample_rate.den);
3033  return 0;
3034  }
3035 
3036  while (spf->samples_per_frame[size]) {
3037  total += spf->samples_per_frame[size];
3038  size++;
3039  }
3040 
3041  av_assert2(size);
3042 
3043  *sample_count = (mxf->current_edit_unit / size) * (uint64_t)total;
3044  for (i = 0; i < mxf->current_edit_unit % size; i++) {
3045  *sample_count += spf->samples_per_frame[i];
3046  }
3047 
3048  return 0;
3049 }
3050 
3052  AVPacket *pkt)
3053 {
3054  MXFTrack *track = mxf->fc->streams[pkt->stream_index]->priv_data;
3055  int64_t bits_per_sample = par->bits_per_coded_sample;
3056 
3057  if (!bits_per_sample)
3058  bits_per_sample = av_get_bits_per_sample(par->codec_id);
3059 
3060  pkt->pts = track->sample_count;
3061 
3062  if ( par->channels <= 0
3063  || bits_per_sample <= 0
3064  || par->channels * (int64_t)bits_per_sample < 8)
3065  return AVERROR(EINVAL);
3066  track->sample_count += pkt->size / (par->channels * (int64_t)bits_per_sample / 8);
3067  return 0;
3068 }
3069 
3070 static int mxf_set_pts(MXFContext *mxf, AVStream *st, AVPacket *pkt, int64_t next_ofs)
3071 {
3072  AVCodecParameters *par = st->codecpar;
3073  MXFTrack *track = st->priv_data;
3074 
3075  if (par->codec_type == AVMEDIA_TYPE_VIDEO && next_ofs >= 0) {
3076  /* mxf->current_edit_unit good - see if we have an
3077  * index table to derive timestamps from */
3078  MXFIndexTable *t = &mxf->index_tables[0];
3079 
3080  if (mxf->nb_index_tables >= 1 && mxf->current_edit_unit < t->nb_ptses) {
3081  pkt->dts = mxf->current_edit_unit + t->first_dts;
3082  pkt->pts = t->ptses[mxf->current_edit_unit];
3083  } else if (track && track->intra_only) {
3084  /* intra-only -> PTS = EditUnit.
3085  * let utils.c figure out DTS since it can be < PTS if low_delay = 0 (Sony IMX30) */
3086  pkt->pts = mxf->current_edit_unit;
3087  }
3088  } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
3089  int ret = mxf_set_audio_pts(mxf, par, pkt);
3090  if (ret < 0)
3091  return ret;
3092  }
3093  return 0;
3094 }
3095 
3097 {
3098  KLVPacket klv;
3099  MXFContext *mxf = s->priv_data;
3100  int ret;
3101 
3102  while ((ret = klv_read_packet(&klv, s->pb)) == 0) {
3103  PRINT_KEY(s, "read packet", klv.key);
3104  av_log(s, AV_LOG_TRACE, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
3106  ret = mxf_decrypt_triplet(s, pkt, &klv);
3107  if (ret < 0) {
3108  av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
3109  return ret;
3110  }
3111  return 0;
3112  }
3116  int index = mxf_get_stream_index(s, &klv);
3117  int64_t next_ofs, next_klv;
3118  AVStream *st;
3119 
3120  if (index < 0) {
3121  av_log(s, AV_LOG_ERROR,
3122  "error getting stream index %"PRIu32"\n",
3123  AV_RB32(klv.key + 12));
3124  goto skip;
3125  }
3126 
3127  st = s->streams[index];
3128 
3129  if (s->streams[index]->discard == AVDISCARD_ALL)
3130  goto skip;
3131 
3132  next_klv = avio_tell(s->pb) + klv.length;
3133  next_ofs = mxf_set_current_edit_unit(mxf, klv.offset);
3134 
3135  if (next_ofs >= 0 && next_klv > next_ofs) {
3136  /* if this check is hit then it's possible OPAtom was treated as OP1a
3137  * truncate the packet since it's probably very large (>2 GiB is common) */
3139  "OPAtom misinterpreted as OP1a? "
3140  "KLV for edit unit %i extending into "
3141  "next edit unit",
3142  mxf->current_edit_unit);
3143  klv.length = next_ofs - avio_tell(s->pb);
3144  }
3145 
3146  /* check for 8 channels AES3 element */
3147  if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
3148  ret = mxf_get_d10_aes3_packet(s->pb, s->streams[index],
3149  pkt, klv.length);
3150  if (ret < 0) {
3151  av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
3152  return ret;
3153  }
3154  } else {
3155  ret = av_get_packet(s->pb, pkt, klv.length);
3156  if (ret < 0)
3157  return ret;
3158  }
3159  pkt->stream_index = index;
3160  pkt->pos = klv.offset;
3161 
3162  ret = mxf_set_pts(mxf, st, pkt, next_ofs);
3163  if (ret < 0)
3164  return ret;
3165 
3166  /* seek for truncated packets */
3167  avio_seek(s->pb, next_klv, SEEK_SET);
3168 
3169  return 0;
3170  } else
3171  skip:
3172  avio_skip(s->pb, klv.length);
3173  }
3174  return avio_feof(s->pb) ? AVERROR_EOF : ret;
3175 }
3176 
3178 {
3179  MXFContext *mxf = s->priv_data;
3180  int ret, size;
3181  int64_t ret64, pos, next_pos;
3182  AVStream *st;
3183  MXFIndexTable *t;
3184  int edit_units;
3185 
3186  if (mxf->op != OPAtom)
3187  return mxf_read_packet_old(s, pkt);
3188 
3189  // If we have no streams then we basically are at EOF
3190  st = mxf_get_opatom_stream(mxf);
3191  if (!st)
3192  return AVERROR_EOF;
3193 
3194  /* OPAtom - clip wrapped demuxing */
3195  /* NOTE: mxf_read_header() makes sure nb_index_tables > 0 for OPAtom */
3196  t = &mxf->index_tables[0];
3197 
3198  if (mxf->current_edit_unit >= st->duration)
3199  return AVERROR_EOF;
3200 
3201  edit_units = FFMIN(mxf->edit_units_per_packet, st->duration - mxf->current_edit_unit);
3202 
3203  if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit, NULL, &pos, 1)) < 0)
3204  return ret;
3205 
3206  /* compute size by finding the next edit unit or the end of the essence container
3207  * not pretty, but it works */
3208  if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit + edit_units, NULL, &next_pos, 0)) < 0 &&
3209  (next_pos = mxf_essence_container_end(mxf, t->body_sid)) <= 0) {
3210  av_log(s, AV_LOG_ERROR, "unable to compute the size of the last packet\n");
3211  return AVERROR_INVALIDDATA;
3212  }
3213 
3214  if ((size = next_pos - pos) <= 0) {
3215  av_log(s, AV_LOG_ERROR, "bad size: %i\n", size);
3216  return AVERROR_INVALIDDATA;
3217  }
3218 
3219  if ((ret64 = avio_seek(s->pb, pos, SEEK_SET)) < 0)
3220  return ret64;
3221 
3222  if ((size = av_get_packet(s->pb, pkt, size)) < 0)
3223  return size;
3224 
3225  pkt->stream_index = st->index;
3226 
3227  ret = mxf_set_pts(mxf, st, pkt, next_pos);
3228  if (ret < 0)
3229  return ret;
3230 
3231  mxf->current_edit_unit += edit_units;
3232 
3233  return 0;
3234 }
3235 
3237 {
3238  MXFContext *mxf = s->priv_data;
3239  int i;
3240 
3241  av_freep(&mxf->packages_refs);
3242 
3243  for (i = 0; i < s->nb_streams; i++)
3244  s->streams[i]->priv_data = NULL;
3245 
3246  for (i = 0; i < mxf->metadata_sets_count; i++) {
3247  mxf_free_metadataset(mxf->metadata_sets + i, 1);
3248  }
3249  mxf->metadata_sets_count = 0;
3250  av_freep(&mxf->partitions);
3251  av_freep(&mxf->metadata_sets);
3252  av_freep(&mxf->aesc);
3253  av_freep(&mxf->local_tags);
3254 
3255  if (mxf->index_tables) {
3256  for (i = 0; i < mxf->nb_index_tables; i++) {
3257  av_freep(&mxf->index_tables[i].segments);
3258  av_freep(&mxf->index_tables[i].ptses);
3259  av_freep(&mxf->index_tables[i].fake_index);
3260  av_freep(&mxf->index_tables[i].offsets);
3261  }
3262  }
3263  av_freep(&mxf->index_tables);
3264 
3265  return 0;
3266 }
3267 
3268 static int mxf_probe(AVProbeData *p) {
3269  const uint8_t *bufp = p->buf;
3270  const uint8_t *end = p->buf + p->buf_size;
3271 
3272  if (p->buf_size < sizeof(mxf_header_partition_pack_key))
3273  return 0;
3274 
3275  /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
3276  end -= sizeof(mxf_header_partition_pack_key);
3277 
3278  for (; bufp < end;) {
3279  if (!((bufp[13] - 1) & 0xF2)){
3280  if (AV_RN32(bufp ) == AV_RN32(mxf_header_partition_pack_key ) &&
3281  AV_RN32(bufp+ 4) == AV_RN32(mxf_header_partition_pack_key+ 4) &&
3282  AV_RN32(bufp+ 8) == AV_RN32(mxf_header_partition_pack_key+ 8) &&
3284  return AVPROBE_SCORE_MAX;
3285  bufp ++;
3286  } else
3287  bufp += 10;
3288  }
3289 
3290  return 0;
3291 }
3292 
3293 /* rudimentary byte seek */
3294 /* XXX: use MXF Index */
3295 static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
3296 {
3297  AVStream *st = s->streams[stream_index];
3298  int64_t seconds;
3299  MXFContext* mxf = s->priv_data;
3300  int64_t seekpos;
3301  int i, ret;
3302  MXFIndexTable *t;
3303  MXFTrack *source_track = st->priv_data;
3304 
3306  return 0;
3307 
3308  /* if audio then truncate sample_time to EditRate */
3310  sample_time = av_rescale_q(sample_time, st->time_base,
3311  av_inv_q(source_track->edit_rate));
3312 
3313  if (mxf->nb_index_tables <= 0) {
3314  if (!s->bit_rate)
3315  return AVERROR_INVALIDDATA;
3316  if (sample_time < 0)
3317  sample_time = 0;
3318  seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
3319 
3320  seekpos = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
3321  if (seekpos < 0)
3322  return seekpos;
3323 
3324  ff_update_cur_dts(s, st, sample_time);
3325  mxf->current_edit_unit = sample_time;
3326  } else {
3327  t = &mxf->index_tables[0];
3328 
3329  /* clamp above zero, else ff_index_search_timestamp() returns negative
3330  * this also means we allow seeking before the start */
3331  sample_time = FFMAX(sample_time, 0);
3332 
3333  if (t->fake_index) {
3334  /* The first frames may not be keyframes in presentation order, so
3335  * we have to advance the target to be able to find the first
3336  * keyframe backwards... */
3337  if (!(flags & AVSEEK_FLAG_ANY) &&
3338  (flags & AVSEEK_FLAG_BACKWARD) &&
3339  t->ptses[0] != AV_NOPTS_VALUE &&
3340  sample_time < t->ptses[0] &&
3341  (t->fake_index[t->ptses[0]].flags & AVINDEX_KEYFRAME))
3342  sample_time = t->ptses[0];
3343 
3344  /* behave as if we have a proper index */
3345  if ((sample_time = ff_index_search_timestamp(t->fake_index, t->nb_ptses, sample_time, flags)) < 0)
3346  return sample_time;
3347  /* get the stored order index from the display order index */
3348  sample_time += t->offsets[sample_time];
3349  } else {
3350  /* no IndexEntryArray (one or more CBR segments)
3351  * make sure we don't seek past the end */
3352  sample_time = FFMIN(sample_time, source_track->original_duration - 1);
3353  }
3354 
3355  if ((ret = mxf_edit_unit_absolute_offset(mxf, t, sample_time, &sample_time, &seekpos, 1)) < 0)
3356  return ret;
3357 
3358  ff_update_cur_dts(s, st, sample_time);
3359  mxf->current_edit_unit = sample_time;
3360  avio_seek(s->pb, seekpos, SEEK_SET);
3361  }
3362 
3363  // Update all tracks sample count
3364  for (i = 0; i < s->nb_streams; i++) {
3365  AVStream *cur_st = s->streams[i];
3366  MXFTrack *cur_track = cur_st->priv_data;
3367  uint64_t current_sample_count = 0;
3368  if (cur_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
3369  ret = mxf_compute_sample_count(mxf, i, &current_sample_count);
3370  if (ret < 0)
3371  return ret;
3372 
3373  cur_track->sample_count = current_sample_count;
3374  }
3375  }
3376  return 0;
3377 }
3378 
3380  .name = "mxf",
3381  .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
3382  .flags = AVFMT_SEEK_TO_PTS,
3383  .priv_data_size = sizeof(MXFContext),
3384  .read_probe = mxf_probe,
3389 };
static const uint8_t mxf_crypto_source_container_ul[]
Definition: mxfdec.c:295
const char * name
Definition: avisynth_c.h:775
static void mxf_compute_essence_containers(MXFContext *mxf)
Figures out the proper offset and length of the essence container in each partition.
Definition: mxfdec.c:2637
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value.
Definition: parseutils.c:568
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:86
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2423
#define NULL
Definition: coverity.c:32
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:812
static int mxf_parse_structural_metadata(MXFContext *mxf)
Definition: mxfdec.c:1904
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:4233
MXFMetadataSetType
Definition: mxf.h:30
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static enum AVPixelFormat pix_fmt
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:334
uint8_t origin
Definition: mxfdec.c:120
unsigned int component_depth
Definition: mxfdec.c:186
int size
KLVPacket current_klv_data
Definition: mxfdec.c:257
static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:929
Definition: mxf.h:39
AVTimecode tc
Definition: mxfdec.c:129
UID * comment_refs
Definition: mxfdec.c:222
static const MXFMetadataReadTableEntry mxf_metadata_read_table[]
Definition: mxfdec.c:2372
int current_edit_unit
Definition: mxfdec.c:264
int structural_components_count
Definition: mxfdec.c:142
#define PRINT_KEY(pc, s, x)
Definition: mxf.h:115
int index_sid
Definition: mxfdec.c:233
static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
Definition: mxfdec.c:375
UID * structural_components_refs
Definition: mxfdec.c:117
MXFOP
Definition: mxfdec.c:65
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int mxf_seek_to_previous_partition(MXFContext *mxf)
Seeks to the previous partition and parses it, if possible.
Definition: mxfdec.c:2535
int edit_unit_byte_count
Definition: mxfdec.c:200
enum MXFMetadataSetType type
Definition: mxfdec.c:104
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1699
static int mxf_probe(AVProbeData *p)
Definition: mxfdec.c:3268
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
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:4756
int64_t * ptses
Definition: mxfdec.c:237
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2425
UID * structural_components_refs
Definition: mxfdec.c:141
UID sequence_ref
Definition: mxfdec.c:157
static void mxf_read_random_index_pack(AVFormatContext *s)
Definition: mxfdec.c:2784
#define avpriv_request_sample(...)
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:890
#define SET_TS_METADATA(pb, name, var, str)
Definition: mxfdec.c:2316
int size
Definition: avcodec.h:1680
Definition: mxf.h:34
uint64_t footer_partition
Definition: mxfdec.c:256
const char * b
Definition: vf_curves.c:113
int closed
Definition: mxfdec.c:80
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
enum MXFMetadataSetType type
Definition: mxfdec.c:282
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
static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
Definition: mxfdec.c:3295
static int mxf_uid_to_str(UID uid, char **str)
Definition: mxfdec.c:1607
int64_t bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1451
static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
Definition: mxfdec.c:401
MXFSequence * sequence
Definition: mxfdec.c:156
#define tc
Definition: regdef.h:69
int linked_track_id
Definition: mxfdec.c:191
UID key
Definition: mxf.h:62
static int mxf_umid_to_str(UID ul, UID uid, char **str)
Definition: mxfdec.c:1625
int64_t offset
Definition: mxf.h:63
void * priv_data
Definition: avformat.h:904
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
char * name
Definition: mxfdec.c:149
discard all
Definition: avcodec.h:830
static AVPacket pkt
#define MXF_FIELD_DOMINANCE_DEFAULT
Definition: mxfdec.c:179
Definition: mxfdec.c:72
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:2003
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:774
static int mxf_is_intra_only(MXFDescriptor *descriptor)
Definition: mxfdec.c:1599
#define sample
static int mxf_read_header(AVFormatContext *s)
Definition: mxfdec.c:2835
UID source_container_ul
Definition: mxfdec.c:99
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.
char * name
Definition: mxfdec.c:221
int id
Definition: mxf.h:70
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
static int mxf_parse_klv(MXFContext *mxf, KLVPacket klv, MXFMetadataReadFunc *read, int ctx_size, enum MXFMetadataSetType type)
Parses a metadata KLV.
Definition: mxfdec.c:2504
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:230
enum MXFMetadataSetType type
Definition: mxfdec.c:169
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that&#39;s been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:73
Definition: mxfdec.c:71
static int mxf_parse_handle_partition_or_eof(MXFContext *mxf)
Called when the next partition or EOF is encountered.
Definition: mxfdec.c:2629
static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
Definition: mxfdec.c:2425
static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxfdec.c:3177
Format I/O context.
Definition: avformat.h:1349
Definition: mxfdec.c:69
static int is_pcm(enum AVCodecID codec_id)
Definition: mxfdec.c:2675
uint8_t UID[16]
Definition: mxf.h:28
int frame_layout
Definition: mxfdec.c:177
UID uid
Definition: mxfenc.c:1895
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:1908
static int mxf_handle_missing_index_segment(MXFContext *mxf)
Deal with the case where OPAtom files does not have any IndexTableSegments.
Definition: mxfdec.c:2729
static const uint8_t mxf_indirect_value_utf16le[]
Definition: mxfdec.c:302
Definition: mxfdec.c:62
UID * packages_refs
Definition: mxfdec.c:248
#define MXF_FIELD_DOMINANCE_FF
Definition: mxfdec.c:180
uint8_t
UID * tracks_refs
Definition: mxfdec.c:217
#define av_malloc(s)
int64_t first_dts
Definition: mxfdec.c:236
const MXFSamplesPerFrame * ff_mxf_get_samples_per_frame(AVFormatContext *s, AVRational time_base)
Definition: mxf.c:152
Opaque data information usually continuous.
Definition: avutil.h:203
static const uint8_t mxf_avid_essence_element_key[]
Definition: mxfdec.c:290
int bits_per_sample
Definition: mxfdec.c:184
int width
Video only.
Definition: avcodec.h:4218
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
Definition: mxfdec.c:272
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:789
timecode is drop frame
Definition: timecode.h:36
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVRational index_edit_rate
Definition: mxfdec.c:203
enum AVStreamParseType need_parsing
Definition: avformat.h:1081
int id
Format-specific stream ID.
Definition: avformat.h:896
static int mxf_read_close(AVFormatContext *s)
Definition: mxfdec.c:3236
Definition: mxf.h:61
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4383
static int mxf_add_umid_metadata(AVDictionary **pm, const char *key, MXFPackage *package)
Definition: mxfdec.c:1646
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
#define UID_ARG(x)
Definition: mxf.h:92
static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t offset, int64_t *offset_out)
Computes the absolute file offset of the given essence container offset.
Definition: mxfdec.c:1283
int complete
Definition: mxfdec.c:81
static const uint8_t mxf_klv_key[]
Definition: mxfdec.c:293
int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:276
static int mxf_compute_index_tables(MXFContext *mxf)
Sorts and collects index table segments into index tables.
Definition: mxfdec.c:1499
MXFIndexTableSegment ** segments
Definition: mxfdec.c:239
const MXFCodecUL ff_mxf_codec_uls[]
Definition: mxf.c:36
uint8_t * data
Definition: avcodec.h:1679
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
static const uint8_t mxf_sony_mpeg4_extradata[]
Definition: mxfdec.c:299
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1414
Definition: mxfdec.c:271
#define AVERROR_EOF
End of file.
Definition: error.h:55
static const uint8_t mxf_avid_project_name[]
Definition: mxfdec.c:300
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
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:292
Definition: mxfdec.c:67
Definition: ismindex.c:69
static int mxf_read_utf16_string(AVIOContext *pb, int size, char **str, int be)
Definition: mxfdec.c:718
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:856
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:556
static const MXFCodecUL mxf_intra_only_essence_container_uls[]
Definition: mxfdec.c:1175
unsigned int vert_subsampling
Definition: mxfdec.c:188
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:184
int intra_only
Definition: mxfdec.c:162
#define MXF_FIELD_DOMINANCE_FL
Definition: mxfdec.c:181
#define av_log(a,...)
int32_t kag_size
Definition: mxfdec.c:89
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:636
static const uint8_t mxf_random_index_pack_key[]
Definition: mxfdec.c:298
static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
Definition: mxfdec.c:1215
static MXFDescriptor * mxf_resolve_multidescriptor(MXFContext *mxf, MXFDescriptor *descriptor, int track_id)
Definition: mxfdec.c:1703
static AVStream * mxf_get_opatom_stream(MXFContext *mxf)
Definition: mxfdec.c:2681
uint8_t track_number[4]
Definition: mxfdec.c:160
int64_t original_duration
Definition: mxfdec.c:164
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
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:184
#define SET_UID_METADATA(pb, name, var, str)
Definition: mxfdec.c:2309
static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
Definition: mxfdec.c:362
#define AVINDEX_KEYFRAME
Definition: avformat.h:827
int metadata_sets_count
Definition: mxfdec.c:251
UID essence_container_ul
Definition: mxfdec.c:170
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:214
int64_t start_position
Definition: mxfdec.c:109
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int last_forward_partition
Definition: mxfdec.c:263
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1709
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
static int mxf_read_timecode_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:789
MXFPartitionType
Definition: mxfdec.c:59
static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
Definition: mxfdec.c:966
static MXFTimecodeComponent * mxf_resolve_timecode_component(MXFContext *mxf, UID *strong_ref)
Definition: mxfdec.c:1666
UID essence_codec_ul
Definition: mxfdec.c:171
int8_t * temporal_offset_entries
Definition: mxfdec.c:206
static int mxf_add_timecode_metadata(AVDictionary **pm, const char *key, AVTimecode *tc)
Definition: mxfdec.c:1658
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:841
static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_table, int64_t edit_unit, int64_t *edit_unit_out, int64_t *offset_out, int nag)
Definition: mxfdec.c:1333
MXFDescriptor * descriptor
Definition: mxfdec.c:219
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:821
uint64_t index_start_position
Definition: mxfdec.c:204
int nb_ptses
Definition: mxfdec.c:235
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:181
static int mxf_read_index_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
Definition: mxfdec.c:899
unsigned matching_len
Definition: mxf.h:69
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static const MXFCodecUL mxf_intra_only_picture_essence_coding_uls[]
Definition: mxfdec.c:1181
Definition: mxf.h:56
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
const char * arg
Definition: jacosubdec.c:66
uint64_t length
Definition: mxf.h:64
static const MXFCodecUL mxf_sound_essence_container_uls[]
Definition: mxfdec.c:1196
simple assert() macros that are a bit more flexible than ISO C assert().
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static int mxf_parse_handle_essence(MXFContext *mxf)
Called when essence is encountered.
Definition: mxfdec.c:2586
#define IS_KLV_KEY(x, y)
Definition: mxfdec.c:305
Definition: mxf.h:40
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:31
static int64_t round_to_kag(int64_t position, int kag_size)
Definition: mxfdec.c:2667
int track_id
Definition: mxfdec.c:158
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
UID package_uid
Definition: mxfdec.c:215
#define FFMAX(a, b)
Definition: common.h:94
static void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.h:229
int64_t essence_length
Definition: mxfdec.c:88
#define fail()
Definition: checkasm.h:109
static int mxf_read_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:875
static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:767
int64_t duration
Definition: mxfdec.c:119
int video_line_map[2]
Definition: mxfdec.c:178
static int mxf_match_uid(const UID key, const UID uid, int len)
Definition: mxfdec.c:1124
int packages_count
Definition: mxfdec.c:249
Only parse headers, do not repack.
Definition: avformat.h:811
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:627
static MXFStructuralComponent * mxf_resolve_sourceclip(MXFContext *mxf, UID *strong_ref)
Definition: mxfdec.c:1756
int64_t index_byte_count
Definition: mxfdec.c:91
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
int ff_mxf_decode_pixel_layout(const char pixel_layout[16], enum AVPixelFormat *pix_fmt)
Definition: mxf.c:119
const MXFCodecUL ff_mxf_pixel_format_uls[]
Definition: mxf.c:77
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
Definition: hls.c:67
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1405
int nb_segments
Definition: mxfdec.c:238
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: utils.c:5133
static const MXFCodecUL * mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
Definition: mxfdec.c:1134
static const uint8_t mxf_jp2k_rsiz[]
Definition: mxfdec.c:301
MXFPartition * partitions
Definition: mxfdec.c:245
int8_t * offsets
Definition: mxfdec.c:241
enum MXFMetadataSetType type
Definition: mxfdec.c:199
static const uint8_t mxf_essence_element_key[]
Definition: mxfdec.c:289
static void * mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
Definition: mxfdec.c:1144
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
int local_tags_count
Definition: mxfdec.c:255
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:3230
unsigned int horiz_subsampling
Definition: mxfdec.c:187
#define PRIxUID
Definition: mxf.h:86
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
static MXFStructuralComponent * mxf_resolve_essence_group_choice(MXFContext *mxf, MXFEssenceGroup *essence_group)
Definition: mxfdec.c:1729
static const uint8_t mxf_encrypted_triplet_key[]
Definition: mxfdec.c:296
#define FFMIN(a, b)
Definition: common.h:96
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
enum AVPixelFormat pix_fmt
Definition: mxfdec.c:194
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
UID uid
Definition: mxfdec.c:114
int64_t essence_offset
absolute offset of essence
Definition: mxfdec.c:87
struct AVRational rate
Definition: mxfdec.c:128
static int mxf_parse_package_comments(MXFContext *mxf, AVDictionary **pm, MXFPackage *package)
Definition: mxfdec.c:1774
int edit_units_per_packet
how many edit units to read at a time (PCM, OPAtom)
Definition: mxfdec.c:267
int64_t last_forward_tell
Definition: mxfdec.c:262
static const uint8_t mxf_header_partition_pack_key[]
Definition: mxfdec.c:288
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:496
static const MXFCodecUL mxf_intra_only_picture_coded_width[]
Definition: mxfdec.c:1188
Definition: mxfdec.c:70
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:961
#define FF_PROFILE_JPEG2000_DCINEMA_4K
Definition: avcodec.h:3347
int64_t this_partition
Definition: mxfdec.c:86
static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
Definition: mxfdec.c:433
static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset)
Definition: mxfdec.c:2322
enum AVCodecID codec_id
Definition: vaapi_decode.c:235
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
Definition: mxfdec.c:386
static void mxf_handle_small_eubc(AVFormatContext *s)
Deal with the case where for some audio atoms EditUnitByteCount is very small (2, 4...
Definition: mxfdec.c:2701
const MXFCodecUL ff_mxf_data_definition_uls[]
SMPTE RP224 http://www.smpte-ra.org/mdd/index.html.
Definition: mxf.c:28
UID * sub_descriptors_refs
Definition: mxfdec.c:189
static int mxf_read_essence_group(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:861
if(ret< 0)
Definition: vf_mcdeint.c:279
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:219
#define FF_ARRAY_ELEMS(a)
uint8_t le
Definition: crc.c:295
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:1341
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
uint64_t index_duration
Definition: mxfdec.c:205
Stream structure.
Definition: avformat.h:889
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static const char *const mxf_data_essence_descriptor[]
Definition: mxfdec.c:1211
static int mxf_read_strong_ref_array(AVIOContext *pb, UID **refs, int *count)
Definition: mxfdec.c:705
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:195
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
static int mxf_read_tagged_value(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:1108
sample_rate
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Timecode helpers header.
int sub_descriptors_count
Definition: mxfdec.c:190
int nb_index_tables
Definition: mxfdec.c:265
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:986
static const uint8_t mxf_encrypted_essence_container[]
Definition: mxfdec.c:297
static int mxf_metadataset_init(MXFMetadataSet *ctx, enum MXFMetadataSetType type)
Definition: mxfdec.c:2411
Definition: mxfdec.c:66
int samples_per_frame[6]
Definition: mxf.h:75
#define AV_RN16(p)
Definition: intreadwrite.h:365
void * buf
Definition: avisynth_c.h:690
int run_in
Definition: mxfdec.c:259
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
int comment_count
Definition: mxfdec.c:223
double value
Definition: eval.c:91
static const MXFCodecUL mxf_data_essence_container_uls[]
Definition: mxfdec.c:1206
#define AV_PIX_FMT_XYZ12
Definition: pixfmt.h:425
AVRational aspect_ratio
Definition: mxfdec.c:174
int index
Definition: gxfenc.c:89
Definition: mxf.h:54
Rational number (pair of numerator and denominator).
Definition: rational.h:58
static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:522
MXFPartitionType type
Definition: mxfdec.c:82
static const uint8_t mxf_canopus_essence_element_key[]
Definition: mxfdec.c:291
AVRational display_aspect_ratio
display aspect ratio (0 if unknown)
Definition: avformat.h:1233
static int mxf_set_pts(MXFContext *mxf, AVStream *st, AVPacket *pkt, int64_t next_ofs)
Definition: mxfdec.c:3070
static const uint8_t mxf_system_item_key[]
Definition: mxfdec.c:292
struct AVAES * aesc
Definition: mxfdec.c:253
static MXFPackage * mxf_resolve_source_package(MXFContext *mxf, UID package_uid)
Definition: mxfdec.c:1687
AVFormatContext * fc
Definition: mxfdec.c:252
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition: avformat.h:517
uint8_t * extradata
Definition: mxfdec.c:192
UID package_ul
Definition: mxfdec.c:216
unsigned partitions_count
Definition: mxfdec.c:246
AVRational sample_rate
Definition: mxfdec.c:173
#define snprintf
Definition: snprintf.h:34
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
Definition: mxf.h:67
#define AV_RN32(p)
Definition: intreadwrite.h:369
misc parsing utilities
uint8_t * local_tags
Definition: mxfdec.c:254
int channels
Definition: mxfdec.c:183
#define READ_STR16(type, big_endian)
Definition: mxfdec.c:745
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:79
static const uint8_t mxf_indirect_value_utf16be[]
Definition: mxfdec.c:303
static int64_t klv_decode_ber_length(AVIOContext *pb)
Definition: mxfdec.c:347
MXFWrappingScheme
Definition: mxfdec.c:270
int64_t duration
Definition: mxfdec.c:143
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
Load timecode string in buf.
Definition: timecode.c:84
char * name
Definition: mxfdec.c:159
static int mxf_read_pulldown_component(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:806
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
int sample_rate
Audio only.
Definition: avcodec.h:4262
Definition: mxfdec.c:73
static int mxf_parse_physical_source_package(MXFContext *mxf, MXFTrack *source_track, AVStream *st)
Definition: mxfdec.c:1796
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
const uint8_t * key
Definition: avformat.h:1503
int parsing_backward
Definition: mxfdec.c:261
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:78
full parsing and repack
Definition: avformat.h:810
Main libavformat public API header.
static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:754
int tracks_count
Definition: mxfdec.c:218
Definition: mxf.h:37
int body_sid
Definition: mxfdec.c:234
static const MXFCodecUL mxf_picture_essence_container_uls[]
Definition: mxfdec.c:1159
int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen)
int height
Definition: mxfdec.c:176
UID codec_ul
Definition: mxfdec.c:172
Definition: mxfdec.c:75
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:936
MXFMetadataReadFunc * read
Definition: mxfdec.c:280
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
Definition: mxfdec.c:68
Definition: mxfdec.c:74
static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
Definition: mxfdec.c:1866
int field_dominance
Definition: mxfdec.c:182
static void mxf_free_metadataset(MXFMetadataSet **ctx, int freectx)
Definition: mxfdec.c:307
AVInputFormat ff_mxf_demuxer
Definition: mxfdec.c:3379
UID uid
Definition: mxfdec.c:213
int den
Denominator.
Definition: rational.h:60
AVIndexEntry * fake_index
Definition: mxfdec.c:240
int64_t pack_ofs
absolute offset of pack in file, including run-in
Definition: mxfdec.c:93
int structural_components_count
Definition: mxfdec.c:118
UID data_definition_ul
Definition: mxfdec.c:116
static int64_t mxf_timestamp_to_int64(uint64_t timestamp)
Definition: mxfdec.c:2281
enum MXFMetadataSetType type
Definition: mxfdec.c:228
static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:695
int body_sid
Definition: mxfdec.c:85
static int mxf_compute_ptses_fake_index(MXFContext *mxf, MXFIndexTable *index_table)
Definition: mxfdec.c:1381
#define av_free(p)
static int mxf_set_audio_pts(MXFContext *mxf, AVCodecParameters *par, AVPacket *pkt)
Definition: mxfdec.c:3051
int current_klv_index
Definition: mxfdec.c:258
const MXFCodecUL ff_mxf_codec_tag_uls[]
Definition: mxf.c:83
int len
uint64_t * stream_offset_entries
Definition: mxfdec.c:208
static int mxf_is_partition_pack_key(UID key)
Matches any partition pack key, in other words:
Definition: mxfdec.c:2493
static int mxf_read_preface_metadata(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:2359
static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
Definition: mxfdec.c:682
MXFOP op
Definition: mxfdec.c:247
void * priv_data
Format private data.
Definition: avformat.h:1377
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:1312
#define FF_PROFILE_JPEG2000_DCINEMA_2K
Definition: avcodec.h:3346
int extradata_size
Definition: mxfdec.c:193
UID uid
Definition: mxfdec.c:154
static int mxf_read_indirect_value(void *arg, AVIOContext *pb, int size)
Definition: mxfdec.c:1090
uint64_t layout
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4194
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4166
static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
Definition: mxfdec.c:817
#define SET_STR_METADATA(pb, name, str)
Definition: mxfdec.c:2303
int channels
Audio only.
Definition: avcodec.h:4258
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1678
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
static int64_t mxf_set_current_edit_unit(MXFContext *mxf, int64_t current_offset)
Sets mxf->current_edit_unit based on what offset we&#39;re currently at.
Definition: mxfdec.c:2973
int64_t duration
Definition: mxfdec.c:185
MXFMetadataSet ** metadata_sets
Definition: mxfdec.c:250
static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt)
Definition: mxfdec.c:3096
static int mxf_compute_sample_count(MXFContext *mxf, int stream_index, uint64_t *sample_count)
Definition: mxfdec.c:3010
#define av_freep(p)
void INT64 INT64 count
Definition: avisynth_c.h:690
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4156
MXFIndexTable * index_tables
Definition: mxfdec.c:266
const char int length
Definition: avisynth_c.h:768
UID uid
Definition: mxf.h:68
int pack_length
Definition: mxfdec.c:92
int stream_index
Definition: avcodec.h:1681
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:926
AVRational edit_rate
Definition: mxfdec.c:161
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:952
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1656
int index_sid
Definition: mxfdec.c:84
Definition: mxf.h:31
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672
for(j=16;j >0;--j)
Definition: mxfdec.c:60
uint64_t sample_count
Definition: mxfdec.c:163
MXFPartition * current_partition
Definition: mxfdec.c:260
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
char * value
Definition: mxfdec.c:150
uint64_t previous_partition
Definition: mxfdec.c:83
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
int64_t header_byte_count
Definition: mxfdec.c:90
static uint8_t tmp[11]
Definition: aes_ctr.c:26
UID descriptor_ref
Definition: mxfdec.c:220