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.

2631 lines
91KB

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