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.

1706 lines
59KB

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