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.

2572 lines
91KB

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