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.

1684 lines
58KB

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