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.

2850 lines
90KB

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