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.

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