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.

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