mxfdec.c 39 KB

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