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.

1070 lines
40KB

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