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.

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