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.

3241 lines
107KB

  1. /*
  2. * Matroska file demuxer (no muxer yet)
  3. * Copyright (c) 2003-2004 The ffmpeg Project
  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. * @file matroskadec.c
  23. * Matroska file demuxer
  24. * by Ronald Bultje <rbultje@ronald.bitfreak.net>
  25. * with a little help from Moritz Bunkus <moritz@bunkus.org>
  26. * Specs available on the matroska project page:
  27. * http://www.matroska.org/.
  28. */
  29. #include "avformat.h"
  30. /* For codec_get_id(). */
  31. #include "riff.h"
  32. #include "matroska.h"
  33. #include "libavcodec/mpeg4audio.h"
  34. #include "libavutil/intfloat_readwrite.h"
  35. #include "libavutil/lzo.h"
  36. #ifdef CONFIG_ZLIB
  37. #include <zlib.h>
  38. #endif
  39. #ifdef CONFIG_BZLIB
  40. #include <bzlib.h>
  41. #endif
  42. typedef struct Track {
  43. MatroskaTrackType type;
  44. /* Unique track number and track ID. stream_index is the index that
  45. * the calling app uses for this track. */
  46. uint32_t num;
  47. uint32_t uid;
  48. int stream_index;
  49. char *name;
  50. char language[4];
  51. char *codec_id;
  52. char *codec_name;
  53. unsigned char *codec_priv;
  54. int codec_priv_size;
  55. uint64_t default_duration;
  56. MatroskaTrackFlags flags;
  57. int encoding_scope;
  58. MatroskaTrackEncodingCompAlgo encoding_algo;
  59. uint8_t *encoding_settings;
  60. int encoding_settings_len;
  61. } MatroskaTrack;
  62. typedef struct MatroskaVideoTrack {
  63. MatroskaTrack track;
  64. int pixel_width;
  65. int pixel_height;
  66. int display_width;
  67. int display_height;
  68. uint32_t fourcc;
  69. MatroskaAspectRatioMode ar_mode;
  70. MatroskaEyeMode eye_mode;
  71. //..
  72. } MatroskaVideoTrack;
  73. typedef struct MatroskaAudioTrack {
  74. MatroskaTrack track;
  75. int channels;
  76. int bitdepth;
  77. int internal_samplerate;
  78. int samplerate;
  79. int block_align;
  80. /* real audio header */
  81. int coded_framesize;
  82. int sub_packet_h;
  83. int frame_size;
  84. int sub_packet_size;
  85. int sub_packet_cnt;
  86. int pkt_cnt;
  87. uint8_t *buf;
  88. //..
  89. } MatroskaAudioTrack;
  90. typedef struct MatroskaSubtitleTrack {
  91. MatroskaTrack track;
  92. //..
  93. } MatroskaSubtitleTrack;
  94. #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
  95. sizeof(MatroskaAudioTrack), \
  96. sizeof(MatroskaSubtitleTrack)))
  97. typedef struct MatroskaLevel {
  98. uint64_t start;
  99. uint64_t length;
  100. } MatroskaLevel;
  101. typedef struct MatroskaDemuxIndex {
  102. uint64_t pos; /* of the corresponding *cluster*! */
  103. uint16_t track; /* reference to 'num' */
  104. uint64_t time; /* in nanoseconds */
  105. } MatroskaDemuxIndex;
  106. typedef struct MatroskaDemuxContext {
  107. AVFormatContext *ctx;
  108. /* ebml stuff */
  109. int num_levels;
  110. MatroskaLevel levels[EBML_MAX_DEPTH];
  111. int level_up;
  112. /* matroska stuff */
  113. char *writing_app;
  114. char *muxing_app;
  115. int64_t created;
  116. /* timescale in the file */
  117. int64_t time_scale;
  118. /* num_streams is the number of streams that av_new_stream() was called
  119. * for ( = that are available to the calling program). */
  120. int num_tracks;
  121. int num_streams;
  122. MatroskaTrack *tracks[MAX_STREAMS];
  123. /* cache for ID peeking */
  124. uint32_t peek_id;
  125. /* byte position of the segment inside the stream */
  126. offset_t segment_start;
  127. /* The packet queue. */
  128. AVPacket **packets;
  129. int num_packets;
  130. /* have we already parse metadata/cues/clusters? */
  131. int metadata_parsed;
  132. int index_parsed;
  133. int done;
  134. /* The index for seeking. */
  135. int num_indexes;
  136. MatroskaDemuxIndex *index;
  137. /* What to skip before effectively reading a packet. */
  138. int skip_to_keyframe;
  139. AVStream *skip_to_stream;
  140. } MatroskaDemuxContext;
  141. /*
  142. * The first few functions handle EBML file parsing. The rest
  143. * is the document interpretation. Matroska really just is a
  144. * EBML file.
  145. */
  146. /*
  147. * Return: the amount of levels in the hierarchy that the
  148. * current element lies higher than the previous one.
  149. * The opposite isn't done - that's auto-done using master
  150. * element reading.
  151. */
  152. static int
  153. ebml_read_element_level_up (MatroskaDemuxContext *matroska)
  154. {
  155. ByteIOContext *pb = matroska->ctx->pb;
  156. offset_t pos = url_ftell(pb);
  157. int num = 0;
  158. while (matroska->num_levels > 0) {
  159. MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
  160. if (pos >= level->start + level->length) {
  161. matroska->num_levels--;
  162. num++;
  163. } else {
  164. break;
  165. }
  166. }
  167. return num;
  168. }
  169. /*
  170. * Read: an "EBML number", which is defined as a variable-length
  171. * array of bytes. The first byte indicates the length by giving a
  172. * number of 0-bits followed by a one. The position of the first
  173. * "one" bit inside the first byte indicates the length of this
  174. * number.
  175. * Returns: num. of bytes read. < 0 on error.
  176. */
  177. static int
  178. ebml_read_num (MatroskaDemuxContext *matroska,
  179. int max_size,
  180. uint64_t *number)
  181. {
  182. ByteIOContext *pb = matroska->ctx->pb;
  183. int len_mask = 0x80, read = 1, n = 1;
  184. int64_t total = 0;
  185. /* the first byte tells us the length in bytes - get_byte() can normally
  186. * return 0, but since that's not a valid first ebmlID byte, we can
  187. * use it safely here to catch EOS. */
  188. if (!(total = get_byte(pb))) {
  189. /* we might encounter EOS here */
  190. if (!url_feof(pb)) {
  191. offset_t pos = url_ftell(pb);
  192. av_log(matroska->ctx, AV_LOG_ERROR,
  193. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
  194. pos, pos);
  195. }
  196. return AVERROR(EIO); /* EOS or actual I/O error */
  197. }
  198. /* get the length of the EBML number */
  199. while (read <= max_size && !(total & len_mask)) {
  200. read++;
  201. len_mask >>= 1;
  202. }
  203. if (read > max_size) {
  204. offset_t pos = url_ftell(pb) - 1;
  205. av_log(matroska->ctx, AV_LOG_ERROR,
  206. "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
  207. (uint8_t) total, pos, pos);
  208. return AVERROR_INVALIDDATA;
  209. }
  210. /* read out length */
  211. total &= ~len_mask;
  212. while (n++ < read)
  213. total = (total << 8) | get_byte(pb);
  214. *number = total;
  215. return read;
  216. }
  217. /*
  218. * Read: the element content data ID.
  219. * Return: the number of bytes read or < 0 on error.
  220. */
  221. static int
  222. ebml_read_element_id (MatroskaDemuxContext *matroska,
  223. uint32_t *id,
  224. int *level_up)
  225. {
  226. int read;
  227. uint64_t total;
  228. /* if we re-call this, use our cached ID */
  229. if (matroska->peek_id != 0) {
  230. if (level_up)
  231. *level_up = 0;
  232. *id = matroska->peek_id;
  233. return 0;
  234. }
  235. /* read out the "EBML number", include tag in ID */
  236. if ((read = ebml_read_num(matroska, 4, &total)) < 0)
  237. return read;
  238. *id = matroska->peek_id = total | (1 << (read * 7));
  239. /* level tracking */
  240. if (level_up)
  241. *level_up = ebml_read_element_level_up(matroska);
  242. return read;
  243. }
  244. /*
  245. * Read: element content length.
  246. * Return: the number of bytes read or < 0 on error.
  247. */
  248. static int
  249. ebml_read_element_length (MatroskaDemuxContext *matroska,
  250. uint64_t *length)
  251. {
  252. /* clear cache since we're now beyond that data point */
  253. matroska->peek_id = 0;
  254. /* read out the "EBML number", include tag in ID */
  255. return ebml_read_num(matroska, 8, length);
  256. }
  257. /*
  258. * Return: the ID of the next element, or 0 on error.
  259. * Level_up contains the amount of levels that this
  260. * next element lies higher than the previous one.
  261. */
  262. static uint32_t
  263. ebml_peek_id (MatroskaDemuxContext *matroska,
  264. int *level_up)
  265. {
  266. uint32_t id;
  267. if (ebml_read_element_id(matroska, &id, level_up) < 0)
  268. return 0;
  269. return id;
  270. }
  271. /*
  272. * Seek to a given offset.
  273. * 0 is success, -1 is failure.
  274. */
  275. static int
  276. ebml_read_seek (MatroskaDemuxContext *matroska,
  277. offset_t offset)
  278. {
  279. ByteIOContext *pb = matroska->ctx->pb;
  280. /* clear ID cache, if any */
  281. matroska->peek_id = 0;
  282. return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
  283. }
  284. /*
  285. * Skip the next element.
  286. * 0 is success, -1 is failure.
  287. */
  288. static int
  289. ebml_read_skip (MatroskaDemuxContext *matroska)
  290. {
  291. ByteIOContext *pb = matroska->ctx->pb;
  292. uint32_t id;
  293. uint64_t length;
  294. int res;
  295. if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
  296. (res = ebml_read_element_length(matroska, &length)) < 0)
  297. return res;
  298. url_fskip(pb, length);
  299. return 0;
  300. }
  301. /*
  302. * Read the next element as an unsigned int.
  303. * 0 is success, < 0 is failure.
  304. */
  305. static int
  306. ebml_read_uint (MatroskaDemuxContext *matroska,
  307. uint32_t *id,
  308. uint64_t *num)
  309. {
  310. ByteIOContext *pb = matroska->ctx->pb;
  311. int n = 0, size, res;
  312. uint64_t rlength;
  313. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  314. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  315. return res;
  316. size = rlength;
  317. if (size < 1 || size > 8) {
  318. offset_t pos = url_ftell(pb);
  319. av_log(matroska->ctx, AV_LOG_ERROR,
  320. "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
  321. size, pos, pos);
  322. return AVERROR_INVALIDDATA;
  323. }
  324. /* big-endian ordening; build up number */
  325. *num = 0;
  326. while (n++ < size)
  327. *num = (*num << 8) | get_byte(pb);
  328. return 0;
  329. }
  330. /*
  331. * Read the next element as a signed int.
  332. * 0 is success, < 0 is failure.
  333. */
  334. static int
  335. ebml_read_sint (MatroskaDemuxContext *matroska,
  336. uint32_t *id,
  337. int64_t *num)
  338. {
  339. ByteIOContext *pb = matroska->ctx->pb;
  340. int size, n = 1, negative = 0, res;
  341. uint64_t rlength;
  342. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  343. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  344. return res;
  345. size = rlength;
  346. if (size < 1 || size > 8) {
  347. offset_t pos = url_ftell(pb);
  348. av_log(matroska->ctx, AV_LOG_ERROR,
  349. "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
  350. size, pos, pos);
  351. return AVERROR_INVALIDDATA;
  352. }
  353. if ((*num = get_byte(pb)) & 0x80) {
  354. negative = 1;
  355. *num &= ~0x80;
  356. }
  357. while (n++ < size)
  358. *num = (*num << 8) | get_byte(pb);
  359. /* make signed */
  360. if (negative)
  361. *num = *num - (1LL << ((8 * size) - 1));
  362. return 0;
  363. }
  364. /*
  365. * Read the next element as a float.
  366. * 0 is success, < 0 is failure.
  367. */
  368. static int
  369. ebml_read_float (MatroskaDemuxContext *matroska,
  370. uint32_t *id,
  371. double *num)
  372. {
  373. ByteIOContext *pb = matroska->ctx->pb;
  374. int size, res;
  375. uint64_t rlength;
  376. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  377. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  378. return res;
  379. size = rlength;
  380. if (size == 4) {
  381. *num= av_int2flt(get_be32(pb));
  382. } else if(size==8){
  383. *num= av_int2dbl(get_be64(pb));
  384. } else{
  385. offset_t pos = url_ftell(pb);
  386. av_log(matroska->ctx, AV_LOG_ERROR,
  387. "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
  388. size, pos, pos);
  389. return AVERROR_INVALIDDATA;
  390. }
  391. return 0;
  392. }
  393. /*
  394. * Read the next element as an ASCII string.
  395. * 0 is success, < 0 is failure.
  396. */
  397. static int
  398. ebml_read_ascii (MatroskaDemuxContext *matroska,
  399. uint32_t *id,
  400. char **str)
  401. {
  402. ByteIOContext *pb = matroska->ctx->pb;
  403. int size, res;
  404. uint64_t rlength;
  405. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  406. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  407. return res;
  408. size = rlength;
  409. /* ebml strings are usually not 0-terminated, so we allocate one
  410. * byte more, read the string and NULL-terminate it ourselves. */
  411. if (size < 0 || !(*str = av_malloc(size + 1))) {
  412. av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
  413. return AVERROR(ENOMEM);
  414. }
  415. if (get_buffer(pb, (uint8_t *) *str, size) != size) {
  416. offset_t pos = url_ftell(pb);
  417. av_log(matroska->ctx, AV_LOG_ERROR,
  418. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
  419. return AVERROR(EIO);
  420. }
  421. (*str)[size] = '\0';
  422. return 0;
  423. }
  424. /*
  425. * Read the next element as a UTF-8 string.
  426. * 0 is success, < 0 is failure.
  427. */
  428. static int
  429. ebml_read_utf8 (MatroskaDemuxContext *matroska,
  430. uint32_t *id,
  431. char **str)
  432. {
  433. return ebml_read_ascii(matroska, id, str);
  434. }
  435. /*
  436. * Read the next element as a date (nanoseconds since 1/1/2000).
  437. * 0 is success, < 0 is failure.
  438. */
  439. static int
  440. ebml_read_date (MatroskaDemuxContext *matroska,
  441. uint32_t *id,
  442. int64_t *date)
  443. {
  444. return ebml_read_sint(matroska, id, date);
  445. }
  446. /*
  447. * Read the next element, but only the header. The contents
  448. * are supposed to be sub-elements which can be read separately.
  449. * 0 is success, < 0 is failure.
  450. */
  451. static int
  452. ebml_read_master (MatroskaDemuxContext *matroska,
  453. uint32_t *id)
  454. {
  455. ByteIOContext *pb = matroska->ctx->pb;
  456. uint64_t length;
  457. MatroskaLevel *level;
  458. int res;
  459. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  460. (res = ebml_read_element_length(matroska, &length)) < 0)
  461. return res;
  462. /* protect... (Heaven forbids that the '>' is true) */
  463. if (matroska->num_levels >= EBML_MAX_DEPTH) {
  464. av_log(matroska->ctx, AV_LOG_ERROR,
  465. "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
  466. return AVERROR(ENOSYS);
  467. }
  468. /* remember level */
  469. level = &matroska->levels[matroska->num_levels++];
  470. level->start = url_ftell(pb);
  471. level->length = length;
  472. return 0;
  473. }
  474. /*
  475. * Read the next element as binary data.
  476. * 0 is success, < 0 is failure.
  477. */
  478. static int
  479. ebml_read_binary (MatroskaDemuxContext *matroska,
  480. uint32_t *id,
  481. uint8_t **binary,
  482. int *size)
  483. {
  484. ByteIOContext *pb = matroska->ctx->pb;
  485. uint64_t rlength;
  486. int res;
  487. if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
  488. (res = ebml_read_element_length(matroska, &rlength)) < 0)
  489. return res;
  490. *size = rlength;
  491. if (!(*binary = av_malloc(*size))) {
  492. av_log(matroska->ctx, AV_LOG_ERROR,
  493. "Memory allocation error\n");
  494. return AVERROR(ENOMEM);
  495. }
  496. if (get_buffer(pb, *binary, *size) != *size) {
  497. offset_t pos = url_ftell(pb);
  498. av_log(matroska->ctx, AV_LOG_ERROR,
  499. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
  500. return AVERROR(EIO);
  501. }
  502. return 0;
  503. }
  504. /*
  505. * Read signed/unsigned "EBML" numbers.
  506. * Return: number of bytes processed, < 0 on error.
  507. * XXX: use ebml_read_num().
  508. */
  509. static int
  510. matroska_ebmlnum_uint (uint8_t *data,
  511. uint32_t size,
  512. uint64_t *num)
  513. {
  514. int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
  515. uint64_t total;
  516. if (size <= 0)
  517. return AVERROR_INVALIDDATA;
  518. total = data[0];
  519. while (read <= 8 && !(total & len_mask)) {
  520. read++;
  521. len_mask >>= 1;
  522. }
  523. if (read > 8)
  524. return AVERROR_INVALIDDATA;
  525. if ((total &= (len_mask - 1)) == len_mask - 1)
  526. num_ffs++;
  527. if (size < read)
  528. return AVERROR_INVALIDDATA;
  529. while (n < read) {
  530. if (data[n] == 0xff)
  531. num_ffs++;
  532. total = (total << 8) | data[n];
  533. n++;
  534. }
  535. if (read == num_ffs)
  536. *num = (uint64_t)-1;
  537. else
  538. *num = total;
  539. return read;
  540. }
  541. /*
  542. * Same as above, but signed.
  543. */
  544. static int
  545. matroska_ebmlnum_sint (uint8_t *data,
  546. uint32_t size,
  547. int64_t *num)
  548. {
  549. uint64_t unum;
  550. int res;
  551. /* read as unsigned number first */
  552. if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
  553. return res;
  554. /* make signed (weird way) */
  555. if (unum == (uint64_t)-1)
  556. *num = INT64_MAX;
  557. else
  558. *num = unum - ((1LL << ((7 * res) - 1)) - 1);
  559. return res;
  560. }
  561. /*
  562. * Read an EBML header.
  563. * 0 is success, < 0 is failure.
  564. */
  565. static int
  566. ebml_read_header (MatroskaDemuxContext *matroska,
  567. char **doctype,
  568. int *version)
  569. {
  570. uint32_t id;
  571. int level_up, res = 0;
  572. /* default init */
  573. if (doctype)
  574. *doctype = NULL;
  575. if (version)
  576. *version = 1;
  577. if (!(id = ebml_peek_id(matroska, &level_up)) ||
  578. level_up != 0 || id != EBML_ID_HEADER) {
  579. av_log(matroska->ctx, AV_LOG_ERROR,
  580. "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
  581. return AVERROR_INVALIDDATA;
  582. }
  583. if ((res = ebml_read_master(matroska, &id)) < 0)
  584. return res;
  585. while (res == 0) {
  586. if (!(id = ebml_peek_id(matroska, &level_up)))
  587. return AVERROR(EIO);
  588. /* end-of-header */
  589. if (level_up)
  590. break;
  591. switch (id) {
  592. /* is our read version uptodate? */
  593. case EBML_ID_EBMLREADVERSION: {
  594. uint64_t num;
  595. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  596. return res;
  597. if (num > EBML_VERSION) {
  598. av_log(matroska->ctx, AV_LOG_ERROR,
  599. "EBML version %"PRIu64" (> %d) is not supported\n",
  600. num, EBML_VERSION);
  601. return AVERROR_INVALIDDATA;
  602. }
  603. break;
  604. }
  605. /* we only handle 8 byte lengths at max */
  606. case EBML_ID_EBMLMAXSIZELENGTH: {
  607. uint64_t num;
  608. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  609. return res;
  610. if (num > sizeof(uint64_t)) {
  611. av_log(matroska->ctx, AV_LOG_ERROR,
  612. "Integers of size %"PRIu64" (> %zd) not supported\n",
  613. num, sizeof(uint64_t));
  614. return AVERROR_INVALIDDATA;
  615. }
  616. break;
  617. }
  618. /* we handle 4 byte IDs at max */
  619. case EBML_ID_EBMLMAXIDLENGTH: {
  620. uint64_t num;
  621. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  622. return res;
  623. if (num > sizeof(uint32_t)) {
  624. av_log(matroska->ctx, AV_LOG_ERROR,
  625. "IDs of size %"PRIu64" (> %zu) not supported\n",
  626. num, sizeof(uint32_t));
  627. return AVERROR_INVALIDDATA;
  628. }
  629. break;
  630. }
  631. case EBML_ID_DOCTYPE: {
  632. char *text;
  633. if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
  634. return res;
  635. if (doctype) {
  636. if (*doctype)
  637. av_free(*doctype);
  638. *doctype = text;
  639. } else
  640. av_free(text);
  641. break;
  642. }
  643. case EBML_ID_DOCTYPEREADVERSION: {
  644. uint64_t num;
  645. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  646. return res;
  647. if (version)
  648. *version = num;
  649. break;
  650. }
  651. default:
  652. av_log(matroska->ctx, AV_LOG_INFO,
  653. "Unknown data type 0x%x in EBML header", id);
  654. /* pass-through */
  655. case EBML_ID_VOID:
  656. /* we ignore these two, as they don't tell us anything we
  657. * care about */
  658. case EBML_ID_EBMLVERSION:
  659. case EBML_ID_DOCTYPEVERSION:
  660. res = ebml_read_skip (matroska);
  661. break;
  662. }
  663. }
  664. return 0;
  665. }
  666. static int
  667. matroska_find_track_by_num (MatroskaDemuxContext *matroska,
  668. int num)
  669. {
  670. int i;
  671. for (i = 0; i < matroska->num_tracks; i++)
  672. if (matroska->tracks[i]->num == num)
  673. return i;
  674. return -1;
  675. }
  676. /*
  677. * Put one packet in an application-supplied AVPacket struct.
  678. * Returns 0 on success or -1 on failure.
  679. */
  680. static int
  681. matroska_deliver_packet (MatroskaDemuxContext *matroska,
  682. AVPacket *pkt)
  683. {
  684. if (matroska->num_packets > 0) {
  685. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  686. av_free(matroska->packets[0]);
  687. if (matroska->num_packets > 1) {
  688. memmove(&matroska->packets[0], &matroska->packets[1],
  689. (matroska->num_packets - 1) * sizeof(AVPacket *));
  690. matroska->packets =
  691. av_realloc(matroska->packets, (matroska->num_packets - 1) *
  692. sizeof(AVPacket *));
  693. } else {
  694. av_freep(&matroska->packets);
  695. }
  696. matroska->num_packets--;
  697. return 0;
  698. }
  699. return -1;
  700. }
  701. /*
  702. * Put a packet into our internal queue. Will be delivered to the
  703. * user/application during the next get_packet() call.
  704. */
  705. static void
  706. matroska_queue_packet (MatroskaDemuxContext *matroska,
  707. AVPacket *pkt)
  708. {
  709. matroska->packets =
  710. av_realloc(matroska->packets, (matroska->num_packets + 1) *
  711. sizeof(AVPacket *));
  712. matroska->packets[matroska->num_packets] = pkt;
  713. matroska->num_packets++;
  714. }
  715. /*
  716. * Free all packets in our internal queue.
  717. */
  718. static void
  719. matroska_clear_queue (MatroskaDemuxContext *matroska)
  720. {
  721. if (matroska->packets) {
  722. int n;
  723. for (n = 0; n < matroska->num_packets; n++) {
  724. av_free_packet(matroska->packets[n]);
  725. av_free(matroska->packets[n]);
  726. }
  727. av_free(matroska->packets);
  728. matroska->packets = NULL;
  729. matroska->num_packets = 0;
  730. }
  731. }
  732. /*
  733. * Autodetecting...
  734. */
  735. static int
  736. matroska_probe (AVProbeData *p)
  737. {
  738. uint64_t total = 0;
  739. int len_mask = 0x80, size = 1, n = 1;
  740. uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
  741. /* ebml header? */
  742. if (AV_RB32(p->buf) != EBML_ID_HEADER)
  743. return 0;
  744. /* length of header */
  745. total = p->buf[4];
  746. while (size <= 8 && !(total & len_mask)) {
  747. size++;
  748. len_mask >>= 1;
  749. }
  750. if (size > 8)
  751. return 0;
  752. total &= (len_mask - 1);
  753. while (n < size)
  754. total = (total << 8) | p->buf[4 + n++];
  755. /* does the probe data contain the whole header? */
  756. if (p->buf_size < 4 + size + total)
  757. return 0;
  758. /* the header must contain the document type 'matroska'. For now,
  759. * we don't parse the whole header but simply check for the
  760. * availability of that array of characters inside the header.
  761. * Not fully fool-proof, but good enough. */
  762. for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
  763. if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
  764. return AVPROBE_SCORE_MAX;
  765. return 0;
  766. }
  767. /*
  768. * From here on, it's all XML-style DTD stuff... Needs no comments.
  769. */
  770. static int
  771. matroska_parse_info (MatroskaDemuxContext *matroska)
  772. {
  773. int res = 0;
  774. uint32_t id;
  775. av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
  776. while (res == 0) {
  777. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  778. res = AVERROR(EIO);
  779. break;
  780. } else if (matroska->level_up) {
  781. matroska->level_up--;
  782. break;
  783. }
  784. switch (id) {
  785. /* cluster timecode */
  786. case MATROSKA_ID_TIMECODESCALE: {
  787. uint64_t num;
  788. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  789. break;
  790. matroska->time_scale = num;
  791. break;
  792. }
  793. case MATROSKA_ID_DURATION: {
  794. double num;
  795. if ((res = ebml_read_float(matroska, &id, &num)) < 0)
  796. break;
  797. matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
  798. break;
  799. }
  800. case MATROSKA_ID_TITLE: {
  801. char *text;
  802. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  803. break;
  804. strncpy(matroska->ctx->title, text,
  805. sizeof(matroska->ctx->title)-1);
  806. av_free(text);
  807. break;
  808. }
  809. case MATROSKA_ID_WRITINGAPP: {
  810. char *text;
  811. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  812. break;
  813. matroska->writing_app = text;
  814. break;
  815. }
  816. case MATROSKA_ID_MUXINGAPP: {
  817. char *text;
  818. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  819. break;
  820. matroska->muxing_app = text;
  821. break;
  822. }
  823. case MATROSKA_ID_DATEUTC: {
  824. int64_t time;
  825. if ((res = ebml_read_date(matroska, &id, &time)) < 0)
  826. break;
  827. matroska->created = time;
  828. break;
  829. }
  830. default:
  831. av_log(matroska->ctx, AV_LOG_INFO,
  832. "Unknown entry 0x%x in info header\n", id);
  833. /* fall-through */
  834. case EBML_ID_VOID:
  835. res = ebml_read_skip(matroska);
  836. break;
  837. }
  838. if (matroska->level_up) {
  839. matroska->level_up--;
  840. break;
  841. }
  842. }
  843. return res;
  844. }
  845. static int
  846. matroska_add_stream (MatroskaDemuxContext *matroska)
  847. {
  848. int res = 0;
  849. uint32_t id;
  850. MatroskaTrack *track;
  851. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
  852. /* Allocate a generic track. As soon as we know its type we'll realloc. */
  853. track = av_mallocz(MAX_TRACK_SIZE);
  854. matroska->num_tracks++;
  855. strcpy(track->language, "eng");
  856. /* start with the master */
  857. if ((res = ebml_read_master(matroska, &id)) < 0)
  858. return res;
  859. /* try reading the trackentry headers */
  860. while (res == 0) {
  861. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  862. res = AVERROR(EIO);
  863. break;
  864. } else if (matroska->level_up > 0) {
  865. matroska->level_up--;
  866. break;
  867. }
  868. switch (id) {
  869. /* track number (unique stream ID) */
  870. case MATROSKA_ID_TRACKNUMBER: {
  871. uint64_t num;
  872. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  873. break;
  874. track->num = num;
  875. break;
  876. }
  877. /* track UID (unique identifier) */
  878. case MATROSKA_ID_TRACKUID: {
  879. uint64_t num;
  880. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  881. break;
  882. track->uid = num;
  883. break;
  884. }
  885. /* track type (video, audio, combined, subtitle, etc.) */
  886. case MATROSKA_ID_TRACKTYPE: {
  887. uint64_t num;
  888. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  889. break;
  890. if (track->type && track->type != num) {
  891. av_log(matroska->ctx, AV_LOG_INFO,
  892. "More than one tracktype in an entry - skip\n");
  893. break;
  894. }
  895. track->type = num;
  896. switch (track->type) {
  897. case MATROSKA_TRACK_TYPE_VIDEO:
  898. case MATROSKA_TRACK_TYPE_AUDIO:
  899. case MATROSKA_TRACK_TYPE_SUBTITLE:
  900. break;
  901. case MATROSKA_TRACK_TYPE_COMPLEX:
  902. case MATROSKA_TRACK_TYPE_LOGO:
  903. case MATROSKA_TRACK_TYPE_CONTROL:
  904. default:
  905. av_log(matroska->ctx, AV_LOG_INFO,
  906. "Unknown or unsupported track type 0x%x\n",
  907. track->type);
  908. track->type = MATROSKA_TRACK_TYPE_NONE;
  909. break;
  910. }
  911. matroska->tracks[matroska->num_tracks - 1] = track;
  912. break;
  913. }
  914. /* tracktype specific stuff for video */
  915. case MATROSKA_ID_TRACKVIDEO: {
  916. MatroskaVideoTrack *videotrack;
  917. if (!track->type)
  918. track->type = MATROSKA_TRACK_TYPE_VIDEO;
  919. if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
  920. av_log(matroska->ctx, AV_LOG_INFO,
  921. "video data in non-video track - ignoring\n");
  922. res = AVERROR_INVALIDDATA;
  923. break;
  924. } else if ((res = ebml_read_master(matroska, &id)) < 0)
  925. break;
  926. videotrack = (MatroskaVideoTrack *)track;
  927. while (res == 0) {
  928. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  929. res = AVERROR(EIO);
  930. break;
  931. } else if (matroska->level_up > 0) {
  932. matroska->level_up--;
  933. break;
  934. }
  935. switch (id) {
  936. /* fixme, this should be one-up, but I get it here */
  937. case MATROSKA_ID_TRACKDEFAULTDURATION: {
  938. uint64_t num;
  939. if ((res = ebml_read_uint (matroska, &id,
  940. &num)) < 0)
  941. break;
  942. track->default_duration = num;
  943. break;
  944. }
  945. /* video framerate */
  946. case MATROSKA_ID_VIDEOFRAMERATE: {
  947. double num;
  948. if ((res = ebml_read_float(matroska, &id,
  949. &num)) < 0)
  950. break;
  951. if (!track->default_duration)
  952. track->default_duration = 1000000000/num;
  953. break;
  954. }
  955. /* width of the size to display the video at */
  956. case MATROSKA_ID_VIDEODISPLAYWIDTH: {
  957. uint64_t num;
  958. if ((res = ebml_read_uint(matroska, &id,
  959. &num)) < 0)
  960. break;
  961. videotrack->display_width = num;
  962. break;
  963. }
  964. /* height of the size to display the video at */
  965. case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
  966. uint64_t num;
  967. if ((res = ebml_read_uint(matroska, &id,
  968. &num)) < 0)
  969. break;
  970. videotrack->display_height = num;
  971. break;
  972. }
  973. /* width of the video in the file */
  974. case MATROSKA_ID_VIDEOPIXELWIDTH: {
  975. uint64_t num;
  976. if ((res = ebml_read_uint(matroska, &id,
  977. &num)) < 0)
  978. break;
  979. videotrack->pixel_width = num;
  980. break;
  981. }
  982. /* height of the video in the file */
  983. case MATROSKA_ID_VIDEOPIXELHEIGHT: {
  984. uint64_t num;
  985. if ((res = ebml_read_uint(matroska, &id,
  986. &num)) < 0)
  987. break;
  988. videotrack->pixel_height = num;
  989. break;
  990. }
  991. /* whether the video is interlaced */
  992. case MATROSKA_ID_VIDEOFLAGINTERLACED: {
  993. uint64_t num;
  994. if ((res = ebml_read_uint(matroska, &id,
  995. &num)) < 0)
  996. break;
  997. if (num)
  998. track->flags |=
  999. MATROSKA_VIDEOTRACK_INTERLACED;
  1000. else
  1001. track->flags &=
  1002. ~MATROSKA_VIDEOTRACK_INTERLACED;
  1003. break;
  1004. }
  1005. /* stereo mode (whether the video has two streams,
  1006. * where one is for the left eye and the other for
  1007. * the right eye, which creates a 3D-like
  1008. * effect) */
  1009. case MATROSKA_ID_VIDEOSTEREOMODE: {
  1010. uint64_t num;
  1011. if ((res = ebml_read_uint(matroska, &id,
  1012. &num)) < 0)
  1013. break;
  1014. if (num != MATROSKA_EYE_MODE_MONO &&
  1015. num != MATROSKA_EYE_MODE_LEFT &&
  1016. num != MATROSKA_EYE_MODE_RIGHT &&
  1017. num != MATROSKA_EYE_MODE_BOTH) {
  1018. av_log(matroska->ctx, AV_LOG_INFO,
  1019. "Ignoring unknown eye mode 0x%x\n",
  1020. (uint32_t) num);
  1021. break;
  1022. }
  1023. videotrack->eye_mode = num;
  1024. break;
  1025. }
  1026. /* aspect ratio behaviour */
  1027. case MATROSKA_ID_VIDEOASPECTRATIO: {
  1028. uint64_t num;
  1029. if ((res = ebml_read_uint(matroska, &id,
  1030. &num)) < 0)
  1031. break;
  1032. if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
  1033. num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
  1034. num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
  1035. av_log(matroska->ctx, AV_LOG_INFO,
  1036. "Ignoring unknown aspect ratio 0x%x\n",
  1037. (uint32_t) num);
  1038. break;
  1039. }
  1040. videotrack->ar_mode = num;
  1041. break;
  1042. }
  1043. /* colorspace (only matters for raw video)
  1044. * fourcc */
  1045. case MATROSKA_ID_VIDEOCOLORSPACE: {
  1046. uint64_t num;
  1047. if ((res = ebml_read_uint(matroska, &id,
  1048. &num)) < 0)
  1049. break;
  1050. videotrack->fourcc = num;
  1051. break;
  1052. }
  1053. default:
  1054. av_log(matroska->ctx, AV_LOG_INFO,
  1055. "Unknown video track header entry "
  1056. "0x%x - ignoring\n", id);
  1057. /* pass-through */
  1058. case EBML_ID_VOID:
  1059. res = ebml_read_skip(matroska);
  1060. break;
  1061. }
  1062. if (matroska->level_up) {
  1063. matroska->level_up--;
  1064. break;
  1065. }
  1066. }
  1067. break;
  1068. }
  1069. /* tracktype specific stuff for audio */
  1070. case MATROSKA_ID_TRACKAUDIO: {
  1071. MatroskaAudioTrack *audiotrack;
  1072. if (!track->type)
  1073. track->type = MATROSKA_TRACK_TYPE_AUDIO;
  1074. if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
  1075. av_log(matroska->ctx, AV_LOG_INFO,
  1076. "audio data in non-audio track - ignoring\n");
  1077. res = AVERROR_INVALIDDATA;
  1078. break;
  1079. } else if ((res = ebml_read_master(matroska, &id)) < 0)
  1080. break;
  1081. audiotrack = (MatroskaAudioTrack *)track;
  1082. audiotrack->channels = 1;
  1083. audiotrack->samplerate = 8000;
  1084. while (res == 0) {
  1085. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1086. res = AVERROR(EIO);
  1087. break;
  1088. } else if (matroska->level_up > 0) {
  1089. matroska->level_up--;
  1090. break;
  1091. }
  1092. switch (id) {
  1093. /* samplerate */
  1094. case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
  1095. double num;
  1096. if ((res = ebml_read_float(matroska, &id,
  1097. &num)) < 0)
  1098. break;
  1099. audiotrack->internal_samplerate =
  1100. audiotrack->samplerate = num;
  1101. break;
  1102. }
  1103. case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
  1104. double num;
  1105. if ((res = ebml_read_float(matroska, &id,
  1106. &num)) < 0)
  1107. break;
  1108. audiotrack->samplerate = num;
  1109. break;
  1110. }
  1111. /* bitdepth */
  1112. case MATROSKA_ID_AUDIOBITDEPTH: {
  1113. uint64_t num;
  1114. if ((res = ebml_read_uint(matroska, &id,
  1115. &num)) < 0)
  1116. break;
  1117. audiotrack->bitdepth = num;
  1118. break;
  1119. }
  1120. /* channels */
  1121. case MATROSKA_ID_AUDIOCHANNELS: {
  1122. uint64_t num;
  1123. if ((res = ebml_read_uint(matroska, &id,
  1124. &num)) < 0)
  1125. break;
  1126. audiotrack->channels = num;
  1127. break;
  1128. }
  1129. default:
  1130. av_log(matroska->ctx, AV_LOG_INFO,
  1131. "Unknown audio track header entry "
  1132. "0x%x - ignoring\n", id);
  1133. /* pass-through */
  1134. case EBML_ID_VOID:
  1135. res = ebml_read_skip(matroska);
  1136. break;
  1137. }
  1138. if (matroska->level_up) {
  1139. matroska->level_up--;
  1140. break;
  1141. }
  1142. }
  1143. break;
  1144. }
  1145. /* codec identifier */
  1146. case MATROSKA_ID_CODECID: {
  1147. char *text;
  1148. if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
  1149. break;
  1150. track->codec_id = text;
  1151. break;
  1152. }
  1153. /* codec private data */
  1154. case MATROSKA_ID_CODECPRIVATE: {
  1155. uint8_t *data;
  1156. int size;
  1157. if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
  1158. break;
  1159. track->codec_priv = data;
  1160. track->codec_priv_size = size;
  1161. break;
  1162. }
  1163. /* name of the codec */
  1164. case MATROSKA_ID_CODECNAME: {
  1165. char *text;
  1166. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1167. break;
  1168. track->codec_name = text;
  1169. break;
  1170. }
  1171. /* name of this track */
  1172. case MATROSKA_ID_TRACKNAME: {
  1173. char *text;
  1174. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1175. break;
  1176. track->name = text;
  1177. break;
  1178. }
  1179. /* language (matters for audio/subtitles, mostly) */
  1180. case MATROSKA_ID_TRACKLANGUAGE: {
  1181. char *text, *end;
  1182. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1183. break;
  1184. if ((end = strchr(text, '-')))
  1185. *end = '\0';
  1186. if (strlen(text) == 3)
  1187. strcpy(track->language, text);
  1188. av_free(text);
  1189. break;
  1190. }
  1191. /* whether this is actually used */
  1192. case MATROSKA_ID_TRACKFLAGENABLED: {
  1193. uint64_t num;
  1194. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1195. break;
  1196. if (num)
  1197. track->flags |= MATROSKA_TRACK_ENABLED;
  1198. else
  1199. track->flags &= ~MATROSKA_TRACK_ENABLED;
  1200. break;
  1201. }
  1202. /* whether it's the default for this track type */
  1203. case MATROSKA_ID_TRACKFLAGDEFAULT: {
  1204. uint64_t num;
  1205. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1206. break;
  1207. if (num)
  1208. track->flags |= MATROSKA_TRACK_DEFAULT;
  1209. else
  1210. track->flags &= ~MATROSKA_TRACK_DEFAULT;
  1211. break;
  1212. }
  1213. /* lacing (like MPEG, where blocks don't end/start on frame
  1214. * boundaries) */
  1215. case MATROSKA_ID_TRACKFLAGLACING: {
  1216. uint64_t num;
  1217. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1218. break;
  1219. if (num)
  1220. track->flags |= MATROSKA_TRACK_LACING;
  1221. else
  1222. track->flags &= ~MATROSKA_TRACK_LACING;
  1223. break;
  1224. }
  1225. /* default length (in time) of one data block in this track */
  1226. case MATROSKA_ID_TRACKDEFAULTDURATION: {
  1227. uint64_t num;
  1228. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1229. break;
  1230. track->default_duration = num;
  1231. break;
  1232. }
  1233. case MATROSKA_ID_TRACKCONTENTENCODINGS: {
  1234. if ((res = ebml_read_master(matroska, &id)) < 0)
  1235. break;
  1236. while (res == 0) {
  1237. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1238. res = AVERROR(EIO);
  1239. break;
  1240. } else if (matroska->level_up > 0) {
  1241. matroska->level_up--;
  1242. break;
  1243. }
  1244. switch (id) {
  1245. case MATROSKA_ID_TRACKCONTENTENCODING: {
  1246. int encoding_scope = 1;
  1247. if ((res = ebml_read_master(matroska, &id)) < 0)
  1248. break;
  1249. while (res == 0) {
  1250. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1251. res = AVERROR(EIO);
  1252. break;
  1253. } else if (matroska->level_up > 0) {
  1254. matroska->level_up--;
  1255. break;
  1256. }
  1257. switch (id) {
  1258. case MATROSKA_ID_ENCODINGSCOPE: {
  1259. uint64_t num;
  1260. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1261. break;
  1262. encoding_scope = num;
  1263. break;
  1264. }
  1265. case MATROSKA_ID_ENCODINGTYPE: {
  1266. uint64_t num;
  1267. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1268. break;
  1269. if (num)
  1270. av_log(matroska->ctx, AV_LOG_ERROR,
  1271. "Unsupported encoding type");
  1272. break;
  1273. }
  1274. case MATROSKA_ID_ENCODINGCOMPRESSION: {
  1275. if ((res = ebml_read_master(matroska, &id)) < 0)
  1276. break;
  1277. while (res == 0) {
  1278. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1279. res = AVERROR(EIO);
  1280. break;
  1281. } else if (matroska->level_up > 0) {
  1282. matroska->level_up--;
  1283. break;
  1284. }
  1285. switch (id) {
  1286. case MATROSKA_ID_ENCODINGCOMPALGO: {
  1287. uint64_t num;
  1288. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1289. break;
  1290. if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
  1291. #ifdef CONFIG_ZLIB
  1292. num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1293. #endif
  1294. #ifdef CONFIG_BZLIB
  1295. num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1296. #endif
  1297. num != MATROSKA_TRACK_ENCODING_COMP_LZO)
  1298. av_log(matroska->ctx, AV_LOG_ERROR,
  1299. "Unsupported compression algo\n");
  1300. track->encoding_algo = num;
  1301. break;
  1302. }
  1303. case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
  1304. uint8_t *data;
  1305. int size;
  1306. if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
  1307. break;
  1308. track->encoding_settings = data;
  1309. track->encoding_settings_len = size;
  1310. break;
  1311. }
  1312. default:
  1313. av_log(matroska->ctx, AV_LOG_INFO,
  1314. "Unknown compression header entry "
  1315. "0x%x - ignoring\n", id);
  1316. /* pass-through */
  1317. case EBML_ID_VOID:
  1318. res = ebml_read_skip(matroska);
  1319. break;
  1320. }
  1321. if (matroska->level_up) {
  1322. matroska->level_up--;
  1323. break;
  1324. }
  1325. }
  1326. break;
  1327. }
  1328. default:
  1329. av_log(matroska->ctx, AV_LOG_INFO,
  1330. "Unknown content encoding header entry "
  1331. "0x%x - ignoring\n", id);
  1332. /* pass-through */
  1333. case EBML_ID_VOID:
  1334. res = ebml_read_skip(matroska);
  1335. break;
  1336. }
  1337. if (matroska->level_up) {
  1338. matroska->level_up--;
  1339. break;
  1340. }
  1341. }
  1342. track->encoding_scope = encoding_scope;
  1343. break;
  1344. }
  1345. default:
  1346. av_log(matroska->ctx, AV_LOG_INFO,
  1347. "Unknown content encodings header entry "
  1348. "0x%x - ignoring\n", id);
  1349. /* pass-through */
  1350. case EBML_ID_VOID:
  1351. res = ebml_read_skip(matroska);
  1352. break;
  1353. }
  1354. if (matroska->level_up) {
  1355. matroska->level_up--;
  1356. break;
  1357. }
  1358. }
  1359. break;
  1360. }
  1361. default:
  1362. av_log(matroska->ctx, AV_LOG_INFO,
  1363. "Unknown track header entry 0x%x - ignoring\n", id);
  1364. /* pass-through */
  1365. case EBML_ID_VOID:
  1366. /* we ignore these because they're nothing useful. */
  1367. case MATROSKA_ID_CODECINFOURL:
  1368. case MATROSKA_ID_CODECDOWNLOADURL:
  1369. case MATROSKA_ID_TRACKMINCACHE:
  1370. case MATROSKA_ID_TRACKMAXCACHE:
  1371. res = ebml_read_skip(matroska);
  1372. break;
  1373. }
  1374. if (matroska->level_up) {
  1375. matroska->level_up--;
  1376. break;
  1377. }
  1378. }
  1379. return res;
  1380. }
  1381. static int
  1382. matroska_parse_tracks (MatroskaDemuxContext *matroska)
  1383. {
  1384. int res = 0;
  1385. uint32_t id;
  1386. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
  1387. while (res == 0) {
  1388. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1389. res = AVERROR(EIO);
  1390. break;
  1391. } else if (matroska->level_up) {
  1392. matroska->level_up--;
  1393. break;
  1394. }
  1395. switch (id) {
  1396. /* one track within the "all-tracks" header */
  1397. case MATROSKA_ID_TRACKENTRY:
  1398. res = matroska_add_stream(matroska);
  1399. break;
  1400. default:
  1401. av_log(matroska->ctx, AV_LOG_INFO,
  1402. "Unknown entry 0x%x in track header\n", id);
  1403. /* fall-through */
  1404. case EBML_ID_VOID:
  1405. res = ebml_read_skip(matroska);
  1406. break;
  1407. }
  1408. if (matroska->level_up) {
  1409. matroska->level_up--;
  1410. break;
  1411. }
  1412. }
  1413. return res;
  1414. }
  1415. static int
  1416. matroska_parse_index (MatroskaDemuxContext *matroska)
  1417. {
  1418. int res = 0;
  1419. uint32_t id;
  1420. MatroskaDemuxIndex idx;
  1421. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
  1422. while (res == 0) {
  1423. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1424. res = AVERROR(EIO);
  1425. break;
  1426. } else if (matroska->level_up) {
  1427. matroska->level_up--;
  1428. break;
  1429. }
  1430. switch (id) {
  1431. /* one single index entry ('point') */
  1432. case MATROSKA_ID_POINTENTRY:
  1433. if ((res = ebml_read_master(matroska, &id)) < 0)
  1434. break;
  1435. /* in the end, we hope to fill one entry with a
  1436. * timestamp, a file position and a tracknum */
  1437. idx.pos = (uint64_t) -1;
  1438. idx.time = (uint64_t) -1;
  1439. idx.track = (uint16_t) -1;
  1440. while (res == 0) {
  1441. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1442. res = AVERROR(EIO);
  1443. break;
  1444. } else if (matroska->level_up) {
  1445. matroska->level_up--;
  1446. break;
  1447. }
  1448. switch (id) {
  1449. /* one single index entry ('point') */
  1450. case MATROSKA_ID_CUETIME: {
  1451. uint64_t time;
  1452. if ((res = ebml_read_uint(matroska, &id,
  1453. &time)) < 0)
  1454. break;
  1455. idx.time = time * matroska->time_scale;
  1456. break;
  1457. }
  1458. /* position in the file + track to which it
  1459. * belongs */
  1460. case MATROSKA_ID_CUETRACKPOSITION:
  1461. if ((res = ebml_read_master(matroska, &id)) < 0)
  1462. break;
  1463. while (res == 0) {
  1464. if (!(id = ebml_peek_id (matroska,
  1465. &matroska->level_up))) {
  1466. res = AVERROR(EIO);
  1467. break;
  1468. } else if (matroska->level_up) {
  1469. matroska->level_up--;
  1470. break;
  1471. }
  1472. switch (id) {
  1473. /* track number */
  1474. case MATROSKA_ID_CUETRACK: {
  1475. uint64_t num;
  1476. if ((res = ebml_read_uint(matroska,
  1477. &id, &num)) < 0)
  1478. break;
  1479. idx.track = num;
  1480. break;
  1481. }
  1482. /* position in file */
  1483. case MATROSKA_ID_CUECLUSTERPOSITION: {
  1484. uint64_t num;
  1485. if ((res = ebml_read_uint(matroska,
  1486. &id, &num)) < 0)
  1487. break;
  1488. idx.pos = num+matroska->segment_start;
  1489. break;
  1490. }
  1491. default:
  1492. av_log(matroska->ctx, AV_LOG_INFO,
  1493. "Unknown entry 0x%x in "
  1494. "CuesTrackPositions\n", id);
  1495. /* fall-through */
  1496. case EBML_ID_VOID:
  1497. res = ebml_read_skip(matroska);
  1498. break;
  1499. }
  1500. if (matroska->level_up) {
  1501. matroska->level_up--;
  1502. break;
  1503. }
  1504. }
  1505. break;
  1506. default:
  1507. av_log(matroska->ctx, AV_LOG_INFO,
  1508. "Unknown entry 0x%x in cuespoint "
  1509. "index\n", id);
  1510. /* fall-through */
  1511. case EBML_ID_VOID:
  1512. res = ebml_read_skip(matroska);
  1513. break;
  1514. }
  1515. if (matroska->level_up) {
  1516. matroska->level_up--;
  1517. break;
  1518. }
  1519. }
  1520. /* so let's see if we got what we wanted */
  1521. if (idx.pos != (uint64_t) -1 &&
  1522. idx.time != (uint64_t) -1 &&
  1523. idx.track != (uint16_t) -1) {
  1524. if (matroska->num_indexes % 32 == 0) {
  1525. /* re-allocate bigger index */
  1526. matroska->index =
  1527. av_realloc(matroska->index,
  1528. (matroska->num_indexes + 32) *
  1529. sizeof(MatroskaDemuxIndex));
  1530. }
  1531. matroska->index[matroska->num_indexes] = idx;
  1532. matroska->num_indexes++;
  1533. }
  1534. break;
  1535. default:
  1536. av_log(matroska->ctx, AV_LOG_INFO,
  1537. "Unknown entry 0x%x in cues header\n", id);
  1538. /* fall-through */
  1539. case EBML_ID_VOID:
  1540. res = ebml_read_skip(matroska);
  1541. break;
  1542. }
  1543. if (matroska->level_up) {
  1544. matroska->level_up--;
  1545. break;
  1546. }
  1547. }
  1548. return res;
  1549. }
  1550. static int
  1551. matroska_parse_metadata (MatroskaDemuxContext *matroska)
  1552. {
  1553. int res = 0;
  1554. uint32_t id;
  1555. while (res == 0) {
  1556. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1557. res = AVERROR(EIO);
  1558. break;
  1559. } else if (matroska->level_up) {
  1560. matroska->level_up--;
  1561. break;
  1562. }
  1563. switch (id) {
  1564. /* Hm, this is unsupported... */
  1565. default:
  1566. av_log(matroska->ctx, AV_LOG_INFO,
  1567. "Unknown entry 0x%x in metadata header\n", id);
  1568. /* fall-through */
  1569. case EBML_ID_VOID:
  1570. res = ebml_read_skip(matroska);
  1571. break;
  1572. }
  1573. if (matroska->level_up) {
  1574. matroska->level_up--;
  1575. break;
  1576. }
  1577. }
  1578. return res;
  1579. }
  1580. static int
  1581. matroska_parse_seekhead (MatroskaDemuxContext *matroska)
  1582. {
  1583. int res = 0;
  1584. uint32_t id;
  1585. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
  1586. while (res == 0) {
  1587. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1588. res = AVERROR(EIO);
  1589. break;
  1590. } else if (matroska->level_up) {
  1591. matroska->level_up--;
  1592. break;
  1593. }
  1594. switch (id) {
  1595. case MATROSKA_ID_SEEKENTRY: {
  1596. uint32_t seek_id = 0, peek_id_cache = 0;
  1597. uint64_t seek_pos = (uint64_t) -1, t;
  1598. if ((res = ebml_read_master(matroska, &id)) < 0)
  1599. break;
  1600. while (res == 0) {
  1601. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1602. res = AVERROR(EIO);
  1603. break;
  1604. } else if (matroska->level_up) {
  1605. matroska->level_up--;
  1606. break;
  1607. }
  1608. switch (id) {
  1609. case MATROSKA_ID_SEEKID:
  1610. res = ebml_read_uint(matroska, &id, &t);
  1611. seek_id = t;
  1612. break;
  1613. case MATROSKA_ID_SEEKPOSITION:
  1614. res = ebml_read_uint(matroska, &id, &seek_pos);
  1615. break;
  1616. default:
  1617. av_log(matroska->ctx, AV_LOG_INFO,
  1618. "Unknown seekhead ID 0x%x\n", id);
  1619. /* fall-through */
  1620. case EBML_ID_VOID:
  1621. res = ebml_read_skip(matroska);
  1622. break;
  1623. }
  1624. if (matroska->level_up) {
  1625. matroska->level_up--;
  1626. break;
  1627. }
  1628. }
  1629. if (!seek_id || seek_pos == (uint64_t) -1) {
  1630. av_log(matroska->ctx, AV_LOG_INFO,
  1631. "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
  1632. seek_id, seek_pos);
  1633. break;
  1634. }
  1635. switch (seek_id) {
  1636. case MATROSKA_ID_CUES:
  1637. case MATROSKA_ID_TAGS: {
  1638. uint32_t level_up = matroska->level_up;
  1639. offset_t before_pos;
  1640. uint64_t length;
  1641. MatroskaLevel level;
  1642. /* remember the peeked ID and the current position */
  1643. peek_id_cache = matroska->peek_id;
  1644. before_pos = url_ftell(matroska->ctx->pb);
  1645. /* seek */
  1646. if ((res = ebml_read_seek(matroska, seek_pos +
  1647. matroska->segment_start)) < 0)
  1648. goto finish;
  1649. /* we don't want to lose our seekhead level, so we add
  1650. * a dummy. This is a crude hack. */
  1651. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1652. av_log(matroska->ctx, AV_LOG_INFO,
  1653. "Max EBML element depth (%d) reached, "
  1654. "cannot parse further.\n", EBML_MAX_DEPTH);
  1655. return AVERROR_UNKNOWN;
  1656. }
  1657. level.start = 0;
  1658. level.length = (uint64_t)-1;
  1659. matroska->levels[matroska->num_levels] = level;
  1660. matroska->num_levels++;
  1661. /* check ID */
  1662. if (!(id = ebml_peek_id (matroska,
  1663. &matroska->level_up)))
  1664. goto finish;
  1665. if (id != seek_id) {
  1666. av_log(matroska->ctx, AV_LOG_INFO,
  1667. "We looked for ID=0x%x but got "
  1668. "ID=0x%x (pos=%"PRIu64")",
  1669. seek_id, id, seek_pos +
  1670. matroska->segment_start);
  1671. goto finish;
  1672. }
  1673. /* read master + parse */
  1674. if ((res = ebml_read_master(matroska, &id)) < 0)
  1675. goto finish;
  1676. switch (id) {
  1677. case MATROSKA_ID_CUES:
  1678. if (!(res = matroska_parse_index(matroska)) ||
  1679. url_feof(matroska->ctx->pb)) {
  1680. matroska->index_parsed = 1;
  1681. res = 0;
  1682. }
  1683. break;
  1684. case MATROSKA_ID_TAGS:
  1685. if (!(res = matroska_parse_metadata(matroska)) ||
  1686. url_feof(matroska->ctx->pb)) {
  1687. matroska->metadata_parsed = 1;
  1688. res = 0;
  1689. }
  1690. break;
  1691. }
  1692. finish:
  1693. /* remove dummy level */
  1694. while (matroska->num_levels) {
  1695. matroska->num_levels--;
  1696. length =
  1697. matroska->levels[matroska->num_levels].length;
  1698. if (length == (uint64_t)-1)
  1699. break;
  1700. }
  1701. /* seek back */
  1702. if ((res = ebml_read_seek(matroska, before_pos)) < 0)
  1703. return res;
  1704. matroska->peek_id = peek_id_cache;
  1705. matroska->level_up = level_up;
  1706. break;
  1707. }
  1708. default:
  1709. av_log(matroska->ctx, AV_LOG_INFO,
  1710. "Ignoring seekhead entry for ID=0x%x\n",
  1711. seek_id);
  1712. break;
  1713. }
  1714. break;
  1715. }
  1716. default:
  1717. av_log(matroska->ctx, AV_LOG_INFO,
  1718. "Unknown seekhead ID 0x%x\n", id);
  1719. /* fall-through */
  1720. case EBML_ID_VOID:
  1721. res = ebml_read_skip(matroska);
  1722. break;
  1723. }
  1724. if (matroska->level_up) {
  1725. matroska->level_up--;
  1726. break;
  1727. }
  1728. }
  1729. return res;
  1730. }
  1731. static int
  1732. matroska_parse_attachments(AVFormatContext *s)
  1733. {
  1734. MatroskaDemuxContext *matroska = s->priv_data;
  1735. int res = 0;
  1736. uint32_t id;
  1737. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
  1738. while (res == 0) {
  1739. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1740. res = AVERROR(EIO);
  1741. break;
  1742. } else if (matroska->level_up) {
  1743. matroska->level_up--;
  1744. break;
  1745. }
  1746. switch (id) {
  1747. case MATROSKA_ID_ATTACHEDFILE: {
  1748. char* name = NULL;
  1749. char* mime = NULL;
  1750. uint8_t* data = NULL;
  1751. int i, data_size = 0;
  1752. AVStream *st;
  1753. if ((res = ebml_read_master(matroska, &id)) < 0)
  1754. break;
  1755. while (res == 0) {
  1756. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1757. res = AVERROR(EIO);
  1758. break;
  1759. } else if (matroska->level_up) {
  1760. matroska->level_up--;
  1761. break;
  1762. }
  1763. switch (id) {
  1764. case MATROSKA_ID_FILENAME:
  1765. res = ebml_read_utf8 (matroska, &id, &name);
  1766. break;
  1767. case MATROSKA_ID_FILEMIMETYPE:
  1768. res = ebml_read_ascii (matroska, &id, &mime);
  1769. break;
  1770. case MATROSKA_ID_FILEDATA:
  1771. res = ebml_read_binary(matroska, &id, &data, &data_size);
  1772. break;
  1773. default:
  1774. av_log(matroska->ctx, AV_LOG_INFO,
  1775. "Unknown attachedfile ID 0x%x\n", id);
  1776. case EBML_ID_VOID:
  1777. res = ebml_read_skip(matroska);
  1778. break;
  1779. }
  1780. if (matroska->level_up) {
  1781. matroska->level_up--;
  1782. break;
  1783. }
  1784. }
  1785. if (!(name && mime && data && data_size > 0)) {
  1786. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1787. break;
  1788. }
  1789. st = av_new_stream(s, matroska->num_streams++);
  1790. if (st == NULL)
  1791. return AVERROR(ENOMEM);
  1792. st->filename = av_strdup(name);
  1793. st->codec->codec_id = CODEC_ID_NONE;
  1794. st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
  1795. st->codec->extradata = av_malloc(data_size);
  1796. if(st->codec->extradata == NULL)
  1797. return AVERROR(ENOMEM);
  1798. st->codec->extradata_size = data_size;
  1799. memcpy(st->codec->extradata, data, data_size);
  1800. for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
  1801. if (!strncmp(ff_mkv_mime_tags[i].str, mime,
  1802. strlen(ff_mkv_mime_tags[i].str))) {
  1803. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1804. break;
  1805. }
  1806. }
  1807. av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
  1808. break;
  1809. }
  1810. default:
  1811. av_log(matroska->ctx, AV_LOG_INFO,
  1812. "Unknown attachments ID 0x%x\n", id);
  1813. /* fall-through */
  1814. case EBML_ID_VOID:
  1815. res = ebml_read_skip(matroska);
  1816. break;
  1817. }
  1818. if (matroska->level_up) {
  1819. matroska->level_up--;
  1820. break;
  1821. }
  1822. }
  1823. return res;
  1824. }
  1825. static int
  1826. matroska_parse_chapters(AVFormatContext *s)
  1827. {
  1828. MatroskaDemuxContext *matroska = s->priv_data;
  1829. int res = 0;
  1830. uint32_t id;
  1831. av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
  1832. while (res == 0) {
  1833. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1834. res = AVERROR(EIO);
  1835. break;
  1836. } else if (matroska->level_up) {
  1837. matroska->level_up--;
  1838. break;
  1839. }
  1840. switch (id) {
  1841. case MATROSKA_ID_EDITIONENTRY: {
  1842. uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
  1843. char* title = NULL;
  1844. /* if there is more than one chapter edition
  1845. we take only the first one */
  1846. if(s->chapters) {
  1847. ebml_read_skip(matroska);
  1848. break;
  1849. }
  1850. if ((res = ebml_read_master(matroska, &id)) < 0)
  1851. break;
  1852. while (res == 0) {
  1853. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1854. res = AVERROR(EIO);
  1855. break;
  1856. } else if (matroska->level_up) {
  1857. matroska->level_up--;
  1858. break;
  1859. }
  1860. switch (id) {
  1861. case MATROSKA_ID_CHAPTERATOM:
  1862. if ((res = ebml_read_master(matroska, &id)) < 0)
  1863. break;
  1864. while (res == 0) {
  1865. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1866. res = AVERROR(EIO);
  1867. break;
  1868. } else if (matroska->level_up) {
  1869. matroska->level_up--;
  1870. break;
  1871. }
  1872. switch (id) {
  1873. case MATROSKA_ID_CHAPTERTIMEEND:
  1874. res = ebml_read_uint(matroska, &id, &end);
  1875. break;
  1876. case MATROSKA_ID_CHAPTERTIMESTART:
  1877. res = ebml_read_uint(matroska, &id, &start);
  1878. break;
  1879. case MATROSKA_ID_CHAPTERDISPLAY:
  1880. if ((res = ebml_read_master(matroska, &id)) < 0)
  1881. break;
  1882. while (res == 0) {
  1883. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1884. res = AVERROR(EIO);
  1885. break;
  1886. } else if (matroska->level_up) {
  1887. matroska->level_up--;
  1888. break;
  1889. }
  1890. switch (id) {
  1891. case MATROSKA_ID_CHAPSTRING:
  1892. res = ebml_read_utf8(matroska, &id, &title);
  1893. break;
  1894. default:
  1895. av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
  1896. case EBML_ID_VOID:
  1897. res = ebml_read_skip(matroska);
  1898. break;
  1899. }
  1900. if (matroska->level_up) {
  1901. matroska->level_up--;
  1902. break;
  1903. }
  1904. }
  1905. break;
  1906. default:
  1907. av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
  1908. case MATROSKA_ID_CHAPTERUID:
  1909. case MATROSKA_ID_CHAPTERFLAGHIDDEN:
  1910. case EBML_ID_VOID:
  1911. res = ebml_read_skip(matroska);
  1912. break;
  1913. }
  1914. if (matroska->level_up) {
  1915. matroska->level_up--;
  1916. break;
  1917. }
  1918. }
  1919. if (start != AV_NOPTS_VALUE) {
  1920. start = start * AV_TIME_BASE / 1000000000;
  1921. if (end != AV_NOPTS_VALUE)
  1922. end = end * AV_TIME_BASE / 1000000000;
  1923. res = ff_new_chapter(s, start, end, title ? title : "(unnamed)");
  1924. }
  1925. av_free(title);
  1926. break;
  1927. default:
  1928. av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
  1929. case MATROSKA_ID_EDITIONUID:
  1930. case MATROSKA_ID_EDITIONFLAGHIDDEN:
  1931. case EBML_ID_VOID:
  1932. res = ebml_read_skip(matroska);
  1933. break;
  1934. }
  1935. if (matroska->level_up) {
  1936. matroska->level_up--;
  1937. break;
  1938. }
  1939. }
  1940. break;
  1941. }
  1942. default:
  1943. av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
  1944. case EBML_ID_VOID:
  1945. res = ebml_read_skip(matroska);
  1946. break;
  1947. }
  1948. if (matroska->level_up) {
  1949. matroska->level_up--;
  1950. break;
  1951. }
  1952. }
  1953. return res;
  1954. }
  1955. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
  1956. static int
  1957. matroska_aac_profile (char *codec_id)
  1958. {
  1959. static const char *aac_profiles[] = {
  1960. "MAIN", "LC", "SSR"
  1961. };
  1962. int profile;
  1963. for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
  1964. if (strstr(codec_id, aac_profiles[profile]))
  1965. break;
  1966. return profile + 1;
  1967. }
  1968. static int
  1969. matroska_aac_sri (int samplerate)
  1970. {
  1971. int sri;
  1972. for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
  1973. if (ff_mpeg4audio_sample_rates[sri] == samplerate)
  1974. break;
  1975. return sri;
  1976. }
  1977. static int
  1978. matroska_read_header (AVFormatContext *s,
  1979. AVFormatParameters *ap)
  1980. {
  1981. MatroskaDemuxContext *matroska = s->priv_data;
  1982. char *doctype;
  1983. int version, last_level, res = 0;
  1984. uint32_t id;
  1985. matroska->ctx = s;
  1986. /* First read the EBML header. */
  1987. doctype = NULL;
  1988. if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
  1989. return res;
  1990. if ((doctype == NULL) || strcmp(doctype, "matroska")) {
  1991. av_log(matroska->ctx, AV_LOG_ERROR,
  1992. "Wrong EBML doctype ('%s' != 'matroska').\n",
  1993. doctype ? doctype : "(none)");
  1994. if (doctype)
  1995. av_free(doctype);
  1996. return AVERROR_NOFMT;
  1997. }
  1998. av_free(doctype);
  1999. if (version > 2) {
  2000. av_log(matroska->ctx, AV_LOG_ERROR,
  2001. "Matroska demuxer version 2 too old for file version %d\n",
  2002. version);
  2003. return AVERROR_NOFMT;
  2004. }
  2005. /* The next thing is a segment. */
  2006. while (1) {
  2007. if (!(id = ebml_peek_id(matroska, &last_level)))
  2008. return AVERROR(EIO);
  2009. if (id == MATROSKA_ID_SEGMENT)
  2010. break;
  2011. /* oi! */
  2012. av_log(matroska->ctx, AV_LOG_INFO,
  2013. "Expected a Segment ID (0x%x), but received 0x%x!\n",
  2014. MATROSKA_ID_SEGMENT, id);
  2015. if ((res = ebml_read_skip(matroska)) < 0)
  2016. return res;
  2017. }
  2018. /* We now have a Matroska segment.
  2019. * Seeks are from the beginning of the segment,
  2020. * after the segment ID/length. */
  2021. if ((res = ebml_read_master(matroska, &id)) < 0)
  2022. return res;
  2023. matroska->segment_start = url_ftell(s->pb);
  2024. matroska->time_scale = 1000000;
  2025. /* we've found our segment, start reading the different contents in here */
  2026. while (res == 0) {
  2027. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2028. res = AVERROR(EIO);
  2029. break;
  2030. } else if (matroska->level_up) {
  2031. matroska->level_up--;
  2032. break;
  2033. }
  2034. switch (id) {
  2035. /* stream info */
  2036. case MATROSKA_ID_INFO: {
  2037. if ((res = ebml_read_master(matroska, &id)) < 0)
  2038. break;
  2039. res = matroska_parse_info(matroska);
  2040. break;
  2041. }
  2042. /* track info headers */
  2043. case MATROSKA_ID_TRACKS: {
  2044. if ((res = ebml_read_master(matroska, &id)) < 0)
  2045. break;
  2046. res = matroska_parse_tracks(matroska);
  2047. break;
  2048. }
  2049. /* stream index */
  2050. case MATROSKA_ID_CUES: {
  2051. if (!matroska->index_parsed) {
  2052. if ((res = ebml_read_master(matroska, &id)) < 0)
  2053. break;
  2054. res = matroska_parse_index(matroska);
  2055. } else
  2056. res = ebml_read_skip(matroska);
  2057. break;
  2058. }
  2059. /* metadata */
  2060. case MATROSKA_ID_TAGS: {
  2061. if (!matroska->metadata_parsed) {
  2062. if ((res = ebml_read_master(matroska, &id)) < 0)
  2063. break;
  2064. res = matroska_parse_metadata(matroska);
  2065. } else
  2066. res = ebml_read_skip(matroska);
  2067. break;
  2068. }
  2069. /* file index (if seekable, seek to Cues/Tags to parse it) */
  2070. case MATROSKA_ID_SEEKHEAD: {
  2071. if ((res = ebml_read_master(matroska, &id)) < 0)
  2072. break;
  2073. res = matroska_parse_seekhead(matroska);
  2074. break;
  2075. }
  2076. case MATROSKA_ID_ATTACHMENTS: {
  2077. if ((res = ebml_read_master(matroska, &id)) < 0)
  2078. break;
  2079. res = matroska_parse_attachments(s);
  2080. break;
  2081. }
  2082. case MATROSKA_ID_CLUSTER: {
  2083. /* Do not read the master - this will be done in the next
  2084. * call to matroska_read_packet. */
  2085. res = 1;
  2086. break;
  2087. }
  2088. case MATROSKA_ID_CHAPTERS: {
  2089. if ((res = ebml_read_master(matroska, &id)) < 0)
  2090. return res;
  2091. res = matroska_parse_chapters(s);
  2092. break;
  2093. }
  2094. default:
  2095. av_log(matroska->ctx, AV_LOG_INFO,
  2096. "Unknown matroska file header ID 0x%x\n", id);
  2097. /* fall-through */
  2098. case EBML_ID_VOID:
  2099. res = ebml_read_skip(matroska);
  2100. break;
  2101. }
  2102. if (matroska->level_up) {
  2103. matroska->level_up--;
  2104. break;
  2105. }
  2106. }
  2107. /* Have we found a cluster? */
  2108. if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
  2109. int i, j;
  2110. MatroskaTrack *track;
  2111. AVStream *st;
  2112. for (i = 0; i < matroska->num_tracks; i++) {
  2113. enum CodecID codec_id = CODEC_ID_NONE;
  2114. uint8_t *extradata = NULL;
  2115. int extradata_size = 0;
  2116. int extradata_offset = 0;
  2117. track = matroska->tracks[i];
  2118. track->stream_index = -1;
  2119. /* Apply some sanity checks. */
  2120. if (track->codec_id == NULL)
  2121. continue;
  2122. for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
  2123. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  2124. strlen(ff_mkv_codec_tags[j].str))){
  2125. codec_id= ff_mkv_codec_tags[j].id;
  2126. break;
  2127. }
  2128. }
  2129. /* Set the FourCC from the CodecID. */
  2130. /* This is the MS compatibility mode which stores a
  2131. * BITMAPINFOHEADER in the CodecPrivate. */
  2132. if (!strcmp(track->codec_id,
  2133. MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
  2134. (track->codec_priv_size >= 40) &&
  2135. (track->codec_priv != NULL)) {
  2136. MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
  2137. /* Offset of biCompression. Stored in LE. */
  2138. vtrack->fourcc = AV_RL32(track->codec_priv + 16);
  2139. codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
  2140. }
  2141. /* This is the MS compatibility mode which stores a
  2142. * WAVEFORMATEX in the CodecPrivate. */
  2143. else if (!strcmp(track->codec_id,
  2144. MATROSKA_CODEC_ID_AUDIO_ACM) &&
  2145. (track->codec_priv_size >= 18) &&
  2146. (track->codec_priv != NULL)) {
  2147. uint16_t tag;
  2148. /* Offset of wFormatTag. Stored in LE. */
  2149. tag = AV_RL16(track->codec_priv);
  2150. codec_id = codec_get_id(codec_wav_tags, tag);
  2151. }
  2152. else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
  2153. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
  2154. int profile = matroska_aac_profile(track->codec_id);
  2155. int sri = matroska_aac_sri(audiotrack->internal_samplerate);
  2156. extradata = av_malloc(5);
  2157. if (extradata == NULL)
  2158. return AVERROR(ENOMEM);
  2159. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  2160. extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
  2161. if (strstr(track->codec_id, "SBR")) {
  2162. sri = matroska_aac_sri(audiotrack->samplerate);
  2163. extradata[2] = 0x56;
  2164. extradata[3] = 0xE5;
  2165. extradata[4] = 0x80 | (sri<<3);
  2166. extradata_size = 5;
  2167. } else {
  2168. extradata_size = 2;
  2169. }
  2170. }
  2171. else if (codec_id == CODEC_ID_TTA) {
  2172. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
  2173. ByteIOContext b;
  2174. extradata_size = 30;
  2175. extradata = av_mallocz(extradata_size);
  2176. if (extradata == NULL)
  2177. return AVERROR(ENOMEM);
  2178. init_put_byte(&b, extradata, extradata_size, 1,
  2179. NULL, NULL, NULL, NULL);
  2180. put_buffer(&b, "TTA1", 4);
  2181. put_le16(&b, 1);
  2182. put_le16(&b, audiotrack->channels);
  2183. put_le16(&b, audiotrack->bitdepth);
  2184. put_le32(&b, audiotrack->samplerate);
  2185. put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
  2186. }
  2187. else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
  2188. codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
  2189. extradata_offset = 26;
  2190. track->codec_priv_size -= extradata_offset;
  2191. }
  2192. else if (codec_id == CODEC_ID_RA_144) {
  2193. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2194. audiotrack->samplerate = 8000;
  2195. audiotrack->channels = 1;
  2196. }
  2197. else if (codec_id == CODEC_ID_RA_288 ||
  2198. codec_id == CODEC_ID_COOK ||
  2199. codec_id == CODEC_ID_ATRAC3) {
  2200. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2201. ByteIOContext b;
  2202. init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
  2203. NULL, NULL, NULL, NULL);
  2204. url_fskip(&b, 24);
  2205. audiotrack->coded_framesize = get_be32(&b);
  2206. url_fskip(&b, 12);
  2207. audiotrack->sub_packet_h = get_be16(&b);
  2208. audiotrack->frame_size = get_be16(&b);
  2209. audiotrack->sub_packet_size = get_be16(&b);
  2210. audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
  2211. if (codec_id == CODEC_ID_RA_288) {
  2212. audiotrack->block_align = audiotrack->coded_framesize;
  2213. track->codec_priv_size = 0;
  2214. } else {
  2215. audiotrack->block_align = audiotrack->sub_packet_size;
  2216. extradata_offset = 78;
  2217. track->codec_priv_size -= extradata_offset;
  2218. }
  2219. }
  2220. if (codec_id == CODEC_ID_NONE) {
  2221. av_log(matroska->ctx, AV_LOG_INFO,
  2222. "Unknown/unsupported CodecID %s.\n",
  2223. track->codec_id);
  2224. }
  2225. track->stream_index = matroska->num_streams;
  2226. matroska->num_streams++;
  2227. st = av_new_stream(s, track->stream_index);
  2228. if (st == NULL)
  2229. return AVERROR(ENOMEM);
  2230. av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  2231. st->codec->codec_id = codec_id;
  2232. st->start_time = 0;
  2233. if (strcmp(track->language, "und"))
  2234. strcpy(st->language, track->language);
  2235. if (track->flags & MATROSKA_TRACK_DEFAULT)
  2236. st->disposition |= AV_DISPOSITION_DEFAULT;
  2237. if (track->default_duration)
  2238. av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
  2239. track->default_duration, 1000000000, 30000);
  2240. if(extradata){
  2241. st->codec->extradata = extradata;
  2242. st->codec->extradata_size = extradata_size;
  2243. } else if(track->codec_priv && track->codec_priv_size > 0){
  2244. st->codec->extradata = av_malloc(track->codec_priv_size);
  2245. if(st->codec->extradata == NULL)
  2246. return AVERROR(ENOMEM);
  2247. st->codec->extradata_size = track->codec_priv_size;
  2248. memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
  2249. track->codec_priv_size);
  2250. }
  2251. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  2252. MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
  2253. st->codec->codec_type = CODEC_TYPE_VIDEO;
  2254. st->codec->codec_tag = videotrack->fourcc;
  2255. st->codec->width = videotrack->pixel_width;
  2256. st->codec->height = videotrack->pixel_height;
  2257. if (videotrack->display_width == 0)
  2258. videotrack->display_width= videotrack->pixel_width;
  2259. if (videotrack->display_height == 0)
  2260. videotrack->display_height= videotrack->pixel_height;
  2261. av_reduce(&st->codec->sample_aspect_ratio.num,
  2262. &st->codec->sample_aspect_ratio.den,
  2263. st->codec->height * videotrack->display_width,
  2264. st->codec-> width * videotrack->display_height,
  2265. 255);
  2266. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  2267. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  2268. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2269. st->codec->codec_type = CODEC_TYPE_AUDIO;
  2270. st->codec->sample_rate = audiotrack->samplerate;
  2271. st->codec->channels = audiotrack->channels;
  2272. st->codec->block_align = audiotrack->block_align;
  2273. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  2274. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  2275. }
  2276. /* What do we do with private data? E.g. for Vorbis. */
  2277. }
  2278. res = 0;
  2279. }
  2280. if (matroska->index_parsed) {
  2281. int i, track, stream;
  2282. for (i=0; i<matroska->num_indexes; i++) {
  2283. MatroskaDemuxIndex *idx = &matroska->index[i];
  2284. track = matroska_find_track_by_num(matroska, idx->track);
  2285. if (track < 0) continue;
  2286. stream = matroska->tracks[track]->stream_index;
  2287. if (stream >= 0 && stream < matroska->ctx->nb_streams)
  2288. av_add_index_entry(matroska->ctx->streams[stream],
  2289. idx->pos, idx->time/matroska->time_scale,
  2290. 0, 0, AVINDEX_KEYFRAME);
  2291. }
  2292. }
  2293. return res;
  2294. }
  2295. static int
  2296. matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
  2297. int64_t pos, uint64_t cluster_time, uint64_t duration,
  2298. int is_keyframe, int is_bframe)
  2299. {
  2300. int res = 0;
  2301. int track;
  2302. AVStream *st;
  2303. AVPacket *pkt;
  2304. uint8_t *origdata = data;
  2305. int16_t block_time;
  2306. uint32_t *lace_size = NULL;
  2307. int n, flags, laces = 0;
  2308. uint64_t num;
  2309. int stream_index;
  2310. /* first byte(s): tracknum */
  2311. if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
  2312. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  2313. av_free(origdata);
  2314. return res;
  2315. }
  2316. data += n;
  2317. size -= n;
  2318. /* fetch track from num */
  2319. track = matroska_find_track_by_num(matroska, num);
  2320. if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
  2321. av_log(matroska->ctx, AV_LOG_INFO,
  2322. "Invalid stream %d or size %u\n", track, size);
  2323. av_free(origdata);
  2324. return res;
  2325. }
  2326. stream_index = matroska->tracks[track]->stream_index;
  2327. if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
  2328. av_free(origdata);
  2329. return res;
  2330. }
  2331. st = matroska->ctx->streams[stream_index];
  2332. if (st->discard >= AVDISCARD_ALL) {
  2333. av_free(origdata);
  2334. return res;
  2335. }
  2336. if (duration == AV_NOPTS_VALUE)
  2337. duration = matroska->tracks[track]->default_duration / matroska->time_scale;
  2338. /* block_time (relative to cluster time) */
  2339. block_time = AV_RB16(data);
  2340. data += 2;
  2341. flags = *data++;
  2342. size -= 3;
  2343. if (is_keyframe == -1)
  2344. is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
  2345. if (matroska->skip_to_keyframe) {
  2346. if (!is_keyframe || st != matroska->skip_to_stream) {
  2347. av_free(origdata);
  2348. return res;
  2349. }
  2350. matroska->skip_to_keyframe = 0;
  2351. }
  2352. switch ((flags & 0x06) >> 1) {
  2353. case 0x0: /* no lacing */
  2354. laces = 1;
  2355. lace_size = av_mallocz(sizeof(int));
  2356. lace_size[0] = size;
  2357. break;
  2358. case 0x1: /* xiph lacing */
  2359. case 0x2: /* fixed-size lacing */
  2360. case 0x3: /* EBML lacing */
  2361. if (size == 0) {
  2362. res = -1;
  2363. break;
  2364. }
  2365. laces = (*data) + 1;
  2366. data += 1;
  2367. size -= 1;
  2368. lace_size = av_mallocz(laces * sizeof(int));
  2369. switch ((flags & 0x06) >> 1) {
  2370. case 0x1: /* xiph lacing */ {
  2371. uint8_t temp;
  2372. uint32_t total = 0;
  2373. for (n = 0; res == 0 && n < laces - 1; n++) {
  2374. while (1) {
  2375. if (size == 0) {
  2376. res = -1;
  2377. break;
  2378. }
  2379. temp = *data;
  2380. lace_size[n] += temp;
  2381. data += 1;
  2382. size -= 1;
  2383. if (temp != 0xff)
  2384. break;
  2385. }
  2386. total += lace_size[n];
  2387. }
  2388. lace_size[n] = size - total;
  2389. break;
  2390. }
  2391. case 0x2: /* fixed-size lacing */
  2392. for (n = 0; n < laces; n++)
  2393. lace_size[n] = size / laces;
  2394. break;
  2395. case 0x3: /* EBML lacing */ {
  2396. uint32_t total;
  2397. n = matroska_ebmlnum_uint(data, size, &num);
  2398. if (n < 0) {
  2399. av_log(matroska->ctx, AV_LOG_INFO,
  2400. "EBML block data error\n");
  2401. break;
  2402. }
  2403. data += n;
  2404. size -= n;
  2405. total = lace_size[0] = num;
  2406. for (n = 1; res == 0 && n < laces - 1; n++) {
  2407. int64_t snum;
  2408. int r;
  2409. r = matroska_ebmlnum_sint (data, size, &snum);
  2410. if (r < 0) {
  2411. av_log(matroska->ctx, AV_LOG_INFO,
  2412. "EBML block data error\n");
  2413. break;
  2414. }
  2415. data += r;
  2416. size -= r;
  2417. lace_size[n] = lace_size[n - 1] + snum;
  2418. total += lace_size[n];
  2419. }
  2420. lace_size[n] = size - total;
  2421. break;
  2422. }
  2423. }
  2424. break;
  2425. }
  2426. if (res == 0) {
  2427. uint64_t timecode = AV_NOPTS_VALUE;
  2428. if (cluster_time != (uint64_t)-1
  2429. && (block_time >= 0 || cluster_time >= -block_time))
  2430. timecode = cluster_time + block_time;
  2431. for (n = 0; n < laces; n++) {
  2432. if (st->codec->codec_id == CODEC_ID_RA_288 ||
  2433. st->codec->codec_id == CODEC_ID_COOK ||
  2434. st->codec->codec_id == CODEC_ID_ATRAC3) {
  2435. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
  2436. int a = st->codec->block_align;
  2437. int sps = audiotrack->sub_packet_size;
  2438. int cfs = audiotrack->coded_framesize;
  2439. int h = audiotrack->sub_packet_h;
  2440. int y = audiotrack->sub_packet_cnt;
  2441. int w = audiotrack->frame_size;
  2442. int x;
  2443. if (!audiotrack->pkt_cnt) {
  2444. if (st->codec->codec_id == CODEC_ID_RA_288)
  2445. for (x=0; x<h/2; x++)
  2446. memcpy(audiotrack->buf+x*2*w+y*cfs,
  2447. data+x*cfs, cfs);
  2448. else
  2449. for (x=0; x<w/sps; x++)
  2450. memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  2451. if (++audiotrack->sub_packet_cnt >= h) {
  2452. audiotrack->sub_packet_cnt = 0;
  2453. audiotrack->pkt_cnt = h*w / a;
  2454. }
  2455. }
  2456. while (audiotrack->pkt_cnt) {
  2457. pkt = av_mallocz(sizeof(AVPacket));
  2458. av_new_packet(pkt, a);
  2459. memcpy(pkt->data, audiotrack->buf
  2460. + a * (h*w / a - audiotrack->pkt_cnt--), a);
  2461. pkt->pos = pos;
  2462. pkt->stream_index = stream_index;
  2463. matroska_queue_packet(matroska, pkt);
  2464. }
  2465. } else {
  2466. int result, offset = 0, ilen, olen, pkt_size = lace_size[n];
  2467. uint8_t *pkt_data = data;
  2468. if (matroska->tracks[track]->encoding_scope & 1) {
  2469. switch (matroska->tracks[track]->encoding_algo) {
  2470. case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
  2471. offset = matroska->tracks[track]->encoding_settings_len;
  2472. break;
  2473. case MATROSKA_TRACK_ENCODING_COMP_LZO:
  2474. pkt_data = NULL;
  2475. do {
  2476. ilen = lace_size[n];
  2477. olen = pkt_size *= 3;
  2478. pkt_data = av_realloc(pkt_data,
  2479. pkt_size+LZO_OUTPUT_PADDING);
  2480. result = lzo1x_decode(pkt_data, &olen, data, &ilen);
  2481. } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
  2482. if (result) {
  2483. av_free(pkt_data);
  2484. continue;
  2485. }
  2486. pkt_size -= olen;
  2487. break;
  2488. #ifdef CONFIG_ZLIB
  2489. case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
  2490. z_stream zstream = {0};
  2491. pkt_data = NULL;
  2492. if (inflateInit(&zstream) != Z_OK)
  2493. continue;
  2494. zstream.next_in = data;
  2495. zstream.avail_in = lace_size[n];
  2496. do {
  2497. pkt_size *= 3;
  2498. pkt_data = av_realloc(pkt_data, pkt_size);
  2499. zstream.avail_out = pkt_size - zstream.total_out;
  2500. zstream.next_out = pkt_data + zstream.total_out;
  2501. result = inflate(&zstream, Z_NO_FLUSH);
  2502. } while (result==Z_OK && pkt_size<10000000);
  2503. pkt_size = zstream.total_out;
  2504. inflateEnd(&zstream);
  2505. if (result != Z_STREAM_END) {
  2506. av_free(pkt_data);
  2507. continue;
  2508. }
  2509. break;
  2510. }
  2511. #endif
  2512. #ifdef CONFIG_BZLIB
  2513. case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
  2514. bz_stream bzstream = {0};
  2515. pkt_data = NULL;
  2516. if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
  2517. continue;
  2518. bzstream.next_in = data;
  2519. bzstream.avail_in = lace_size[n];
  2520. do {
  2521. pkt_size *= 3;
  2522. pkt_data = av_realloc(pkt_data, pkt_size);
  2523. bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
  2524. bzstream.next_out = pkt_data + bzstream.total_out_lo32;
  2525. result = BZ2_bzDecompress(&bzstream);
  2526. } while (result==BZ_OK && pkt_size<10000000);
  2527. pkt_size = bzstream.total_out_lo32;
  2528. BZ2_bzDecompressEnd(&bzstream);
  2529. if (result != BZ_STREAM_END) {
  2530. av_free(pkt_data);
  2531. continue;
  2532. }
  2533. break;
  2534. }
  2535. #endif
  2536. }
  2537. }
  2538. pkt = av_mallocz(sizeof(AVPacket));
  2539. /* XXX: prevent data copy... */
  2540. if (av_new_packet(pkt, pkt_size+offset) < 0) {
  2541. res = AVERROR(ENOMEM);
  2542. n = laces-1;
  2543. break;
  2544. }
  2545. if (offset)
  2546. memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
  2547. memcpy (pkt->data+offset, pkt_data, pkt_size);
  2548. if (n == 0)
  2549. pkt->flags = is_keyframe;
  2550. pkt->stream_index = stream_index;
  2551. pkt->pts = timecode;
  2552. pkt->pos = pos;
  2553. pkt->duration = duration;
  2554. matroska_queue_packet(matroska, pkt);
  2555. }
  2556. if (timecode != AV_NOPTS_VALUE)
  2557. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  2558. data += lace_size[n];
  2559. }
  2560. }
  2561. av_free(lace_size);
  2562. av_free(origdata);
  2563. return res;
  2564. }
  2565. static int
  2566. matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
  2567. uint64_t cluster_time)
  2568. {
  2569. int res = 0;
  2570. uint32_t id;
  2571. int is_bframe = 0;
  2572. int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
  2573. uint64_t duration = AV_NOPTS_VALUE;
  2574. uint8_t *data;
  2575. int size = 0;
  2576. int64_t pos = 0;
  2577. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
  2578. while (res == 0) {
  2579. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2580. res = AVERROR(EIO);
  2581. break;
  2582. } else if (matroska->level_up) {
  2583. matroska->level_up--;
  2584. break;
  2585. }
  2586. switch (id) {
  2587. /* one block inside the group. Note, block parsing is one
  2588. * of the harder things, so this code is a bit complicated.
  2589. * See http://www.matroska.org/ for documentation. */
  2590. case MATROSKA_ID_BLOCK: {
  2591. pos = url_ftell(matroska->ctx->pb);
  2592. res = ebml_read_binary(matroska, &id, &data, &size);
  2593. break;
  2594. }
  2595. case MATROSKA_ID_BLOCKDURATION: {
  2596. if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
  2597. break;
  2598. break;
  2599. }
  2600. case MATROSKA_ID_BLOCKREFERENCE: {
  2601. int64_t num;
  2602. /* We've found a reference, so not even the first frame in
  2603. * the lace is a key frame. */
  2604. is_keyframe = 0;
  2605. if (last_num_packets != matroska->num_packets)
  2606. matroska->packets[last_num_packets]->flags = 0;
  2607. if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
  2608. break;
  2609. if (num > 0)
  2610. is_bframe = 1;
  2611. break;
  2612. }
  2613. default:
  2614. av_log(matroska->ctx, AV_LOG_INFO,
  2615. "Unknown entry 0x%x in blockgroup data\n", id);
  2616. /* fall-through */
  2617. case EBML_ID_VOID:
  2618. res = ebml_read_skip(matroska);
  2619. break;
  2620. }
  2621. if (matroska->level_up) {
  2622. matroska->level_up--;
  2623. break;
  2624. }
  2625. }
  2626. if (res)
  2627. return res;
  2628. if (size > 0)
  2629. res = matroska_parse_block(matroska, data, size, pos, cluster_time,
  2630. duration, is_keyframe, is_bframe);
  2631. return res;
  2632. }
  2633. static int
  2634. matroska_parse_cluster (MatroskaDemuxContext *matroska)
  2635. {
  2636. int res = 0;
  2637. uint32_t id;
  2638. uint64_t cluster_time = 0;
  2639. uint8_t *data;
  2640. int64_t pos;
  2641. int size;
  2642. av_log(matroska->ctx, AV_LOG_DEBUG,
  2643. "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
  2644. while (res == 0) {
  2645. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2646. res = AVERROR(EIO);
  2647. break;
  2648. } else if (matroska->level_up) {
  2649. matroska->level_up--;
  2650. break;
  2651. }
  2652. switch (id) {
  2653. /* cluster timecode */
  2654. case MATROSKA_ID_CLUSTERTIMECODE: {
  2655. uint64_t num;
  2656. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  2657. break;
  2658. cluster_time = num;
  2659. break;
  2660. }
  2661. /* a group of blocks inside a cluster */
  2662. case MATROSKA_ID_BLOCKGROUP:
  2663. if ((res = ebml_read_master(matroska, &id)) < 0)
  2664. break;
  2665. res = matroska_parse_blockgroup(matroska, cluster_time);
  2666. break;
  2667. case MATROSKA_ID_SIMPLEBLOCK:
  2668. pos = url_ftell(matroska->ctx->pb);
  2669. res = ebml_read_binary(matroska, &id, &data, &size);
  2670. if (res == 0)
  2671. res = matroska_parse_block(matroska, data, size, pos,
  2672. cluster_time, AV_NOPTS_VALUE,
  2673. -1, 0);
  2674. break;
  2675. default:
  2676. av_log(matroska->ctx, AV_LOG_INFO,
  2677. "Unknown entry 0x%x in cluster data\n", id);
  2678. /* fall-through */
  2679. case EBML_ID_VOID:
  2680. res = ebml_read_skip(matroska);
  2681. break;
  2682. }
  2683. if (matroska->level_up) {
  2684. matroska->level_up--;
  2685. break;
  2686. }
  2687. }
  2688. return res;
  2689. }
  2690. static int
  2691. matroska_read_packet (AVFormatContext *s,
  2692. AVPacket *pkt)
  2693. {
  2694. MatroskaDemuxContext *matroska = s->priv_data;
  2695. int res;
  2696. uint32_t id;
  2697. /* Read stream until we have a packet queued. */
  2698. while (matroska_deliver_packet(matroska, pkt)) {
  2699. /* Have we already reached the end? */
  2700. if (matroska->done)
  2701. return AVERROR(EIO);
  2702. res = 0;
  2703. while (res == 0) {
  2704. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2705. return AVERROR(EIO);
  2706. } else if (matroska->level_up) {
  2707. matroska->level_up--;
  2708. break;
  2709. }
  2710. switch (id) {
  2711. case MATROSKA_ID_CLUSTER:
  2712. if ((res = ebml_read_master(matroska, &id)) < 0)
  2713. break;
  2714. if ((res = matroska_parse_cluster(matroska)) == 0)
  2715. res = 1; /* Parsed one cluster, let's get out. */
  2716. break;
  2717. default:
  2718. case EBML_ID_VOID:
  2719. res = ebml_read_skip(matroska);
  2720. break;
  2721. }
  2722. if (matroska->level_up) {
  2723. matroska->level_up--;
  2724. break;
  2725. }
  2726. }
  2727. if (res == -1)
  2728. matroska->done = 1;
  2729. }
  2730. return 0;
  2731. }
  2732. static int
  2733. matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
  2734. int flags)
  2735. {
  2736. MatroskaDemuxContext *matroska = s->priv_data;
  2737. AVStream *st = s->streams[stream_index];
  2738. int index;
  2739. /* find index entry */
  2740. index = av_index_search_timestamp(st, timestamp, flags);
  2741. if (index < 0)
  2742. return 0;
  2743. matroska_clear_queue(matroska);
  2744. /* do the seek */
  2745. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  2746. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  2747. matroska->skip_to_stream = st;
  2748. matroska->peek_id = 0;
  2749. return 0;
  2750. }
  2751. static int
  2752. matroska_read_close (AVFormatContext *s)
  2753. {
  2754. MatroskaDemuxContext *matroska = s->priv_data;
  2755. int n = 0;
  2756. av_free(matroska->writing_app);
  2757. av_free(matroska->muxing_app);
  2758. av_free(matroska->index);
  2759. matroska_clear_queue(matroska);
  2760. for (n = 0; n < matroska->num_tracks; n++) {
  2761. MatroskaTrack *track = matroska->tracks[n];
  2762. av_free(track->codec_id);
  2763. av_free(track->codec_name);
  2764. av_free(track->codec_priv);
  2765. av_free(track->name);
  2766. if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  2767. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2768. av_free(audiotrack->buf);
  2769. }
  2770. av_free(track);
  2771. }
  2772. return 0;
  2773. }
  2774. AVInputFormat matroska_demuxer = {
  2775. "matroska",
  2776. "Matroska file format",
  2777. sizeof(MatroskaDemuxContext),
  2778. matroska_probe,
  2779. matroska_read_header,
  2780. matroska_read_packet,
  2781. matroska_read_close,
  2782. matroska_read_seek,
  2783. };