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.

2646 lines
84KB

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