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.

3050 lines
100KB

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