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.

3203 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_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
  815. {
  816. uint8_t* data = *buf;
  817. int isize = *buf_size;
  818. uint8_t* pkt_data = NULL;
  819. int pkt_size = isize;
  820. int result = 0;
  821. int olen;
  822. switch (track->encoding_algo) {
  823. case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
  824. return track->encoding_settings_len;
  825. case MATROSKA_TRACK_ENCODING_COMP_LZO:
  826. do {
  827. olen = pkt_size *= 3;
  828. pkt_data = av_realloc(pkt_data,
  829. pkt_size+LZO_OUTPUT_PADDING);
  830. result = lzo1x_decode(pkt_data, &olen, data, &isize);
  831. } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
  832. if (result)
  833. goto failed;
  834. pkt_size -= olen;
  835. break;
  836. #ifdef CONFIG_ZLIB
  837. case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
  838. z_stream zstream = {0};
  839. if (inflateInit(&zstream) != Z_OK)
  840. return -1;
  841. zstream.next_in = data;
  842. zstream.avail_in = isize;
  843. do {
  844. pkt_size *= 3;
  845. pkt_data = av_realloc(pkt_data, pkt_size);
  846. zstream.avail_out = pkt_size - zstream.total_out;
  847. zstream.next_out = pkt_data + zstream.total_out;
  848. result = inflate(&zstream, Z_NO_FLUSH);
  849. } while (result==Z_OK && pkt_size<10000000);
  850. pkt_size = zstream.total_out;
  851. inflateEnd(&zstream);
  852. if (result != Z_STREAM_END)
  853. goto failed;
  854. break;
  855. }
  856. #endif
  857. #ifdef CONFIG_BZLIB
  858. case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
  859. bz_stream bzstream = {0};
  860. if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
  861. return -1;
  862. bzstream.next_in = data;
  863. bzstream.avail_in = isize;
  864. do {
  865. pkt_size *= 3;
  866. pkt_data = av_realloc(pkt_data, pkt_size);
  867. bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
  868. bzstream.next_out = pkt_data + bzstream.total_out_lo32;
  869. result = BZ2_bzDecompress(&bzstream);
  870. } while (result==BZ_OK && pkt_size<10000000);
  871. pkt_size = bzstream.total_out_lo32;
  872. BZ2_bzDecompressEnd(&bzstream);
  873. if (result != BZ_STREAM_END)
  874. goto failed;
  875. break;
  876. }
  877. #endif
  878. }
  879. *buf = pkt_data;
  880. *buf_size = pkt_size;
  881. return 0;
  882. failed:
  883. av_free(pkt_data);
  884. return -1;
  885. }
  886. static int
  887. matroska_add_stream (MatroskaDemuxContext *matroska)
  888. {
  889. int res = 0;
  890. uint32_t id;
  891. MatroskaTrack *track;
  892. /* start with the master */
  893. if ((res = ebml_read_master(matroska, &id)) < 0)
  894. return res;
  895. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
  896. /* Allocate a generic track. */
  897. track = av_mallocz(MAX_TRACK_SIZE);
  898. track->time_scale = 1.0;
  899. strcpy(track->language, "eng");
  900. /* try reading the trackentry headers */
  901. while (res == 0) {
  902. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  903. res = AVERROR(EIO);
  904. break;
  905. } else if (matroska->level_up > 0) {
  906. matroska->level_up--;
  907. break;
  908. }
  909. switch (id) {
  910. /* track number (unique stream ID) */
  911. case MATROSKA_ID_TRACKNUMBER: {
  912. uint64_t num;
  913. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  914. break;
  915. track->num = num;
  916. break;
  917. }
  918. /* track UID (unique identifier) */
  919. case MATROSKA_ID_TRACKUID: {
  920. uint64_t num;
  921. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  922. break;
  923. track->uid = num;
  924. break;
  925. }
  926. /* track type (video, audio, combined, subtitle, etc.) */
  927. case MATROSKA_ID_TRACKTYPE: {
  928. uint64_t num;
  929. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  930. break;
  931. if (track->type && track->type != num) {
  932. av_log(matroska->ctx, AV_LOG_INFO,
  933. "More than one tracktype in an entry - skip\n");
  934. break;
  935. }
  936. track->type = num;
  937. switch (track->type) {
  938. case MATROSKA_TRACK_TYPE_VIDEO:
  939. case MATROSKA_TRACK_TYPE_AUDIO:
  940. case MATROSKA_TRACK_TYPE_SUBTITLE:
  941. break;
  942. case MATROSKA_TRACK_TYPE_COMPLEX:
  943. case MATROSKA_TRACK_TYPE_LOGO:
  944. case MATROSKA_TRACK_TYPE_CONTROL:
  945. default:
  946. av_log(matroska->ctx, AV_LOG_INFO,
  947. "Unknown or unsupported track type 0x%x\n",
  948. track->type);
  949. track->type = MATROSKA_TRACK_TYPE_NONE;
  950. break;
  951. }
  952. break;
  953. }
  954. /* tracktype specific stuff for video */
  955. case MATROSKA_ID_TRACKVIDEO: {
  956. MatroskaVideoTrack *videotrack;
  957. if (!track->type)
  958. track->type = MATROSKA_TRACK_TYPE_VIDEO;
  959. if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
  960. av_log(matroska->ctx, AV_LOG_INFO,
  961. "video data in non-video track - ignoring\n");
  962. res = AVERROR_INVALIDDATA;
  963. break;
  964. } else if ((res = ebml_read_master(matroska, &id)) < 0)
  965. break;
  966. videotrack = (MatroskaVideoTrack *)track;
  967. while (res == 0) {
  968. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  969. res = AVERROR(EIO);
  970. break;
  971. } else if (matroska->level_up > 0) {
  972. matroska->level_up--;
  973. break;
  974. }
  975. switch (id) {
  976. /* fixme, this should be one-up, but I get it here */
  977. case MATROSKA_ID_TRACKDEFAULTDURATION: {
  978. uint64_t num;
  979. if ((res = ebml_read_uint (matroska, &id,
  980. &num)) < 0)
  981. break;
  982. track->default_duration = num;
  983. break;
  984. }
  985. /* video framerate */
  986. case MATROSKA_ID_VIDEOFRAMERATE: {
  987. double num;
  988. if ((res = ebml_read_float(matroska, &id,
  989. &num)) < 0)
  990. break;
  991. if (!track->default_duration)
  992. track->default_duration = 1000000000/num;
  993. break;
  994. }
  995. /* width of the size to display the video at */
  996. case MATROSKA_ID_VIDEODISPLAYWIDTH: {
  997. uint64_t num;
  998. if ((res = ebml_read_uint(matroska, &id,
  999. &num)) < 0)
  1000. break;
  1001. videotrack->display_width = num;
  1002. break;
  1003. }
  1004. /* height of the size to display the video at */
  1005. case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
  1006. uint64_t num;
  1007. if ((res = ebml_read_uint(matroska, &id,
  1008. &num)) < 0)
  1009. break;
  1010. videotrack->display_height = num;
  1011. break;
  1012. }
  1013. /* width of the video in the file */
  1014. case MATROSKA_ID_VIDEOPIXELWIDTH: {
  1015. uint64_t num;
  1016. if ((res = ebml_read_uint(matroska, &id,
  1017. &num)) < 0)
  1018. break;
  1019. videotrack->pixel_width = num;
  1020. break;
  1021. }
  1022. /* height of the video in the file */
  1023. case MATROSKA_ID_VIDEOPIXELHEIGHT: {
  1024. uint64_t num;
  1025. if ((res = ebml_read_uint(matroska, &id,
  1026. &num)) < 0)
  1027. break;
  1028. videotrack->pixel_height = num;
  1029. break;
  1030. }
  1031. /* whether the video is interlaced */
  1032. case MATROSKA_ID_VIDEOFLAGINTERLACED: {
  1033. uint64_t num;
  1034. if ((res = ebml_read_uint(matroska, &id,
  1035. &num)) < 0)
  1036. break;
  1037. if (num)
  1038. track->flags |=
  1039. MATROSKA_VIDEOTRACK_INTERLACED;
  1040. else
  1041. track->flags &=
  1042. ~MATROSKA_VIDEOTRACK_INTERLACED;
  1043. break;
  1044. }
  1045. /* colorspace (only matters for raw video)
  1046. * fourcc */
  1047. case MATROSKA_ID_VIDEOCOLORSPACE: {
  1048. uint64_t num;
  1049. if ((res = ebml_read_uint(matroska, &id,
  1050. &num)) < 0)
  1051. break;
  1052. videotrack->fourcc = num;
  1053. break;
  1054. }
  1055. default:
  1056. av_log(matroska->ctx, AV_LOG_INFO,
  1057. "Unknown video track header entry "
  1058. "0x%x - ignoring\n", id);
  1059. /* pass-through */
  1060. case MATROSKA_ID_VIDEOSTEREOMODE:
  1061. case MATROSKA_ID_VIDEOASPECTRATIO:
  1062. case EBML_ID_VOID:
  1063. res = ebml_read_skip(matroska);
  1064. break;
  1065. }
  1066. if (matroska->level_up) {
  1067. matroska->level_up--;
  1068. break;
  1069. }
  1070. }
  1071. break;
  1072. }
  1073. /* tracktype specific stuff for audio */
  1074. case MATROSKA_ID_TRACKAUDIO: {
  1075. MatroskaAudioTrack *audiotrack;
  1076. if (!track->type)
  1077. track->type = MATROSKA_TRACK_TYPE_AUDIO;
  1078. if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
  1079. av_log(matroska->ctx, AV_LOG_INFO,
  1080. "audio data in non-audio track - ignoring\n");
  1081. res = AVERROR_INVALIDDATA;
  1082. break;
  1083. } else if ((res = ebml_read_master(matroska, &id)) < 0)
  1084. break;
  1085. audiotrack = (MatroskaAudioTrack *)track;
  1086. audiotrack->channels = 1;
  1087. audiotrack->samplerate = 8000;
  1088. while (res == 0) {
  1089. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1090. res = AVERROR(EIO);
  1091. break;
  1092. } else if (matroska->level_up > 0) {
  1093. matroska->level_up--;
  1094. break;
  1095. }
  1096. switch (id) {
  1097. /* samplerate */
  1098. case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
  1099. double num;
  1100. if ((res = ebml_read_float(matroska, &id,
  1101. &num)) < 0)
  1102. break;
  1103. audiotrack->internal_samplerate =
  1104. audiotrack->samplerate = num;
  1105. break;
  1106. }
  1107. case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
  1108. double num;
  1109. if ((res = ebml_read_float(matroska, &id,
  1110. &num)) < 0)
  1111. break;
  1112. audiotrack->samplerate = num;
  1113. break;
  1114. }
  1115. /* bitdepth */
  1116. case MATROSKA_ID_AUDIOBITDEPTH: {
  1117. uint64_t num;
  1118. if ((res = ebml_read_uint(matroska, &id,
  1119. &num)) < 0)
  1120. break;
  1121. audiotrack->bitdepth = num;
  1122. break;
  1123. }
  1124. /* channels */
  1125. case MATROSKA_ID_AUDIOCHANNELS: {
  1126. uint64_t num;
  1127. if ((res = ebml_read_uint(matroska, &id,
  1128. &num)) < 0)
  1129. break;
  1130. audiotrack->channels = num;
  1131. break;
  1132. }
  1133. default:
  1134. av_log(matroska->ctx, AV_LOG_INFO,
  1135. "Unknown audio track header entry "
  1136. "0x%x - ignoring\n", id);
  1137. /* pass-through */
  1138. case EBML_ID_VOID:
  1139. res = ebml_read_skip(matroska);
  1140. break;
  1141. }
  1142. if (matroska->level_up) {
  1143. matroska->level_up--;
  1144. break;
  1145. }
  1146. }
  1147. break;
  1148. }
  1149. /* codec identifier */
  1150. case MATROSKA_ID_CODECID: {
  1151. char *text;
  1152. if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
  1153. break;
  1154. track->codec_id = text;
  1155. break;
  1156. }
  1157. /* codec private data */
  1158. case MATROSKA_ID_CODECPRIVATE: {
  1159. uint8_t *data;
  1160. int size;
  1161. if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
  1162. break;
  1163. track->codec_priv = data;
  1164. track->codec_priv_size = size;
  1165. break;
  1166. }
  1167. /* name of this track */
  1168. case MATROSKA_ID_TRACKNAME: {
  1169. char *text;
  1170. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1171. break;
  1172. track->name = text;
  1173. break;
  1174. }
  1175. /* language (matters for audio/subtitles, mostly) */
  1176. case MATROSKA_ID_TRACKLANGUAGE: {
  1177. char *text, *end;
  1178. if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
  1179. break;
  1180. if ((end = strchr(text, '-')))
  1181. *end = '\0';
  1182. if (strlen(text) == 3)
  1183. strcpy(track->language, text);
  1184. av_free(text);
  1185. break;
  1186. }
  1187. /* whether this is actually used */
  1188. case MATROSKA_ID_TRACKFLAGENABLED: {
  1189. uint64_t num;
  1190. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1191. break;
  1192. if (num)
  1193. track->flags |= MATROSKA_TRACK_ENABLED;
  1194. else
  1195. track->flags &= ~MATROSKA_TRACK_ENABLED;
  1196. break;
  1197. }
  1198. /* whether it's the default for this track type */
  1199. case MATROSKA_ID_TRACKFLAGDEFAULT: {
  1200. uint64_t num;
  1201. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1202. break;
  1203. if (num)
  1204. track->flags |= MATROSKA_TRACK_DEFAULT;
  1205. else
  1206. track->flags &= ~MATROSKA_TRACK_DEFAULT;
  1207. break;
  1208. }
  1209. /* lacing (like MPEG, where blocks don't end/start on frame
  1210. * boundaries) */
  1211. case MATROSKA_ID_TRACKFLAGLACING: {
  1212. uint64_t num;
  1213. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1214. break;
  1215. if (num)
  1216. track->flags |= MATROSKA_TRACK_LACING;
  1217. else
  1218. track->flags &= ~MATROSKA_TRACK_LACING;
  1219. break;
  1220. }
  1221. /* default length (in time) of one data block in this track */
  1222. case MATROSKA_ID_TRACKDEFAULTDURATION: {
  1223. uint64_t num;
  1224. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1225. break;
  1226. track->default_duration = num;
  1227. break;
  1228. }
  1229. case MATROSKA_ID_TRACKCONTENTENCODINGS: {
  1230. if ((res = ebml_read_master(matroska, &id)) < 0)
  1231. break;
  1232. while (res == 0) {
  1233. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1234. res = AVERROR(EIO);
  1235. break;
  1236. } else if (matroska->level_up > 0) {
  1237. matroska->level_up--;
  1238. break;
  1239. }
  1240. switch (id) {
  1241. case MATROSKA_ID_TRACKCONTENTENCODING: {
  1242. int encoding_scope = 1;
  1243. if ((res = ebml_read_master(matroska, &id)) < 0)
  1244. break;
  1245. while (res == 0) {
  1246. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1247. res = AVERROR(EIO);
  1248. break;
  1249. } else if (matroska->level_up > 0) {
  1250. matroska->level_up--;
  1251. break;
  1252. }
  1253. switch (id) {
  1254. case MATROSKA_ID_ENCODINGSCOPE: {
  1255. uint64_t num;
  1256. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1257. break;
  1258. encoding_scope = num;
  1259. break;
  1260. }
  1261. case MATROSKA_ID_ENCODINGTYPE: {
  1262. uint64_t num;
  1263. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1264. break;
  1265. if (num)
  1266. av_log(matroska->ctx, AV_LOG_ERROR,
  1267. "Unsupported encoding type");
  1268. break;
  1269. }
  1270. case MATROSKA_ID_ENCODINGCOMPRESSION: {
  1271. if ((res = ebml_read_master(matroska, &id)) < 0)
  1272. break;
  1273. while (res == 0) {
  1274. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1275. res = AVERROR(EIO);
  1276. break;
  1277. } else if (matroska->level_up > 0) {
  1278. matroska->level_up--;
  1279. break;
  1280. }
  1281. switch (id) {
  1282. case MATROSKA_ID_ENCODINGCOMPALGO: {
  1283. uint64_t num;
  1284. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  1285. break;
  1286. if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
  1287. #ifdef CONFIG_ZLIB
  1288. num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1289. #endif
  1290. #ifdef CONFIG_BZLIB
  1291. num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1292. #endif
  1293. num != MATROSKA_TRACK_ENCODING_COMP_LZO)
  1294. av_log(matroska->ctx, AV_LOG_ERROR,
  1295. "Unsupported compression algo\n");
  1296. track->encoding_algo = num;
  1297. break;
  1298. }
  1299. case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
  1300. uint8_t *data;
  1301. int size;
  1302. if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
  1303. break;
  1304. track->encoding_settings = data;
  1305. track->encoding_settings_len = size;
  1306. break;
  1307. }
  1308. default:
  1309. av_log(matroska->ctx, AV_LOG_INFO,
  1310. "Unknown compression header entry "
  1311. "0x%x - ignoring\n", id);
  1312. /* pass-through */
  1313. case EBML_ID_VOID:
  1314. res = ebml_read_skip(matroska);
  1315. break;
  1316. }
  1317. if (matroska->level_up) {
  1318. matroska->level_up--;
  1319. break;
  1320. }
  1321. }
  1322. break;
  1323. }
  1324. default:
  1325. av_log(matroska->ctx, AV_LOG_INFO,
  1326. "Unknown content encoding header entry "
  1327. "0x%x - ignoring\n", id);
  1328. /* pass-through */
  1329. case EBML_ID_VOID:
  1330. res = ebml_read_skip(matroska);
  1331. break;
  1332. }
  1333. if (matroska->level_up) {
  1334. matroska->level_up--;
  1335. break;
  1336. }
  1337. }
  1338. track->encoding_scope = encoding_scope;
  1339. break;
  1340. }
  1341. default:
  1342. av_log(matroska->ctx, AV_LOG_INFO,
  1343. "Unknown content encodings header entry "
  1344. "0x%x - ignoring\n", id);
  1345. /* pass-through */
  1346. case EBML_ID_VOID:
  1347. res = ebml_read_skip(matroska);
  1348. break;
  1349. }
  1350. if (matroska->level_up) {
  1351. matroska->level_up--;
  1352. break;
  1353. }
  1354. }
  1355. break;
  1356. }
  1357. case MATROSKA_ID_TRACKTIMECODESCALE: {
  1358. double num;
  1359. if ((res = ebml_read_float(matroska, &id, &num)) < 0)
  1360. break;
  1361. track->time_scale = num;
  1362. break;
  1363. }
  1364. default:
  1365. av_log(matroska->ctx, AV_LOG_INFO,
  1366. "Unknown track header entry 0x%x - ignoring\n", id);
  1367. /* pass-through */
  1368. case EBML_ID_VOID:
  1369. /* we ignore these because they're nothing useful. */
  1370. case MATROSKA_ID_TRACKFLAGFORCED:
  1371. case MATROSKA_ID_CODECNAME:
  1372. case MATROSKA_ID_CODECDECODEALL:
  1373. case MATROSKA_ID_CODECINFOURL:
  1374. case MATROSKA_ID_CODECDOWNLOADURL:
  1375. case MATROSKA_ID_TRACKMINCACHE:
  1376. case MATROSKA_ID_TRACKMAXCACHE:
  1377. res = ebml_read_skip(matroska);
  1378. break;
  1379. }
  1380. if (matroska->level_up) {
  1381. matroska->level_up--;
  1382. break;
  1383. }
  1384. }
  1385. if (track->codec_priv_size && track->encoding_scope & 2) {
  1386. uint8_t *orig_priv = track->codec_priv;
  1387. int offset = matroska_decode_buffer(&track->codec_priv,
  1388. &track->codec_priv_size, track);
  1389. if (offset > 0) {
  1390. track->codec_priv = av_malloc(track->codec_priv_size + offset);
  1391. memcpy(track->codec_priv, track->encoding_settings, offset);
  1392. memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
  1393. track->codec_priv_size += offset;
  1394. av_free(orig_priv);
  1395. } else if (!offset) {
  1396. av_free(orig_priv);
  1397. } else
  1398. av_log(matroska->ctx, AV_LOG_ERROR,
  1399. "Failed to decode codec private data\n");
  1400. }
  1401. if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
  1402. matroska->tracks[matroska->num_tracks++] = track;
  1403. } else {
  1404. av_free(track);
  1405. }
  1406. return res;
  1407. }
  1408. static int
  1409. matroska_parse_tracks (MatroskaDemuxContext *matroska)
  1410. {
  1411. int res = 0;
  1412. uint32_t id;
  1413. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
  1414. while (res == 0) {
  1415. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1416. res = AVERROR(EIO);
  1417. break;
  1418. } else if (matroska->level_up) {
  1419. matroska->level_up--;
  1420. break;
  1421. }
  1422. switch (id) {
  1423. /* one track within the "all-tracks" header */
  1424. case MATROSKA_ID_TRACKENTRY:
  1425. res = matroska_add_stream(matroska);
  1426. break;
  1427. default:
  1428. av_log(matroska->ctx, AV_LOG_INFO,
  1429. "Unknown entry 0x%x in track header\n", id);
  1430. /* fall-through */
  1431. case EBML_ID_VOID:
  1432. res = ebml_read_skip(matroska);
  1433. break;
  1434. }
  1435. if (matroska->level_up) {
  1436. matroska->level_up--;
  1437. break;
  1438. }
  1439. }
  1440. return res;
  1441. }
  1442. static int
  1443. matroska_parse_index (MatroskaDemuxContext *matroska)
  1444. {
  1445. int res = 0;
  1446. uint32_t id;
  1447. MatroskaDemuxIndex idx;
  1448. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
  1449. while (res == 0) {
  1450. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1451. res = AVERROR(EIO);
  1452. break;
  1453. } else if (matroska->level_up) {
  1454. matroska->level_up--;
  1455. break;
  1456. }
  1457. switch (id) {
  1458. /* one single index entry ('point') */
  1459. case MATROSKA_ID_POINTENTRY:
  1460. if ((res = ebml_read_master(matroska, &id)) < 0)
  1461. break;
  1462. /* in the end, we hope to fill one entry with a
  1463. * timestamp, a file position and a tracknum */
  1464. idx.pos = (uint64_t) -1;
  1465. idx.time = (uint64_t) -1;
  1466. idx.track = (uint16_t) -1;
  1467. while (res == 0) {
  1468. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1469. res = AVERROR(EIO);
  1470. break;
  1471. } else if (matroska->level_up) {
  1472. matroska->level_up--;
  1473. break;
  1474. }
  1475. switch (id) {
  1476. /* one single index entry ('point') */
  1477. case MATROSKA_ID_CUETIME: {
  1478. uint64_t time;
  1479. if ((res = ebml_read_uint(matroska, &id,
  1480. &time)) < 0)
  1481. break;
  1482. idx.time = time * matroska->time_scale;
  1483. break;
  1484. }
  1485. /* position in the file + track to which it
  1486. * belongs */
  1487. case MATROSKA_ID_CUETRACKPOSITION:
  1488. if ((res = ebml_read_master(matroska, &id)) < 0)
  1489. break;
  1490. while (res == 0) {
  1491. if (!(id = ebml_peek_id (matroska,
  1492. &matroska->level_up))) {
  1493. res = AVERROR(EIO);
  1494. break;
  1495. } else if (matroska->level_up) {
  1496. matroska->level_up--;
  1497. break;
  1498. }
  1499. switch (id) {
  1500. /* track number */
  1501. case MATROSKA_ID_CUETRACK: {
  1502. uint64_t num;
  1503. if ((res = ebml_read_uint(matroska,
  1504. &id, &num)) < 0)
  1505. break;
  1506. idx.track = num;
  1507. break;
  1508. }
  1509. /* position in file */
  1510. case MATROSKA_ID_CUECLUSTERPOSITION: {
  1511. uint64_t num;
  1512. if ((res = ebml_read_uint(matroska,
  1513. &id, &num)) < 0)
  1514. break;
  1515. idx.pos = num+matroska->segment_start;
  1516. break;
  1517. }
  1518. default:
  1519. av_log(matroska->ctx, AV_LOG_INFO,
  1520. "Unknown entry 0x%x in "
  1521. "CuesTrackPositions\n", id);
  1522. /* fall-through */
  1523. case EBML_ID_VOID:
  1524. res = ebml_read_skip(matroska);
  1525. break;
  1526. }
  1527. if (matroska->level_up) {
  1528. matroska->level_up--;
  1529. break;
  1530. }
  1531. }
  1532. break;
  1533. default:
  1534. av_log(matroska->ctx, AV_LOG_INFO,
  1535. "Unknown entry 0x%x in cuespoint "
  1536. "index\n", id);
  1537. /* fall-through */
  1538. case EBML_ID_VOID:
  1539. res = ebml_read_skip(matroska);
  1540. break;
  1541. }
  1542. if (matroska->level_up) {
  1543. matroska->level_up--;
  1544. break;
  1545. }
  1546. }
  1547. /* so let's see if we got what we wanted */
  1548. if (idx.pos != (uint64_t) -1 &&
  1549. idx.time != (uint64_t) -1 &&
  1550. idx.track != (uint16_t) -1) {
  1551. if (matroska->num_indexes % 32 == 0) {
  1552. /* re-allocate bigger index */
  1553. matroska->index =
  1554. av_realloc(matroska->index,
  1555. (matroska->num_indexes + 32) *
  1556. sizeof(MatroskaDemuxIndex));
  1557. }
  1558. matroska->index[matroska->num_indexes] = idx;
  1559. matroska->num_indexes++;
  1560. }
  1561. break;
  1562. default:
  1563. av_log(matroska->ctx, AV_LOG_INFO,
  1564. "Unknown entry 0x%x in cues header\n", id);
  1565. /* fall-through */
  1566. case EBML_ID_VOID:
  1567. res = ebml_read_skip(matroska);
  1568. break;
  1569. }
  1570. if (matroska->level_up) {
  1571. matroska->level_up--;
  1572. break;
  1573. }
  1574. }
  1575. return res;
  1576. }
  1577. static int
  1578. matroska_parse_metadata (MatroskaDemuxContext *matroska)
  1579. {
  1580. int res = 0;
  1581. uint32_t id;
  1582. while (res == 0) {
  1583. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1584. res = AVERROR(EIO);
  1585. break;
  1586. } else if (matroska->level_up) {
  1587. matroska->level_up--;
  1588. break;
  1589. }
  1590. switch (id) {
  1591. /* Hm, this is unsupported... */
  1592. default:
  1593. av_log(matroska->ctx, AV_LOG_INFO,
  1594. "Unknown entry 0x%x in metadata header\n", id);
  1595. /* fall-through */
  1596. case EBML_ID_VOID:
  1597. res = ebml_read_skip(matroska);
  1598. break;
  1599. }
  1600. if (matroska->level_up) {
  1601. matroska->level_up--;
  1602. break;
  1603. }
  1604. }
  1605. return res;
  1606. }
  1607. static int
  1608. matroska_parse_seekhead (MatroskaDemuxContext *matroska)
  1609. {
  1610. int res = 0;
  1611. uint32_t id;
  1612. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
  1613. while (res == 0) {
  1614. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1615. res = AVERROR(EIO);
  1616. break;
  1617. } else if (matroska->level_up) {
  1618. matroska->level_up--;
  1619. break;
  1620. }
  1621. switch (id) {
  1622. case MATROSKA_ID_SEEKENTRY: {
  1623. uint32_t seek_id = 0, peek_id_cache = 0;
  1624. uint64_t seek_pos = (uint64_t) -1, t;
  1625. int dummy_level = 0;
  1626. if ((res = ebml_read_master(matroska, &id)) < 0)
  1627. break;
  1628. while (res == 0) {
  1629. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1630. res = AVERROR(EIO);
  1631. break;
  1632. } else if (matroska->level_up) {
  1633. matroska->level_up--;
  1634. break;
  1635. }
  1636. switch (id) {
  1637. case MATROSKA_ID_SEEKID:
  1638. res = ebml_read_uint(matroska, &id, &t);
  1639. seek_id = t;
  1640. break;
  1641. case MATROSKA_ID_SEEKPOSITION:
  1642. res = ebml_read_uint(matroska, &id, &seek_pos);
  1643. break;
  1644. default:
  1645. av_log(matroska->ctx, AV_LOG_INFO,
  1646. "Unknown seekhead ID 0x%x\n", id);
  1647. /* fall-through */
  1648. case EBML_ID_VOID:
  1649. res = ebml_read_skip(matroska);
  1650. break;
  1651. }
  1652. if (matroska->level_up) {
  1653. matroska->level_up--;
  1654. break;
  1655. }
  1656. }
  1657. if (!seek_id || seek_pos == (uint64_t) -1) {
  1658. av_log(matroska->ctx, AV_LOG_INFO,
  1659. "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
  1660. seek_id, seek_pos);
  1661. break;
  1662. }
  1663. switch (seek_id) {
  1664. case MATROSKA_ID_CUES:
  1665. case MATROSKA_ID_TAGS: {
  1666. uint32_t level_up = matroska->level_up;
  1667. offset_t before_pos;
  1668. uint64_t length;
  1669. MatroskaLevel level;
  1670. /* remember the peeked ID and the current position */
  1671. peek_id_cache = matroska->peek_id;
  1672. before_pos = url_ftell(matroska->ctx->pb);
  1673. /* seek */
  1674. if ((res = ebml_read_seek(matroska, seek_pos +
  1675. matroska->segment_start)) < 0)
  1676. goto finish;
  1677. /* we don't want to lose our seekhead level, so we add
  1678. * a dummy. This is a crude hack. */
  1679. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1680. av_log(matroska->ctx, AV_LOG_INFO,
  1681. "Max EBML element depth (%d) reached, "
  1682. "cannot parse further.\n", EBML_MAX_DEPTH);
  1683. return AVERROR_UNKNOWN;
  1684. }
  1685. level.start = 0;
  1686. level.length = (uint64_t)-1;
  1687. matroska->levels[matroska->num_levels] = level;
  1688. matroska->num_levels++;
  1689. dummy_level = 1;
  1690. /* check ID */
  1691. if (!(id = ebml_peek_id (matroska,
  1692. &matroska->level_up)))
  1693. goto finish;
  1694. if (id != seek_id) {
  1695. av_log(matroska->ctx, AV_LOG_INFO,
  1696. "We looked for ID=0x%x but got "
  1697. "ID=0x%x (pos=%"PRIu64")",
  1698. seek_id, id, seek_pos +
  1699. matroska->segment_start);
  1700. goto finish;
  1701. }
  1702. /* read master + parse */
  1703. if ((res = ebml_read_master(matroska, &id)) < 0)
  1704. goto finish;
  1705. switch (id) {
  1706. case MATROSKA_ID_CUES:
  1707. if (!(res = matroska_parse_index(matroska)) ||
  1708. url_feof(matroska->ctx->pb)) {
  1709. matroska->index_parsed = 1;
  1710. res = 0;
  1711. }
  1712. break;
  1713. case MATROSKA_ID_TAGS:
  1714. if (!(res = matroska_parse_metadata(matroska)) ||
  1715. url_feof(matroska->ctx->pb)) {
  1716. matroska->metadata_parsed = 1;
  1717. res = 0;
  1718. }
  1719. break;
  1720. }
  1721. finish:
  1722. /* remove dummy level */
  1723. if (dummy_level)
  1724. while (matroska->num_levels) {
  1725. matroska->num_levels--;
  1726. length =
  1727. matroska->levels[matroska->num_levels].length;
  1728. if (length == (uint64_t)-1)
  1729. break;
  1730. }
  1731. /* seek back */
  1732. if ((res = ebml_read_seek(matroska, before_pos)) < 0)
  1733. return res;
  1734. matroska->peek_id = peek_id_cache;
  1735. matroska->level_up = level_up;
  1736. break;
  1737. }
  1738. default:
  1739. av_log(matroska->ctx, AV_LOG_INFO,
  1740. "Ignoring seekhead entry for ID=0x%x\n",
  1741. seek_id);
  1742. break;
  1743. }
  1744. break;
  1745. }
  1746. default:
  1747. av_log(matroska->ctx, AV_LOG_INFO,
  1748. "Unknown seekhead ID 0x%x\n", id);
  1749. /* fall-through */
  1750. case EBML_ID_VOID:
  1751. res = ebml_read_skip(matroska);
  1752. break;
  1753. }
  1754. if (matroska->level_up) {
  1755. matroska->level_up--;
  1756. break;
  1757. }
  1758. }
  1759. return res;
  1760. }
  1761. static int
  1762. matroska_parse_attachments(AVFormatContext *s)
  1763. {
  1764. MatroskaDemuxContext *matroska = s->priv_data;
  1765. int res = 0;
  1766. uint32_t id;
  1767. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
  1768. while (res == 0) {
  1769. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1770. res = AVERROR(EIO);
  1771. break;
  1772. } else if (matroska->level_up) {
  1773. matroska->level_up--;
  1774. break;
  1775. }
  1776. switch (id) {
  1777. case MATROSKA_ID_ATTACHEDFILE: {
  1778. char* name = NULL;
  1779. char* mime = NULL;
  1780. uint8_t* data = NULL;
  1781. int i, data_size = 0;
  1782. AVStream *st;
  1783. if ((res = ebml_read_master(matroska, &id)) < 0)
  1784. break;
  1785. while (res == 0) {
  1786. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1787. res = AVERROR(EIO);
  1788. break;
  1789. } else if (matroska->level_up) {
  1790. matroska->level_up--;
  1791. break;
  1792. }
  1793. switch (id) {
  1794. case MATROSKA_ID_FILENAME:
  1795. res = ebml_read_utf8 (matroska, &id, &name);
  1796. break;
  1797. case MATROSKA_ID_FILEMIMETYPE:
  1798. res = ebml_read_ascii (matroska, &id, &mime);
  1799. break;
  1800. case MATROSKA_ID_FILEDATA:
  1801. res = ebml_read_binary(matroska, &id, &data, &data_size);
  1802. break;
  1803. default:
  1804. av_log(matroska->ctx, AV_LOG_INFO,
  1805. "Unknown attachedfile ID 0x%x\n", id);
  1806. case MATROSKA_ID_FILEUID:
  1807. case EBML_ID_VOID:
  1808. res = ebml_read_skip(matroska);
  1809. break;
  1810. }
  1811. if (matroska->level_up) {
  1812. matroska->level_up--;
  1813. break;
  1814. }
  1815. }
  1816. if (!(name && mime && data && data_size > 0)) {
  1817. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1818. break;
  1819. }
  1820. st = av_new_stream(s, matroska->num_streams++);
  1821. if (st == NULL)
  1822. return AVERROR(ENOMEM);
  1823. st->filename = av_strdup(name);
  1824. st->codec->codec_id = CODEC_ID_NONE;
  1825. st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
  1826. st->codec->extradata = av_malloc(data_size);
  1827. if(st->codec->extradata == NULL)
  1828. return AVERROR(ENOMEM);
  1829. st->codec->extradata_size = data_size;
  1830. memcpy(st->codec->extradata, data, data_size);
  1831. for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
  1832. if (!strncmp(ff_mkv_mime_tags[i].str, mime,
  1833. strlen(ff_mkv_mime_tags[i].str))) {
  1834. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1835. break;
  1836. }
  1837. }
  1838. av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
  1839. break;
  1840. }
  1841. default:
  1842. av_log(matroska->ctx, AV_LOG_INFO,
  1843. "Unknown attachments ID 0x%x\n", id);
  1844. /* fall-through */
  1845. case EBML_ID_VOID:
  1846. res = ebml_read_skip(matroska);
  1847. break;
  1848. }
  1849. if (matroska->level_up) {
  1850. matroska->level_up--;
  1851. break;
  1852. }
  1853. }
  1854. return res;
  1855. }
  1856. static int
  1857. matroska_parse_chapters(AVFormatContext *s)
  1858. {
  1859. MatroskaDemuxContext *matroska = s->priv_data;
  1860. int res = 0;
  1861. uint32_t id;
  1862. av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
  1863. while (res == 0) {
  1864. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1865. res = AVERROR(EIO);
  1866. break;
  1867. } else if (matroska->level_up) {
  1868. matroska->level_up--;
  1869. break;
  1870. }
  1871. switch (id) {
  1872. case MATROSKA_ID_EDITIONENTRY: {
  1873. uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
  1874. int64_t uid= -1;
  1875. char* title = NULL;
  1876. /* if there is more than one chapter edition
  1877. we take only the first one */
  1878. if(s->chapters) {
  1879. ebml_read_skip(matroska);
  1880. break;
  1881. }
  1882. if ((res = ebml_read_master(matroska, &id)) < 0)
  1883. break;
  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. case MATROSKA_ID_CHAPTERATOM:
  1894. if ((res = ebml_read_master(matroska, &id)) < 0)
  1895. break;
  1896. while (res == 0) {
  1897. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1898. res = AVERROR(EIO);
  1899. break;
  1900. } else if (matroska->level_up) {
  1901. matroska->level_up--;
  1902. break;
  1903. }
  1904. switch (id) {
  1905. case MATROSKA_ID_CHAPTERTIMEEND:
  1906. res = ebml_read_uint(matroska, &id, &end);
  1907. break;
  1908. case MATROSKA_ID_CHAPTERTIMESTART:
  1909. res = ebml_read_uint(matroska, &id, &start);
  1910. break;
  1911. case MATROSKA_ID_CHAPTERDISPLAY:
  1912. if ((res = ebml_read_master(matroska, &id)) < 0)
  1913. break;
  1914. while (res == 0) {
  1915. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  1916. res = AVERROR(EIO);
  1917. break;
  1918. } else if (matroska->level_up) {
  1919. matroska->level_up--;
  1920. break;
  1921. }
  1922. switch (id) {
  1923. case MATROSKA_ID_CHAPSTRING:
  1924. res = ebml_read_utf8(matroska, &id, &title);
  1925. break;
  1926. default:
  1927. av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
  1928. case EBML_ID_VOID:
  1929. res = ebml_read_skip(matroska);
  1930. break;
  1931. }
  1932. if (matroska->level_up) {
  1933. matroska->level_up--;
  1934. break;
  1935. }
  1936. }
  1937. break;
  1938. case MATROSKA_ID_CHAPTERUID:
  1939. res = ebml_read_uint(matroska, &id, &uid);
  1940. break;
  1941. default:
  1942. av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
  1943. case MATROSKA_ID_CHAPTERFLAGHIDDEN:
  1944. case EBML_ID_VOID:
  1945. res = ebml_read_skip(matroska);
  1946. break;
  1947. }
  1948. if (matroska->level_up) {
  1949. matroska->level_up--;
  1950. break;
  1951. }
  1952. }
  1953. if (start != AV_NOPTS_VALUE && uid != -1) {
  1954. if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
  1955. res= AVERROR(ENOMEM);
  1956. }
  1957. av_free(title);
  1958. break;
  1959. default:
  1960. av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
  1961. case MATROSKA_ID_EDITIONUID:
  1962. case MATROSKA_ID_EDITIONFLAGHIDDEN:
  1963. case MATROSKA_ID_EDITIONFLAGDEFAULT:
  1964. case EBML_ID_VOID:
  1965. res = ebml_read_skip(matroska);
  1966. break;
  1967. }
  1968. if (matroska->level_up) {
  1969. matroska->level_up--;
  1970. break;
  1971. }
  1972. }
  1973. break;
  1974. }
  1975. default:
  1976. av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
  1977. case EBML_ID_VOID:
  1978. res = ebml_read_skip(matroska);
  1979. break;
  1980. }
  1981. if (matroska->level_up) {
  1982. matroska->level_up--;
  1983. break;
  1984. }
  1985. }
  1986. return res;
  1987. }
  1988. static int
  1989. matroska_aac_profile (char *codec_id)
  1990. {
  1991. static const char *aac_profiles[] = {
  1992. "MAIN", "LC", "SSR"
  1993. };
  1994. int profile;
  1995. for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
  1996. if (strstr(codec_id, aac_profiles[profile]))
  1997. break;
  1998. return profile + 1;
  1999. }
  2000. static int
  2001. matroska_aac_sri (int samplerate)
  2002. {
  2003. int sri;
  2004. for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
  2005. if (ff_mpeg4audio_sample_rates[sri] == samplerate)
  2006. break;
  2007. return sri;
  2008. }
  2009. static int
  2010. matroska_read_header (AVFormatContext *s,
  2011. AVFormatParameters *ap)
  2012. {
  2013. MatroskaDemuxContext *matroska = s->priv_data;
  2014. char *doctype;
  2015. int version, last_level, res = 0;
  2016. uint32_t id;
  2017. matroska->ctx = s;
  2018. /* First read the EBML header. */
  2019. doctype = NULL;
  2020. if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
  2021. return res;
  2022. if ((doctype == NULL) || strcmp(doctype, "matroska")) {
  2023. av_log(matroska->ctx, AV_LOG_ERROR,
  2024. "Wrong EBML doctype ('%s' != 'matroska').\n",
  2025. doctype ? doctype : "(none)");
  2026. if (doctype)
  2027. av_free(doctype);
  2028. return AVERROR_NOFMT;
  2029. }
  2030. av_free(doctype);
  2031. if (version > 2) {
  2032. av_log(matroska->ctx, AV_LOG_ERROR,
  2033. "Matroska demuxer version 2 too old for file version %d\n",
  2034. version);
  2035. return AVERROR_NOFMT;
  2036. }
  2037. /* The next thing is a segment. */
  2038. while (1) {
  2039. if (!(id = ebml_peek_id(matroska, &last_level)))
  2040. return AVERROR(EIO);
  2041. if (id == MATROSKA_ID_SEGMENT)
  2042. break;
  2043. /* oi! */
  2044. av_log(matroska->ctx, AV_LOG_INFO,
  2045. "Expected a Segment ID (0x%x), but received 0x%x!\n",
  2046. MATROSKA_ID_SEGMENT, id);
  2047. if ((res = ebml_read_skip(matroska)) < 0)
  2048. return res;
  2049. }
  2050. /* We now have a Matroska segment.
  2051. * Seeks are from the beginning of the segment,
  2052. * after the segment ID/length. */
  2053. if ((res = ebml_read_master(matroska, &id)) < 0)
  2054. return res;
  2055. matroska->segment_start = url_ftell(s->pb);
  2056. matroska->time_scale = 1000000;
  2057. /* we've found our segment, start reading the different contents in here */
  2058. while (res == 0) {
  2059. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2060. res = AVERROR(EIO);
  2061. break;
  2062. } else if (matroska->level_up) {
  2063. matroska->level_up--;
  2064. break;
  2065. }
  2066. switch (id) {
  2067. /* stream info */
  2068. case MATROSKA_ID_INFO: {
  2069. if ((res = ebml_read_master(matroska, &id)) < 0)
  2070. break;
  2071. res = matroska_parse_info(matroska);
  2072. break;
  2073. }
  2074. /* track info headers */
  2075. case MATROSKA_ID_TRACKS: {
  2076. if ((res = ebml_read_master(matroska, &id)) < 0)
  2077. break;
  2078. res = matroska_parse_tracks(matroska);
  2079. break;
  2080. }
  2081. /* stream index */
  2082. case MATROSKA_ID_CUES: {
  2083. if (!matroska->index_parsed) {
  2084. if ((res = ebml_read_master(matroska, &id)) < 0)
  2085. break;
  2086. res = matroska_parse_index(matroska);
  2087. } else
  2088. res = ebml_read_skip(matroska);
  2089. break;
  2090. }
  2091. /* metadata */
  2092. case MATROSKA_ID_TAGS: {
  2093. if (!matroska->metadata_parsed) {
  2094. if ((res = ebml_read_master(matroska, &id)) < 0)
  2095. break;
  2096. res = matroska_parse_metadata(matroska);
  2097. } else
  2098. res = ebml_read_skip(matroska);
  2099. break;
  2100. }
  2101. /* file index (if seekable, seek to Cues/Tags to parse it) */
  2102. case MATROSKA_ID_SEEKHEAD: {
  2103. if ((res = ebml_read_master(matroska, &id)) < 0)
  2104. break;
  2105. res = matroska_parse_seekhead(matroska);
  2106. break;
  2107. }
  2108. case MATROSKA_ID_ATTACHMENTS: {
  2109. if ((res = ebml_read_master(matroska, &id)) < 0)
  2110. break;
  2111. res = matroska_parse_attachments(s);
  2112. break;
  2113. }
  2114. case MATROSKA_ID_CLUSTER: {
  2115. /* Do not read the master - this will be done in the next
  2116. * call to matroska_read_packet. */
  2117. res = 1;
  2118. break;
  2119. }
  2120. case MATROSKA_ID_CHAPTERS: {
  2121. if ((res = ebml_read_master(matroska, &id)) < 0)
  2122. return res;
  2123. res = matroska_parse_chapters(s);
  2124. break;
  2125. }
  2126. default:
  2127. av_log(matroska->ctx, AV_LOG_INFO,
  2128. "Unknown matroska file header ID 0x%x\n", id);
  2129. /* fall-through */
  2130. case EBML_ID_VOID:
  2131. res = ebml_read_skip(matroska);
  2132. break;
  2133. }
  2134. if (matroska->level_up) {
  2135. matroska->level_up--;
  2136. break;
  2137. }
  2138. }
  2139. /* Have we found a cluster? */
  2140. if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
  2141. int i, j;
  2142. MatroskaTrack *track;
  2143. AVStream *st;
  2144. for (i = 0; i < matroska->num_tracks; i++) {
  2145. enum CodecID codec_id = CODEC_ID_NONE;
  2146. uint8_t *extradata = NULL;
  2147. int extradata_size = 0;
  2148. int extradata_offset = 0;
  2149. track = matroska->tracks[i];
  2150. track->stream_index = -1;
  2151. /* Apply some sanity checks. */
  2152. if (track->codec_id == NULL)
  2153. continue;
  2154. for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
  2155. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  2156. strlen(ff_mkv_codec_tags[j].str))){
  2157. codec_id= ff_mkv_codec_tags[j].id;
  2158. break;
  2159. }
  2160. }
  2161. /* Set the FourCC from the CodecID. */
  2162. /* This is the MS compatibility mode which stores a
  2163. * BITMAPINFOHEADER in the CodecPrivate. */
  2164. if (!strcmp(track->codec_id,
  2165. MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
  2166. (track->codec_priv_size >= 40) &&
  2167. (track->codec_priv != NULL)) {
  2168. MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
  2169. /* Offset of biCompression. Stored in LE. */
  2170. vtrack->fourcc = AV_RL32(track->codec_priv + 16);
  2171. codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
  2172. }
  2173. /* This is the MS compatibility mode which stores a
  2174. * WAVEFORMATEX in the CodecPrivate. */
  2175. else if (!strcmp(track->codec_id,
  2176. MATROSKA_CODEC_ID_AUDIO_ACM) &&
  2177. (track->codec_priv_size >= 18) &&
  2178. (track->codec_priv != NULL)) {
  2179. uint16_t tag;
  2180. /* Offset of wFormatTag. Stored in LE. */
  2181. tag = AV_RL16(track->codec_priv);
  2182. codec_id = codec_get_id(codec_wav_tags, tag);
  2183. }
  2184. else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
  2185. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
  2186. int profile = matroska_aac_profile(track->codec_id);
  2187. int sri = matroska_aac_sri(audiotrack->internal_samplerate);
  2188. extradata = av_malloc(5);
  2189. if (extradata == NULL)
  2190. return AVERROR(ENOMEM);
  2191. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  2192. extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
  2193. if (strstr(track->codec_id, "SBR")) {
  2194. sri = matroska_aac_sri(audiotrack->samplerate);
  2195. extradata[2] = 0x56;
  2196. extradata[3] = 0xE5;
  2197. extradata[4] = 0x80 | (sri<<3);
  2198. extradata_size = 5;
  2199. } else {
  2200. extradata_size = 2;
  2201. }
  2202. }
  2203. else if (codec_id == CODEC_ID_TTA) {
  2204. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
  2205. ByteIOContext b;
  2206. extradata_size = 30;
  2207. extradata = av_mallocz(extradata_size);
  2208. if (extradata == NULL)
  2209. return AVERROR(ENOMEM);
  2210. init_put_byte(&b, extradata, extradata_size, 1,
  2211. NULL, NULL, NULL, NULL);
  2212. put_buffer(&b, "TTA1", 4);
  2213. put_le16(&b, 1);
  2214. put_le16(&b, audiotrack->channels);
  2215. put_le16(&b, audiotrack->bitdepth);
  2216. put_le32(&b, audiotrack->samplerate);
  2217. put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
  2218. }
  2219. else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
  2220. codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
  2221. extradata_offset = 26;
  2222. track->codec_priv_size -= extradata_offset;
  2223. }
  2224. else if (codec_id == CODEC_ID_RA_144) {
  2225. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2226. audiotrack->samplerate = 8000;
  2227. audiotrack->channels = 1;
  2228. }
  2229. else if (codec_id == CODEC_ID_RA_288 ||
  2230. codec_id == CODEC_ID_COOK ||
  2231. codec_id == CODEC_ID_ATRAC3) {
  2232. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2233. ByteIOContext b;
  2234. init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
  2235. NULL, NULL, NULL, NULL);
  2236. url_fskip(&b, 24);
  2237. audiotrack->coded_framesize = get_be32(&b);
  2238. url_fskip(&b, 12);
  2239. audiotrack->sub_packet_h = get_be16(&b);
  2240. audiotrack->frame_size = get_be16(&b);
  2241. audiotrack->sub_packet_size = get_be16(&b);
  2242. audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
  2243. if (codec_id == CODEC_ID_RA_288) {
  2244. audiotrack->block_align = audiotrack->coded_framesize;
  2245. track->codec_priv_size = 0;
  2246. } else {
  2247. audiotrack->block_align = audiotrack->sub_packet_size;
  2248. extradata_offset = 78;
  2249. track->codec_priv_size -= extradata_offset;
  2250. }
  2251. }
  2252. if (codec_id == CODEC_ID_NONE) {
  2253. av_log(matroska->ctx, AV_LOG_INFO,
  2254. "Unknown/unsupported CodecID %s.\n",
  2255. track->codec_id);
  2256. }
  2257. track->stream_index = matroska->num_streams;
  2258. matroska->num_streams++;
  2259. st = av_new_stream(s, track->stream_index);
  2260. if (st == NULL)
  2261. return AVERROR(ENOMEM);
  2262. av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  2263. st->codec->codec_id = codec_id;
  2264. st->start_time = 0;
  2265. if (strcmp(track->language, "und"))
  2266. strcpy(st->language, track->language);
  2267. if (track->flags & MATROSKA_TRACK_DEFAULT)
  2268. st->disposition |= AV_DISPOSITION_DEFAULT;
  2269. if (track->default_duration)
  2270. av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
  2271. track->default_duration, 1000000000, 30000);
  2272. if(extradata){
  2273. st->codec->extradata = extradata;
  2274. st->codec->extradata_size = extradata_size;
  2275. } else if(track->codec_priv && track->codec_priv_size > 0){
  2276. st->codec->extradata = av_malloc(track->codec_priv_size);
  2277. if(st->codec->extradata == NULL)
  2278. return AVERROR(ENOMEM);
  2279. st->codec->extradata_size = track->codec_priv_size;
  2280. memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
  2281. track->codec_priv_size);
  2282. }
  2283. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  2284. MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
  2285. st->codec->codec_type = CODEC_TYPE_VIDEO;
  2286. st->codec->codec_tag = videotrack->fourcc;
  2287. st->codec->width = videotrack->pixel_width;
  2288. st->codec->height = videotrack->pixel_height;
  2289. if (videotrack->display_width == 0)
  2290. videotrack->display_width= videotrack->pixel_width;
  2291. if (videotrack->display_height == 0)
  2292. videotrack->display_height= videotrack->pixel_height;
  2293. av_reduce(&st->codec->sample_aspect_ratio.num,
  2294. &st->codec->sample_aspect_ratio.den,
  2295. st->codec->height * videotrack->display_width,
  2296. st->codec-> width * videotrack->display_height,
  2297. 255);
  2298. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  2299. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  2300. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2301. st->codec->codec_type = CODEC_TYPE_AUDIO;
  2302. st->codec->sample_rate = audiotrack->samplerate;
  2303. st->codec->channels = audiotrack->channels;
  2304. st->codec->block_align = audiotrack->block_align;
  2305. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  2306. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  2307. }
  2308. /* What do we do with private data? E.g. for Vorbis. */
  2309. }
  2310. res = 0;
  2311. }
  2312. if (matroska->index_parsed) {
  2313. int i, track, stream;
  2314. for (i=0; i<matroska->num_indexes; i++) {
  2315. MatroskaDemuxIndex *idx = &matroska->index[i];
  2316. track = matroska_find_track_by_num(matroska, idx->track);
  2317. if (track < 0) continue;
  2318. stream = matroska->tracks[track]->stream_index;
  2319. if (stream >= 0 && stream < matroska->ctx->nb_streams)
  2320. av_add_index_entry(matroska->ctx->streams[stream],
  2321. idx->pos, idx->time/AV_TIME_BASE,
  2322. 0, 0, AVINDEX_KEYFRAME);
  2323. }
  2324. }
  2325. return res;
  2326. }
  2327. static int
  2328. matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
  2329. int64_t pos, uint64_t cluster_time, uint64_t duration,
  2330. int is_keyframe, int is_bframe)
  2331. {
  2332. int res = 0;
  2333. int track;
  2334. AVStream *st;
  2335. AVPacket *pkt;
  2336. uint8_t *origdata = data;
  2337. int16_t block_time;
  2338. uint32_t *lace_size = NULL;
  2339. int n, flags, laces = 0;
  2340. uint64_t num;
  2341. int stream_index;
  2342. /* first byte(s): tracknum */
  2343. if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
  2344. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  2345. av_free(origdata);
  2346. return res;
  2347. }
  2348. data += n;
  2349. size -= n;
  2350. /* fetch track from num */
  2351. track = matroska_find_track_by_num(matroska, num);
  2352. if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
  2353. av_log(matroska->ctx, AV_LOG_INFO,
  2354. "Invalid stream %d or size %u\n", track, size);
  2355. av_free(origdata);
  2356. return res;
  2357. }
  2358. stream_index = matroska->tracks[track]->stream_index;
  2359. if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
  2360. av_free(origdata);
  2361. return res;
  2362. }
  2363. st = matroska->ctx->streams[stream_index];
  2364. if (st->discard >= AVDISCARD_ALL) {
  2365. av_free(origdata);
  2366. return res;
  2367. }
  2368. if (duration == AV_NOPTS_VALUE)
  2369. duration = matroska->tracks[track]->default_duration / matroska->time_scale;
  2370. /* block_time (relative to cluster time) */
  2371. block_time = AV_RB16(data);
  2372. data += 2;
  2373. flags = *data++;
  2374. size -= 3;
  2375. if (is_keyframe == -1)
  2376. is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
  2377. if (matroska->skip_to_keyframe) {
  2378. if (!is_keyframe || st != matroska->skip_to_stream) {
  2379. av_free(origdata);
  2380. return res;
  2381. }
  2382. matroska->skip_to_keyframe = 0;
  2383. }
  2384. switch ((flags & 0x06) >> 1) {
  2385. case 0x0: /* no lacing */
  2386. laces = 1;
  2387. lace_size = av_mallocz(sizeof(int));
  2388. lace_size[0] = size;
  2389. break;
  2390. case 0x1: /* xiph lacing */
  2391. case 0x2: /* fixed-size lacing */
  2392. case 0x3: /* EBML lacing */
  2393. assert(size>0); // size <=3 is checked before size-=3 above
  2394. laces = (*data) + 1;
  2395. data += 1;
  2396. size -= 1;
  2397. lace_size = av_mallocz(laces * sizeof(int));
  2398. switch ((flags & 0x06) >> 1) {
  2399. case 0x1: /* xiph lacing */ {
  2400. uint8_t temp;
  2401. uint32_t total = 0;
  2402. for (n = 0; res == 0 && n < laces - 1; n++) {
  2403. while (1) {
  2404. if (size == 0) {
  2405. res = -1;
  2406. break;
  2407. }
  2408. temp = *data;
  2409. lace_size[n] += temp;
  2410. data += 1;
  2411. size -= 1;
  2412. if (temp != 0xff)
  2413. break;
  2414. }
  2415. total += lace_size[n];
  2416. }
  2417. lace_size[n] = size - total;
  2418. break;
  2419. }
  2420. case 0x2: /* fixed-size lacing */
  2421. for (n = 0; n < laces; n++)
  2422. lace_size[n] = size / laces;
  2423. break;
  2424. case 0x3: /* EBML lacing */ {
  2425. uint32_t total;
  2426. n = matroska_ebmlnum_uint(data, size, &num);
  2427. if (n < 0) {
  2428. av_log(matroska->ctx, AV_LOG_INFO,
  2429. "EBML block data error\n");
  2430. break;
  2431. }
  2432. data += n;
  2433. size -= n;
  2434. total = lace_size[0] = num;
  2435. for (n = 1; res == 0 && n < laces - 1; n++) {
  2436. int64_t snum;
  2437. int r;
  2438. r = matroska_ebmlnum_sint (data, size, &snum);
  2439. if (r < 0) {
  2440. av_log(matroska->ctx, AV_LOG_INFO,
  2441. "EBML block data error\n");
  2442. break;
  2443. }
  2444. data += r;
  2445. size -= r;
  2446. lace_size[n] = lace_size[n - 1] + snum;
  2447. total += lace_size[n];
  2448. }
  2449. lace_size[n] = size - total;
  2450. break;
  2451. }
  2452. }
  2453. break;
  2454. }
  2455. if (res == 0) {
  2456. uint64_t timecode = AV_NOPTS_VALUE;
  2457. if (cluster_time != (uint64_t)-1
  2458. && (block_time >= 0 || cluster_time >= -block_time))
  2459. timecode = cluster_time + block_time;
  2460. for (n = 0; n < laces; n++) {
  2461. if (st->codec->codec_id == CODEC_ID_RA_288 ||
  2462. st->codec->codec_id == CODEC_ID_COOK ||
  2463. st->codec->codec_id == CODEC_ID_ATRAC3) {
  2464. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
  2465. int a = st->codec->block_align;
  2466. int sps = audiotrack->sub_packet_size;
  2467. int cfs = audiotrack->coded_framesize;
  2468. int h = audiotrack->sub_packet_h;
  2469. int y = audiotrack->sub_packet_cnt;
  2470. int w = audiotrack->frame_size;
  2471. int x;
  2472. if (!audiotrack->pkt_cnt) {
  2473. if (st->codec->codec_id == CODEC_ID_RA_288)
  2474. for (x=0; x<h/2; x++)
  2475. memcpy(audiotrack->buf+x*2*w+y*cfs,
  2476. data+x*cfs, cfs);
  2477. else
  2478. for (x=0; x<w/sps; x++)
  2479. memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  2480. if (++audiotrack->sub_packet_cnt >= h) {
  2481. audiotrack->sub_packet_cnt = 0;
  2482. audiotrack->pkt_cnt = h*w / a;
  2483. }
  2484. }
  2485. while (audiotrack->pkt_cnt) {
  2486. pkt = av_mallocz(sizeof(AVPacket));
  2487. av_new_packet(pkt, a);
  2488. memcpy(pkt->data, audiotrack->buf
  2489. + a * (h*w / a - audiotrack->pkt_cnt--), a);
  2490. pkt->pos = pos;
  2491. pkt->stream_index = stream_index;
  2492. matroska_queue_packet(matroska, pkt);
  2493. }
  2494. } else {
  2495. int offset = 0, pkt_size = lace_size[n];
  2496. uint8_t *pkt_data = data;
  2497. if (matroska->tracks[track]->encoding_scope & 1) {
  2498. offset = matroska_decode_buffer(&pkt_data, &pkt_size,
  2499. matroska->tracks[track]);
  2500. if (offset < 0)
  2501. continue;
  2502. }
  2503. pkt = av_mallocz(sizeof(AVPacket));
  2504. /* XXX: prevent data copy... */
  2505. if (av_new_packet(pkt, pkt_size+offset) < 0) {
  2506. av_free(pkt);
  2507. res = AVERROR(ENOMEM);
  2508. n = laces-1;
  2509. break;
  2510. }
  2511. if (offset)
  2512. memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
  2513. memcpy (pkt->data+offset, pkt_data, pkt_size);
  2514. if (pkt_data != data)
  2515. av_free(pkt_data);
  2516. if (n == 0)
  2517. pkt->flags = is_keyframe;
  2518. pkt->stream_index = stream_index;
  2519. pkt->pts = timecode;
  2520. pkt->pos = pos;
  2521. pkt->duration = duration;
  2522. matroska_queue_packet(matroska, pkt);
  2523. }
  2524. if (timecode != AV_NOPTS_VALUE)
  2525. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  2526. data += lace_size[n];
  2527. }
  2528. }
  2529. av_free(lace_size);
  2530. av_free(origdata);
  2531. return res;
  2532. }
  2533. static int
  2534. matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
  2535. uint64_t cluster_time)
  2536. {
  2537. int res = 0;
  2538. uint32_t id;
  2539. int is_bframe = 0;
  2540. int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
  2541. uint64_t duration = AV_NOPTS_VALUE;
  2542. uint8_t *data;
  2543. int size = 0;
  2544. int64_t pos = 0;
  2545. av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
  2546. while (res == 0) {
  2547. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2548. res = AVERROR(EIO);
  2549. break;
  2550. } else if (matroska->level_up) {
  2551. matroska->level_up--;
  2552. break;
  2553. }
  2554. switch (id) {
  2555. /* one block inside the group. Note, block parsing is one
  2556. * of the harder things, so this code is a bit complicated.
  2557. * See http://www.matroska.org/ for documentation. */
  2558. case MATROSKA_ID_BLOCK: {
  2559. pos = url_ftell(matroska->ctx->pb);
  2560. res = ebml_read_binary(matroska, &id, &data, &size);
  2561. break;
  2562. }
  2563. case MATROSKA_ID_BLOCKDURATION: {
  2564. if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
  2565. break;
  2566. break;
  2567. }
  2568. case MATROSKA_ID_BLOCKREFERENCE: {
  2569. int64_t num;
  2570. /* We've found a reference, so not even the first frame in
  2571. * the lace is a key frame. */
  2572. is_keyframe = 0;
  2573. if (last_num_packets != matroska->num_packets)
  2574. matroska->packets[last_num_packets]->flags = 0;
  2575. if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
  2576. break;
  2577. if (num > 0)
  2578. is_bframe = 1;
  2579. break;
  2580. }
  2581. default:
  2582. av_log(matroska->ctx, AV_LOG_INFO,
  2583. "Unknown entry 0x%x in blockgroup data\n", id);
  2584. /* fall-through */
  2585. case EBML_ID_VOID:
  2586. res = ebml_read_skip(matroska);
  2587. break;
  2588. }
  2589. if (matroska->level_up) {
  2590. matroska->level_up--;
  2591. break;
  2592. }
  2593. }
  2594. if (res)
  2595. return res;
  2596. if (size > 0)
  2597. res = matroska_parse_block(matroska, data, size, pos, cluster_time,
  2598. duration, is_keyframe, is_bframe);
  2599. return res;
  2600. }
  2601. static int
  2602. matroska_parse_cluster (MatroskaDemuxContext *matroska)
  2603. {
  2604. int res = 0;
  2605. uint32_t id;
  2606. uint64_t cluster_time = 0;
  2607. uint8_t *data;
  2608. int64_t pos;
  2609. int size;
  2610. av_log(matroska->ctx, AV_LOG_DEBUG,
  2611. "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
  2612. while (res == 0) {
  2613. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2614. res = AVERROR(EIO);
  2615. break;
  2616. } else if (matroska->level_up) {
  2617. matroska->level_up--;
  2618. break;
  2619. }
  2620. switch (id) {
  2621. /* cluster timecode */
  2622. case MATROSKA_ID_CLUSTERTIMECODE: {
  2623. uint64_t num;
  2624. if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
  2625. break;
  2626. cluster_time = num;
  2627. break;
  2628. }
  2629. /* a group of blocks inside a cluster */
  2630. case MATROSKA_ID_BLOCKGROUP:
  2631. if ((res = ebml_read_master(matroska, &id)) < 0)
  2632. break;
  2633. res = matroska_parse_blockgroup(matroska, cluster_time);
  2634. break;
  2635. case MATROSKA_ID_SIMPLEBLOCK:
  2636. pos = url_ftell(matroska->ctx->pb);
  2637. res = ebml_read_binary(matroska, &id, &data, &size);
  2638. if (res == 0)
  2639. res = matroska_parse_block(matroska, data, size, pos,
  2640. cluster_time, AV_NOPTS_VALUE,
  2641. -1, 0);
  2642. break;
  2643. default:
  2644. av_log(matroska->ctx, AV_LOG_INFO,
  2645. "Unknown entry 0x%x in cluster data\n", id);
  2646. /* fall-through */
  2647. case EBML_ID_VOID:
  2648. res = ebml_read_skip(matroska);
  2649. break;
  2650. }
  2651. if (matroska->level_up) {
  2652. matroska->level_up--;
  2653. break;
  2654. }
  2655. }
  2656. return res;
  2657. }
  2658. static int
  2659. matroska_read_packet (AVFormatContext *s,
  2660. AVPacket *pkt)
  2661. {
  2662. MatroskaDemuxContext *matroska = s->priv_data;
  2663. int res;
  2664. uint32_t id;
  2665. /* Read stream until we have a packet queued. */
  2666. while (matroska_deliver_packet(matroska, pkt)) {
  2667. /* Have we already reached the end? */
  2668. if (matroska->done)
  2669. return AVERROR(EIO);
  2670. res = 0;
  2671. while (res == 0) {
  2672. if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
  2673. return AVERROR(EIO);
  2674. } else if (matroska->level_up) {
  2675. matroska->level_up--;
  2676. break;
  2677. }
  2678. switch (id) {
  2679. case MATROSKA_ID_CLUSTER:
  2680. if ((res = ebml_read_master(matroska, &id)) < 0)
  2681. break;
  2682. if ((res = matroska_parse_cluster(matroska)) == 0)
  2683. res = 1; /* Parsed one cluster, let's get out. */
  2684. break;
  2685. default:
  2686. case EBML_ID_VOID:
  2687. res = ebml_read_skip(matroska);
  2688. break;
  2689. }
  2690. if (matroska->level_up) {
  2691. matroska->level_up--;
  2692. break;
  2693. }
  2694. }
  2695. if (res == -1)
  2696. matroska->done = 1;
  2697. }
  2698. return 0;
  2699. }
  2700. static int
  2701. matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
  2702. int flags)
  2703. {
  2704. MatroskaDemuxContext *matroska = s->priv_data;
  2705. AVStream *st = s->streams[stream_index];
  2706. int index;
  2707. /* find index entry */
  2708. index = av_index_search_timestamp(st, timestamp, flags);
  2709. if (index < 0)
  2710. return 0;
  2711. matroska_clear_queue(matroska);
  2712. /* do the seek */
  2713. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  2714. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  2715. matroska->skip_to_stream = st;
  2716. matroska->peek_id = 0;
  2717. av_update_cur_dts(s, st, st->index_entries[index].timestamp);
  2718. return 0;
  2719. }
  2720. static int
  2721. matroska_read_close (AVFormatContext *s)
  2722. {
  2723. MatroskaDemuxContext *matroska = s->priv_data;
  2724. int n = 0;
  2725. av_free(matroska->index);
  2726. matroska_clear_queue(matroska);
  2727. for (n = 0; n < matroska->num_tracks; n++) {
  2728. MatroskaTrack *track = matroska->tracks[n];
  2729. av_free(track->codec_id);
  2730. av_free(track->codec_priv);
  2731. av_free(track->name);
  2732. if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  2733. MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
  2734. av_free(audiotrack->buf);
  2735. }
  2736. av_free(track);
  2737. }
  2738. return 0;
  2739. }
  2740. AVInputFormat matroska_demuxer = {
  2741. "matroska",
  2742. NULL_IF_CONFIG_SMALL("Matroska file format"),
  2743. sizeof(MatroskaDemuxContext),
  2744. matroska_probe,
  2745. matroska_read_header,
  2746. matroska_read_packet,
  2747. matroska_read_close,
  2748. matroska_read_seek,
  2749. };