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.

3213 lines
105KB

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