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.

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