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.

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