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.

2647 lines
84KB

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