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.

2781 lines
97KB

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