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.

3171 lines
104KB

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