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.

2704 lines
87KB

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