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.

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