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.

1042 lines
39KB

  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 struct {
  52. UID uid;
  53. enum MXFMetadataSetType type;
  54. UID source_container_ul;
  55. } MXFCryptoContext;
  56. typedef struct {
  57. UID uid;
  58. enum MXFMetadataSetType type;
  59. UID source_package_uid;
  60. UID data_definition_ul;
  61. int64_t duration;
  62. int64_t start_position;
  63. int source_track_id;
  64. } MXFStructuralComponent;
  65. typedef struct {
  66. UID uid;
  67. enum MXFMetadataSetType type;
  68. UID data_definition_ul;
  69. UID *structural_components_refs;
  70. int structural_components_count;
  71. int64_t duration;
  72. } MXFSequence;
  73. typedef struct {
  74. UID uid;
  75. enum MXFMetadataSetType type;
  76. MXFSequence *sequence; /* mandatory, and only one */
  77. UID sequence_ref;
  78. int track_id;
  79. uint8_t track_number[4];
  80. AVRational edit_rate;
  81. } MXFTrack;
  82. typedef struct {
  83. UID uid;
  84. enum MXFMetadataSetType type;
  85. UID essence_container_ul;
  86. UID essence_codec_ul;
  87. AVRational sample_rate;
  88. AVRational aspect_ratio;
  89. int width;
  90. int height;
  91. int channels;
  92. int bits_per_sample;
  93. UID *sub_descriptors_refs;
  94. int sub_descriptors_count;
  95. int linked_track_id;
  96. uint8_t *extradata;
  97. int extradata_size;
  98. enum PixelFormat pix_fmt;
  99. } MXFDescriptor;
  100. typedef struct {
  101. UID uid;
  102. enum MXFMetadataSetType type;
  103. } MXFIndexTableSegment;
  104. typedef struct {
  105. UID uid;
  106. enum MXFMetadataSetType type;
  107. UID package_uid;
  108. UID *tracks_refs;
  109. int tracks_count;
  110. MXFDescriptor *descriptor; /* only one */
  111. UID descriptor_ref;
  112. } MXFPackage;
  113. typedef struct {
  114. UID uid;
  115. enum MXFMetadataSetType type;
  116. } MXFMetadataSet;
  117. typedef struct {
  118. UID *packages_refs;
  119. int packages_count;
  120. MXFMetadataSet **metadata_sets;
  121. int metadata_sets_count;
  122. AVFormatContext *fc;
  123. struct AVAES *aesc;
  124. uint8_t *local_tags;
  125. int local_tags_count;
  126. } MXFContext;
  127. enum MXFWrappingScheme {
  128. Frame,
  129. Clip,
  130. };
  131. typedef int MXFMetadataReadFunc(void *arg, AVIOContext *pb, int tag, int size, UID uid);
  132. typedef struct {
  133. const UID key;
  134. MXFMetadataReadFunc *read;
  135. int ctx_size;
  136. enum MXFMetadataSetType type;
  137. } MXFMetadataReadTableEntry;
  138. /* partial keys to match */
  139. static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 };
  140. static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 };
  141. static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 };
  142. /* complete keys to match */
  143. 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 };
  144. static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 };
  145. static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 };
  146. static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 };
  147. #define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
  148. static int64_t klv_decode_ber_length(AVIOContext *pb)
  149. {
  150. uint64_t size = avio_r8(pb);
  151. if (size & 0x80) { /* long form */
  152. int bytes_num = size & 0x7f;
  153. /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
  154. if (bytes_num > 8)
  155. return AVERROR_INVALIDDATA;
  156. size = 0;
  157. while (bytes_num--)
  158. size = size << 8 | avio_r8(pb);
  159. }
  160. return size;
  161. }
  162. static int mxf_read_sync(AVIOContext *pb, const uint8_t *key, unsigned size)
  163. {
  164. int i, b;
  165. for (i = 0; i < size && !pb->eof_reached; i++) {
  166. b = avio_r8(pb);
  167. if (b == key[0])
  168. i = 0;
  169. else if (b != key[i])
  170. i = -1;
  171. }
  172. return i == size;
  173. }
  174. static int klv_read_packet(KLVPacket *klv, AVIOContext *pb)
  175. {
  176. if (!mxf_read_sync(pb, mxf_klv_key, 4))
  177. return AVERROR_INVALIDDATA;
  178. klv->offset = avio_tell(pb) - 4;
  179. memcpy(klv->key, mxf_klv_key, 4);
  180. avio_read(pb, klv->key + 4, 12);
  181. klv->length = klv_decode_ber_length(pb);
  182. return klv->length == -1 ? -1 : 0;
  183. }
  184. static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
  185. {
  186. int i;
  187. for (i = 0; i < s->nb_streams; i++) {
  188. MXFTrack *track = s->streams[i]->priv_data;
  189. /* SMPTE 379M 7.3 */
  190. if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
  191. return i;
  192. }
  193. /* return 0 if only one stream, for OP Atom files with 0 as track number */
  194. return s->nb_streams == 1 ? 0 : -1;
  195. }
  196. /* XXX: use AVBitStreamFilter */
  197. static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
  198. {
  199. const uint8_t *buf_ptr, *end_ptr;
  200. uint8_t *data_ptr;
  201. int i;
  202. if (length > 61444) /* worst case PAL 1920 samples 8 channels */
  203. return AVERROR_INVALIDDATA;
  204. length = av_get_packet(pb, pkt, length);
  205. if (length < 0)
  206. return length;
  207. data_ptr = pkt->data;
  208. end_ptr = pkt->data + length;
  209. buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
  210. for (; buf_ptr + st->codec->channels*4 < end_ptr; ) {
  211. for (i = 0; i < st->codec->channels; i++) {
  212. uint32_t sample = bytestream_get_le32(&buf_ptr);
  213. if (st->codec->bits_per_coded_sample == 24)
  214. bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
  215. else
  216. bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
  217. }
  218. buf_ptr += 32 - st->codec->channels*4; // always 8 channels stored SMPTE 331M
  219. }
  220. av_shrink_packet(pkt, data_ptr - pkt->data);
  221. return 0;
  222. }
  223. static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv)
  224. {
  225. static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b};
  226. MXFContext *mxf = s->priv_data;
  227. AVIOContext *pb = s->pb;
  228. int64_t end = avio_tell(pb) + klv->length;
  229. int64_t size;
  230. uint64_t orig_size;
  231. uint64_t plaintext_size;
  232. uint8_t ivec[16];
  233. uint8_t tmpbuf[16];
  234. int index;
  235. if (!mxf->aesc && s->key && s->keylen == 16) {
  236. mxf->aesc = av_malloc(av_aes_size);
  237. if (!mxf->aesc)
  238. return AVERROR(ENOMEM);
  239. av_aes_init(mxf->aesc, s->key, 128, 1);
  240. }
  241. // crypto context
  242. avio_skip(pb, klv_decode_ber_length(pb));
  243. // plaintext offset
  244. klv_decode_ber_length(pb);
  245. plaintext_size = avio_rb64(pb);
  246. // source klv key
  247. klv_decode_ber_length(pb);
  248. avio_read(pb, klv->key, 16);
  249. if (!IS_KLV_KEY(klv, mxf_essence_element_key))
  250. return AVERROR_INVALIDDATA;
  251. index = mxf_get_stream_index(s, klv);
  252. if (index < 0)
  253. return AVERROR_INVALIDDATA;
  254. // source size
  255. klv_decode_ber_length(pb);
  256. orig_size = avio_rb64(pb);
  257. if (orig_size < plaintext_size)
  258. return AVERROR_INVALIDDATA;
  259. // enc. code
  260. size = klv_decode_ber_length(pb);
  261. if (size < 32 || size - 32 < orig_size)
  262. return AVERROR_INVALIDDATA;
  263. avio_read(pb, ivec, 16);
  264. avio_read(pb, tmpbuf, 16);
  265. if (mxf->aesc)
  266. av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1);
  267. if (memcmp(tmpbuf, checkv, 16))
  268. av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
  269. size -= 32;
  270. size = av_get_packet(pb, pkt, size);
  271. if (size < 0)
  272. return size;
  273. else if (size < plaintext_size)
  274. return AVERROR_INVALIDDATA;
  275. size -= plaintext_size;
  276. if (mxf->aesc)
  277. av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
  278. &pkt->data[plaintext_size], size >> 4, ivec, 1);
  279. av_shrink_packet(pkt, orig_size);
  280. pkt->stream_index = index;
  281. avio_skip(pb, end - avio_tell(pb));
  282. return 0;
  283. }
  284. static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
  285. {
  286. KLVPacket klv;
  287. while (!s->pb->eof_reached) {
  288. int ret;
  289. if (klv_read_packet(&klv, s->pb) < 0)
  290. return -1;
  291. PRINT_KEY(s, "read packet", klv.key);
  292. av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
  293. if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
  294. ret = mxf_decrypt_triplet(s, pkt, &klv);
  295. if (ret < 0) {
  296. av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
  297. return AVERROR_INVALIDDATA;
  298. }
  299. return 0;
  300. }
  301. if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
  302. int index = mxf_get_stream_index(s, &klv);
  303. if (index < 0) {
  304. av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12));
  305. goto skip;
  306. }
  307. if (s->streams[index]->discard == AVDISCARD_ALL)
  308. goto skip;
  309. /* check for 8 channels AES3 element */
  310. if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
  311. if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
  312. av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
  313. return AVERROR_INVALIDDATA;
  314. }
  315. } else {
  316. ret = av_get_packet(s->pb, pkt, klv.length);
  317. if (ret < 0)
  318. return ret;
  319. }
  320. pkt->stream_index = index;
  321. pkt->pos = klv.offset;
  322. return 0;
  323. } else
  324. skip:
  325. avio_skip(s->pb, klv.length);
  326. }
  327. return AVERROR_EOF;
  328. }
  329. static int mxf_read_primer_pack(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  330. {
  331. MXFContext *mxf = arg;
  332. int item_num = avio_rb32(pb);
  333. int item_len = avio_rb32(pb);
  334. if (item_len != 18) {
  335. av_log_ask_for_sample(pb, "unsupported primer pack item length %d\n",
  336. item_len);
  337. return AVERROR_PATCHWELCOME;
  338. }
  339. if (item_num > UINT_MAX / item_len)
  340. return AVERROR_INVALIDDATA;
  341. mxf->local_tags_count = item_num;
  342. mxf->local_tags = av_malloc(item_num*item_len);
  343. if (!mxf->local_tags)
  344. return AVERROR(ENOMEM);
  345. avio_read(pb, mxf->local_tags, item_num*item_len);
  346. return 0;
  347. }
  348. static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
  349. {
  350. if (mxf->metadata_sets_count+1 >= UINT_MAX / sizeof(*mxf->metadata_sets))
  351. return AVERROR(ENOMEM);
  352. mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
  353. if (!mxf->metadata_sets)
  354. return AVERROR(ENOMEM);
  355. mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
  356. mxf->metadata_sets_count++;
  357. return 0;
  358. }
  359. static int mxf_read_cryptographic_context(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  360. {
  361. MXFCryptoContext *cryptocontext = arg;
  362. if (size != 16)
  363. return AVERROR_INVALIDDATA;
  364. if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
  365. avio_read(pb, cryptocontext->source_container_ul, 16);
  366. return 0;
  367. }
  368. static int mxf_read_content_storage(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  369. {
  370. MXFContext *mxf = arg;
  371. switch (tag) {
  372. case 0x1901:
  373. mxf->packages_count = avio_rb32(pb);
  374. if (mxf->packages_count >= UINT_MAX / sizeof(UID))
  375. return AVERROR_INVALIDDATA;
  376. mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
  377. if (!mxf->packages_refs)
  378. return AVERROR(ENOMEM);
  379. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  380. avio_read(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
  381. break;
  382. }
  383. return 0;
  384. }
  385. static int mxf_read_source_clip(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  386. {
  387. MXFStructuralComponent *source_clip = arg;
  388. switch(tag) {
  389. case 0x0202:
  390. source_clip->duration = avio_rb64(pb);
  391. break;
  392. case 0x1201:
  393. source_clip->start_position = avio_rb64(pb);
  394. break;
  395. case 0x1101:
  396. /* UMID, only get last 16 bytes */
  397. avio_skip(pb, 16);
  398. avio_read(pb, source_clip->source_package_uid, 16);
  399. break;
  400. case 0x1102:
  401. source_clip->source_track_id = avio_rb32(pb);
  402. break;
  403. }
  404. return 0;
  405. }
  406. static int mxf_read_material_package(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  407. {
  408. MXFPackage *package = arg;
  409. switch(tag) {
  410. case 0x4403:
  411. package->tracks_count = avio_rb32(pb);
  412. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  413. return AVERROR_INVALIDDATA;
  414. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  415. if (!package->tracks_refs)
  416. return AVERROR(ENOMEM);
  417. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  418. avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  419. break;
  420. }
  421. return 0;
  422. }
  423. static int mxf_read_track(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  424. {
  425. MXFTrack *track = arg;
  426. switch(tag) {
  427. case 0x4801:
  428. track->track_id = avio_rb32(pb);
  429. break;
  430. case 0x4804:
  431. avio_read(pb, track->track_number, 4);
  432. break;
  433. case 0x4B01:
  434. track->edit_rate.den = avio_rb32(pb);
  435. track->edit_rate.num = avio_rb32(pb);
  436. break;
  437. case 0x4803:
  438. avio_read(pb, track->sequence_ref, 16);
  439. break;
  440. }
  441. return 0;
  442. }
  443. static int mxf_read_sequence(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  444. {
  445. MXFSequence *sequence = arg;
  446. switch(tag) {
  447. case 0x0202:
  448. sequence->duration = avio_rb64(pb);
  449. break;
  450. case 0x0201:
  451. avio_read(pb, sequence->data_definition_ul, 16);
  452. break;
  453. case 0x1001:
  454. sequence->structural_components_count = avio_rb32(pb);
  455. if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
  456. return AVERROR_INVALIDDATA;
  457. sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
  458. if (!sequence->structural_components_refs)
  459. return AVERROR(ENOMEM);
  460. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  461. avio_read(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
  462. break;
  463. }
  464. return 0;
  465. }
  466. static int mxf_read_source_package(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  467. {
  468. MXFPackage *package = arg;
  469. switch(tag) {
  470. case 0x4403:
  471. package->tracks_count = avio_rb32(pb);
  472. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  473. return AVERROR_INVALIDDATA;
  474. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  475. if (!package->tracks_refs)
  476. return AVERROR(ENOMEM);
  477. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  478. avio_read(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  479. break;
  480. case 0x4401:
  481. /* UMID, only get last 16 bytes */
  482. avio_skip(pb, 16);
  483. avio_read(pb, package->package_uid, 16);
  484. break;
  485. case 0x4701:
  486. avio_read(pb, package->descriptor_ref, 16);
  487. break;
  488. }
  489. return 0;
  490. }
  491. static int mxf_read_index_table_segment(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  492. {
  493. switch(tag) {
  494. case 0x3F05: av_dlog(NULL, "EditUnitByteCount %d\n", avio_rb32(pb)); break;
  495. case 0x3F06: av_dlog(NULL, "IndexSID %d\n", avio_rb32(pb)); break;
  496. case 0x3F07: av_dlog(NULL, "BodySID %d\n", avio_rb32(pb)); break;
  497. case 0x3F0B: av_dlog(NULL, "IndexEditRate %d/%d\n", avio_rb32(pb), avio_rb32(pb)); break;
  498. case 0x3F0C: av_dlog(NULL, "IndexStartPosition %"PRIu64"\n", avio_rb64(pb)); break;
  499. case 0x3F0D: av_dlog(NULL, "IndexDuration %"PRIu64"\n", avio_rb64(pb)); break;
  500. }
  501. return 0;
  502. }
  503. static void mxf_read_pixel_layout(AVIOContext *pb, MXFDescriptor *descriptor)
  504. {
  505. int code, value, ofs = 0;
  506. char layout[16] = {0};
  507. do {
  508. code = avio_r8(pb);
  509. value = avio_r8(pb);
  510. av_dlog(NULL, "pixel layout: code %#x\n", code);
  511. if (ofs < 16) {
  512. layout[ofs++] = code;
  513. layout[ofs++] = value;
  514. }
  515. } while (code != 0); /* SMPTE 377M E.2.46 */
  516. ff_mxf_decode_pixel_layout(layout, &descriptor->pix_fmt);
  517. }
  518. static int mxf_read_generic_descriptor(void *arg, AVIOContext *pb, int tag, int size, UID uid)
  519. {
  520. MXFDescriptor *descriptor = arg;
  521. switch(tag) {
  522. case 0x3F01:
  523. descriptor->sub_descriptors_count = avio_rb32(pb);
  524. if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
  525. return AVERROR_INVALIDDATA;
  526. descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
  527. if (!descriptor->sub_descriptors_refs)
  528. return AVERROR(ENOMEM);
  529. avio_skip(pb, 4); /* useless size of objects, always 16 according to specs */
  530. avio_read(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
  531. break;
  532. case 0x3004:
  533. avio_read(pb, descriptor->essence_container_ul, 16);
  534. break;
  535. case 0x3006:
  536. descriptor->linked_track_id = avio_rb32(pb);
  537. break;
  538. case 0x3201: /* PictureEssenceCoding */
  539. avio_read(pb, descriptor->essence_codec_ul, 16);
  540. break;
  541. case 0x3203:
  542. descriptor->width = avio_rb32(pb);
  543. break;
  544. case 0x3202:
  545. descriptor->height = avio_rb32(pb);
  546. break;
  547. case 0x320E:
  548. descriptor->aspect_ratio.num = avio_rb32(pb);
  549. descriptor->aspect_ratio.den = avio_rb32(pb);
  550. break;
  551. case 0x3D03:
  552. descriptor->sample_rate.num = avio_rb32(pb);
  553. descriptor->sample_rate.den = avio_rb32(pb);
  554. break;
  555. case 0x3D06: /* SoundEssenceCompression */
  556. avio_read(pb, descriptor->essence_codec_ul, 16);
  557. break;
  558. case 0x3D07:
  559. descriptor->channels = avio_rb32(pb);
  560. break;
  561. case 0x3D01:
  562. descriptor->bits_per_sample = avio_rb32(pb);
  563. break;
  564. case 0x3401:
  565. mxf_read_pixel_layout(pb, descriptor);
  566. break;
  567. default:
  568. /* Private uid used by SONY C0023S01.mxf */
  569. if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
  570. descriptor->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  571. if (!descriptor->extradata)
  572. return AVERROR(ENOMEM);
  573. descriptor->extradata_size = size;
  574. avio_read(pb, descriptor->extradata, size);
  575. }
  576. break;
  577. }
  578. return 0;
  579. }
  580. /*
  581. * Match an uid independently of the version byte and up to len common bytes
  582. * Returns: boolean
  583. */
  584. static int mxf_match_uid(const UID key, const UID uid, int len)
  585. {
  586. int i;
  587. for (i = 0; i < len; i++) {
  588. if (i != 7 && key[i] != uid[i])
  589. return 0;
  590. }
  591. return 1;
  592. }
  593. static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
  594. {
  595. while (uls->uid[0]) {
  596. if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
  597. break;
  598. uls++;
  599. }
  600. return uls;
  601. }
  602. static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
  603. {
  604. int i;
  605. if (!strong_ref)
  606. return NULL;
  607. for (i = 0; i < mxf->metadata_sets_count; i++) {
  608. if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
  609. (type == AnyType || mxf->metadata_sets[i]->type == type)) {
  610. return mxf->metadata_sets[i];
  611. }
  612. }
  613. return NULL;
  614. }
  615. static const MXFCodecUL mxf_essence_container_uls[] = {
  616. // video essence container uls
  617. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO }, /* MPEG-ES Frame wrapped */
  618. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, CODEC_ID_DVVIDEO }, /* DV 625 25mbps */
  619. // sound essence container uls
  620. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */
  621. { { 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 */
  622. { { 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 */
  623. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
  624. };
  625. static int mxf_parse_structural_metadata(MXFContext *mxf)
  626. {
  627. MXFPackage *material_package = NULL;
  628. MXFPackage *temp_package = NULL;
  629. int i, j, k;
  630. av_dlog(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
  631. /* TODO: handle multiple material packages (OP3x) */
  632. for (i = 0; i < mxf->packages_count; i++) {
  633. material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
  634. if (material_package) break;
  635. }
  636. if (!material_package) {
  637. av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
  638. return AVERROR_INVALIDDATA;
  639. }
  640. for (i = 0; i < material_package->tracks_count; i++) {
  641. MXFPackage *source_package = NULL;
  642. MXFTrack *material_track = NULL;
  643. MXFTrack *source_track = NULL;
  644. MXFTrack *temp_track = NULL;
  645. MXFDescriptor *descriptor = NULL;
  646. MXFStructuralComponent *component = NULL;
  647. UID *essence_container_ul = NULL;
  648. const MXFCodecUL *codec_ul = NULL;
  649. const MXFCodecUL *container_ul = NULL;
  650. AVStream *st;
  651. if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
  652. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
  653. continue;
  654. }
  655. if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
  656. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
  657. continue;
  658. }
  659. /* TODO: handle multiple source clips */
  660. for (j = 0; j < material_track->sequence->structural_components_count; j++) {
  661. /* TODO: handle timecode component */
  662. component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
  663. if (!component)
  664. continue;
  665. for (k = 0; k < mxf->packages_count; k++) {
  666. temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
  667. if (!temp_package)
  668. continue;
  669. if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
  670. source_package = temp_package;
  671. break;
  672. }
  673. }
  674. if (!source_package) {
  675. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
  676. break;
  677. }
  678. for (k = 0; k < source_package->tracks_count; k++) {
  679. if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
  680. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
  681. return AVERROR_INVALIDDATA;
  682. }
  683. if (temp_track->track_id == component->source_track_id) {
  684. source_track = temp_track;
  685. break;
  686. }
  687. }
  688. if (!source_track) {
  689. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
  690. break;
  691. }
  692. }
  693. if (!source_track)
  694. continue;
  695. st = avformat_new_stream(mxf->fc, NULL);
  696. if (!st) {
  697. av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
  698. return AVERROR(ENOMEM);
  699. }
  700. st->id = source_track->track_id;
  701. st->priv_data = source_track;
  702. st->duration = component->duration;
  703. if (st->duration == -1)
  704. st->duration = AV_NOPTS_VALUE;
  705. st->start_time = component->start_position;
  706. avpriv_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
  707. if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
  708. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
  709. return AVERROR_INVALIDDATA;
  710. }
  711. PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
  712. codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
  713. st->codec->codec_type = codec_ul->id;
  714. source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
  715. if (source_package->descriptor) {
  716. if (source_package->descriptor->type == MultipleDescriptor) {
  717. for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
  718. MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
  719. if (!sub_descriptor) {
  720. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
  721. continue;
  722. }
  723. if (sub_descriptor->linked_track_id == source_track->track_id) {
  724. descriptor = sub_descriptor;
  725. break;
  726. }
  727. }
  728. } else if (source_package->descriptor->type == Descriptor)
  729. descriptor = source_package->descriptor;
  730. }
  731. if (!descriptor) {
  732. av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
  733. continue;
  734. }
  735. PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
  736. PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
  737. essence_container_ul = &descriptor->essence_container_ul;
  738. /* HACK: replacing the original key with mxf_encrypted_essence_container
  739. * is not allowed according to s429-6, try to find correct information anyway */
  740. if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
  741. av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
  742. for (k = 0; k < mxf->metadata_sets_count; k++) {
  743. MXFMetadataSet *metadata = mxf->metadata_sets[k];
  744. if (metadata->type == CryptoContext) {
  745. essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
  746. break;
  747. }
  748. }
  749. }
  750. /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
  751. codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul);
  752. st->codec->codec_id = codec_ul->id;
  753. if (descriptor->extradata) {
  754. st->codec->extradata = descriptor->extradata;
  755. st->codec->extradata_size = descriptor->extradata_size;
  756. }
  757. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  758. container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
  759. if (st->codec->codec_id == CODEC_ID_NONE)
  760. st->codec->codec_id = container_ul->id;
  761. st->codec->width = descriptor->width;
  762. st->codec->height = descriptor->height;
  763. if (st->codec->codec_id == CODEC_ID_RAWVIDEO)
  764. st->codec->pix_fmt = descriptor->pix_fmt;
  765. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  766. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  767. container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul);
  768. if (st->codec->codec_id == CODEC_ID_NONE)
  769. st->codec->codec_id = container_ul->id;
  770. st->codec->channels = descriptor->channels;
  771. st->codec->bits_per_coded_sample = descriptor->bits_per_sample;
  772. st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
  773. /* TODO: implement CODEC_ID_RAWAUDIO */
  774. if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
  775. if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
  776. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  777. else if (descriptor->bits_per_sample == 32)
  778. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  779. } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
  780. if (descriptor->bits_per_sample > 16 && descriptor->bits_per_sample <= 24)
  781. st->codec->codec_id = CODEC_ID_PCM_S24BE;
  782. else if (descriptor->bits_per_sample == 32)
  783. st->codec->codec_id = CODEC_ID_PCM_S32BE;
  784. } else if (st->codec->codec_id == CODEC_ID_MP2) {
  785. st->need_parsing = AVSTREAM_PARSE_FULL;
  786. }
  787. }
  788. if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
  789. av_log(mxf->fc, AV_LOG_WARNING, "only frame wrapped mappings are correctly supported\n");
  790. st->need_parsing = AVSTREAM_PARSE_FULL;
  791. }
  792. }
  793. return 0;
  794. }
  795. static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
  796. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
  797. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
  798. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
  799. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
  800. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
  801. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
  802. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
  803. { { 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 */
  804. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
  805. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
  806. { { 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 */
  807. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
  808. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
  809. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
  810. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
  811. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
  812. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment },
  813. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
  814. };
  815. static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, MXFMetadataReadFunc *read_child, int ctx_size, enum MXFMetadataSetType type)
  816. {
  817. AVIOContext *pb = mxf->fc->pb;
  818. MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
  819. uint64_t klv_end = avio_tell(pb) + klv->length;
  820. if (!ctx)
  821. return AVERROR(ENOMEM);
  822. while (avio_tell(pb) + 4 < klv_end) {
  823. int ret;
  824. int tag = avio_rb16(pb);
  825. int size = avio_rb16(pb); /* KLV specified by 0x53 */
  826. uint64_t next = avio_tell(pb) + size;
  827. UID uid = {0};
  828. av_dlog(mxf->fc, "local tag %#04x size %d\n", tag, size);
  829. if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
  830. av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
  831. continue;
  832. }
  833. if (tag > 0x7FFF) { /* dynamic tag */
  834. int i;
  835. for (i = 0; i < mxf->local_tags_count; i++) {
  836. int local_tag = AV_RB16(mxf->local_tags+i*18);
  837. if (local_tag == tag) {
  838. memcpy(uid, mxf->local_tags+i*18+2, 16);
  839. av_dlog(mxf->fc, "local tag %#04x\n", local_tag);
  840. PRINT_KEY(mxf->fc, "uid", uid);
  841. }
  842. }
  843. }
  844. if (ctx_size && tag == 0x3C0A)
  845. avio_read(pb, ctx->uid, 16);
  846. else if ((ret = read_child(ctx, pb, tag, size, uid)) < 0)
  847. return ret;
  848. avio_seek(pb, next, SEEK_SET);
  849. }
  850. if (ctx_size) ctx->type = type;
  851. return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
  852. }
  853. static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  854. {
  855. MXFContext *mxf = s->priv_data;
  856. KLVPacket klv;
  857. if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
  858. av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
  859. return AVERROR_INVALIDDATA;
  860. }
  861. avio_seek(s->pb, -14, SEEK_CUR);
  862. mxf->fc = s;
  863. while (!s->pb->eof_reached) {
  864. int ret;
  865. const MXFMetadataReadTableEntry *metadata;
  866. if ((ret = klv_read_packet(&klv, s->pb)) < 0)
  867. return ret;
  868. PRINT_KEY(s, "read header", klv.key);
  869. av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
  870. if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
  871. IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
  872. /* FIXME avoid seek */
  873. avio_seek(s->pb, klv.offset, SEEK_SET);
  874. break;
  875. }
  876. for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
  877. if (IS_KLV_KEY(klv.key, metadata->key)) {
  878. int res;
  879. if (klv.key[5] == 0x53) {
  880. res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);
  881. } else
  882. res = metadata->read(mxf, s->pb, 0, 0, NULL);
  883. if (res < 0) {
  884. av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
  885. return res;
  886. }
  887. break;
  888. }
  889. }
  890. if (!metadata->read)
  891. avio_skip(s->pb, klv.length);
  892. }
  893. return mxf_parse_structural_metadata(mxf);
  894. }
  895. static int mxf_read_close(AVFormatContext *s)
  896. {
  897. MXFContext *mxf = s->priv_data;
  898. int i;
  899. av_freep(&mxf->packages_refs);
  900. for (i = 0; i < s->nb_streams; i++)
  901. s->streams[i]->priv_data = NULL;
  902. for (i = 0; i < mxf->metadata_sets_count; i++) {
  903. switch (mxf->metadata_sets[i]->type) {
  904. case MultipleDescriptor:
  905. av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
  906. break;
  907. case Sequence:
  908. av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
  909. break;
  910. case SourcePackage:
  911. case MaterialPackage:
  912. av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
  913. break;
  914. default:
  915. break;
  916. }
  917. av_freep(&mxf->metadata_sets[i]);
  918. }
  919. av_freep(&mxf->metadata_sets);
  920. av_freep(&mxf->aesc);
  921. av_freep(&mxf->local_tags);
  922. return 0;
  923. }
  924. static int mxf_probe(AVProbeData *p) {
  925. uint8_t *bufp = p->buf;
  926. uint8_t *end = p->buf + p->buf_size;
  927. if (p->buf_size < sizeof(mxf_header_partition_pack_key))
  928. return 0;
  929. /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
  930. end -= sizeof(mxf_header_partition_pack_key);
  931. for (; bufp < end; bufp++) {
  932. if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
  933. return AVPROBE_SCORE_MAX;
  934. }
  935. return 0;
  936. }
  937. /* rudimentary byte seek */
  938. /* XXX: use MXF Index */
  939. static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  940. {
  941. AVStream *st = s->streams[stream_index];
  942. int64_t seconds;
  943. if (!s->bit_rate)
  944. return AVERROR_INVALIDDATA;
  945. if (sample_time < 0)
  946. sample_time = 0;
  947. seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
  948. avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
  949. ff_update_cur_dts(s, st, sample_time);
  950. return 0;
  951. }
  952. AVInputFormat ff_mxf_demuxer = {
  953. .name = "mxf",
  954. .long_name = NULL_IF_CONFIG_SMALL("Material eXchange Format"),
  955. .priv_data_size = sizeof(MXFContext),
  956. .read_probe = mxf_probe,
  957. .read_header = mxf_read_header,
  958. .read_packet = mxf_read_packet,
  959. .read_close = mxf_read_close,
  960. .read_seek = mxf_read_seek,
  961. };