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.

1072 lines
41KB

  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. goto skip;
  322. }
  323. if (s->streams[index]->discard == AVDISCARD_ALL)
  324. goto skip;
  325. /* check for 8 channels AES3 element */
  326. if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
  327. if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
  328. av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
  329. return -1;
  330. }
  331. } else
  332. av_get_packet(s->pb, pkt, klv.length);
  333. pkt->stream_index = index;
  334. pkt->pos = klv.offset;
  335. return 0;
  336. } else
  337. skip:
  338. url_fskip(s->pb, klv.length);
  339. }
  340. return AVERROR(EIO);
  341. }
  342. static int mxf_read_primer_pack(MXFContext *mxf)
  343. {
  344. ByteIOContext *pb = mxf->fc->pb;
  345. int item_num = get_be32(pb);
  346. int item_len = get_be32(pb);
  347. if (item_len != 18) {
  348. av_log(mxf->fc, AV_LOG_ERROR, "unsupported primer pack item length\n");
  349. return -1;
  350. }
  351. if (item_num > UINT_MAX / item_len)
  352. return -1;
  353. mxf->local_tags_count = item_num;
  354. mxf->local_tags = av_malloc(item_num*item_len);
  355. if (!mxf->local_tags)
  356. return -1;
  357. get_buffer(pb, mxf->local_tags, item_num*item_len);
  358. return 0;
  359. }
  360. static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set)
  361. {
  362. mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets));
  363. if (!mxf->metadata_sets)
  364. return -1;
  365. mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set;
  366. mxf->metadata_sets_count++;
  367. return 0;
  368. }
  369. static int mxf_read_cryptographic_context(MXFCryptoContext *cryptocontext, ByteIOContext *pb, int tag, int size, UID uid)
  370. {
  371. if (size != 16)
  372. return -1;
  373. if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
  374. get_buffer(pb, cryptocontext->source_container_ul, 16);
  375. return 0;
  376. }
  377. static int mxf_read_content_storage(MXFContext *mxf, ByteIOContext *pb, int tag)
  378. {
  379. switch (tag) {
  380. case 0x1901:
  381. mxf->packages_count = get_be32(pb);
  382. if (mxf->packages_count >= UINT_MAX / sizeof(UID))
  383. return -1;
  384. mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
  385. if (!mxf->packages_refs)
  386. return -1;
  387. url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
  388. get_buffer(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
  389. break;
  390. }
  391. return 0;
  392. }
  393. static int mxf_read_source_clip(MXFStructuralComponent *source_clip, ByteIOContext *pb, int tag)
  394. {
  395. switch(tag) {
  396. case 0x0202:
  397. source_clip->duration = get_be64(pb);
  398. break;
  399. case 0x1201:
  400. source_clip->start_position = get_be64(pb);
  401. break;
  402. case 0x1101:
  403. /* UMID, only get last 16 bytes */
  404. url_fskip(pb, 16);
  405. get_buffer(pb, source_clip->source_package_uid, 16);
  406. break;
  407. case 0x1102:
  408. source_clip->source_track_id = get_be32(pb);
  409. break;
  410. }
  411. return 0;
  412. }
  413. static int mxf_read_material_package(MXFPackage *package, ByteIOContext *pb, int tag)
  414. {
  415. switch(tag) {
  416. case 0x4403:
  417. package->tracks_count = get_be32(pb);
  418. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  419. return -1;
  420. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  421. if (!package->tracks_refs)
  422. return -1;
  423. url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
  424. get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  425. break;
  426. }
  427. return 0;
  428. }
  429. static int mxf_read_track(MXFTrack *track, ByteIOContext *pb, int tag)
  430. {
  431. switch(tag) {
  432. case 0x4801:
  433. track->track_id = get_be32(pb);
  434. break;
  435. case 0x4804:
  436. get_buffer(pb, track->track_number, 4);
  437. break;
  438. case 0x4B01:
  439. track->edit_rate.den = get_be32(pb);
  440. track->edit_rate.num = get_be32(pb);
  441. break;
  442. case 0x4803:
  443. get_buffer(pb, track->sequence_ref, 16);
  444. break;
  445. }
  446. return 0;
  447. }
  448. static int mxf_read_sequence(MXFSequence *sequence, ByteIOContext *pb, int tag)
  449. {
  450. switch(tag) {
  451. case 0x0202:
  452. sequence->duration = get_be64(pb);
  453. break;
  454. case 0x0201:
  455. get_buffer(pb, sequence->data_definition_ul, 16);
  456. break;
  457. case 0x1001:
  458. sequence->structural_components_count = get_be32(pb);
  459. if (sequence->structural_components_count >= UINT_MAX / sizeof(UID))
  460. return -1;
  461. sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
  462. if (!sequence->structural_components_refs)
  463. return -1;
  464. url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
  465. get_buffer(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
  466. break;
  467. }
  468. return 0;
  469. }
  470. static int mxf_read_source_package(MXFPackage *package, ByteIOContext *pb, int tag)
  471. {
  472. switch(tag) {
  473. case 0x4403:
  474. package->tracks_count = get_be32(pb);
  475. if (package->tracks_count >= UINT_MAX / sizeof(UID))
  476. return -1;
  477. package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
  478. if (!package->tracks_refs)
  479. return -1;
  480. url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
  481. get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
  482. break;
  483. case 0x4401:
  484. /* UMID, only get last 16 bytes */
  485. url_fskip(pb, 16);
  486. get_buffer(pb, package->package_uid, 16);
  487. break;
  488. case 0x4701:
  489. get_buffer(pb, package->descriptor_ref, 16);
  490. break;
  491. }
  492. return 0;
  493. }
  494. static void mxf_read_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor)
  495. {
  496. int code;
  497. do {
  498. code = get_byte(pb);
  499. dprintf(NULL, "pixel layout: code 0x%x\n", code);
  500. switch (code) {
  501. case 0x52: /* R */
  502. descriptor->bits_per_sample += get_byte(pb);
  503. break;
  504. case 0x47: /* G */
  505. descriptor->bits_per_sample += get_byte(pb);
  506. break;
  507. case 0x42: /* B */
  508. descriptor->bits_per_sample += get_byte(pb);
  509. break;
  510. default:
  511. get_byte(pb);
  512. }
  513. } while (code != 0); /* SMPTE 377M E.2.46 */
  514. }
  515. static int mxf_read_generic_descriptor(MXFDescriptor *descriptor, ByteIOContext *pb, int tag, int size, UID uid)
  516. {
  517. switch(tag) {
  518. case 0x3F01:
  519. descriptor->sub_descriptors_count = get_be32(pb);
  520. if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID))
  521. return -1;
  522. descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
  523. if (!descriptor->sub_descriptors_refs)
  524. return -1;
  525. url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
  526. get_buffer(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
  527. break;
  528. case 0x3004:
  529. get_buffer(pb, descriptor->essence_container_ul, 16);
  530. break;
  531. case 0x3006:
  532. descriptor->linked_track_id = get_be32(pb);
  533. break;
  534. case 0x3201: /* PictureEssenceCoding */
  535. get_buffer(pb, descriptor->essence_codec_ul, 16);
  536. break;
  537. case 0x3203:
  538. descriptor->width = get_be32(pb);
  539. break;
  540. case 0x3202:
  541. descriptor->height = get_be32(pb);
  542. break;
  543. case 0x320E:
  544. descriptor->aspect_ratio.num = get_be32(pb);
  545. descriptor->aspect_ratio.den = get_be32(pb);
  546. break;
  547. case 0x3D03:
  548. descriptor->sample_rate.num = get_be32(pb);
  549. descriptor->sample_rate.den = get_be32(pb);
  550. break;
  551. case 0x3D06: /* SoundEssenceCompression */
  552. get_buffer(pb, descriptor->essence_codec_ul, 16);
  553. break;
  554. case 0x3D07:
  555. descriptor->channels = get_be32(pb);
  556. break;
  557. case 0x3D01:
  558. descriptor->bits_per_sample = get_be32(pb);
  559. break;
  560. case 0x3401:
  561. mxf_read_pixel_layout(pb, descriptor);
  562. break;
  563. default:
  564. /* Private uid used by SONY C0023S01.mxf */
  565. if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
  566. descriptor->extradata = av_malloc(size);
  567. if (!descriptor->extradata)
  568. return -1;
  569. descriptor->extradata_size = size;
  570. get_buffer(pb, descriptor->extradata, size);
  571. }
  572. break;
  573. }
  574. return 0;
  575. }
  576. /* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
  577. static const MXFDataDefinitionUL mxf_data_definition_uls[] = {
  578. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 }, CODEC_TYPE_VIDEO },
  579. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 }, CODEC_TYPE_AUDIO },
  580. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x05,0x01,0x03,0x02,0x02,0x02,0x02,0x00,0x00 }, CODEC_TYPE_AUDIO },
  581. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, CODEC_TYPE_DATA },
  582. };
  583. static const MXFCodecUL mxf_codec_uls[] = {
  584. /* PictureEssenceCoding */
  585. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, 14, CODEC_ID_MPEG2VIDEO }, /* MP@ML Long GoP */
  586. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, 14, CODEC_ID_MPEG2VIDEO }, /* D-10 50Mbps PAL */
  587. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, 14, CODEC_ID_MPEG2VIDEO }, /* MP@HL Long GoP */
  588. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, 14, CODEC_ID_MPEG2VIDEO }, /* 422P@HL I-Frame */
  589. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x03 }, 14, CODEC_ID_MPEG4 }, /* XDCAM proxy_pal030926.mxf */
  590. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 }, 13, CODEC_ID_DVVIDEO }, /* DV25 IEC PAL */
  591. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, 14, CODEC_ID_JPEG2000 }, /* JPEG2000 Codestream */
  592. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_RAWVIDEO }, /* Uncompressed */
  593. /* SoundEssenceCompression */
  594. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 13, CODEC_ID_PCM_S16LE }, /* Uncompressed */
  595. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13, CODEC_ID_PCM_S16LE },
  596. { { 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 */
  597. { { 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 */
  598. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 }, 15, CODEC_ID_AC3 },
  599. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 }, 15, CODEC_ID_MP2 }, /* MP2 or MP3 */
  600. //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 }, 15, CODEC_ID_DOLBY_E }, /* Dolby-E */
  601. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
  602. };
  603. static const MXFCodecUL mxf_picture_essence_container_uls[] = {
  604. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO }, /* MPEG-ES Frame wrapped */
  605. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, CODEC_ID_DVVIDEO }, /* DV 625 25mbps */
  606. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
  607. };
  608. static const MXFCodecUL mxf_sound_essence_container_uls[] = {
  609. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */
  610. { { 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 */
  611. { { 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 */
  612. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE },
  613. };
  614. /*
  615. * Match an uid independently of the version byte and up to len common bytes
  616. * Returns: boolean
  617. */
  618. static int mxf_match_uid(const UID key, const UID uid, int len)
  619. {
  620. int i;
  621. for (i = 0; i < len; i++) {
  622. if (i != 7 && key[i] != uid[i])
  623. return 0;
  624. }
  625. return 1;
  626. }
  627. static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
  628. {
  629. while (uls->id != CODEC_ID_NONE) {
  630. if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
  631. break;
  632. uls++;
  633. }
  634. return uls;
  635. }
  636. static enum CodecType mxf_get_codec_type(const MXFDataDefinitionUL *uls, UID *uid)
  637. {
  638. while (uls->type != CODEC_TYPE_DATA) {
  639. if(mxf_match_uid(uls->uid, *uid, 16))
  640. break;
  641. uls++;
  642. }
  643. return uls->type;
  644. }
  645. static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type)
  646. {
  647. int i;
  648. if (!strong_ref)
  649. return NULL;
  650. for (i = 0; i < mxf->metadata_sets_count; i++) {
  651. if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) &&
  652. (type == AnyType || mxf->metadata_sets[i]->type == type)) {
  653. return mxf->metadata_sets[i];
  654. }
  655. }
  656. return NULL;
  657. }
  658. static int mxf_parse_structural_metadata(MXFContext *mxf)
  659. {
  660. MXFPackage *material_package = NULL;
  661. MXFPackage *temp_package = NULL;
  662. int i, j, k;
  663. dprintf(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
  664. /* TODO: handle multiple material packages (OP3x) */
  665. for (i = 0; i < mxf->packages_count; i++) {
  666. material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage);
  667. if (material_package) break;
  668. }
  669. if (!material_package) {
  670. av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
  671. return -1;
  672. }
  673. for (i = 0; i < material_package->tracks_count; i++) {
  674. MXFPackage *source_package = NULL;
  675. MXFTrack *material_track = NULL;
  676. MXFTrack *source_track = NULL;
  677. MXFTrack *temp_track = NULL;
  678. MXFDescriptor *descriptor = NULL;
  679. MXFStructuralComponent *component = NULL;
  680. UID *essence_container_ul = NULL;
  681. const MXFCodecUL *codec_ul = NULL;
  682. const MXFCodecUL *container_ul = NULL;
  683. AVStream *st;
  684. if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
  685. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
  686. continue;
  687. }
  688. if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
  689. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
  690. return -1;
  691. }
  692. /* TODO: handle multiple source clips */
  693. for (j = 0; j < material_track->sequence->structural_components_count; j++) {
  694. /* TODO: handle timecode component */
  695. component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip);
  696. if (!component)
  697. continue;
  698. for (k = 0; k < mxf->packages_count; k++) {
  699. temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage);
  700. if (!temp_package)
  701. continue;
  702. if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) {
  703. source_package = temp_package;
  704. break;
  705. }
  706. }
  707. if (!source_package) {
  708. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
  709. break;
  710. }
  711. for (k = 0; k < source_package->tracks_count; k++) {
  712. if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
  713. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
  714. return -1;
  715. }
  716. if (temp_track->track_id == component->source_track_id) {
  717. source_track = temp_track;
  718. break;
  719. }
  720. }
  721. if (!source_track) {
  722. av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
  723. break;
  724. }
  725. }
  726. if (!source_track)
  727. continue;
  728. st = av_new_stream(mxf->fc, source_track->track_id);
  729. if (!st) {
  730. av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
  731. return -1;
  732. }
  733. st->priv_data = source_track;
  734. st->duration = component->duration;
  735. if (st->duration == -1)
  736. st->duration = AV_NOPTS_VALUE;
  737. st->start_time = component->start_position;
  738. av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
  739. if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
  740. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
  741. return -1;
  742. }
  743. #ifdef DEBUG
  744. PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
  745. #endif
  746. st->codec->codec_type = mxf_get_codec_type(mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
  747. source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType);
  748. if (source_package->descriptor) {
  749. if (source_package->descriptor->type == MultipleDescriptor) {
  750. for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) {
  751. MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor);
  752. if (!sub_descriptor) {
  753. av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
  754. continue;
  755. }
  756. if (sub_descriptor->linked_track_id == source_track->track_id) {
  757. descriptor = sub_descriptor;
  758. break;
  759. }
  760. }
  761. } else if (source_package->descriptor->type == Descriptor)
  762. descriptor = source_package->descriptor;
  763. }
  764. if (!descriptor) {
  765. av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
  766. continue;
  767. }
  768. #ifdef DEBUG
  769. PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
  770. PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
  771. #endif
  772. essence_container_ul = &descriptor->essence_container_ul;
  773. /* HACK: replacing the original key with mxf_encrypted_essence_container
  774. * is not allowed according to s429-6, try to find correct information anyway */
  775. if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
  776. av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
  777. for (k = 0; k < mxf->metadata_sets_count; k++) {
  778. MXFMetadataSet *metadata = mxf->metadata_sets[k];
  779. if (metadata->type == CryptoContext) {
  780. essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul;
  781. break;
  782. }
  783. }
  784. }
  785. /* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
  786. codec_ul = mxf_get_codec_ul(mxf_codec_uls, &descriptor->essence_codec_ul);
  787. st->codec->codec_id = codec_ul->id;
  788. if (descriptor->extradata) {
  789. st->codec->extradata = descriptor->extradata;
  790. st->codec->extradata_size = descriptor->extradata_size;
  791. }
  792. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  793. container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, essence_container_ul);
  794. if (st->codec->codec_id == CODEC_ID_NONE)
  795. st->codec->codec_id = container_ul->id;
  796. st->codec->width = descriptor->width;
  797. st->codec->height = descriptor->height;
  798. st->codec->bits_per_sample = descriptor->bits_per_sample; /* Uncompressed */
  799. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  800. } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  801. container_ul = mxf_get_codec_ul(mxf_sound_essence_container_uls, essence_container_ul);
  802. if (st->codec->codec_id == CODEC_ID_NONE)
  803. st->codec->codec_id = container_ul->id;
  804. st->codec->channels = descriptor->channels;
  805. st->codec->bits_per_sample = descriptor->bits_per_sample;
  806. st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
  807. /* TODO: implement CODEC_ID_RAWAUDIO */
  808. if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
  809. if (descriptor->bits_per_sample == 24)
  810. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  811. else if (descriptor->bits_per_sample == 32)
  812. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  813. } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
  814. if (descriptor->bits_per_sample == 24)
  815. st->codec->codec_id = CODEC_ID_PCM_S24BE;
  816. else if (descriptor->bits_per_sample == 32)
  817. st->codec->codec_id = CODEC_ID_PCM_S32BE;
  818. } else if (st->codec->codec_id == CODEC_ID_MP2) {
  819. st->need_parsing = AVSTREAM_PARSE_FULL;
  820. }
  821. }
  822. if (st->codec->codec_type != CODEC_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
  823. av_log(mxf->fc, AV_LOG_WARNING, "only frame wrapped mappings are correctly supported\n");
  824. st->need_parsing = AVSTREAM_PARSE_FULL;
  825. }
  826. }
  827. return 0;
  828. }
  829. static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
  830. { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
  831. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
  832. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
  833. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
  834. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence },
  835. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip },
  836. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor },
  837. { { 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 */
  838. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */
  839. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */
  840. { { 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 */
  841. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */
  842. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */
  843. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */
  844. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */
  845. { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
  846. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
  847. };
  848. static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, int (*read_child)(), int ctx_size, enum MXFMetadataSetType type)
  849. {
  850. ByteIOContext *pb = mxf->fc->pb;
  851. MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf;
  852. uint64_t klv_end = url_ftell(pb) + klv->length;
  853. if (!ctx)
  854. return -1;
  855. while (url_ftell(pb) + 4 < klv_end) {
  856. int tag = get_be16(pb);
  857. int size = get_be16(pb); /* KLV specified by 0x53 */
  858. uint64_t next = url_ftell(pb) + size;
  859. UID uid = {0};
  860. if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
  861. av_log(mxf->fc, AV_LOG_ERROR, "local tag 0x%04X with 0 size\n", tag);
  862. continue;
  863. }
  864. if (tag > 0x7FFF) { /* dynamic tag */
  865. int i;
  866. for (i = 0; i < mxf->local_tags_count; i++) {
  867. int local_tag = AV_RB16(mxf->local_tags+i*18);
  868. if (local_tag == tag) {
  869. memcpy(uid, mxf->local_tags+i*18+2, 16);
  870. dprintf(mxf->fc, "local tag 0x%04X\n", local_tag);
  871. #ifdef DEBUG
  872. PRINT_KEY(mxf->fc, "uid", uid);
  873. #endif
  874. }
  875. }
  876. }
  877. if (ctx_size && tag == 0x3C0A)
  878. get_buffer(pb, ctx->uid, 16);
  879. else if (read_child(ctx, pb, tag, size, uid) < 0)
  880. return -1;
  881. url_fseek(pb, next, SEEK_SET);
  882. }
  883. if (ctx_size) ctx->type = type;
  884. return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0;
  885. }
  886. static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
  887. {
  888. MXFContext *mxf = s->priv_data;
  889. KLVPacket klv;
  890. if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {
  891. av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
  892. return -1;
  893. }
  894. url_fseek(s->pb, -14, SEEK_CUR);
  895. mxf->fc = s;
  896. while (!url_feof(s->pb)) {
  897. const MXFMetadataReadTableEntry *metadata;
  898. if (klv_read_packet(&klv, s->pb) < 0)
  899. return -1;
  900. #ifdef DEBUG
  901. PRINT_KEY(s, "read header", klv.key);
  902. #endif
  903. if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
  904. IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
  905. /* FIXME avoid seek */
  906. url_fseek(s->pb, klv.offset, SEEK_SET);
  907. break;
  908. }
  909. for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
  910. if (IS_KLV_KEY(klv.key, metadata->key)) {
  911. int (*read)() = klv.key[5] == 0x53 ? mxf_read_local_tags : metadata->read;
  912. if (read(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type) < 0) {
  913. av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
  914. return -1;
  915. }
  916. break;
  917. }
  918. }
  919. if (!metadata->read)
  920. url_fskip(s->pb, klv.length);
  921. }
  922. return mxf_parse_structural_metadata(mxf);
  923. }
  924. static int mxf_read_close(AVFormatContext *s)
  925. {
  926. MXFContext *mxf = s->priv_data;
  927. int i;
  928. av_freep(&mxf->packages_refs);
  929. for (i = 0; i < mxf->metadata_sets_count; i++) {
  930. switch (mxf->metadata_sets[i]->type) {
  931. case MultipleDescriptor:
  932. av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs);
  933. break;
  934. case Sequence:
  935. av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs);
  936. break;
  937. case SourcePackage:
  938. case MaterialPackage:
  939. av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
  940. break;
  941. default:
  942. break;
  943. }
  944. av_freep(&mxf->metadata_sets[i]);
  945. }
  946. av_freep(&mxf->metadata_sets);
  947. av_freep(&mxf->aesc);
  948. av_freep(&mxf->local_tags);
  949. return 0;
  950. }
  951. static int mxf_probe(AVProbeData *p) {
  952. uint8_t *bufp = p->buf;
  953. uint8_t *end = p->buf + p->buf_size;
  954. if (p->buf_size < sizeof(mxf_header_partition_pack_key))
  955. return 0;
  956. /* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
  957. end -= sizeof(mxf_header_partition_pack_key);
  958. for (; bufp < end; bufp++) {
  959. if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
  960. return AVPROBE_SCORE_MAX;
  961. }
  962. return 0;
  963. }
  964. /* rudimentary byte seek */
  965. /* XXX: use MXF Index */
  966. static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  967. {
  968. AVStream *st = s->streams[stream_index];
  969. int64_t seconds;
  970. if (!s->bit_rate)
  971. return -1;
  972. if (sample_time < 0)
  973. sample_time = 0;
  974. seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
  975. url_fseek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
  976. av_update_cur_dts(s, st, sample_time);
  977. return 0;
  978. }
  979. AVInputFormat mxf_demuxer = {
  980. "mxf",
  981. "MXF format",
  982. sizeof(MXFContext),
  983. mxf_probe,
  984. mxf_read_header,
  985. mxf_read_packet,
  986. mxf_read_close,
  987. mxf_read_seek,
  988. };