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.

3080 lines
101KB

  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. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
  1826. static int
  1827. matroska_aac_profile (char *codec_id)
  1828. {
  1829. static const char *aac_profiles[] = {
  1830. "MAIN", "LC", "SSR"
  1831. };
  1832. int profile;
  1833. for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
  1834. if (strstr(codec_id, aac_profiles[profile]))
  1835. break;
  1836. return profile + 1;
  1837. }
  1838. static int
  1839. matroska_aac_sri (int samplerate)
  1840. {
  1841. int sri;
  1842. for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
  1843. if (ff_mpeg4audio_sample_rates[sri] == samplerate)
  1844. break;
  1845. return sri;
  1846. }
  1847. static int
  1848. matroska_read_header (AVFormatContext *s,
  1849. AVFormatParameters *ap)
  1850. {
  1851. MatroskaDemuxContext *matroska = s->priv_data;
  1852. char *doctype;
  1853. int version, last_level, res = 0;
  1854. uint32_t id;
  1855. matroska->ctx = s;
  1856. /* First read the EBML header. */
  1857. doctype = NULL;
  1858. if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
  1859. return res;
  1860. if ((doctype == NULL) || strcmp(doctype, "matroska")) {
  1861. av_log(matroska->ctx, AV_LOG_ERROR,
  1862. "Wrong EBML doctype ('%s' != 'matroska').\n",
  1863. doctype ? doctype : "(none)");
  1864. if (doctype)
  1865. av_free(doctype);
  1866. return AVERROR_NOFMT;
  1867. }
  1868. av_free(doctype);
  1869. if (version > 2) {
  1870. av_log(matroska->ctx, AV_LOG_ERROR,
  1871. "Matroska demuxer version 2 too old for file version %d\n",
  1872. version);
  1873. return AVERROR_NOFMT;
  1874. }
  1875. /* The next thing is a segment. */
  1876. while (1) {
  1877. if (!(id = ebml_peek_id(matroska, &last_level)))
  1878. return AVERROR(EIO);
  1879. if (id == MATROSKA_ID_SEGMENT)
  1880. break;
  1881. /* oi! */
  1882. av_log(matroska->ctx, AV_LOG_INFO,
  1883. "Expected a Segment ID (0x%x), but received 0x%x!\n",
  1884. MATROSKA_ID_SEGMENT, id);
  1885. if ((res = ebml_read_skip(matroska)) < 0)
  1886. return res;
  1887. }
  1888. /* We now have a Matroska segment.
  1889. * Seeks are from the beginning of the segment,
  1890. * after the segment ID/length. */
  1891. if ((res = ebml_read_master(matroska, &id)) < 0)
  1892. return res;
  1893. matroska->segment_start = url_ftell(s->pb);
  1894. matroska->time_scale = 1000000;
  1895. /* we've found our segment, start reading the different contents in here */
  1896. while (res == 0) {
  1897. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1898. res = AVERROR(EIO);
  1899. break;
  1900. } else if (matroska->level_up) {
  1901. matroska->level_up--;
  1902. break;
  1903. }
  1904. switch (id) {
  1905. /* stream info */
  1906. case MATROSKA_ID_INFO: {
  1907. if ((res = ebml_read_master(matroska, &id)) < 0)
  1908. break;
  1909. res = matroska_parse_info(matroska);
  1910. break;
  1911. }
  1912. /* track info headers */
  1913. case MATROSKA_ID_TRACKS: {
  1914. if ((res = ebml_read_master(matroska, &id)) < 0)
  1915. break;
  1916. res = matroska_parse_tracks(matroska);
  1917. break;
  1918. }
  1919. /* stream index */
  1920. case MATROSKA_ID_CUES: {
  1921. if (!matroska->index_parsed) {
  1922. if ((res = ebml_read_master(matroska, &id)) < 0)
  1923. break;
  1924. res = matroska_parse_index(matroska);
  1925. } else
  1926. res = ebml_read_skip(matroska);
  1927. break;
  1928. }
  1929. /* metadata */
  1930. case MATROSKA_ID_TAGS: {
  1931. if (!matroska->metadata_parsed) {
  1932. if ((res = ebml_read_master(matroska, &id)) < 0)
  1933. break;
  1934. res = matroska_parse_metadata(matroska);
  1935. } else
  1936. res = ebml_read_skip(matroska);
  1937. break;
  1938. }
  1939. /* file index (if seekable, seek to Cues/Tags to parse it) */
  1940. case MATROSKA_ID_SEEKHEAD: {
  1941. if ((res = ebml_read_master(matroska, &id)) < 0)
  1942. break;
  1943. res = matroska_parse_seekhead(matroska);
  1944. break;
  1945. }
  1946. case MATROSKA_ID_ATTACHMENTS: {
  1947. if ((res = ebml_read_master(matroska, &id)) < 0)
  1948. break;
  1949. res = matroska_parse_attachments(s);
  1950. break;
  1951. }
  1952. case MATROSKA_ID_CLUSTER: {
  1953. /* Do not read the master - this will be done in the next
  1954. * call to matroska_read_packet. */
  1955. res = 1;
  1956. break;
  1957. }
  1958. default:
  1959. av_log(matroska->ctx, AV_LOG_INFO,
  1960. "Unknown matroska file header ID 0x%x\n", id);
  1961. /* fall-through */
  1962. case EBML_ID_VOID:
  1963. res = ebml_read_skip(matroska);
  1964. break;
  1965. }
  1966. if (matroska->level_up) {
  1967. matroska->level_up--;
  1968. break;
  1969. }
  1970. }
  1971. /* Have we found a cluster? */
  1972. if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
  1973. int i, j;
  1974. MatroskaTrack *track;
  1975. AVStream *st;
  1976. for (i = 0; i < matroska->num_tracks; i++) {
  1977. enum CodecID codec_id = CODEC_ID_NONE;
  1978. uint8_t *extradata = NULL;
  1979. int extradata_size = 0;
  1980. int extradata_offset = 0;
  1981. track = matroska->tracks[i];
  1982. track->stream_index = -1;
  1983. /* Apply some sanity checks. */
  1984. if (track->codec_id == NULL)
  1985. continue;
  1986. for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
  1987. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  1988. strlen(ff_mkv_codec_tags[j].str))){
  1989. codec_id= ff_mkv_codec_tags[j].id;
  1990. break;
  1991. }
  1992. }
  1993. /* Set the FourCC from the CodecID. */
  1994. /* This is the MS compatibility mode which stores a
  1995. * BITMAPINFOHEADER in the CodecPrivate. */
  1996. if (!strcmp(track->codec_id,
  1997. MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
  1998. (track->codec_priv_size >= 40) &&
  1999. (track->codec_priv != NULL)) {
  2000. MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
  2001. /* Offset of biCompression. Stored in LE. */
  2002. vtrack->fourcc = AV_RL32(track->codec_priv + 16);
  2003. codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
  2004. }
  2005. /* This is the MS compatibility mode which stores a
  2006. * WAVEFORMATEX in the CodecPrivate. */
  2007. else if (!strcmp(track->codec_id,
  2008. MATROSKA_CODEC_ID_AUDIO_ACM) &&
  2009. (track->codec_priv_size >= 18) &&
  2010. (track->codec_priv != NULL)) {
  2011. uint16_t tag;
  2012. /* Offset of wFormatTag. Stored in LE. */
  2013. tag = AV_RL16(track->codec_priv);
  2014. codec_id = codec_get_id(codec_wav_tags, tag);
  2015. }
  2016. else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
  2017. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
  2018. int profile = matroska_aac_profile(track->codec_id);
  2019. int sri = matroska_aac_sri(audiotrack->internal_samplerate);
  2020. extradata = av_malloc(5);
  2021. if (extradata == NULL)
  2022. return AVERROR(ENOMEM);
  2023. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  2024. extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
  2025. if (strstr(track->codec_id, "SBR")) {
  2026. sri = matroska_aac_sri(audiotrack->samplerate);
  2027. extradata[2] = 0x56;
  2028. extradata[3] = 0xE5;
  2029. extradata[4] = 0x80 | (sri<<3);
  2030. extradata_size = 5;
  2031. } else {
  2032. extradata_size = 2;
  2033. }
  2034. }
  2035. else if (codec_id == CODEC_ID_TTA) {
  2036. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
  2037. ByteIOContext b;
  2038. extradata_size = 30;
  2039. extradata = av_mallocz(extradata_size);
  2040. if (extradata == NULL)
  2041. return AVERROR(ENOMEM);
  2042. init_put_byte(&b, extradata, extradata_size, 1,
  2043. NULL, NULL, NULL, NULL);
  2044. put_buffer(&b, "TTA1", 4);
  2045. put_le16(&b, 1);
  2046. put_le16(&b, audiotrack->channels);
  2047. put_le16(&b, audiotrack->bitdepth);
  2048. put_le32(&b, audiotrack->samplerate);
  2049. put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
  2050. }
  2051. else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
  2052. codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
  2053. extradata_offset = 26;
  2054. track->codec_priv_size -= extradata_offset;
  2055. }
  2056. else if (codec_id == CODEC_ID_RA_144) {
  2057. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2058. audiotrack->samplerate = 8000;
  2059. audiotrack->channels = 1;
  2060. }
  2061. else if (codec_id == CODEC_ID_RA_288 ||
  2062. codec_id == CODEC_ID_COOK ||
  2063. codec_id == CODEC_ID_ATRAC3) {
  2064. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2065. ByteIOContext b;
  2066. init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
  2067. NULL, NULL, NULL, NULL);
  2068. url_fskip(&b, 24);
  2069. audiotrack->coded_framesize = get_be32(&b);
  2070. url_fskip(&b, 12);
  2071. audiotrack->sub_packet_h = get_be16(&b);
  2072. audiotrack->frame_size = get_be16(&b);
  2073. audiotrack->sub_packet_size = get_be16(&b);
  2074. audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
  2075. if (codec_id == CODEC_ID_RA_288) {
  2076. audiotrack->block_align = audiotrack->coded_framesize;
  2077. track->codec_priv_size = 0;
  2078. } else {
  2079. audiotrack->block_align = audiotrack->sub_packet_size;
  2080. extradata_offset = 78;
  2081. track->codec_priv_size -= extradata_offset;
  2082. }
  2083. }
  2084. if (codec_id == CODEC_ID_NONE) {
  2085. av_log(matroska->ctx, AV_LOG_INFO,
  2086. "Unknown/unsupported CodecID %s.\n",
  2087. track->codec_id);
  2088. }
  2089. track->stream_index = matroska->num_streams;
  2090. matroska->num_streams++;
  2091. st = av_new_stream(s, track->stream_index);
  2092. if (st == NULL)
  2093. return AVERROR(ENOMEM);
  2094. av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  2095. st->codec->codec_id = codec_id;
  2096. st->start_time = 0;
  2097. if (strcmp(track->language, "und"))
  2098. strcpy(st->language, track->language);
  2099. if (track->flags & MATROSKA_TRACK_DEFAULT)
  2100. st->disposition |= AV_DISPOSITION_DEFAULT;
  2101. if (track->default_duration)
  2102. av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
  2103. track->default_duration, 1000000000, 30000);
  2104. if(extradata){
  2105. st->codec->extradata = extradata;
  2106. st->codec->extradata_size = extradata_size;
  2107. } else if(track->codec_priv && track->codec_priv_size > 0){
  2108. st->codec->extradata = av_malloc(track->codec_priv_size);
  2109. if(st->codec->extradata == NULL)
  2110. return AVERROR(ENOMEM);
  2111. st->codec->extradata_size = track->codec_priv_size;
  2112. memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
  2113. track->codec_priv_size);
  2114. }
  2115. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  2116. MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
  2117. st->codec->codec_type = CODEC_TYPE_VIDEO;
  2118. st->codec->codec_tag = videotrack->fourcc;
  2119. st->codec->width = videotrack->pixel_width;
  2120. st->codec->height = videotrack->pixel_height;
  2121. if (videotrack->display_width == 0)
  2122. videotrack->display_width= videotrack->pixel_width;
  2123. if (videotrack->display_height == 0)
  2124. videotrack->display_height= videotrack->pixel_height;
  2125. av_reduce(&st->codec->sample_aspect_ratio.num,
  2126. &st->codec->sample_aspect_ratio.den,
  2127. st->codec->height * videotrack->display_width,
  2128. st->codec-> width * videotrack->display_height,
  2129. 255);
  2130. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  2131. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  2132. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2133. st->codec->codec_type = CODEC_TYPE_AUDIO;
  2134. st->codec->sample_rate = audiotrack->samplerate;
  2135. st->codec->channels = audiotrack->channels;
  2136. st->codec->block_align = audiotrack->block_align;
  2137. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  2138. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  2139. }
  2140. /* What do we do with private data? E.g. for Vorbis. */
  2141. }
  2142. res = 0;
  2143. }
  2144. if (matroska->index_parsed) {
  2145. int i, track, stream;
  2146. for (i=0; i<matroska->num_indexes; i++) {
  2147. MatroskaDemuxIndex *idx = &matroska->index[i];
  2148. track = matroska_find_track_by_num(matroska, idx->track);
  2149. if (track < 0) continue;
  2150. stream = matroska->tracks[track]->stream_index;
  2151. if (stream >= 0 && stream < matroska->ctx->nb_streams)
  2152. av_add_index_entry(matroska->ctx->streams[stream],
  2153. idx->pos, idx->time/matroska->time_scale,
  2154. 0, 0, AVINDEX_KEYFRAME);
  2155. }
  2156. }
  2157. return res;
  2158. }
  2159. static int
  2160. matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
  2161. int64_t pos, uint64_t cluster_time, uint64_t duration,
  2162. int is_keyframe, int is_bframe)
  2163. {
  2164. int res = 0;
  2165. int track;
  2166. AVStream *st;
  2167. AVPacket *pkt;
  2168. uint8_t *origdata = data;
  2169. int16_t block_time;
  2170. uint32_t *lace_size = NULL;
  2171. int n, flags, laces = 0;
  2172. uint64_t num;
  2173. int stream_index;
  2174. /* first byte(s): tracknum */
  2175. if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
  2176. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  2177. av_free(origdata);
  2178. return res;
  2179. }
  2180. data += n;
  2181. size -= n;
  2182. /* fetch track from num */
  2183. track = matroska_find_track_by_num(matroska, num);
  2184. if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
  2185. av_log(matroska->ctx, AV_LOG_INFO,
  2186. "Invalid stream %d or size %u\n", track, size);
  2187. av_free(origdata);
  2188. return res;
  2189. }
  2190. stream_index = matroska->tracks[track]->stream_index;
  2191. if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
  2192. av_free(origdata);
  2193. return res;
  2194. }
  2195. st = matroska->ctx->streams[stream_index];
  2196. if (st->discard >= AVDISCARD_ALL) {
  2197. av_free(origdata);
  2198. return res;
  2199. }
  2200. if (duration == AV_NOPTS_VALUE)
  2201. duration = matroska->tracks[track]->default_duration / matroska->time_scale;
  2202. /* block_time (relative to cluster time) */
  2203. block_time = AV_RB16(data);
  2204. data += 2;
  2205. flags = *data++;
  2206. size -= 3;
  2207. if (is_keyframe == -1)
  2208. is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
  2209. if (matroska->skip_to_keyframe) {
  2210. if (!is_keyframe || st != matroska->skip_to_stream) {
  2211. av_free(origdata);
  2212. return res;
  2213. }
  2214. matroska->skip_to_keyframe = 0;
  2215. }
  2216. switch ((flags & 0x06) >> 1) {
  2217. case 0x0: /* no lacing */
  2218. laces = 1;
  2219. lace_size = av_mallocz(sizeof(int));
  2220. lace_size[0] = size;
  2221. break;
  2222. case 0x1: /* xiph lacing */
  2223. case 0x2: /* fixed-size lacing */
  2224. case 0x3: /* EBML lacing */
  2225. if (size == 0) {
  2226. res = -1;
  2227. break;
  2228. }
  2229. laces = (*data) + 1;
  2230. data += 1;
  2231. size -= 1;
  2232. lace_size = av_mallocz(laces * sizeof(int));
  2233. switch ((flags & 0x06) >> 1) {
  2234. case 0x1: /* xiph lacing */ {
  2235. uint8_t temp;
  2236. uint32_t total = 0;
  2237. for (n = 0; res == 0 && n < laces - 1; n++) {
  2238. while (1) {
  2239. if (size == 0) {
  2240. res = -1;
  2241. break;
  2242. }
  2243. temp = *data;
  2244. lace_size[n] += temp;
  2245. data += 1;
  2246. size -= 1;
  2247. if (temp != 0xff)
  2248. break;
  2249. }
  2250. total += lace_size[n];
  2251. }
  2252. lace_size[n] = size - total;
  2253. break;
  2254. }
  2255. case 0x2: /* fixed-size lacing */
  2256. for (n = 0; n < laces; n++)
  2257. lace_size[n] = size / laces;
  2258. break;
  2259. case 0x3: /* EBML lacing */ {
  2260. uint32_t total;
  2261. n = matroska_ebmlnum_uint(data, size, &num);
  2262. if (n < 0) {
  2263. av_log(matroska->ctx, AV_LOG_INFO,
  2264. "EBML block data error\n");
  2265. break;
  2266. }
  2267. data += n;
  2268. size -= n;
  2269. total = lace_size[0] = num;
  2270. for (n = 1; res == 0 && n < laces - 1; n++) {
  2271. int64_t snum;
  2272. int r;
  2273. r = matroska_ebmlnum_sint (data, size, &snum);
  2274. if (r < 0) {
  2275. av_log(matroska->ctx, AV_LOG_INFO,
  2276. "EBML block data error\n");
  2277. break;
  2278. }
  2279. data += r;
  2280. size -= r;
  2281. lace_size[n] = lace_size[n - 1] + snum;
  2282. total += lace_size[n];
  2283. }
  2284. lace_size[n] = size - total;
  2285. break;
  2286. }
  2287. }
  2288. break;
  2289. }
  2290. if (res == 0) {
  2291. uint64_t timecode = AV_NOPTS_VALUE;
  2292. if (cluster_time != (uint64_t)-1
  2293. && (block_time >= 0 || cluster_time >= -block_time))
  2294. timecode = cluster_time + block_time;
  2295. for (n = 0; n < laces; n++) {
  2296. if (st->codec->codec_id == CODEC_ID_RA_288 ||
  2297. st->codec->codec_id == CODEC_ID_COOK ||
  2298. st->codec->codec_id == CODEC_ID_ATRAC3) {
  2299. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
  2300. int a = st->codec->block_align;
  2301. int sps = audiotrack->sub_packet_size;
  2302. int cfs = audiotrack->coded_framesize;
  2303. int h = audiotrack->sub_packet_h;
  2304. int y = audiotrack->sub_packet_cnt;
  2305. int w = audiotrack->frame_size;
  2306. int x;
  2307. if (!audiotrack->pkt_cnt) {
  2308. if (st->codec->codec_id == CODEC_ID_RA_288)
  2309. for (x=0; x<h/2; x++)
  2310. memcpy(audiotrack->buf+x*2*w+y*cfs,
  2311. data+x*cfs, cfs);
  2312. else
  2313. for (x=0; x<w/sps; x++)
  2314. memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  2315. if (++audiotrack->sub_packet_cnt >= h) {
  2316. audiotrack->sub_packet_cnt = 0;
  2317. audiotrack->pkt_cnt = h*w / a;
  2318. }
  2319. }
  2320. while (audiotrack->pkt_cnt) {
  2321. pkt = av_mallocz(sizeof(AVPacket));
  2322. av_new_packet(pkt, a);
  2323. memcpy(pkt->data, audiotrack->buf
  2324. + a * (h*w / a - audiotrack->pkt_cnt--), a);
  2325. pkt->pos = pos;
  2326. pkt->stream_index = stream_index;
  2327. matroska_queue_packet(matroska, pkt);
  2328. }
  2329. } else {
  2330. int result, offset = 0, ilen, olen, pkt_size = lace_size[n];
  2331. uint8_t *pkt_data = data;
  2332. if (matroska->tracks[track]->encoding_scope & 1) {
  2333. switch (matroska->tracks[track]->encoding_algo) {
  2334. case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
  2335. offset = matroska->tracks[track]->encoding_settings_len;
  2336. break;
  2337. case MATROSKA_TRACK_ENCODING_COMP_LZO:
  2338. pkt_data = NULL;
  2339. do {
  2340. ilen = lace_size[n];
  2341. olen = pkt_size *= 3;
  2342. pkt_data = av_realloc(pkt_data,
  2343. pkt_size+LZO_OUTPUT_PADDING);
  2344. result = lzo1x_decode(pkt_data, &olen, data, &ilen);
  2345. } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
  2346. if (result) {
  2347. av_free(pkt_data);
  2348. continue;
  2349. }
  2350. pkt_size -= olen;
  2351. break;
  2352. #ifdef CONFIG_ZLIB
  2353. case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
  2354. z_stream zstream = {0};
  2355. pkt_data = NULL;
  2356. if (inflateInit(&zstream) != Z_OK)
  2357. continue;
  2358. zstream.next_in = data;
  2359. zstream.avail_in = lace_size[n];
  2360. do {
  2361. pkt_size *= 3;
  2362. pkt_data = av_realloc(pkt_data, pkt_size);
  2363. zstream.avail_out = pkt_size - zstream.total_out;
  2364. zstream.next_out = pkt_data + zstream.total_out;
  2365. result = inflate(&zstream, Z_NO_FLUSH);
  2366. } while (result==Z_OK && pkt_size<10000000);
  2367. pkt_size = zstream.total_out;
  2368. inflateEnd(&zstream);
  2369. if (result != Z_STREAM_END) {
  2370. av_free(pkt_data);
  2371. continue;
  2372. }
  2373. break;
  2374. }
  2375. #endif
  2376. #ifdef CONFIG_BZLIB
  2377. case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
  2378. bz_stream bzstream = {0};
  2379. pkt_data = NULL;
  2380. if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
  2381. continue;
  2382. bzstream.next_in = data;
  2383. bzstream.avail_in = lace_size[n];
  2384. do {
  2385. pkt_size *= 3;
  2386. pkt_data = av_realloc(pkt_data, pkt_size);
  2387. bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
  2388. bzstream.next_out = pkt_data + bzstream.total_out_lo32;
  2389. result = BZ2_bzDecompress(&bzstream);
  2390. } while (result==BZ_OK && pkt_size<10000000);
  2391. pkt_size = bzstream.total_out_lo32;
  2392. BZ2_bzDecompressEnd(&bzstream);
  2393. if (result != BZ_STREAM_END) {
  2394. av_free(pkt_data);
  2395. continue;
  2396. }
  2397. break;
  2398. }
  2399. #endif
  2400. }
  2401. }
  2402. pkt = av_mallocz(sizeof(AVPacket));
  2403. /* XXX: prevent data copy... */
  2404. if (av_new_packet(pkt, pkt_size+offset) < 0) {
  2405. res = AVERROR(ENOMEM);
  2406. n = laces-1;
  2407. break;
  2408. }
  2409. if (offset)
  2410. memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
  2411. memcpy (pkt->data+offset, pkt_data, pkt_size);
  2412. if (n == 0)
  2413. pkt->flags = is_keyframe;
  2414. pkt->stream_index = stream_index;
  2415. pkt->pts = timecode;
  2416. pkt->pos = pos;
  2417. pkt->duration = duration;
  2418. matroska_queue_packet(matroska, pkt);
  2419. }
  2420. if (timecode != AV_NOPTS_VALUE)
  2421. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  2422. data += lace_size[n];
  2423. }
  2424. }
  2425. av_free(lace_size);
  2426. av_free(origdata);
  2427. return res;
  2428. }
  2429. static int
  2430. matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
  2431. uint64_t cluster_time)
  2432. {
  2433. int res = 0;
  2434. uint32_t id;
  2435. int is_bframe = 0;
  2436. int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
  2437. uint64_t duration = AV_NOPTS_VALUE;
  2438. uint8_t *data;
  2439. int size = 0;
  2440. int64_t pos = 0;
  2441. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
  2442. while (res == 0) {
  2443. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2444. res = AVERROR(EIO);
  2445. break;
  2446. } else if (matroska->level_up) {
  2447. matroska->level_up--;
  2448. break;
  2449. }
  2450. switch (id) {
  2451. /* one block inside the group. Note, block parsing is one
  2452. * of the harder things, so this code is a bit complicated.
  2453. * See http://www.matroska.org/ for documentation. */
  2454. case MATROSKA_ID_BLOCK: {
  2455. pos = url_ftell(matroska->ctx->pb);
  2456. res = ebml_read_binary(matroska, &id, &data, &size);
  2457. break;
  2458. }
  2459. case MATROSKA_ID_BLOCKDURATION: {
  2460. if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
  2461. break;
  2462. break;
  2463. }
  2464. case MATROSKA_ID_BLOCKREFERENCE: {
  2465. int64_t num;
  2466. /* We've found a reference, so not even the first frame in
  2467. * the lace is a key frame. */
  2468. is_keyframe = 0;
  2469. if (last_num_packets != matroska->num_packets)
  2470. matroska->packets[last_num_packets]->flags = 0;
  2471. if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
  2472. break;
  2473. if (num > 0)
  2474. is_bframe = 1;
  2475. break;
  2476. }
  2477. default:
  2478. av_log(matroska->ctx, AV_LOG_INFO,
  2479. "Unknown entry 0x%x in blockgroup data\n", id);
  2480. /* fall-through */
  2481. case EBML_ID_VOID:
  2482. res = ebml_read_skip(matroska);
  2483. break;
  2484. }
  2485. if (matroska->level_up) {
  2486. matroska->level_up--;
  2487. break;
  2488. }
  2489. }
  2490. if (res)
  2491. return res;
  2492. if (size > 0)
  2493. res = matroska_parse_block(matroska, data, size, pos, cluster_time,
  2494. duration, is_keyframe, is_bframe);
  2495. return res;
  2496. }
  2497. static int
  2498. matroska_parse_cluster (MatroskaDemuxContext *matroska)
  2499. {
  2500. int res = 0;
  2501. uint32_t id;
  2502. uint64_t cluster_time = 0;
  2503. uint8_t *data;
  2504. int64_t pos;
  2505. int size;
  2506. av_log(matroska->ctx, AV_LOG_DEBUG,
  2507. "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
  2508. while (res == 0) {
  2509. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2510. res = AVERROR(EIO);
  2511. break;
  2512. } else if (matroska->level_up) {
  2513. matroska->level_up--;
  2514. break;
  2515. }
  2516. switch (id) {
  2517. /* cluster timecode */
  2518. case MATROSKA_ID_CLUSTERTIMECODE: {
  2519. uint64_t num;
  2520. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  2521. break;
  2522. cluster_time = num;
  2523. break;
  2524. }
  2525. /* a group of blocks inside a cluster */
  2526. case MATROSKA_ID_BLOCKGROUP:
  2527. if ((res = ebml_read_master(matroska, &id)) < 0)
  2528. break;
  2529. res = matroska_parse_blockgroup(matroska, cluster_time);
  2530. break;
  2531. case MATROSKA_ID_SIMPLEBLOCK:
  2532. pos = url_ftell(matroska->ctx->pb);
  2533. res = ebml_read_binary(matroska, &id, &data, &size);
  2534. if (res == 0)
  2535. res = matroska_parse_block(matroska, data, size, pos,
  2536. cluster_time, AV_NOPTS_VALUE,
  2537. -1, 0);
  2538. break;
  2539. default:
  2540. av_log(matroska->ctx, AV_LOG_INFO,
  2541. "Unknown entry 0x%x in cluster data\n", id);
  2542. /* fall-through */
  2543. case EBML_ID_VOID:
  2544. res = ebml_read_skip(matroska);
  2545. break;
  2546. }
  2547. if (matroska->level_up) {
  2548. matroska->level_up--;
  2549. break;
  2550. }
  2551. }
  2552. return res;
  2553. }
  2554. static int
  2555. matroska_read_packet (AVFormatContext *s,
  2556. AVPacket *pkt)
  2557. {
  2558. MatroskaDemuxContext *matroska = s->priv_data;
  2559. int res;
  2560. uint32_t id;
  2561. /* Read stream until we have a packet queued. */
  2562. while (matroska_deliver_packet(matroska, pkt)) {
  2563. /* Have we already reached the end? */
  2564. if (matroska->done)
  2565. return AVERROR(EIO);
  2566. res = 0;
  2567. while (res == 0) {
  2568. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2569. return AVERROR(EIO);
  2570. } else if (matroska->level_up) {
  2571. matroska->level_up--;
  2572. break;
  2573. }
  2574. switch (id) {
  2575. case MATROSKA_ID_CLUSTER:
  2576. if ((res = ebml_read_master(matroska, &id)) < 0)
  2577. break;
  2578. if ((res = matroska_parse_cluster(matroska)) == 0)
  2579. res = 1; /* Parsed one cluster, let's get out. */
  2580. break;
  2581. default:
  2582. case EBML_ID_VOID:
  2583. res = ebml_read_skip(matroska);
  2584. break;
  2585. }
  2586. if (matroska->level_up) {
  2587. matroska->level_up--;
  2588. break;
  2589. }
  2590. }
  2591. if (res == -1)
  2592. matroska->done = 1;
  2593. }
  2594. return 0;
  2595. }
  2596. static int
  2597. matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
  2598. int flags)
  2599. {
  2600. MatroskaDemuxContext *matroska = s->priv_data;
  2601. AVStream *st = s->streams[stream_index];
  2602. int index;
  2603. /* find index entry */
  2604. index = av_index_search_timestamp(st, timestamp, flags);
  2605. if (index < 0)
  2606. return 0;
  2607. matroska_clear_queue(matroska);
  2608. /* do the seek */
  2609. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  2610. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  2611. matroska->skip_to_stream = st;
  2612. matroska->peek_id = 0;
  2613. return 0;
  2614. }
  2615. static int
  2616. matroska_read_close (AVFormatContext *s)
  2617. {
  2618. MatroskaDemuxContext *matroska = s->priv_data;
  2619. int n = 0;
  2620. av_free(matroska->writing_app);
  2621. av_free(matroska->muxing_app);
  2622. av_free(matroska->index);
  2623. matroska_clear_queue(matroska);
  2624. for (n = 0; n < matroska->num_tracks; n++) {
  2625. MatroskaTrack *track = matroska->tracks[n];
  2626. av_free(track->codec_id);
  2627. av_free(track->codec_name);
  2628. av_free(track->codec_priv);
  2629. av_free(track->name);
  2630. if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  2631. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2632. av_free(audiotrack->buf);
  2633. }
  2634. av_free(track);
  2635. }
  2636. return 0;
  2637. }
  2638. AVInputFormat matroska_demuxer = {
  2639. "matroska",
  2640. "Matroska file format",
  2641. sizeof(MatroskaDemuxContext),
  2642. matroska_probe,
  2643. matroska_read_header,
  2644. matroska_read_packet,
  2645. matroska_read_close,
  2646. matroska_read_seek,
  2647. };