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.

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