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.

1707 lines
60KB

  1. /*
  2. * Matroska file demuxer
  3. * Copyright (c) 2003-2008 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 matroskadec.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. * totally reworked by Aurelien Jacobs <aurel@gnuage.org>
  27. * Specs available on the Matroska project page: http://www.matroska.org/.
  28. */
  29. #include "avformat.h"
  30. /* For codec_get_id(). */
  31. #include "riff.h"
  32. #include "isom.h"
  33. #include "matroska.h"
  34. #include "libavcodec/mpeg4audio.h"
  35. #include "libavutil/intfloat_readwrite.h"
  36. #include "libavutil/avstring.h"
  37. #include "libavutil/lzo.h"
  38. #ifdef CONFIG_ZLIB
  39. #include <zlib.h>
  40. #endif
  41. #ifdef CONFIG_BZLIB
  42. #include <bzlib.h>
  43. #endif
  44. typedef enum {
  45. EBML_NONE,
  46. EBML_UINT,
  47. EBML_FLOAT,
  48. EBML_STR,
  49. EBML_UTF8,
  50. EBML_BIN,
  51. EBML_NEST,
  52. EBML_PASS,
  53. EBML_STOP,
  54. } EbmlType;
  55. typedef const struct EbmlSyntax {
  56. uint32_t id;
  57. EbmlType type;
  58. int list_elem_size;
  59. int data_offset;
  60. union {
  61. uint64_t u;
  62. double f;
  63. const char *s;
  64. const struct EbmlSyntax *n;
  65. } def;
  66. } EbmlSyntax;
  67. typedef struct {
  68. int nb_elem;
  69. void *elem;
  70. } EbmlList;
  71. typedef struct {
  72. int size;
  73. uint8_t *data;
  74. int64_t pos;
  75. } EbmlBin;
  76. typedef struct {
  77. uint64_t version;
  78. uint64_t max_size;
  79. uint64_t id_length;
  80. char *doctype;
  81. uint64_t doctype_version;
  82. } Ebml;
  83. typedef struct {
  84. uint64_t algo;
  85. EbmlBin settings;
  86. } MatroskaTrackCompression;
  87. typedef struct {
  88. uint64_t scope;
  89. uint64_t type;
  90. MatroskaTrackCompression compression;
  91. } MatroskaTrackEncoding;
  92. typedef struct {
  93. double frame_rate;
  94. uint64_t display_width;
  95. uint64_t display_height;
  96. uint64_t pixel_width;
  97. uint64_t pixel_height;
  98. uint64_t fourcc;
  99. } MatroskaTrackVideo;
  100. typedef struct {
  101. double samplerate;
  102. double out_samplerate;
  103. uint64_t bitdepth;
  104. uint64_t channels;
  105. /* real audio header (extracted from extradata) */
  106. int coded_framesize;
  107. int sub_packet_h;
  108. int frame_size;
  109. int sub_packet_size;
  110. int sub_packet_cnt;
  111. int pkt_cnt;
  112. uint8_t *buf;
  113. } MatroskaTrackAudio;
  114. typedef struct {
  115. uint64_t num;
  116. uint64_t type;
  117. char *codec_id;
  118. EbmlBin codec_priv;
  119. char *language;
  120. double time_scale;
  121. uint64_t default_duration;
  122. uint64_t flag_default;
  123. MatroskaTrackVideo video;
  124. MatroskaTrackAudio audio;
  125. EbmlList encodings;
  126. AVStream *stream;
  127. } MatroskaTrack;
  128. typedef struct {
  129. char *filename;
  130. char *mime;
  131. EbmlBin bin;
  132. } MatroskaAttachement;
  133. typedef struct {
  134. uint64_t start;
  135. uint64_t end;
  136. uint64_t uid;
  137. char *title;
  138. } MatroskaChapter;
  139. typedef struct {
  140. uint64_t track;
  141. uint64_t pos;
  142. } MatroskaIndexPos;
  143. typedef struct {
  144. uint64_t time;
  145. EbmlList pos;
  146. } MatroskaIndex;
  147. typedef struct {
  148. char *name;
  149. char *string;
  150. EbmlList sub;
  151. } MatroskaTag;
  152. typedef struct {
  153. uint64_t id;
  154. uint64_t pos;
  155. } MatroskaSeekhead;
  156. typedef struct {
  157. uint64_t start;
  158. uint64_t length;
  159. } MatroskaLevel;
  160. typedef struct {
  161. AVFormatContext *ctx;
  162. /* EBML stuff */
  163. int num_levels;
  164. MatroskaLevel levels[EBML_MAX_DEPTH];
  165. int level_up;
  166. uint64_t time_scale;
  167. double duration;
  168. char *title;
  169. EbmlList tracks;
  170. EbmlList attachments;
  171. EbmlList chapters;
  172. EbmlList index;
  173. EbmlList tags;
  174. EbmlList seekhead;
  175. /* byte position of the segment inside the stream */
  176. offset_t segment_start;
  177. /* the packet queue */
  178. AVPacket **packets;
  179. int num_packets;
  180. int done;
  181. int has_cluster_id;
  182. /* What to skip before effectively reading a packet. */
  183. int skip_to_keyframe;
  184. AVStream *skip_to_stream;
  185. } MatroskaDemuxContext;
  186. typedef struct {
  187. uint64_t duration;
  188. int64_t reference;
  189. EbmlBin bin;
  190. } MatroskaBlock;
  191. typedef struct {
  192. uint64_t timecode;
  193. EbmlList blocks;
  194. } MatroskaCluster;
  195. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
  196. static EbmlSyntax ebml_header[] = {
  197. { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
  198. { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
  199. { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
  200. { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
  201. { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
  202. { EBML_ID_EBMLVERSION, EBML_NONE },
  203. { EBML_ID_DOCTYPEVERSION, EBML_NONE },
  204. { EBML_ID_VOID, EBML_NONE },
  205. { 0 }
  206. };
  207. static EbmlSyntax ebml_syntax[] = {
  208. { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
  209. { 0 }
  210. };
  211. static EbmlSyntax matroska_info[] = {
  212. { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
  213. { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
  214. { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) },
  215. { MATROSKA_ID_WRITINGAPP, EBML_NONE },
  216. { MATROSKA_ID_MUXINGAPP, EBML_NONE },
  217. { MATROSKA_ID_DATEUTC, EBML_NONE },
  218. { MATROSKA_ID_SEGMENTUID, EBML_NONE },
  219. { EBML_ID_CRC32, EBML_NONE },
  220. { EBML_ID_VOID, EBML_NONE },
  221. { 0 }
  222. };
  223. static EbmlSyntax matroska_track_video[] = {
  224. { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
  225. { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
  226. { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
  227. { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
  228. { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
  229. { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) },
  230. { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE },
  231. { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
  232. { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
  233. { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
  234. { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE },
  235. { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE },
  236. { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE },
  237. { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
  238. { EBML_ID_VOID, EBML_NONE },
  239. { 0 }
  240. };
  241. static EbmlSyntax matroska_track_audio[] = {
  242. { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} },
  243. { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) },
  244. { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) },
  245. { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} },
  246. { EBML_ID_VOID, EBML_NONE },
  247. { 0 }
  248. };
  249. static EbmlSyntax matroska_track_encoding_compression[] = {
  250. { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} },
  251. { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) },
  252. { EBML_ID_VOID, EBML_NONE },
  253. { 0 }
  254. };
  255. static EbmlSyntax matroska_track_encoding[] = {
  256. { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
  257. { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
  258. { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
  259. { MATROSKA_ID_ENCODINGORDER, EBML_NONE },
  260. { EBML_ID_VOID, EBML_NONE },
  261. { 0 }
  262. };
  263. static EbmlSyntax matroska_track_encodings[] = {
  264. { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} },
  265. { EBML_ID_VOID, EBML_NONE },
  266. { 0 }
  267. };
  268. static EbmlSyntax matroska_track[] = {
  269. { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) },
  270. { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) },
  271. { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) },
  272. { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) },
  273. { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} },
  274. { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
  275. { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
  276. { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
  277. { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
  278. { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
  279. { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
  280. { MATROSKA_ID_TRACKUID, EBML_NONE },
  281. { MATROSKA_ID_TRACKNAME, EBML_NONE },
  282. { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE },
  283. { MATROSKA_ID_TRACKFLAGFORCED, EBML_NONE },
  284. { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
  285. { MATROSKA_ID_CODECNAME, EBML_NONE },
  286. { MATROSKA_ID_CODECDECODEALL, EBML_NONE },
  287. { MATROSKA_ID_CODECINFOURL, EBML_NONE },
  288. { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
  289. { MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
  290. { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
  291. { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE },
  292. { EBML_ID_CRC32, EBML_NONE },
  293. { EBML_ID_VOID, EBML_NONE },
  294. { 0 }
  295. };
  296. static EbmlSyntax matroska_tracks[] = {
  297. { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} },
  298. { EBML_ID_CRC32, EBML_NONE },
  299. { EBML_ID_VOID, EBML_NONE },
  300. { 0 }
  301. };
  302. static EbmlSyntax matroska_attachment[] = {
  303. { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
  304. { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) },
  305. { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) },
  306. { MATROSKA_ID_FILEDESC, EBML_NONE },
  307. { MATROSKA_ID_FILEUID, EBML_NONE },
  308. { EBML_ID_VOID, EBML_NONE },
  309. { 0 }
  310. };
  311. static EbmlSyntax matroska_attachments[] = {
  312. { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
  313. { EBML_ID_CRC32, EBML_NONE },
  314. { EBML_ID_VOID, EBML_NONE },
  315. { 0 }
  316. };
  317. static EbmlSyntax matroska_chapter_display[] = {
  318. { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
  319. { MATROSKA_ID_CHAPLANG, EBML_NONE },
  320. { EBML_ID_VOID, EBML_NONE },
  321. { 0 }
  322. };
  323. static EbmlSyntax matroska_chapter_entry[] = {
  324. { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
  325. { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
  326. { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
  327. { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
  328. { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
  329. { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
  330. { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
  331. { MATROSKA_ID_CHAPTERATOM, EBML_NONE },
  332. { EBML_ID_CRC32, EBML_NONE },
  333. { EBML_ID_VOID, EBML_NONE },
  334. { 0 }
  335. };
  336. static EbmlSyntax matroska_chapter[] = {
  337. { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
  338. { MATROSKA_ID_EDITIONUID, EBML_NONE },
  339. { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
  340. { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
  341. { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
  342. { EBML_ID_CRC32, EBML_NONE },
  343. { EBML_ID_VOID, EBML_NONE },
  344. { 0 }
  345. };
  346. static EbmlSyntax matroska_chapters[] = {
  347. { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} },
  348. { EBML_ID_CRC32, EBML_NONE },
  349. { EBML_ID_VOID, EBML_NONE },
  350. { 0 }
  351. };
  352. static EbmlSyntax matroska_index_pos[] = {
  353. { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
  354. { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) },
  355. { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE },
  356. { EBML_ID_VOID, EBML_NONE },
  357. { 0 }
  358. };
  359. static EbmlSyntax matroska_index_entry[] = {
  360. { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) },
  361. { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
  362. { EBML_ID_VOID, EBML_NONE },
  363. { 0 }
  364. };
  365. static EbmlSyntax matroska_index[] = {
  366. { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
  367. { EBML_ID_CRC32, EBML_NONE },
  368. { EBML_ID_VOID, EBML_NONE },
  369. { 0 }
  370. };
  371. static EbmlSyntax matroska_simpletag[] = {
  372. { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) },
  373. { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) },
  374. { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
  375. { MATROSKA_ID_TAGLANG, EBML_NONE },
  376. { MATROSKA_ID_TAGDEFAULT, EBML_NONE },
  377. { EBML_ID_CRC32, EBML_NONE },
  378. { EBML_ID_VOID, EBML_NONE },
  379. { 0 }
  380. };
  381. static EbmlSyntax matroska_tag[] = {
  382. { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), 0, {.n=matroska_simpletag} },
  383. { MATROSKA_ID_TAGTARGETS, EBML_NONE },
  384. { EBML_ID_CRC32, EBML_NONE },
  385. { EBML_ID_VOID, EBML_NONE },
  386. { 0 }
  387. };
  388. static EbmlSyntax matroska_tags[] = {
  389. { MATROSKA_ID_TAG, EBML_NEST, 0, offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} },
  390. { EBML_ID_CRC32, EBML_NONE },
  391. { EBML_ID_VOID, EBML_NONE },
  392. { 0 }
  393. };
  394. static EbmlSyntax matroska_seekhead_entry[] = {
  395. { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) },
  396. { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} },
  397. { EBML_ID_VOID, EBML_NONE },
  398. { 0 }
  399. };
  400. static EbmlSyntax matroska_seekhead[] = {
  401. { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} },
  402. { EBML_ID_CRC32, EBML_NONE },
  403. { EBML_ID_VOID, EBML_NONE },
  404. { 0 }
  405. };
  406. static EbmlSyntax matroska_segment[] = {
  407. { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } },
  408. { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } },
  409. { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} },
  410. { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } },
  411. { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } },
  412. { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } },
  413. { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } },
  414. { MATROSKA_ID_CLUSTER, EBML_STOP, 0, offsetof(MatroskaDemuxContext,has_cluster_id) },
  415. { EBML_ID_VOID, EBML_NONE },
  416. { 0 }
  417. };
  418. static EbmlSyntax matroska_segments[] = {
  419. { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } },
  420. { 0 }
  421. };
  422. static EbmlSyntax matroska_blockgroup[] = {
  423. { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
  424. { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
  425. { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
  426. { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
  427. { EBML_ID_VOID, EBML_NONE },
  428. { 0 }
  429. };
  430. static EbmlSyntax matroska_cluster[] = {
  431. { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
  432. { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  433. { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  434. { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
  435. { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
  436. { EBML_ID_CRC32, EBML_NONE },
  437. { EBML_ID_VOID, EBML_NONE },
  438. { 0 }
  439. };
  440. static EbmlSyntax matroska_clusters[] = {
  441. { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} },
  442. { MATROSKA_ID_INFO, EBML_NONE },
  443. { MATROSKA_ID_CUES, EBML_NONE },
  444. { MATROSKA_ID_TAGS, EBML_NONE },
  445. { MATROSKA_ID_SEEKHEAD, EBML_NONE },
  446. { 0 }
  447. };
  448. #define SIZE_OFF(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x)
  449. const struct {
  450. const char name[16];
  451. int size;
  452. int offset;
  453. } metadata[] = {
  454. { "TITLE", SIZE_OFF(title) },
  455. { "ARTIST", SIZE_OFF(author) },
  456. { "WRITTEN_BY", SIZE_OFF(author) },
  457. { "LEAD_PERFORMER", SIZE_OFF(author) },
  458. { "COPYRIGHT", SIZE_OFF(copyright) },
  459. { "COMMENT", SIZE_OFF(comment) },
  460. { "ALBUM", SIZE_OFF(album) },
  461. { "DATE_WRITTEN", SIZE_OFF(year) },
  462. { "DATE_RELEASED", SIZE_OFF(year) },
  463. { "PART_NUMBER", SIZE_OFF(track) },
  464. { "GENRE", SIZE_OFF(genre) },
  465. };
  466. /*
  467. * Return: Whether we reached the end of a level in the hierarchy or not.
  468. */
  469. static int ebml_level_end(MatroskaDemuxContext *matroska)
  470. {
  471. ByteIOContext *pb = matroska->ctx->pb;
  472. offset_t pos = url_ftell(pb);
  473. if (matroska->num_levels > 0) {
  474. MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
  475. if (pos - level->start >= level->length) {
  476. matroska->num_levels--;
  477. return 1;
  478. }
  479. }
  480. return 0;
  481. }
  482. /*
  483. * Read: an "EBML number", which is defined as a variable-length
  484. * array of bytes. The first byte indicates the length by giving a
  485. * number of 0-bits followed by a one. The position of the first
  486. * "one" bit inside the first byte indicates the length of this
  487. * number.
  488. * Returns: number of bytes read, < 0 on error
  489. */
  490. static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb,
  491. int max_size, uint64_t *number)
  492. {
  493. int len_mask = 0x80, read = 1, n = 1;
  494. int64_t total = 0;
  495. /* The first byte tells us the length in bytes - get_byte() can normally
  496. * return 0, but since that's not a valid first ebmlID byte, we can
  497. * use it safely here to catch EOS. */
  498. if (!(total = get_byte(pb))) {
  499. /* we might encounter EOS here */
  500. if (!url_feof(pb)) {
  501. offset_t pos = url_ftell(pb);
  502. av_log(matroska->ctx, AV_LOG_ERROR,
  503. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
  504. pos, pos);
  505. }
  506. return AVERROR(EIO); /* EOS or actual I/O error */
  507. }
  508. /* get the length of the EBML number */
  509. while (read <= max_size && !(total & len_mask)) {
  510. read++;
  511. len_mask >>= 1;
  512. }
  513. if (read > max_size) {
  514. offset_t pos = url_ftell(pb) - 1;
  515. av_log(matroska->ctx, AV_LOG_ERROR,
  516. "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
  517. (uint8_t) total, pos, pos);
  518. return AVERROR_INVALIDDATA;
  519. }
  520. /* read out length */
  521. total &= ~len_mask;
  522. while (n++ < read)
  523. total = (total << 8) | get_byte(pb);
  524. *number = total;
  525. return read;
  526. }
  527. /*
  528. * Read the next element as an unsigned int.
  529. * 0 is success, < 0 is failure.
  530. */
  531. static int ebml_read_uint(ByteIOContext *pb, int size, uint64_t *num)
  532. {
  533. int n = 0;
  534. if (size < 1 || size > 8)
  535. return AVERROR_INVALIDDATA;
  536. /* big-endian ordering; build up number */
  537. *num = 0;
  538. while (n++ < size)
  539. *num = (*num << 8) | get_byte(pb);
  540. return 0;
  541. }
  542. /*
  543. * Read the next element as a float.
  544. * 0 is success, < 0 is failure.
  545. */
  546. static int ebml_read_float(ByteIOContext *pb, int size, double *num)
  547. {
  548. if (size == 4) {
  549. *num= av_int2flt(get_be32(pb));
  550. } else if(size==8){
  551. *num= av_int2dbl(get_be64(pb));
  552. } else
  553. return AVERROR_INVALIDDATA;
  554. return 0;
  555. }
  556. /*
  557. * Read the next element as an ASCII string.
  558. * 0 is success, < 0 is failure.
  559. */
  560. static int ebml_read_ascii(ByteIOContext *pb, int size, char **str)
  561. {
  562. av_free(*str);
  563. /* EBML strings are usually not 0-terminated, so we allocate one
  564. * byte more, read the string and NULL-terminate it ourselves. */
  565. if (!(*str = av_malloc(size + 1)))
  566. return AVERROR(ENOMEM);
  567. if (get_buffer(pb, (uint8_t *) *str, size) != size) {
  568. av_free(*str);
  569. return AVERROR(EIO);
  570. }
  571. (*str)[size] = '\0';
  572. return 0;
  573. }
  574. /*
  575. * Read the next element as binary data.
  576. * 0 is success, < 0 is failure.
  577. */
  578. static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin)
  579. {
  580. av_free(bin->data);
  581. if (!(bin->data = av_malloc(length)))
  582. return AVERROR(ENOMEM);
  583. bin->size = length;
  584. bin->pos = url_ftell(pb);
  585. if (get_buffer(pb, bin->data, length) != length)
  586. return AVERROR(EIO);
  587. return 0;
  588. }
  589. /*
  590. * Read the next element, but only the header. The contents
  591. * are supposed to be sub-elements which can be read separately.
  592. * 0 is success, < 0 is failure.
  593. */
  594. static int ebml_read_master(MatroskaDemuxContext *matroska, int length)
  595. {
  596. ByteIOContext *pb = matroska->ctx->pb;
  597. MatroskaLevel *level;
  598. if (matroska->num_levels >= EBML_MAX_DEPTH) {
  599. av_log(matroska->ctx, AV_LOG_ERROR,
  600. "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
  601. return AVERROR(ENOSYS);
  602. }
  603. level = &matroska->levels[matroska->num_levels++];
  604. level->start = url_ftell(pb);
  605. level->length = length;
  606. return 0;
  607. }
  608. /*
  609. * Read signed/unsigned "EBML" numbers.
  610. * Return: number of bytes processed, < 0 on error
  611. */
  612. static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
  613. uint8_t *data, uint32_t size, uint64_t *num)
  614. {
  615. ByteIOContext pb;
  616. init_put_byte(&pb, data, size, 0, NULL, NULL, NULL, NULL);
  617. return ebml_read_num(matroska, &pb, 8, num);
  618. }
  619. /*
  620. * Same as above, but signed.
  621. */
  622. static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
  623. uint8_t *data, uint32_t size, int64_t *num)
  624. {
  625. uint64_t unum;
  626. int res;
  627. /* read as unsigned number first */
  628. if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
  629. return res;
  630. /* make signed (weird way) */
  631. *num = unum - ((1LL << (7*res - 1)) - 1);
  632. return res;
  633. }
  634. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  635. EbmlSyntax *syntax, void *data);
  636. static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  637. uint32_t id, void *data)
  638. {
  639. int i;
  640. for (i=0; syntax[i].id; i++)
  641. if (id == syntax[i].id)
  642. break;
  643. if (!syntax[i].id)
  644. av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
  645. return ebml_parse_elem(matroska, &syntax[i], data);
  646. }
  647. static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  648. void *data)
  649. {
  650. uint64_t id;
  651. int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
  652. id |= 1 << 7*res;
  653. return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data);
  654. }
  655. static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  656. void *data)
  657. {
  658. int i, res = 0;
  659. for (i=0; syntax[i].id; i++)
  660. switch (syntax[i].type) {
  661. case EBML_UINT:
  662. *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
  663. break;
  664. case EBML_FLOAT:
  665. *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
  666. break;
  667. case EBML_STR:
  668. case EBML_UTF8:
  669. *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
  670. break;
  671. }
  672. while (!res && !ebml_level_end(matroska))
  673. res = ebml_parse(matroska, syntax, data);
  674. return res;
  675. }
  676. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  677. EbmlSyntax *syntax, void *data)
  678. {
  679. ByteIOContext *pb = matroska->ctx->pb;
  680. uint32_t id = syntax->id;
  681. uint64_t length;
  682. int res;
  683. data = (char *)data + syntax->data_offset;
  684. if (syntax->list_elem_size) {
  685. EbmlList *list = data;
  686. list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
  687. data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
  688. memset(data, 0, syntax->list_elem_size);
  689. list->nb_elem++;
  690. }
  691. if (syntax->type != EBML_PASS && syntax->type != EBML_STOP)
  692. if ((res = ebml_read_num(matroska, pb, 8, &length)) < 0)
  693. return res;
  694. switch (syntax->type) {
  695. case EBML_UINT: res = ebml_read_uint (pb, length, data); break;
  696. case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;
  697. case EBML_STR:
  698. case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;
  699. case EBML_BIN: res = ebml_read_binary(pb, length, data); break;
  700. case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)
  701. return res;
  702. if (id == MATROSKA_ID_SEGMENT)
  703. matroska->segment_start = url_ftell(matroska->ctx->pb);
  704. return ebml_parse_nest(matroska, syntax->def.n, data);
  705. case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);
  706. case EBML_STOP: *(int *)data = 1; return 1;
  707. default: url_fskip(pb, length); return 0;
  708. }
  709. if (res == AVERROR_INVALIDDATA)
  710. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
  711. else if (res == AVERROR(EIO))
  712. av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
  713. return res;
  714. }
  715. static void ebml_free(EbmlSyntax *syntax, void *data)
  716. {
  717. int i, j;
  718. for (i=0; syntax[i].id; i++) {
  719. void *data_off = (char *)data + syntax[i].data_offset;
  720. switch (syntax[i].type) {
  721. case EBML_STR:
  722. case EBML_UTF8: av_freep(data_off); break;
  723. case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
  724. case EBML_NEST:
  725. if (syntax[i].list_elem_size) {
  726. EbmlList *list = data_off;
  727. char *ptr = list->elem;
  728. for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
  729. ebml_free(syntax[i].def.n, ptr);
  730. av_free(list->elem);
  731. } else
  732. ebml_free(syntax[i].def.n, data_off);
  733. default: break;
  734. }
  735. }
  736. }
  737. /*
  738. * Autodetecting...
  739. */
  740. static int matroska_probe(AVProbeData *p)
  741. {
  742. uint64_t total = 0;
  743. int len_mask = 0x80, size = 1, n = 1;
  744. char probe_data[] = "matroska";
  745. /* EBML header? */
  746. if (AV_RB32(p->buf) != EBML_ID_HEADER)
  747. return 0;
  748. /* length of header */
  749. total = p->buf[4];
  750. while (size <= 8 && !(total & len_mask)) {
  751. size++;
  752. len_mask >>= 1;
  753. }
  754. if (size > 8)
  755. return 0;
  756. total &= (len_mask - 1);
  757. while (n < size)
  758. total = (total << 8) | p->buf[4 + n++];
  759. /* Does the probe data contain the whole header? */
  760. if (p->buf_size < 4 + size + total)
  761. return 0;
  762. /* The header must contain the document type 'matroska'. For now,
  763. * we don't parse the whole header but simply check for the
  764. * availability of that array of characters inside the header.
  765. * Not fully fool-proof, but good enough. */
  766. for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++)
  767. if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1))
  768. return AVPROBE_SCORE_MAX;
  769. return 0;
  770. }
  771. static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
  772. int num)
  773. {
  774. MatroskaTrack *tracks = matroska->tracks.elem;
  775. int i;
  776. for (i=0; i < matroska->tracks.nb_elem; i++)
  777. if (tracks[i].num == num)
  778. return &tracks[i];
  779. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
  780. return NULL;
  781. }
  782. static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
  783. MatroskaTrack *track)
  784. {
  785. MatroskaTrackEncoding *encodings = track->encodings.elem;
  786. uint8_t* data = *buf;
  787. int isize = *buf_size;
  788. uint8_t* pkt_data = NULL;
  789. int pkt_size = isize;
  790. int result = 0;
  791. int olen;
  792. switch (encodings[0].compression.algo) {
  793. case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
  794. return encodings[0].compression.settings.size;
  795. case MATROSKA_TRACK_ENCODING_COMP_LZO:
  796. do {
  797. olen = pkt_size *= 3;
  798. pkt_data = av_realloc(pkt_data,
  799. pkt_size+LZO_OUTPUT_PADDING);
  800. result = lzo1x_decode(pkt_data, &olen, data, &isize);
  801. } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
  802. if (result)
  803. goto failed;
  804. pkt_size -= olen;
  805. break;
  806. #ifdef CONFIG_ZLIB
  807. case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
  808. z_stream zstream = {0};
  809. if (inflateInit(&zstream) != Z_OK)
  810. return -1;
  811. zstream.next_in = data;
  812. zstream.avail_in = isize;
  813. do {
  814. pkt_size *= 3;
  815. pkt_data = av_realloc(pkt_data, pkt_size);
  816. zstream.avail_out = pkt_size - zstream.total_out;
  817. zstream.next_out = pkt_data + zstream.total_out;
  818. result = inflate(&zstream, Z_NO_FLUSH);
  819. } while (result==Z_OK && pkt_size<10000000);
  820. pkt_size = zstream.total_out;
  821. inflateEnd(&zstream);
  822. if (result != Z_STREAM_END)
  823. goto failed;
  824. break;
  825. }
  826. #endif
  827. #ifdef CONFIG_BZLIB
  828. case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
  829. bz_stream bzstream = {0};
  830. if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
  831. return -1;
  832. bzstream.next_in = data;
  833. bzstream.avail_in = isize;
  834. do {
  835. pkt_size *= 3;
  836. pkt_data = av_realloc(pkt_data, pkt_size);
  837. bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
  838. bzstream.next_out = pkt_data + bzstream.total_out_lo32;
  839. result = BZ2_bzDecompress(&bzstream);
  840. } while (result==BZ_OK && pkt_size<10000000);
  841. pkt_size = bzstream.total_out_lo32;
  842. BZ2_bzDecompressEnd(&bzstream);
  843. if (result != BZ_STREAM_END)
  844. goto failed;
  845. break;
  846. }
  847. #endif
  848. }
  849. *buf = pkt_data;
  850. *buf_size = pkt_size;
  851. return 0;
  852. failed:
  853. av_free(pkt_data);
  854. return -1;
  855. }
  856. static void matroska_convert_tags(AVFormatContext *s, EbmlList *list)
  857. {
  858. MatroskaTag *tags = list->elem;
  859. int i, j;
  860. for (i=0; i < list->nb_elem; i++) {
  861. for (j=0; j < ARRAY_SIZE(metadata); j++){
  862. if (!strcmp(tags[i].name, metadata[j].name)) {
  863. int *ptr = (int *)((char *)s + metadata[j].offset);
  864. if (*ptr) continue;
  865. if (metadata[j].size > sizeof(int))
  866. av_strlcpy((char *)ptr, tags[i].string, metadata[j].size);
  867. else
  868. *ptr = atoi(tags[i].string);
  869. }
  870. }
  871. if (tags[i].sub.nb_elem)
  872. matroska_convert_tags(s, &tags[i].sub);
  873. }
  874. }
  875. static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
  876. {
  877. EbmlList *seekhead_list = &matroska->seekhead;
  878. MatroskaSeekhead *seekhead = seekhead_list->elem;
  879. uint32_t level_up = matroska->level_up;
  880. offset_t before_pos = url_ftell(matroska->ctx->pb);
  881. MatroskaLevel level;
  882. int i;
  883. for (i=0; i<seekhead_list->nb_elem; i++) {
  884. offset_t offset = seekhead[i].pos + matroska->segment_start;
  885. if (seekhead[i].pos <= before_pos
  886. || seekhead[i].id == MATROSKA_ID_SEEKHEAD
  887. || seekhead[i].id == MATROSKA_ID_CLUSTER)
  888. continue;
  889. /* seek */
  890. if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset)
  891. continue;
  892. /* We don't want to lose our seekhead level, so we add
  893. * a dummy. This is a crude hack. */
  894. if (matroska->num_levels == EBML_MAX_DEPTH) {
  895. av_log(matroska->ctx, AV_LOG_INFO,
  896. "Max EBML element depth (%d) reached, "
  897. "cannot parse further.\n", EBML_MAX_DEPTH);
  898. break;
  899. }
  900. level.start = 0;
  901. level.length = (uint64_t)-1;
  902. matroska->levels[matroska->num_levels] = level;
  903. matroska->num_levels++;
  904. ebml_parse(matroska, matroska_segment, matroska);
  905. /* remove dummy level */
  906. while (matroska->num_levels) {
  907. uint64_t length = matroska->levels[--matroska->num_levels].length;
  908. if (length == (uint64_t)-1)
  909. break;
  910. }
  911. }
  912. /* seek back */
  913. url_fseek(matroska->ctx->pb, before_pos, SEEK_SET);
  914. matroska->level_up = level_up;
  915. }
  916. static int matroska_aac_profile(char *codec_id)
  917. {
  918. static const char *aac_profiles[] = { "MAIN", "LC", "SSR" };
  919. int profile;
  920. for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
  921. if (strstr(codec_id, aac_profiles[profile]))
  922. break;
  923. return profile + 1;
  924. }
  925. static int matroska_aac_sri(int samplerate)
  926. {
  927. int sri;
  928. for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
  929. if (ff_mpeg4audio_sample_rates[sri] == samplerate)
  930. break;
  931. return sri;
  932. }
  933. static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap)
  934. {
  935. MatroskaDemuxContext *matroska = s->priv_data;
  936. EbmlList *attachements_list = &matroska->attachments;
  937. MatroskaAttachement *attachements;
  938. EbmlList *chapters_list = &matroska->chapters;
  939. MatroskaChapter *chapters;
  940. MatroskaTrack *tracks;
  941. EbmlList *index_list;
  942. MatroskaIndex *index;
  943. Ebml ebml = { 0 };
  944. AVStream *st;
  945. int i, j;
  946. matroska->ctx = s;
  947. /* First read the EBML header. */
  948. if (ebml_parse(matroska, ebml_syntax, &ebml)
  949. || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
  950. || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
  951. || ebml.doctype_version > 2) {
  952. av_log(matroska->ctx, AV_LOG_ERROR,
  953. "EBML header using unsupported features\n"
  954. "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
  955. ebml.version, ebml.doctype, ebml.doctype_version);
  956. return AVERROR_NOFMT;
  957. }
  958. ebml_free(ebml_syntax, &ebml);
  959. /* The next thing is a segment. */
  960. if (ebml_parse(matroska, matroska_segments, matroska) < 0)
  961. return -1;
  962. matroska_execute_seekhead(matroska);
  963. if (matroska->duration)
  964. matroska->ctx->duration = matroska->duration * matroska->time_scale
  965. * 1000 / AV_TIME_BASE;
  966. if (matroska->title)
  967. strncpy(matroska->ctx->title, matroska->title,
  968. sizeof(matroska->ctx->title)-1);
  969. matroska_convert_tags(s, &matroska->tags);
  970. tracks = matroska->tracks.elem;
  971. for (i=0; i < matroska->tracks.nb_elem; i++) {
  972. MatroskaTrack *track = &tracks[i];
  973. enum CodecID codec_id = CODEC_ID_NONE;
  974. EbmlList *encodings_list = &tracks->encodings;
  975. MatroskaTrackEncoding *encodings = encodings_list->elem;
  976. uint8_t *extradata = NULL;
  977. int extradata_size = 0;
  978. int extradata_offset = 0;
  979. /* Apply some sanity checks. */
  980. if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
  981. track->type != MATROSKA_TRACK_TYPE_AUDIO &&
  982. track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  983. av_log(matroska->ctx, AV_LOG_INFO,
  984. "Unknown or unsupported track type %"PRIu64"\n",
  985. track->type);
  986. continue;
  987. }
  988. if (track->codec_id == NULL)
  989. continue;
  990. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  991. if (!track->default_duration)
  992. track->default_duration = 1000000000/track->video.frame_rate;
  993. if (!track->video.display_width)
  994. track->video.display_width = track->video.pixel_width;
  995. if (!track->video.display_height)
  996. track->video.display_height = track->video.pixel_height;
  997. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  998. if (!track->audio.out_samplerate)
  999. track->audio.out_samplerate = track->audio.samplerate;
  1000. }
  1001. if (encodings_list->nb_elem > 1) {
  1002. av_log(matroska->ctx, AV_LOG_ERROR,
  1003. "Multiple combined encodings no supported");
  1004. } else if (encodings_list->nb_elem == 1) {
  1005. if (encodings[0].type ||
  1006. (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
  1007. #ifdef CONFIG_ZLIB
  1008. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1009. #endif
  1010. #ifdef CONFIG_BZLIB
  1011. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1012. #endif
  1013. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
  1014. encodings[0].scope = 0;
  1015. av_log(matroska->ctx, AV_LOG_ERROR,
  1016. "Unsupported encoding type");
  1017. } else if (track->codec_priv.size && encodings[0].scope&2) {
  1018. uint8_t *codec_priv = track->codec_priv.data;
  1019. int offset = matroska_decode_buffer(&track->codec_priv.data,
  1020. &track->codec_priv.size,
  1021. track);
  1022. if (offset < 0) {
  1023. track->codec_priv.data = NULL;
  1024. track->codec_priv.size = 0;
  1025. av_log(matroska->ctx, AV_LOG_ERROR,
  1026. "Failed to decode codec private data\n");
  1027. } else if (offset > 0) {
  1028. track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
  1029. memcpy(track->codec_priv.data,
  1030. encodings[0].compression.settings.data, offset);
  1031. memcpy(track->codec_priv.data+offset, codec_priv,
  1032. track->codec_priv.size);
  1033. track->codec_priv.size += offset;
  1034. }
  1035. if (codec_priv != track->codec_priv.data)
  1036. av_free(codec_priv);
  1037. }
  1038. }
  1039. for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
  1040. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  1041. strlen(ff_mkv_codec_tags[j].str))){
  1042. codec_id= ff_mkv_codec_tags[j].id;
  1043. break;
  1044. }
  1045. }
  1046. st = track->stream = av_new_stream(s, 0);
  1047. if (st == NULL)
  1048. return AVERROR(ENOMEM);
  1049. if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
  1050. && track->codec_priv.size >= 40
  1051. && track->codec_priv.data != NULL) {
  1052. track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
  1053. codec_id = codec_get_id(codec_bmp_tags, track->video.fourcc);
  1054. } else if (!strcmp(track->codec_id, "A_MS/ACM")
  1055. && track->codec_priv.size >= 18
  1056. && track->codec_priv.data != NULL) {
  1057. uint16_t tag = AV_RL16(track->codec_priv.data);
  1058. codec_id = codec_get_id(codec_wav_tags, tag);
  1059. } else if (!strcmp(track->codec_id, "V_QUICKTIME")
  1060. && (track->codec_priv.size >= 86)
  1061. && (track->codec_priv.data != NULL)) {
  1062. track->video.fourcc = AV_RL32(track->codec_priv.data);
  1063. codec_id=codec_get_id(codec_movvideo_tags, track->video.fourcc);
  1064. } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
  1065. int profile = matroska_aac_profile(track->codec_id);
  1066. int sri = matroska_aac_sri(track->audio.samplerate);
  1067. extradata = av_malloc(5);
  1068. if (extradata == NULL)
  1069. return AVERROR(ENOMEM);
  1070. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  1071. extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
  1072. if (strstr(track->codec_id, "SBR")) {
  1073. sri = matroska_aac_sri(track->audio.out_samplerate);
  1074. extradata[2] = 0x56;
  1075. extradata[3] = 0xE5;
  1076. extradata[4] = 0x80 | (sri<<3);
  1077. extradata_size = 5;
  1078. } else
  1079. extradata_size = 2;
  1080. } else if (codec_id == CODEC_ID_TTA) {
  1081. ByteIOContext b;
  1082. extradata_size = 30;
  1083. extradata = av_mallocz(extradata_size);
  1084. if (extradata == NULL)
  1085. return AVERROR(ENOMEM);
  1086. init_put_byte(&b, extradata, extradata_size, 1,
  1087. NULL, NULL, NULL, NULL);
  1088. put_buffer(&b, "TTA1", 4);
  1089. put_le16(&b, 1);
  1090. put_le16(&b, track->audio.channels);
  1091. put_le16(&b, track->audio.bitdepth);
  1092. put_le32(&b, track->audio.out_samplerate);
  1093. put_le32(&b, matroska->ctx->duration * track->audio.out_samplerate);
  1094. } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
  1095. codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
  1096. extradata_offset = 26;
  1097. track->codec_priv.size -= extradata_offset;
  1098. } else if (codec_id == CODEC_ID_RA_144) {
  1099. track->audio.out_samplerate = 8000;
  1100. track->audio.channels = 1;
  1101. } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
  1102. codec_id == CODEC_ID_ATRAC3) {
  1103. ByteIOContext b;
  1104. init_put_byte(&b, track->codec_priv.data,track->codec_priv.size,
  1105. 0, NULL, NULL, NULL, NULL);
  1106. url_fskip(&b, 24);
  1107. track->audio.coded_framesize = get_be32(&b);
  1108. url_fskip(&b, 12);
  1109. track->audio.sub_packet_h = get_be16(&b);
  1110. track->audio.frame_size = get_be16(&b);
  1111. track->audio.sub_packet_size = get_be16(&b);
  1112. track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
  1113. if (codec_id == CODEC_ID_RA_288) {
  1114. st->codec->block_align = track->audio.coded_framesize;
  1115. track->codec_priv.size = 0;
  1116. } else {
  1117. st->codec->block_align = track->audio.sub_packet_size;
  1118. extradata_offset = 78;
  1119. track->codec_priv.size -= extradata_offset;
  1120. }
  1121. }
  1122. if (codec_id == CODEC_ID_NONE)
  1123. av_log(matroska->ctx, AV_LOG_INFO,
  1124. "Unknown/unsupported CodecID %s.\n", track->codec_id);
  1125. av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  1126. st->codec->codec_id = codec_id;
  1127. st->start_time = 0;
  1128. if (strcmp(track->language, "und"))
  1129. av_strlcpy(st->language, track->language, 4);
  1130. if (track->flag_default)
  1131. st->disposition |= AV_DISPOSITION_DEFAULT;
  1132. if (track->default_duration)
  1133. av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
  1134. track->default_duration, 1000000000, 30000);
  1135. if(extradata){
  1136. st->codec->extradata = extradata;
  1137. st->codec->extradata_size = extradata_size;
  1138. } else if(track->codec_priv.data && track->codec_priv.size > 0){
  1139. st->codec->extradata = av_malloc(track->codec_priv.size);
  1140. if(st->codec->extradata == NULL)
  1141. return AVERROR(ENOMEM);
  1142. st->codec->extradata_size = track->codec_priv.size;
  1143. memcpy(st->codec->extradata,
  1144. track->codec_priv.data + extradata_offset,
  1145. track->codec_priv.size);
  1146. }
  1147. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1148. st->codec->codec_type = CODEC_TYPE_VIDEO;
  1149. st->codec->codec_tag = track->video.fourcc;
  1150. st->codec->width = track->video.pixel_width;
  1151. st->codec->height = track->video.pixel_height;
  1152. av_reduce(&st->codec->sample_aspect_ratio.num,
  1153. &st->codec->sample_aspect_ratio.den,
  1154. st->codec->height * track->video.display_width,
  1155. st->codec-> width * track->video.display_height,
  1156. 255);
  1157. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1158. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1159. st->codec->codec_type = CODEC_TYPE_AUDIO;
  1160. st->codec->sample_rate = track->audio.out_samplerate;
  1161. st->codec->channels = track->audio.channels;
  1162. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  1163. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  1164. }
  1165. }
  1166. attachements = attachements_list->elem;
  1167. for (j=0; j<attachements_list->nb_elem; j++) {
  1168. if (!(attachements[j].filename && attachements[j].mime &&
  1169. attachements[j].bin.data && attachements[j].bin.size > 0)) {
  1170. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1171. } else {
  1172. AVStream *st = av_new_stream(s, 0);
  1173. if (st == NULL)
  1174. break;
  1175. st->filename = av_strdup(attachements[j].filename);
  1176. st->codec->codec_id = CODEC_ID_NONE;
  1177. st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
  1178. st->codec->extradata = av_malloc(attachements[j].bin.size);
  1179. if(st->codec->extradata == NULL)
  1180. break;
  1181. st->codec->extradata_size = attachements[j].bin.size;
  1182. memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
  1183. for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
  1184. if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
  1185. strlen(ff_mkv_mime_tags[i].str))) {
  1186. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1187. break;
  1188. }
  1189. }
  1190. }
  1191. }
  1192. chapters = chapters_list->elem;
  1193. for (i=0; i<chapters_list->nb_elem; i++)
  1194. if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid)
  1195. ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
  1196. chapters[i].start, chapters[i].end,
  1197. chapters[i].title);
  1198. index_list = &matroska->index;
  1199. index = index_list->elem;
  1200. for (i=0; i<index_list->nb_elem; i++) {
  1201. EbmlList *pos_list = &index[i].pos;
  1202. MatroskaIndexPos *pos = pos_list->elem;
  1203. for (j=0; j<pos_list->nb_elem; j++) {
  1204. MatroskaTrack *track = matroska_find_track_by_num(matroska,
  1205. pos[j].track);
  1206. if (track && track->stream)
  1207. av_add_index_entry(track->stream,
  1208. pos[j].pos + matroska->segment_start,
  1209. index[i].time*matroska->time_scale/AV_TIME_BASE,
  1210. 0, 0, AVINDEX_KEYFRAME);
  1211. }
  1212. }
  1213. return 0;
  1214. }
  1215. /*
  1216. * Put one packet in an application-supplied AVPacket struct.
  1217. * Returns 0 on success or -1 on failure.
  1218. */
  1219. static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
  1220. AVPacket *pkt)
  1221. {
  1222. if (matroska->num_packets > 0) {
  1223. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  1224. av_free(matroska->packets[0]);
  1225. if (matroska->num_packets > 1) {
  1226. memmove(&matroska->packets[0], &matroska->packets[1],
  1227. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1228. matroska->packets =
  1229. av_realloc(matroska->packets, (matroska->num_packets - 1) *
  1230. sizeof(AVPacket *));
  1231. } else {
  1232. av_freep(&matroska->packets);
  1233. }
  1234. matroska->num_packets--;
  1235. return 0;
  1236. }
  1237. return -1;
  1238. }
  1239. /*
  1240. * Free all packets in our internal queue.
  1241. */
  1242. static void matroska_clear_queue(MatroskaDemuxContext *matroska)
  1243. {
  1244. if (matroska->packets) {
  1245. int n;
  1246. for (n = 0; n < matroska->num_packets; n++) {
  1247. av_free_packet(matroska->packets[n]);
  1248. av_free(matroska->packets[n]);
  1249. }
  1250. av_freep(&matroska->packets);
  1251. matroska->num_packets = 0;
  1252. }
  1253. }
  1254. static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
  1255. int size, int64_t pos, uint64_t cluster_time,
  1256. uint64_t duration, int is_keyframe)
  1257. {
  1258. MatroskaTrack *track;
  1259. int res = 0;
  1260. AVStream *st;
  1261. AVPacket *pkt;
  1262. int16_t block_time;
  1263. uint32_t *lace_size = NULL;
  1264. int n, flags, laces = 0;
  1265. uint64_t num;
  1266. if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
  1267. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  1268. return res;
  1269. }
  1270. data += n;
  1271. size -= n;
  1272. track = matroska_find_track_by_num(matroska, num);
  1273. if (size <= 3 || !track || !track->stream) {
  1274. av_log(matroska->ctx, AV_LOG_INFO,
  1275. "Invalid stream %"PRIu64" or size %u\n", num, size);
  1276. return res;
  1277. }
  1278. st = track->stream;
  1279. if (st->discard >= AVDISCARD_ALL)
  1280. return res;
  1281. if (duration == AV_NOPTS_VALUE)
  1282. duration = track->default_duration / matroska->time_scale;
  1283. block_time = AV_RB16(data);
  1284. data += 2;
  1285. flags = *data++;
  1286. size -= 3;
  1287. if (is_keyframe == -1)
  1288. is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
  1289. if (matroska->skip_to_keyframe) {
  1290. if (!is_keyframe || st != matroska->skip_to_stream)
  1291. return res;
  1292. matroska->skip_to_keyframe = 0;
  1293. }
  1294. switch ((flags & 0x06) >> 1) {
  1295. case 0x0: /* no lacing */
  1296. laces = 1;
  1297. lace_size = av_mallocz(sizeof(int));
  1298. lace_size[0] = size;
  1299. break;
  1300. case 0x1: /* Xiph lacing */
  1301. case 0x2: /* fixed-size lacing */
  1302. case 0x3: /* EBML lacing */
  1303. assert(size>0); // size <=3 is checked before size-=3 above
  1304. laces = (*data) + 1;
  1305. data += 1;
  1306. size -= 1;
  1307. lace_size = av_mallocz(laces * sizeof(int));
  1308. switch ((flags & 0x06) >> 1) {
  1309. case 0x1: /* Xiph lacing */ {
  1310. uint8_t temp;
  1311. uint32_t total = 0;
  1312. for (n = 0; res == 0 && n < laces - 1; n++) {
  1313. while (1) {
  1314. if (size == 0) {
  1315. res = -1;
  1316. break;
  1317. }
  1318. temp = *data;
  1319. lace_size[n] += temp;
  1320. data += 1;
  1321. size -= 1;
  1322. if (temp != 0xff)
  1323. break;
  1324. }
  1325. total += lace_size[n];
  1326. }
  1327. lace_size[n] = size - total;
  1328. break;
  1329. }
  1330. case 0x2: /* fixed-size lacing */
  1331. for (n = 0; n < laces; n++)
  1332. lace_size[n] = size / laces;
  1333. break;
  1334. case 0x3: /* EBML lacing */ {
  1335. uint32_t total;
  1336. n = matroska_ebmlnum_uint(matroska, data, size, &num);
  1337. if (n < 0) {
  1338. av_log(matroska->ctx, AV_LOG_INFO,
  1339. "EBML block data error\n");
  1340. break;
  1341. }
  1342. data += n;
  1343. size -= n;
  1344. total = lace_size[0] = num;
  1345. for (n = 1; res == 0 && n < laces - 1; n++) {
  1346. int64_t snum;
  1347. int r;
  1348. r = matroska_ebmlnum_sint(matroska, data, size, &snum);
  1349. if (r < 0) {
  1350. av_log(matroska->ctx, AV_LOG_INFO,
  1351. "EBML block data error\n");
  1352. break;
  1353. }
  1354. data += r;
  1355. size -= r;
  1356. lace_size[n] = lace_size[n - 1] + snum;
  1357. total += lace_size[n];
  1358. }
  1359. lace_size[n] = size - total;
  1360. break;
  1361. }
  1362. }
  1363. break;
  1364. }
  1365. if (res == 0) {
  1366. uint64_t timecode = AV_NOPTS_VALUE;
  1367. if (cluster_time != (uint64_t)-1
  1368. && (block_time >= 0 || cluster_time >= -block_time))
  1369. timecode = cluster_time + block_time;
  1370. for (n = 0; n < laces; n++) {
  1371. if (st->codec->codec_id == CODEC_ID_RA_288 ||
  1372. st->codec->codec_id == CODEC_ID_COOK ||
  1373. st->codec->codec_id == CODEC_ID_ATRAC3) {
  1374. int a = st->codec->block_align;
  1375. int sps = track->audio.sub_packet_size;
  1376. int cfs = track->audio.coded_framesize;
  1377. int h = track->audio.sub_packet_h;
  1378. int y = track->audio.sub_packet_cnt;
  1379. int w = track->audio.frame_size;
  1380. int x;
  1381. if (!track->audio.pkt_cnt) {
  1382. if (st->codec->codec_id == CODEC_ID_RA_288)
  1383. for (x=0; x<h/2; x++)
  1384. memcpy(track->audio.buf+x*2*w+y*cfs,
  1385. data+x*cfs, cfs);
  1386. else
  1387. for (x=0; x<w/sps; x++)
  1388. memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  1389. if (++track->audio.sub_packet_cnt >= h) {
  1390. track->audio.sub_packet_cnt = 0;
  1391. track->audio.pkt_cnt = h*w / a;
  1392. }
  1393. }
  1394. while (track->audio.pkt_cnt) {
  1395. pkt = av_mallocz(sizeof(AVPacket));
  1396. av_new_packet(pkt, a);
  1397. memcpy(pkt->data, track->audio.buf
  1398. + a * (h*w / a - track->audio.pkt_cnt--), a);
  1399. pkt->pos = pos;
  1400. pkt->stream_index = st->index;
  1401. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1402. }
  1403. } else {
  1404. MatroskaTrackEncoding *encodings = track->encodings.elem;
  1405. int offset = 0, pkt_size = lace_size[n];
  1406. uint8_t *pkt_data = data;
  1407. if (encodings && encodings->scope & 1) {
  1408. offset = matroska_decode_buffer(&pkt_data,&pkt_size, track);
  1409. if (offset < 0)
  1410. continue;
  1411. }
  1412. pkt = av_mallocz(sizeof(AVPacket));
  1413. /* XXX: prevent data copy... */
  1414. if (av_new_packet(pkt, pkt_size+offset) < 0) {
  1415. av_free(pkt);
  1416. res = AVERROR(ENOMEM);
  1417. n = laces-1;
  1418. break;
  1419. }
  1420. if (offset)
  1421. memcpy (pkt->data, encodings->compression.settings.data, offset);
  1422. memcpy (pkt->data+offset, pkt_data, pkt_size);
  1423. if (pkt_data != data)
  1424. av_free(pkt_data);
  1425. if (n == 0)
  1426. pkt->flags = is_keyframe;
  1427. pkt->stream_index = st->index;
  1428. pkt->pts = timecode;
  1429. pkt->pos = pos;
  1430. pkt->duration = duration;
  1431. dynarray_add(&matroska->packets, &matroska->num_packets, pkt);
  1432. }
  1433. if (timecode != AV_NOPTS_VALUE)
  1434. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  1435. data += lace_size[n];
  1436. }
  1437. }
  1438. av_free(lace_size);
  1439. return res;
  1440. }
  1441. static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
  1442. {
  1443. MatroskaCluster cluster = { 0 };
  1444. EbmlList *blocks_list;
  1445. MatroskaBlock *blocks;
  1446. int i, res;
  1447. if (matroska->has_cluster_id){
  1448. /* For the first cluster we parse, its ID was already read as
  1449. part of matroska_read_header(), so don't read it again */
  1450. res = ebml_parse_id(matroska, matroska_clusters,
  1451. MATROSKA_ID_CLUSTER, &cluster);
  1452. matroska->has_cluster_id = 0;
  1453. } else
  1454. res = ebml_parse(matroska, matroska_clusters, &cluster);
  1455. blocks_list = &cluster.blocks;
  1456. blocks = blocks_list->elem;
  1457. for (i=0; i<blocks_list->nb_elem; i++)
  1458. if (blocks[i].bin.size > 0)
  1459. res=matroska_parse_block(matroska,
  1460. blocks[i].bin.data, blocks[i].bin.size,
  1461. blocks[i].bin.pos, cluster.timecode,
  1462. blocks[i].duration, !blocks[i].reference);
  1463. ebml_free(matroska_cluster, &cluster);
  1464. return res;
  1465. }
  1466. static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
  1467. {
  1468. MatroskaDemuxContext *matroska = s->priv_data;
  1469. while (matroska_deliver_packet(matroska, pkt)) {
  1470. if (matroska->done)
  1471. return AVERROR(EIO);
  1472. if (matroska_parse_cluster(matroska) < 0)
  1473. matroska->done = 1;
  1474. }
  1475. return 0;
  1476. }
  1477. static int matroska_read_seek(AVFormatContext *s, int stream_index,
  1478. int64_t timestamp, int flags)
  1479. {
  1480. MatroskaDemuxContext *matroska = s->priv_data;
  1481. AVStream *st = s->streams[stream_index];
  1482. int index;
  1483. index = av_index_search_timestamp(st, timestamp, flags);
  1484. if (index < 0)
  1485. return 0;
  1486. matroska_clear_queue(matroska);
  1487. url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
  1488. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  1489. matroska->skip_to_stream = st;
  1490. av_update_cur_dts(s, st, st->index_entries[index].timestamp);
  1491. return 0;
  1492. }
  1493. static int matroska_read_close(AVFormatContext *s)
  1494. {
  1495. MatroskaDemuxContext *matroska = s->priv_data;
  1496. MatroskaTrack *tracks = matroska->tracks.elem;
  1497. int n;
  1498. matroska_clear_queue(matroska);
  1499. for (n=0; n < matroska->tracks.nb_elem; n++)
  1500. if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
  1501. av_free(tracks[n].audio.buf);
  1502. ebml_free(matroska_segment, matroska);
  1503. return 0;
  1504. }
  1505. AVInputFormat matroska_demuxer = {
  1506. "matroska",
  1507. NULL_IF_CONFIG_SMALL("Matroska file format"),
  1508. sizeof(MatroskaDemuxContext),
  1509. matroska_probe,
  1510. matroska_read_header,
  1511. matroska_read_packet,
  1512. matroska_read_close,
  1513. matroska_read_seek,
  1514. };