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.

2171 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. return pb->error ? pb->error : AVERROR(EIO);
  526. }
  527. return AVERROR_EOF;
  528. }
  529. /* get the length of the EBML number */
  530. read = 8 - ff_log2_tab[total];
  531. if (read > max_size) {
  532. int64_t pos = avio_tell(pb) - 1;
  533. av_log(matroska->ctx, AV_LOG_ERROR,
  534. "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
  535. (uint8_t) total, pos, pos);
  536. return AVERROR_INVALIDDATA;
  537. }
  538. /* read out length */
  539. total ^= 1 << ff_log2_tab[total];
  540. while (n++ < read)
  541. total = (total << 8) | avio_r8(pb);
  542. *number = total;
  543. return read;
  544. }
  545. /**
  546. * Read a EBML length value.
  547. * This needs special handling for the "unknown length" case which has multiple
  548. * encodings.
  549. */
  550. static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
  551. uint64_t *number)
  552. {
  553. int res = ebml_read_num(matroska, pb, 8, number);
  554. if (res > 0 && *number + 1 == 1ULL << (7 * res))
  555. *number = 0xffffffffffffffULL;
  556. return res;
  557. }
  558. /*
  559. * Read the next element as an unsigned int.
  560. * 0 is success, < 0 is failure.
  561. */
  562. static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
  563. {
  564. int n = 0;
  565. if (size > 8)
  566. return AVERROR_INVALIDDATA;
  567. /* big-endian ordering; build up number */
  568. *num = 0;
  569. while (n++ < size)
  570. *num = (*num << 8) | avio_r8(pb);
  571. return 0;
  572. }
  573. /*
  574. * Read the next element as a float.
  575. * 0 is success, < 0 is failure.
  576. */
  577. static int ebml_read_float(AVIOContext *pb, int size, double *num)
  578. {
  579. if (size == 0) {
  580. *num = 0;
  581. } else if (size == 4) {
  582. *num = av_int2float(avio_rb32(pb));
  583. } else if (size == 8){
  584. *num = av_int2double(avio_rb64(pb));
  585. } else
  586. return AVERROR_INVALIDDATA;
  587. return 0;
  588. }
  589. /*
  590. * Read the next element as an ASCII string.
  591. * 0 is success, < 0 is failure.
  592. */
  593. static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
  594. {
  595. char *res;
  596. /* EBML strings are usually not 0-terminated, so we allocate one
  597. * byte more, read the string and NULL-terminate it ourselves. */
  598. if (!(res = av_malloc(size + 1)))
  599. return AVERROR(ENOMEM);
  600. if (avio_read(pb, (uint8_t *) res, size) != size) {
  601. av_free(res);
  602. return AVERROR(EIO);
  603. }
  604. (res)[size] = '\0';
  605. av_free(*str);
  606. *str = res;
  607. return 0;
  608. }
  609. /*
  610. * Read the next element as binary data.
  611. * 0 is success, < 0 is failure.
  612. */
  613. static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
  614. {
  615. av_free(bin->data);
  616. if (!(bin->data = av_malloc(length)))
  617. return AVERROR(ENOMEM);
  618. bin->size = length;
  619. bin->pos = avio_tell(pb);
  620. if (avio_read(pb, bin->data, length) != length) {
  621. av_freep(&bin->data);
  622. return AVERROR(EIO);
  623. }
  624. return 0;
  625. }
  626. /*
  627. * Read the next element, but only the header. The contents
  628. * are supposed to be sub-elements which can be read separately.
  629. * 0 is success, < 0 is failure.
  630. */
  631. static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
  632. {
  633. AVIOContext *pb = matroska->ctx->pb;
  634. MatroskaLevel *level;
  635. if (matroska->num_levels >= EBML_MAX_DEPTH) {
  636. av_log(matroska->ctx, AV_LOG_ERROR,
  637. "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
  638. return AVERROR(ENOSYS);
  639. }
  640. level = &matroska->levels[matroska->num_levels++];
  641. level->start = avio_tell(pb);
  642. level->length = length;
  643. return 0;
  644. }
  645. /*
  646. * Read signed/unsigned "EBML" numbers.
  647. * Return: number of bytes processed, < 0 on error
  648. */
  649. static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
  650. uint8_t *data, uint32_t size, uint64_t *num)
  651. {
  652. AVIOContext pb;
  653. ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
  654. return ebml_read_num(matroska, &pb, FFMIN(size, 8), num);
  655. }
  656. /*
  657. * Same as above, but signed.
  658. */
  659. static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
  660. uint8_t *data, uint32_t size, int64_t *num)
  661. {
  662. uint64_t unum;
  663. int res;
  664. /* read as unsigned number first */
  665. if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
  666. return res;
  667. /* make signed (weird way) */
  668. *num = unum - ((1LL << (7*res - 1)) - 1);
  669. return res;
  670. }
  671. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  672. EbmlSyntax *syntax, void *data);
  673. static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  674. uint32_t id, void *data)
  675. {
  676. int i;
  677. for (i=0; syntax[i].id; i++)
  678. if (id == syntax[i].id)
  679. break;
  680. if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
  681. matroska->num_levels > 0 &&
  682. matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff)
  683. return 0; // we reached the end of an unknown size cluster
  684. if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
  685. av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
  686. if (matroska->ctx->error_recognition & AV_EF_EXPLODE)
  687. return AVERROR_INVALIDDATA;
  688. }
  689. return ebml_parse_elem(matroska, &syntax[i], data);
  690. }
  691. static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  692. void *data)
  693. {
  694. if (!matroska->current_id) {
  695. uint64_t id;
  696. int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
  697. if (res < 0)
  698. return res;
  699. matroska->current_id = id | 1 << 7*res;
  700. }
  701. return ebml_parse_id(matroska, syntax, matroska->current_id, data);
  702. }
  703. static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  704. void *data)
  705. {
  706. int i, res = 0;
  707. for (i=0; syntax[i].id; i++)
  708. switch (syntax[i].type) {
  709. case EBML_UINT:
  710. *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
  711. break;
  712. case EBML_FLOAT:
  713. *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
  714. break;
  715. case EBML_STR:
  716. case EBML_UTF8:
  717. *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
  718. break;
  719. }
  720. while (!res && !ebml_level_end(matroska))
  721. res = ebml_parse(matroska, syntax, data);
  722. return res;
  723. }
  724. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  725. EbmlSyntax *syntax, void *data)
  726. {
  727. static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
  728. [EBML_UINT] = 8,
  729. [EBML_FLOAT] = 8,
  730. // max. 16 MB for strings
  731. [EBML_STR] = 0x1000000,
  732. [EBML_UTF8] = 0x1000000,
  733. // max. 256 MB for binary data
  734. [EBML_BIN] = 0x10000000,
  735. // no limits for anything else
  736. };
  737. AVIOContext *pb = matroska->ctx->pb;
  738. uint32_t id = syntax->id;
  739. uint64_t length;
  740. int res;
  741. void *newelem;
  742. data = (char *)data + syntax->data_offset;
  743. if (syntax->list_elem_size) {
  744. EbmlList *list = data;
  745. newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
  746. if (!newelem)
  747. return AVERROR(ENOMEM);
  748. list->elem = newelem;
  749. data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
  750. memset(data, 0, syntax->list_elem_size);
  751. list->nb_elem++;
  752. }
  753. if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {
  754. matroska->current_id = 0;
  755. if ((res = ebml_read_length(matroska, pb, &length)) < 0)
  756. return res;
  757. if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
  758. av_log(matroska->ctx, AV_LOG_ERROR,
  759. "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",
  760. length, max_lengths[syntax->type], syntax->type);
  761. return AVERROR_INVALIDDATA;
  762. }
  763. }
  764. switch (syntax->type) {
  765. case EBML_UINT: res = ebml_read_uint (pb, length, data); break;
  766. case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;
  767. case EBML_STR:
  768. case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;
  769. case EBML_BIN: res = ebml_read_binary(pb, length, data); break;
  770. case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)
  771. return res;
  772. if (id == MATROSKA_ID_SEGMENT)
  773. matroska->segment_start = avio_tell(matroska->ctx->pb);
  774. return ebml_parse_nest(matroska, syntax->def.n, data);
  775. case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);
  776. case EBML_STOP: return 1;
  777. default: return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;
  778. }
  779. if (res == AVERROR_INVALIDDATA)
  780. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
  781. else if (res == AVERROR(EIO))
  782. av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
  783. return res;
  784. }
  785. static void ebml_free(EbmlSyntax *syntax, void *data)
  786. {
  787. int i, j;
  788. for (i=0; syntax[i].id; i++) {
  789. void *data_off = (char *)data + syntax[i].data_offset;
  790. switch (syntax[i].type) {
  791. case EBML_STR:
  792. case EBML_UTF8: av_freep(data_off); break;
  793. case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
  794. case EBML_NEST:
  795. if (syntax[i].list_elem_size) {
  796. EbmlList *list = data_off;
  797. char *ptr = list->elem;
  798. for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
  799. ebml_free(syntax[i].def.n, ptr);
  800. av_free(list->elem);
  801. } else
  802. ebml_free(syntax[i].def.n, data_off);
  803. default: break;
  804. }
  805. }
  806. }
  807. /*
  808. * Autodetecting...
  809. */
  810. static int matroska_probe(AVProbeData *p)
  811. {
  812. uint64_t total = 0;
  813. int len_mask = 0x80, size = 1, n = 1, i;
  814. /* EBML header? */
  815. if (AV_RB32(p->buf) != EBML_ID_HEADER)
  816. return 0;
  817. /* length of header */
  818. total = p->buf[4];
  819. while (size <= 8 && !(total & len_mask)) {
  820. size++;
  821. len_mask >>= 1;
  822. }
  823. if (size > 8)
  824. return 0;
  825. total &= (len_mask - 1);
  826. while (n < size)
  827. total = (total << 8) | p->buf[4 + n++];
  828. /* Does the probe data contain the whole header? */
  829. if (p->buf_size < 4 + size + total)
  830. return 0;
  831. /* The header should contain a known document type. For now,
  832. * we don't parse the whole header but simply check for the
  833. * availability of that array of characters inside the header.
  834. * Not fully fool-proof, but good enough. */
  835. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
  836. int probelen = strlen(matroska_doctypes[i]);
  837. if (total < probelen)
  838. continue;
  839. for (n = 4+size; n <= 4+size+total-probelen; n++)
  840. if (!memcmp(p->buf+n, matroska_doctypes[i], probelen))
  841. return AVPROBE_SCORE_MAX;
  842. }
  843. // probably valid EBML header but no recognized doctype
  844. return AVPROBE_SCORE_MAX/2;
  845. }
  846. static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
  847. int num)
  848. {
  849. MatroskaTrack *tracks = matroska->tracks.elem;
  850. int i;
  851. for (i=0; i < matroska->tracks.nb_elem; i++)
  852. if (tracks[i].num == num)
  853. return &tracks[i];
  854. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
  855. return NULL;
  856. }
  857. static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
  858. MatroskaTrack *track)
  859. {
  860. MatroskaTrackEncoding *encodings = track->encodings.elem;
  861. uint8_t* data = *buf;
  862. int isize = *buf_size;
  863. uint8_t* pkt_data = NULL;
  864. uint8_t av_unused *newpktdata;
  865. int pkt_size = isize;
  866. int result = 0;
  867. int olen;
  868. if (pkt_size >= 10000000)
  869. return -1;
  870. switch (encodings[0].compression.algo) {
  871. case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
  872. return encodings[0].compression.settings.size;
  873. case MATROSKA_TRACK_ENCODING_COMP_LZO:
  874. do {
  875. olen = pkt_size *= 3;
  876. pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING);
  877. result = av_lzo1x_decode(pkt_data, &olen, data, &isize);
  878. } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000);
  879. if (result)
  880. goto failed;
  881. pkt_size -= olen;
  882. break;
  883. #if CONFIG_ZLIB
  884. case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
  885. z_stream zstream = {0};
  886. if (inflateInit(&zstream) != Z_OK)
  887. return -1;
  888. zstream.next_in = data;
  889. zstream.avail_in = isize;
  890. do {
  891. pkt_size *= 3;
  892. newpktdata = av_realloc(pkt_data, pkt_size);
  893. if (!newpktdata) {
  894. inflateEnd(&zstream);
  895. goto failed;
  896. }
  897. pkt_data = newpktdata;
  898. zstream.avail_out = pkt_size - zstream.total_out;
  899. zstream.next_out = pkt_data + zstream.total_out;
  900. result = inflate(&zstream, Z_NO_FLUSH);
  901. } while (result==Z_OK && pkt_size<10000000);
  902. pkt_size = zstream.total_out;
  903. inflateEnd(&zstream);
  904. if (result != Z_STREAM_END)
  905. goto failed;
  906. break;
  907. }
  908. #endif
  909. #if CONFIG_BZLIB
  910. case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
  911. bz_stream bzstream = {0};
  912. if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
  913. return -1;
  914. bzstream.next_in = data;
  915. bzstream.avail_in = isize;
  916. do {
  917. pkt_size *= 3;
  918. newpktdata = av_realloc(pkt_data, pkt_size);
  919. if (!newpktdata) {
  920. BZ2_bzDecompressEnd(&bzstream);
  921. goto failed;
  922. }
  923. pkt_data = newpktdata;
  924. bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
  925. bzstream.next_out = pkt_data + bzstream.total_out_lo32;
  926. result = BZ2_bzDecompress(&bzstream);
  927. } while (result==BZ_OK && pkt_size<10000000);
  928. pkt_size = bzstream.total_out_lo32;
  929. BZ2_bzDecompressEnd(&bzstream);
  930. if (result != BZ_STREAM_END)
  931. goto failed;
  932. break;
  933. }
  934. #endif
  935. default:
  936. return -1;
  937. }
  938. *buf = pkt_data;
  939. *buf_size = pkt_size;
  940. return 0;
  941. failed:
  942. av_free(pkt_data);
  943. return -1;
  944. }
  945. static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
  946. AVPacket *pkt, uint64_t display_duration)
  947. {
  948. char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
  949. for (; *ptr!=',' && ptr<end-1; ptr++);
  950. if (*ptr == ',')
  951. layer = ++ptr;
  952. for (; *ptr!=',' && ptr<end-1; ptr++);
  953. if (*ptr == ',') {
  954. int64_t end_pts = pkt->pts + display_duration;
  955. int sc = matroska->time_scale * pkt->pts / 10000000;
  956. int ec = matroska->time_scale * end_pts / 10000000;
  957. int sh, sm, ss, eh, em, es, len;
  958. sh = sc/360000; sc -= 360000*sh;
  959. sm = sc/ 6000; sc -= 6000*sm;
  960. ss = sc/ 100; sc -= 100*ss;
  961. eh = ec/360000; ec -= 360000*eh;
  962. em = ec/ 6000; ec -= 6000*em;
  963. es = ec/ 100; ec -= 100*es;
  964. *ptr++ = '\0';
  965. len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
  966. if (!(line = av_malloc(len)))
  967. return;
  968. snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
  969. layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
  970. av_free(pkt->data);
  971. pkt->data = line;
  972. pkt->size = strlen(line);
  973. }
  974. }
  975. static int matroska_merge_packets(AVPacket *out, AVPacket *in)
  976. {
  977. void *newdata = av_realloc(out->data, out->size+in->size);
  978. if (!newdata)
  979. return AVERROR(ENOMEM);
  980. out->data = newdata;
  981. memcpy(out->data+out->size, in->data, in->size);
  982. out->size += in->size;
  983. av_destruct_packet(in);
  984. av_free(in);
  985. return 0;
  986. }
  987. static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
  988. AVDictionary **metadata, char *prefix)
  989. {
  990. MatroskaTag *tags = list->elem;
  991. char key[1024];
  992. int i;
  993. for (i=0; i < list->nb_elem; i++) {
  994. const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
  995. if (!tags[i].name) {
  996. av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
  997. continue;
  998. }
  999. if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
  1000. else av_strlcpy(key, tags[i].name, sizeof(key));
  1001. if (tags[i].def || !lang) {
  1002. av_dict_set(metadata, key, tags[i].string, 0);
  1003. if (tags[i].sub.nb_elem)
  1004. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  1005. }
  1006. if (lang) {
  1007. av_strlcat(key, "-", sizeof(key));
  1008. av_strlcat(key, lang, sizeof(key));
  1009. av_dict_set(metadata, key, tags[i].string, 0);
  1010. if (tags[i].sub.nb_elem)
  1011. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  1012. }
  1013. }
  1014. ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
  1015. }
  1016. static void matroska_convert_tags(AVFormatContext *s)
  1017. {
  1018. MatroskaDemuxContext *matroska = s->priv_data;
  1019. MatroskaTags *tags = matroska->tags.elem;
  1020. int i, j;
  1021. for (i=0; i < matroska->tags.nb_elem; i++) {
  1022. if (tags[i].target.attachuid) {
  1023. MatroskaAttachement *attachment = matroska->attachments.elem;
  1024. for (j=0; j<matroska->attachments.nb_elem; j++)
  1025. if (attachment[j].uid == tags[i].target.attachuid
  1026. && attachment[j].stream)
  1027. matroska_convert_tag(s, &tags[i].tag,
  1028. &attachment[j].stream->metadata, NULL);
  1029. } else if (tags[i].target.chapteruid) {
  1030. MatroskaChapter *chapter = matroska->chapters.elem;
  1031. for (j=0; j<matroska->chapters.nb_elem; j++)
  1032. if (chapter[j].uid == tags[i].target.chapteruid
  1033. && chapter[j].chapter)
  1034. matroska_convert_tag(s, &tags[i].tag,
  1035. &chapter[j].chapter->metadata, NULL);
  1036. } else if (tags[i].target.trackuid) {
  1037. MatroskaTrack *track = matroska->tracks.elem;
  1038. for (j=0; j<matroska->tracks.nb_elem; j++)
  1039. if (track[j].uid == tags[i].target.trackuid && track[j].stream)
  1040. matroska_convert_tag(s, &tags[i].tag,
  1041. &track[j].stream->metadata, NULL);
  1042. } else {
  1043. matroska_convert_tag(s, &tags[i].tag, &s->metadata,
  1044. tags[i].target.type);
  1045. }
  1046. }
  1047. }
  1048. static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx)
  1049. {
  1050. EbmlList *seekhead_list = &matroska->seekhead;
  1051. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1052. uint32_t level_up = matroska->level_up;
  1053. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1054. uint32_t saved_id = matroska->current_id;
  1055. MatroskaLevel level;
  1056. int64_t offset;
  1057. int ret = 0;
  1058. if (idx >= seekhead_list->nb_elem
  1059. || seekhead[idx].id == MATROSKA_ID_SEEKHEAD
  1060. || seekhead[idx].id == MATROSKA_ID_CLUSTER)
  1061. return 0;
  1062. /* seek */
  1063. offset = seekhead[idx].pos + matroska->segment_start;
  1064. if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) {
  1065. /* We don't want to lose our seekhead level, so we add
  1066. * a dummy. This is a crude hack. */
  1067. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1068. av_log(matroska->ctx, AV_LOG_INFO,
  1069. "Max EBML element depth (%d) reached, "
  1070. "cannot parse further.\n", EBML_MAX_DEPTH);
  1071. ret = AVERROR_INVALIDDATA;
  1072. } else {
  1073. level.start = 0;
  1074. level.length = (uint64_t)-1;
  1075. matroska->levels[matroska->num_levels] = level;
  1076. matroska->num_levels++;
  1077. matroska->current_id = 0;
  1078. ret = ebml_parse(matroska, matroska_segment, matroska);
  1079. /* remove dummy level */
  1080. while (matroska->num_levels) {
  1081. uint64_t length = matroska->levels[--matroska->num_levels].length;
  1082. if (length == (uint64_t)-1)
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. /* seek back */
  1088. avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
  1089. matroska->level_up = level_up;
  1090. matroska->current_id = saved_id;
  1091. return ret;
  1092. }
  1093. static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
  1094. {
  1095. EbmlList *seekhead_list = &matroska->seekhead;
  1096. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1097. int i;
  1098. // we should not do any seeking in the streaming case
  1099. if (!matroska->ctx->pb->seekable ||
  1100. (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
  1101. return;
  1102. for (i = 0; i < seekhead_list->nb_elem; i++) {
  1103. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1104. if (seekhead[i].pos <= before_pos)
  1105. continue;
  1106. // defer cues parsing until we actually need cue data.
  1107. if (seekhead[i].id == MATROSKA_ID_CUES) {
  1108. matroska->cues_parsing_deferred = 1;
  1109. continue;
  1110. }
  1111. if (matroska_parse_seekhead_entry(matroska, i) < 0)
  1112. break;
  1113. }
  1114. }
  1115. static void matroska_parse_cues(MatroskaDemuxContext *matroska) {
  1116. EbmlList *seekhead_list = &matroska->seekhead;
  1117. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1118. EbmlList *index_list;
  1119. MatroskaIndex *index;
  1120. int index_scale = 1;
  1121. int i, j;
  1122. for (i = 0; i < seekhead_list->nb_elem; i++)
  1123. if (seekhead[i].id == MATROSKA_ID_CUES)
  1124. break;
  1125. assert(i <= seekhead_list->nb_elem);
  1126. matroska_parse_seekhead_entry(matroska, i);
  1127. index_list = &matroska->index;
  1128. index = index_list->elem;
  1129. if (index_list->nb_elem
  1130. && index[0].time > 1E14/matroska->time_scale) {
  1131. av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
  1132. index_scale = matroska->time_scale;
  1133. }
  1134. for (i = 0; i < index_list->nb_elem; i++) {
  1135. EbmlList *pos_list = &index[i].pos;
  1136. MatroskaIndexPos *pos = pos_list->elem;
  1137. for (j = 0; j < pos_list->nb_elem; j++) {
  1138. MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track);
  1139. if (track && track->stream)
  1140. av_add_index_entry(track->stream,
  1141. pos[j].pos + matroska->segment_start,
  1142. index[i].time/index_scale, 0, 0,
  1143. AVINDEX_KEYFRAME);
  1144. }
  1145. }
  1146. }
  1147. static int matroska_aac_profile(char *codec_id)
  1148. {
  1149. static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
  1150. int profile;
  1151. for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
  1152. if (strstr(codec_id, aac_profiles[profile]))
  1153. break;
  1154. return profile + 1;
  1155. }
  1156. static int matroska_aac_sri(int samplerate)
  1157. {
  1158. int sri;
  1159. for (sri=0; sri<FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++)
  1160. if (avpriv_mpeg4audio_sample_rates[sri] == samplerate)
  1161. break;
  1162. return sri;
  1163. }
  1164. static int matroska_read_header(AVFormatContext *s)
  1165. {
  1166. MatroskaDemuxContext *matroska = s->priv_data;
  1167. EbmlList *attachements_list = &matroska->attachments;
  1168. MatroskaAttachement *attachements;
  1169. EbmlList *chapters_list = &matroska->chapters;
  1170. MatroskaChapter *chapters;
  1171. MatroskaTrack *tracks;
  1172. uint64_t max_start = 0;
  1173. Ebml ebml = { 0 };
  1174. AVStream *st;
  1175. int i, j, res;
  1176. matroska->ctx = s;
  1177. /* First read the EBML header. */
  1178. if (ebml_parse(matroska, ebml_syntax, &ebml)
  1179. || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
  1180. || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 2) {
  1181. av_log(matroska->ctx, AV_LOG_ERROR,
  1182. "EBML header using unsupported features\n"
  1183. "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
  1184. ebml.version, ebml.doctype, ebml.doctype_version);
  1185. ebml_free(ebml_syntax, &ebml);
  1186. return AVERROR_PATCHWELCOME;
  1187. }
  1188. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
  1189. if (!strcmp(ebml.doctype, matroska_doctypes[i]))
  1190. break;
  1191. if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
  1192. av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
  1193. }
  1194. ebml_free(ebml_syntax, &ebml);
  1195. /* The next thing is a segment. */
  1196. if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0)
  1197. return res;
  1198. matroska_execute_seekhead(matroska);
  1199. if (!matroska->time_scale)
  1200. matroska->time_scale = 1000000;
  1201. if (matroska->duration)
  1202. matroska->ctx->duration = matroska->duration * matroska->time_scale
  1203. * 1000 / AV_TIME_BASE;
  1204. av_dict_set(&s->metadata, "title", matroska->title, 0);
  1205. tracks = matroska->tracks.elem;
  1206. for (i=0; i < matroska->tracks.nb_elem; i++) {
  1207. MatroskaTrack *track = &tracks[i];
  1208. enum CodecID codec_id = CODEC_ID_NONE;
  1209. EbmlList *encodings_list = &tracks->encodings;
  1210. MatroskaTrackEncoding *encodings = encodings_list->elem;
  1211. uint8_t *extradata = NULL;
  1212. int extradata_size = 0;
  1213. int extradata_offset = 0;
  1214. AVIOContext b;
  1215. /* Apply some sanity checks. */
  1216. if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
  1217. track->type != MATROSKA_TRACK_TYPE_AUDIO &&
  1218. track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1219. av_log(matroska->ctx, AV_LOG_INFO,
  1220. "Unknown or unsupported track type %"PRIu64"\n",
  1221. track->type);
  1222. continue;
  1223. }
  1224. if (track->codec_id == NULL)
  1225. continue;
  1226. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1227. if (!track->default_duration && track->video.frame_rate > 0)
  1228. track->default_duration = 1000000000/track->video.frame_rate;
  1229. if (!track->video.display_width)
  1230. track->video.display_width = track->video.pixel_width;
  1231. if (!track->video.display_height)
  1232. track->video.display_height = track->video.pixel_height;
  1233. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1234. if (!track->audio.out_samplerate)
  1235. track->audio.out_samplerate = track->audio.samplerate;
  1236. }
  1237. if (encodings_list->nb_elem > 1) {
  1238. av_log(matroska->ctx, AV_LOG_ERROR,
  1239. "Multiple combined encodings not supported");
  1240. } else if (encodings_list->nb_elem == 1) {
  1241. if (encodings[0].type ||
  1242. (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
  1243. #if CONFIG_ZLIB
  1244. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1245. #endif
  1246. #if CONFIG_BZLIB
  1247. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1248. #endif
  1249. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
  1250. encodings[0].scope = 0;
  1251. av_log(matroska->ctx, AV_LOG_ERROR,
  1252. "Unsupported encoding type");
  1253. } else if (track->codec_priv.size && encodings[0].scope&2) {
  1254. uint8_t *codec_priv = track->codec_priv.data;
  1255. int offset = matroska_decode_buffer(&track->codec_priv.data,
  1256. &track->codec_priv.size,
  1257. track);
  1258. if (offset < 0) {
  1259. track->codec_priv.data = NULL;
  1260. track->codec_priv.size = 0;
  1261. av_log(matroska->ctx, AV_LOG_ERROR,
  1262. "Failed to decode codec private data\n");
  1263. } else if (offset > 0) {
  1264. track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
  1265. memcpy(track->codec_priv.data,
  1266. encodings[0].compression.settings.data, offset);
  1267. memcpy(track->codec_priv.data+offset, codec_priv,
  1268. track->codec_priv.size);
  1269. track->codec_priv.size += offset;
  1270. }
  1271. if (codec_priv != track->codec_priv.data)
  1272. av_free(codec_priv);
  1273. }
  1274. }
  1275. for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
  1276. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  1277. strlen(ff_mkv_codec_tags[j].str))){
  1278. codec_id= ff_mkv_codec_tags[j].id;
  1279. break;
  1280. }
  1281. }
  1282. st = track->stream = avformat_new_stream(s, NULL);
  1283. if (st == NULL)
  1284. return AVERROR(ENOMEM);
  1285. if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
  1286. && track->codec_priv.size >= 40
  1287. && track->codec_priv.data != NULL) {
  1288. track->ms_compat = 1;
  1289. track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
  1290. codec_id = ff_codec_get_id(ff_codec_bmp_tags, track->video.fourcc);
  1291. extradata_offset = 40;
  1292. } else if (!strcmp(track->codec_id, "A_MS/ACM")
  1293. && track->codec_priv.size >= 14
  1294. && track->codec_priv.data != NULL) {
  1295. int ret;
  1296. ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size,
  1297. AVIO_FLAG_READ, NULL, NULL, NULL, NULL);
  1298. ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size);
  1299. if (ret < 0)
  1300. return ret;
  1301. codec_id = st->codec->codec_id;
  1302. extradata_offset = FFMIN(track->codec_priv.size, 18);
  1303. } else if (!strcmp(track->codec_id, "V_QUICKTIME")
  1304. && (track->codec_priv.size >= 86)
  1305. && (track->codec_priv.data != NULL)) {
  1306. track->video.fourcc = AV_RL32(track->codec_priv.data);
  1307. codec_id=ff_codec_get_id(ff_codec_movvideo_tags, track->video.fourcc);
  1308. } else if (codec_id == CODEC_ID_PCM_S16BE) {
  1309. switch (track->audio.bitdepth) {
  1310. case 8: codec_id = CODEC_ID_PCM_U8; break;
  1311. case 24: codec_id = CODEC_ID_PCM_S24BE; break;
  1312. case 32: codec_id = CODEC_ID_PCM_S32BE; break;
  1313. }
  1314. } else if (codec_id == CODEC_ID_PCM_S16LE) {
  1315. switch (track->audio.bitdepth) {
  1316. case 8: codec_id = CODEC_ID_PCM_U8; break;
  1317. case 24: codec_id = CODEC_ID_PCM_S24LE; break;
  1318. case 32: codec_id = CODEC_ID_PCM_S32LE; break;
  1319. }
  1320. } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
  1321. codec_id = CODEC_ID_PCM_F64LE;
  1322. } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
  1323. int profile = matroska_aac_profile(track->codec_id);
  1324. int sri = matroska_aac_sri(track->audio.samplerate);
  1325. extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
  1326. if (extradata == NULL)
  1327. return AVERROR(ENOMEM);
  1328. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  1329. extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
  1330. if (strstr(track->codec_id, "SBR")) {
  1331. sri = matroska_aac_sri(track->audio.out_samplerate);
  1332. extradata[2] = 0x56;
  1333. extradata[3] = 0xE5;
  1334. extradata[4] = 0x80 | (sri<<3);
  1335. extradata_size = 5;
  1336. } else
  1337. extradata_size = 2;
  1338. } else if (codec_id == CODEC_ID_TTA) {
  1339. extradata_size = 30;
  1340. extradata = av_mallocz(extradata_size);
  1341. if (extradata == NULL)
  1342. return AVERROR(ENOMEM);
  1343. ffio_init_context(&b, extradata, extradata_size, 1,
  1344. NULL, NULL, NULL, NULL);
  1345. avio_write(&b, "TTA1", 4);
  1346. avio_wl16(&b, 1);
  1347. avio_wl16(&b, track->audio.channels);
  1348. avio_wl16(&b, track->audio.bitdepth);
  1349. avio_wl32(&b, track->audio.out_samplerate);
  1350. avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate);
  1351. } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
  1352. codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
  1353. extradata_offset = 26;
  1354. } else if (codec_id == CODEC_ID_RA_144) {
  1355. track->audio.out_samplerate = 8000;
  1356. track->audio.channels = 1;
  1357. } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
  1358. codec_id == CODEC_ID_ATRAC3 || codec_id == CODEC_ID_SIPR) {
  1359. int flavor;
  1360. ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size,
  1361. 0, NULL, NULL, NULL, NULL);
  1362. avio_skip(&b, 22);
  1363. flavor = avio_rb16(&b);
  1364. track->audio.coded_framesize = avio_rb32(&b);
  1365. avio_skip(&b, 12);
  1366. track->audio.sub_packet_h = avio_rb16(&b);
  1367. track->audio.frame_size = avio_rb16(&b);
  1368. track->audio.sub_packet_size = avio_rb16(&b);
  1369. track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
  1370. if (codec_id == CODEC_ID_RA_288) {
  1371. st->codec->block_align = track->audio.coded_framesize;
  1372. track->codec_priv.size = 0;
  1373. } else {
  1374. if (codec_id == CODEC_ID_SIPR && flavor < 4) {
  1375. const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
  1376. track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
  1377. st->codec->bit_rate = sipr_bit_rate[flavor];
  1378. }
  1379. st->codec->block_align = track->audio.sub_packet_size;
  1380. extradata_offset = 78;
  1381. }
  1382. }
  1383. track->codec_priv.size -= extradata_offset;
  1384. if (codec_id == CODEC_ID_NONE)
  1385. av_log(matroska->ctx, AV_LOG_INFO,
  1386. "Unknown/unsupported CodecID %s.\n", track->codec_id);
  1387. if (track->time_scale < 0.01)
  1388. track->time_scale = 1.0;
  1389. avpriv_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  1390. st->codec->codec_id = codec_id;
  1391. st->start_time = 0;
  1392. if (strcmp(track->language, "und"))
  1393. av_dict_set(&st->metadata, "language", track->language, 0);
  1394. av_dict_set(&st->metadata, "title", track->name, 0);
  1395. if (track->flag_default)
  1396. st->disposition |= AV_DISPOSITION_DEFAULT;
  1397. if (track->flag_forced)
  1398. st->disposition |= AV_DISPOSITION_FORCED;
  1399. if (!st->codec->extradata) {
  1400. if(extradata){
  1401. st->codec->extradata = extradata;
  1402. st->codec->extradata_size = extradata_size;
  1403. } else if(track->codec_priv.data && track->codec_priv.size > 0){
  1404. st->codec->extradata = av_mallocz(track->codec_priv.size +
  1405. FF_INPUT_BUFFER_PADDING_SIZE);
  1406. if(st->codec->extradata == NULL)
  1407. return AVERROR(ENOMEM);
  1408. st->codec->extradata_size = track->codec_priv.size;
  1409. memcpy(st->codec->extradata,
  1410. track->codec_priv.data + extradata_offset,
  1411. track->codec_priv.size);
  1412. }
  1413. }
  1414. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1415. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1416. st->codec->codec_tag = track->video.fourcc;
  1417. st->codec->width = track->video.pixel_width;
  1418. st->codec->height = track->video.pixel_height;
  1419. av_reduce(&st->sample_aspect_ratio.num,
  1420. &st->sample_aspect_ratio.den,
  1421. st->codec->height * track->video.display_width,
  1422. st->codec-> width * track->video.display_height,
  1423. 255);
  1424. if (st->codec->codec_id != CODEC_ID_H264)
  1425. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1426. if (track->default_duration) {
  1427. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1428. 1000000000, track->default_duration, 30000);
  1429. #if FF_API_R_FRAME_RATE
  1430. st->r_frame_rate = st->avg_frame_rate;
  1431. #endif
  1432. }
  1433. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1434. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1435. st->codec->sample_rate = track->audio.out_samplerate;
  1436. st->codec->channels = track->audio.channels;
  1437. if (st->codec->codec_id != CODEC_ID_AAC)
  1438. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1439. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  1440. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1441. if (st->codec->codec_id == CODEC_ID_SSA)
  1442. matroska->contains_ssa = 1;
  1443. }
  1444. }
  1445. attachements = attachements_list->elem;
  1446. for (j=0; j<attachements_list->nb_elem; j++) {
  1447. if (!(attachements[j].filename && attachements[j].mime &&
  1448. attachements[j].bin.data && attachements[j].bin.size > 0)) {
  1449. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1450. } else {
  1451. AVStream *st = avformat_new_stream(s, NULL);
  1452. if (st == NULL)
  1453. break;
  1454. av_dict_set(&st->metadata, "filename",attachements[j].filename, 0);
  1455. av_dict_set(&st->metadata, "mimetype", attachements[j].mime, 0);
  1456. st->codec->codec_id = CODEC_ID_NONE;
  1457. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  1458. st->codec->extradata = av_malloc(attachements[j].bin.size);
  1459. if(st->codec->extradata == NULL)
  1460. break;
  1461. st->codec->extradata_size = attachements[j].bin.size;
  1462. memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
  1463. for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
  1464. if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
  1465. strlen(ff_mkv_mime_tags[i].str))) {
  1466. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1467. break;
  1468. }
  1469. }
  1470. attachements[j].stream = st;
  1471. }
  1472. }
  1473. chapters = chapters_list->elem;
  1474. for (i=0; i<chapters_list->nb_elem; i++)
  1475. if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
  1476. && (max_start==0 || chapters[i].start > max_start)) {
  1477. chapters[i].chapter =
  1478. avpriv_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
  1479. chapters[i].start, chapters[i].end,
  1480. chapters[i].title);
  1481. av_dict_set(&chapters[i].chapter->metadata,
  1482. "title", chapters[i].title, 0);
  1483. max_start = chapters[i].start;
  1484. }
  1485. matroska_convert_tags(s);
  1486. return 0;
  1487. }
  1488. /*
  1489. * Put one packet in an application-supplied AVPacket struct.
  1490. * Returns 0 on success or -1 on failure.
  1491. */
  1492. static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
  1493. AVPacket *pkt)
  1494. {
  1495. if (matroska->num_packets > 0) {
  1496. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  1497. av_free(matroska->packets[0]);
  1498. if (matroska->num_packets > 1) {
  1499. void *newpackets;
  1500. memmove(&matroska->packets[0], &matroska->packets[1],
  1501. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1502. newpackets = av_realloc(matroska->packets,
  1503. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1504. if (newpackets)
  1505. matroska->packets = newpackets;
  1506. } else {
  1507. av_freep(&matroska->packets);
  1508. matroska->prev_pkt = NULL;
  1509. }
  1510. matroska->num_packets--;
  1511. return 0;
  1512. }
  1513. return -1;
  1514. }
  1515. /*
  1516. * Free all packets in our internal queue.
  1517. */
  1518. static void matroska_clear_queue(MatroskaDemuxContext *matroska)
  1519. {
  1520. if (matroska->packets) {
  1521. int n;
  1522. for (n = 0; n < matroska->num_packets; n++) {
  1523. av_free_packet(matroska->packets[n]);
  1524. av_free(matroska->packets[n]);
  1525. }
  1526. av_freep(&matroska->packets);
  1527. matroska->num_packets = 0;
  1528. }
  1529. }
  1530. static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
  1531. int size, int64_t pos, uint64_t cluster_time,
  1532. uint64_t duration, int is_keyframe,
  1533. int64_t cluster_pos)
  1534. {
  1535. uint64_t timecode = AV_NOPTS_VALUE;
  1536. MatroskaTrack *track;
  1537. int res = 0;
  1538. AVStream *st;
  1539. AVPacket *pkt;
  1540. int16_t block_time;
  1541. uint32_t *lace_size = NULL;
  1542. int n, flags, laces = 0;
  1543. uint64_t num;
  1544. if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
  1545. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  1546. return n;
  1547. }
  1548. data += n;
  1549. size -= n;
  1550. track = matroska_find_track_by_num(matroska, num);
  1551. if (!track || !track->stream) {
  1552. av_log(matroska->ctx, AV_LOG_INFO,
  1553. "Invalid stream %"PRIu64" or size %u\n", num, size);
  1554. return AVERROR_INVALIDDATA;
  1555. } else if (size <= 3)
  1556. return 0;
  1557. st = track->stream;
  1558. if (st->discard >= AVDISCARD_ALL)
  1559. return res;
  1560. if (duration == AV_NOPTS_VALUE)
  1561. duration = track->default_duration / matroska->time_scale;
  1562. block_time = AV_RB16(data);
  1563. data += 2;
  1564. flags = *data++;
  1565. size -= 3;
  1566. if (is_keyframe == -1)
  1567. is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
  1568. if (cluster_time != (uint64_t)-1
  1569. && (block_time >= 0 || cluster_time >= -block_time)) {
  1570. timecode = cluster_time + block_time;
  1571. if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
  1572. && timecode < track->end_timecode)
  1573. is_keyframe = 0; /* overlapping subtitles are not key frame */
  1574. if (is_keyframe)
  1575. av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
  1576. track->end_timecode = FFMAX(track->end_timecode, timecode+duration);
  1577. }
  1578. if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1579. if (!is_keyframe || timecode < matroska->skip_to_timecode)
  1580. return res;
  1581. matroska->skip_to_keyframe = 0;
  1582. }
  1583. switch ((flags & 0x06) >> 1) {
  1584. case 0x0: /* no lacing */
  1585. laces = 1;
  1586. lace_size = av_mallocz(sizeof(int));
  1587. lace_size[0] = size;
  1588. break;
  1589. case 0x1: /* Xiph lacing */
  1590. case 0x2: /* fixed-size lacing */
  1591. case 0x3: /* EBML lacing */
  1592. assert(size>0); // size <=3 is checked before size-=3 above
  1593. laces = (*data) + 1;
  1594. data += 1;
  1595. size -= 1;
  1596. lace_size = av_mallocz(laces * sizeof(int));
  1597. switch ((flags & 0x06) >> 1) {
  1598. case 0x1: /* Xiph lacing */ {
  1599. uint8_t temp;
  1600. uint32_t total = 0;
  1601. for (n = 0; res == 0 && n < laces - 1; n++) {
  1602. while (1) {
  1603. if (size == 0) {
  1604. res = -1;
  1605. break;
  1606. }
  1607. temp = *data;
  1608. lace_size[n] += temp;
  1609. data += 1;
  1610. size -= 1;
  1611. if (temp != 0xff)
  1612. break;
  1613. }
  1614. total += lace_size[n];
  1615. }
  1616. lace_size[n] = size - total;
  1617. break;
  1618. }
  1619. case 0x2: /* fixed-size lacing */
  1620. for (n = 0; n < laces; n++)
  1621. lace_size[n] = size / laces;
  1622. break;
  1623. case 0x3: /* EBML lacing */ {
  1624. uint32_t total;
  1625. n = matroska_ebmlnum_uint(matroska, data, size, &num);
  1626. if (n < 0) {
  1627. av_log(matroska->ctx, AV_LOG_INFO,
  1628. "EBML block data error\n");
  1629. break;
  1630. }
  1631. data += n;
  1632. size -= n;
  1633. total = lace_size[0] = num;
  1634. for (n = 1; res == 0 && n < laces - 1; n++) {
  1635. int64_t snum;
  1636. int r;
  1637. r = matroska_ebmlnum_sint(matroska, data, size, &snum);
  1638. if (r < 0) {
  1639. av_log(matroska->ctx, AV_LOG_INFO,
  1640. "EBML block data error\n");
  1641. break;
  1642. }
  1643. data += r;
  1644. size -= r;
  1645. lace_size[n] = lace_size[n - 1] + snum;
  1646. total += lace_size[n];
  1647. }
  1648. lace_size[laces - 1] = size - total;
  1649. break;
  1650. }
  1651. }
  1652. break;
  1653. }
  1654. if (res == 0) {
  1655. for (n = 0; n < laces; n++) {
  1656. if ((st->codec->codec_id == CODEC_ID_RA_288 ||
  1657. st->codec->codec_id == CODEC_ID_COOK ||
  1658. st->codec->codec_id == CODEC_ID_SIPR ||
  1659. st->codec->codec_id == CODEC_ID_ATRAC3) &&
  1660. st->codec->block_align && track->audio.sub_packet_size) {
  1661. int a = st->codec->block_align;
  1662. int sps = track->audio.sub_packet_size;
  1663. int cfs = track->audio.coded_framesize;
  1664. int h = track->audio.sub_packet_h;
  1665. int y = track->audio.sub_packet_cnt;
  1666. int w = track->audio.frame_size;
  1667. int x;
  1668. if (!track->audio.pkt_cnt) {
  1669. if (track->audio.sub_packet_cnt == 0)
  1670. track->audio.buf_timecode = timecode;
  1671. if (st->codec->codec_id == CODEC_ID_RA_288) {
  1672. if (size < cfs * h / 2) {
  1673. av_log(matroska->ctx, AV_LOG_ERROR,
  1674. "Corrupt int4 RM-style audio packet size\n");
  1675. res = AVERROR_INVALIDDATA;
  1676. goto end;
  1677. }
  1678. for (x=0; x<h/2; x++)
  1679. memcpy(track->audio.buf+x*2*w+y*cfs,
  1680. data+x*cfs, cfs);
  1681. } else if (st->codec->codec_id == CODEC_ID_SIPR) {
  1682. if (size < w) {
  1683. av_log(matroska->ctx, AV_LOG_ERROR,
  1684. "Corrupt sipr RM-style audio packet size\n");
  1685. res = AVERROR_INVALIDDATA;
  1686. goto end;
  1687. }
  1688. memcpy(track->audio.buf + y*w, data, w);
  1689. } else {
  1690. if (size < sps * w / sps) {
  1691. av_log(matroska->ctx, AV_LOG_ERROR,
  1692. "Corrupt generic RM-style audio packet size\n");
  1693. res = AVERROR_INVALIDDATA;
  1694. goto end;
  1695. }
  1696. for (x=0; x<w/sps; x++)
  1697. memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  1698. }
  1699. if (++track->audio.sub_packet_cnt >= h) {
  1700. if (st->codec->codec_id == CODEC_ID_SIPR)
  1701. ff_rm_reorder_sipr_data(track->audio.buf, h, w);
  1702. track->audio.sub_packet_cnt = 0;
  1703. track->audio.pkt_cnt = h*w / a;
  1704. }
  1705. }
  1706. while (track->audio.pkt_cnt) {
  1707. pkt = av_mallocz(sizeof(AVPacket));
  1708. av_new_packet(pkt, a);
  1709. memcpy(pkt->data, track->audio.buf
  1710. + a * (h*w / a - track->audio.pkt_cnt--), a);
  1711. pkt->pts = track->audio.buf_timecode;
  1712. track->audio.buf_timecode = AV_NOPTS_VALUE;
  1713. pkt->pos = pos;
  1714. pkt->stream_index = st->index;
  1715. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1716. }
  1717. } else {
  1718. MatroskaTrackEncoding *encodings = track->encodings.elem;
  1719. int offset = 0, pkt_size = lace_size[n];
  1720. uint8_t *pkt_data = data;
  1721. if (pkt_size > size) {
  1722. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
  1723. break;
  1724. }
  1725. if (encodings && encodings->scope & 1) {
  1726. offset = matroska_decode_buffer(&pkt_data,&pkt_size, track);
  1727. if (offset < 0)
  1728. continue;
  1729. }
  1730. pkt = av_mallocz(sizeof(AVPacket));
  1731. /* XXX: prevent data copy... */
  1732. if (av_new_packet(pkt, pkt_size+offset) < 0) {
  1733. av_free(pkt);
  1734. res = AVERROR(ENOMEM);
  1735. break;
  1736. }
  1737. if (offset)
  1738. memcpy (pkt->data, encodings->compression.settings.data, offset);
  1739. memcpy (pkt->data+offset, pkt_data, pkt_size);
  1740. if (pkt_data != data)
  1741. av_free(pkt_data);
  1742. if (n == 0)
  1743. pkt->flags = is_keyframe;
  1744. pkt->stream_index = st->index;
  1745. if (track->ms_compat)
  1746. pkt->dts = timecode;
  1747. else
  1748. pkt->pts = timecode;
  1749. pkt->pos = pos;
  1750. if (st->codec->codec_id == CODEC_ID_TEXT)
  1751. pkt->convergence_duration = duration;
  1752. else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE)
  1753. pkt->duration = duration;
  1754. if (st->codec->codec_id == CODEC_ID_SSA)
  1755. matroska_fix_ass_packet(matroska, pkt, duration);
  1756. if (matroska->prev_pkt &&
  1757. timecode != AV_NOPTS_VALUE &&
  1758. matroska->prev_pkt->pts == timecode &&
  1759. matroska->prev_pkt->stream_index == st->index &&
  1760. st->codec->codec_id == CODEC_ID_SSA)
  1761. matroska_merge_packets(matroska->prev_pkt, pkt);
  1762. else {
  1763. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1764. matroska->prev_pkt = pkt;
  1765. }
  1766. }
  1767. if (timecode != AV_NOPTS_VALUE)
  1768. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  1769. data += lace_size[n];
  1770. size -= lace_size[n];
  1771. }
  1772. }
  1773. end:
  1774. av_free(lace_size);
  1775. return res;
  1776. }
  1777. static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska)
  1778. {
  1779. EbmlList *blocks_list;
  1780. MatroskaBlock *blocks;
  1781. int i, res;
  1782. res = ebml_parse(matroska,
  1783. matroska_cluster_incremental_parsing,
  1784. &matroska->current_cluster);
  1785. if (res == 1) {
  1786. /* New Cluster */
  1787. if (matroska->current_cluster_pos)
  1788. ebml_level_end(matroska);
  1789. ebml_free(matroska_cluster, &matroska->current_cluster);
  1790. memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster));
  1791. matroska->current_cluster_num_blocks = 0;
  1792. matroska->current_cluster_pos = avio_tell(matroska->ctx->pb);
  1793. matroska->prev_pkt = NULL;
  1794. /* sizeof the ID which was already read */
  1795. if (matroska->current_id)
  1796. matroska->current_cluster_pos -= 4;
  1797. res = ebml_parse(matroska,
  1798. matroska_clusters_incremental,
  1799. &matroska->current_cluster);
  1800. /* Try parsing the block again. */
  1801. if (res == 1)
  1802. res = ebml_parse(matroska,
  1803. matroska_cluster_incremental_parsing,
  1804. &matroska->current_cluster);
  1805. }
  1806. if (!res &&
  1807. matroska->current_cluster_num_blocks <
  1808. matroska->current_cluster.blocks.nb_elem) {
  1809. blocks_list = &matroska->current_cluster.blocks;
  1810. blocks = blocks_list->elem;
  1811. matroska->current_cluster_num_blocks = blocks_list->nb_elem;
  1812. i = blocks_list->nb_elem - 1;
  1813. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  1814. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  1815. if (!blocks[i].non_simple)
  1816. blocks[i].duration = AV_NOPTS_VALUE;
  1817. res = matroska_parse_block(matroska,
  1818. blocks[i].bin.data, blocks[i].bin.size,
  1819. blocks[i].bin.pos,
  1820. matroska->current_cluster.timecode,
  1821. blocks[i].duration, is_keyframe,
  1822. matroska->current_cluster_pos);
  1823. }
  1824. }
  1825. if (res < 0) matroska->done = 1;
  1826. return res;
  1827. }
  1828. static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
  1829. {
  1830. MatroskaCluster cluster = { 0 };
  1831. EbmlList *blocks_list;
  1832. MatroskaBlock *blocks;
  1833. int i, res;
  1834. int64_t pos;
  1835. if (!matroska->contains_ssa)
  1836. return matroska_parse_cluster_incremental(matroska);
  1837. pos = avio_tell(matroska->ctx->pb);
  1838. matroska->prev_pkt = NULL;
  1839. if (matroska->current_id)
  1840. pos -= 4; /* sizeof the ID which was already read */
  1841. res = ebml_parse(matroska, matroska_clusters, &cluster);
  1842. blocks_list = &cluster.blocks;
  1843. blocks = blocks_list->elem;
  1844. for (i=0; i<blocks_list->nb_elem && !res; i++)
  1845. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  1846. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  1847. if (!blocks[i].non_simple)
  1848. blocks[i].duration = AV_NOPTS_VALUE;
  1849. res=matroska_parse_block(matroska,
  1850. blocks[i].bin.data, blocks[i].bin.size,
  1851. blocks[i].bin.pos, cluster.timecode,
  1852. blocks[i].duration, is_keyframe,
  1853. pos);
  1854. }
  1855. ebml_free(matroska_cluster, &cluster);
  1856. if (res < 0) matroska->done = 1;
  1857. return res;
  1858. }
  1859. static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
  1860. {
  1861. MatroskaDemuxContext *matroska = s->priv_data;
  1862. int ret = 0;
  1863. while (!ret && matroska_deliver_packet(matroska, pkt)) {
  1864. if (matroska->done)
  1865. return AVERROR_EOF;
  1866. ret = matroska_parse_cluster(matroska);
  1867. }
  1868. if (ret == AVERROR_INVALIDDATA) {
  1869. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  1870. return 0;
  1871. }
  1872. return ret;
  1873. }
  1874. static int matroska_read_seek(AVFormatContext *s, int stream_index,
  1875. int64_t timestamp, int flags)
  1876. {
  1877. MatroskaDemuxContext *matroska = s->priv_data;
  1878. MatroskaTrack *tracks = matroska->tracks.elem;
  1879. AVStream *st = s->streams[stream_index];
  1880. int i, index, index_sub, index_min;
  1881. /* Parse the CUES now since we need the index data to seek. */
  1882. if (matroska->cues_parsing_deferred) {
  1883. matroska_parse_cues(matroska);
  1884. matroska->cues_parsing_deferred = 0;
  1885. }
  1886. if (!st->nb_index_entries)
  1887. return 0;
  1888. timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
  1889. if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  1890. avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
  1891. matroska->current_id = 0;
  1892. while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  1893. matroska->prev_pkt = NULL;
  1894. matroska_clear_queue(matroska);
  1895. if (matroska_parse_cluster(matroska) < 0)
  1896. break;
  1897. }
  1898. }
  1899. matroska_clear_queue(matroska);
  1900. if (index < 0)
  1901. return 0;
  1902. index_min = index;
  1903. for (i=0; i < matroska->tracks.nb_elem; i++) {
  1904. tracks[i].audio.pkt_cnt = 0;
  1905. tracks[i].audio.sub_packet_cnt = 0;
  1906. tracks[i].audio.buf_timecode = AV_NOPTS_VALUE;
  1907. tracks[i].end_timecode = 0;
  1908. if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
  1909. && !tracks[i].stream->discard != AVDISCARD_ALL) {
  1910. index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
  1911. if (index_sub >= 0
  1912. && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
  1913. && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
  1914. index_min = index_sub;
  1915. }
  1916. }
  1917. avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
  1918. matroska->current_id = 0;
  1919. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  1920. matroska->skip_to_timecode = st->index_entries[index].timestamp;
  1921. matroska->done = 0;
  1922. ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
  1923. return 0;
  1924. }
  1925. static int matroska_read_close(AVFormatContext *s)
  1926. {
  1927. MatroskaDemuxContext *matroska = s->priv_data;
  1928. MatroskaTrack *tracks = matroska->tracks.elem;
  1929. int n;
  1930. matroska_clear_queue(matroska);
  1931. for (n=0; n < matroska->tracks.nb_elem; n++)
  1932. if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
  1933. av_free(tracks[n].audio.buf);
  1934. ebml_free(matroska_cluster, &matroska->current_cluster);
  1935. ebml_free(matroska_segment, matroska);
  1936. return 0;
  1937. }
  1938. AVInputFormat ff_matroska_demuxer = {
  1939. .name = "matroska,webm",
  1940. .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
  1941. .priv_data_size = sizeof(MatroskaDemuxContext),
  1942. .read_probe = matroska_probe,
  1943. .read_header = matroska_read_header,
  1944. .read_packet = matroska_read_packet,
  1945. .read_close = matroska_read_close,
  1946. .read_seek = matroska_read_seek,
  1947. };