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.

3020 lines
99KB

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