matroskadec.c 74 KB

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