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.

1616 lines
61KB

  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. //#define DEBUG
  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,
  58. OP1b,
  59. OP1c,
  60. OP2a,
  61. OP2b,
  62. OP2c,
  63. OP3a,
  64. OP3b,
  65. OP3c,
  66. OPAtom,
  67. } MXFOP;
  68. typedef struct {
  69. int closed;
  70. int complete;
  71. MXFPartitionType type;
  72. uint64_t previous_partition;
  73. int index_sid;
  74. int body_sid;
  75. int64_t this_partition;
  76. } MXFPartition;
  77. typedef struct {
  78. UID uid;
  79. enum MXFMetadataSetType type;
  80. UID source_container_ul;
  81. } MXFCryptoContext;
  82. typedef struct {
  83. UID uid;
  84. enum MXFMetadataSetType type;
  85. UID source_package_uid;
  86. UID data_definition_ul;
  87. int64_t duration;
  88. int64_t start_position;
  89. int source_track_id;
  90. } MXFStructuralComponent;
  91. typedef struct {
  92. UID uid;
  93. enum MXFMetadataSetType type;
  94. UID data_definition_ul;
  95. UID *structural_components_refs;
  96. int structural_components_count;
  97. int64_t duration;
  98. } MXFSequence;
  99. typedef struct {
  100. UID uid;
  101. enum MXFMetadataSetType type;
  102. MXFSequence *sequence; /* mandatory, and only one */
  103. UID sequence_ref;
  104. int track_id;
  105. uint8_t track_number[4];
  106. AVRational edit_rate;
  107. } MXFTrack;
  108. typedef struct {
  109. UID uid;
  110. enum MXFMetadataSetType type;
  111. UID essence_container_ul;
  112. UID essence_codec_ul;
  113. AVRational sample_rate;
  114. AVRational aspect_ratio;
  115. int width;
  116. int height;
  117. int channels;
  118. int bits_per_sample;
  119. UID *sub_descriptors_refs;
  120. int sub_descriptors_count;
  121. int linked_track_id;
  122. uint8_t *extradata;
  123. int extradata_size;
  124. enum PixelFormat pix_fmt;
  125. } MXFDescriptor;
  126. typedef struct {
  127. UID uid;
  128. enum MXFMetadataSetType type;
  129. int edit_unit_byte_count;
  130. int index_sid;
  131. int body_sid;
  132. int slice_count;
  133. AVRational index_edit_rate;
  134. uint64_t index_start_position;
  135. uint64_t index_duration;
  136. int *slice;
  137. int *element_delta;
  138. int nb_delta_entries;
  139. int *flag_entries;
  140. uint64_t *stream_offset_entries;
  141. uint32_t **slice_offset_entries;
  142. int nb_index_entries;
  143. } MXFIndexTableSegment;
  144. typedef struct {
  145. UID uid;
  146. enum MXFMetadataSetType type;
  147. UID package_uid;
  148. UID *tracks_refs;
  149. int tracks_count;
  150. MXFDescriptor *descriptor; /* only one */
  151. UID descriptor_ref;
  152. } MXFPackage;
  153. typedef struct {
  154. UID uid;
  155. enum MXFMetadataSetType type;
  156. } MXFMetadataSet;
  157. typedef struct {
  158. MXFPartition *partitions;
  159. unsigned partitions_count;
  160. MXFOP op;
  161. UID *packages_refs;
  162. int packages_count;
  163. MXFMetadataSet **metadata_sets;
  164. int metadata_sets_count;
  165. AVFormatContext *fc;
  166. struct AVAES *aesc;
  167. uint8_t *local_tags;
  168. int local_tags_count;
  169. uint64_t footer_partition;
  170. int system_item;
  171. int64_t essence_offset;
  172. int first_essence_kl_length;
  173. int64_t first_essence_length;
  174. KLVPacket current_klv_data;
  175. int current_klv_index;
  176. int run_in;
  177. MXFPartition *current_partition;
  178. int parsing_backward;
  179. int64_t last_forward_tell;
  180. int last_forward_partition;
  181. } MXFContext;
  182. enum MXFWrappingScheme {
  183. Frame,
  184. Clip,
  185. };
  186. typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid);
  187. typedef struct {
  188. const UID key;
  189. MXFMetadataReadFunc *read;
  190. int ctx_size;
  191. enum MXFMetadataSetType type;
  192. } MXFMetadataReadTableEntry;
  193. /* partial keys to match */
  194. static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
  195. static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
  196. static const uint8_t mxf_system_item_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04 };
  197. static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
  198. /* complete keys to match */
  199. 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 };
  200. static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
  201. static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
  202. static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
  203. #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
  204. static int64_t klv_decode_ber_length(AVIOContext *pb)
  205. {
  206. uint64_t size = avio_r8(pb);
  207. if (size & 0x80) { /* long form */
  208. int bytes_num = size & 0x7f;
  209. /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
  210. if (bytes_num > 8)
  211. return AVERROR_INVALIDDATA;
  212. size = 0;
  213. while (bytes_num--)
  214. size = size << 8 | avio_r8(pb);
  215. }
  216. return size;
  217. }
  218. static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
  219. {
  220. int i, b;
  221. for (i = 0; i < size && !pb->eof_reached; i++) {
  222. b = avio_r8(pb);
  223. if (b == key[0])
  224. i = 0;
  225. else if (b != key[i])
  226. i = -1;
  227. }
  228. return i == size;
  229. }
  230. static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
  231. {
  232. if (!mxf_read_sync(pb, mxf_klv_key, 4))
  233. return AVERROR_INVALIDDATA;
  234. klv->offset = avio_tell(pb) - 4;
  235. memcpy(klv->key, mxf_klv_key, 4);
  236. avio_read(pb, klv->key + 4, 12);
  237. klv->length = klv_decode_ber_length(pb);
  238. return klv->length == -1 ? -1 : 0;
  239. }
  240. static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
  241. {
  242. int i;
  243. for (i = 0; i < s->nb_streams; i++) {
  244. MXFTrack *track = s->streams[i]->priv_data;
  245. /* SMPTE 379M 7.3 */
  246. if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
  247. return i;
  248. }
  249. /* return 0 if only one stream, for OP Atom files with 0 as track number */
  250. return s->nb_streams == 1 ? 0 : -1;
  251. }
  252. /* XXX: use AVBitStreamFilter */
  253. static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
  254. {
  255. const uint8_t *buf_ptr, *end_ptr;
  256. uint8_t *data_ptr;
  257. int i;
  258. if (length > 61444) /* worst case PAL 1920 samples 8 channels */
  259. return AVERROR_INVALIDDATA;
  260. length = av_get_packet(pb, pkt, length);
  261. if (length < 0)
  262. return length;
  263. data_ptr = pkt->data;
  264. end_ptr = pkt->data + length;
  265. buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
  266. for (; buf_ptr + st->codec->channels*4 < end_ptr; ) {
  267. for (i = 0; i < st->codec->channels; i++) {
  268. uint32_t sample = bytestream_get_le32(&buf_ptr);
  269. if (st->codec->bits_per_coded_sample == 24)
  270. bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
  271. else
  272. bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
  273. }
  274. buf_ptr += 32 - st->codec->channels*4; // always 8 channels stored SMPTE 331M
  275. }
  276. av_shrink_packet(pkt, data_ptr - pkt->data);
  277. return 0;
  278. }
  279. static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
  280. {
  281. static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
  282. MXFContext *mxf = s->priv_data;
  283. AVIOContext *pb = s->pb;
  284. int64_t end = avio_tell(pb) + klv->length;
  285. int64_t size;
  286. uint64_t orig_size;
  287. uint64_t plaintext_size;
  288. uint8_t ivec[16];
  289. uint8_t tmpbuf[16];
  290. int index;
  291. if (!mxf->aesc && s->key && s->keylen == 16) {
  292. mxf->aesc = av_malloc(av_aes_size);
  293. if (!mxf->aesc)
  294. return AVERROR(ENOMEM);
  295. av_aes_init(mxf->aesc, s->key, 128, 1);
  296. }
  297. // crypto context
  298. avio_skip(pb, klv_decode_ber_length(pb));
  299. // plaintext offset
  300. klv_decode_ber_length(pb);
  301. plaintext_size = avio_rb64(pb);
  302. // source klv key
  303. klv_decode_ber_length(pb);
  304. avio_read(pb, klv->key, 16);
  305. if (!IS_KLV_KEY(klv, mxf_essence_element_key))
  306. return AVERROR_INVALIDDATA;
  307. index = mxf_get_stream_index(s, klv);
  308. if (index < 0)
  309. return AVERROR_INVALIDDATA;
  310. // source size
  311. klv_decode_ber_length(pb);
  312. orig_size = avio_rb64(pb);
  313. if (orig_size < plaintext_size)
  314. return AVERROR_INVALIDDATA;
  315. // enc. code
  316. size = klv_decode_ber_length(pb);
  317. if (size < 32 || size - 32 < orig_size)
  318. return AVERROR_INVALIDDATA;
  319. avio_read(pb, ivec, 16);
  320. avio_read(pb, tmpbuf, 16);
  321. if (mxf->aesc)
  322. av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
  323. if (memcmp(tmpbuf, checkv, 16))
  324. av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
  325. size -= 32;
  326. size = av_get_packet(pb, pkt, size);
  327. if (size < 0)
  328. return size;
  329. else if (size < plaintext_size)
  330. return AVERROR_INVALIDDATA;
  331. size -= plaintext_size;
  332. if (mxf->aesc)
  333. av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
  334. &pkt->data[plaintext_size], size >> 4, ivec, 1);
  335. av_shrink_packet(pkt, orig_size);
  336. pkt->stream_index = index;
  337. avio_skip(pb, end - avio_tell(pb));
  338. return 0;
  339. }
  340. static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
  341. {
  342. KLVPacket klv;
  343. while (!s->pb->eof_reached) {
  344. int ret;
  345. if (klv_read_packet(&klv, s->pb) < 0)
  346. return -1;
  347. PRINT_KEY(s, "read packet", klv.key);
  348. av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
  349. if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
  350. ret = mxf_decrypt_triplet(s, pkt, &klv);
  351. if (ret < 0) {
  352. av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
  353. return AVERROR_INVALIDDATA;
  354. }
  355. return 0;
  356. }
  357. if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
  358. int index = mxf_get_stream_index(s, &klv);
  359. if (index < 0) {
  360. av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12));
  361. goto skip;
  362. }
  363. if (s->streams[index]->discard == AVDISCARD_ALL)
  364. goto skip;
  365. /* check for 8 channels AES3 element */
  366. if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
  367. if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
  368. av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
  369. return AVERROR_INVALIDDATA;
  370. }
  371. } else {
  372. ret = av_get_packet(s->pb, pkt, klv.length);
  373. if (ret < 0)
  374. return ret;
  375. }
  376. pkt->stream_index = index;
  377. pkt->pos = klv.offset;
  378. return 0;
  379. } else
  380. skip:
  381. avio_skip(s->pb, klv.length);
  382. }
  383. return AVERROR_EOF;
  384. }
  385. static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  386. {
  387. MXFContext *mxf = arg;
  388. int item_num = avio_rb32(pb);
  389. int item_len = avio_rb32(pb);
  390. if (item_len != 18) {
  391. av_log_ask_for_sample(pb, "unsupported primer pack item length %d\n",
  392. item_len);
  393. return AVERROR_PATCHWELCOME;
  394. }
  395. if (item_num > UINT_MAX / item_len)
  396. return AVERROR_INVALIDDATA;
  397. mxf->local_tags_count = item_num;
  398. mxf->local_tags = av_malloc(item_num*item_len);
  399. if (!mxf->local_tags)
  400. return AVERROR(ENOMEM);
  401. avio_read(pb, mxf->local_tags, item_num*item_len);
  402. return 0;
  403. }
  404. static int mxf_read_partition_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  405. {
  406. MXFContext *mxf = arg;
  407. MXFPartition *partition;
  408. UID op;
  409. uint64_t footer_partition;
  410. if (mxf->partitions_count+1 >= UINT_MAX / sizeof(*mxf->partitions))
  411. return AVERROR(ENOMEM);
  412. mxf->partitions = av_realloc(mxf->partitions, (mxf->partitions_count + 1) * sizeof(*mxf->partitions));
  413. if (!mxf->partitions)
  414. return AVERROR(ENOMEM);
  415. if (mxf->parsing_backward) {
  416. /* insert the new partition pack in the middle
  417. * this makes the entries in mxf->partitions sorted by offset */
  418. memmove(&mxf->partitions[mxf->last_forward_partition+1],
  419. &mxf->partitions[mxf->last_forward_partition],
  420. (mxf->partitions_count - mxf->last_forward_partition)*sizeof(*mxf->partitions));
  421. partition = mxf->current_partition = &mxf->partitions[mxf->last_forward_partition];
  422. } else {
  423. mxf->last_forward_partition++;
  424. partition = mxf->current_partition = &mxf->partitions[mxf->partitions_count];
  425. }
  426. memset(partition, 0, sizeof(*partition));
  427. mxf->partitions_count++;
  428. switch(uid[13]) {
  429. case 2:
  430. partition->type = Header;
  431. break;
  432. case 3:
  433. partition->type = BodyPartition;
  434. break;
  435. case 4:
  436. partition->type = Footer;
  437. break;
  438. default:
  439. av_log(mxf->fc, AV_LOG_ERROR, "unknown partition type %i\n", uid[13]);
  440. return AVERROR_INVALIDDATA;
  441. }
  442. /* consider both footers to be closed (there is only Footer and CompleteFooter) */
  443. partition->closed = partition->type == Footer || !(uid[14] & 1);
  444. partition->complete = uid[14] > 2;
  445. avio_skip(pb, 8);
  446. partition->this_partition = avio_rb64(pb);
  447. partition->previous_partition = avio_rb64(pb);
  448. footer_partition = avio_rb64(pb);
  449. avio_skip(pb, 16);
  450. partition->index_sid = avio_rb32(pb);
  451. avio_skip(pb, 8);
  452. partition->body_sid = avio_rb32(pb);
  453. avio_read(pb, op, sizeof(UID));
  454. /* some files don'thave FooterPartition set in every partition */
  455. if (footer_partition) {
  456. if (mxf->footer_partition && mxf->footer_partition != footer_partition) {
  457. av_log(mxf->fc, AV_LOG_ERROR, "inconsistent FooterPartition value: %li != %li\n",
  458. mxf->footer_partition, footer_partition);
  459. } else {
  460. mxf->footer_partition = footer_partition;
  461. }
  462. }
  463. av_dlog(mxf->fc, "PartitionPack: ThisPartition = 0x%lx, PreviousPartition = 0x%lx, "
  464. "FooterPartition = 0x%lx, IndexSID = %i, BodySID = %i\n",
  465. partition->this_partition,
  466. partition->previous_partition, footer_partition,
  467. partition->index_sid, partition->body_sid);
  468. if (op[12] == 1 && op[13] == 1) mxf->op = OP1a;
  469. else if (op[12] == 1 && op[13] == 2) mxf->op = OP1b;
  470. else if (op[12] == 1 && op[13] == 3) mxf->op = OP1c;
  471. else if (op[12] == 2 && op[13] == 1) mxf->op = OP2a;
  472. else if (op[12] == 2 && op[13] == 2) mxf->op = OP2b;
  473. else if (op[12] == 2 && op[13] == 3) mxf->op = OP2c;
  474. else if (op[12] == 3 && op[13] == 1) mxf->op = OP3a;
  475. else if (op[12] == 3 && op[13] == 2) mxf->op = OP3b;
  476. else if (op[12] == 3 && op[13] == 3) mxf->op = OP3c;
  477. else if (op[12] == 0x10) mxf->op = OPAtom;
  478. else
  479. av_log(mxf->fc, AV_LOG_ERROR, "unknown operational pattern: %02xh %02xh\n", op[12], op[13]);
  480. return 0;
  481. }
  482. static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
  483. {
  484. if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets))
  485. return AVERROR(ENOMEM);
  486. mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
  487. if (!mxf->metadata_sets)
  488. return AVERROR(ENOMEM);
  489. mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
  490. mxf->metadata_sets_count++;
  491. return 0;
  492. }
  493. static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  494. {
  495. MXFCryptoContext *cryptocontext = arg;
  496. if (size != 16)
  497. return AVERROR_INVALIDDATA;
  498. if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
  499. avio_read(pb, cryptocontext->source_container_ul, 16);
  500. return 0;
  501. }
  502. static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  503. {
  504. MXFContext *mxf = arg;
  505. switch (tag) {
  506. case 0x1901:
  507. mxf->packages_count = avio_rb32(pb);
  508. if (mxf->packages_count >= UINT_MAX / sizeof(UID))
  509. return AVERROR_INVALIDDATA;
  510. mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
  511. if (!mxf->packages_refs)
  512. return AVERROR(ENOMEM);
  513. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  514. avio_read(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
  515. break;
  516. }
  517. return 0;
  518. }
  519. static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  520. {
  521. MXFStructuralComponent *source_clip = arg;
  522. switch(tag) {
  523. case 0x0202:
  524. source_clip->duration = avio_rb64(pb);
  525. break;
  526. case 0x1201:
  527. source_clip->start_position = avio_rb64(pb);
  528. break;
  529. case 0x1101:
  530. /* UMID, only get last 16 bytes */
  531. avio_skip(pb, 16);
  532. avio_read(pb, source_clip->source_package_uid, 16);
  533. break;
  534. case 0x1102:
  535. source_clip->source_track_id = avio_rb32(pb);
  536. break;
  537. }
  538. return 0;
  539. }
  540. static int mxf_read_material_package(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  541. {
  542. MXFPackage *package = arg;
  543. switch(tag) {
  544. case 0x4403:
  545. package->tracks_count = avio_rb32(pb);
  546. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  547. return AVERROR_INVALIDDATA;
  548. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  549. if (!package->tracks_refs)
  550. return AVERROR(ENOMEM);
  551. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  552. avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  553. break;
  554. }
  555. return 0;
  556. }
  557. static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  558. {
  559. MXFTrack *track = arg;
  560. switch(tag) {
  561. case 0x4801:
  562. track->track_id = avio_rb32(pb);
  563. break;
  564. case 0x4804:
  565. avio_read(pb, track->track_number, 4);
  566. break;
  567. case 0x4B01:
  568. track->edit_rate.den = avio_rb32(pb);
  569. track->edit_rate.num = avio_rb32(pb);
  570. break;
  571. case 0x4803:
  572. avio_read(pb, track->sequence_ref, 16);
  573. break;
  574. }
  575. return 0;
  576. }
  577. static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  578. {
  579. MXFSequence *sequence = arg;
  580. switch(tag) {
  581. case 0x0202:
  582. sequence->duration = avio_rb64(pb);
  583. break;
  584. case 0x0201:
  585. avio_read(pb, sequence->data_definition_ul, 16);
  586. break;
  587. case 0x1001:
  588. sequence->structural_components_count = avio_rb32(pb);
  589. if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
  590. return AVERROR_INVALIDDATA;
  591. sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
  592. if (!sequence->structural_components_refs)
  593. return AVERROR(ENOMEM);
  594. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  595. avio_read(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
  596. break;
  597. }
  598. return 0;
  599. }
  600. static int mxf_read_source_package(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  601. {
  602. MXFPackage *package = arg;
  603. switch(tag) {
  604. case 0x4403:
  605. package->tracks_count = avio_rb32(pb);
  606. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  607. return AVERROR_INVALIDDATA;
  608. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  609. if (!package->tracks_refs)
  610. return AVERROR(ENOMEM);
  611. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  612. avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  613. break;
  614. case 0x4401:
  615. /* UMID, only get last 16 bytes */
  616. avio_skip(pb, 16);
  617. avio_read(pb, package->package_uid, 16);
  618. break;
  619. case 0x4701:
  620. avio_read(pb, package->descriptor_ref, 16);
  621. break;
  622. }
  623. return 0;
  624. }
  625. static int mxf_read_delta_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
  626. {
  627. int i, length;
  628. segment->nb_delta_entries = avio_rb32(pb);
  629. if (segment->nb_delta_entries < 1 ||
  630. segment->nb_delta_entries > INT_MAX >> av_log2(sizeof(*segment->slice)))
  631. return AVERROR(ENOMEM);
  632. length = avio_rb32(pb);
  633. segment->slice = av_mallocz(segment->nb_delta_entries *
  634. sizeof(*segment->slice));
  635. if (!segment->slice)
  636. return AVERROR(ENOMEM);
  637. segment->element_delta = av_mallocz(segment->nb_delta_entries *
  638. sizeof(*segment->element_delta));
  639. if (!segment->element_delta) {
  640. av_freep(&segment->slice);
  641. return AVERROR(ENOMEM);
  642. }
  643. for (i = 0; i < segment->nb_delta_entries; i++) {
  644. avio_r8(pb); /* PosTableIndex */
  645. segment->slice[i] = avio_r8(pb);
  646. segment->element_delta[i] = avio_rb32(pb);
  647. }
  648. return 0;
  649. }
  650. static int mxf_read_index_entry_array(AVIOContext *pb, MXFIndexTableSegment *segment)
  651. {
  652. int i, j, length;
  653. segment->nb_index_entries = avio_rb32(pb);
  654. if (!segment->nb_index_entries)
  655. return 0;
  656. else if (segment->nb_index_entries < 0 ||
  657. segment->nb_index_entries >
  658. (INT_MAX >> av_log2(sizeof(*segment->stream_offset_entries))))
  659. return AVERROR(ENOMEM);
  660. length = avio_rb32(pb);
  661. segment->flag_entries = av_mallocz(segment->nb_index_entries *
  662. sizeof(*segment->flag_entries));
  663. segment->stream_offset_entries = av_mallocz(segment->nb_index_entries *
  664. sizeof(*segment->stream_offset_entries));
  665. segment->slice_offset_entries = av_mallocz(segment->nb_index_entries *
  666. sizeof(*segment->slice_offset_entries));
  667. if (!segment->flag_entries || !segment->stream_offset_entries ||
  668. !segment->slice_offset_entries)
  669. goto errmem;
  670. for (i = 0; i < segment->nb_index_entries; i++) {
  671. avio_rb16(pb); /* TemporalOffset and KeyFrameOffset */
  672. segment->flag_entries[i] = avio_r8(pb);
  673. segment->stream_offset_entries[i] = avio_rb64(pb);
  674. if (segment->slice_count) {
  675. segment->slice_offset_entries[i] = av_mallocz(segment->slice_count *
  676. sizeof(**segment->slice_offset_entries));
  677. if (!segment->slice_offset_entries[i])
  678. goto errmem;
  679. for (j = 0; j < segment->slice_count; j++)
  680. segment->slice_offset_entries[i][j] = avio_rb32(pb);
  681. }
  682. avio_skip(pb, length - 11 - 4 * segment->slice_count);
  683. }
  684. return 0;
  685. errmem:
  686. if (segment->slice_offset_entries && segment->slice_count) {
  687. for (i = 0; i < segment->nb_index_entries; i++)
  688. av_free(segment->slice_offset_entries[i]);
  689. }
  690. av_freep(&segment->flag_entries);
  691. av_freep(&segment->stream_offset_entries);
  692. av_freep(&segment->slice_offset_entries);
  693. return AVERROR(ENOMEM);
  694. }
  695. static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  696. {
  697. MXFIndexTableSegment *segment = arg;
  698. switch(tag) {
  699. case 0x3F05:
  700. segment->edit_unit_byte_count = avio_rb32(pb);
  701. av_dlog(NULL, "EditUnitByteCount %d\n", segment->edit_unit_byte_count);
  702. break;
  703. case 0x3F06:
  704. segment->index_sid = avio_rb32(pb);
  705. av_dlog(NULL, "IndexSID %d\n", segment->index_sid);
  706. break;
  707. case 0x3F07:
  708. segment->body_sid = avio_rb32(pb);
  709. av_dlog(NULL, "BodySID %d\n", segment->body_sid);
  710. break;
  711. case 0x3F08:
  712. segment->slice_count = avio_r8(pb);
  713. av_dlog(NULL, "SliceCount %d\n", segment->slice_count);
  714. break;
  715. case 0x3F09:
  716. av_dlog(NULL, "DeltaEntryArray found\n");
  717. return mxf_read_delta_entry_array(pb, segment);
  718. case 0x3F0A:
  719. av_dlog(NULL, "IndexEntryArray found\n");
  720. return mxf_read_index_entry_array(pb, segment);
  721. case 0x3F0B:
  722. segment->index_edit_rate.num = avio_rb32(pb);
  723. segment->index_edit_rate.den = avio_rb32(pb);
  724. av_dlog(NULL, "IndexEditRate %d/%d\n", segment->index_edit_rate.num,
  725. segment->index_edit_rate.den);
  726. break;
  727. case 0x3F0C:
  728. segment->index_start_position = avio_rb64(pb);
  729. av_dlog(NULL, "IndexStartPosition %"PRId64"\n", segment->index_start_position);
  730. break;
  731. case 0x3F0D:
  732. segment->index_duration = avio_rb64(pb);
  733. av_dlog(NULL, "IndexDuration %"PRId64"\n", segment->index_duration);
  734. break;
  735. }
  736. return 0;
  737. }
  738. static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
  739. {
  740. int code, value, ofs = 0;
  741. char layout[16] = {0};
  742. do {
  743. code = avio_r8(pb);
  744. value = avio_r8(pb);
  745. av_dlog(NULL, "pixel layout: code %#x\n", code);
  746. if (ofs < 16) {
  747. layout[ofs++] = code;
  748. layout[ofs++] = value;
  749. }
  750. } while (code != 0); /* SMPTE 377M E.2.46 */
  751. ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
  752. }
  753. static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  754. {
  755. MXFDescriptor *descriptor = arg;
  756. switch(tag) {
  757. case 0x3F01:
  758. descriptor->sub_descriptors_count = avio_rb32(pb);
  759. if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
  760. return AVERROR_INVALIDDATA;
  761. descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
  762. if (!descriptor->sub_descriptors_refs)
  763. return AVERROR(ENOMEM);
  764. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  765. avio_read(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
  766. break;
  767. case 0x3004:
  768. avio_read(pb, descriptor->essence_container_ul, 16);
  769. break;
  770. case 0x3006:
  771. descriptor->linked_track_id = avio_rb32(pb);
  772. break;
  773. case 0x3201: /* PictureEssenceCoding */
  774. avio_read(pb, descriptor->essence_codec_ul, 16);
  775. break;
  776. case 0x3203:
  777. descriptor->width = avio_rb32(pb);
  778. break;
  779. case 0x3202:
  780. descriptor->height = avio_rb32(pb);
  781. break;
  782. case 0x320E:
  783. descriptor->aspect_ratio.num = avio_rb32(pb);
  784. descriptor->aspect_ratio.den = avio_rb32(pb);
  785. break;
  786. case 0x3D03:
  787. descriptor->sample_rate.num = avio_rb32(pb);
  788. descriptor->sample_rate.den = avio_rb32(pb);
  789. break;
  790. case 0x3D06: /* SoundEssenceCompression */
  791. avio_read(pb, descriptor->essence_codec_ul, 16);
  792. break;
  793. case 0x3D07:
  794. descriptor->channels = avio_rb32(pb);
  795. break;
  796. case 0x3D01:
  797. descriptor->bits_per_sample = avio_rb32(pb);
  798. break;
  799. case 0x3401:
  800. mxf_read_pixel_layout(pb, descriptor);
  801. break;
  802. default:
  803. /* Private uid used by SONY C0023S01.mxf */
  804. if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
  805. descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  806. if (!descriptor->extradata)
  807. return AVERROR(ENOMEM);
  808. descriptor->extradata_size = size;
  809. avio_read(pb, descriptor->extradata, size);
  810. }
  811. break;
  812. }
  813. return 0;
  814. }
  815. /*
  816. * Match an uid independently of the version byte and up to len common bytes
  817. * Returns: boolean
  818. */
  819. static int mxf_match_uid(const UID key, const UID uid, int len)
  820. {
  821. int i;
  822. for (i = 0; i < len; i++) {
  823. if (i != 7 && key[i] != uid[i])
  824. return 0;
  825. }
  826. return 1;
  827. }
  828. static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
  829. {
  830. while (uls->uid[0]) {
  831. if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
  832. break;
  833. uls++;
  834. }
  835. return uls;
  836. }
  837. static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
  838. {
  839. int i;
  840. if (!strong_ref)
  841. return NULL;
  842. for (i = 0; i < mxf->metadata_sets_count; i++) {
  843. if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
  844. (type == AnyType || mxf->metadata_sets[i]->type == type)) {
  845. return mxf->metadata_sets[i];
  846. }
  847. }
  848. return NULL;
  849. }
  850. static const MXFCodecUL mxf_essence_container_uls[] = {
  851. // video essence container uls
  852. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO }, /* MPEG-ES Frame wrapped */
  853. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, CODEC_ID_DVVIDEO }, /* DV 625 25mbps */
  854. // sound essence container uls
  855. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */
  856. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, CODEC_ID_MP2 }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */
  857. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, CODEC_ID_PCM_S16LE }, /* D-10 Mapping 50Mbps PAL Extended Template */
  858. { { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0xFF,0x4B,0x46,0x41,0x41,0x00,0x0D,0x4D,0x4F }, 14, CODEC_ID_PCM_S16LE }, /* 0001GL00.MXF.A1.mxf_opatom.mxf */
  859. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
  860. };
  861. static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)
  862. {
  863. int i, j, nb_segments = 0;
  864. MXFIndexTableSegment **unsorted_segments;
  865. int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;
  866. /* count number of segments, allocate arrays and copy unsorted segments */
  867. for (i = 0; i < mxf->metadata_sets_count; i++)
  868. if (mxf->metadata_sets[i]->type == IndexTableSegment)
  869. nb_segments++;
  870. *sorted_segments = av_mallocz(nb_segments * sizeof(**sorted_segments));
  871. unsorted_segments = av_mallocz(nb_segments * sizeof(*unsorted_segments));
  872. if (!sorted_segments || !unsorted_segments) {
  873. av_freep(sorted_segments);
  874. av_free(unsorted_segments);
  875. return AVERROR(ENOMEM);
  876. }
  877. for (i = j = 0; i < mxf->metadata_sets_count; i++)
  878. if (mxf->metadata_sets[i]->type == IndexTableSegment)
  879. unsorted_segments[j++] = (MXFIndexTableSegment*)mxf->metadata_sets[i];
  880. *nb_sorted_segments = 0;
  881. /* sort segments by {BodySID, IndexSID, IndexStartPosition}, remove duplicates while we're at it */
  882. for (i = 0; i < nb_segments; i++) {
  883. int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;
  884. for (j = 0; j < nb_segments; j++) {
  885. MXFIndexTableSegment *s = unsorted_segments[j];
  886. /* Require larger BosySID, IndexSID or IndexStartPosition then the previous entry. This removes duplicates.
  887. * We want the smallest values for the keys than what we currently have, unless this is the first such entry this time around.
  888. */
  889. if ((i == 0 || s->body_sid > last_body_sid || s->index_sid > last_index_sid || s->index_start_position > last_index_start) &&
  890. (best == -1 || s->body_sid < best_body_sid || s->index_sid < best_index_sid || s->index_start_position < best_index_start)) {
  891. best = j;
  892. best_body_sid = s->body_sid;
  893. best_index_sid = s->index_sid;
  894. best_index_start = s->index_start_position;
  895. }
  896. }
  897. /* no suitable entry found -> we're done */
  898. if (best == -1)
  899. break;
  900. (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];
  901. last_body_sid = best_body_sid;
  902. last_index_sid = best_index_sid;
  903. last_index_start = best_index_start;
  904. }
  905. av_free(unsorted_segments);
  906. return 0;
  907. }
  908. static int mxf_parse_index(MXFContext *mxf, int i, AVStream *st)
  909. {
  910. int64_t accumulated_offset = 0;
  911. int j, k, ret, nb_sorted_segments;
  912. MXFIndexTableSegment **sorted_segments;
  913. if ((ret = mxf_get_sorted_table_segments(mxf, &nb_sorted_segments, &sorted_segments)))
  914. return ret;
  915. for (j = 0; j < nb_sorted_segments; j++) {
  916. int n_delta = i;
  917. int duration, sample_duration = 1, last_sample_size = 0;
  918. int64_t segment_size;
  919. MXFIndexTableSegment *tableseg = sorted_segments[j];
  920. /* reset accumulated_offset on BodySID change */
  921. if (j > 0 && tableseg->body_sid != sorted_segments[j-1]->body_sid)
  922. accumulated_offset = 0;
  923. /* HACK: How to correctly link between streams and slices? */
  924. if (i < mxf->system_item + st->index)
  925. n_delta++;
  926. if (n_delta >= tableseg->nb_delta_entries && st->index != 0)
  927. continue;
  928. duration = tableseg->index_duration > 0 ? tableseg->index_duration :
  929. st->duration - st->nb_index_entries;
  930. segment_size = tableseg->edit_unit_byte_count * duration;
  931. /* check small EditUnitByteCount for audio */
  932. if (tableseg->edit_unit_byte_count && tableseg->edit_unit_byte_count < 32
  933. && !tableseg->index_duration) {
  934. /* duration might be prime relative to the new sample_duration,
  935. * which means we need to handle the last frame differently */
  936. sample_duration = 8192;
  937. last_sample_size = (duration % sample_duration) * tableseg->edit_unit_byte_count;
  938. tableseg->edit_unit_byte_count *= sample_duration;
  939. duration /= sample_duration;
  940. if (last_sample_size) duration++;
  941. }
  942. for (k = 0; k < duration; k++) {
  943. int64_t pos;
  944. int size, flags = 0;
  945. if (k < tableseg->nb_index_entries) {
  946. pos = tableseg->stream_offset_entries[k];
  947. if (n_delta < tableseg->nb_delta_entries) {
  948. if (n_delta < tableseg->nb_delta_entries - 1) {
  949. size =
  950. tableseg->slice_offset_entries[k][tableseg->slice[n_delta+1]-1] +
  951. tableseg->element_delta[n_delta+1] -
  952. tableseg->element_delta[n_delta];
  953. if (tableseg->slice[n_delta] > 0)
  954. size -= tableseg->slice_offset_entries[k][tableseg->slice[n_delta]-1];
  955. } else if (k < duration - 1) {
  956. size = tableseg->stream_offset_entries[k+1] -
  957. tableseg->stream_offset_entries[k] -
  958. tableseg->slice_offset_entries[k][tableseg->slice[tableseg->nb_delta_entries-1]-1] -
  959. tableseg->element_delta[tableseg->nb_delta_entries-1];
  960. } else
  961. size = 0;
  962. if (tableseg->slice[n_delta] > 0)
  963. pos += tableseg->slice_offset_entries[k][tableseg->slice[n_delta]-1];
  964. pos += tableseg->element_delta[n_delta];
  965. } else
  966. size = 0;
  967. flags = !(tableseg->flag_entries[k] & 0x30) ? AVINDEX_KEYFRAME : 0;
  968. } else {
  969. pos = (int64_t)k * tableseg->edit_unit_byte_count + accumulated_offset;
  970. if (n_delta < tableseg->nb_delta_entries - 1)
  971. size = tableseg->element_delta[n_delta+1] - tableseg->element_delta[n_delta];
  972. else {
  973. /* use smaller size for last sample if we should */
  974. if (last_sample_size && k == duration - 1)
  975. size = last_sample_size;
  976. else
  977. size = tableseg->edit_unit_byte_count;
  978. if (tableseg->nb_delta_entries)
  979. size -= tableseg->element_delta[tableseg->nb_delta_entries-1];
  980. }
  981. if (n_delta < tableseg->nb_delta_entries)
  982. pos += tableseg->element_delta[n_delta];
  983. flags = AVINDEX_KEYFRAME;
  984. }
  985. if (k > 0 && pos < mxf->first_essence_length && accumulated_offset == 0)
  986. pos += mxf->first_essence_kl_length;
  987. pos += mxf->essence_offset;
  988. av_dlog(mxf->fc, "Stream %d IndexEntry %d n_Delta %d Offset %"PRIx64" Timestamp %"PRId64"\n",
  989. st->index, st->nb_index_entries, n_delta, pos, sample_duration * st->nb_index_entries);
  990. if ((ret = av_add_index_entry(st, pos, sample_duration * st->nb_index_entries, size, 0, flags)) < 0)
  991. return ret;
  992. }
  993. accumulated_offset += segment_size;
  994. }
  995. av_free(sorted_segments);
  996. return 0;
  997. }
  998. static int mxf_parse_structural_metadata(MXFContext *mxf)
  999. {
  1000. MXFPackage *material_package = NULL;
  1001. MXFPackage *temp_package = NULL;
  1002. int i, j, k, ret;
  1003. av_dlog(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
  1004. /* TODO: handle multiple material packages (OP3x) */
  1005. for (i = 0; i < mxf->packages_count; i++) {
  1006. material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
  1007. if (material_package) break;
  1008. }
  1009. if (!material_package) {
  1010. av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
  1011. return AVERROR_INVALIDDATA;
  1012. }
  1013. for (i = 0; i < material_package->tracks_count; i++) {
  1014. MXFPackage *source_package = NULL;
  1015. MXFTrack *material_track = NULL;
  1016. MXFTrack *source_track = NULL;
  1017. MXFTrack *temp_track = NULL;
  1018. MXFDescriptor *descriptor = NULL;
  1019. MXFStructuralComponent *component = NULL;
  1020. UID *essence_container_ul = NULL;
  1021. const MXFCodecUL *codec_ul = NULL;
  1022. const MXFCodecUL *container_ul = NULL;
  1023. AVStream *st;
  1024. if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
  1025. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
  1026. continue;
  1027. }
  1028. if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
  1029. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
  1030. continue;
  1031. }
  1032. /* TODO: handle multiple source clips */
  1033. for (j = 0; j < material_track->sequence->structural_components_count; j++) {
  1034. /* TODO: handle timecode component */
  1035. component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
  1036. if (!component)
  1037. continue;
  1038. for (k = 0; k < mxf->packages_count; k++) {
  1039. temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
  1040. if (!temp_package)
  1041. continue;
  1042. if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
  1043. source_package = temp_package;
  1044. break;
  1045. }
  1046. }
  1047. if (!source_package) {
  1048. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
  1049. break;
  1050. }
  1051. for (k = 0; k < source_package->tracks_count; k++) {
  1052. if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
  1053. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
  1054. return AVERROR_INVALIDDATA;
  1055. }
  1056. if (temp_track->track_id == component->source_track_id) {
  1057. source_track = temp_track;
  1058. break;
  1059. }
  1060. }
  1061. if (!source_track) {
  1062. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
  1063. break;
  1064. }
  1065. }
  1066. if (!source_track)
  1067. continue;
  1068. if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
  1069. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
  1070. return AVERROR_INVALIDDATA;
  1071. }
  1072. /* 0001GL00.MXF.A1.mxf_opatom.mxf has the same SourcePackageID as 0001GL.MXF.V1.mxf_opatom.mxf
  1073. * This would result in both files appearing to have two streams. Work around this by sanity checking DataDefinition */
  1074. if (memcmp(material_track->sequence->data_definition_ul, source_track->sequence->data_definition_ul, 16)) {
  1075. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: DataDefinition mismatch\n", material_track->track_id);
  1076. continue;
  1077. }
  1078. st = avformat_new_stream(mxf->fc, NULL);
  1079. if (!st) {
  1080. av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
  1081. return AVERROR(ENOMEM);
  1082. }
  1083. st->id = source_track->track_id;
  1084. st->priv_data = source_track;
  1085. st->duration = component->duration;
  1086. if (st->duration == -1)
  1087. st->duration = AV_NOPTS_VALUE;
  1088. st->start_time = component->start_position;
  1089. avpriv_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
  1090. PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
  1091. codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
  1092. st->codec->codec_type = codec_ul->id;
  1093. source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
  1094. if (source_package->descriptor) {
  1095. if (source_package->descriptor->type == MultipleDescriptor) {
  1096. for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
  1097. MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
  1098. if (!sub_descriptor) {
  1099. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
  1100. continue;
  1101. }
  1102. if (sub_descriptor->linked_track_id == source_track->track_id) {
  1103. descriptor = sub_descriptor;
  1104. break;
  1105. }
  1106. }
  1107. } else if (source_package->descriptor->type == Descriptor)
  1108. descriptor = source_package->descriptor;
  1109. }
  1110. if (!descriptor) {
  1111. av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
  1112. continue;
  1113. }
  1114. PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
  1115. PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
  1116. essence_container_ul = &descriptor->essence_container_ul;
  1117. /* HACK: replacing the original key with mxf_encrypted_essence_container
  1118. * is not allowed according to s429-6, try to find correct information anyway */
  1119. if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
  1120. av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
  1121. for (k = 0; k < mxf->metadata_sets_count; k++) {
  1122. MXFMetadataSet *metadata = mxf->metadata_sets[k];
  1123. if (metadata->type == CryptoContext) {
  1124. essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
  1130. codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
  1131. st->codec->codec_id = codec_ul->id;
  1132. if (descriptor->extradata) {
  1133. st->codec->extradata = descriptor->extradata;
  1134. st->codec->extradata_size = descriptor->extradata_size;
  1135. }
  1136. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1137. container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
  1138. if (st->codec->codec_id == CODEC_ID_NONE)
  1139. st->codec->codec_id = container_ul->id;
  1140. st->codec->width = descriptor->width;
  1141. st->codec->height = descriptor->height;
  1142. if (st->codec->codec_id == CODEC_ID_RAWVIDEO)
  1143. st->codec->pix_fmt = descriptor->pix_fmt;
  1144. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1145. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  1146. container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
  1147. if (st->codec->codec_id == CODEC_ID_NONE)
  1148. st->codec->codec_id = container_ul->id;
  1149. st->codec->channels = descriptor->channels;
  1150. st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
  1151. st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
  1152. /* TODO: implement CODEC_ID_RAWAUDIO */
  1153. if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
  1154. if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
  1155. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  1156. else if (descriptor->bits_per_sample == 32)
  1157. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  1158. } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
  1159. if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
  1160. st->codec->codec_id = CODEC_ID_PCM_S24BE;
  1161. else if (descriptor->bits_per_sample == 32)
  1162. st->codec->codec_id = CODEC_ID_PCM_S32BE;
  1163. } else if (st->codec->codec_id == CODEC_ID_MP2) {
  1164. st->need_parsing = AVSTREAM_PARSE_FULL;
  1165. }
  1166. }
  1167. if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
  1168. av_log(mxf->fc, AV_LOG_WARNING, "only frame wrapped mappings are correctly supported\n");
  1169. st->need_parsing = AVSTREAM_PARSE_FULL;
  1170. }
  1171. if ((ret = mxf_parse_index(mxf, i, st)))
  1172. return ret;
  1173. }
  1174. return 0;
  1175. }
  1176. static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
  1177. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
  1178. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
  1179. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, mxf_read_partition_pack },
  1180. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x03,0x00 }, mxf_read_partition_pack },
  1181. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }, mxf_read_partition_pack },
  1182. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x01,0x00 }, mxf_read_partition_pack },
  1183. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x02,0x00 }, mxf_read_partition_pack },
  1184. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x03,0x00 }, mxf_read_partition_pack },
  1185. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
  1186. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
  1187. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
  1188. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
  1189. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
  1190. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
  1191. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
  1192. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
  1193. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
  1194. { { 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 */
  1195. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
  1196. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
  1197. { { 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 */
  1198. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
  1199. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
  1200. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
  1201. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
  1202. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
  1203. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
  1204. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
  1205. };
  1206. static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
  1207. {
  1208. AVIOContext *pb = mxf->fc->pb;
  1209. MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
  1210. uint64_t klv_end = avio_tell(pb) + klv->length;
  1211. if (!ctx)
  1212. return AVERROR(ENOMEM);
  1213. while (avio_tell(pb) + 4 < klv_end) {
  1214. int ret;
  1215. int tag = avio_rb16(pb);
  1216. int size = avio_rb16(pb); /* KLV specified by 0x53 */
  1217. uint64_t next = avio_tell(pb) + size;
  1218. UID uid = {0};
  1219. av_dlog(mxf->fc, "local tag %#04x size %d\n", tag, size);
  1220. if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
  1221. av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
  1222. continue;
  1223. }
  1224. if (tag > 0x7FFF) { /* dynamic tag */
  1225. int i;
  1226. for (i = 0; i < mxf->local_tags_count; i++) {
  1227. int local_tag = AV_RB16(mxf->local_tags+i*18);
  1228. if (local_tag == tag) {
  1229. memcpy(uid, mxf->local_tags+i*18+2, 16);
  1230. av_dlog(mxf->fc, "local tag %#04x\n", local_tag);
  1231. PRINT_KEY(mxf->fc, "uid", uid);
  1232. }
  1233. }
  1234. }
  1235. if (ctx_size && tag == 0x3C0A)
  1236. avio_read(pb, ctx->uid, 16);
  1237. else if ((ret = read_child(ctx, pb, tag, size, uid)) < 0)
  1238. return ret;
  1239. avio_seek(pb, next, SEEK_SET);
  1240. }
  1241. if (ctx_size) ctx->type = type;
  1242. return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
  1243. }
  1244. /**
  1245. * Seeks to the previous partition, if possible
  1246. * @return <= 0 if we should stop parsing, > 0 if we should keep going
  1247. */
  1248. static int mxf_seek_to_previous_partition(MXFContext *mxf)
  1249. {
  1250. AVIOContext *pb = mxf->fc->pb;
  1251. if (!mxf->current_partition ||
  1252. mxf->run_in + mxf->current_partition->previous_partition <= mxf->last_forward_tell)
  1253. return 0; /* we've parsed all partitions */
  1254. /* seek to previous partition */
  1255. avio_seek(pb, mxf->run_in + mxf->current_partition->previous_partition, SEEK_SET);
  1256. mxf->current_partition = NULL;
  1257. av_dlog(mxf->fc, "seeking to previous partition\n");
  1258. return 1;
  1259. }
  1260. /**
  1261. * Called when essence is encountered
  1262. * @return <= 0 if we should stop parsing, > 0 if we should keep going
  1263. */
  1264. static int mxf_parse_handle_essence(MXFContext *mxf)
  1265. {
  1266. AVIOContext *pb = mxf->fc->pb;
  1267. int64_t ret;
  1268. if (!mxf->current_partition) {
  1269. av_log(mxf->fc, AV_LOG_ERROR, "found essence prior to PartitionPack\n");
  1270. return AVERROR_INVALIDDATA;
  1271. }
  1272. if (mxf->parsing_backward) {
  1273. return mxf_seek_to_previous_partition(mxf);
  1274. } else {
  1275. if (!mxf->footer_partition) {
  1276. av_dlog(mxf->fc, "no footer\n");
  1277. return 0;
  1278. }
  1279. av_dlog(mxf->fc, "seeking to footer\n");
  1280. /* remember where we were so we don't end up seeking further back than this */
  1281. mxf->last_forward_tell = avio_tell(pb);
  1282. if (!pb->seekable) {
  1283. av_log(mxf->fc, AV_LOG_INFO, "file is not seekable - not parsing footer\n");
  1284. return -1;
  1285. }
  1286. /* seek to footer partition and parse backward */
  1287. if ((ret = avio_seek(pb, mxf->run_in + mxf->footer_partition, SEEK_SET)) < 0) {
  1288. av_log(mxf->fc, AV_LOG_ERROR, "failed to seek to footer @ 0x%"PRIx64" (%"PRId64") - partial file?\n",
  1289. mxf->run_in + mxf->footer_partition, ret);
  1290. return ret;
  1291. }
  1292. mxf->current_partition = NULL;
  1293. mxf->parsing_backward = 1;
  1294. }
  1295. return 1;
  1296. }
  1297. /**
  1298. * Called when the next partition or EOF is encountered
  1299. * @return <= 0 if we should stop parsing, > 0 if we should keep going
  1300. */
  1301. static int mxf_parse_handle_partition_or_eof(MXFContext *mxf)
  1302. {
  1303. return mxf->parsing_backward ? mxf_seek_to_previous_partition(mxf) : 1;
  1304. }
  1305. static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1306. {
  1307. MXFContext *mxf = s->priv_data;
  1308. KLVPacket klv;
  1309. mxf->last_forward_tell = INT64_MAX;
  1310. if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
  1311. av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
  1312. return AVERROR_INVALIDDATA;
  1313. }
  1314. avio_seek(s->pb, -14, SEEK_CUR);
  1315. mxf->fc = s;
  1316. mxf->run_in = avio_tell(s->pb);
  1317. while (!s->pb->eof_reached) {
  1318. const MXFMetadataReadTableEntry *metadata;
  1319. if (klv_read_packet(&klv, s->pb) < 0) {
  1320. /* EOF - seek to previous partition or stop */
  1321. if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
  1322. break;
  1323. else
  1324. continue;
  1325. }
  1326. PRINT_KEY(s, "read header", klv.key);
  1327. av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
  1328. if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
  1329. IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
  1330. IS_KLV_KEY(klv.key, mxf_system_item_key)) {
  1331. if (IS_KLV_KEY(klv.key, mxf_system_item_key)) {
  1332. mxf->system_item = 1;
  1333. }
  1334. if (!mxf->essence_offset)
  1335. mxf->essence_offset = klv.offset;
  1336. if (!mxf->first_essence_kl_length && IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
  1337. mxf->first_essence_kl_length = avio_tell(s->pb) - klv.offset;
  1338. mxf->first_essence_length = klv.length;
  1339. }
  1340. /* seek to footer, previous partition or stop */
  1341. if (mxf_parse_handle_essence(mxf) <= 0)
  1342. break;
  1343. continue;
  1344. } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) &&
  1345. klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) {
  1346. /* next partition pack - keep going, seek to previous partition or stop */
  1347. if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
  1348. break;
  1349. }
  1350. for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
  1351. if (IS_KLV_KEY(klv.key, metadata->key)) {
  1352. int res;
  1353. if (klv.key[5] == 0x53) {
  1354. res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);
  1355. } else {
  1356. uint64_t next = avio_tell(s->pb) + klv.length;
  1357. res = metadata->read(mxf, s->pb, 0, 0, klv.key);
  1358. avio_seek(s->pb, next, SEEK_SET);
  1359. }
  1360. if (res < 0) {
  1361. av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
  1362. return res;
  1363. }
  1364. break;
  1365. }
  1366. }
  1367. if (!metadata->read)
  1368. avio_skip(s->pb, klv.length);
  1369. }
  1370. /* FIXME avoid seek */
  1371. if (!mxf->essence_offset) {
  1372. av_log(s, AV_LOG_ERROR, "no essence\n");
  1373. return AVERROR_INVALIDDATA;
  1374. }
  1375. avio_seek(s->pb, mxf->essence_offset, SEEK_SET);
  1376. return mxf_parse_structural_metadata(mxf);
  1377. }
  1378. static int mxf_read_close(AVFormatContext *s)
  1379. {
  1380. MXFContext *mxf = s->priv_data;
  1381. MXFIndexTableSegment *seg;
  1382. int i, j;
  1383. av_freep(&mxf->packages_refs);
  1384. for (i = 0; i < s->nb_streams; i++)
  1385. s->streams[i]->priv_data = NULL;
  1386. for (i = 0; i < mxf->metadata_sets_count; i++) {
  1387. switch (mxf->metadata_sets[i]->type) {
  1388. case MultipleDescriptor:
  1389. av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
  1390. break;
  1391. case Sequence:
  1392. av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
  1393. break;
  1394. case SourcePackage:
  1395. case MaterialPackage:
  1396. av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
  1397. break;
  1398. case IndexTableSegment:
  1399. seg = (MXFIndexTableSegment *)mxf->metadata_sets[i];
  1400. if (seg->slice_count)
  1401. for (j = 0; j < seg->nb_index_entries; j++)
  1402. av_freep(&seg->slice_offset_entries[j]);
  1403. av_freep(&seg->slice);
  1404. av_freep(&seg->element_delta);
  1405. av_freep(&seg->flag_entries);
  1406. av_freep(&seg->stream_offset_entries);
  1407. av_freep(&seg->slice_offset_entries);
  1408. break;
  1409. default:
  1410. break;
  1411. }
  1412. av_freep(&mxf->metadata_sets[i]);
  1413. }
  1414. av_freep(&mxf->partitions);
  1415. av_freep(&mxf->metadata_sets);
  1416. av_freep(&mxf->aesc);
  1417. av_freep(&mxf->local_tags);
  1418. return 0;
  1419. }
  1420. static int mxf_probe(AVProbeData *p) {
  1421. uint8_t *bufp = p->buf;
  1422. uint8_t *end = p->buf + p->buf_size;
  1423. if (p->buf_size < sizeof(mxf_header_partition_pack_key))
  1424. return 0;
  1425. /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
  1426. end -= sizeof(mxf_header_partition_pack_key);
  1427. for (; bufp < end; bufp++) {
  1428. if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
  1429. return AVPROBE_SCORE_MAX;
  1430. }
  1431. return 0;
  1432. }
  1433. /* rudimentary byte seek */
  1434. /* XXX: use MXF Index */
  1435. static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  1436. {
  1437. AVStream *st = s->streams[stream_index];
  1438. int64_t seconds;
  1439. int ret;
  1440. if (!s->bit_rate)
  1441. return AVERROR_INVALIDDATA;
  1442. if (sample_time < 0)
  1443. sample_time = 0;
  1444. seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
  1445. if ((ret = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET)) < 0)
  1446. return ret;
  1447. ff_update_cur_dts(s, st, sample_time);
  1448. return 0;
  1449. }
  1450. AVInputFormat ff_mxf_demuxer = {
  1451. .name = "mxf",
  1452. .long_name = NULL_IF_CONFIG_SMALL("Material eXchange Format"),
  1453. .priv_data_size = sizeof(MXFContext),
  1454. .read_probe = mxf_probe,
  1455. .read_header = mxf_read_header,
  1456. .read_packet = mxf_read_packet,
  1457. .read_close = mxf_read_close,
  1458. .read_seek = mxf_read_seek,
  1459. };