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.

1158 lines
43KB

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