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.

3259 lines
108KB

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