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.

2560 lines
90KB

  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. static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
  1073. AVPacket *pkt, uint64_t display_duration)
  1074. {
  1075. AVBufferRef *line;
  1076. char *layer, *ptr = pkt->data, *end = ptr+pkt->size;
  1077. for (; *ptr!=',' && ptr<end-1; ptr++);
  1078. if (*ptr == ',')
  1079. ptr++;
  1080. layer = ptr;
  1081. for (; *ptr!=',' && ptr<end-1; ptr++);
  1082. if (*ptr == ',') {
  1083. int64_t end_pts = pkt->pts + display_duration;
  1084. int sc = matroska->time_scale * pkt->pts / 10000000;
  1085. int ec = matroska->time_scale * end_pts / 10000000;
  1086. int sh, sm, ss, eh, em, es, len;
  1087. sh = sc/360000; sc -= 360000*sh;
  1088. sm = sc/ 6000; sc -= 6000*sm;
  1089. ss = sc/ 100; sc -= 100*ss;
  1090. eh = ec/360000; ec -= 360000*eh;
  1091. em = ec/ 6000; ec -= 6000*em;
  1092. es = ec/ 100; ec -= 100*es;
  1093. *ptr++ = '\0';
  1094. len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
  1095. if (!(line = av_buffer_alloc(len)))
  1096. return;
  1097. snprintf(line->data, len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
  1098. layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
  1099. av_buffer_unref(&pkt->buf);
  1100. pkt->buf = line;
  1101. pkt->data = line->data;
  1102. pkt->size = strlen(line->data);
  1103. }
  1104. }
  1105. static int matroska_merge_packets(AVPacket *out, AVPacket *in)
  1106. {
  1107. int ret = av_grow_packet(out, in->size);
  1108. if (ret < 0)
  1109. return ret;
  1110. memcpy(out->data + out->size - in->size, in->data, in->size);
  1111. av_free_packet(in);
  1112. av_free(in);
  1113. return 0;
  1114. }
  1115. static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
  1116. AVDictionary **metadata, char *prefix)
  1117. {
  1118. MatroskaTag *tags = list->elem;
  1119. char key[1024];
  1120. int i;
  1121. for (i=0; i < list->nb_elem; i++) {
  1122. const char *lang= (tags[i].lang && strcmp(tags[i].lang, "und")) ? tags[i].lang : NULL;
  1123. if (!tags[i].name) {
  1124. av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
  1125. continue;
  1126. }
  1127. if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
  1128. else av_strlcpy(key, tags[i].name, sizeof(key));
  1129. if (tags[i].def || !lang) {
  1130. av_dict_set(metadata, key, tags[i].string, 0);
  1131. if (tags[i].sub.nb_elem)
  1132. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  1133. }
  1134. if (lang) {
  1135. av_strlcat(key, "-", sizeof(key));
  1136. av_strlcat(key, lang, sizeof(key));
  1137. av_dict_set(metadata, key, tags[i].string, 0);
  1138. if (tags[i].sub.nb_elem)
  1139. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  1140. }
  1141. }
  1142. ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
  1143. }
  1144. static void matroska_convert_tags(AVFormatContext *s)
  1145. {
  1146. MatroskaDemuxContext *matroska = s->priv_data;
  1147. MatroskaTags *tags = matroska->tags.elem;
  1148. int i, j;
  1149. for (i=0; i < matroska->tags.nb_elem; i++) {
  1150. if (tags[i].target.attachuid) {
  1151. MatroskaAttachement *attachment = matroska->attachments.elem;
  1152. for (j=0; j<matroska->attachments.nb_elem; j++)
  1153. if (attachment[j].uid == tags[i].target.attachuid
  1154. && attachment[j].stream)
  1155. matroska_convert_tag(s, &tags[i].tag,
  1156. &attachment[j].stream->metadata, NULL);
  1157. } else if (tags[i].target.chapteruid) {
  1158. MatroskaChapter *chapter = matroska->chapters.elem;
  1159. for (j=0; j<matroska->chapters.nb_elem; j++)
  1160. if (chapter[j].uid == tags[i].target.chapteruid
  1161. && chapter[j].chapter)
  1162. matroska_convert_tag(s, &tags[i].tag,
  1163. &chapter[j].chapter->metadata, NULL);
  1164. } else if (tags[i].target.trackuid) {
  1165. MatroskaTrack *track = matroska->tracks.elem;
  1166. for (j=0; j<matroska->tracks.nb_elem; j++)
  1167. if (track[j].uid == tags[i].target.trackuid && track[j].stream)
  1168. matroska_convert_tag(s, &tags[i].tag,
  1169. &track[j].stream->metadata, NULL);
  1170. } else {
  1171. matroska_convert_tag(s, &tags[i].tag, &s->metadata,
  1172. tags[i].target.type);
  1173. }
  1174. }
  1175. }
  1176. static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx)
  1177. {
  1178. EbmlList *seekhead_list = &matroska->seekhead;
  1179. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1180. uint32_t level_up = matroska->level_up;
  1181. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1182. uint32_t saved_id = matroska->current_id;
  1183. MatroskaLevel level;
  1184. int64_t offset;
  1185. int ret = 0;
  1186. if (idx >= seekhead_list->nb_elem
  1187. || seekhead[idx].id == MATROSKA_ID_SEEKHEAD
  1188. || seekhead[idx].id == MATROSKA_ID_CLUSTER)
  1189. return 0;
  1190. /* seek */
  1191. offset = seekhead[idx].pos + matroska->segment_start;
  1192. if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) {
  1193. /* We don't want to lose our seekhead level, so we add
  1194. * a dummy. This is a crude hack. */
  1195. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1196. av_log(matroska->ctx, AV_LOG_INFO,
  1197. "Max EBML element depth (%d) reached, "
  1198. "cannot parse further.\n", EBML_MAX_DEPTH);
  1199. ret = AVERROR_INVALIDDATA;
  1200. } else {
  1201. level.start = 0;
  1202. level.length = (uint64_t)-1;
  1203. matroska->levels[matroska->num_levels] = level;
  1204. matroska->num_levels++;
  1205. matroska->current_id = 0;
  1206. ret = ebml_parse(matroska, matroska_segment, matroska);
  1207. /* remove dummy level */
  1208. while (matroska->num_levels) {
  1209. uint64_t length = matroska->levels[--matroska->num_levels].length;
  1210. if (length == (uint64_t)-1)
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. /* seek back */
  1216. avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
  1217. matroska->level_up = level_up;
  1218. matroska->current_id = saved_id;
  1219. return ret;
  1220. }
  1221. static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
  1222. {
  1223. EbmlList *seekhead_list = &matroska->seekhead;
  1224. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1225. int i;
  1226. // we should not do any seeking in the streaming case
  1227. if (!matroska->ctx->pb->seekable ||
  1228. (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
  1229. return;
  1230. for (i = 0; i < seekhead_list->nb_elem; i++) {
  1231. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1232. if (seekhead[i].pos <= before_pos)
  1233. continue;
  1234. // defer cues parsing until we actually need cue data.
  1235. if (seekhead[i].id == MATROSKA_ID_CUES) {
  1236. matroska->cues_parsing_deferred = 1;
  1237. continue;
  1238. }
  1239. if (matroska_parse_seekhead_entry(matroska, i) < 0) {
  1240. // mark index as broken
  1241. matroska->cues_parsing_deferred = -1;
  1242. break;
  1243. }
  1244. }
  1245. }
  1246. static void matroska_add_index_entries(MatroskaDemuxContext *matroska) {
  1247. EbmlList *index_list;
  1248. MatroskaIndex *index;
  1249. int index_scale = 1;
  1250. int i, j;
  1251. index_list = &matroska->index;
  1252. index = index_list->elem;
  1253. if (index_list->nb_elem
  1254. && index[0].time > 1E14/matroska->time_scale) {
  1255. av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
  1256. index_scale = matroska->time_scale;
  1257. }
  1258. for (i = 0; i < index_list->nb_elem; i++) {
  1259. EbmlList *pos_list = &index[i].pos;
  1260. MatroskaIndexPos *pos = pos_list->elem;
  1261. for (j = 0; j < pos_list->nb_elem; j++) {
  1262. MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track);
  1263. if (track && track->stream)
  1264. av_add_index_entry(track->stream,
  1265. pos[j].pos + matroska->segment_start,
  1266. index[i].time/index_scale, 0, 0,
  1267. AVINDEX_KEYFRAME);
  1268. }
  1269. }
  1270. }
  1271. static void matroska_parse_cues(MatroskaDemuxContext *matroska) {
  1272. EbmlList *seekhead_list = &matroska->seekhead;
  1273. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1274. int i;
  1275. for (i = 0; i < seekhead_list->nb_elem; i++)
  1276. if (seekhead[i].id == MATROSKA_ID_CUES)
  1277. break;
  1278. av_assert1(i <= seekhead_list->nb_elem);
  1279. if (matroska_parse_seekhead_entry(matroska, i) < 0)
  1280. matroska->cues_parsing_deferred = -1;
  1281. matroska_add_index_entries(matroska);
  1282. }
  1283. static int matroska_aac_profile(char *codec_id)
  1284. {
  1285. static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
  1286. int profile;
  1287. for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
  1288. if (strstr(codec_id, aac_profiles[profile]))
  1289. break;
  1290. return profile + 1;
  1291. }
  1292. static int matroska_aac_sri(int samplerate)
  1293. {
  1294. int sri;
  1295. for (sri=0; sri<FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++)
  1296. if (avpriv_mpeg4audio_sample_rates[sri] == samplerate)
  1297. break;
  1298. return sri;
  1299. }
  1300. static void matroska_metadata_creation_time(AVDictionary **metadata, int64_t date_utc)
  1301. {
  1302. char buffer[32];
  1303. /* Convert to seconds and adjust by number of seconds between 2001-01-01 and Epoch */
  1304. time_t creation_time = date_utc / 1000000000 + 978307200;
  1305. struct tm *ptm = gmtime(&creation_time);
  1306. if (!ptm) return;
  1307. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  1308. av_dict_set(metadata, "creation_time", buffer, 0);
  1309. }
  1310. static int matroska_read_header(AVFormatContext *s)
  1311. {
  1312. MatroskaDemuxContext *matroska = s->priv_data;
  1313. EbmlList *attachements_list = &matroska->attachments;
  1314. MatroskaAttachement *attachements;
  1315. EbmlList *chapters_list = &matroska->chapters;
  1316. MatroskaChapter *chapters;
  1317. MatroskaTrack *tracks;
  1318. uint64_t max_start = 0;
  1319. int64_t pos;
  1320. Ebml ebml = { 0 };
  1321. AVStream *st;
  1322. int i, j, k, res;
  1323. matroska->ctx = s;
  1324. /* First read the EBML header. */
  1325. if (ebml_parse(matroska, ebml_syntax, &ebml)
  1326. || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
  1327. || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 3 || !ebml.doctype) {
  1328. av_log(matroska->ctx, AV_LOG_ERROR,
  1329. "EBML header using unsupported features\n"
  1330. "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
  1331. ebml.version, ebml.doctype, ebml.doctype_version);
  1332. ebml_free(ebml_syntax, &ebml);
  1333. return AVERROR_PATCHWELCOME;
  1334. } else if (ebml.doctype_version == 3) {
  1335. av_log(matroska->ctx, AV_LOG_WARNING,
  1336. "EBML header using unsupported features\n"
  1337. "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
  1338. ebml.version, ebml.doctype, ebml.doctype_version);
  1339. }
  1340. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
  1341. if (!strcmp(ebml.doctype, matroska_doctypes[i]))
  1342. break;
  1343. if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
  1344. av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
  1345. if (matroska->ctx->error_recognition & AV_EF_EXPLODE) {
  1346. ebml_free(ebml_syntax, &ebml);
  1347. return AVERROR_INVALIDDATA;
  1348. }
  1349. }
  1350. ebml_free(ebml_syntax, &ebml);
  1351. /* The next thing is a segment. */
  1352. pos = avio_tell(matroska->ctx->pb);
  1353. res = ebml_parse(matroska, matroska_segments, matroska);
  1354. // try resyncing until we find a EBML_STOP type element.
  1355. while (res != 1) {
  1356. res = matroska_resync(matroska, pos);
  1357. if (res < 0)
  1358. return res;
  1359. pos = avio_tell(matroska->ctx->pb);
  1360. res = ebml_parse(matroska, matroska_segment, matroska);
  1361. }
  1362. matroska_execute_seekhead(matroska);
  1363. if (!matroska->time_scale)
  1364. matroska->time_scale = 1000000;
  1365. if (matroska->duration)
  1366. matroska->ctx->duration = matroska->duration * matroska->time_scale
  1367. * 1000 / AV_TIME_BASE;
  1368. av_dict_set(&s->metadata, "title", matroska->title, 0);
  1369. if (matroska->date_utc.size == 8)
  1370. matroska_metadata_creation_time(&s->metadata, AV_RB64(matroska->date_utc.data));
  1371. tracks = matroska->tracks.elem;
  1372. for (i=0; i < matroska->tracks.nb_elem; i++) {
  1373. MatroskaTrack *track = &tracks[i];
  1374. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  1375. EbmlList *encodings_list = &track->encodings;
  1376. MatroskaTrackEncoding *encodings = encodings_list->elem;
  1377. uint8_t *extradata = NULL;
  1378. int extradata_size = 0;
  1379. int extradata_offset = 0;
  1380. uint32_t fourcc = 0;
  1381. AVIOContext b;
  1382. char* key_id_base64 = NULL;
  1383. /* Apply some sanity checks. */
  1384. if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
  1385. track->type != MATROSKA_TRACK_TYPE_AUDIO &&
  1386. track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1387. av_log(matroska->ctx, AV_LOG_INFO,
  1388. "Unknown or unsupported track type %"PRIu64"\n",
  1389. track->type);
  1390. continue;
  1391. }
  1392. if (track->codec_id == NULL)
  1393. continue;
  1394. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1395. if (!track->default_duration && track->video.frame_rate > 0)
  1396. track->default_duration = 1000000000/track->video.frame_rate;
  1397. if (track->video.display_width == -1)
  1398. track->video.display_width = track->video.pixel_width;
  1399. if (track->video.display_height == -1)
  1400. track->video.display_height = track->video.pixel_height;
  1401. if (track->video.color_space.size == 4)
  1402. fourcc = AV_RL32(track->video.color_space.data);
  1403. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1404. if (!track->audio.out_samplerate)
  1405. track->audio.out_samplerate = track->audio.samplerate;
  1406. }
  1407. if (encodings_list->nb_elem > 1) {
  1408. av_log(matroska->ctx, AV_LOG_ERROR,
  1409. "Multiple combined encodings not supported");
  1410. } else if (encodings_list->nb_elem == 1) {
  1411. if (encodings[0].type) {
  1412. if (encodings[0].encryption.key_id.size > 0) {
  1413. /* Save the encryption key id to be stored later as a
  1414. metadata tag. */
  1415. const int b64_size = AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
  1416. key_id_base64 = av_malloc(b64_size);
  1417. if (key_id_base64 == NULL)
  1418. return AVERROR(ENOMEM);
  1419. av_base64_encode(key_id_base64, b64_size,
  1420. encodings[0].encryption.key_id.data,
  1421. encodings[0].encryption.key_id.size);
  1422. } else {
  1423. encodings[0].scope = 0;
  1424. av_log(matroska->ctx, AV_LOG_ERROR,
  1425. "Unsupported encoding type");
  1426. }
  1427. } else if (
  1428. #if CONFIG_ZLIB
  1429. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1430. #endif
  1431. #if CONFIG_BZLIB
  1432. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1433. #endif
  1434. #if CONFIG_LZO
  1435. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO &&
  1436. #endif
  1437. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP) {
  1438. encodings[0].scope = 0;
  1439. av_log(matroska->ctx, AV_LOG_ERROR,
  1440. "Unsupported encoding type");
  1441. } else if (track->codec_priv.size && encodings[0].scope&2) {
  1442. uint8_t *codec_priv = track->codec_priv.data;
  1443. int ret = matroska_decode_buffer(&track->codec_priv.data,
  1444. &track->codec_priv.size,
  1445. track);
  1446. if (ret < 0) {
  1447. track->codec_priv.data = NULL;
  1448. track->codec_priv.size = 0;
  1449. av_log(matroska->ctx, AV_LOG_ERROR,
  1450. "Failed to decode codec private data\n");
  1451. }
  1452. if (codec_priv != track->codec_priv.data)
  1453. av_free(codec_priv);
  1454. }
  1455. }
  1456. for(j=0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++){
  1457. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  1458. strlen(ff_mkv_codec_tags[j].str))){
  1459. codec_id= ff_mkv_codec_tags[j].id;
  1460. break;
  1461. }
  1462. }
  1463. st = track->stream = avformat_new_stream(s, NULL);
  1464. if (st == NULL) {
  1465. av_free(key_id_base64);
  1466. return AVERROR(ENOMEM);
  1467. }
  1468. if (key_id_base64) {
  1469. /* export encryption key id as base64 metadata tag */
  1470. av_dict_set(&st->metadata, "enc_key_id", key_id_base64, 0);
  1471. av_freep(&key_id_base64);
  1472. }
  1473. if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
  1474. && track->codec_priv.size >= 40
  1475. && track->codec_priv.data != NULL) {
  1476. track->ms_compat = 1;
  1477. fourcc = AV_RL32(track->codec_priv.data + 16);
  1478. codec_id = ff_codec_get_id(ff_codec_bmp_tags, fourcc);
  1479. extradata_offset = 40;
  1480. } else if (!strcmp(track->codec_id, "A_MS/ACM")
  1481. && track->codec_priv.size >= 14
  1482. && track->codec_priv.data != NULL) {
  1483. int ret;
  1484. ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size,
  1485. 0, NULL, NULL, NULL, NULL);
  1486. ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size);
  1487. if (ret < 0)
  1488. return ret;
  1489. codec_id = st->codec->codec_id;
  1490. extradata_offset = FFMIN(track->codec_priv.size, 18);
  1491. } else if (!strcmp(track->codec_id, "V_QUICKTIME")
  1492. && (track->codec_priv.size >= 86)
  1493. && (track->codec_priv.data != NULL)) {
  1494. fourcc = AV_RL32(track->codec_priv.data);
  1495. codec_id = ff_codec_get_id(ff_codec_movvideo_tags, fourcc);
  1496. } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size && track->codec_priv.size < INT_MAX - 12 - FF_INPUT_BUFFER_PADDING_SIZE) {
  1497. /* Only ALAC's magic cookie is stored in Matroska's track headers.
  1498. Create the "atom size", "tag", and "tag version" fields the
  1499. decoder expects manually. */
  1500. extradata_size = 12 + track->codec_priv.size;
  1501. extradata = av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  1502. if (extradata == NULL)
  1503. return AVERROR(ENOMEM);
  1504. AV_WB32(extradata, extradata_size);
  1505. memcpy(&extradata[4], "alac", 4);
  1506. AV_WB32(&extradata[8], 0);
  1507. memcpy(&extradata[12], track->codec_priv.data, track->codec_priv.size);
  1508. } else if (codec_id == AV_CODEC_ID_PCM_S16BE) {
  1509. switch (track->audio.bitdepth) {
  1510. case 8: codec_id = AV_CODEC_ID_PCM_U8; break;
  1511. case 24: codec_id = AV_CODEC_ID_PCM_S24BE; break;
  1512. case 32: codec_id = AV_CODEC_ID_PCM_S32BE; break;
  1513. }
  1514. } else if (codec_id == AV_CODEC_ID_PCM_S16LE) {
  1515. switch (track->audio.bitdepth) {
  1516. case 8: codec_id = AV_CODEC_ID_PCM_U8; break;
  1517. case 24: codec_id = AV_CODEC_ID_PCM_S24LE; break;
  1518. case 32: codec_id = AV_CODEC_ID_PCM_S32LE; break;
  1519. }
  1520. } else if (codec_id==AV_CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
  1521. codec_id = AV_CODEC_ID_PCM_F64LE;
  1522. } else if (codec_id == AV_CODEC_ID_AAC && !track->codec_priv.size) {
  1523. int profile = matroska_aac_profile(track->codec_id);
  1524. int sri = matroska_aac_sri(track->audio.samplerate);
  1525. extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
  1526. if (extradata == NULL)
  1527. return AVERROR(ENOMEM);
  1528. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  1529. extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
  1530. if (strstr(track->codec_id, "SBR")) {
  1531. sri = matroska_aac_sri(track->audio.out_samplerate);
  1532. extradata[2] = 0x56;
  1533. extradata[3] = 0xE5;
  1534. extradata[4] = 0x80 | (sri<<3);
  1535. extradata_size = 5;
  1536. } else
  1537. extradata_size = 2;
  1538. } else if (codec_id == AV_CODEC_ID_TTA) {
  1539. extradata_size = 30;
  1540. extradata = av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  1541. if (extradata == NULL)
  1542. return AVERROR(ENOMEM);
  1543. ffio_init_context(&b, extradata, extradata_size, 1,
  1544. NULL, NULL, NULL, NULL);
  1545. avio_write(&b, "TTA1", 4);
  1546. avio_wl16(&b, 1);
  1547. avio_wl16(&b, track->audio.channels);
  1548. avio_wl16(&b, track->audio.bitdepth);
  1549. avio_wl32(&b, track->audio.out_samplerate);
  1550. avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate);
  1551. } else if (codec_id == AV_CODEC_ID_RV10 || codec_id == AV_CODEC_ID_RV20 ||
  1552. codec_id == AV_CODEC_ID_RV30 || codec_id == AV_CODEC_ID_RV40) {
  1553. extradata_offset = 26;
  1554. } else if (codec_id == AV_CODEC_ID_RA_144) {
  1555. track->audio.out_samplerate = 8000;
  1556. track->audio.channels = 1;
  1557. } else if ((codec_id == AV_CODEC_ID_RA_288 || codec_id == AV_CODEC_ID_COOK ||
  1558. codec_id == AV_CODEC_ID_ATRAC3 || codec_id == AV_CODEC_ID_SIPR)
  1559. && track->codec_priv.data) {
  1560. int flavor;
  1561. ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size,
  1562. 0, NULL, NULL, NULL, NULL);
  1563. avio_skip(&b, 22);
  1564. flavor = avio_rb16(&b);
  1565. track->audio.coded_framesize = avio_rb32(&b);
  1566. avio_skip(&b, 12);
  1567. track->audio.sub_packet_h = avio_rb16(&b);
  1568. track->audio.frame_size = avio_rb16(&b);
  1569. track->audio.sub_packet_size = avio_rb16(&b);
  1570. track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
  1571. if (codec_id == AV_CODEC_ID_RA_288) {
  1572. st->codec->block_align = track->audio.coded_framesize;
  1573. track->codec_priv.size = 0;
  1574. } else {
  1575. if (codec_id == AV_CODEC_ID_SIPR && flavor < 4) {
  1576. const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
  1577. track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
  1578. st->codec->bit_rate = sipr_bit_rate[flavor];
  1579. }
  1580. st->codec->block_align = track->audio.sub_packet_size;
  1581. extradata_offset = 78;
  1582. }
  1583. }
  1584. track->codec_priv.size -= extradata_offset;
  1585. if (codec_id == AV_CODEC_ID_NONE)
  1586. av_log(matroska->ctx, AV_LOG_INFO,
  1587. "Unknown/unsupported AVCodecID %s.\n", track->codec_id);
  1588. if (track->time_scale < 0.01)
  1589. track->time_scale = 1.0;
  1590. avpriv_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  1591. st->codec->codec_id = codec_id;
  1592. st->start_time = 0;
  1593. if (strcmp(track->language, "und"))
  1594. av_dict_set(&st->metadata, "language", track->language, 0);
  1595. av_dict_set(&st->metadata, "title", track->name, 0);
  1596. if (track->flag_default)
  1597. st->disposition |= AV_DISPOSITION_DEFAULT;
  1598. if (track->flag_forced)
  1599. st->disposition |= AV_DISPOSITION_FORCED;
  1600. if (!st->codec->extradata) {
  1601. if(extradata){
  1602. st->codec->extradata = extradata;
  1603. st->codec->extradata_size = extradata_size;
  1604. } else if(track->codec_priv.data && track->codec_priv.size > 0){
  1605. st->codec->extradata = av_mallocz(track->codec_priv.size +
  1606. FF_INPUT_BUFFER_PADDING_SIZE);
  1607. if(st->codec->extradata == NULL)
  1608. return AVERROR(ENOMEM);
  1609. st->codec->extradata_size = track->codec_priv.size;
  1610. memcpy(st->codec->extradata,
  1611. track->codec_priv.data + extradata_offset,
  1612. track->codec_priv.size);
  1613. }
  1614. }
  1615. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1616. MatroskaTrackPlane *planes = track->operation.combine_planes.elem;
  1617. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1618. st->codec->codec_tag = fourcc;
  1619. st->codec->width = track->video.pixel_width;
  1620. st->codec->height = track->video.pixel_height;
  1621. av_reduce(&st->sample_aspect_ratio.num,
  1622. &st->sample_aspect_ratio.den,
  1623. st->codec->height * track->video.display_width,
  1624. st->codec-> width * track->video.display_height,
  1625. 255);
  1626. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1627. if (track->default_duration) {
  1628. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1629. 1000000000, track->default_duration, 30000);
  1630. #if FF_API_R_FRAME_RATE
  1631. st->r_frame_rate = st->avg_frame_rate;
  1632. #endif
  1633. }
  1634. /* export stereo mode flag as metadata tag */
  1635. if (track->video.stereo_mode && track->video.stereo_mode < MATROSKA_VIDEO_STEREO_MODE_COUNT)
  1636. av_dict_set(&st->metadata, "stereo_mode", ff_matroska_video_stereo_mode[track->video.stereo_mode], 0);
  1637. /* export alpha mode flag as metadata tag */
  1638. if (track->video.alpha_mode)
  1639. av_dict_set(&st->metadata, "alpha_mode", "1", 0);
  1640. /* if we have virtual track, mark the real tracks */
  1641. for (j=0; j < track->operation.combine_planes.nb_elem; j++) {
  1642. char buf[32];
  1643. if (planes[j].type >= MATROSKA_VIDEO_STEREO_PLANE_COUNT)
  1644. continue;
  1645. snprintf(buf, sizeof(buf), "%s_%d",
  1646. ff_matroska_video_stereo_plane[planes[j].type], i);
  1647. for (k=0; k < matroska->tracks.nb_elem; k++)
  1648. if (planes[j].uid == tracks[k].uid) {
  1649. av_dict_set(&s->streams[k]->metadata,
  1650. "stereo_mode", buf, 0);
  1651. break;
  1652. }
  1653. }
  1654. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1655. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1656. st->codec->sample_rate = track->audio.out_samplerate;
  1657. st->codec->channels = track->audio.channels;
  1658. st->codec->bits_per_coded_sample = track->audio.bitdepth;
  1659. if (st->codec->codec_id != AV_CODEC_ID_AAC)
  1660. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1661. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  1662. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1663. if (st->codec->codec_id == AV_CODEC_ID_SSA)
  1664. matroska->contains_ssa = 1;
  1665. }
  1666. }
  1667. attachements = attachements_list->elem;
  1668. for (j=0; j<attachements_list->nb_elem; j++) {
  1669. if (!(attachements[j].filename && attachements[j].mime &&
  1670. attachements[j].bin.data && attachements[j].bin.size > 0)) {
  1671. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1672. } else {
  1673. AVStream *st = avformat_new_stream(s, NULL);
  1674. if (st == NULL)
  1675. break;
  1676. av_dict_set(&st->metadata, "filename",attachements[j].filename, 0);
  1677. av_dict_set(&st->metadata, "mimetype", attachements[j].mime, 0);
  1678. st->codec->codec_id = AV_CODEC_ID_NONE;
  1679. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  1680. st->codec->extradata = av_malloc(attachements[j].bin.size + FF_INPUT_BUFFER_PADDING_SIZE);
  1681. if(st->codec->extradata == NULL)
  1682. break;
  1683. st->codec->extradata_size = attachements[j].bin.size;
  1684. memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
  1685. for (i=0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
  1686. if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
  1687. strlen(ff_mkv_mime_tags[i].str))) {
  1688. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1689. break;
  1690. }
  1691. }
  1692. attachements[j].stream = st;
  1693. }
  1694. }
  1695. chapters = chapters_list->elem;
  1696. for (i=0; i<chapters_list->nb_elem; i++)
  1697. if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
  1698. && (max_start==0 || chapters[i].start > max_start)) {
  1699. chapters[i].chapter =
  1700. avpriv_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
  1701. chapters[i].start, chapters[i].end,
  1702. chapters[i].title);
  1703. av_dict_set(&chapters[i].chapter->metadata,
  1704. "title", chapters[i].title, 0);
  1705. max_start = chapters[i].start;
  1706. }
  1707. matroska_add_index_entries(matroska);
  1708. matroska_convert_tags(s);
  1709. return 0;
  1710. }
  1711. /*
  1712. * Put one packet in an application-supplied AVPacket struct.
  1713. * Returns 0 on success or -1 on failure.
  1714. */
  1715. static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
  1716. AVPacket *pkt)
  1717. {
  1718. if (matroska->num_packets > 0) {
  1719. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  1720. av_free(matroska->packets[0]);
  1721. if (matroska->num_packets > 1) {
  1722. void *newpackets;
  1723. memmove(&matroska->packets[0], &matroska->packets[1],
  1724. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1725. newpackets = av_realloc(matroska->packets,
  1726. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1727. if (newpackets)
  1728. matroska->packets = newpackets;
  1729. } else {
  1730. av_freep(&matroska->packets);
  1731. matroska->prev_pkt = NULL;
  1732. }
  1733. matroska->num_packets--;
  1734. return 0;
  1735. }
  1736. return -1;
  1737. }
  1738. /*
  1739. * Free all packets in our internal queue.
  1740. */
  1741. static void matroska_clear_queue(MatroskaDemuxContext *matroska)
  1742. {
  1743. matroska->prev_pkt = NULL;
  1744. if (matroska->packets) {
  1745. int n;
  1746. for (n = 0; n < matroska->num_packets; n++) {
  1747. av_free_packet(matroska->packets[n]);
  1748. av_free(matroska->packets[n]);
  1749. }
  1750. av_freep(&matroska->packets);
  1751. matroska->num_packets = 0;
  1752. }
  1753. }
  1754. static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
  1755. int* buf_size, int type,
  1756. uint32_t **lace_buf, int *laces)
  1757. {
  1758. int res = 0, n, size = *buf_size;
  1759. uint8_t *data = *buf;
  1760. uint32_t *lace_size;
  1761. if (!type) {
  1762. *laces = 1;
  1763. *lace_buf = av_mallocz(sizeof(int));
  1764. if (!*lace_buf)
  1765. return AVERROR(ENOMEM);
  1766. *lace_buf[0] = size;
  1767. return 0;
  1768. }
  1769. av_assert0(size > 0);
  1770. *laces = *data + 1;
  1771. data += 1;
  1772. size -= 1;
  1773. lace_size = av_mallocz(*laces * sizeof(int));
  1774. if (!lace_size)
  1775. return AVERROR(ENOMEM);
  1776. switch (type) {
  1777. case 0x1: /* Xiph lacing */ {
  1778. uint8_t temp;
  1779. uint32_t total = 0;
  1780. for (n = 0; res == 0 && n < *laces - 1; n++) {
  1781. while (1) {
  1782. if (size <= total) {
  1783. res = AVERROR_INVALIDDATA;
  1784. break;
  1785. }
  1786. temp = *data;
  1787. total += temp;
  1788. lace_size[n] += temp;
  1789. data += 1;
  1790. size -= 1;
  1791. if (temp != 0xff)
  1792. break;
  1793. }
  1794. }
  1795. if (size <= total) {
  1796. res = AVERROR_INVALIDDATA;
  1797. break;
  1798. }
  1799. lace_size[n] = size - total;
  1800. break;
  1801. }
  1802. case 0x2: /* fixed-size lacing */
  1803. if (size % (*laces)) {
  1804. res = AVERROR_INVALIDDATA;
  1805. break;
  1806. }
  1807. for (n = 0; n < *laces; n++)
  1808. lace_size[n] = size / *laces;
  1809. break;
  1810. case 0x3: /* EBML lacing */ {
  1811. uint64_t num;
  1812. uint64_t total;
  1813. n = matroska_ebmlnum_uint(matroska, data, size, &num);
  1814. if (n < 0 || num > INT_MAX) {
  1815. av_log(matroska->ctx, AV_LOG_INFO,
  1816. "EBML block data error\n");
  1817. res = n<0 ? n : AVERROR_INVALIDDATA;
  1818. break;
  1819. }
  1820. data += n;
  1821. size -= n;
  1822. total = lace_size[0] = num;
  1823. for (n = 1; res == 0 && n < *laces - 1; n++) {
  1824. int64_t snum;
  1825. int r;
  1826. r = matroska_ebmlnum_sint(matroska, data, size, &snum);
  1827. if (r < 0 || lace_size[n - 1] + snum > (uint64_t)INT_MAX) {
  1828. av_log(matroska->ctx, AV_LOG_INFO,
  1829. "EBML block data error\n");
  1830. res = r<0 ? r : AVERROR_INVALIDDATA;
  1831. break;
  1832. }
  1833. data += r;
  1834. size -= r;
  1835. lace_size[n] = lace_size[n - 1] + snum;
  1836. total += lace_size[n];
  1837. }
  1838. if (size <= total) {
  1839. res = AVERROR_INVALIDDATA;
  1840. break;
  1841. }
  1842. lace_size[*laces - 1] = size - total;
  1843. break;
  1844. }
  1845. }
  1846. *buf = data;
  1847. *lace_buf = lace_size;
  1848. *buf_size = size;
  1849. return res;
  1850. }
  1851. static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska,
  1852. MatroskaTrack *track,
  1853. AVStream *st,
  1854. uint8_t *data, int size,
  1855. uint64_t timecode,
  1856. int64_t pos)
  1857. {
  1858. int a = st->codec->block_align;
  1859. int sps = track->audio.sub_packet_size;
  1860. int cfs = track->audio.coded_framesize;
  1861. int h = track->audio.sub_packet_h;
  1862. int y = track->audio.sub_packet_cnt;
  1863. int w = track->audio.frame_size;
  1864. int x;
  1865. if (!track->audio.pkt_cnt) {
  1866. if (track->audio.sub_packet_cnt == 0)
  1867. track->audio.buf_timecode = timecode;
  1868. if (st->codec->codec_id == AV_CODEC_ID_RA_288) {
  1869. if (size < cfs * h / 2) {
  1870. av_log(matroska->ctx, AV_LOG_ERROR,
  1871. "Corrupt int4 RM-style audio packet size\n");
  1872. return AVERROR_INVALIDDATA;
  1873. }
  1874. for (x=0; x<h/2; x++)
  1875. memcpy(track->audio.buf+x*2*w+y*cfs,
  1876. data+x*cfs, cfs);
  1877. } else if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
  1878. if (size < w) {
  1879. av_log(matroska->ctx, AV_LOG_ERROR,
  1880. "Corrupt sipr RM-style audio packet size\n");
  1881. return AVERROR_INVALIDDATA;
  1882. }
  1883. memcpy(track->audio.buf + y*w, data, w);
  1884. } else {
  1885. if (size < sps * w / sps || h<=0) {
  1886. av_log(matroska->ctx, AV_LOG_ERROR,
  1887. "Corrupt generic RM-style audio packet size\n");
  1888. return AVERROR_INVALIDDATA;
  1889. }
  1890. for (x=0; x<w/sps; x++)
  1891. memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  1892. }
  1893. if (++track->audio.sub_packet_cnt >= h) {
  1894. if (st->codec->codec_id == AV_CODEC_ID_SIPR)
  1895. ff_rm_reorder_sipr_data(track->audio.buf, h, w);
  1896. track->audio.sub_packet_cnt = 0;
  1897. track->audio.pkt_cnt = h*w / a;
  1898. }
  1899. }
  1900. while (track->audio.pkt_cnt) {
  1901. AVPacket *pkt = NULL;
  1902. if (!(pkt = av_mallocz(sizeof(AVPacket))) || av_new_packet(pkt, a) < 0){
  1903. av_free(pkt);
  1904. return AVERROR(ENOMEM);
  1905. }
  1906. memcpy(pkt->data, track->audio.buf
  1907. + a * (h*w / a - track->audio.pkt_cnt--), a);
  1908. pkt->pts = track->audio.buf_timecode;
  1909. track->audio.buf_timecode = AV_NOPTS_VALUE;
  1910. pkt->pos = pos;
  1911. pkt->stream_index = st->index;
  1912. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1913. }
  1914. return 0;
  1915. }
  1916. static int matroska_parse_frame(MatroskaDemuxContext *matroska,
  1917. MatroskaTrack *track,
  1918. AVStream *st,
  1919. uint8_t *data, int pkt_size,
  1920. uint64_t timecode, uint64_t lace_duration,
  1921. int64_t pos, int is_keyframe,
  1922. uint8_t *additional, uint64_t additional_id, int additional_size)
  1923. {
  1924. MatroskaTrackEncoding *encodings = track->encodings.elem;
  1925. uint8_t *pkt_data = data;
  1926. int offset = 0, res;
  1927. AVPacket *pkt;
  1928. if (encodings && !encodings->type && encodings->scope & 1) {
  1929. res = matroska_decode_buffer(&pkt_data, &pkt_size, track);
  1930. if (res < 0)
  1931. return res;
  1932. }
  1933. if (st->codec->codec_id == AV_CODEC_ID_PRORES)
  1934. offset = 8;
  1935. pkt = av_mallocz(sizeof(AVPacket));
  1936. /* XXX: prevent data copy... */
  1937. if (av_new_packet(pkt, pkt_size + offset) < 0) {
  1938. av_free(pkt);
  1939. return AVERROR(ENOMEM);
  1940. }
  1941. if (st->codec->codec_id == AV_CODEC_ID_PRORES) {
  1942. uint8_t *buf = pkt->data;
  1943. bytestream_put_be32(&buf, pkt_size);
  1944. bytestream_put_be32(&buf, MKBETAG('i', 'c', 'p', 'f'));
  1945. }
  1946. memcpy(pkt->data + offset, pkt_data, pkt_size);
  1947. if (pkt_data != data)
  1948. av_free(pkt_data);
  1949. pkt->flags = is_keyframe;
  1950. pkt->stream_index = st->index;
  1951. if (additional_size > 0) {
  1952. uint8_t *side_data = av_packet_new_side_data(pkt,
  1953. AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
  1954. additional_size + 8);
  1955. if(side_data == NULL) {
  1956. av_free_packet(pkt);
  1957. av_free(pkt);
  1958. return AVERROR(ENOMEM);
  1959. }
  1960. AV_WB64(side_data, additional_id);
  1961. memcpy(side_data + 8, additional, additional_size);
  1962. }
  1963. if (track->ms_compat)
  1964. pkt->dts = timecode;
  1965. else
  1966. pkt->pts = timecode;
  1967. pkt->pos = pos;
  1968. if (st->codec->codec_id == AV_CODEC_ID_SUBRIP) {
  1969. /*
  1970. * For backward compatibility.
  1971. * Historically, we have put subtitle duration
  1972. * in convergence_duration, on the off chance
  1973. * that the time_scale is less than 1us, which
  1974. * could result in a 32bit overflow on the
  1975. * normal duration field.
  1976. */
  1977. pkt->convergence_duration = lace_duration;
  1978. }
  1979. if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE ||
  1980. lace_duration <= INT_MAX) {
  1981. /*
  1982. * For non subtitle tracks, just store the duration
  1983. * as normal.
  1984. *
  1985. * If it's a subtitle track and duration value does
  1986. * not overflow a uint32, then also store it normally.
  1987. */
  1988. pkt->duration = lace_duration;
  1989. }
  1990. if (st->codec->codec_id == AV_CODEC_ID_SSA)
  1991. matroska_fix_ass_packet(matroska, pkt, lace_duration);
  1992. if (matroska->prev_pkt &&
  1993. timecode != AV_NOPTS_VALUE &&
  1994. matroska->prev_pkt->pts == timecode &&
  1995. matroska->prev_pkt->stream_index == st->index &&
  1996. st->codec->codec_id == AV_CODEC_ID_SSA)
  1997. matroska_merge_packets(matroska->prev_pkt, pkt);
  1998. else {
  1999. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  2000. matroska->prev_pkt = pkt;
  2001. }
  2002. return 0;
  2003. }
  2004. static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
  2005. int size, int64_t pos, uint64_t cluster_time,
  2006. uint64_t block_duration, int is_keyframe,
  2007. uint8_t *additional, uint64_t additional_id, int additional_size,
  2008. int64_t cluster_pos)
  2009. {
  2010. uint64_t timecode = AV_NOPTS_VALUE;
  2011. MatroskaTrack *track;
  2012. int res = 0;
  2013. AVStream *st;
  2014. int16_t block_time;
  2015. uint32_t *lace_size = NULL;
  2016. int n, flags, laces = 0;
  2017. uint64_t num;
  2018. if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
  2019. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  2020. return n;
  2021. }
  2022. data += n;
  2023. size -= n;
  2024. track = matroska_find_track_by_num(matroska, num);
  2025. if (!track || !track->stream) {
  2026. av_log(matroska->ctx, AV_LOG_INFO,
  2027. "Invalid stream %"PRIu64" or size %u\n", num, size);
  2028. return AVERROR_INVALIDDATA;
  2029. } else if (size <= 3)
  2030. return 0;
  2031. st = track->stream;
  2032. if (st->discard >= AVDISCARD_ALL)
  2033. return res;
  2034. av_assert1(block_duration != AV_NOPTS_VALUE);
  2035. block_time = AV_RB16(data);
  2036. data += 2;
  2037. flags = *data++;
  2038. size -= 3;
  2039. if (is_keyframe == -1)
  2040. is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
  2041. if (cluster_time != (uint64_t)-1
  2042. && (block_time >= 0 || cluster_time >= -block_time)) {
  2043. timecode = cluster_time + block_time;
  2044. if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
  2045. && timecode < track->end_timecode)
  2046. is_keyframe = 0; /* overlapping subtitles are not key frame */
  2047. if (is_keyframe)
  2048. av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
  2049. }
  2050. if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  2051. if (timecode < matroska->skip_to_timecode)
  2052. return res;
  2053. if (!st->skip_to_keyframe) {
  2054. av_log(matroska->ctx, AV_LOG_ERROR, "File is broken, keyframes not correctly marked!\n");
  2055. matroska->skip_to_keyframe = 0;
  2056. }
  2057. if (is_keyframe)
  2058. matroska->skip_to_keyframe = 0;
  2059. }
  2060. res = matroska_parse_laces(matroska, &data, &size, (flags & 0x06) >> 1,
  2061. &lace_size, &laces);
  2062. if (res)
  2063. goto end;
  2064. if (!block_duration)
  2065. block_duration = track->default_duration * laces / matroska->time_scale;
  2066. if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
  2067. track->end_timecode =
  2068. FFMAX(track->end_timecode, timecode + block_duration);
  2069. for (n = 0; n < laces; n++) {
  2070. int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
  2071. if (lace_size[n] > size) {
  2072. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
  2073. break;
  2074. }
  2075. if ((st->codec->codec_id == AV_CODEC_ID_RA_288 ||
  2076. st->codec->codec_id == AV_CODEC_ID_COOK ||
  2077. st->codec->codec_id == AV_CODEC_ID_SIPR ||
  2078. st->codec->codec_id == AV_CODEC_ID_ATRAC3) &&
  2079. st->codec->block_align && track->audio.sub_packet_size) {
  2080. res = matroska_parse_rm_audio(matroska, track, st, data,
  2081. lace_size[n],
  2082. timecode, pos);
  2083. if (res)
  2084. goto end;
  2085. } else {
  2086. res = matroska_parse_frame(matroska, track, st, data, lace_size[n],
  2087. timecode, lace_duration,
  2088. pos, !n? is_keyframe : 0,
  2089. additional, additional_id, additional_size);
  2090. if (res)
  2091. goto end;
  2092. }
  2093. if (timecode != AV_NOPTS_VALUE)
  2094. timecode = lace_duration ? timecode + lace_duration : AV_NOPTS_VALUE;
  2095. data += lace_size[n];
  2096. size -= lace_size[n];
  2097. }
  2098. end:
  2099. av_free(lace_size);
  2100. return res;
  2101. }
  2102. static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska)
  2103. {
  2104. EbmlList *blocks_list;
  2105. MatroskaBlock *blocks;
  2106. int i, res;
  2107. res = ebml_parse(matroska,
  2108. matroska_cluster_incremental_parsing,
  2109. &matroska->current_cluster);
  2110. if (res == 1) {
  2111. /* New Cluster */
  2112. if (matroska->current_cluster_pos)
  2113. ebml_level_end(matroska);
  2114. ebml_free(matroska_cluster, &matroska->current_cluster);
  2115. memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster));
  2116. matroska->current_cluster_num_blocks = 0;
  2117. matroska->current_cluster_pos = avio_tell(matroska->ctx->pb);
  2118. matroska->prev_pkt = NULL;
  2119. /* sizeof the ID which was already read */
  2120. if (matroska->current_id)
  2121. matroska->current_cluster_pos -= 4;
  2122. res = ebml_parse(matroska,
  2123. matroska_clusters_incremental,
  2124. &matroska->current_cluster);
  2125. /* Try parsing the block again. */
  2126. if (res == 1)
  2127. res = ebml_parse(matroska,
  2128. matroska_cluster_incremental_parsing,
  2129. &matroska->current_cluster);
  2130. }
  2131. if (!res &&
  2132. matroska->current_cluster_num_blocks <
  2133. matroska->current_cluster.blocks.nb_elem) {
  2134. blocks_list = &matroska->current_cluster.blocks;
  2135. blocks = blocks_list->elem;
  2136. matroska->current_cluster_num_blocks = blocks_list->nb_elem;
  2137. i = blocks_list->nb_elem - 1;
  2138. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  2139. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  2140. uint8_t* additional = blocks[i].additional.size > 0 ?
  2141. blocks[i].additional.data : NULL;
  2142. if (!blocks[i].non_simple)
  2143. blocks[i].duration = 0;
  2144. res = matroska_parse_block(matroska,
  2145. blocks[i].bin.data, blocks[i].bin.size,
  2146. blocks[i].bin.pos,
  2147. matroska->current_cluster.timecode,
  2148. blocks[i].duration, is_keyframe,
  2149. additional, blocks[i].additional_id,
  2150. blocks[i].additional.size,
  2151. matroska->current_cluster_pos);
  2152. }
  2153. }
  2154. if (res < 0) matroska->done = 1;
  2155. return res;
  2156. }
  2157. static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
  2158. {
  2159. MatroskaCluster cluster = { 0 };
  2160. EbmlList *blocks_list;
  2161. MatroskaBlock *blocks;
  2162. int i, res;
  2163. int64_t pos;
  2164. if (!matroska->contains_ssa)
  2165. return matroska_parse_cluster_incremental(matroska);
  2166. pos = avio_tell(matroska->ctx->pb);
  2167. matroska->prev_pkt = NULL;
  2168. if (matroska->current_id)
  2169. pos -= 4; /* sizeof the ID which was already read */
  2170. res = ebml_parse(matroska, matroska_clusters, &cluster);
  2171. blocks_list = &cluster.blocks;
  2172. blocks = blocks_list->elem;
  2173. for (i=0; i<blocks_list->nb_elem; i++)
  2174. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  2175. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  2176. res=matroska_parse_block(matroska,
  2177. blocks[i].bin.data, blocks[i].bin.size,
  2178. blocks[i].bin.pos, cluster.timecode,
  2179. blocks[i].duration, is_keyframe, NULL, 0, 0,
  2180. pos);
  2181. }
  2182. ebml_free(matroska_cluster, &cluster);
  2183. return res;
  2184. }
  2185. static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
  2186. {
  2187. MatroskaDemuxContext *matroska = s->priv_data;
  2188. while (matroska_deliver_packet(matroska, pkt)) {
  2189. int64_t pos = avio_tell(matroska->ctx->pb);
  2190. if (matroska->done)
  2191. return AVERROR_EOF;
  2192. if (matroska_parse_cluster(matroska) < 0)
  2193. matroska_resync(matroska, pos);
  2194. }
  2195. return 0;
  2196. }
  2197. static int matroska_read_seek(AVFormatContext *s, int stream_index,
  2198. int64_t timestamp, int flags)
  2199. {
  2200. MatroskaDemuxContext *matroska = s->priv_data;
  2201. MatroskaTrack *tracks = matroska->tracks.elem;
  2202. AVStream *st = s->streams[stream_index];
  2203. int i, index, index_sub, index_min;
  2204. /* Parse the CUES now since we need the index data to seek. */
  2205. if (matroska->cues_parsing_deferred > 0) {
  2206. matroska->cues_parsing_deferred = 0;
  2207. matroska_parse_cues(matroska);
  2208. }
  2209. if (!st->nb_index_entries)
  2210. goto err;
  2211. timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
  2212. if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  2213. avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
  2214. matroska->current_id = 0;
  2215. while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  2216. matroska_clear_queue(matroska);
  2217. if (matroska_parse_cluster(matroska) < 0)
  2218. break;
  2219. }
  2220. }
  2221. matroska_clear_queue(matroska);
  2222. if (index < 0 || (matroska->cues_parsing_deferred < 0 && index == st->nb_index_entries - 1))
  2223. goto err;
  2224. index_min = index;
  2225. for (i=0; i < matroska->tracks.nb_elem; i++) {
  2226. tracks[i].audio.pkt_cnt = 0;
  2227. tracks[i].audio.sub_packet_cnt = 0;
  2228. tracks[i].audio.buf_timecode = AV_NOPTS_VALUE;
  2229. tracks[i].end_timecode = 0;
  2230. if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
  2231. && tracks[i].stream->discard != AVDISCARD_ALL) {
  2232. index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
  2233. if (index_sub >= 0
  2234. && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
  2235. && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
  2236. index_min = index_sub;
  2237. }
  2238. }
  2239. avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
  2240. matroska->current_id = 0;
  2241. if (flags & AVSEEK_FLAG_ANY) {
  2242. st->skip_to_keyframe = 0;
  2243. matroska->skip_to_timecode = timestamp;
  2244. } else {
  2245. st->skip_to_keyframe = 1;
  2246. matroska->skip_to_timecode = st->index_entries[index].timestamp;
  2247. }
  2248. matroska->skip_to_keyframe = 1;
  2249. matroska->done = 0;
  2250. matroska->num_levels = 0;
  2251. ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
  2252. return 0;
  2253. err:
  2254. // slightly hackish but allows proper fallback to
  2255. // the generic seeking code.
  2256. matroska_clear_queue(matroska);
  2257. matroska->current_id = 0;
  2258. st->skip_to_keyframe =
  2259. matroska->skip_to_keyframe = 0;
  2260. matroska->done = 0;
  2261. matroska->num_levels = 0;
  2262. return -1;
  2263. }
  2264. static int matroska_read_close(AVFormatContext *s)
  2265. {
  2266. MatroskaDemuxContext *matroska = s->priv_data;
  2267. MatroskaTrack *tracks = matroska->tracks.elem;
  2268. int n;
  2269. matroska_clear_queue(matroska);
  2270. for (n=0; n < matroska->tracks.nb_elem; n++)
  2271. if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
  2272. av_free(tracks[n].audio.buf);
  2273. ebml_free(matroska_cluster, &matroska->current_cluster);
  2274. ebml_free(matroska_segment, matroska);
  2275. return 0;
  2276. }
  2277. AVInputFormat ff_matroska_demuxer = {
  2278. .name = "matroska,webm",
  2279. .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
  2280. .priv_data_size = sizeof(MatroskaDemuxContext),
  2281. .read_probe = matroska_probe,
  2282. .read_header = matroska_read_header,
  2283. .read_packet = matroska_read_packet,
  2284. .read_close = matroska_read_close,
  2285. .read_seek = matroska_read_seek,
  2286. };