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.

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