You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2641 lines
98KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*
  22. * References
  23. * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
  24. * SMPTE 377M MXF File Format Specifications
  25. * SMPTE 378M Operational Pattern 1a
  26. * SMPTE 379M MXF Generic Container
  27. * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
  28. * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container
  29. * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container
  30. *
  31. * Principle
  32. * Search for Track numbers which will identify essence element KLV packets.
  33. * Search for SourcePackage which define tracks which contains Track numbers.
  34. * Material Package contains tracks with reference to SourcePackage tracks.
  35. * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
  36. * Assign Descriptors to correct Tracks.
  37. *
  38. * Metadata reading functions read Local Tags, get InstanceUID(0x3C0A) then add MetaDataSet to MXFContext.
  39. * Metadata parsing resolves Strong References to objects.
  40. *
  41. * Simple demuxer, only OP1A supported and some files might not work at all.
  42. * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
  43. */
  44. #include <inttypes.h>
  45. #include "libavutil/aes.h"
  46. #include "libavutil/mathematics.h"
  47. #include "libavcodec/bytestream.h"
  48. #include "avformat.h"
  49. #include "internal.h"
  50. #include "mxf.h"
  51. typedef enum {
  52. Header,
  53. BodyPartition,
  54. Footer
  55. } MXFPartitionType;
  56. typedef enum {
  57. OP1a = 1,
  58. OP1b,
  59. OP1c,
  60. OP2a,
  61. OP2b,
  62. OP2c,
  63. OP3a,
  64. OP3b,
  65. OP3c,
  66. OPAtom,
  67. OPSonyOpt, /* FATE sample, violates the spec in places */
  68. } MXFOP;
  69. typedef struct {
  70. int closed;
  71. int complete;
  72. MXFPartitionType type;
  73. uint64_t previous_partition;
  74. int index_sid;
  75. int body_sid;
  76. int64_t this_partition;
  77. int64_t essence_offset; ///< absolute offset of essence
  78. int64_t essence_length;
  79. int32_t kag_size;
  80. int64_t header_byte_count;
  81. int64_t index_byte_count;
  82. int pack_length;
  83. } MXFPartition;
  84. typedef struct {
  85. UID uid;
  86. enum MXFMetadataSetType type;
  87. UID source_container_ul;
  88. } MXFCryptoContext;
  89. typedef struct {
  90. UID uid;
  91. enum MXFMetadataSetType type;
  92. UID source_package_uid;
  93. UID data_definition_ul;
  94. int64_t duration;
  95. int64_t start_position;
  96. int source_track_id;
  97. } MXFStructuralComponent;
  98. typedef struct {
  99. UID uid;
  100. enum MXFMetadataSetType type;
  101. UID data_definition_ul;
  102. UID *structural_components_refs;
  103. int structural_components_count;
  104. int64_t duration;
  105. uint8_t origin;
  106. } MXFSequence;
  107. typedef struct {
  108. UID uid;
  109. enum MXFMetadataSetType type;
  110. MXFSequence *sequence; /* mandatory, and only one */
  111. UID sequence_ref;
  112. int track_id;
  113. uint8_t track_number[4];
  114. AVRational edit_rate;
  115. int intra_only;
  116. uint64_t sample_count;
  117. int64_t original_duration; /* st->duration in SampleRate/EditRate units */
  118. } MXFTrack;
  119. typedef struct {
  120. UID uid;
  121. enum MXFMetadataSetType type;
  122. UID essence_container_ul;
  123. UID essence_codec_ul;
  124. AVRational sample_rate;
  125. AVRational aspect_ratio;
  126. int width;
  127. int height; /* Field height, not frame height */
  128. int frame_layout; /* See MXFFrameLayout enum */
  129. #define MXF_TFF 1
  130. #define MXF_BFF 2
  131. int field_dominance;
  132. int channels;
  133. int bits_per_sample;
  134. unsigned int component_depth;
  135. unsigned int horiz_subsampling;
  136. unsigned int vert_subsampling;
  137. UID *sub_descriptors_refs;
  138. int sub_descriptors_count;
  139. int linked_track_id;
  140. uint8_t *extradata;
  141. int extradata_size;
  142. enum AVPixelFormat pix_fmt;
  143. } MXFDescriptor;
  144. typedef struct {
  145. UID uid;
  146. enum MXFMetadataSetType type;
  147. int edit_unit_byte_count;
  148. int index_sid;
  149. int body_sid;
  150. AVRational index_edit_rate;
  151. uint64_t index_start_position;
  152. uint64_t index_duration;
  153. int8_t *temporal_offset_entries;
  154. int *flag_entries;
  155. uint64_t *stream_offset_entries;
  156. int nb_index_entries;
  157. } MXFIndexTableSegment;
  158. typedef struct {
  159. UID uid;
  160. enum MXFMetadataSetType type;
  161. UID package_uid;
  162. UID *tracks_refs;
  163. int tracks_count;
  164. MXFDescriptor *descriptor; /* only one */
  165. UID descriptor_ref;
  166. } MXFPackage;
  167. typedef struct {
  168. UID uid;
  169. enum MXFMetadataSetType type;
  170. } MXFMetadataSet;
  171. /* decoded index table */
  172. typedef struct {
  173. int index_sid;
  174. int body_sid;
  175. int nb_ptses; /* number of PTSes or total duration of index */
  176. int64_t first_dts; /* DTS = EditUnit + first_dts */
  177. int64_t *ptses; /* maps EditUnit -> PTS */
  178. int nb_segments;
  179. MXFIndexTableSegment **segments; /* sorted by IndexStartPosition */
  180. AVIndexEntry *fake_index; /* used for calling ff_index_search_timestamp() */
  181. } MXFIndexTable;
  182. typedef struct {
  183. MXFPartition *partitions;
  184. unsigned partitions_count;
  185. MXFOP op;
  186. UID *packages_refs;
  187. int packages_count;
  188. MXFMetadataSet **metadata_sets;
  189. int metadata_sets_count;
  190. AVFormatContext *fc;
  191. struct AVAES *aesc;
  192. uint8_t *local_tags;
  193. int local_tags_count;
  194. uint64_t last_partition;
  195. uint64_t footer_partition;
  196. KLVPacket current_klv_data;
  197. int current_klv_index;
  198. int run_in;
  199. MXFPartition *current_partition;
  200. int parsing_backward;
  201. int64_t last_forward_tell;
  202. int last_forward_partition;
  203. int current_edit_unit;
  204. int nb_index_tables;
  205. MXFIndexTable *index_tables;
  206. int edit_units_per_packet; ///< how many edit units to read at a time (PCM, OPAtom)
  207. } MXFContext;
  208. enum MXFWrappingScheme {
  209. Frame,
  210. Clip,
  211. };
  212. /* NOTE: klv_offset is not set (-1) for local keys */
  213. typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset);
  214. typedef struct {
  215. const UID key;
  216. MXFMetadataReadFunc *read;
  217. int ctx_size;
  218. enum MXFMetadataSetType type;
  219. } MXFMetadataReadTableEntry;
  220. /* partial keys to match */
  221. static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
  222. static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
  223. static const uint8_t mxf_avid_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0e,0x04,0x03,0x01 };
  224. static const uint8_t mxf_system_item_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x03,0x01,0x04 };
  225. static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
  226. /* complete keys to match */
  227. 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 };
  228. static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
  229. static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
  230. 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 };
  231. static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
  232. #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
  233. static int64_t klv_decode_ber_length(AVIOContext *pb)
  234. {
  235. uint64_t size = avio_r8(pb);
  236. if (size & 0x80) { /* long form */
  237. int bytes_num = size & 0x7f;
  238. /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
  239. if (bytes_num > 8)
  240. return AVERROR_INVALIDDATA;
  241. size = 0;
  242. while (bytes_num--)
  243. size = size << 8 | avio_r8(pb);
  244. }
  245. return size;
  246. }
  247. static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
  248. {
  249. int i, b;
  250. for (i = 0; i < size && !pb->eof_reached; i++) {
  251. b = avio_r8(pb);
  252. if (b == key[0])
  253. i = 0;
  254. else if (b != key[i])
  255. i = -1;
  256. }
  257. return i == size;
  258. }
  259. static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
  260. {
  261. if (!mxf_read_sync(pb, mxf_klv_key, 4))
  262. return AVERROR_INVALIDDATA;
  263. klv->offset = avio_tell(pb) - 4;
  264. memcpy(klv->key, mxf_klv_key, 4);
  265. avio_read(pb, klv->key + 4, 12);
  266. klv->length = klv_decode_ber_length(pb);
  267. return klv->length == -1 ? -1 : 0;
  268. }
  269. static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
  270. {
  271. int i;
  272. for (i = 0; i < s->nb_streams; i++) {
  273. MXFTrack *track = s->streams[i]->priv_data;
  274. /* SMPTE 379M 7.3 */
  275. if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
  276. return i;
  277. }
  278. /* return 0 if only one stream, for OP Atom files with 0 as track number */
  279. return s->nb_streams == 1 ? 0 : -1;
  280. }
  281. /* XXX: use AVBitStreamFilter */
  282. static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
  283. {
  284. const uint8_t *buf_ptr, *end_ptr;
  285. uint8_t *data_ptr;
  286. int i;
  287. if (length > 61444) /* worst case PAL 1920 samples 8 channels */
  288. return AVERROR_INVALIDDATA;
  289. length = av_get_packet(pb, pkt, length);
  290. if (length < 0)
  291. return length;
  292. data_ptr = pkt->data;
  293. end_ptr = pkt->data + length;
  294. buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
  295. for (; end_ptr - buf_ptr >= st->codec->channels * 4; ) {
  296. for (i = 0; i < st->codec->channels; i++) {
  297. uint32_t sample = bytestream_get_le32(&buf_ptr);
  298. if (st->codec->bits_per_coded_sample == 24)
  299. bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
  300. else
  301. bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
  302. }
  303. buf_ptr += 32 - st->codec->channels*4; // always 8 channels stored SMPTE 331M
  304. }
  305. av_shrink_packet(pkt, data_ptr - pkt->data);
  306. return 0;
  307. }
  308. static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
  309. {
  310. static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
  311. MXFContext *mxf = s->priv_data;
  312. AVIOContext *pb = s->pb;
  313. int64_t end = avio_tell(pb) + klv->length;
  314. int64_t size;
  315. uint64_t orig_size;
  316. uint64_t plaintext_size;
  317. uint8_t ivec[16];
  318. uint8_t tmpbuf[16];
  319. int index;
  320. if (!mxf->aesc && s->key && s->keylen == 16) {
  321. mxf->aesc = av_aes_alloc();
  322. if (!mxf->aesc)
  323. return AVERROR(ENOMEM);
  324. av_aes_init(mxf->aesc, s->key, 128, 1);
  325. }
  326. // crypto context
  327. avio_skip(pb, klv_decode_ber_length(pb));
  328. // plaintext offset
  329. klv_decode_ber_length(pb);
  330. plaintext_size = avio_rb64(pb);
  331. // source klv key
  332. klv_decode_ber_length(pb);
  333. avio_read(pb, klv->key, 16);
  334. if (!IS_KLV_KEY(klv, mxf_essence_element_key))
  335. return AVERROR_INVALIDDATA;
  336. index = mxf_get_stream_index(s, klv);
  337. if (index < 0)
  338. return AVERROR_INVALIDDATA;
  339. // source size
  340. klv_decode_ber_length(pb);
  341. orig_size = avio_rb64(pb);
  342. if (orig_size < plaintext_size)
  343. return AVERROR_INVALIDDATA;
  344. // enc. code
  345. size = klv_decode_ber_length(pb);
  346. if (size < 32 || size - 32 < orig_size)
  347. return AVERROR_INVALIDDATA;
  348. avio_read(pb, ivec, 16);
  349. avio_read(pb, tmpbuf, 16);
  350. if (mxf->aesc)
  351. av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
  352. if (memcmp(tmpbuf, checkv, 16))
  353. av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
  354. size -= 32;
  355. size = av_get_packet(pb, pkt, size);
  356. if (size < 0)
  357. return size;
  358. else if (size < plaintext_size)
  359. return AVERROR_INVALIDDATA;
  360. size -= plaintext_size;
  361. if (mxf->aesc)
  362. av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
  363. &pkt->data[plaintext_size], size >> 4, ivec, 1);
  364. av_shrink_packet(pkt, orig_size);
  365. pkt->stream_index = index;
  366. avio_skip(pb, end - avio_tell(pb));
  367. return 0;
  368. }
  369. static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  370. {
  371. MXFContext *mxf = arg;
  372. int item_num = avio_rb32(pb);
  373. int item_len = avio_rb32(pb);
  374. if (item_len != 18) {
  375. avpriv_request_sample(pb, "Primer pack item length %d", item_len);
  376. return AVERROR_PATCHWELCOME;
  377. }
  378. if (item_num > UINT_MAX / item_len)
  379. return AVERROR_INVALIDDATA;
  380. mxf->local_tags_count = item_num;
  381. mxf->local_tags = av_malloc(item_num*item_len);
  382. if (!mxf->local_tags)
  383. return AVERROR(ENOMEM);
  384. avio_read(pb, mxf->local_tags, item_num*item_len);
  385. return 0;
  386. }
  387. static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  388. {
  389. MXFContext *mxf = arg;
  390. MXFPartition *partition;
  391. UID op;
  392. uint64_t footer_partition;
  393. uint32_t nb_essence_containers;
  394. int err;
  395. if ((err = av_reallocp_array(&mxf->partitions, mxf->partitions_count + 1,
  396. sizeof(*mxf->partitions))) < 0) {
  397. mxf->partitions_count = 0;
  398. return err;
  399. }
  400. if (mxf->parsing_backward) {
  401. /* insert the new partition pack in the middle
  402. * this makes the entries in mxf->partitions sorted by offset */
  403. memmove(&mxf->partitions[mxf->last_forward_partition+1],
  404. &mxf->partitions[mxf->last_forward_partition],
  405. (mxf->partitions_count - mxf->last_forward_partition)*sizeof(*mxf->partitions));
  406. partition = mxf->current_partition = &mxf->partitions[mxf->last_forward_partition];
  407. } else {
  408. mxf->last_forward_partition++;
  409. partition = mxf->current_partition = &mxf->partitions[mxf->partitions_count];
  410. }
  411. memset(partition, 0, sizeof(*partition));
  412. mxf->partitions_count++;
  413. partition->pack_length = avio_tell(pb) - klv_offset + size;
  414. switch(uid[13]) {
  415. case 2:
  416. partition->type = Header;
  417. break;
  418. case 3:
  419. partition->type = BodyPartition;
  420. break;
  421. case 4:
  422. partition->type = Footer;
  423. break;
  424. default:
  425. av_log(mxf->fc, AV_LOG_ERROR, "unknown partition type %i\n", uid[13]);
  426. return AVERROR_INVALIDDATA;
  427. }
  428. /* consider both footers to be closed (there is only Footer and CompleteFooter) */
  429. partition->closed = partition->type == Footer || !(uid[14] & 1);
  430. partition->complete = uid[14] > 2;
  431. avio_skip(pb, 4);
  432. partition->kag_size = avio_rb32(pb);
  433. partition->this_partition = avio_rb64(pb);
  434. partition->previous_partition = avio_rb64(pb);
  435. footer_partition = avio_rb64(pb);
  436. partition->header_byte_count = avio_rb64(pb);
  437. partition->index_byte_count = avio_rb64(pb);
  438. partition->index_sid = avio_rb32(pb);
  439. avio_skip(pb, 8);
  440. partition->body_sid = avio_rb32(pb);
  441. avio_read(pb, op, sizeof(UID));
  442. nb_essence_containers = avio_rb32(pb);
  443. if (partition->this_partition &&
  444. partition->previous_partition == partition->this_partition) {
  445. av_log(mxf->fc, AV_LOG_ERROR,
  446. "PreviousPartition equal to ThisPartition %"PRIx64"\n",
  447. partition->previous_partition);
  448. /* override with the actual previous partition offset */
  449. if (!mxf->parsing_backward && mxf->last_forward_partition > 1) {
  450. MXFPartition *prev =
  451. mxf->partitions + mxf->last_forward_partition - 2;
  452. partition->previous_partition = prev->this_partition;
  453. }
  454. /* if no previous body partition are found point to the header
  455. * partition */
  456. if (partition->previous_partition == partition->this_partition)
  457. partition->previous_partition = 0;
  458. av_log(mxf->fc, AV_LOG_ERROR,
  459. "Overriding PreviousPartition with %"PRIx64"\n",
  460. partition->previous_partition);
  461. }
  462. /* some files don'thave FooterPartition set in every partition */
  463. if (footer_partition) {
  464. if (mxf->footer_partition && mxf->footer_partition != footer_partition) {
  465. av_log(mxf->fc, AV_LOG_ERROR,
  466. "inconsistent FooterPartition value: %"PRIu64" != %"PRIu64"\n",
  467. mxf->footer_partition, footer_partition);
  468. } else {
  469. mxf->footer_partition = footer_partition;
  470. }
  471. }
  472. av_dlog(mxf->fc,
  473. "PartitionPack: ThisPartition = 0x%"PRIX64
  474. ", PreviousPartition = 0x%"PRIX64", "
  475. "FooterPartition = 0x%"PRIX64", IndexSID = %i, BodySID = %i\n",
  476. partition->this_partition,
  477. partition->previous_partition, footer_partition,
  478. partition->index_sid, partition->body_sid);
  479. /* sanity check PreviousPartition if set */
  480. if (partition->previous_partition &&
  481. mxf->run_in + partition->previous_partition >= klv_offset) {
  482. av_log(mxf->fc, AV_LOG_ERROR,
  483. "PreviousPartition points to this partition or forward\n");
  484. return AVERROR_INVALIDDATA;
  485. }
  486. if (op[12] == 1 && op[13] == 1) mxf->op = OP1a;
  487. else if (op[12] == 1 && op[13] == 2) mxf->op = OP1b;
  488. else if (op[12] == 1 && op[13] == 3) mxf->op = OP1c;
  489. else if (op[12] == 2 && op[13] == 1) mxf->op = OP2a;
  490. else if (op[12] == 2 && op[13] == 2) mxf->op = OP2b;
  491. else if (op[12] == 2 && op[13] == 3) mxf->op = OP2c;
  492. else if (op[12] == 3 && op[13] == 1) mxf->op = OP3a;
  493. else if (op[12] == 3 && op[13] == 2) mxf->op = OP3b;
  494. else if (op[12] == 3 && op[13] == 3) mxf->op = OP3c;
  495. else if (op[12] == 64&& op[13] == 1) mxf->op = OPSonyOpt;
  496. else if (op[12] == 0x10) {
  497. /* SMPTE 390m: "There shall be exactly one essence container"
  498. * The following block deals with files that violate this, namely:
  499. * 2011_DCPTEST_24FPS.V.mxf - two ECs, OP1a
  500. * abcdefghiv016f56415e.mxf - zero ECs, OPAtom, output by Avid AirSpeed */
  501. if (nb_essence_containers != 1) {
  502. MXFOP op = nb_essence_containers ? OP1a : OPAtom;
  503. /* only nag once */
  504. if (!mxf->op)
  505. av_log(mxf->fc, AV_LOG_WARNING,
  506. "\"OPAtom\" with %"PRIu32" ECs - assuming %s\n",
  507. nb_essence_containers,
  508. op == OP1a ? "OP1a" : "OPAtom");
  509. mxf->op = op;
  510. } else
  511. mxf->op = OPAtom;
  512. } else {
  513. av_log(mxf->fc, AV_LOG_ERROR, "unknown operational pattern: %02xh %02xh - guessing OP1a\n", op[12], op[13]);
  514. mxf->op = OP1a;
  515. }
  516. if (partition->kag_size <= 0 || partition->kag_size > (1 << 20)) {
  517. av_log(mxf->fc, AV_LOG_WARNING, "invalid KAGSize %"PRId32" - guessing ",
  518. partition->kag_size);
  519. if (mxf->op == OPSonyOpt)
  520. partition->kag_size = 512;
  521. else
  522. partition->kag_size = 1;
  523. av_log(mxf->fc, AV_LOG_WARNING, "%"PRId32"\n", partition->kag_size);
  524. }
  525. return 0;
  526. }
  527. static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
  528. {
  529. int err;
  530. if ((err = av_reallocp_array(&mxf->metadata_sets, mxf->metadata_sets_count + 1,
  531. sizeof(*mxf->metadata_sets))) < 0) {
  532. mxf->metadata_sets_count = 0;
  533. return err;
  534. }
  535. mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
  536. mxf->metadata_sets_count++;
  537. return 0;
  538. }
  539. static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  540. {
  541. MXFCryptoContext *cryptocontext = arg;
  542. if (size != 16)
  543. return AVERROR_INVALIDDATA;
  544. if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
  545. avio_read(pb, cryptocontext->source_container_ul, 16);
  546. return 0;
  547. }
  548. static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  549. {
  550. MXFContext *mxf = arg;
  551. switch (tag) {
  552. case 0x1901:
  553. mxf->packages_count = avio_rb32(pb);
  554. if (mxf->packages_count >= UINT_MAX / sizeof(UID))
  555. return AVERROR_INVALIDDATA;
  556. mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
  557. if (!mxf->packages_refs)
  558. return AVERROR(ENOMEM);
  559. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  560. avio_read(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
  561. break;
  562. }
  563. return 0;
  564. }
  565. static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  566. {
  567. MXFStructuralComponent *source_clip = arg;
  568. switch(tag) {
  569. case 0x0202:
  570. source_clip->duration = avio_rb64(pb);
  571. break;
  572. case 0x1201:
  573. source_clip->start_position = avio_rb64(pb);
  574. break;
  575. case 0x1101:
  576. /* UMID, only get last 16 bytes */
  577. avio_skip(pb, 16);
  578. avio_read(pb, source_clip->source_package_uid, 16);
  579. break;
  580. case 0x1102:
  581. source_clip->source_track_id = avio_rb32(pb);
  582. break;
  583. }
  584. return 0;
  585. }
  586. static int mxf_read_material_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  587. {
  588. MXFPackage *package = arg;
  589. switch(tag) {
  590. case 0x4403:
  591. package->tracks_count = avio_rb32(pb);
  592. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  593. return AVERROR_INVALIDDATA;
  594. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  595. if (!package->tracks_refs)
  596. return AVERROR(ENOMEM);
  597. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  598. avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  599. break;
  600. }
  601. return 0;
  602. }
  603. static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  604. {
  605. MXFTrack *track = arg;
  606. switch(tag) {
  607. case 0x4801:
  608. track->track_id = avio_rb32(pb);
  609. break;
  610. case 0x4804:
  611. avio_read(pb, track->track_number, 4);
  612. break;
  613. case 0x4b01:
  614. track->edit_rate.num = avio_rb32(pb);
  615. track->edit_rate.den = avio_rb32(pb);
  616. break;
  617. case 0x4803:
  618. avio_read(pb, track->sequence_ref, 16);
  619. break;
  620. }
  621. return 0;
  622. }
  623. static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  624. {
  625. MXFSequence *sequence = arg;
  626. switch(tag) {
  627. case 0x0202:
  628. sequence->duration = avio_rb64(pb);
  629. break;
  630. case 0x0201:
  631. avio_read(pb, sequence->data_definition_ul, 16);
  632. break;
  633. case 0x4b02:
  634. sequence->origin = avio_r8(pb);
  635. break;
  636. case 0x1001:
  637. sequence->structural_components_count = avio_rb32(pb);
  638. if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
  639. return AVERROR_INVALIDDATA;
  640. sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
  641. if (!sequence->structural_components_refs)
  642. return AVERROR(ENOMEM);
  643. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  644. avio_read(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
  645. break;
  646. }
  647. return 0;
  648. }
  649. static int mxf_read_source_package(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  650. {
  651. MXFPackage *package = arg;
  652. switch(tag) {
  653. case 0x4403:
  654. package->tracks_count = avio_rb32(pb);
  655. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  656. return AVERROR_INVALIDDATA;
  657. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  658. if (!package->tracks_refs)
  659. return AVERROR(ENOMEM);
  660. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  661. avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  662. break;
  663. case 0x4401:
  664. /* UMID, only get last 16 bytes */
  665. avio_skip(pb, 16);
  666. avio_read(pb, package->package_uid, 16);
  667. break;
  668. case 0x4701:
  669. avio_read(pb, package->descriptor_ref, 16);
  670. break;
  671. }
  672. return 0;
  673. }
  674. static int mxf_read_index_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
  675. {
  676. int i, length;
  677. segment->nb_index_entries = avio_rb32(pb);
  678. if (!segment->nb_index_entries)
  679. return 0;
  680. else if (segment->nb_index_entries < 0 ||
  681. segment->nb_index_entries >
  682. (INT_MAX / sizeof(*segment->stream_offset_entries)))
  683. return AVERROR(ENOMEM);
  684. length = avio_rb32(pb);
  685. segment->temporal_offset_entries = av_mallocz(segment->nb_index_entries *
  686. sizeof(*segment->temporal_offset_entries));
  687. segment->flag_entries = av_mallocz(segment->nb_index_entries *
  688. sizeof(*segment->flag_entries));
  689. segment->stream_offset_entries = av_mallocz(segment->nb_index_entries *
  690. sizeof(*segment->stream_offset_entries));
  691. if (!segment->flag_entries || !segment->stream_offset_entries ||
  692. !segment->temporal_offset_entries) {
  693. av_freep(&segment->flag_entries);
  694. av_freep(&segment->stream_offset_entries);
  695. av_freep(&segment->temporal_offset_entries);
  696. return AVERROR(ENOMEM);
  697. }
  698. for (i = 0; i < segment->nb_index_entries; i++) {
  699. segment->temporal_offset_entries[i] = avio_r8(pb);
  700. avio_r8(pb); /* KeyFrameOffset */
  701. segment->flag_entries[i] = avio_r8(pb);
  702. segment->stream_offset_entries[i] = avio_rb64(pb);
  703. avio_skip(pb, length - 11);
  704. }
  705. return 0;
  706. }
  707. static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  708. {
  709. MXFIndexTableSegment *segment = arg;
  710. switch(tag) {
  711. case 0x3F05:
  712. segment->edit_unit_byte_count = avio_rb32(pb);
  713. av_dlog(NULL, "EditUnitByteCount %d\n", segment->edit_unit_byte_count);
  714. break;
  715. case 0x3F06:
  716. segment->index_sid = avio_rb32(pb);
  717. av_dlog(NULL, "IndexSID %d\n", segment->index_sid);
  718. break;
  719. case 0x3F07:
  720. segment->body_sid = avio_rb32(pb);
  721. av_dlog(NULL, "BodySID %d\n", segment->body_sid);
  722. break;
  723. case 0x3F0A:
  724. av_dlog(NULL, "IndexEntryArray found\n");
  725. return mxf_read_index_entry_array(pb, segment);
  726. case 0x3F0B:
  727. segment->index_edit_rate.num = avio_rb32(pb);
  728. segment->index_edit_rate.den = avio_rb32(pb);
  729. av_dlog(NULL, "IndexEditRate %d/%d\n", segment->index_edit_rate.num,
  730. segment->index_edit_rate.den);
  731. break;
  732. case 0x3F0C:
  733. segment->index_start_position = avio_rb64(pb);
  734. av_dlog(NULL, "IndexStartPosition %"PRId64"\n", segment->index_start_position);
  735. break;
  736. case 0x3F0D:
  737. segment->index_duration = avio_rb64(pb);
  738. av_dlog(NULL, "IndexDuration %"PRId64"\n", segment->index_duration);
  739. break;
  740. }
  741. return 0;
  742. }
  743. static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
  744. {
  745. int code, value, ofs = 0;
  746. char layout[16] = {0};
  747. do {
  748. code = avio_r8(pb);
  749. value = avio_r8(pb);
  750. av_dlog(NULL, "pixel layout: code %#x\n", code);
  751. if (ofs < 16) {
  752. layout[ofs++] = code;
  753. layout[ofs++] = value;
  754. }
  755. } while (code != 0); /* SMPTE 377M E.2.46 */
  756. ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
  757. }
  758. static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid, int64_t klv_offset)
  759. {
  760. MXFDescriptor *descriptor = arg;
  761. descriptor->pix_fmt = AV_PIX_FMT_NONE;
  762. switch(tag) {
  763. case 0x3F01:
  764. descriptor->sub_descriptors_count = avio_rb32(pb);
  765. if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
  766. return AVERROR_INVALIDDATA;
  767. descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
  768. if (!descriptor->sub_descriptors_refs)
  769. return AVERROR(ENOMEM);
  770. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  771. avio_read(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
  772. break;
  773. case 0x3004:
  774. avio_read(pb, descriptor->essence_container_ul, 16);
  775. break;
  776. case 0x3006:
  777. descriptor->linked_track_id = avio_rb32(pb);
  778. break;
  779. case 0x3201: /* PictureEssenceCoding */
  780. avio_read(pb, descriptor->essence_codec_ul, 16);
  781. break;
  782. case 0x3203:
  783. descriptor->width = avio_rb32(pb);
  784. break;
  785. case 0x3202:
  786. descriptor->height = avio_rb32(pb);
  787. break;
  788. case 0x320C:
  789. descriptor->frame_layout = avio_r8(pb);
  790. break;
  791. case 0x320E:
  792. descriptor->aspect_ratio.num = avio_rb32(pb);
  793. descriptor->aspect_ratio.den = avio_rb32(pb);
  794. break;
  795. case 0x3212:
  796. descriptor->field_dominance = avio_r8(pb);
  797. break;
  798. case 0x3301:
  799. descriptor->component_depth = avio_rb32(pb);
  800. break;
  801. case 0x3302:
  802. descriptor->horiz_subsampling = avio_rb32(pb);
  803. break;
  804. case 0x3308:
  805. descriptor->vert_subsampling = avio_rb32(pb);
  806. break;
  807. case 0x3D03:
  808. descriptor->sample_rate.num = avio_rb32(pb);
  809. descriptor->sample_rate.den = avio_rb32(pb);
  810. break;
  811. case 0x3D06: /* SoundEssenceCompression */
  812. avio_read(pb, descriptor->essence_codec_ul, 16);
  813. break;
  814. case 0x3D07:
  815. descriptor->channels = avio_rb32(pb);
  816. break;
  817. case 0x3D01:
  818. descriptor->bits_per_sample = avio_rb32(pb);
  819. break;
  820. case 0x3401:
  821. mxf_read_pixel_layout(pb, descriptor);
  822. break;
  823. default:
  824. /* Private uid used by SONY C0023S01.mxf */
  825. if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
  826. av_free(descriptor->extradata);
  827. descriptor->extradata_size = 0;
  828. descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  829. if (!descriptor->extradata)
  830. return AVERROR(ENOMEM);
  831. descriptor->extradata_size = size;
  832. avio_read(pb, descriptor->extradata, size);
  833. }
  834. break;
  835. }
  836. return 0;
  837. }
  838. /*
  839. * Match an uid independently of the version byte and up to len common bytes
  840. * Returns: boolean
  841. */
  842. static int mxf_match_uid(const UID key, const UID uid, int len)
  843. {
  844. int i;
  845. for (i = 0; i < len; i++) {
  846. if (i != 7 && key[i] != uid[i])
  847. return 0;
  848. }
  849. return 1;
  850. }
  851. static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
  852. {
  853. while (uls->uid[0]) {
  854. if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
  855. break;
  856. uls++;
  857. }
  858. return uls;
  859. }
  860. static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
  861. {
  862. int i;
  863. if (!strong_ref)
  864. return NULL;
  865. for (i = 0; i < mxf->metadata_sets_count; i++) {
  866. if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
  867. (type == AnyType || mxf->metadata_sets[i]->type == type)) {
  868. return mxf->metadata_sets[i];
  869. }
  870. }
  871. return NULL;
  872. }
  873. static const MXFCodecUL mxf_picture_essence_container_uls[] = {
  874. // video essence container uls
  875. { { 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 */
  876. { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, AV_CODEC_ID_DVVIDEO }, /* DV 625 25mbps */
  877. { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0d,0x01,0x03,0x01,0x02,0x05,0x00,0x00 }, 14, AV_CODEC_ID_RAWVIDEO }, /* Uncompressed Picture */
  878. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
  879. };
  880. /* EC ULs for intra-only formats */
  881. static const MXFCodecUL mxf_intra_only_essence_container_uls[] = {
  882. { { 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 */
  883. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
  884. };
  885. /* intra-only PictureEssenceCoding ULs, where no corresponding EC UL exists */
  886. static const MXFCodecUL mxf_intra_only_picture_essence_coding_uls[] = {
  887. { { 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 */
  888. { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, 14, AV_CODEC_ID_JPEG2000 }, /* JPEG2000 Codestream */
  889. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
  890. };
  891. static const MXFCodecUL mxf_sound_essence_container_uls[] = {
  892. // sound essence container uls
  893. { { 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 */
  894. { { 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 */
  895. { { 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 */
  896. { { 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 */
  897. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, AV_CODEC_ID_NONE },
  898. };
  899. static const MXFCodecUL mxf_data_essence_container_uls[] = {
  900. { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 }, 16, 0 },
  901. { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x09,0x0d,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 }, 16, AV_CODEC_ID_NONE },
  902. };
  903. static const char* mxf_data_essence_descriptor[] = {
  904. "vbi_vanc_smpte_436M",
  905. };
  906. static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
  907. {
  908. int i, j, nb_segments = 0;
  909. MXFIndexTableSegment **unsorted_segments;
  910. int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
  911. /* count number of segments, allocate arrays and copy unsorted segments */
  912. for (i = 0; i < mxf->metadata_sets_count; i++)
  913. if (mxf->metadata_sets[i]->type == IndexTableSegment)
  914. nb_segments++;
  915. if (!nb_segments)
  916. return AVERROR_INVALIDDATA;
  917. *sorted_segments = av_mallocz(nb_segments * sizeof(**sorted_segments));
  918. unsorted_segments = av_mallocz(nb_segments * sizeof(*unsorted_segments));
  919. if (!*sorted_segments || !unsorted_segments) {
  920. av_freep(sorted_segments);
  921. av_free(unsorted_segments);
  922. return AVERROR(ENOMEM);
  923. }
  924. for (i = j = 0; i < mxf->metadata_sets_count; i++)
  925. if (mxf->metadata_sets[i]->type == IndexTableSegment)
  926. unsorted_segments[j++] = (MXFIndexTableSegment*)mxf->metadata_sets[i];
  927. *nb_sorted_segments = 0;
  928. /* sort segments by {BodySID, IndexSID, IndexStartPosition}, remove duplicates while we're at it */
  929. for (i = 0; i < nb_segments; i++) {
  930. int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
  931. uint64_t best_index_duration = 0;
  932. for (j = 0; j < nb_segments; j++) {
  933. MXFIndexTableSegment *s = unsorted_segments[j];
  934. /* Require larger BosySID, IndexSID or IndexStartPosition then the previous entry. This removes duplicates.
  935. * We want the smallest values for the keys than what we currently have, unless this is the first such entry this time around.
  936. * If we come across an entry with the same IndexStartPosition but larger IndexDuration, then we'll prefer it over the one we currently have.
  937. */
  938. if ((i == 0 || s->body_sid > last_body_sid || s->index_sid > last_index_sid || s->index_start_position > last_index_start) &&
  939. (best == -1 || s->body_sid < best_body_sid || s->index_sid < best_index_sid || s->index_start_position < best_index_start ||
  940. (s->index_start_position == best_index_start && s->index_duration > best_index_duration))) {
  941. best = j;
  942. best_body_sid = s->body_sid;
  943. best_index_sid = s->index_sid;
  944. best_index_start = s->index_start_position;
  945. best_index_duration = s->index_duration;
  946. }
  947. }
  948. /* no suitable entry found -> we're done */
  949. if (best == -1)
  950. break;
  951. (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
  952. last_body_sid = best_body_sid;
  953. last_index_sid = best_index_sid;
  954. last_index_start = best_index_start;
  955. }
  956. av_free(unsorted_segments);
  957. return 0;
  958. }
  959. /**
  960. * Computes the absolute file offset of the given essence container offset
  961. */
  962. static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t offset, int64_t *offset_out)
  963. {
  964. int x;
  965. int64_t offset_in = offset; /* for logging */
  966. for (x = 0; x < mxf->partitions_count; x++) {
  967. MXFPartition *p = &mxf->partitions[x];
  968. if (p->body_sid != body_sid)
  969. continue;
  970. if (offset < p->essence_length || !p->essence_length) {
  971. *offset_out = p->essence_offset + offset;
  972. return 0;
  973. }
  974. offset -= p->essence_length;
  975. }
  976. av_log(mxf->fc, AV_LOG_ERROR,
  977. "failed to find absolute offset of %"PRIX64" in BodySID %i - partial file?\n",
  978. offset_in, body_sid);
  979. return AVERROR_INVALIDDATA;
  980. }
  981. /**
  982. * Returns the end position of the essence container with given BodySID, or zero if unknown
  983. */
  984. static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid)
  985. {
  986. int x;
  987. int64_t ret = 0;
  988. for (x = 0; x < mxf->partitions_count; x++) {
  989. MXFPartition *p = &mxf->partitions[x];
  990. if (p->body_sid != body_sid)
  991. continue;
  992. if (!p->essence_length)
  993. return 0;
  994. ret = p->essence_offset + p->essence_length;
  995. }
  996. return ret;
  997. }
  998. /* EditUnit -> absolute offset */
  999. 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)
  1000. {
  1001. int i;
  1002. int64_t offset_temp = 0;
  1003. for (i = 0; i < index_table->nb_segments; i++) {
  1004. MXFIndexTableSegment *s = index_table->segments[i];
  1005. edit_unit = FFMAX(edit_unit, s->index_start_position); /* clamp if trying to seek before start */
  1006. if (edit_unit < s->index_start_position + s->index_duration) {
  1007. int64_t index = edit_unit - s->index_start_position;
  1008. if (s->edit_unit_byte_count)
  1009. offset_temp += s->edit_unit_byte_count * index;
  1010. else if (s->nb_index_entries) {
  1011. if (s->nb_index_entries == 2 * s->index_duration + 1)
  1012. index *= 2; /* Avid index */
  1013. if (index < 0 || index >= s->nb_index_entries) {
  1014. av_log(mxf->fc, AV_LOG_ERROR, "IndexSID %i segment at %"PRId64" IndexEntryArray too small\n",
  1015. index_table->index_sid, s->index_start_position);
  1016. return AVERROR_INVALIDDATA;
  1017. }
  1018. offset_temp = s->stream_offset_entries[index];
  1019. } else {
  1020. av_log(mxf->fc, AV_LOG_ERROR, "IndexSID %i segment at %"PRId64" missing EditUnitByteCount and IndexEntryArray\n",
  1021. index_table->index_sid, s->index_start_position);
  1022. return AVERROR_INVALIDDATA;
  1023. }
  1024. if (edit_unit_out)
  1025. *edit_unit_out = edit_unit;
  1026. return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, offset_temp, offset_out);
  1027. } else {
  1028. /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */
  1029. offset_temp += s->edit_unit_byte_count * s->index_duration;
  1030. }
  1031. }
  1032. if (nag)
  1033. 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);
  1034. return AVERROR_INVALIDDATA;
  1035. }
  1036. static int mxf_compute_ptses_fake_index(MXFContext *mxf, MXFIndexTable *index_table)
  1037. {
  1038. int i, j, x;
  1039. int8_t max_temporal_offset = -128;
  1040. /* first compute how many entries we have */
  1041. for (i = 0; i < index_table->nb_segments; i++) {
  1042. MXFIndexTableSegment *s = index_table->segments[i];
  1043. if (!s->nb_index_entries) {
  1044. index_table->nb_ptses = 0;
  1045. return 0; /* no TemporalOffsets */
  1046. }
  1047. index_table->nb_ptses += s->index_duration;
  1048. }
  1049. /* paranoid check */
  1050. if (index_table->nb_ptses <= 0)
  1051. return 0;
  1052. if (index_table->nb_ptses > INT_MAX / sizeof(AVIndexEntry))
  1053. return AVERROR(ENOMEM);
  1054. index_table->ptses = av_mallocz(index_table->nb_ptses *
  1055. sizeof(int64_t));
  1056. index_table->fake_index = av_mallocz(index_table->nb_ptses *
  1057. sizeof(AVIndexEntry));
  1058. if (!index_table->ptses || !index_table->fake_index) {
  1059. av_freep(&index_table->ptses);
  1060. return AVERROR(ENOMEM);
  1061. }
  1062. /* we may have a few bad TemporalOffsets
  1063. * make sure the corresponding PTSes don't have the bogus value 0 */
  1064. for (x = 0; x < index_table->nb_ptses; x++)
  1065. index_table->ptses[x] = AV_NOPTS_VALUE;
  1066. /**
  1067. * We have this:
  1068. *
  1069. * x TemporalOffset
  1070. * 0: 0
  1071. * 1: 1
  1072. * 2: 1
  1073. * 3: -2
  1074. * 4: 1
  1075. * 5: 1
  1076. * 6: -2
  1077. *
  1078. * We want to transform it into this:
  1079. *
  1080. * x DTS PTS
  1081. * 0: -1 0
  1082. * 1: 0 3
  1083. * 2: 1 1
  1084. * 3: 2 2
  1085. * 4: 3 6
  1086. * 5: 4 4
  1087. * 6: 5 5
  1088. *
  1089. * We do this by bucket sorting x by x+TemporalOffset[x] into mxf->ptses,
  1090. * then settings mxf->first_dts = -max(TemporalOffset[x]).
  1091. * The latter makes DTS <= PTS.
  1092. */
  1093. for (i = x = 0; i < index_table->nb_segments; i++) {
  1094. MXFIndexTableSegment *s = index_table->segments[i];
  1095. int index_delta = 1;
  1096. int n = s->nb_index_entries;
  1097. if (s->nb_index_entries == 2 * s->index_duration + 1) {
  1098. index_delta = 2; /* Avid index */
  1099. /* ignore the last entry - it's the size of the essence container */
  1100. n--;
  1101. }
  1102. for (j = 0; j < n; j += index_delta, x++) {
  1103. int offset = s->temporal_offset_entries[j] / index_delta;
  1104. int index = x + offset;
  1105. if (x >= index_table->nb_ptses) {
  1106. av_log(mxf->fc, AV_LOG_ERROR,
  1107. "x >= nb_ptses - IndexEntryCount %i < IndexDuration %"PRId64"?\n",
  1108. s->nb_index_entries, s->index_duration);
  1109. break;
  1110. }
  1111. index_table->fake_index[x].timestamp = x;
  1112. index_table->fake_index[x].flags = !(s->flag_entries[j] & 0x30) ? AVINDEX_KEYFRAME : 0;
  1113. if (index < 0 || index >= index_table->nb_ptses) {
  1114. av_log(mxf->fc, AV_LOG_ERROR,
  1115. "index entry %i + TemporalOffset %i = %i, which is out of bounds\n",
  1116. x, offset, index);
  1117. continue;
  1118. }
  1119. index_table->ptses[index] = x;
  1120. max_temporal_offset = FFMAX(max_temporal_offset, offset);
  1121. }
  1122. }
  1123. index_table->first_dts = -max_temporal_offset;
  1124. return 0;
  1125. }
  1126. /**
  1127. * Sorts and collects index table segments into index tables.
  1128. * Also computes PTSes if possible.
  1129. */
  1130. static int mxf_compute_index_tables(MXFContext *mxf)
  1131. {
  1132. int i, j, k, ret, nb_sorted_segments;
  1133. MXFIndexTableSegment **sorted_segments = NULL;
  1134. if ((ret = mxf_get_sorted_table_segments(mxf, &nb_sorted_segments, &sorted_segments)) ||
  1135. nb_sorted_segments <= 0) {
  1136. av_log(mxf->fc, AV_LOG_WARNING, "broken or empty index\n");
  1137. return 0;
  1138. }
  1139. /* sanity check and count unique BodySIDs/IndexSIDs */
  1140. for (i = 0; i < nb_sorted_segments; i++) {
  1141. if (i == 0 || sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid)
  1142. mxf->nb_index_tables++;
  1143. else if (sorted_segments[i-1]->body_sid != sorted_segments[i]->body_sid) {
  1144. av_log(mxf->fc, AV_LOG_ERROR, "found inconsistent BodySID\n");
  1145. ret = AVERROR_INVALIDDATA;
  1146. goto finish_decoding_index;
  1147. }
  1148. }
  1149. mxf->index_tables = av_mallocz_array(mxf->nb_index_tables,
  1150. sizeof(*mxf->index_tables));
  1151. if (!mxf->index_tables) {
  1152. av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate index tables\n");
  1153. ret = AVERROR(ENOMEM);
  1154. goto finish_decoding_index;
  1155. }
  1156. /* distribute sorted segments to index tables */
  1157. for (i = j = 0; i < nb_sorted_segments; i++) {
  1158. if (i != 0 && sorted_segments[i-1]->index_sid != sorted_segments[i]->index_sid) {
  1159. /* next IndexSID */
  1160. j++;
  1161. }
  1162. mxf->index_tables[j].nb_segments++;
  1163. }
  1164. for (i = j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) {
  1165. MXFIndexTable *t = &mxf->index_tables[j];
  1166. t->segments = av_mallocz_array(t->nb_segments,
  1167. sizeof(*t->segments));
  1168. if (!t->segments) {
  1169. av_log(mxf->fc, AV_LOG_ERROR, "failed to allocate IndexTableSegment"
  1170. " pointer array\n");
  1171. ret = AVERROR(ENOMEM);
  1172. goto finish_decoding_index;
  1173. }
  1174. if (sorted_segments[i]->index_start_position)
  1175. av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i starts at EditUnit %"PRId64" - seeking may not work as expected\n",
  1176. sorted_segments[i]->index_sid, sorted_segments[i]->index_start_position);
  1177. memcpy(t->segments, &sorted_segments[i], t->nb_segments * sizeof(MXFIndexTableSegment*));
  1178. t->index_sid = sorted_segments[i]->index_sid;
  1179. t->body_sid = sorted_segments[i]->body_sid;
  1180. if ((ret = mxf_compute_ptses_fake_index(mxf, t)) < 0)
  1181. goto finish_decoding_index;
  1182. /* fix zero IndexDurations */
  1183. for (k = 0; k < t->nb_segments; k++) {
  1184. if (t->segments[k]->index_duration)
  1185. continue;
  1186. if (t->nb_segments > 1)
  1187. av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has zero IndexDuration and there's more than one segment\n",
  1188. t->index_sid, k);
  1189. if (mxf->fc->nb_streams <= 0) {
  1190. av_log(mxf->fc, AV_LOG_WARNING, "no streams?\n");
  1191. break;
  1192. }
  1193. /* assume the first stream's duration is reasonable
  1194. * leave index_duration = 0 on further segments in case we have any (unlikely)
  1195. */
  1196. t->segments[k]->index_duration = mxf->fc->streams[0]->duration;
  1197. break;
  1198. }
  1199. }
  1200. ret = 0;
  1201. finish_decoding_index:
  1202. av_free(sorted_segments);
  1203. return ret;
  1204. }
  1205. static int mxf_is_intra_only(MXFDescriptor *d)
  1206. {
  1207. return mxf_get_codec_ul(mxf_intra_only_essence_container_uls,
  1208. &d->essence_container_ul)->id != AV_CODEC_ID_NONE ||
  1209. mxf_get_codec_ul(mxf_intra_only_picture_essence_coding_uls,
  1210. &d->essence_codec_ul)->id != AV_CODEC_ID_NONE;
  1211. }
  1212. static int mxf_parse_structural_metadata(MXFContext *mxf)
  1213. {
  1214. MXFPackage *material_package = NULL;
  1215. MXFPackage *temp_package = NULL;
  1216. int i, j, k, ret;
  1217. av_dlog(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
  1218. /* TODO: handle multiple material packages (OP3x) */
  1219. for (i = 0; i < mxf->packages_count; i++) {
  1220. material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
  1221. if (material_package) break;
  1222. }
  1223. if (!material_package) {
  1224. av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
  1225. return AVERROR_INVALIDDATA;
  1226. }
  1227. for (i = 0; i < material_package->tracks_count; i++) {
  1228. MXFPackage *source_package = NULL;
  1229. MXFTrack *material_track = NULL;
  1230. MXFTrack *source_track = NULL;
  1231. MXFTrack *temp_track = NULL;
  1232. MXFDescriptor *descriptor = NULL;
  1233. MXFStructuralComponent *component = NULL;
  1234. UID *essence_container_ul = NULL;
  1235. const MXFCodecUL *codec_ul = NULL;
  1236. const MXFCodecUL *container_ul = NULL;
  1237. const MXFCodecUL *pix_fmt_ul = NULL;
  1238. AVStream *st;
  1239. if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
  1240. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
  1241. continue;
  1242. }
  1243. if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
  1244. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
  1245. continue;
  1246. }
  1247. /* TODO: handle multiple source clips */
  1248. for (j = 0; j < material_track->sequence->structural_components_count; j++) {
  1249. /* TODO: handle timecode component */
  1250. component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
  1251. if (!component)
  1252. continue;
  1253. for (k = 0; k < mxf->packages_count; k++) {
  1254. temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
  1255. if (!temp_package)
  1256. continue;
  1257. if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
  1258. source_package = temp_package;
  1259. break;
  1260. }
  1261. }
  1262. if (!source_package) {
  1263. av_dlog(mxf->fc, "material track %d: no corresponding source package found\n", material_track->track_id);
  1264. break;
  1265. }
  1266. for (k = 0; k < source_package->tracks_count; k++) {
  1267. if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
  1268. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
  1269. ret = AVERROR_INVALIDDATA;
  1270. goto fail_and_free;
  1271. }
  1272. if (temp_track->track_id == component->source_track_id) {
  1273. source_track = temp_track;
  1274. break;
  1275. }
  1276. }
  1277. if (!source_track) {
  1278. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
  1279. break;
  1280. }
  1281. }
  1282. if (!source_track || !component)
  1283. continue;
  1284. if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
  1285. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
  1286. ret = AVERROR_INVALIDDATA;
  1287. goto fail_and_free;
  1288. }
  1289. /* 0001GL00.MXF.A1.mxf_opatom.mxf has the same SourcePackageID as 0001GL.MXF.V1.mxf_opatom.mxf
  1290. * This would result in both files appearing to have two streams. Work around this by sanity checking DataDefinition */
  1291. if (memcmp(material_track->sequence->data_definition_ul, source_track->sequence->data_definition_ul, 16)) {
  1292. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: DataDefinition mismatch\n", material_track->track_id);
  1293. continue;
  1294. }
  1295. st = avformat_new_stream(mxf->fc, NULL);
  1296. if (!st) {
  1297. av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
  1298. ret = AVERROR(ENOMEM);
  1299. goto fail_and_free;
  1300. }
  1301. st->id = source_track->track_id;
  1302. st->priv_data = source_track;
  1303. source_track->original_duration = st->duration = component->duration;
  1304. if (st->duration == -1)
  1305. st->duration = AV_NOPTS_VALUE;
  1306. st->start_time = component->start_position;
  1307. if (material_track->edit_rate.num <= 0 ||
  1308. material_track->edit_rate.den <= 0) {
  1309. av_log(mxf->fc, AV_LOG_WARNING,
  1310. "Invalid edit rate (%d/%d) found on stream #%d, "
  1311. "defaulting to 25/1\n",
  1312. material_track->edit_rate.num,
  1313. material_track->edit_rate.den, st->index);
  1314. material_track->edit_rate = (AVRational){25, 1};
  1315. }
  1316. avpriv_set_pts_info(st, 64, material_track->edit_rate.den, material_track->edit_rate.num);
  1317. /* ensure SourceTrack EditRate == MaterialTrack EditRate since only
  1318. * the former is accessible via st->priv_data */
  1319. source_track->edit_rate = material_track->edit_rate;
  1320. PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
  1321. codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
  1322. st->codec->codec_type = codec_ul->id;
  1323. source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
  1324. if (source_package->descriptor) {
  1325. if (source_package->descriptor->type == MultipleDescriptor) {
  1326. for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
  1327. MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
  1328. if (!sub_descriptor) {
  1329. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
  1330. continue;
  1331. }
  1332. if (sub_descriptor->linked_track_id == source_track->track_id) {
  1333. descriptor = sub_descriptor;
  1334. break;
  1335. }
  1336. }
  1337. } else if (source_package->descriptor->type == Descriptor)
  1338. descriptor = source_package->descriptor;
  1339. }
  1340. if (!descriptor) {
  1341. av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
  1342. continue;
  1343. }
  1344. PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
  1345. PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
  1346. essence_container_ul = &descriptor->essence_container_ul;
  1347. /* HACK: replacing the original key with mxf_encrypted_essence_container
  1348. * is not allowed according to s429-6, try to find correct information anyway */
  1349. if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
  1350. av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
  1351. for (k = 0; k < mxf->metadata_sets_count; k++) {
  1352. MXFMetadataSet *metadata = mxf->metadata_sets[k];
  1353. if (metadata->type == CryptoContext) {
  1354. essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
  1355. break;
  1356. }
  1357. }
  1358. }
  1359. /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
  1360. codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
  1361. st->codec->codec_id = codec_ul->id;
  1362. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1363. source_track->intra_only = mxf_is_intra_only(descriptor);
  1364. container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul);
  1365. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  1366. st->codec->codec_id = container_ul->id;
  1367. st->codec->width = descriptor->width;
  1368. /* Field height, not frame height */
  1369. st->codec->height = descriptor->height;
  1370. switch (descriptor->frame_layout) {
  1371. case SegmentedFrame:
  1372. /* This one is a weird layout I don't fully understand. */
  1373. av_log(mxf->fc, AV_LOG_INFO,
  1374. "SegmentedFrame layout isn't currently supported\n");
  1375. break;
  1376. case FullFrame:
  1377. st->codec->field_order = AV_FIELD_PROGRESSIVE;
  1378. break;
  1379. case OneField:
  1380. /* Every other line is stored and needs to be duplicated. */
  1381. av_log(mxf->fc, AV_LOG_INFO,
  1382. "OneField frame layout isn't currently supported\n");
  1383. break;
  1384. /* The correct thing to do here is fall through, but by
  1385. * breaking we might be able to decode some streams at half
  1386. * the vertical resolution, rather than not al all.
  1387. * It's also for compatibility with the old behavior. */
  1388. case SeparateFields:
  1389. case MixedFields:
  1390. switch (descriptor->field_dominance) {
  1391. case MXF_TFF:
  1392. st->codec->field_order = AV_FIELD_TT;
  1393. break;
  1394. case MXF_BFF:
  1395. st->codec->field_order = AV_FIELD_BB;
  1396. break;
  1397. default:
  1398. avpriv_request_sample(mxf->fc,
  1399. "Field dominance %d support",
  1400. descriptor->field_dominance);
  1401. break;
  1402. }
  1403. /* Turn field height into frame height. */
  1404. st->codec->height *= 2;
  1405. default:
  1406. av_log(mxf->fc, AV_LOG_INFO,
  1407. "Unknown frame layout type: %d\n",
  1408. descriptor->frame_layout);
  1409. }
  1410. if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
  1411. st->codec->pix_fmt = descriptor->pix_fmt;
  1412. if (st->codec->pix_fmt == AV_PIX_FMT_NONE) {
  1413. pix_fmt_ul = mxf_get_codec_ul(ff_mxf_pixel_format_uls,
  1414. &descriptor->essence_codec_ul);
  1415. st->codec->pix_fmt = pix_fmt_ul->id;
  1416. if (st->codec->pix_fmt == AV_PIX_FMT_NONE) {
  1417. /* support files created before RP224v10 by defaulting to UYVY422
  1418. if subsampling is 4:2:2 and component depth is 8-bit */
  1419. if (descriptor->horiz_subsampling == 2 &&
  1420. descriptor->vert_subsampling == 1 &&
  1421. descriptor->component_depth == 8) {
  1422. st->codec->pix_fmt = AV_PIX_FMT_UYVY422;
  1423. }
  1424. }
  1425. }
  1426. }
  1427. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1428. if (material_track->sequence->origin) {
  1429. char material_origin[3];
  1430. snprintf(material_origin, sizeof(material_origin), "%d", material_track->sequence->origin);
  1431. av_dict_set(&st->metadata, "material_track_origin", material_origin, 0);
  1432. }
  1433. if (source_track->sequence->origin) {
  1434. char source_origin[3];
  1435. snprintf(source_origin, sizeof(source_origin), "%d", source_track->sequence->origin);
  1436. av_dict_set(&st->metadata, "source_track_origin", source_origin, 0);
  1437. }
  1438. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  1439. container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
  1440. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  1441. st->codec->codec_id = container_ul->id;
  1442. st->codec->channels = descriptor->channels;
  1443. st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
  1444. if (descriptor->sample_rate.den > 0) {
  1445. st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
  1446. avpriv_set_pts_info(st, 64, descriptor->sample_rate.den, descriptor->sample_rate.num);
  1447. } else {
  1448. av_log(mxf->fc, AV_LOG_WARNING, "invalid sample rate (%d/%d) "
  1449. "found for stream #%d, time base forced to 1/48000\n",
  1450. descriptor->sample_rate.num, descriptor->sample_rate.den,
  1451. st->index);
  1452. avpriv_set_pts_info(st, 64, 1, 48000);
  1453. }
  1454. /* if duration is set, rescale it from EditRate to SampleRate */
  1455. if (st->duration != AV_NOPTS_VALUE)
  1456. st->duration = av_rescale_q(st->duration,
  1457. av_inv_q(material_track->edit_rate),
  1458. st->time_base);
  1459. /* TODO: implement AV_CODEC_ID_RAWAUDIO */
  1460. if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
  1461. if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
  1462. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  1463. else if (descriptor->bits_per_sample == 32)
  1464. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  1465. } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) {
  1466. if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
  1467. st->codec->codec_id = AV_CODEC_ID_PCM_S24BE;
  1468. else if (descriptor->bits_per_sample == 32)
  1469. st->codec->codec_id = AV_CODEC_ID_PCM_S32BE;
  1470. } else if (st->codec->codec_id == AV_CODEC_ID_MP2) {
  1471. st->need_parsing = AVSTREAM_PARSE_FULL;
  1472. }
  1473. } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
  1474. int codec_id = mxf_get_codec_ul(mxf_data_essence_container_uls,
  1475. essence_container_ul)->id;
  1476. if (codec_id >= 0 &&
  1477. codec_id < FF_ARRAY_ELEMS(mxf_data_essence_descriptor)) {
  1478. av_dict_set(&st->metadata, "data_type",
  1479. mxf_data_essence_descriptor[codec_id], 0);
  1480. }
  1481. }
  1482. if (descriptor->extradata) {
  1483. st->codec->extradata = av_mallocz(descriptor->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  1484. if (st->codec->extradata) {
  1485. memcpy(st->codec->extradata, descriptor->extradata, descriptor->extradata_size);
  1486. st->codec->extradata_size = descriptor->extradata_size;
  1487. }
  1488. } else if (st->codec->codec_id == AV_CODEC_ID_H264) {
  1489. ret = ff_generate_avci_extradata(st);
  1490. if (ret < 0)
  1491. return ret;
  1492. }
  1493. if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
  1494. /* TODO: decode timestamps */
  1495. st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
  1496. }
  1497. }
  1498. ret = 0;
  1499. fail_and_free:
  1500. return ret;
  1501. }
  1502. static int mxf_read_utf16_string(AVIOContext *pb, int size, char** str)
  1503. {
  1504. int ret;
  1505. size_t buf_size;
  1506. if (size < 0)
  1507. return AVERROR(EINVAL);
  1508. buf_size = size + size / 2 + 1;
  1509. *str = av_malloc(buf_size);
  1510. if (!*str)
  1511. return AVERROR(ENOMEM);
  1512. if ((ret = avio_get_str16be(pb, size, *str, buf_size)) < 0) {
  1513. av_freep(str);
  1514. return ret;
  1515. }
  1516. return ret;
  1517. }
  1518. static int mxf_uid_to_str(UID uid, char **str)
  1519. {
  1520. int i;
  1521. char *p;
  1522. p = *str = av_mallocz(sizeof(UID) * 2 + 4 + 1);
  1523. if (!p)
  1524. return AVERROR(ENOMEM);
  1525. for (i = 0; i < sizeof(UID); i++) {
  1526. snprintf(p, 2 + 1, "%.2x", uid[i]);
  1527. p += 2;
  1528. if (i == 3 || i == 5 || i == 7 || i == 9) {
  1529. snprintf(p, 1 + 1, "-");
  1530. p++;
  1531. }
  1532. }
  1533. return 0;
  1534. }
  1535. static int mxf_timestamp_to_str(uint64_t timestamp, char **str)
  1536. {
  1537. struct tm time = { 0 };
  1538. time.tm_year = (timestamp >> 48) - 1900;
  1539. time.tm_mon = (timestamp >> 40 & 0xFF) - 1;
  1540. time.tm_mday = (timestamp >> 32 & 0xFF);
  1541. time.tm_hour = (timestamp >> 24 & 0xFF);
  1542. time.tm_min = (timestamp >> 16 & 0xFF);
  1543. time.tm_sec = (timestamp >> 8 & 0xFF);
  1544. /* msvcrt versions of strftime calls the invalid parameter handler
  1545. * (aborting the process if one isn't set) if the parameters are out
  1546. * of range. */
  1547. time.tm_mon = av_clip(time.tm_mon, 0, 11);
  1548. time.tm_mday = av_clip(time.tm_mday, 1, 31);
  1549. time.tm_hour = av_clip(time.tm_hour, 0, 23);
  1550. time.tm_min = av_clip(time.tm_min, 0, 59);
  1551. time.tm_sec = av_clip(time.tm_sec, 0, 59);
  1552. *str = av_mallocz(32);
  1553. if (!*str)
  1554. return AVERROR(ENOMEM);
  1555. strftime(*str, 32, "%Y-%m-%d %H:%M:%S", &time);
  1556. return 0;
  1557. }
  1558. #define SET_STR_METADATA(pb, name, str) do { \
  1559. if ((ret = mxf_read_utf16_string(pb, size, &str)) < 0) \
  1560. return ret; \
  1561. av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
  1562. } while (0)
  1563. #define SET_UID_METADATA(pb, name, var, str) do { \
  1564. avio_read(pb, var, 16); \
  1565. if ((ret = mxf_uid_to_str(var, &str)) < 0) \
  1566. return ret; \
  1567. av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
  1568. } while (0)
  1569. #define SET_TS_METADATA(pb, name, var, str) do { \
  1570. var = avio_rb64(pb); \
  1571. if ((ret = mxf_timestamp_to_str(var, &str)) < 0) \
  1572. return ret; \
  1573. av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
  1574. } while (0)
  1575. static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset)
  1576. {
  1577. MXFContext *mxf = arg;
  1578. AVFormatContext *s = mxf->fc;
  1579. int ret;
  1580. UID uid = { 0 };
  1581. char *str = NULL;
  1582. uint64_t ts;
  1583. switch (tag) {
  1584. case 0x3C01:
  1585. SET_STR_METADATA(pb, "company_name", str);
  1586. break;
  1587. case 0x3C02:
  1588. SET_STR_METADATA(pb, "product_name", str);
  1589. break;
  1590. case 0x3C04:
  1591. SET_STR_METADATA(pb, "product_version", str);
  1592. break;
  1593. case 0x3C05:
  1594. SET_UID_METADATA(pb, "product_uid", uid, str);
  1595. break;
  1596. case 0x3C06:
  1597. SET_TS_METADATA(pb, "modification_date", ts, str);
  1598. break;
  1599. case 0x3C08:
  1600. SET_STR_METADATA(pb, "application_platform", str);
  1601. break;
  1602. case 0x3C09:
  1603. SET_UID_METADATA(pb, "generation_uid", uid, str);
  1604. break;
  1605. case 0x3C0A:
  1606. SET_UID_METADATA(pb, "uid", uid, str);
  1607. break;
  1608. }
  1609. return 0;
  1610. }
  1611. static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
  1612. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
  1613. // { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x2f,0x00 }, mxf_read_preface_pack },
  1614. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
  1615. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack },
  1616. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack },
  1617. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack },
  1618. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack },
  1619. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack },
  1620. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack },
  1621. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
  1622. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
  1623. { { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
  1624. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x30,0x00 }, mxf_read_identification_metadata },
  1625. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
  1626. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
  1627. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
  1628. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
  1629. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
  1630. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
  1631. { { 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 */
  1632. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
  1633. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
  1634. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG 2 Video */
  1635. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
  1636. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
  1637. { { 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 */
  1638. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
  1639. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
  1640. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
  1641. { { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
  1642. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
  1643. };
  1644. static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
  1645. {
  1646. AVIOContext *pb = mxf->fc->pb;
  1647. MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
  1648. uint64_t klv_end = avio_tell(pb) + klv->length;
  1649. if (!ctx)
  1650. return AVERROR(ENOMEM);
  1651. while (avio_tell(pb) + 4 < klv_end && !pb->eof_reached) {
  1652. int ret;
  1653. int tag = avio_rb16(pb);
  1654. int size = avio_rb16(pb); /* KLV specified by 0x53 */
  1655. uint64_t next = avio_tell(pb) + size;
  1656. UID uid = {0};
  1657. av_dlog(mxf->fc, "local tag %#04x size %d\n", tag, size);
  1658. if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
  1659. av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
  1660. continue;
  1661. }
  1662. if (tag > 0x7FFF) { /* dynamic tag */
  1663. int i;
  1664. for (i = 0; i < mxf->local_tags_count; i++) {
  1665. int local_tag = AV_RB16(mxf->local_tags+i*18);
  1666. if (local_tag == tag) {
  1667. memcpy(uid, mxf->local_tags+i*18+2, 16);
  1668. av_dlog(mxf->fc, "local tag %#04x\n", local_tag);
  1669. PRINT_KEY(mxf->fc, "uid", uid);
  1670. }
  1671. }
  1672. }
  1673. if (ctx_size && tag == 0x3C0A)
  1674. avio_read(pb, ctx->uid, 16);
  1675. else if ((ret = read_child(ctx, pb, tag, size, uid, -1)) < 0)
  1676. return ret;
  1677. /* Accept the 64k local set limit being exceeded (Avid). Don't accept
  1678. * it extending past the end of the KLV though (zzuf5.mxf). */
  1679. if (avio_tell(pb) > klv_end) {
  1680. if (ctx_size)
  1681. av_free(ctx);
  1682. av_log(mxf->fc, AV_LOG_ERROR,
  1683. "local tag %#04x extends past end of local set @ %#"PRIx64"\n",
  1684. tag, klv->offset);
  1685. return AVERROR_INVALIDDATA;
  1686. } else if (avio_tell(pb) <= next) /* only seek forward, else this can loop for a long time */
  1687. avio_seek(pb, next, SEEK_SET);
  1688. }
  1689. if (ctx_size) ctx->type = type;
  1690. return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
  1691. }
  1692. /**
  1693. * Seeks to the previous partition, if possible
  1694. * @return <= 0 if we should stop parsing, > 0 if we should keep going
  1695. */
  1696. static int mxf_seek_to_previous_partition(MXFContext *mxf)
  1697. {
  1698. AVIOContext *pb = mxf->fc->pb;
  1699. if (!mxf->current_partition ||
  1700. mxf->run_in + mxf->current_partition->previous_partition <= mxf->last_forward_tell)
  1701. return 0; /* we've parsed all partitions */
  1702. /* seek to previous partition */
  1703. avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET);
  1704. mxf->current_partition = NULL;
  1705. av_dlog(mxf->fc, "seeking to previous partition\n");
  1706. return 1;
  1707. }
  1708. /**
  1709. * Called when essence is encountered
  1710. * @return <= 0 if we should stop parsing, > 0 if we should keep going
  1711. */
  1712. static int mxf_parse_handle_essence(MXFContext *mxf)
  1713. {
  1714. AVIOContext *pb = mxf->fc->pb;
  1715. int64_t ret;
  1716. if (mxf->parsing_backward) {
  1717. return mxf_seek_to_previous_partition(mxf);
  1718. } else {
  1719. uint64_t offset = mxf->footer_partition ? mxf->footer_partition
  1720. : mxf->last_partition;
  1721. if (!offset) {
  1722. av_dlog(mxf->fc, "no last partition\n");
  1723. return 0;
  1724. }
  1725. av_dlog(mxf->fc, "seeking to last partition\n");
  1726. /* remember where we were so we don't end up seeking further back than this */
  1727. mxf->last_forward_tell = avio_tell(pb);
  1728. if (!pb->seekable) {
  1729. av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing last partition\n");
  1730. return -1;
  1731. }
  1732. /* seek to last partition and parse backward */
  1733. if ((ret = avio_seek(pb, mxf->run_in + offset, SEEK_SET)) < 0) {
  1734. av_log(mxf->fc, AV_LOG_ERROR,
  1735. "failed to seek to last partition @ 0x%" PRIx64
  1736. " (%"PRId64") - partial file?\n",
  1737. mxf->run_in + offset, ret);
  1738. return ret;
  1739. }
  1740. mxf->current_partition = NULL;
  1741. mxf->parsing_backward = 1;
  1742. }
  1743. return 1;
  1744. }
  1745. /**
  1746. * Called when the next partition or EOF is encountered
  1747. * @return <= 0 if we should stop parsing, > 0 if we should keep going
  1748. */
  1749. static int mxf_parse_handle_partition_or_eof(MXFContext *mxf)
  1750. {
  1751. return mxf->parsing_backward ? mxf_seek_to_previous_partition(mxf) : 1;
  1752. }
  1753. /**
  1754. * Figure out the proper offset and length of the essence container
  1755. * in each partition
  1756. */
  1757. static void mxf_compute_essence_containers(MXFContext *mxf)
  1758. {
  1759. int x;
  1760. /* everything is already correct */
  1761. if (mxf->op == OPAtom)
  1762. return;
  1763. for (x = 0; x < mxf->partitions_count; x++) {
  1764. MXFPartition *p = &mxf->partitions[x];
  1765. if (!p->body_sid)
  1766. continue; /* BodySID == 0 -> no essence */
  1767. if (x >= mxf->partitions_count - 1)
  1768. break; /* last partition - can't compute length (and we don't need to) */
  1769. /* essence container spans to the next partition */
  1770. p->essence_length = mxf->partitions[x+1].this_partition - p->essence_offset;
  1771. if (p->essence_length < 0) {
  1772. /* next ThisPartition < essence_offset */
  1773. p->essence_length = 0;
  1774. av_log(mxf->fc, AV_LOG_ERROR,
  1775. "partition %i: bad ThisPartition = %"PRIX64"\n",
  1776. x+1, mxf->partitions[x+1].this_partition);
  1777. }
  1778. }
  1779. }
  1780. static int64_t round_to_kag(int64_t position, int kag_size)
  1781. {
  1782. /* TODO: account for run-in? the spec isn't clear whether KAG should account for it */
  1783. /* NOTE: kag_size may be any integer between 1 - 2^10 */
  1784. int64_t ret = (position / kag_size) * kag_size;
  1785. return ret == position ? ret : ret + kag_size;
  1786. }
  1787. static inline void compute_partition_essence_offset(AVFormatContext *s,
  1788. MXFContext *mxf,
  1789. KLVPacket *klv)
  1790. {
  1791. MXFPartition *cur_part = mxf->current_partition;
  1792. /* for OP1a we compute essence_offset
  1793. * for OPAtom we point essence_offset after the KL
  1794. * (usually op1a_essence_offset + 20 or 25)
  1795. * TODO: for OP1a we could eliminate this entire if statement, always
  1796. * stopping parsing at op1a_essence_offset
  1797. * for OPAtom we still need the actual essence_offset though
  1798. * (the KL's length can vary)
  1799. */
  1800. int64_t op1a_essence_offset =
  1801. round_to_kag(cur_part->this_partition + cur_part->pack_length,
  1802. cur_part->kag_size) +
  1803. round_to_kag(cur_part->header_byte_count, cur_part->kag_size) +
  1804. round_to_kag(cur_part->index_byte_count, cur_part->kag_size);
  1805. if (mxf->op == OPAtom) {
  1806. /* point essence_offset to the actual data
  1807. * OPAtom has all the essence in one big KLV
  1808. */
  1809. cur_part->essence_offset = avio_tell(s->pb);
  1810. cur_part->essence_length = klv->length;
  1811. } else {
  1812. /* NOTE: op1a_essence_offset may be less than to klv.offset
  1813. * (C0023S01.mxf) */
  1814. cur_part->essence_offset = op1a_essence_offset;
  1815. }
  1816. }
  1817. static int is_pcm(enum AVCodecID codec_id)
  1818. {
  1819. /* we only care about "normal" PCM codecs until we get samples */
  1820. return codec_id >= AV_CODEC_ID_PCM_S16LE && codec_id < AV_CODEC_ID_PCM_S24DAUD;
  1821. }
  1822. /**
  1823. * Deal with the case where for some audio atoms EditUnitByteCount is
  1824. * very small (2, 4..). In those cases we should read more than one
  1825. * sample per call to mxf_read_packet().
  1826. */
  1827. static void mxf_handle_small_eubc(AVFormatContext *s)
  1828. {
  1829. MXFContext *mxf = s->priv_data;
  1830. /* assuming non-OPAtom == frame wrapped
  1831. * no sane writer would wrap 2 byte PCM packets with 20 byte headers.. */
  1832. if (mxf->op != OPAtom)
  1833. return;
  1834. /* expect PCM with exactly one index table segment and a small (< 32) EUBC */
  1835. if (s->nb_streams != 1 ||
  1836. s->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
  1837. !is_pcm(s->streams[0]->codec->codec_id) ||
  1838. mxf->nb_index_tables != 1 ||
  1839. mxf->index_tables[0].nb_segments != 1 ||
  1840. mxf->index_tables[0].segments[0]->edit_unit_byte_count >= 32)
  1841. return;
  1842. /* arbitrarily default to 48 kHz PAL audio frame size */
  1843. /* TODO: We could compute this from the ratio between the audio
  1844. * and video edit rates for 48 kHz NTSC we could use the
  1845. * 1802-1802-1802-1802-1801 pattern. */
  1846. mxf->edit_units_per_packet = 1920;
  1847. }
  1848. static void mxf_read_random_index_pack(AVFormatContext *s)
  1849. {
  1850. MXFContext *mxf = s->priv_data;
  1851. uint32_t length;
  1852. int64_t file_size;
  1853. KLVPacket klv;
  1854. if (!s->pb->seekable)
  1855. return;
  1856. file_size = avio_size(s->pb);
  1857. avio_seek(s->pb, file_size - 4, SEEK_SET);
  1858. length = avio_rb32(s->pb);
  1859. if (length <= 32 || length >= FFMIN(file_size, INT_MAX))
  1860. goto end;
  1861. avio_seek(s->pb, file_size - length, SEEK_SET);
  1862. if (klv_read_packet(&klv, s->pb) < 0 ||
  1863. !IS_KLV_KEY(klv.key, mxf_random_index_pack_key) ||
  1864. klv.length != length - 20)
  1865. goto end;
  1866. avio_skip(s->pb, klv.length - 12);
  1867. mxf->last_partition = avio_rb64(s->pb);
  1868. end:
  1869. avio_seek(s->pb, mxf->run_in, SEEK_SET);
  1870. }
  1871. static int mxf_read_header(AVFormatContext *s)
  1872. {
  1873. MXFContext *mxf = s->priv_data;
  1874. KLVPacket klv;
  1875. int64_t essence_offset = 0;
  1876. int ret;
  1877. mxf->last_forward_tell = INT64_MAX;
  1878. mxf->edit_units_per_packet = 1;
  1879. if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
  1880. av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
  1881. return AVERROR_INVALIDDATA;
  1882. }
  1883. avio_seek(s->pb, -14, SEEK_CUR);
  1884. mxf->fc = s;
  1885. mxf->run_in = avio_tell(s->pb);
  1886. mxf_read_random_index_pack(s);
  1887. while (!s->pb->eof_reached) {
  1888. const MXFMetadataReadTableEntry *metadata;
  1889. if (klv_read_packet(&klv, s->pb) < 0) {
  1890. /* EOF - seek to previous partition or stop */
  1891. if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
  1892. break;
  1893. else
  1894. continue;
  1895. }
  1896. PRINT_KEY(s, "read header", klv.key);
  1897. av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
  1898. if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
  1899. IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
  1900. IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) ||
  1901. IS_KLV_KEY(klv.key, mxf_system_item_key)) {
  1902. if (!mxf->current_partition) {
  1903. av_log(mxf->fc, AV_LOG_ERROR,
  1904. "found essence prior to first PartitionPack\n");
  1905. return AVERROR_INVALIDDATA;
  1906. }
  1907. if (!mxf->current_partition->essence_offset) {
  1908. compute_partition_essence_offset(s, mxf, &klv);
  1909. }
  1910. if (!essence_offset)
  1911. essence_offset = klv.offset;
  1912. /* seek to footer, previous partition or stop */
  1913. if (mxf_parse_handle_essence(mxf) <= 0)
  1914. break;
  1915. continue;
  1916. } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) &&
  1917. klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) {
  1918. /* next partition pack - keep going, seek to previous partition or stop */
  1919. if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
  1920. break;
  1921. else if (mxf->parsing_backward)
  1922. continue;
  1923. /* we're still parsing forward. proceed to parsing this partition pack */
  1924. }
  1925. for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
  1926. if (IS_KLV_KEY(klv.key, metadata->key)) {
  1927. int res;
  1928. if (klv.key[5] == 0x53) {
  1929. res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);
  1930. } else {
  1931. uint64_t next = avio_tell(s->pb) + klv.length;
  1932. res = metadata->read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);
  1933. /* only seek forward, else this can loop for a long time */
  1934. if (avio_tell(s->pb) > next) {
  1935. av_log(s, AV_LOG_ERROR, "read past end of KLV @ %#"PRIx64"\n",
  1936. klv.offset);
  1937. return AVERROR_INVALIDDATA;
  1938. }
  1939. avio_seek(s->pb, next, SEEK_SET);
  1940. }
  1941. if (res < 0) {
  1942. av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
  1943. return res;
  1944. }
  1945. break;
  1946. }
  1947. }
  1948. if (!metadata->read)
  1949. avio_skip(s->pb, klv.length);
  1950. }
  1951. /* FIXME avoid seek */
  1952. if (!essence_offset) {
  1953. av_log(s, AV_LOG_ERROR, "no essence\n");
  1954. return AVERROR_INVALIDDATA;
  1955. }
  1956. avio_seek(s->pb, essence_offset, SEEK_SET);
  1957. mxf_compute_essence_containers(mxf);
  1958. /* we need to do this before computing the index tables
  1959. * to be able to fill in zero IndexDurations with st->duration */
  1960. if ((ret = mxf_parse_structural_metadata(mxf)) < 0)
  1961. return ret;
  1962. if ((ret = mxf_compute_index_tables(mxf)) < 0)
  1963. return ret;
  1964. if (mxf->nb_index_tables > 1) {
  1965. /* TODO: look up which IndexSID to use via EssenceContainerData */
  1966. av_log(mxf->fc, AV_LOG_INFO, "got %i index tables - only the first one (IndexSID %i) will be used\n",
  1967. mxf->nb_index_tables, mxf->index_tables[0].index_sid);
  1968. } else if (mxf->nb_index_tables == 0 && mxf->op == OPAtom) {
  1969. av_log(mxf->fc, AV_LOG_ERROR, "cannot demux OPAtom without an index\n");
  1970. return AVERROR_INVALIDDATA;
  1971. }
  1972. mxf_handle_small_eubc(s);
  1973. return 0;
  1974. }
  1975. /**
  1976. * Sets mxf->current_edit_unit based on what offset we're currently at.
  1977. * @return next_ofs if OK, <0 on error
  1978. */
  1979. static int64_t mxf_set_current_edit_unit(MXFContext *mxf, int64_t current_offset)
  1980. {
  1981. int64_t last_ofs = -1, next_ofs = -1;
  1982. MXFIndexTable *t = &mxf->index_tables[0];
  1983. /* this is called from the OP1a demuxing logic, which means there
  1984. * may be no index tables */
  1985. if (mxf->nb_index_tables <= 0)
  1986. return -1;
  1987. /* find mxf->current_edit_unit so that the next edit unit starts ahead
  1988. * of current_offset */
  1989. while (mxf->current_edit_unit >= 0) {
  1990. if (mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit + 1,
  1991. NULL, &next_ofs, 0) < 0)
  1992. return -1;
  1993. if (next_ofs <= last_ofs) {
  1994. /* large next_ofs didn't change or current_edit_unit wrapped
  1995. * around this fixes the infinite loop on zzuf3.mxf */
  1996. av_log(mxf->fc, AV_LOG_ERROR,
  1997. "next_ofs didn't change. not deriving packet timestamps\n");
  1998. return -1;
  1999. }
  2000. if (next_ofs > current_offset)
  2001. break;
  2002. last_ofs = next_ofs;
  2003. mxf->current_edit_unit++;
  2004. }
  2005. /* not checking mxf->current_edit_unit >= t->nb_ptses here since CBR files
  2006. * may lack IndexEntryArrays */
  2007. if (mxf->current_edit_unit < 0)
  2008. return -1;
  2009. return next_ofs;
  2010. }
  2011. static int mxf_compute_sample_count(MXFContext *mxf, int stream_index,
  2012. uint64_t *sample_count)
  2013. {
  2014. int i, total = 0, size = 0;
  2015. AVStream *st = mxf->fc->streams[stream_index];
  2016. MXFTrack *track = st->priv_data;
  2017. AVRational time_base = av_inv_q(track->edit_rate);
  2018. AVRational sample_rate = av_inv_q(st->time_base);
  2019. const MXFSamplesPerFrame *spf = NULL;
  2020. if ((sample_rate.num / sample_rate.den) == 48000)
  2021. spf = ff_mxf_get_samples_per_frame(mxf->fc, time_base);
  2022. if (!spf) {
  2023. int remainder = (sample_rate.num * time_base.num) %
  2024. (time_base.den * sample_rate.den);
  2025. *sample_count = av_q2d(av_mul_q((AVRational){mxf->current_edit_unit, 1},
  2026. av_mul_q(sample_rate, time_base)));
  2027. if (remainder)
  2028. av_log(mxf->fc, AV_LOG_WARNING,
  2029. "seeking detected on stream #%d with time base (%d/%d) and "
  2030. "sample rate (%d/%d), audio pts won't be accurate.\n",
  2031. stream_index, time_base.num, time_base.den,
  2032. sample_rate.num, sample_rate.den);
  2033. return 0;
  2034. }
  2035. while (spf->samples_per_frame[size]) {
  2036. total += spf->samples_per_frame[size];
  2037. size++;
  2038. }
  2039. if (!size)
  2040. return 0;
  2041. *sample_count = (mxf->current_edit_unit / size) * (uint64_t)total;
  2042. for (i = 0; i < mxf->current_edit_unit % size; i++) {
  2043. *sample_count += spf->samples_per_frame[i];
  2044. }
  2045. return 0;
  2046. }
  2047. static int mxf_set_audio_pts(MXFContext *mxf, AVCodecContext *codec,
  2048. AVPacket *pkt)
  2049. {
  2050. MXFTrack *track = mxf->fc->streams[pkt->stream_index]->priv_data;
  2051. int64_t bits_per_sample = av_get_bits_per_sample(codec->codec_id);
  2052. pkt->pts = track->sample_count;
  2053. if (codec->channels <= 0 || codec->channels * bits_per_sample < 8)
  2054. return AVERROR_INVALIDDATA;
  2055. track->sample_count += pkt->size / (codec->channels * bits_per_sample / 8);
  2056. return 0;
  2057. }
  2058. static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt)
  2059. {
  2060. KLVPacket klv;
  2061. MXFContext *mxf = s->priv_data;
  2062. int ret;
  2063. while (!s->pb->eof_reached) {
  2064. if ((ret = klv_read_packet(&klv, s->pb)) < 0)
  2065. return ret;
  2066. PRINT_KEY(s, "read packet", klv.key);
  2067. av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
  2068. if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
  2069. ret = mxf_decrypt_triplet(s, pkt, &klv);
  2070. if (ret < 0) {
  2071. av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
  2072. return ret;
  2073. }
  2074. return 0;
  2075. }
  2076. if (IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
  2077. IS_KLV_KEY(klv.key, mxf_avid_essence_element_key)) {
  2078. int index = mxf_get_stream_index(s, &klv);
  2079. int64_t next_ofs, next_klv;
  2080. AVStream *st;
  2081. MXFTrack *track;
  2082. AVCodecContext *codec;
  2083. if (index < 0) {
  2084. av_log(s, AV_LOG_ERROR,
  2085. "error getting stream index %"PRIu32"\n",
  2086. AV_RB32(klv.key + 12));
  2087. goto skip;
  2088. }
  2089. st = s->streams[index];
  2090. track = st->priv_data;
  2091. if (s->streams[index]->discard == AVDISCARD_ALL)
  2092. goto skip;
  2093. next_klv = avio_tell(s->pb) + klv.length;
  2094. next_ofs = mxf_set_current_edit_unit(mxf, klv.offset);
  2095. if (next_ofs >= 0 && next_klv > next_ofs) {
  2096. /* if this check is hit then it's possible OPAtom was treated
  2097. * as OP1a truncate the packet since it's probably very large
  2098. * (>2 GiB is common) */
  2099. avpriv_request_sample(s,
  2100. "OPAtom misinterpreted as OP1a?"
  2101. "KLV for edit unit %i extending into "
  2102. "next edit unit",
  2103. mxf->current_edit_unit);
  2104. klv.length = next_ofs - avio_tell(s->pb);
  2105. }
  2106. /* check for 8 channels AES3 element */
  2107. if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
  2108. ret = mxf_get_d10_aes3_packet(s->pb, s->streams[index],
  2109. pkt, klv.length);
  2110. if (ret < 0) {
  2111. av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
  2112. return ret;
  2113. }
  2114. } else {
  2115. ret = av_get_packet(s->pb, pkt, klv.length);
  2116. if (ret < 0)
  2117. return ret;
  2118. }
  2119. pkt->stream_index = index;
  2120. pkt->pos = klv.offset;
  2121. codec = s->streams[index]->codec;
  2122. if (codec->codec_type == AVMEDIA_TYPE_VIDEO && next_ofs >= 0) {
  2123. /* mxf->current_edit_unit good - see if we have an
  2124. * index table to derive timestamps from */
  2125. MXFIndexTable *t = &mxf->index_tables[0];
  2126. if (mxf->nb_index_tables >= 1 &&
  2127. mxf->current_edit_unit < t->nb_ptses) {
  2128. pkt->dts = mxf->current_edit_unit + t->first_dts;
  2129. pkt->pts = t->ptses[mxf->current_edit_unit];
  2130. } else if (track->intra_only) {
  2131. /* intra-only -> PTS = EditUnit.
  2132. * let utils.c figure out DTS since it can be
  2133. * < PTS if low_delay = 0 (Sony IMX30) */
  2134. pkt->pts = mxf->current_edit_unit;
  2135. }
  2136. } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  2137. ret = mxf_set_audio_pts(mxf, codec, pkt);
  2138. if (ret < 0)
  2139. return ret;
  2140. }
  2141. /* seek for truncated packets */
  2142. avio_seek(s->pb, next_klv, SEEK_SET);
  2143. return 0;
  2144. } else
  2145. skip:
  2146. avio_skip(s->pb, klv.length);
  2147. }
  2148. return AVERROR_EOF;
  2149. }
  2150. static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
  2151. {
  2152. MXFContext *mxf = s->priv_data;
  2153. int ret, size;
  2154. int64_t ret64, pos, next_pos;
  2155. AVStream *st;
  2156. MXFIndexTable *t;
  2157. int edit_units;
  2158. if (mxf->op != OPAtom)
  2159. return mxf_read_packet_old(s, pkt);
  2160. /* OPAtom - clip wrapped demuxing */
  2161. /* NOTE: mxf_read_header() makes sure nb_index_tables > 0 for OPAtom */
  2162. st = s->streams[0];
  2163. t = &mxf->index_tables[0];
  2164. if (mxf->current_edit_unit >= st->duration)
  2165. return AVERROR_EOF;
  2166. edit_units = FFMIN(mxf->edit_units_per_packet, st->duration - mxf->current_edit_unit);
  2167. if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit, NULL, &pos, 1)) < 0)
  2168. return ret;
  2169. /* compute size by finding the next edit unit or the end of the essence container
  2170. * not pretty, but it works */
  2171. if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit + edit_units, NULL, &next_pos, 0)) < 0 &&
  2172. (next_pos = mxf_essence_container_end(mxf, t->body_sid)) <= 0) {
  2173. av_log(s, AV_LOG_ERROR, "unable to compute the size of the last packet\n");
  2174. return AVERROR_INVALIDDATA;
  2175. }
  2176. if ((size = next_pos - pos) <= 0) {
  2177. av_log(s, AV_LOG_ERROR, "bad size: %i\n", size);
  2178. return AVERROR_INVALIDDATA;
  2179. }
  2180. if ((ret64 = avio_seek(s->pb, pos, SEEK_SET)) < 0)
  2181. return ret64;
  2182. if ((ret = av_get_packet(s->pb, pkt, size)) != size)
  2183. return ret < 0 ? ret : AVERROR_EOF;
  2184. pkt->stream_index = 0;
  2185. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && t->ptses &&
  2186. mxf->current_edit_unit >= 0 && mxf->current_edit_unit < t->nb_ptses) {
  2187. pkt->dts = mxf->current_edit_unit + t->first_dts;
  2188. pkt->pts = t->ptses[mxf->current_edit_unit];
  2189. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  2190. int ret = mxf_set_audio_pts(mxf, st->codec, pkt);
  2191. if (ret < 0)
  2192. return ret;
  2193. }
  2194. mxf->current_edit_unit += edit_units;
  2195. return 0;
  2196. }
  2197. static int mxf_read_close(AVFormatContext *s)
  2198. {
  2199. MXFContext *mxf = s->priv_data;
  2200. MXFIndexTableSegment *seg;
  2201. int i;
  2202. av_freep(&mxf->packages_refs);
  2203. for (i = 0; i < s->nb_streams; i++)
  2204. s->streams[i]->priv_data = NULL;
  2205. for (i = 0; i < mxf->metadata_sets_count; i++) {
  2206. switch (mxf->metadata_sets[i]->type) {
  2207. case Descriptor:
  2208. av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->extradata);
  2209. break;
  2210. case MultipleDescriptor:
  2211. av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
  2212. break;
  2213. case Sequence:
  2214. av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
  2215. break;
  2216. case SourcePackage:
  2217. case MaterialPackage:
  2218. av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
  2219. break;
  2220. case IndexTableSegment:
  2221. seg = (MXFIndexTableSegment *)mxf->metadata_sets[i];
  2222. av_freep(&seg->temporal_offset_entries);
  2223. av_freep(&seg->flag_entries);
  2224. av_freep(&seg->stream_offset_entries);
  2225. break;
  2226. default:
  2227. break;
  2228. }
  2229. av_freep(&mxf->metadata_sets[i]);
  2230. }
  2231. av_freep(&mxf->partitions);
  2232. av_freep(&mxf->metadata_sets);
  2233. av_freep(&mxf->aesc);
  2234. av_freep(&mxf->local_tags);
  2235. for (i = 0; i < mxf->nb_index_tables; i++) {
  2236. av_freep(&mxf->index_tables[i].segments);
  2237. av_freep(&mxf->index_tables[i].ptses);
  2238. av_freep(&mxf->index_tables[i].fake_index);
  2239. }
  2240. av_freep(&mxf->index_tables);
  2241. return 0;
  2242. }
  2243. static int mxf_probe(AVProbeData *p) {
  2244. uint8_t *bufp = p->buf;
  2245. uint8_t *end = p->buf + p->buf_size;
  2246. if (p->buf_size < sizeof(mxf_header_partition_pack_key))
  2247. return 0;
  2248. /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
  2249. end -= sizeof(mxf_header_partition_pack_key);
  2250. for (; bufp < end; bufp++) {
  2251. if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
  2252. return AVPROBE_SCORE_MAX;
  2253. }
  2254. return 0;
  2255. }
  2256. /* rudimentary byte seek */
  2257. /* XXX: use MXF Index */
  2258. static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2259. {
  2260. AVStream *st = s->streams[stream_index];
  2261. int64_t seconds;
  2262. MXFContext* mxf = s->priv_data;
  2263. int64_t seekpos;
  2264. int i, ret;
  2265. MXFIndexTable *t;
  2266. MXFTrack *source_track = st->priv_data;
  2267. /* if audio then truncate sample_time to EditRate */
  2268. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  2269. sample_time = av_rescale_q(sample_time, st->time_base,
  2270. av_inv_q(source_track->edit_rate));
  2271. if (mxf->nb_index_tables <= 0) {
  2272. if (!s->bit_rate)
  2273. return AVERROR_INVALIDDATA;
  2274. if (sample_time < 0)
  2275. sample_time = 0;
  2276. seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
  2277. seekpos = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
  2278. if (seekpos < 0)
  2279. return seekpos;
  2280. ff_update_cur_dts(s, st, sample_time);
  2281. mxf->current_edit_unit = sample_time;
  2282. } else {
  2283. t = &mxf->index_tables[0];
  2284. /* clamp above zero, else ff_index_search_timestamp() returns negative
  2285. * this also means we allow seeking before the start */
  2286. sample_time = FFMAX(sample_time, 0);
  2287. if (t->fake_index) {
  2288. /* behave as if we have a proper index */
  2289. if ((sample_time = ff_index_search_timestamp(t->fake_index, t->nb_ptses, sample_time, flags)) < 0)
  2290. return sample_time;
  2291. } else {
  2292. /* no IndexEntryArray (one or more CBR segments)
  2293. * make sure we don't seek past the end */
  2294. sample_time = FFMIN(sample_time, source_track->original_duration - 1);
  2295. }
  2296. if ((ret = mxf_edit_unit_absolute_offset(mxf, t, sample_time, &sample_time, &seekpos, 1)) << 0)
  2297. return ret;
  2298. ff_update_cur_dts(s, st, sample_time);
  2299. mxf->current_edit_unit = sample_time;
  2300. avio_seek(s->pb, seekpos, SEEK_SET);
  2301. }
  2302. // Update all tracks sample count
  2303. for (i = 0; i < s->nb_streams; i++) {
  2304. AVStream *cur_st = s->streams[i];
  2305. MXFTrack *cur_track = cur_st->priv_data;
  2306. uint64_t current_sample_count = 0;
  2307. if (cur_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  2308. ret = mxf_compute_sample_count(mxf, i, &current_sample_count);
  2309. if (ret < 0)
  2310. return ret;
  2311. cur_track->sample_count = current_sample_count;
  2312. }
  2313. }
  2314. return 0;
  2315. }
  2316. AVInputFormat ff_mxf_demuxer = {
  2317. .name = "mxf",
  2318. .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
  2319. .priv_data_size = sizeof(MXFContext),
  2320. .read_probe = mxf_probe,
  2321. .read_header = mxf_read_header,
  2322. .read_packet = mxf_read_packet,
  2323. .read_close = mxf_read_close,
  2324. .read_seek = mxf_read_seek,
  2325. };