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.

2168 lines
78KB

  1. /*
  2. * Matroska file demuxer
  3. * Copyright (c) 2003-2008 The Libav Project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Matroska file demuxer
  24. * @author Ronald Bultje <rbultje@ronald.bitfreak.net>
  25. * @author with a little help from Moritz Bunkus <moritz@bunkus.org>
  26. * @author totally reworked by Aurelien Jacobs <aurel@gnuage.org>
  27. * @see specs available on the Matroska project page: http://www.matroska.org/
  28. */
  29. #include <stdio.h>
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "avio_internal.h"
  33. /* For ff_codec_get_id(). */
  34. #include "riff.h"
  35. #include "isom.h"
  36. #include "rm.h"
  37. #include "matroska.h"
  38. #include "libavcodec/mpeg4audio.h"
  39. #include "libavutil/intfloat.h"
  40. #include "libavutil/intreadwrite.h"
  41. #include "libavutil/avstring.h"
  42. #include "libavutil/lzo.h"
  43. #include "libavutil/dict.h"
  44. #if CONFIG_ZLIB
  45. #include <zlib.h>
  46. #endif
  47. #if CONFIG_BZLIB
  48. #include <bzlib.h>
  49. #endif
  50. typedef enum {
  51. EBML_NONE,
  52. EBML_UINT,
  53. EBML_FLOAT,
  54. EBML_STR,
  55. EBML_UTF8,
  56. EBML_BIN,
  57. EBML_NEST,
  58. EBML_PASS,
  59. EBML_STOP,
  60. EBML_TYPE_COUNT
  61. } EbmlType;
  62. typedef const struct EbmlSyntax {
  63. uint32_t id;
  64. EbmlType type;
  65. int list_elem_size;
  66. int data_offset;
  67. union {
  68. uint64_t u;
  69. double f;
  70. const char *s;
  71. const struct EbmlSyntax *n;
  72. } def;
  73. } EbmlSyntax;
  74. typedef struct {
  75. int nb_elem;
  76. void *elem;
  77. } EbmlList;
  78. typedef struct {
  79. int size;
  80. uint8_t *data;
  81. int64_t pos;
  82. } EbmlBin;
  83. typedef struct {
  84. uint64_t version;
  85. uint64_t max_size;
  86. uint64_t id_length;
  87. char *doctype;
  88. uint64_t doctype_version;
  89. } Ebml;
  90. typedef struct {
  91. uint64_t algo;
  92. EbmlBin settings;
  93. } MatroskaTrackCompression;
  94. typedef struct {
  95. uint64_t scope;
  96. uint64_t type;
  97. MatroskaTrackCompression compression;
  98. } MatroskaTrackEncoding;
  99. typedef struct {
  100. double frame_rate;
  101. uint64_t display_width;
  102. uint64_t display_height;
  103. uint64_t pixel_width;
  104. uint64_t pixel_height;
  105. uint64_t fourcc;
  106. } MatroskaTrackVideo;
  107. typedef struct {
  108. double samplerate;
  109. double out_samplerate;
  110. uint64_t bitdepth;
  111. uint64_t channels;
  112. /* real audio header (extracted from extradata) */
  113. int coded_framesize;
  114. int sub_packet_h;
  115. int frame_size;
  116. int sub_packet_size;
  117. int sub_packet_cnt;
  118. int pkt_cnt;
  119. uint64_t buf_timecode;
  120. uint8_t *buf;
  121. } MatroskaTrackAudio;
  122. typedef struct {
  123. uint64_t num;
  124. uint64_t uid;
  125. uint64_t type;
  126. char *name;
  127. char *codec_id;
  128. EbmlBin codec_priv;
  129. char *language;
  130. double time_scale;
  131. uint64_t default_duration;
  132. uint64_t flag_default;
  133. uint64_t flag_forced;
  134. MatroskaTrackVideo video;
  135. MatroskaTrackAudio audio;
  136. EbmlList encodings;
  137. AVStream *stream;
  138. int64_t end_timecode;
  139. int ms_compat;
  140. } MatroskaTrack;
  141. typedef struct {
  142. uint64_t uid;
  143. char *filename;
  144. char *mime;
  145. EbmlBin bin;
  146. AVStream *stream;
  147. } MatroskaAttachement;
  148. typedef struct {
  149. uint64_t start;
  150. uint64_t end;
  151. uint64_t uid;
  152. char *title;
  153. AVChapter *chapter;
  154. } MatroskaChapter;
  155. typedef struct {
  156. uint64_t track;
  157. uint64_t pos;
  158. } MatroskaIndexPos;
  159. typedef struct {
  160. uint64_t time;
  161. EbmlList pos;
  162. } MatroskaIndex;
  163. typedef struct {
  164. char *name;
  165. char *string;
  166. char *lang;
  167. uint64_t def;
  168. EbmlList sub;
  169. } MatroskaTag;
  170. typedef struct {
  171. char *type;
  172. uint64_t typevalue;
  173. uint64_t trackuid;
  174. uint64_t chapteruid;
  175. uint64_t attachuid;
  176. } MatroskaTagTarget;
  177. typedef struct {
  178. MatroskaTagTarget target;
  179. EbmlList tag;
  180. } MatroskaTags;
  181. typedef struct {
  182. uint64_t id;
  183. uint64_t pos;
  184. } MatroskaSeekhead;
  185. typedef struct {
  186. uint64_t start;
  187. uint64_t length;
  188. } MatroskaLevel;
  189. typedef struct {
  190. uint64_t timecode;
  191. EbmlList blocks;
  192. } MatroskaCluster;
  193. typedef struct {
  194. AVFormatContext *ctx;
  195. /* EBML stuff */
  196. int num_levels;
  197. MatroskaLevel levels[EBML_MAX_DEPTH];
  198. int level_up;
  199. uint32_t current_id;
  200. uint64_t time_scale;
  201. double duration;
  202. char *title;
  203. EbmlList tracks;
  204. EbmlList attachments;
  205. EbmlList chapters;
  206. EbmlList index;
  207. EbmlList tags;
  208. EbmlList seekhead;
  209. /* byte position of the segment inside the stream */
  210. int64_t segment_start;
  211. /* the packet queue */
  212. AVPacket **packets;
  213. int num_packets;
  214. AVPacket *prev_pkt;
  215. int done;
  216. /* What to skip before effectively reading a packet. */
  217. int skip_to_keyframe;
  218. uint64_t skip_to_timecode;
  219. /* File has a CUES element, but we defer parsing until it is needed. */
  220. int cues_parsing_deferred;
  221. int current_cluster_num_blocks;
  222. int64_t current_cluster_pos;
  223. MatroskaCluster current_cluster;
  224. /* File has SSA subtitles which prevent incremental cluster parsing. */
  225. int contains_ssa;
  226. } MatroskaDemuxContext;
  227. typedef struct {
  228. uint64_t duration;
  229. int64_t reference;
  230. uint64_t non_simple;
  231. EbmlBin bin;
  232. } MatroskaBlock;
  233. static EbmlSyntax ebml_header[] = {
  234. { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
  235. { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
  236. { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
  237. { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
  238. { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
  239. { EBML_ID_EBMLVERSION, EBML_NONE },
  240. { EBML_ID_DOCTYPEVERSION, EBML_NONE },
  241. { 0 }
  242. };
  243. static EbmlSyntax ebml_syntax[] = {
  244. { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
  245. { 0 }
  246. };
  247. static EbmlSyntax matroska_info[] = {
  248. { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
  249. { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
  250. { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) },
  251. { MATROSKA_ID_WRITINGAPP, EBML_NONE },
  252. { MATROSKA_ID_MUXINGAPP, EBML_NONE },
  253. { MATROSKA_ID_DATEUTC, EBML_NONE },
  254. { MATROSKA_ID_SEGMENTUID, EBML_NONE },
  255. { 0 }
  256. };
  257. static EbmlSyntax matroska_track_video[] = {
  258. { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
  259. { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
  260. { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
  261. { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
  262. { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
  263. { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) },
  264. { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE },
  265. { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
  266. { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
  267. { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
  268. { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE },
  269. { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE },
  270. { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE },
  271. { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
  272. { 0 }
  273. };
  274. static EbmlSyntax matroska_track_audio[] = {
  275. { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} },
  276. { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) },
  277. { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) },
  278. { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} },
  279. { 0 }
  280. };
  281. static EbmlSyntax matroska_track_encoding_compression[] = {
  282. { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} },
  283. { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) },
  284. { 0 }
  285. };
  286. static EbmlSyntax matroska_track_encoding[] = {
  287. { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
  288. { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
  289. { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
  290. { MATROSKA_ID_ENCODINGORDER, EBML_NONE },
  291. { 0 }
  292. };
  293. static EbmlSyntax matroska_track_encodings[] = {
  294. { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} },
  295. { 0 }
  296. };
  297. static EbmlSyntax matroska_track[] = {
  298. { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) },
  299. { MATROSKA_ID_TRACKNAME, EBML_UTF8, 0, offsetof(MatroskaTrack,name) },
  300. { MATROSKA_ID_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTrack,uid) },
  301. { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) },
  302. { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) },
  303. { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) },
  304. { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} },
  305. { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
  306. { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
  307. { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
  308. { MATROSKA_ID_TRACKFLAGFORCED, EBML_UINT, 0, offsetof(MatroskaTrack,flag_forced), {.u=0} },
  309. { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
  310. { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
  311. { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
  312. { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE },
  313. { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
  314. { MATROSKA_ID_CODECNAME, EBML_NONE },
  315. { MATROSKA_ID_CODECDECODEALL, EBML_NONE },
  316. { MATROSKA_ID_CODECINFOURL, EBML_NONE },
  317. { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
  318. { MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
  319. { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
  320. { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE },
  321. { 0 }
  322. };
  323. static EbmlSyntax matroska_tracks[] = {
  324. { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} },
  325. { 0 }
  326. };
  327. static EbmlSyntax matroska_attachment[] = {
  328. { MATROSKA_ID_FILEUID, EBML_UINT, 0, offsetof(MatroskaAttachement,uid) },
  329. { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
  330. { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) },
  331. { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) },
  332. { MATROSKA_ID_FILEDESC, EBML_NONE },
  333. { 0 }
  334. };
  335. static EbmlSyntax matroska_attachments[] = {
  336. { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
  337. { 0 }
  338. };
  339. static EbmlSyntax matroska_chapter_display[] = {
  340. { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
  341. { MATROSKA_ID_CHAPLANG, EBML_NONE },
  342. { 0 }
  343. };
  344. static EbmlSyntax matroska_chapter_entry[] = {
  345. { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
  346. { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
  347. { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
  348. { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
  349. { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
  350. { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
  351. { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
  352. { MATROSKA_ID_CHAPTERATOM, EBML_NONE },
  353. { 0 }
  354. };
  355. static EbmlSyntax matroska_chapter[] = {
  356. { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
  357. { MATROSKA_ID_EDITIONUID, EBML_NONE },
  358. { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
  359. { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
  360. { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
  361. { 0 }
  362. };
  363. static EbmlSyntax matroska_chapters[] = {
  364. { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} },
  365. { 0 }
  366. };
  367. static EbmlSyntax matroska_index_pos[] = {
  368. { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
  369. { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) },
  370. { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE },
  371. { 0 }
  372. };
  373. static EbmlSyntax matroska_index_entry[] = {
  374. { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) },
  375. { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
  376. { 0 }
  377. };
  378. static EbmlSyntax matroska_index[] = {
  379. { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
  380. { 0 }
  381. };
  382. static EbmlSyntax matroska_simpletag[] = {
  383. { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) },
  384. { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) },
  385. { MATROSKA_ID_TAGLANG, EBML_STR, 0, offsetof(MatroskaTag,lang), {.s="und"} },
  386. { MATROSKA_ID_TAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTag,def) },
  387. { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0, offsetof(MatroskaTag,def) },
  388. { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
  389. { 0 }
  390. };
  391. static EbmlSyntax matroska_tagtargets[] = {
  392. { MATROSKA_ID_TAGTARGETS_TYPE, EBML_STR, 0, offsetof(MatroskaTagTarget,type) },
  393. { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, offsetof(MatroskaTagTarget,typevalue), {.u=50} },
  394. { MATROSKA_ID_TAGTARGETS_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,trackuid) },
  395. { MATROSKA_ID_TAGTARGETS_CHAPTERUID,EBML_UINT, 0, offsetof(MatroskaTagTarget,chapteruid) },
  396. { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,attachuid) },
  397. { 0 }
  398. };
  399. static EbmlSyntax matroska_tag[] = {
  400. { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTags,tag), {.n=matroska_simpletag} },
  401. { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, offsetof(MatroskaTags,target), {.n=matroska_tagtargets} },
  402. { 0 }
  403. };
  404. static EbmlSyntax matroska_tags[] = {
  405. { MATROSKA_ID_TAG, EBML_NEST, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} },
  406. { 0 }
  407. };
  408. static EbmlSyntax matroska_seekhead_entry[] = {
  409. { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) },
  410. { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} },
  411. { 0 }
  412. };
  413. static EbmlSyntax matroska_seekhead[] = {
  414. { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} },
  415. { 0 }
  416. };
  417. static EbmlSyntax matroska_segment[] = {
  418. { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } },
  419. { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } },
  420. { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} },
  421. { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } },
  422. { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } },
  423. { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } },
  424. { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } },
  425. { MATROSKA_ID_CLUSTER, EBML_STOP },
  426. { 0 }
  427. };
  428. static EbmlSyntax matroska_segments[] = {
  429. { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } },
  430. { 0 }
  431. };
  432. static EbmlSyntax matroska_blockgroup[] = {
  433. { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
  434. { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
  435. { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
  436. { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
  437. { 1, EBML_UINT, 0, offsetof(MatroskaBlock,non_simple), {.u=1} },
  438. { 0 }
  439. };
  440. static EbmlSyntax matroska_cluster[] = {
  441. { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
  442. { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  443. { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  444. { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
  445. { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
  446. { 0 }
  447. };
  448. static EbmlSyntax matroska_clusters[] = {
  449. { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} },
  450. { MATROSKA_ID_INFO, EBML_NONE },
  451. { MATROSKA_ID_CUES, EBML_NONE },
  452. { MATROSKA_ID_TAGS, EBML_NONE },
  453. { MATROSKA_ID_SEEKHEAD, EBML_NONE },
  454. { 0 }
  455. };
  456. static EbmlSyntax matroska_cluster_incremental_parsing[] = {
  457. { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
  458. { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  459. { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  460. { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
  461. { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
  462. { MATROSKA_ID_INFO, EBML_NONE },
  463. { MATROSKA_ID_CUES, EBML_NONE },
  464. { MATROSKA_ID_TAGS, EBML_NONE },
  465. { MATROSKA_ID_SEEKHEAD, EBML_NONE },
  466. { MATROSKA_ID_CLUSTER, EBML_STOP },
  467. { 0 }
  468. };
  469. static EbmlSyntax matroska_cluster_incremental[] = {
  470. { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
  471. { MATROSKA_ID_BLOCKGROUP, EBML_STOP },
  472. { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP },
  473. { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
  474. { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
  475. { 0 }
  476. };
  477. static EbmlSyntax matroska_clusters_incremental[] = {
  478. { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster_incremental} },
  479. { MATROSKA_ID_INFO, EBML_NONE },
  480. { MATROSKA_ID_CUES, EBML_NONE },
  481. { MATROSKA_ID_TAGS, EBML_NONE },
  482. { MATROSKA_ID_SEEKHEAD, EBML_NONE },
  483. { 0 }
  484. };
  485. static const char *const matroska_doctypes[] = { "matroska", "webm" };
  486. /*
  487. * Return: Whether we reached the end of a level in the hierarchy or not.
  488. */
  489. static int ebml_level_end(MatroskaDemuxContext *matroska)
  490. {
  491. AVIOContext *pb = matroska->ctx->pb;
  492. int64_t pos = avio_tell(pb);
  493. if (matroska->num_levels > 0) {
  494. MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
  495. if (pos - level->start >= level->length || matroska->current_id) {
  496. matroska->num_levels--;
  497. return 1;
  498. }
  499. }
  500. return 0;
  501. }
  502. /*
  503. * Read: an "EBML number", which is defined as a variable-length
  504. * array of bytes. The first byte indicates the length by giving a
  505. * number of 0-bits followed by a one. The position of the first
  506. * "one" bit inside the first byte indicates the length of this
  507. * number.
  508. * Returns: number of bytes read, < 0 on error
  509. */
  510. static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb,
  511. int max_size, uint64_t *number)
  512. {
  513. int read = 1, n = 1;
  514. uint64_t total = 0;
  515. /* The first byte tells us the length in bytes - avio_r8() can normally
  516. * return 0, but since that's not a valid first ebmlID byte, we can
  517. * use it safely here to catch EOS. */
  518. if (!(total = avio_r8(pb))) {
  519. /* we might encounter EOS here */
  520. if (!pb->eof_reached) {
  521. int64_t pos = avio_tell(pb);
  522. av_log(matroska->ctx, AV_LOG_ERROR,
  523. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
  524. pos, pos);
  525. }
  526. return AVERROR(EIO); /* EOS or actual I/O error */
  527. }
  528. /* get the length of the EBML number */
  529. read = 8 - ff_log2_tab[total];
  530. if (read > max_size) {
  531. int64_t pos = avio_tell(pb) - 1;
  532. av_log(matroska->ctx, AV_LOG_ERROR,
  533. "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
  534. (uint8_t) total, pos, pos);
  535. return AVERROR_INVALIDDATA;
  536. }
  537. /* read out length */
  538. total ^= 1 << ff_log2_tab[total];
  539. while (n++ < read)
  540. total = (total << 8) | avio_r8(pb);
  541. *number = total;
  542. return read;
  543. }
  544. /**
  545. * Read a EBML length value.
  546. * This needs special handling for the "unknown length" case which has multiple
  547. * encodings.
  548. */
  549. static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
  550. uint64_t *number)
  551. {
  552. int res = ebml_read_num(matroska, pb, 8, number);
  553. if (res > 0 && *number + 1 == 1ULL << (7 * res))
  554. *number = 0xffffffffffffffULL;
  555. return res;
  556. }
  557. /*
  558. * Read the next element as an unsigned int.
  559. * 0 is success, < 0 is failure.
  560. */
  561. static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
  562. {
  563. int n = 0;
  564. if (size > 8)
  565. return AVERROR_INVALIDDATA;
  566. /* big-endian ordering; build up number */
  567. *num = 0;
  568. while (n++ < size)
  569. *num = (*num << 8) | avio_r8(pb);
  570. return 0;
  571. }
  572. /*
  573. * Read the next element as a float.
  574. * 0 is success, < 0 is failure.
  575. */
  576. static int ebml_read_float(AVIOContext *pb, int size, double *num)
  577. {
  578. if (size == 0) {
  579. *num = 0;
  580. } else if (size == 4) {
  581. *num = av_int2float(avio_rb32(pb));
  582. } else if (size == 8){
  583. *num = av_int2double(avio_rb64(pb));
  584. } else
  585. return AVERROR_INVALIDDATA;
  586. return 0;
  587. }
  588. /*
  589. * Read the next element as an ASCII string.
  590. * 0 is success, < 0 is failure.
  591. */
  592. static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
  593. {
  594. char *res;
  595. /* EBML strings are usually not 0-terminated, so we allocate one
  596. * byte more, read the string and NULL-terminate it ourselves. */
  597. if (!(res = av_malloc(size + 1)))
  598. return AVERROR(ENOMEM);
  599. if (avio_read(pb, (uint8_t *) res, size) != size) {
  600. av_free(res);
  601. return AVERROR(EIO);
  602. }
  603. (res)[size] = '\0';
  604. av_free(*str);
  605. *str = res;
  606. return 0;
  607. }
  608. /*
  609. * Read the next element as binary data.
  610. * 0 is success, < 0 is failure.
  611. */
  612. static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
  613. {
  614. av_free(bin->data);
  615. if (!(bin->data = av_malloc(length)))
  616. return AVERROR(ENOMEM);
  617. bin->size = length;
  618. bin->pos = avio_tell(pb);
  619. if (avio_read(pb, bin->data, length) != length) {
  620. av_freep(&bin->data);
  621. return AVERROR(EIO);
  622. }
  623. return 0;
  624. }
  625. /*
  626. * Read the next element, but only the header. The contents
  627. * are supposed to be sub-elements which can be read separately.
  628. * 0 is success, < 0 is failure.
  629. */
  630. static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
  631. {
  632. AVIOContext *pb = matroska->ctx->pb;
  633. MatroskaLevel *level;
  634. if (matroska->num_levels >= EBML_MAX_DEPTH) {
  635. av_log(matroska->ctx, AV_LOG_ERROR,
  636. "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
  637. return AVERROR(ENOSYS);
  638. }
  639. level = &matroska->levels[matroska->num_levels++];
  640. level->start = avio_tell(pb);
  641. level->length = length;
  642. return 0;
  643. }
  644. /*
  645. * Read signed/unsigned "EBML" numbers.
  646. * Return: number of bytes processed, < 0 on error
  647. */
  648. static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
  649. uint8_t *data, uint32_t size, uint64_t *num)
  650. {
  651. AVIOContext pb;
  652. ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
  653. return ebml_read_num(matroska, &pb, FFMIN(size, 8), num);
  654. }
  655. /*
  656. * Same as above, but signed.
  657. */
  658. static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
  659. uint8_t *data, uint32_t size, int64_t *num)
  660. {
  661. uint64_t unum;
  662. int res;
  663. /* read as unsigned number first */
  664. if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
  665. return res;
  666. /* make signed (weird way) */
  667. *num = unum - ((1LL << (7*res - 1)) - 1);
  668. return res;
  669. }
  670. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  671. EbmlSyntax *syntax, void *data);
  672. static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  673. uint32_t id, void *data)
  674. {
  675. int i;
  676. for (i=0; syntax[i].id; i++)
  677. if (id == syntax[i].id)
  678. break;
  679. if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
  680. matroska->num_levels > 0 &&
  681. matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff)
  682. return 0; // we reached the end of an unknown size cluster
  683. if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
  684. av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
  685. if (matroska->ctx->error_recognition & AV_EF_EXPLODE)
  686. return AVERROR_INVALIDDATA;
  687. }
  688. return ebml_parse_elem(matroska, &syntax[i], data);
  689. }
  690. static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  691. void *data)
  692. {
  693. if (!matroska->current_id) {
  694. uint64_t id;
  695. int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
  696. if (res < 0)
  697. return res;
  698. matroska->current_id = id | 1 << 7*res;
  699. }
  700. return ebml_parse_id(matroska, syntax, matroska->current_id, data);
  701. }
  702. static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  703. void *data)
  704. {
  705. int i, res = 0;
  706. for (i=0; syntax[i].id; i++)
  707. switch (syntax[i].type) {
  708. case EBML_UINT:
  709. *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
  710. break;
  711. case EBML_FLOAT:
  712. *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
  713. break;
  714. case EBML_STR:
  715. case EBML_UTF8:
  716. *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
  717. break;
  718. }
  719. while (!res && !ebml_level_end(matroska))
  720. res = ebml_parse(matroska, syntax, data);
  721. return res;
  722. }
  723. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  724. EbmlSyntax *syntax, void *data)
  725. {
  726. static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
  727. [EBML_UINT] = 8,
  728. [EBML_FLOAT] = 8,
  729. // max. 16 MB for strings
  730. [EBML_STR] = 0x1000000,
  731. [EBML_UTF8] = 0x1000000,
  732. // max. 256 MB for binary data
  733. [EBML_BIN] = 0x10000000,
  734. // no limits for anything else
  735. };
  736. AVIOContext *pb = matroska->ctx->pb;
  737. uint32_t id = syntax->id;
  738. uint64_t length;
  739. int res;
  740. void *newelem;
  741. data = (char *)data + syntax->data_offset;
  742. if (syntax->list_elem_size) {
  743. EbmlList *list = data;
  744. newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
  745. if (!newelem)
  746. return AVERROR(ENOMEM);
  747. list->elem = newelem;
  748. data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
  749. memset(data, 0, syntax->list_elem_size);
  750. list->nb_elem++;
  751. }
  752. if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {
  753. matroska->current_id = 0;
  754. if ((res = ebml_read_length(matroska, pb, &length)) < 0)
  755. return res;
  756. if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
  757. av_log(matroska->ctx, AV_LOG_ERROR,
  758. "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",
  759. length, max_lengths[syntax->type], syntax->type);
  760. return AVERROR_INVALIDDATA;
  761. }
  762. }
  763. switch (syntax->type) {
  764. case EBML_UINT: res = ebml_read_uint (pb, length, data); break;
  765. case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;
  766. case EBML_STR:
  767. case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;
  768. case EBML_BIN: res = ebml_read_binary(pb, length, data); break;
  769. case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)
  770. return res;
  771. if (id == MATROSKA_ID_SEGMENT)
  772. matroska->segment_start = avio_tell(matroska->ctx->pb);
  773. return ebml_parse_nest(matroska, syntax->def.n, data);
  774. case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);
  775. case EBML_STOP: return 1;
  776. default: return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;
  777. }
  778. if (res == AVERROR_INVALIDDATA)
  779. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
  780. else if (res == AVERROR(EIO))
  781. av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
  782. return res;
  783. }
  784. static void ebml_free(EbmlSyntax *syntax, void *data)
  785. {
  786. int i, j;
  787. for (i=0; syntax[i].id; i++) {
  788. void *data_off = (char *)data + syntax[i].data_offset;
  789. switch (syntax[i].type) {
  790. case EBML_STR:
  791. case EBML_UTF8: av_freep(data_off); break;
  792. case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
  793. case EBML_NEST:
  794. if (syntax[i].list_elem_size) {
  795. EbmlList *list = data_off;
  796. char *ptr = list->elem;
  797. for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
  798. ebml_free(syntax[i].def.n, ptr);
  799. av_free(list->elem);
  800. } else
  801. ebml_free(syntax[i].def.n, data_off);
  802. default: break;
  803. }
  804. }
  805. }
  806. /*
  807. * Autodetecting...
  808. */
  809. static int matroska_probe(AVProbeData *p)
  810. {
  811. uint64_t total = 0;
  812. int len_mask = 0x80, size = 1, n = 1, i;
  813. /* EBML header? */
  814. if (AV_RB32(p->buf) != EBML_ID_HEADER)
  815. return 0;
  816. /* length of header */
  817. total = p->buf[4];
  818. while (size <= 8 && !(total & len_mask)) {
  819. size++;
  820. len_mask >>= 1;
  821. }
  822. if (size > 8)
  823. return 0;
  824. total &= (len_mask - 1);
  825. while (n < size)
  826. total = (total << 8) | p->buf[4 + n++];
  827. /* Does the probe data contain the whole header? */
  828. if (p->buf_size < 4 + size + total)
  829. return 0;
  830. /* The header should contain a known document type. For now,
  831. * we don't parse the whole header but simply check for the
  832. * availability of that array of characters inside the header.
  833. * Not fully fool-proof, but good enough. */
  834. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
  835. int probelen = strlen(matroska_doctypes[i]);
  836. if (total < probelen)
  837. continue;
  838. for (n = 4+size; n <= 4+size+total-probelen; n++)
  839. if (!memcmp(p->buf+n, matroska_doctypes[i], probelen))
  840. return AVPROBE_SCORE_MAX;
  841. }
  842. // probably valid EBML header but no recognized doctype
  843. return AVPROBE_SCORE_MAX/2;
  844. }
  845. static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
  846. int num)
  847. {
  848. MatroskaTrack *tracks = matroska->tracks.elem;
  849. int i;
  850. for (i=0; i < matroska->tracks.nb_elem; i++)
  851. if (tracks[i].num == num)
  852. return &tracks[i];
  853. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
  854. return NULL;
  855. }
  856. static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
  857. MatroskaTrack *track)
  858. {
  859. MatroskaTrackEncoding *encodings = track->encodings.elem;
  860. uint8_t* data = *buf;
  861. int isize = *buf_size;
  862. uint8_t* pkt_data = NULL;
  863. uint8_t av_unused *newpktdata;
  864. int pkt_size = isize;
  865. int result = 0;
  866. int olen;
  867. if (pkt_size >= 10000000)
  868. return -1;
  869. switch (encodings[0].compression.algo) {
  870. case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
  871. return encodings[0].compression.settings.size;
  872. case MATROSKA_TRACK_ENCODING_COMP_LZO:
  873. do {
  874. olen = pkt_size *= 3;
  875. pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING);
  876. result = av_lzo1x_decode(pkt_data, &olen, data, &isize);
  877. } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000);
  878. if (result)
  879. goto failed;
  880. pkt_size -= olen;
  881. break;
  882. #if CONFIG_ZLIB
  883. case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
  884. z_stream zstream = {0};
  885. if (inflateInit(&zstream) != Z_OK)
  886. return -1;
  887. zstream.next_in = data;
  888. zstream.avail_in = isize;
  889. do {
  890. pkt_size *= 3;
  891. newpktdata = av_realloc(pkt_data, pkt_size);
  892. if (!newpktdata) {
  893. inflateEnd(&zstream);
  894. goto failed;
  895. }
  896. pkt_data = newpktdata;
  897. zstream.avail_out = pkt_size - zstream.total_out;
  898. zstream.next_out = pkt_data + zstream.total_out;
  899. result = inflate(&zstream, Z_NO_FLUSH);
  900. } while (result==Z_OK && pkt_size<10000000);
  901. pkt_size = zstream.total_out;
  902. inflateEnd(&zstream);
  903. if (result != Z_STREAM_END)
  904. goto failed;
  905. break;
  906. }
  907. #endif
  908. #if CONFIG_BZLIB
  909. case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
  910. bz_stream bzstream = {0};
  911. if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
  912. return -1;
  913. bzstream.next_in = data;
  914. bzstream.avail_in = isize;
  915. do {
  916. pkt_size *= 3;
  917. newpktdata = av_realloc(pkt_data, pkt_size);
  918. if (!newpktdata) {
  919. BZ2_bzDecompressEnd(&bzstream);
  920. goto failed;
  921. }
  922. pkt_data = newpktdata;
  923. bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
  924. bzstream.next_out = pkt_data + bzstream.total_out_lo32;
  925. result = BZ2_bzDecompress(&bzstream);
  926. } while (result==BZ_OK && pkt_size<10000000);
  927. pkt_size = bzstream.total_out_lo32;
  928. BZ2_bzDecompressEnd(&bzstream);
  929. if (result != BZ_STREAM_END)
  930. goto failed;
  931. break;
  932. }
  933. #endif
  934. default:
  935. return -1;
  936. }
  937. *buf = pkt_data;
  938. *buf_size = pkt_size;
  939. return 0;
  940. failed:
  941. av_free(pkt_data);
  942. return -1;
  943. }
  944. static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
  945. AVPacket *pkt, uint64_t display_duration)
  946. {
  947. char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
  948. for (; *ptr!=',' && ptr<end-1; ptr++);
  949. if (*ptr == ',')
  950. layer = ++ptr;
  951. for (; *ptr!=',' && ptr<end-1; ptr++);
  952. if (*ptr == ',') {
  953. int64_t end_pts = pkt->pts + display_duration;
  954. int sc = matroska->time_scale * pkt->pts / 10000000;
  955. int ec = matroska->time_scale * end_pts / 10000000;
  956. int sh, sm, ss, eh, em, es, len;
  957. sh = sc/360000; sc -= 360000*sh;
  958. sm = sc/ 6000; sc -= 6000*sm;
  959. ss = sc/ 100; sc -= 100*ss;
  960. eh = ec/360000; ec -= 360000*eh;
  961. em = ec/ 6000; ec -= 6000*em;
  962. es = ec/ 100; ec -= 100*es;
  963. *ptr++ = '\0';
  964. len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
  965. if (!(line = av_malloc(len)))
  966. return;
  967. snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
  968. layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
  969. av_free(pkt->data);
  970. pkt->data = line;
  971. pkt->size = strlen(line);
  972. }
  973. }
  974. static int matroska_merge_packets(AVPacket *out, AVPacket *in)
  975. {
  976. void *newdata = av_realloc(out->data, out->size+in->size);
  977. if (!newdata)
  978. return AVERROR(ENOMEM);
  979. out->data = newdata;
  980. memcpy(out->data+out->size, in->data, in->size);
  981. out->size += in->size;
  982. av_destruct_packet(in);
  983. av_free(in);
  984. return 0;
  985. }
  986. static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
  987. AVDictionary **metadata, char *prefix)
  988. {
  989. MatroskaTag *tags = list->elem;
  990. char key[1024];
  991. int i;
  992. for (i=0; i < list->nb_elem; i++) {
  993. const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
  994. if (!tags[i].name) {
  995. av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
  996. continue;
  997. }
  998. if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
  999. else av_strlcpy(key, tags[i].name, sizeof(key));
  1000. if (tags[i].def || !lang) {
  1001. av_dict_set(metadata, key, tags[i].string, 0);
  1002. if (tags[i].sub.nb_elem)
  1003. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  1004. }
  1005. if (lang) {
  1006. av_strlcat(key, "-", sizeof(key));
  1007. av_strlcat(key, lang, sizeof(key));
  1008. av_dict_set(metadata, key, tags[i].string, 0);
  1009. if (tags[i].sub.nb_elem)
  1010. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  1011. }
  1012. }
  1013. ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
  1014. }
  1015. static void matroska_convert_tags(AVFormatContext *s)
  1016. {
  1017. MatroskaDemuxContext *matroska = s->priv_data;
  1018. MatroskaTags *tags = matroska->tags.elem;
  1019. int i, j;
  1020. for (i=0; i < matroska->tags.nb_elem; i++) {
  1021. if (tags[i].target.attachuid) {
  1022. MatroskaAttachement *attachment = matroska->attachments.elem;
  1023. for (j=0; j<matroska->attachments.nb_elem; j++)
  1024. if (attachment[j].uid == tags[i].target.attachuid
  1025. && attachment[j].stream)
  1026. matroska_convert_tag(s, &tags[i].tag,
  1027. &attachment[j].stream->metadata, NULL);
  1028. } else if (tags[i].target.chapteruid) {
  1029. MatroskaChapter *chapter = matroska->chapters.elem;
  1030. for (j=0; j<matroska->chapters.nb_elem; j++)
  1031. if (chapter[j].uid == tags[i].target.chapteruid
  1032. && chapter[j].chapter)
  1033. matroska_convert_tag(s, &tags[i].tag,
  1034. &chapter[j].chapter->metadata, NULL);
  1035. } else if (tags[i].target.trackuid) {
  1036. MatroskaTrack *track = matroska->tracks.elem;
  1037. for (j=0; j<matroska->tracks.nb_elem; j++)
  1038. if (track[j].uid == tags[i].target.trackuid && track[j].stream)
  1039. matroska_convert_tag(s, &tags[i].tag,
  1040. &track[j].stream->metadata, NULL);
  1041. } else {
  1042. matroska_convert_tag(s, &tags[i].tag, &s->metadata,
  1043. tags[i].target.type);
  1044. }
  1045. }
  1046. }
  1047. static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx)
  1048. {
  1049. EbmlList *seekhead_list = &matroska->seekhead;
  1050. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1051. uint32_t level_up = matroska->level_up;
  1052. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1053. uint32_t saved_id = matroska->current_id;
  1054. MatroskaLevel level;
  1055. int64_t offset;
  1056. int ret = 0;
  1057. if (idx >= seekhead_list->nb_elem
  1058. || seekhead[idx].id == MATROSKA_ID_SEEKHEAD
  1059. || seekhead[idx].id == MATROSKA_ID_CLUSTER)
  1060. return 0;
  1061. /* seek */
  1062. offset = seekhead[idx].pos + matroska->segment_start;
  1063. if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) {
  1064. /* We don't want to lose our seekhead level, so we add
  1065. * a dummy. This is a crude hack. */
  1066. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1067. av_log(matroska->ctx, AV_LOG_INFO,
  1068. "Max EBML element depth (%d) reached, "
  1069. "cannot parse further.\n", EBML_MAX_DEPTH);
  1070. ret = AVERROR_INVALIDDATA;
  1071. } else {
  1072. level.start = 0;
  1073. level.length = (uint64_t)-1;
  1074. matroska->levels[matroska->num_levels] = level;
  1075. matroska->num_levels++;
  1076. matroska->current_id = 0;
  1077. ret = ebml_parse(matroska, matroska_segment, matroska);
  1078. /* remove dummy level */
  1079. while (matroska->num_levels) {
  1080. uint64_t length = matroska->levels[--matroska->num_levels].length;
  1081. if (length == (uint64_t)-1)
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. /* seek back */
  1087. avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
  1088. matroska->level_up = level_up;
  1089. matroska->current_id = saved_id;
  1090. return ret;
  1091. }
  1092. static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
  1093. {
  1094. EbmlList *seekhead_list = &matroska->seekhead;
  1095. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1096. int i;
  1097. // we should not do any seeking in the streaming case
  1098. if (!matroska->ctx->pb->seekable ||
  1099. (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
  1100. return;
  1101. for (i = 0; i < seekhead_list->nb_elem; i++) {
  1102. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1103. if (seekhead[i].pos <= before_pos)
  1104. continue;
  1105. // defer cues parsing until we actually need cue data.
  1106. if (seekhead[i].id == MATROSKA_ID_CUES) {
  1107. matroska->cues_parsing_deferred = 1;
  1108. continue;
  1109. }
  1110. if (matroska_parse_seekhead_entry(matroska, i) < 0)
  1111. break;
  1112. }
  1113. }
  1114. static void matroska_parse_cues(MatroskaDemuxContext *matroska) {
  1115. EbmlList *seekhead_list = &matroska->seekhead;
  1116. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1117. EbmlList *index_list;
  1118. MatroskaIndex *index;
  1119. int index_scale = 1;
  1120. int i, j;
  1121. for (i = 0; i < seekhead_list->nb_elem; i++)
  1122. if (seekhead[i].id == MATROSKA_ID_CUES)
  1123. break;
  1124. assert(i <= seekhead_list->nb_elem);
  1125. matroska_parse_seekhead_entry(matroska, i);
  1126. index_list = &matroska->index;
  1127. index = index_list->elem;
  1128. if (index_list->nb_elem
  1129. && index[0].time > 1E14/matroska->time_scale) {
  1130. av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
  1131. index_scale = matroska->time_scale;
  1132. }
  1133. for (i = 0; i < index_list->nb_elem; i++) {
  1134. EbmlList *pos_list = &index[i].pos;
  1135. MatroskaIndexPos *pos = pos_list->elem;
  1136. for (j = 0; j < pos_list->nb_elem; j++) {
  1137. MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track);
  1138. if (track && track->stream)
  1139. av_add_index_entry(track->stream,
  1140. pos[j].pos + matroska->segment_start,
  1141. index[i].time/index_scale, 0, 0,
  1142. AVINDEX_KEYFRAME);
  1143. }
  1144. }
  1145. }
  1146. static int matroska_aac_profile(char *codec_id)
  1147. {
  1148. static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
  1149. int profile;
  1150. for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
  1151. if (strstr(codec_id, aac_profiles[profile]))
  1152. break;
  1153. return profile + 1;
  1154. }
  1155. static int matroska_aac_sri(int samplerate)
  1156. {
  1157. int sri;
  1158. for (sri=0; sri<FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++)
  1159. if (avpriv_mpeg4audio_sample_rates[sri] == samplerate)
  1160. break;
  1161. return sri;
  1162. }
  1163. static int matroska_read_header(AVFormatContext *s)
  1164. {
  1165. MatroskaDemuxContext *matroska = s->priv_data;
  1166. EbmlList *attachements_list = &matroska->attachments;
  1167. MatroskaAttachement *attachements;
  1168. EbmlList *chapters_list = &matroska->chapters;
  1169. MatroskaChapter *chapters;
  1170. MatroskaTrack *tracks;
  1171. uint64_t max_start = 0;
  1172. Ebml ebml = { 0 };
  1173. AVStream *st;
  1174. int i, j, res;
  1175. matroska->ctx = s;
  1176. /* First read the EBML header. */
  1177. if (ebml_parse(matroska, ebml_syntax, &ebml)
  1178. || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
  1179. || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 2) {
  1180. av_log(matroska->ctx, AV_LOG_ERROR,
  1181. "EBML header using unsupported features\n"
  1182. "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
  1183. ebml.version, ebml.doctype, ebml.doctype_version);
  1184. ebml_free(ebml_syntax, &ebml);
  1185. return AVERROR_PATCHWELCOME;
  1186. }
  1187. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
  1188. if (!strcmp(ebml.doctype, matroska_doctypes[i]))
  1189. break;
  1190. if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
  1191. av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
  1192. }
  1193. ebml_free(ebml_syntax, &ebml);
  1194. /* The next thing is a segment. */
  1195. if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0)
  1196. return res;
  1197. matroska_execute_seekhead(matroska);
  1198. if (!matroska->time_scale)
  1199. matroska->time_scale = 1000000;
  1200. if (matroska->duration)
  1201. matroska->ctx->duration = matroska->duration * matroska->time_scale
  1202. * 1000 / AV_TIME_BASE;
  1203. av_dict_set(&s->metadata, "title", matroska->title, 0);
  1204. tracks = matroska->tracks.elem;
  1205. for (i=0; i < matroska->tracks.nb_elem; i++) {
  1206. MatroskaTrack *track = &tracks[i];
  1207. enum CodecID codec_id = CODEC_ID_NONE;
  1208. EbmlList *encodings_list = &tracks->encodings;
  1209. MatroskaTrackEncoding *encodings = encodings_list->elem;
  1210. uint8_t *extradata = NULL;
  1211. int extradata_size = 0;
  1212. int extradata_offset = 0;
  1213. AVIOContext b;
  1214. /* Apply some sanity checks. */
  1215. if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
  1216. track->type != MATROSKA_TRACK_TYPE_AUDIO &&
  1217. track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1218. av_log(matroska->ctx, AV_LOG_INFO,
  1219. "Unknown or unsupported track type %"PRIu64"\n",
  1220. track->type);
  1221. continue;
  1222. }
  1223. if (track->codec_id == NULL)
  1224. continue;
  1225. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1226. if (!track->default_duration && track->video.frame_rate > 0)
  1227. track->default_duration = 1000000000/track->video.frame_rate;
  1228. if (!track->video.display_width)
  1229. track->video.display_width = track->video.pixel_width;
  1230. if (!track->video.display_height)
  1231. track->video.display_height = track->video.pixel_height;
  1232. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1233. if (!track->audio.out_samplerate)
  1234. track->audio.out_samplerate = track->audio.samplerate;
  1235. }
  1236. if (encodings_list->nb_elem > 1) {
  1237. av_log(matroska->ctx, AV_LOG_ERROR,
  1238. "Multiple combined encodings not supported");
  1239. } else if (encodings_list->nb_elem == 1) {
  1240. if (encodings[0].type ||
  1241. (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
  1242. #if CONFIG_ZLIB
  1243. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1244. #endif
  1245. #if CONFIG_BZLIB
  1246. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1247. #endif
  1248. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
  1249. encodings[0].scope = 0;
  1250. av_log(matroska->ctx, AV_LOG_ERROR,
  1251. "Unsupported encoding type");
  1252. } else if (track->codec_priv.size && encodings[0].scope&2) {
  1253. uint8_t *codec_priv = track->codec_priv.data;
  1254. int offset = matroska_decode_buffer(&track->codec_priv.data,
  1255. &track->codec_priv.size,
  1256. track);
  1257. if (offset < 0) {
  1258. track->codec_priv.data = NULL;
  1259. track->codec_priv.size = 0;
  1260. av_log(matroska->ctx, AV_LOG_ERROR,
  1261. "Failed to decode codec private data\n");
  1262. } else if (offset > 0) {
  1263. track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
  1264. memcpy(track->codec_priv.data,
  1265. encodings[0].compression.settings.data, offset);
  1266. memcpy(track->codec_priv.data+offset, codec_priv,
  1267. track->codec_priv.size);
  1268. track->codec_priv.size += offset;
  1269. }
  1270. if (codec_priv != track->codec_priv.data)
  1271. av_free(codec_priv);
  1272. }
  1273. }
  1274. for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
  1275. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  1276. strlen(ff_mkv_codec_tags[j].str))){
  1277. codec_id= ff_mkv_codec_tags[j].id;
  1278. break;
  1279. }
  1280. }
  1281. st = track->stream = avformat_new_stream(s, NULL);
  1282. if (st == NULL)
  1283. return AVERROR(ENOMEM);
  1284. if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
  1285. && track->codec_priv.size >= 40
  1286. && track->codec_priv.data != NULL) {
  1287. track->ms_compat = 1;
  1288. track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
  1289. codec_id = ff_codec_get_id(ff_codec_bmp_tags, track->video.fourcc);
  1290. extradata_offset = 40;
  1291. } else if (!strcmp(track->codec_id, "A_MS/ACM")
  1292. && track->codec_priv.size >= 14
  1293. && track->codec_priv.data != NULL) {
  1294. int ret;
  1295. ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size,
  1296. AVIO_FLAG_READ, NULL, NULL, NULL, NULL);
  1297. ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size);
  1298. if (ret < 0)
  1299. return ret;
  1300. codec_id = st->codec->codec_id;
  1301. extradata_offset = FFMIN(track->codec_priv.size, 18);
  1302. } else if (!strcmp(track->codec_id, "V_QUICKTIME")
  1303. && (track->codec_priv.size >= 86)
  1304. && (track->codec_priv.data != NULL)) {
  1305. track->video.fourcc = AV_RL32(track->codec_priv.data);
  1306. codec_id=ff_codec_get_id(ff_codec_movvideo_tags, track->video.fourcc);
  1307. } else if (codec_id == CODEC_ID_PCM_S16BE) {
  1308. switch (track->audio.bitdepth) {
  1309. case 8: codec_id = CODEC_ID_PCM_U8; break;
  1310. case 24: codec_id = CODEC_ID_PCM_S24BE; break;
  1311. case 32: codec_id = CODEC_ID_PCM_S32BE; break;
  1312. }
  1313. } else if (codec_id == CODEC_ID_PCM_S16LE) {
  1314. switch (track->audio.bitdepth) {
  1315. case 8: codec_id = CODEC_ID_PCM_U8; break;
  1316. case 24: codec_id = CODEC_ID_PCM_S24LE; break;
  1317. case 32: codec_id = CODEC_ID_PCM_S32LE; break;
  1318. }
  1319. } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
  1320. codec_id = CODEC_ID_PCM_F64LE;
  1321. } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
  1322. int profile = matroska_aac_profile(track->codec_id);
  1323. int sri = matroska_aac_sri(track->audio.samplerate);
  1324. extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
  1325. if (extradata == NULL)
  1326. return AVERROR(ENOMEM);
  1327. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  1328. extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
  1329. if (strstr(track->codec_id, "SBR")) {
  1330. sri = matroska_aac_sri(track->audio.out_samplerate);
  1331. extradata[2] = 0x56;
  1332. extradata[3] = 0xE5;
  1333. extradata[4] = 0x80 | (sri<<3);
  1334. extradata_size = 5;
  1335. } else
  1336. extradata_size = 2;
  1337. } else if (codec_id == CODEC_ID_TTA) {
  1338. extradata_size = 30;
  1339. extradata = av_mallocz(extradata_size);
  1340. if (extradata == NULL)
  1341. return AVERROR(ENOMEM);
  1342. ffio_init_context(&b, extradata, extradata_size, 1,
  1343. NULL, NULL, NULL, NULL);
  1344. avio_write(&b, "TTA1", 4);
  1345. avio_wl16(&b, 1);
  1346. avio_wl16(&b, track->audio.channels);
  1347. avio_wl16(&b, track->audio.bitdepth);
  1348. avio_wl32(&b, track->audio.out_samplerate);
  1349. avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate);
  1350. } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
  1351. codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
  1352. extradata_offset = 26;
  1353. } else if (codec_id == CODEC_ID_RA_144) {
  1354. track->audio.out_samplerate = 8000;
  1355. track->audio.channels = 1;
  1356. } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
  1357. codec_id == CODEC_ID_ATRAC3 || codec_id == CODEC_ID_SIPR) {
  1358. int flavor;
  1359. ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size,
  1360. 0, NULL, NULL, NULL, NULL);
  1361. avio_skip(&b, 22);
  1362. flavor = avio_rb16(&b);
  1363. track->audio.coded_framesize = avio_rb32(&b);
  1364. avio_skip(&b, 12);
  1365. track->audio.sub_packet_h = avio_rb16(&b);
  1366. track->audio.frame_size = avio_rb16(&b);
  1367. track->audio.sub_packet_size = avio_rb16(&b);
  1368. track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
  1369. if (codec_id == CODEC_ID_RA_288) {
  1370. st->codec->block_align = track->audio.coded_framesize;
  1371. track->codec_priv.size = 0;
  1372. } else {
  1373. if (codec_id == CODEC_ID_SIPR && flavor < 4) {
  1374. const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
  1375. track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
  1376. st->codec->bit_rate = sipr_bit_rate[flavor];
  1377. }
  1378. st->codec->block_align = track->audio.sub_packet_size;
  1379. extradata_offset = 78;
  1380. }
  1381. }
  1382. track->codec_priv.size -= extradata_offset;
  1383. if (codec_id == CODEC_ID_NONE)
  1384. av_log(matroska->ctx, AV_LOG_INFO,
  1385. "Unknown/unsupported CodecID %s.\n", track->codec_id);
  1386. if (track->time_scale < 0.01)
  1387. track->time_scale = 1.0;
  1388. avpriv_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  1389. st->codec->codec_id = codec_id;
  1390. st->start_time = 0;
  1391. if (strcmp(track->language, "und"))
  1392. av_dict_set(&st->metadata, "language", track->language, 0);
  1393. av_dict_set(&st->metadata, "title", track->name, 0);
  1394. if (track->flag_default)
  1395. st->disposition |= AV_DISPOSITION_DEFAULT;
  1396. if (track->flag_forced)
  1397. st->disposition |= AV_DISPOSITION_FORCED;
  1398. if (!st->codec->extradata) {
  1399. if(extradata){
  1400. st->codec->extradata = extradata;
  1401. st->codec->extradata_size = extradata_size;
  1402. } else if(track->codec_priv.data && track->codec_priv.size > 0){
  1403. st->codec->extradata = av_mallocz(track->codec_priv.size +
  1404. FF_INPUT_BUFFER_PADDING_SIZE);
  1405. if(st->codec->extradata == NULL)
  1406. return AVERROR(ENOMEM);
  1407. st->codec->extradata_size = track->codec_priv.size;
  1408. memcpy(st->codec->extradata,
  1409. track->codec_priv.data + extradata_offset,
  1410. track->codec_priv.size);
  1411. }
  1412. }
  1413. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1414. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1415. st->codec->codec_tag = track->video.fourcc;
  1416. st->codec->width = track->video.pixel_width;
  1417. st->codec->height = track->video.pixel_height;
  1418. av_reduce(&st->sample_aspect_ratio.num,
  1419. &st->sample_aspect_ratio.den,
  1420. st->codec->height * track->video.display_width,
  1421. st->codec-> width * track->video.display_height,
  1422. 255);
  1423. if (st->codec->codec_id != CODEC_ID_H264)
  1424. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1425. if (track->default_duration) {
  1426. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1427. 1000000000, track->default_duration, 30000);
  1428. st->avg_frame_rate = st->r_frame_rate;
  1429. }
  1430. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1431. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1432. st->codec->sample_rate = track->audio.out_samplerate;
  1433. st->codec->channels = track->audio.channels;
  1434. if (st->codec->codec_id != CODEC_ID_AAC)
  1435. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1436. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  1437. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1438. if (st->codec->codec_id == CODEC_ID_SSA)
  1439. matroska->contains_ssa = 1;
  1440. }
  1441. }
  1442. attachements = attachements_list->elem;
  1443. for (j=0; j<attachements_list->nb_elem; j++) {
  1444. if (!(attachements[j].filename && attachements[j].mime &&
  1445. attachements[j].bin.data && attachements[j].bin.size > 0)) {
  1446. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1447. } else {
  1448. AVStream *st = avformat_new_stream(s, NULL);
  1449. if (st == NULL)
  1450. break;
  1451. av_dict_set(&st->metadata, "filename",attachements[j].filename, 0);
  1452. av_dict_set(&st->metadata, "mimetype", attachements[j].mime, 0);
  1453. st->codec->codec_id = CODEC_ID_NONE;
  1454. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  1455. st->codec->extradata = av_malloc(attachements[j].bin.size);
  1456. if(st->codec->extradata == NULL)
  1457. break;
  1458. st->codec->extradata_size = attachements[j].bin.size;
  1459. memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
  1460. for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
  1461. if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
  1462. strlen(ff_mkv_mime_tags[i].str))) {
  1463. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1464. break;
  1465. }
  1466. }
  1467. attachements[j].stream = st;
  1468. }
  1469. }
  1470. chapters = chapters_list->elem;
  1471. for (i=0; i<chapters_list->nb_elem; i++)
  1472. if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
  1473. && (max_start==0 || chapters[i].start > max_start)) {
  1474. chapters[i].chapter =
  1475. avpriv_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
  1476. chapters[i].start, chapters[i].end,
  1477. chapters[i].title);
  1478. av_dict_set(&chapters[i].chapter->metadata,
  1479. "title", chapters[i].title, 0);
  1480. max_start = chapters[i].start;
  1481. }
  1482. matroska_convert_tags(s);
  1483. return 0;
  1484. }
  1485. /*
  1486. * Put one packet in an application-supplied AVPacket struct.
  1487. * Returns 0 on success or -1 on failure.
  1488. */
  1489. static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
  1490. AVPacket *pkt)
  1491. {
  1492. if (matroska->num_packets > 0) {
  1493. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  1494. av_free(matroska->packets[0]);
  1495. if (matroska->num_packets > 1) {
  1496. void *newpackets;
  1497. memmove(&matroska->packets[0], &matroska->packets[1],
  1498. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1499. newpackets = av_realloc(matroska->packets,
  1500. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1501. if (newpackets)
  1502. matroska->packets = newpackets;
  1503. } else {
  1504. av_freep(&matroska->packets);
  1505. matroska->prev_pkt = NULL;
  1506. }
  1507. matroska->num_packets--;
  1508. return 0;
  1509. }
  1510. return -1;
  1511. }
  1512. /*
  1513. * Free all packets in our internal queue.
  1514. */
  1515. static void matroska_clear_queue(MatroskaDemuxContext *matroska)
  1516. {
  1517. if (matroska->packets) {
  1518. int n;
  1519. for (n = 0; n < matroska->num_packets; n++) {
  1520. av_free_packet(matroska->packets[n]);
  1521. av_free(matroska->packets[n]);
  1522. }
  1523. av_freep(&matroska->packets);
  1524. matroska->num_packets = 0;
  1525. }
  1526. }
  1527. static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
  1528. int size, int64_t pos, uint64_t cluster_time,
  1529. uint64_t duration, int is_keyframe,
  1530. int64_t cluster_pos)
  1531. {
  1532. uint64_t timecode = AV_NOPTS_VALUE;
  1533. MatroskaTrack *track;
  1534. int res = 0;
  1535. AVStream *st;
  1536. AVPacket *pkt;
  1537. int16_t block_time;
  1538. uint32_t *lace_size = NULL;
  1539. int n, flags, laces = 0;
  1540. uint64_t num;
  1541. if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
  1542. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  1543. return n;
  1544. }
  1545. data += n;
  1546. size -= n;
  1547. track = matroska_find_track_by_num(matroska, num);
  1548. if (!track || !track->stream) {
  1549. av_log(matroska->ctx, AV_LOG_INFO,
  1550. "Invalid stream %"PRIu64" or size %u\n", num, size);
  1551. return AVERROR_INVALIDDATA;
  1552. } else if (size <= 3)
  1553. return 0;
  1554. st = track->stream;
  1555. if (st->discard >= AVDISCARD_ALL)
  1556. return res;
  1557. if (duration == AV_NOPTS_VALUE)
  1558. duration = track->default_duration / matroska->time_scale;
  1559. block_time = AV_RB16(data);
  1560. data += 2;
  1561. flags = *data++;
  1562. size -= 3;
  1563. if (is_keyframe == -1)
  1564. is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
  1565. if (cluster_time != (uint64_t)-1
  1566. && (block_time >= 0 || cluster_time >= -block_time)) {
  1567. timecode = cluster_time + block_time;
  1568. if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
  1569. && timecode < track->end_timecode)
  1570. is_keyframe = 0; /* overlapping subtitles are not key frame */
  1571. if (is_keyframe)
  1572. av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
  1573. track->end_timecode = FFMAX(track->end_timecode, timecode+duration);
  1574. }
  1575. if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1576. if (!is_keyframe || timecode < matroska->skip_to_timecode)
  1577. return res;
  1578. matroska->skip_to_keyframe = 0;
  1579. }
  1580. switch ((flags & 0x06) >> 1) {
  1581. case 0x0: /* no lacing */
  1582. laces = 1;
  1583. lace_size = av_mallocz(sizeof(int));
  1584. lace_size[0] = size;
  1585. break;
  1586. case 0x1: /* Xiph lacing */
  1587. case 0x2: /* fixed-size lacing */
  1588. case 0x3: /* EBML lacing */
  1589. assert(size>0); // size <=3 is checked before size-=3 above
  1590. laces = (*data) + 1;
  1591. data += 1;
  1592. size -= 1;
  1593. lace_size = av_mallocz(laces * sizeof(int));
  1594. switch ((flags & 0x06) >> 1) {
  1595. case 0x1: /* Xiph lacing */ {
  1596. uint8_t temp;
  1597. uint32_t total = 0;
  1598. for (n = 0; res == 0 && n < laces - 1; n++) {
  1599. while (1) {
  1600. if (size == 0) {
  1601. res = -1;
  1602. break;
  1603. }
  1604. temp = *data;
  1605. lace_size[n] += temp;
  1606. data += 1;
  1607. size -= 1;
  1608. if (temp != 0xff)
  1609. break;
  1610. }
  1611. total += lace_size[n];
  1612. }
  1613. lace_size[n] = size - total;
  1614. break;
  1615. }
  1616. case 0x2: /* fixed-size lacing */
  1617. for (n = 0; n < laces; n++)
  1618. lace_size[n] = size / laces;
  1619. break;
  1620. case 0x3: /* EBML lacing */ {
  1621. uint32_t total;
  1622. n = matroska_ebmlnum_uint(matroska, data, size, &num);
  1623. if (n < 0) {
  1624. av_log(matroska->ctx, AV_LOG_INFO,
  1625. "EBML block data error\n");
  1626. break;
  1627. }
  1628. data += n;
  1629. size -= n;
  1630. total = lace_size[0] = num;
  1631. for (n = 1; res == 0 && n < laces - 1; n++) {
  1632. int64_t snum;
  1633. int r;
  1634. r = matroska_ebmlnum_sint(matroska, data, size, &snum);
  1635. if (r < 0) {
  1636. av_log(matroska->ctx, AV_LOG_INFO,
  1637. "EBML block data error\n");
  1638. break;
  1639. }
  1640. data += r;
  1641. size -= r;
  1642. lace_size[n] = lace_size[n - 1] + snum;
  1643. total += lace_size[n];
  1644. }
  1645. lace_size[laces - 1] = size - total;
  1646. break;
  1647. }
  1648. }
  1649. break;
  1650. }
  1651. if (res == 0) {
  1652. for (n = 0; n < laces; n++) {
  1653. if ((st->codec->codec_id == CODEC_ID_RA_288 ||
  1654. st->codec->codec_id == CODEC_ID_COOK ||
  1655. st->codec->codec_id == CODEC_ID_SIPR ||
  1656. st->codec->codec_id == CODEC_ID_ATRAC3) &&
  1657. st->codec->block_align && track->audio.sub_packet_size) {
  1658. int a = st->codec->block_align;
  1659. int sps = track->audio.sub_packet_size;
  1660. int cfs = track->audio.coded_framesize;
  1661. int h = track->audio.sub_packet_h;
  1662. int y = track->audio.sub_packet_cnt;
  1663. int w = track->audio.frame_size;
  1664. int x;
  1665. if (!track->audio.pkt_cnt) {
  1666. if (track->audio.sub_packet_cnt == 0)
  1667. track->audio.buf_timecode = timecode;
  1668. if (st->codec->codec_id == CODEC_ID_RA_288) {
  1669. if (size < cfs * h / 2) {
  1670. av_log(matroska->ctx, AV_LOG_ERROR,
  1671. "Corrupt int4 RM-style audio packet size\n");
  1672. res = AVERROR_INVALIDDATA;
  1673. goto end;
  1674. }
  1675. for (x=0; x<h/2; x++)
  1676. memcpy(track->audio.buf+x*2*w+y*cfs,
  1677. data+x*cfs, cfs);
  1678. } else if (st->codec->codec_id == CODEC_ID_SIPR) {
  1679. if (size < w) {
  1680. av_log(matroska->ctx, AV_LOG_ERROR,
  1681. "Corrupt sipr RM-style audio packet size\n");
  1682. res = AVERROR_INVALIDDATA;
  1683. goto end;
  1684. }
  1685. memcpy(track->audio.buf + y*w, data, w);
  1686. } else {
  1687. if (size < sps * w / sps) {
  1688. av_log(matroska->ctx, AV_LOG_ERROR,
  1689. "Corrupt generic RM-style audio packet size\n");
  1690. res = AVERROR_INVALIDDATA;
  1691. goto end;
  1692. }
  1693. for (x=0; x<w/sps; x++)
  1694. memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  1695. }
  1696. if (++track->audio.sub_packet_cnt >= h) {
  1697. if (st->codec->codec_id == CODEC_ID_SIPR)
  1698. ff_rm_reorder_sipr_data(track->audio.buf, h, w);
  1699. track->audio.sub_packet_cnt = 0;
  1700. track->audio.pkt_cnt = h*w / a;
  1701. }
  1702. }
  1703. while (track->audio.pkt_cnt) {
  1704. pkt = av_mallocz(sizeof(AVPacket));
  1705. av_new_packet(pkt, a);
  1706. memcpy(pkt->data, track->audio.buf
  1707. + a * (h*w / a - track->audio.pkt_cnt--), a);
  1708. pkt->pts = track->audio.buf_timecode;
  1709. track->audio.buf_timecode = AV_NOPTS_VALUE;
  1710. pkt->pos = pos;
  1711. pkt->stream_index = st->index;
  1712. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1713. }
  1714. } else {
  1715. MatroskaTrackEncoding *encodings = track->encodings.elem;
  1716. int offset = 0, pkt_size = lace_size[n];
  1717. uint8_t *pkt_data = data;
  1718. if (pkt_size > size) {
  1719. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
  1720. break;
  1721. }
  1722. if (encodings && encodings->scope & 1) {
  1723. offset = matroska_decode_buffer(&pkt_data,&pkt_size, track);
  1724. if (offset < 0)
  1725. continue;
  1726. }
  1727. pkt = av_mallocz(sizeof(AVPacket));
  1728. /* XXX: prevent data copy... */
  1729. if (av_new_packet(pkt, pkt_size+offset) < 0) {
  1730. av_free(pkt);
  1731. res = AVERROR(ENOMEM);
  1732. break;
  1733. }
  1734. if (offset)
  1735. memcpy (pkt->data, encodings->compression.settings.data, offset);
  1736. memcpy (pkt->data+offset, pkt_data, pkt_size);
  1737. if (pkt_data != data)
  1738. av_free(pkt_data);
  1739. if (n == 0)
  1740. pkt->flags = is_keyframe;
  1741. pkt->stream_index = st->index;
  1742. if (track->ms_compat)
  1743. pkt->dts = timecode;
  1744. else
  1745. pkt->pts = timecode;
  1746. pkt->pos = pos;
  1747. if (st->codec->codec_id == CODEC_ID_TEXT)
  1748. pkt->convergence_duration = duration;
  1749. else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE)
  1750. pkt->duration = duration;
  1751. if (st->codec->codec_id == CODEC_ID_SSA)
  1752. matroska_fix_ass_packet(matroska, pkt, duration);
  1753. if (matroska->prev_pkt &&
  1754. timecode != AV_NOPTS_VALUE &&
  1755. matroska->prev_pkt->pts == timecode &&
  1756. matroska->prev_pkt->stream_index == st->index &&
  1757. st->codec->codec_id == CODEC_ID_SSA)
  1758. matroska_merge_packets(matroska->prev_pkt, pkt);
  1759. else {
  1760. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1761. matroska->prev_pkt = pkt;
  1762. }
  1763. }
  1764. if (timecode != AV_NOPTS_VALUE)
  1765. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  1766. data += lace_size[n];
  1767. size -= lace_size[n];
  1768. }
  1769. }
  1770. end:
  1771. av_free(lace_size);
  1772. return res;
  1773. }
  1774. static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska)
  1775. {
  1776. EbmlList *blocks_list;
  1777. MatroskaBlock *blocks;
  1778. int i, res;
  1779. res = ebml_parse(matroska,
  1780. matroska_cluster_incremental_parsing,
  1781. &matroska->current_cluster);
  1782. if (res == 1) {
  1783. /* New Cluster */
  1784. if (matroska->current_cluster_pos)
  1785. ebml_level_end(matroska);
  1786. ebml_free(matroska_cluster, &matroska->current_cluster);
  1787. memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster));
  1788. matroska->current_cluster_num_blocks = 0;
  1789. matroska->current_cluster_pos = avio_tell(matroska->ctx->pb);
  1790. matroska->prev_pkt = NULL;
  1791. /* sizeof the ID which was already read */
  1792. if (matroska->current_id)
  1793. matroska->current_cluster_pos -= 4;
  1794. res = ebml_parse(matroska,
  1795. matroska_clusters_incremental,
  1796. &matroska->current_cluster);
  1797. /* Try parsing the block again. */
  1798. if (res == 1)
  1799. res = ebml_parse(matroska,
  1800. matroska_cluster_incremental_parsing,
  1801. &matroska->current_cluster);
  1802. }
  1803. if (!res &&
  1804. matroska->current_cluster_num_blocks <
  1805. matroska->current_cluster.blocks.nb_elem) {
  1806. blocks_list = &matroska->current_cluster.blocks;
  1807. blocks = blocks_list->elem;
  1808. matroska->current_cluster_num_blocks = blocks_list->nb_elem;
  1809. i = blocks_list->nb_elem - 1;
  1810. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  1811. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  1812. if (!blocks[i].non_simple)
  1813. blocks[i].duration = AV_NOPTS_VALUE;
  1814. res = matroska_parse_block(matroska,
  1815. blocks[i].bin.data, blocks[i].bin.size,
  1816. blocks[i].bin.pos,
  1817. matroska->current_cluster.timecode,
  1818. blocks[i].duration, is_keyframe,
  1819. matroska->current_cluster_pos);
  1820. }
  1821. }
  1822. if (res < 0) matroska->done = 1;
  1823. return res;
  1824. }
  1825. static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
  1826. {
  1827. MatroskaCluster cluster = { 0 };
  1828. EbmlList *blocks_list;
  1829. MatroskaBlock *blocks;
  1830. int i, res;
  1831. int64_t pos;
  1832. if (!matroska->contains_ssa)
  1833. return matroska_parse_cluster_incremental(matroska);
  1834. pos = avio_tell(matroska->ctx->pb);
  1835. matroska->prev_pkt = NULL;
  1836. if (matroska->current_id)
  1837. pos -= 4; /* sizeof the ID which was already read */
  1838. res = ebml_parse(matroska, matroska_clusters, &cluster);
  1839. blocks_list = &cluster.blocks;
  1840. blocks = blocks_list->elem;
  1841. for (i=0; i<blocks_list->nb_elem && !res; i++)
  1842. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  1843. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  1844. if (!blocks[i].non_simple)
  1845. blocks[i].duration = AV_NOPTS_VALUE;
  1846. res=matroska_parse_block(matroska,
  1847. blocks[i].bin.data, blocks[i].bin.size,
  1848. blocks[i].bin.pos, cluster.timecode,
  1849. blocks[i].duration, is_keyframe,
  1850. pos);
  1851. }
  1852. ebml_free(matroska_cluster, &cluster);
  1853. if (res < 0) matroska->done = 1;
  1854. return res;
  1855. }
  1856. static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
  1857. {
  1858. MatroskaDemuxContext *matroska = s->priv_data;
  1859. int ret = 0;
  1860. while (!ret && matroska_deliver_packet(matroska, pkt)) {
  1861. if (matroska->done)
  1862. return AVERROR_EOF;
  1863. ret = matroska_parse_cluster(matroska);
  1864. }
  1865. if (ret == AVERROR_INVALIDDATA) {
  1866. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  1867. return 0;
  1868. }
  1869. return ret;
  1870. }
  1871. static int matroska_read_seek(AVFormatContext *s, int stream_index,
  1872. int64_t timestamp, int flags)
  1873. {
  1874. MatroskaDemuxContext *matroska = s->priv_data;
  1875. MatroskaTrack *tracks = matroska->tracks.elem;
  1876. AVStream *st = s->streams[stream_index];
  1877. int i, index, index_sub, index_min;
  1878. /* Parse the CUES now since we need the index data to seek. */
  1879. if (matroska->cues_parsing_deferred) {
  1880. matroska_parse_cues(matroska);
  1881. matroska->cues_parsing_deferred = 0;
  1882. }
  1883. if (!st->nb_index_entries)
  1884. return 0;
  1885. timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
  1886. if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  1887. avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
  1888. matroska->current_id = 0;
  1889. while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  1890. matroska->prev_pkt = NULL;
  1891. matroska_clear_queue(matroska);
  1892. if (matroska_parse_cluster(matroska) < 0)
  1893. break;
  1894. }
  1895. }
  1896. matroska_clear_queue(matroska);
  1897. if (index < 0)
  1898. return 0;
  1899. index_min = index;
  1900. for (i=0; i < matroska->tracks.nb_elem; i++) {
  1901. tracks[i].audio.pkt_cnt = 0;
  1902. tracks[i].audio.sub_packet_cnt = 0;
  1903. tracks[i].audio.buf_timecode = AV_NOPTS_VALUE;
  1904. tracks[i].end_timecode = 0;
  1905. if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
  1906. && !tracks[i].stream->discard != AVDISCARD_ALL) {
  1907. index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
  1908. if (index_sub >= 0
  1909. && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
  1910. && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
  1911. index_min = index_sub;
  1912. }
  1913. }
  1914. avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
  1915. matroska->current_id = 0;
  1916. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  1917. matroska->skip_to_timecode = st->index_entries[index].timestamp;
  1918. matroska->done = 0;
  1919. ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
  1920. return 0;
  1921. }
  1922. static int matroska_read_close(AVFormatContext *s)
  1923. {
  1924. MatroskaDemuxContext *matroska = s->priv_data;
  1925. MatroskaTrack *tracks = matroska->tracks.elem;
  1926. int n;
  1927. matroska_clear_queue(matroska);
  1928. for (n=0; n < matroska->tracks.nb_elem; n++)
  1929. if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
  1930. av_free(tracks[n].audio.buf);
  1931. ebml_free(matroska_cluster, &matroska->current_cluster);
  1932. ebml_free(matroska_segment, matroska);
  1933. return 0;
  1934. }
  1935. AVInputFormat ff_matroska_demuxer = {
  1936. .name = "matroska,webm",
  1937. .long_name = NULL_IF_CONFIG_SMALL("Matroska/WebM file format"),
  1938. .priv_data_size = sizeof(MatroskaDemuxContext),
  1939. .read_probe = matroska_probe,
  1940. .read_header = matroska_read_header,
  1941. .read_packet = matroska_read_packet,
  1942. .read_close = matroska_read_close,
  1943. .read_seek = matroska_read_seek,
  1944. };