mxfdec.c 38 KB

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