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.

2737 lines
95KB

  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 + AV_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 + AV_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_packet_unref(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. int found = 0;
  1131. for (j = 0; j < matroska->attachments.nb_elem; j++) {
  1132. if (attachment[j].uid == tags[i].target.attachuid &&
  1133. attachment[j].stream) {
  1134. matroska_convert_tag(s, &tags[i].tag,
  1135. &attachment[j].stream->metadata, NULL);
  1136. found = 1;
  1137. }
  1138. }
  1139. if (!found) {
  1140. av_log(NULL, AV_LOG_WARNING,
  1141. "The tags at index %d refer to a "
  1142. "non-existent attachment %"PRId64".\n",
  1143. i, tags[i].target.attachuid);
  1144. }
  1145. } else if (tags[i].target.chapteruid) {
  1146. MatroskaChapter *chapter = matroska->chapters.elem;
  1147. int found = 0;
  1148. for (j = 0; j < matroska->chapters.nb_elem; j++) {
  1149. if (chapter[j].uid == tags[i].target.chapteruid &&
  1150. chapter[j].chapter) {
  1151. matroska_convert_tag(s, &tags[i].tag,
  1152. &chapter[j].chapter->metadata, NULL);
  1153. found = 1;
  1154. }
  1155. }
  1156. if (!found) {
  1157. av_log(NULL, AV_LOG_WARNING,
  1158. "The tags at index %d refer to a non-existent chapter "
  1159. "%"PRId64".\n",
  1160. i, tags[i].target.chapteruid);
  1161. }
  1162. } else if (tags[i].target.trackuid) {
  1163. MatroskaTrack *track = matroska->tracks.elem;
  1164. int found = 0;
  1165. for (j = 0; j < matroska->tracks.nb_elem; j++) {
  1166. if (track[j].uid == tags[i].target.trackuid &&
  1167. track[j].stream) {
  1168. matroska_convert_tag(s, &tags[i].tag,
  1169. &track[j].stream->metadata, NULL);
  1170. found = 1;
  1171. }
  1172. }
  1173. if (!found) {
  1174. av_log(NULL, AV_LOG_WARNING,
  1175. "The tags at index %d refer to a non-existent track "
  1176. "%"PRId64".\n",
  1177. i, tags[i].target.trackuid);
  1178. }
  1179. } else {
  1180. matroska_convert_tag(s, &tags[i].tag, &s->metadata,
  1181. tags[i].target.type);
  1182. }
  1183. }
  1184. }
  1185. static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska,
  1186. int idx)
  1187. {
  1188. EbmlList *seekhead_list = &matroska->seekhead;
  1189. uint32_t level_up = matroska->level_up;
  1190. uint32_t saved_id = matroska->current_id;
  1191. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1192. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1193. MatroskaLevel level;
  1194. int64_t offset;
  1195. int ret = 0;
  1196. if (idx >= seekhead_list->nb_elem ||
  1197. seekhead[idx].id == MATROSKA_ID_SEEKHEAD ||
  1198. seekhead[idx].id == MATROSKA_ID_CLUSTER)
  1199. return 0;
  1200. /* seek */
  1201. offset = seekhead[idx].pos + matroska->segment_start;
  1202. if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) {
  1203. /* We don't want to lose our seekhead level, so we add
  1204. * a dummy. This is a crude hack. */
  1205. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1206. av_log(matroska->ctx, AV_LOG_INFO,
  1207. "Max EBML element depth (%d) reached, "
  1208. "cannot parse further.\n", EBML_MAX_DEPTH);
  1209. ret = AVERROR_INVALIDDATA;
  1210. } else {
  1211. level.start = 0;
  1212. level.length = (uint64_t) -1;
  1213. matroska->levels[matroska->num_levels] = level;
  1214. matroska->num_levels++;
  1215. matroska->current_id = 0;
  1216. ret = ebml_parse(matroska, matroska_segment, matroska);
  1217. /* remove dummy level */
  1218. while (matroska->num_levels) {
  1219. uint64_t length = matroska->levels[--matroska->num_levels].length;
  1220. if (length == (uint64_t) -1)
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. /* seek back */
  1226. avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
  1227. matroska->level_up = level_up;
  1228. matroska->current_id = saved_id;
  1229. return ret;
  1230. }
  1231. static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
  1232. {
  1233. EbmlList *seekhead_list = &matroska->seekhead;
  1234. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1235. int i;
  1236. // we should not do any seeking in the streaming case
  1237. if (!matroska->ctx->pb->seekable ||
  1238. (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
  1239. return;
  1240. for (i = 0; i < seekhead_list->nb_elem; i++) {
  1241. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1242. if (seekhead[i].pos <= before_pos)
  1243. continue;
  1244. // defer cues parsing until we actually need cue data.
  1245. if (seekhead[i].id == MATROSKA_ID_CUES) {
  1246. matroska->cues_parsing_deferred = 1;
  1247. continue;
  1248. }
  1249. if (matroska_parse_seekhead_entry(matroska, i) < 0)
  1250. break;
  1251. }
  1252. }
  1253. static void matroska_parse_cues(MatroskaDemuxContext *matroska)
  1254. {
  1255. EbmlList *seekhead_list = &matroska->seekhead;
  1256. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1257. EbmlList *index_list;
  1258. MatroskaIndex *index;
  1259. int index_scale = 1;
  1260. int i, j;
  1261. for (i = 0; i < seekhead_list->nb_elem; i++)
  1262. if (seekhead[i].id == MATROSKA_ID_CUES)
  1263. break;
  1264. assert(i <= seekhead_list->nb_elem);
  1265. matroska_parse_seekhead_entry(matroska, i);
  1266. index_list = &matroska->index;
  1267. index = index_list->elem;
  1268. if (index_list->nb_elem &&
  1269. index[0].time > 1E14 / matroska->time_scale) {
  1270. av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
  1271. index_scale = matroska->time_scale;
  1272. }
  1273. for (i = 0; i < index_list->nb_elem; i++) {
  1274. EbmlList *pos_list = &index[i].pos;
  1275. MatroskaIndexPos *pos = pos_list->elem;
  1276. for (j = 0; j < pos_list->nb_elem; j++) {
  1277. MatroskaTrack *track = matroska_find_track_by_num(matroska,
  1278. pos[j].track);
  1279. if (track && track->stream)
  1280. av_add_index_entry(track->stream,
  1281. pos[j].pos + matroska->segment_start,
  1282. index[i].time / index_scale, 0, 0,
  1283. AVINDEX_KEYFRAME);
  1284. }
  1285. }
  1286. }
  1287. static int matroska_aac_profile(char *codec_id)
  1288. {
  1289. static const char *const aac_profiles[] = { "MAIN", "LC", "SSR" };
  1290. int profile;
  1291. for (profile = 0; profile < FF_ARRAY_ELEMS(aac_profiles); profile++)
  1292. if (strstr(codec_id, aac_profiles[profile]))
  1293. break;
  1294. return profile + 1;
  1295. }
  1296. static int matroska_aac_sri(int samplerate)
  1297. {
  1298. int sri;
  1299. for (sri = 0; sri < FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++)
  1300. if (avpriv_mpeg4audio_sample_rates[sri] == samplerate)
  1301. break;
  1302. return sri;
  1303. }
  1304. static int matroska_parse_flac(AVFormatContext *s,
  1305. MatroskaTrack *track,
  1306. int *offset)
  1307. {
  1308. AVStream *st = track->stream;
  1309. uint8_t *p = track->codec_priv.data;
  1310. int size = track->codec_priv.size;
  1311. if (size < 8 + FLAC_STREAMINFO_SIZE || p[4] & 0x7f) {
  1312. av_log(s, AV_LOG_WARNING, "Invalid FLAC private data\n");
  1313. track->codec_priv.size = 0;
  1314. return 0;
  1315. }
  1316. *offset = 8;
  1317. track->codec_priv.size = 8 + FLAC_STREAMINFO_SIZE;
  1318. p += track->codec_priv.size;
  1319. size -= track->codec_priv.size;
  1320. /* parse the remaining metadata blocks if present */
  1321. while (size >= 4) {
  1322. int block_last, block_type, block_size;
  1323. flac_parse_block_header(p, &block_last, &block_type, &block_size);
  1324. p += 4;
  1325. size -= 4;
  1326. if (block_size > size)
  1327. return 0;
  1328. /* check for the channel mask */
  1329. if (block_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  1330. AVDictionary *dict = NULL;
  1331. AVDictionaryEntry *chmask;
  1332. ff_vorbis_comment(s, &dict, p, block_size, 0);
  1333. chmask = av_dict_get(dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  1334. if (chmask) {
  1335. uint64_t mask = strtol(chmask->value, NULL, 0);
  1336. if (!mask || mask & ~0x3ffffULL) {
  1337. av_log(s, AV_LOG_WARNING,
  1338. "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  1339. } else
  1340. st->codec->channel_layout = mask;
  1341. }
  1342. av_dict_free(&dict);
  1343. }
  1344. p += block_size;
  1345. size -= block_size;
  1346. }
  1347. return 0;
  1348. }
  1349. static void mkv_stereo_mode_display_mul(int stereo_mode,
  1350. int *h_width, int *h_height)
  1351. {
  1352. switch (stereo_mode) {
  1353. case MATROSKA_VIDEO_STEREOMODE_TYPE_MONO:
  1354. case MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_RL:
  1355. case MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR:
  1356. case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_RL:
  1357. case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR:
  1358. break;
  1359. case MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT:
  1360. case MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT:
  1361. case MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_RL:
  1362. case MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR:
  1363. *h_width = 2;
  1364. break;
  1365. case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTTOM_TOP:
  1366. case MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM:
  1367. case MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_RL:
  1368. case MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR:
  1369. *h_height = 2;
  1370. break;
  1371. }
  1372. }
  1373. static int matroska_parse_tracks(AVFormatContext *s)
  1374. {
  1375. MatroskaDemuxContext *matroska = s->priv_data;
  1376. MatroskaTrack *tracks = matroska->tracks.elem;
  1377. AVStream *st;
  1378. int i, j, ret;
  1379. for (i = 0; i < matroska->tracks.nb_elem; i++) {
  1380. MatroskaTrack *track = &tracks[i];
  1381. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  1382. EbmlList *encodings_list = &track->encodings;
  1383. MatroskaTrackEncoding *encodings = encodings_list->elem;
  1384. uint8_t *extradata = NULL;
  1385. int extradata_size = 0;
  1386. int extradata_offset = 0;
  1387. AVIOContext b;
  1388. /* Apply some sanity checks. */
  1389. if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
  1390. track->type != MATROSKA_TRACK_TYPE_AUDIO &&
  1391. track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1392. av_log(matroska->ctx, AV_LOG_INFO,
  1393. "Unknown or unsupported track type %"PRIu64"\n",
  1394. track->type);
  1395. continue;
  1396. }
  1397. if (!track->codec_id)
  1398. continue;
  1399. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1400. if (!track->default_duration && track->video.frame_rate > 0)
  1401. track->default_duration = 1000000000 / track->video.frame_rate;
  1402. if (!track->video.display_width)
  1403. track->video.display_width = track->video.pixel_width;
  1404. if (!track->video.display_height)
  1405. track->video.display_height = track->video.pixel_height;
  1406. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1407. if (!track->audio.out_samplerate)
  1408. track->audio.out_samplerate = track->audio.samplerate;
  1409. }
  1410. if (encodings_list->nb_elem > 1) {
  1411. av_log(matroska->ctx, AV_LOG_ERROR,
  1412. "Multiple combined encodings not supported");
  1413. } else if (encodings_list->nb_elem == 1) {
  1414. if (encodings[0].type ||
  1415. (
  1416. #if CONFIG_ZLIB
  1417. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1418. #endif
  1419. #if CONFIG_BZLIB
  1420. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1421. #endif
  1422. #if CONFIG_LZO
  1423. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO &&
  1424. #endif
  1425. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP)) {
  1426. encodings[0].scope = 0;
  1427. av_log(matroska->ctx, AV_LOG_ERROR,
  1428. "Unsupported encoding type");
  1429. } else if (track->codec_priv.size && encodings[0].scope & 2) {
  1430. uint8_t *codec_priv = track->codec_priv.data;
  1431. int ret = matroska_decode_buffer(&track->codec_priv.data,
  1432. &track->codec_priv.size,
  1433. track);
  1434. if (ret < 0) {
  1435. track->codec_priv.data = NULL;
  1436. track->codec_priv.size = 0;
  1437. av_log(matroska->ctx, AV_LOG_ERROR,
  1438. "Failed to decode codec private data\n");
  1439. }
  1440. if (codec_priv != track->codec_priv.data)
  1441. av_free(codec_priv);
  1442. }
  1443. }
  1444. for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
  1445. if (!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  1446. strlen(ff_mkv_codec_tags[j].str))) {
  1447. codec_id = ff_mkv_codec_tags[j].id;
  1448. break;
  1449. }
  1450. }
  1451. st = track->stream = avformat_new_stream(s, NULL);
  1452. if (!st)
  1453. return AVERROR(ENOMEM);
  1454. if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") &&
  1455. track->codec_priv.size >= 40 &&
  1456. track->codec_priv.data) {
  1457. track->ms_compat = 1;
  1458. track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
  1459. codec_id = ff_codec_get_id(ff_codec_bmp_tags,
  1460. track->video.fourcc);
  1461. extradata_offset = 40;
  1462. } else if (!strcmp(track->codec_id, "A_MS/ACM") &&
  1463. track->codec_priv.size >= 14 &&
  1464. track->codec_priv.data) {
  1465. int ret;
  1466. ffio_init_context(&b, track->codec_priv.data,
  1467. track->codec_priv.size,
  1468. 0, NULL, NULL, NULL, NULL);
  1469. ret = ff_get_wav_header(s, &b, st->codec, track->codec_priv.size);
  1470. if (ret < 0)
  1471. return ret;
  1472. codec_id = st->codec->codec_id;
  1473. extradata_offset = FFMIN(track->codec_priv.size, 18);
  1474. } else if (!strcmp(track->codec_id, "V_QUICKTIME") &&
  1475. (track->codec_priv.size >= 86) &&
  1476. (track->codec_priv.data)) {
  1477. track->video.fourcc = AV_RL32(track->codec_priv.data);
  1478. codec_id = ff_codec_get_id(ff_codec_movvideo_tags,
  1479. track->video.fourcc);
  1480. if (codec_id == AV_CODEC_ID_NONE) {
  1481. char buf[32];
  1482. av_get_codec_tag_string(buf, sizeof(buf), track->video.fourcc);
  1483. av_log(matroska->ctx, AV_LOG_ERROR,
  1484. "mov FourCC not found %s.\n", buf);
  1485. }
  1486. } else if (codec_id == AV_CODEC_ID_PCM_S16BE) {
  1487. switch (track->audio.bitdepth) {
  1488. case 8:
  1489. codec_id = AV_CODEC_ID_PCM_U8;
  1490. break;
  1491. case 24:
  1492. codec_id = AV_CODEC_ID_PCM_S24BE;
  1493. break;
  1494. case 32:
  1495. codec_id = AV_CODEC_ID_PCM_S32BE;
  1496. break;
  1497. }
  1498. } else if (codec_id == AV_CODEC_ID_PCM_S16LE) {
  1499. switch (track->audio.bitdepth) {
  1500. case 8:
  1501. codec_id = AV_CODEC_ID_PCM_U8;
  1502. break;
  1503. case 24:
  1504. codec_id = AV_CODEC_ID_PCM_S24LE;
  1505. break;
  1506. case 32:
  1507. codec_id = AV_CODEC_ID_PCM_S32LE;
  1508. break;
  1509. }
  1510. } else if (codec_id == AV_CODEC_ID_PCM_F32LE &&
  1511. track->audio.bitdepth == 64) {
  1512. codec_id = AV_CODEC_ID_PCM_F64LE;
  1513. } else if (codec_id == AV_CODEC_ID_AAC && !track->codec_priv.size) {
  1514. int profile = matroska_aac_profile(track->codec_id);
  1515. int sri = matroska_aac_sri(track->audio.samplerate);
  1516. extradata = av_mallocz(5 + AV_INPUT_BUFFER_PADDING_SIZE);
  1517. if (!extradata)
  1518. return AVERROR(ENOMEM);
  1519. extradata[0] = (profile << 3) | ((sri & 0x0E) >> 1);
  1520. extradata[1] = ((sri & 0x01) << 7) | (track->audio.channels << 3);
  1521. if (strstr(track->codec_id, "SBR")) {
  1522. sri = matroska_aac_sri(track->audio.out_samplerate);
  1523. extradata[2] = 0x56;
  1524. extradata[3] = 0xE5;
  1525. extradata[4] = 0x80 | (sri << 3);
  1526. extradata_size = 5;
  1527. } else
  1528. extradata_size = 2;
  1529. } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size) {
  1530. /* Only ALAC's magic cookie is stored in Matroska's track headers.
  1531. * Create the "atom size", "tag", and "tag version" fields the
  1532. * decoder expects manually. */
  1533. extradata_size = 12 + track->codec_priv.size;
  1534. extradata = av_mallocz(extradata_size +
  1535. AV_INPUT_BUFFER_PADDING_SIZE);
  1536. if (!extradata)
  1537. return AVERROR(ENOMEM);
  1538. AV_WB32(extradata, extradata_size);
  1539. memcpy(&extradata[4], "alac", 4);
  1540. AV_WB32(&extradata[8], 0);
  1541. memcpy(&extradata[12], track->codec_priv.data,
  1542. track->codec_priv.size);
  1543. } else if (codec_id == AV_CODEC_ID_TTA) {
  1544. extradata_size = 30;
  1545. extradata = av_mallocz(extradata_size);
  1546. if (!extradata)
  1547. return AVERROR(ENOMEM);
  1548. ffio_init_context(&b, extradata, extradata_size, 1,
  1549. NULL, NULL, NULL, NULL);
  1550. avio_write(&b, "TTA1", 4);
  1551. avio_wl16(&b, 1);
  1552. avio_wl16(&b, track->audio.channels);
  1553. avio_wl16(&b, track->audio.bitdepth);
  1554. avio_wl32(&b, track->audio.out_samplerate);
  1555. avio_wl32(&b, matroska->ctx->duration *
  1556. track->audio.out_samplerate);
  1557. } else if (codec_id == AV_CODEC_ID_RV10 ||
  1558. codec_id == AV_CODEC_ID_RV20 ||
  1559. codec_id == AV_CODEC_ID_RV30 ||
  1560. codec_id == AV_CODEC_ID_RV40) {
  1561. extradata_offset = 26;
  1562. } else if (codec_id == AV_CODEC_ID_RA_144) {
  1563. track->audio.out_samplerate = 8000;
  1564. track->audio.channels = 1;
  1565. } else if (codec_id == AV_CODEC_ID_RA_288 ||
  1566. codec_id == AV_CODEC_ID_COOK ||
  1567. codec_id == AV_CODEC_ID_ATRAC3 ||
  1568. codec_id == AV_CODEC_ID_SIPR) {
  1569. int flavor;
  1570. ffio_init_context(&b, track->codec_priv.data,
  1571. track->codec_priv.size,
  1572. 0, NULL, NULL, NULL, NULL);
  1573. avio_skip(&b, 22);
  1574. flavor = avio_rb16(&b);
  1575. track->audio.coded_framesize = avio_rb32(&b);
  1576. avio_skip(&b, 12);
  1577. track->audio.sub_packet_h = avio_rb16(&b);
  1578. track->audio.frame_size = avio_rb16(&b);
  1579. track->audio.sub_packet_size = avio_rb16(&b);
  1580. if (flavor <= 0 ||
  1581. track->audio.coded_framesize <= 0 ||
  1582. track->audio.sub_packet_h <= 0 ||
  1583. track->audio.frame_size <= 0 ||
  1584. track->audio.sub_packet_size <= 0)
  1585. return AVERROR_INVALIDDATA;
  1586. track->audio.buf = av_malloc(track->audio.frame_size *
  1587. track->audio.sub_packet_h);
  1588. if (!track->audio.buf)
  1589. return AVERROR(ENOMEM);
  1590. if (codec_id == AV_CODEC_ID_RA_288) {
  1591. st->codec->block_align = track->audio.coded_framesize;
  1592. track->codec_priv.size = 0;
  1593. } else {
  1594. if (codec_id == AV_CODEC_ID_SIPR && flavor < 4) {
  1595. const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
  1596. track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
  1597. st->codec->bit_rate = sipr_bit_rate[flavor];
  1598. }
  1599. st->codec->block_align = track->audio.sub_packet_size;
  1600. extradata_offset = 78;
  1601. }
  1602. } else if (codec_id == AV_CODEC_ID_FLAC && track->codec_priv.size) {
  1603. ret = matroska_parse_flac(s, track, &extradata_offset);
  1604. if (ret < 0)
  1605. return ret;
  1606. }
  1607. track->codec_priv.size -= extradata_offset;
  1608. if (codec_id == AV_CODEC_ID_NONE)
  1609. av_log(matroska->ctx, AV_LOG_INFO,
  1610. "Unknown/unsupported AVCodecID %s.\n", track->codec_id);
  1611. if (track->time_scale < 0.01)
  1612. track->time_scale = 1.0;
  1613. avpriv_set_pts_info(st, 64, matroska->time_scale * track->time_scale,
  1614. 1000 * 1000 * 1000); /* 64 bit pts in ns */
  1615. /* convert the delay from ns to the track timebase */
  1616. track->codec_delay = av_rescale_q(track->codec_delay,
  1617. (AVRational){ 1, 1000000000 },
  1618. st->time_base);
  1619. st->codec->codec_id = codec_id;
  1620. st->start_time = 0;
  1621. if (strcmp(track->language, "und"))
  1622. av_dict_set(&st->metadata, "language", track->language, 0);
  1623. av_dict_set(&st->metadata, "title", track->name, 0);
  1624. if (track->flag_default)
  1625. st->disposition |= AV_DISPOSITION_DEFAULT;
  1626. if (track->flag_forced)
  1627. st->disposition |= AV_DISPOSITION_FORCED;
  1628. if (!st->codec->extradata) {
  1629. if (extradata) {
  1630. st->codec->extradata = extradata;
  1631. st->codec->extradata_size = extradata_size;
  1632. } else if (track->codec_priv.data && track->codec_priv.size > 0) {
  1633. st->codec->extradata = av_mallocz(track->codec_priv.size +
  1634. AV_INPUT_BUFFER_PADDING_SIZE);
  1635. if (!st->codec->extradata)
  1636. return AVERROR(ENOMEM);
  1637. st->codec->extradata_size = track->codec_priv.size;
  1638. memcpy(st->codec->extradata,
  1639. track->codec_priv.data + extradata_offset,
  1640. track->codec_priv.size);
  1641. }
  1642. }
  1643. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1644. int display_width_mul = 1;
  1645. int display_height_mul = 1;
  1646. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1647. st->codec->codec_tag = track->video.fourcc;
  1648. st->codec->width = track->video.pixel_width;
  1649. st->codec->height = track->video.pixel_height;
  1650. if (track->video.stereo_mode && track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
  1651. mkv_stereo_mode_display_mul(track->video.stereo_mode, &display_width_mul, &display_height_mul);
  1652. av_reduce(&st->sample_aspect_ratio.num,
  1653. &st->sample_aspect_ratio.den,
  1654. st->codec->height * track->video.display_width * display_width_mul,
  1655. st->codec->width * track->video.display_height * display_height_mul,
  1656. 255);
  1657. if (st->codec->codec_id != AV_CODEC_ID_H264 &&
  1658. st->codec->codec_id != AV_CODEC_ID_HEVC)
  1659. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1660. if (track->default_duration) {
  1661. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1662. 1000000000, track->default_duration, 30000);
  1663. }
  1664. // add stream level stereo3d side data if it is a supported format
  1665. if (track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
  1666. track->video.stereo_mode != 10 && track->video.stereo_mode != 12) {
  1667. int ret = ff_mkv_stereo3d_conv(st, track->video.stereo_mode);
  1668. if (ret < 0)
  1669. return ret;
  1670. }
  1671. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1672. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1673. st->codec->sample_rate = track->audio.out_samplerate;
  1674. st->codec->channels = track->audio.channels;
  1675. if (st->codec->codec_id != AV_CODEC_ID_AAC)
  1676. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1677. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  1678. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1679. if (st->codec->codec_id == AV_CODEC_ID_SSA)
  1680. matroska->contains_ssa = 1;
  1681. }
  1682. }
  1683. return 0;
  1684. }
  1685. static int matroska_read_header(AVFormatContext *s)
  1686. {
  1687. MatroskaDemuxContext *matroska = s->priv_data;
  1688. EbmlList *attachments_list = &matroska->attachments;
  1689. EbmlList *chapters_list = &matroska->chapters;
  1690. MatroskaAttachment *attachments;
  1691. MatroskaChapter *chapters;
  1692. uint64_t max_start = 0;
  1693. int64_t pos;
  1694. Ebml ebml = { 0 };
  1695. int i, j, res;
  1696. matroska->ctx = s;
  1697. /* First read the EBML header. */
  1698. if (ebml_parse(matroska, ebml_syntax, &ebml) || !ebml.doctype) {
  1699. av_log(matroska->ctx, AV_LOG_ERROR, "EBML header parsing failed\n");
  1700. ebml_free(ebml_syntax, &ebml);
  1701. return AVERROR_INVALIDDATA;
  1702. }
  1703. if (ebml.version > EBML_VERSION ||
  1704. ebml.max_size > sizeof(uint64_t) ||
  1705. ebml.id_length > sizeof(uint32_t) ||
  1706. ebml.doctype_version > 3) {
  1707. av_log(matroska->ctx, AV_LOG_ERROR,
  1708. "EBML header using unsupported features\n"
  1709. "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
  1710. ebml.version, ebml.doctype, ebml.doctype_version);
  1711. ebml_free(ebml_syntax, &ebml);
  1712. return AVERROR_PATCHWELCOME;
  1713. }
  1714. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
  1715. if (!strcmp(ebml.doctype, matroska_doctypes[i]))
  1716. break;
  1717. if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
  1718. av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
  1719. if (matroska->ctx->error_recognition & AV_EF_EXPLODE) {
  1720. ebml_free(ebml_syntax, &ebml);
  1721. return AVERROR_INVALIDDATA;
  1722. }
  1723. }
  1724. ebml_free(ebml_syntax, &ebml);
  1725. /* The next thing is a segment. */
  1726. pos = avio_tell(matroska->ctx->pb);
  1727. res = ebml_parse(matroska, matroska_segments, matroska);
  1728. // try resyncing until we find a EBML_STOP type element.
  1729. while (res != 1) {
  1730. res = matroska_resync(matroska, pos);
  1731. if (res < 0)
  1732. return res;
  1733. pos = avio_tell(matroska->ctx->pb);
  1734. res = ebml_parse(matroska, matroska_segment, matroska);
  1735. }
  1736. matroska_execute_seekhead(matroska);
  1737. if (!matroska->time_scale)
  1738. matroska->time_scale = 1000000;
  1739. if (matroska->duration)
  1740. matroska->ctx->duration = matroska->duration * matroska->time_scale *
  1741. 1000 / AV_TIME_BASE;
  1742. av_dict_set(&s->metadata, "title", matroska->title, 0);
  1743. res = matroska_parse_tracks(s);
  1744. if (res < 0)
  1745. return res;
  1746. attachments = attachments_list->elem;
  1747. for (j = 0; j < attachments_list->nb_elem; j++) {
  1748. if (!(attachments[j].filename && attachments[j].mime &&
  1749. attachments[j].bin.data && attachments[j].bin.size > 0)) {
  1750. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1751. } else {
  1752. AVStream *st = avformat_new_stream(s, NULL);
  1753. if (!st)
  1754. break;
  1755. av_dict_set(&st->metadata, "filename", attachments[j].filename, 0);
  1756. av_dict_set(&st->metadata, "mimetype", attachments[j].mime, 0);
  1757. st->codec->codec_id = AV_CODEC_ID_NONE;
  1758. for (i = 0; ff_mkv_image_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
  1759. if (!strncmp(ff_mkv_image_mime_tags[i].str, attachments[j].mime,
  1760. strlen(ff_mkv_image_mime_tags[i].str))) {
  1761. st->codec->codec_id = ff_mkv_image_mime_tags[i].id;
  1762. break;
  1763. }
  1764. }
  1765. attachments[j].stream = st;
  1766. if (st->codec->codec_id != AV_CODEC_ID_NONE) {
  1767. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  1768. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1769. av_init_packet(&st->attached_pic);
  1770. if ((res = av_new_packet(&st->attached_pic, attachments[j].bin.size)) < 0)
  1771. return res;
  1772. memcpy(st->attached_pic.data, attachments[j].bin.data, attachments[j].bin.size);
  1773. st->attached_pic.stream_index = st->index;
  1774. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  1775. } else {
  1776. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  1777. st->codec->extradata = av_malloc(attachments[j].bin.size);
  1778. if (!st->codec->extradata)
  1779. break;
  1780. st->codec->extradata_size = attachments[j].bin.size;
  1781. memcpy(st->codec->extradata, attachments[j].bin.data,
  1782. attachments[j].bin.size);
  1783. for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
  1784. if (!strncmp(ff_mkv_mime_tags[i].str, attachments[j].mime,
  1785. strlen(ff_mkv_mime_tags[i].str))) {
  1786. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1787. break;
  1788. }
  1789. }
  1790. }
  1791. }
  1792. }
  1793. chapters = chapters_list->elem;
  1794. for (i = 0; i < chapters_list->nb_elem; i++)
  1795. if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid &&
  1796. (max_start == 0 || chapters[i].start > max_start)) {
  1797. chapters[i].chapter =
  1798. avpriv_new_chapter(s, chapters[i].uid,
  1799. (AVRational) { 1, 1000000000 },
  1800. chapters[i].start, chapters[i].end,
  1801. chapters[i].title);
  1802. av_dict_set(&chapters[i].chapter->metadata,
  1803. "title", chapters[i].title, 0);
  1804. max_start = chapters[i].start;
  1805. }
  1806. matroska_convert_tags(s);
  1807. return 0;
  1808. }
  1809. /*
  1810. * Put one packet in an application-supplied AVPacket struct.
  1811. * Returns 0 on success or -1 on failure.
  1812. */
  1813. static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
  1814. AVPacket *pkt)
  1815. {
  1816. if (matroska->num_packets > 0) {
  1817. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  1818. av_free(matroska->packets[0]);
  1819. if (matroska->num_packets > 1) {
  1820. void *newpackets;
  1821. memmove(&matroska->packets[0], &matroska->packets[1],
  1822. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1823. newpackets = av_realloc(matroska->packets,
  1824. (matroska->num_packets - 1) *
  1825. sizeof(AVPacket *));
  1826. if (newpackets)
  1827. matroska->packets = newpackets;
  1828. } else {
  1829. av_freep(&matroska->packets);
  1830. matroska->prev_pkt = NULL;
  1831. }
  1832. matroska->num_packets--;
  1833. return 0;
  1834. }
  1835. return -1;
  1836. }
  1837. /*
  1838. * Free all packets in our internal queue.
  1839. */
  1840. static void matroska_clear_queue(MatroskaDemuxContext *matroska)
  1841. {
  1842. matroska->prev_pkt = NULL;
  1843. if (matroska->packets) {
  1844. int n;
  1845. for (n = 0; n < matroska->num_packets; n++) {
  1846. av_packet_unref(matroska->packets[n]);
  1847. av_free(matroska->packets[n]);
  1848. }
  1849. av_freep(&matroska->packets);
  1850. matroska->num_packets = 0;
  1851. }
  1852. }
  1853. static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
  1854. int *buf_size, int type,
  1855. uint32_t **lace_buf, int *laces)
  1856. {
  1857. int res = 0, n, size = *buf_size;
  1858. uint8_t *data = *buf;
  1859. uint32_t *lace_size;
  1860. if (!type) {
  1861. *laces = 1;
  1862. *lace_buf = av_mallocz(sizeof(int));
  1863. if (!*lace_buf)
  1864. return AVERROR(ENOMEM);
  1865. *lace_buf[0] = size;
  1866. return 0;
  1867. }
  1868. assert(size > 0);
  1869. *laces = *data + 1;
  1870. data += 1;
  1871. size -= 1;
  1872. lace_size = av_mallocz(*laces * sizeof(int));
  1873. if (!lace_size)
  1874. return AVERROR(ENOMEM);
  1875. switch (type) {
  1876. case 0x1: /* Xiph lacing */
  1877. {
  1878. uint8_t temp;
  1879. uint32_t total = 0;
  1880. for (n = 0; res == 0 && n < *laces - 1; n++) {
  1881. while (1) {
  1882. if (size == 0) {
  1883. res = AVERROR_EOF;
  1884. break;
  1885. }
  1886. temp = *data;
  1887. lace_size[n] += temp;
  1888. data += 1;
  1889. size -= 1;
  1890. if (temp != 0xff)
  1891. break;
  1892. }
  1893. total += lace_size[n];
  1894. }
  1895. if (size <= total) {
  1896. res = AVERROR_INVALIDDATA;
  1897. break;
  1898. }
  1899. lace_size[n] = size - total;
  1900. break;
  1901. }
  1902. case 0x2: /* fixed-size lacing */
  1903. if (size % (*laces)) {
  1904. res = AVERROR_INVALIDDATA;
  1905. break;
  1906. }
  1907. for (n = 0; n < *laces; n++)
  1908. lace_size[n] = size / *laces;
  1909. break;
  1910. case 0x3: /* EBML lacing */
  1911. {
  1912. uint64_t num;
  1913. uint64_t total;
  1914. n = matroska_ebmlnum_uint(matroska, data, size, &num);
  1915. if (n < 0) {
  1916. av_log(matroska->ctx, AV_LOG_INFO,
  1917. "EBML block data error\n");
  1918. res = n;
  1919. break;
  1920. }
  1921. data += n;
  1922. size -= n;
  1923. total = lace_size[0] = num;
  1924. for (n = 1; res == 0 && n < *laces - 1; n++) {
  1925. int64_t snum;
  1926. int r;
  1927. r = matroska_ebmlnum_sint(matroska, data, size, &snum);
  1928. if (r < 0) {
  1929. av_log(matroska->ctx, AV_LOG_INFO,
  1930. "EBML block data error\n");
  1931. res = r;
  1932. break;
  1933. }
  1934. data += r;
  1935. size -= r;
  1936. lace_size[n] = lace_size[n - 1] + snum;
  1937. total += lace_size[n];
  1938. }
  1939. if (size <= total) {
  1940. res = AVERROR_INVALIDDATA;
  1941. break;
  1942. }
  1943. lace_size[*laces - 1] = size - total;
  1944. break;
  1945. }
  1946. }
  1947. *buf = data;
  1948. *lace_buf = lace_size;
  1949. *buf_size = size;
  1950. return res;
  1951. }
  1952. static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska,
  1953. MatroskaTrack *track, AVStream *st,
  1954. uint8_t *data, int size, uint64_t timecode,
  1955. uint64_t duration, int64_t pos)
  1956. {
  1957. int a = st->codec->block_align;
  1958. int sps = track->audio.sub_packet_size;
  1959. int cfs = track->audio.coded_framesize;
  1960. int h = track->audio.sub_packet_h;
  1961. int y = track->audio.sub_packet_cnt;
  1962. int w = track->audio.frame_size;
  1963. int x;
  1964. if (!track->audio.pkt_cnt) {
  1965. if (track->audio.sub_packet_cnt == 0)
  1966. track->audio.buf_timecode = timecode;
  1967. if (st->codec->codec_id == AV_CODEC_ID_RA_288) {
  1968. if (size < cfs * h / 2) {
  1969. av_log(matroska->ctx, AV_LOG_ERROR,
  1970. "Corrupt int4 RM-style audio packet size\n");
  1971. return AVERROR_INVALIDDATA;
  1972. }
  1973. for (x = 0; x < h / 2; x++)
  1974. memcpy(track->audio.buf + x * 2 * w + y * cfs,
  1975. data + x * cfs, cfs);
  1976. } else if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
  1977. if (size < w) {
  1978. av_log(matroska->ctx, AV_LOG_ERROR,
  1979. "Corrupt sipr RM-style audio packet size\n");
  1980. return AVERROR_INVALIDDATA;
  1981. }
  1982. memcpy(track->audio.buf + y * w, data, w);
  1983. } else {
  1984. if (size < sps * w / sps) {
  1985. av_log(matroska->ctx, AV_LOG_ERROR,
  1986. "Corrupt generic RM-style audio packet size\n");
  1987. return AVERROR_INVALIDDATA;
  1988. }
  1989. for (x = 0; x < w / sps; x++)
  1990. memcpy(track->audio.buf +
  1991. sps * (h * x + ((h + 1) / 2) * (y & 1) + (y >> 1)),
  1992. data + x * sps, sps);
  1993. }
  1994. if (++track->audio.sub_packet_cnt >= h) {
  1995. if (st->codec->codec_id == AV_CODEC_ID_SIPR)
  1996. ff_rm_reorder_sipr_data(track->audio.buf, h, w);
  1997. track->audio.sub_packet_cnt = 0;
  1998. track->audio.pkt_cnt = h * w / a;
  1999. }
  2000. }
  2001. while (track->audio.pkt_cnt) {
  2002. int ret;
  2003. AVPacket *pkt = av_mallocz(sizeof(AVPacket));
  2004. if (!pkt)
  2005. return AVERROR(ENOMEM);
  2006. ret = av_new_packet(pkt, a);
  2007. if (ret < 0) {
  2008. av_free(pkt);
  2009. return ret;
  2010. }
  2011. memcpy(pkt->data,
  2012. track->audio.buf + a * (h * w / a - track->audio.pkt_cnt--),
  2013. a);
  2014. pkt->pts = track->audio.buf_timecode;
  2015. track->audio.buf_timecode = AV_NOPTS_VALUE;
  2016. pkt->pos = pos;
  2017. pkt->stream_index = st->index;
  2018. dynarray_add(&matroska->packets, &matroska->num_packets, pkt);
  2019. }
  2020. return 0;
  2021. }
  2022. /* reconstruct full wavpack blocks from mangled matroska ones */
  2023. static int matroska_parse_wavpack(MatroskaTrack *track, uint8_t *src,
  2024. uint8_t **pdst, int *size)
  2025. {
  2026. uint8_t *dst = NULL;
  2027. int dstlen = 0;
  2028. int srclen = *size;
  2029. uint32_t samples;
  2030. uint16_t ver;
  2031. int ret, offset = 0;
  2032. if (srclen < 12 || track->stream->codec->extradata_size < 2)
  2033. return AVERROR_INVALIDDATA;
  2034. ver = AV_RL16(track->stream->codec->extradata);
  2035. samples = AV_RL32(src);
  2036. src += 4;
  2037. srclen -= 4;
  2038. while (srclen >= 8) {
  2039. int multiblock;
  2040. uint32_t blocksize;
  2041. uint8_t *tmp;
  2042. uint32_t flags = AV_RL32(src);
  2043. uint32_t crc = AV_RL32(src + 4);
  2044. src += 8;
  2045. srclen -= 8;
  2046. multiblock = (flags & 0x1800) != 0x1800;
  2047. if (multiblock) {
  2048. if (srclen < 4) {
  2049. ret = AVERROR_INVALIDDATA;
  2050. goto fail;
  2051. }
  2052. blocksize = AV_RL32(src);
  2053. src += 4;
  2054. srclen -= 4;
  2055. } else
  2056. blocksize = srclen;
  2057. if (blocksize > srclen) {
  2058. ret = AVERROR_INVALIDDATA;
  2059. goto fail;
  2060. }
  2061. tmp = av_realloc(dst, dstlen + blocksize + 32);
  2062. if (!tmp) {
  2063. ret = AVERROR(ENOMEM);
  2064. goto fail;
  2065. }
  2066. dst = tmp;
  2067. dstlen += blocksize + 32;
  2068. AV_WL32(dst + offset, MKTAG('w', 'v', 'p', 'k')); // tag
  2069. AV_WL32(dst + offset + 4, blocksize + 24); // blocksize - 8
  2070. AV_WL16(dst + offset + 8, ver); // version
  2071. AV_WL16(dst + offset + 10, 0); // track/index_no
  2072. AV_WL32(dst + offset + 12, 0); // total samples
  2073. AV_WL32(dst + offset + 16, 0); // block index
  2074. AV_WL32(dst + offset + 20, samples); // number of samples
  2075. AV_WL32(dst + offset + 24, flags); // flags
  2076. AV_WL32(dst + offset + 28, crc); // crc
  2077. memcpy(dst + offset + 32, src, blocksize); // block data
  2078. src += blocksize;
  2079. srclen -= blocksize;
  2080. offset += blocksize + 32;
  2081. }
  2082. *pdst = dst;
  2083. *size = dstlen;
  2084. return 0;
  2085. fail:
  2086. av_freep(&dst);
  2087. return ret;
  2088. }
  2089. static int matroska_parse_frame(MatroskaDemuxContext *matroska,
  2090. MatroskaTrack *track, AVStream *st,
  2091. uint8_t *data, int pkt_size,
  2092. uint64_t timecode, uint64_t duration,
  2093. int64_t pos, int is_keyframe)
  2094. {
  2095. MatroskaTrackEncoding *encodings = track->encodings.elem;
  2096. uint8_t *pkt_data = data;
  2097. int offset = 0, res;
  2098. AVPacket *pkt;
  2099. if (encodings && encodings->scope & 1) {
  2100. res = matroska_decode_buffer(&pkt_data, &pkt_size, track);
  2101. if (res < 0)
  2102. return res;
  2103. }
  2104. if (st->codec->codec_id == AV_CODEC_ID_WAVPACK) {
  2105. uint8_t *wv_data;
  2106. res = matroska_parse_wavpack(track, pkt_data, &wv_data, &pkt_size);
  2107. if (res < 0) {
  2108. av_log(matroska->ctx, AV_LOG_ERROR,
  2109. "Error parsing a wavpack block.\n");
  2110. goto fail;
  2111. }
  2112. if (pkt_data != data)
  2113. av_freep(&pkt_data);
  2114. pkt_data = wv_data;
  2115. }
  2116. if (st->codec->codec_id == AV_CODEC_ID_PRORES)
  2117. offset = 8;
  2118. pkt = av_mallocz(sizeof(AVPacket));
  2119. if (!pkt) {
  2120. av_freep(&pkt_data);
  2121. return AVERROR(ENOMEM);
  2122. }
  2123. /* XXX: prevent data copy... */
  2124. if (av_new_packet(pkt, pkt_size + offset) < 0) {
  2125. av_free(pkt);
  2126. av_freep(&pkt_data);
  2127. return AVERROR(ENOMEM);
  2128. }
  2129. if (st->codec->codec_id == AV_CODEC_ID_PRORES) {
  2130. uint8_t *buf = pkt->data;
  2131. bytestream_put_be32(&buf, pkt_size);
  2132. bytestream_put_be32(&buf, MKBETAG('i', 'c', 'p', 'f'));
  2133. }
  2134. memcpy(pkt->data + offset, pkt_data, pkt_size);
  2135. if (pkt_data != data)
  2136. av_free(pkt_data);
  2137. pkt->flags = is_keyframe;
  2138. pkt->stream_index = st->index;
  2139. if (track->ms_compat)
  2140. pkt->dts = timecode;
  2141. else
  2142. pkt->pts = timecode;
  2143. pkt->pos = pos;
  2144. if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE || st->codec->codec_id == AV_CODEC_ID_TEXT)
  2145. pkt->duration = duration;
  2146. #if FF_API_CONVERGENCE_DURATION
  2147. FF_DISABLE_DEPRECATION_WARNINGS
  2148. if (st->codec->codec_id == AV_CODEC_ID_TEXT)
  2149. pkt->convergence_duration = duration;
  2150. FF_ENABLE_DEPRECATION_WARNINGS
  2151. #endif
  2152. if (st->codec->codec_id == AV_CODEC_ID_SSA)
  2153. matroska_fix_ass_packet(matroska, pkt, duration);
  2154. if (matroska->prev_pkt &&
  2155. timecode != AV_NOPTS_VALUE &&
  2156. matroska->prev_pkt->pts == timecode &&
  2157. matroska->prev_pkt->stream_index == st->index &&
  2158. st->codec->codec_id == AV_CODEC_ID_SSA)
  2159. matroska_merge_packets(matroska->prev_pkt, pkt);
  2160. else {
  2161. dynarray_add(&matroska->packets, &matroska->num_packets, pkt);
  2162. matroska->prev_pkt = pkt;
  2163. }
  2164. return 0;
  2165. fail:
  2166. if (pkt_data != data)
  2167. av_freep(&pkt_data);
  2168. return res;
  2169. }
  2170. static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
  2171. int size, int64_t pos, uint64_t cluster_time,
  2172. uint64_t block_duration, int is_keyframe,
  2173. int64_t cluster_pos)
  2174. {
  2175. uint64_t timecode = AV_NOPTS_VALUE;
  2176. MatroskaTrack *track;
  2177. int res = 0;
  2178. AVStream *st;
  2179. int16_t block_time;
  2180. uint32_t *lace_size = NULL;
  2181. int n, flags, laces = 0;
  2182. uint64_t num, duration;
  2183. if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
  2184. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  2185. return n;
  2186. }
  2187. data += n;
  2188. size -= n;
  2189. track = matroska_find_track_by_num(matroska, num);
  2190. if (!track || !track->stream) {
  2191. av_log(matroska->ctx, AV_LOG_INFO,
  2192. "Invalid stream %"PRIu64" or size %u\n", num, size);
  2193. return AVERROR_INVALIDDATA;
  2194. } else if (size <= 3)
  2195. return 0;
  2196. st = track->stream;
  2197. if (st->discard >= AVDISCARD_ALL)
  2198. return res;
  2199. block_time = AV_RB16(data);
  2200. data += 2;
  2201. flags = *data++;
  2202. size -= 3;
  2203. if (is_keyframe == -1)
  2204. is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
  2205. if (cluster_time != (uint64_t) -1 &&
  2206. (block_time >= 0 || cluster_time >= -block_time)) {
  2207. timecode = cluster_time + block_time - track->codec_delay;
  2208. if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE &&
  2209. timecode < track->end_timecode)
  2210. is_keyframe = 0; /* overlapping subtitles are not key frame */
  2211. if (is_keyframe)
  2212. av_add_index_entry(st, cluster_pos, timecode, 0, 0,
  2213. AVINDEX_KEYFRAME);
  2214. }
  2215. if (matroska->skip_to_keyframe &&
  2216. track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  2217. if (!is_keyframe || timecode < matroska->skip_to_timecode)
  2218. return res;
  2219. matroska->skip_to_keyframe = 0;
  2220. }
  2221. res = matroska_parse_laces(matroska, &data, &size, (flags & 0x06) >> 1,
  2222. &lace_size, &laces);
  2223. if (res)
  2224. goto end;
  2225. if (block_duration != AV_NOPTS_VALUE) {
  2226. duration = block_duration / laces;
  2227. if (block_duration != duration * laces) {
  2228. av_log(matroska->ctx, AV_LOG_WARNING,
  2229. "Incorrect block_duration, possibly corrupted container");
  2230. }
  2231. } else {
  2232. duration = track->default_duration / matroska->time_scale;
  2233. block_duration = duration * laces;
  2234. }
  2235. if (timecode != AV_NOPTS_VALUE)
  2236. track->end_timecode =
  2237. FFMAX(track->end_timecode, timecode + block_duration);
  2238. for (n = 0; n < laces; n++) {
  2239. if ((st->codec->codec_id == AV_CODEC_ID_RA_288 ||
  2240. st->codec->codec_id == AV_CODEC_ID_COOK ||
  2241. st->codec->codec_id == AV_CODEC_ID_SIPR ||
  2242. st->codec->codec_id == AV_CODEC_ID_ATRAC3) &&
  2243. st->codec->block_align && track->audio.sub_packet_size) {
  2244. res = matroska_parse_rm_audio(matroska, track, st, data,
  2245. lace_size[n],
  2246. timecode, duration, pos);
  2247. if (res)
  2248. goto end;
  2249. } else {
  2250. res = matroska_parse_frame(matroska, track, st, data, lace_size[n],
  2251. timecode, duration, pos,
  2252. !n ? is_keyframe : 0);
  2253. if (res)
  2254. goto end;
  2255. }
  2256. if (timecode != AV_NOPTS_VALUE)
  2257. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  2258. data += lace_size[n];
  2259. }
  2260. end:
  2261. av_free(lace_size);
  2262. return res;
  2263. }
  2264. static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska)
  2265. {
  2266. EbmlList *blocks_list;
  2267. MatroskaBlock *blocks;
  2268. int i, res;
  2269. res = ebml_parse(matroska,
  2270. matroska_cluster_incremental_parsing,
  2271. &matroska->current_cluster);
  2272. if (res == 1) {
  2273. /* New Cluster */
  2274. if (matroska->current_cluster_pos)
  2275. ebml_level_end(matroska);
  2276. ebml_free(matroska_cluster, &matroska->current_cluster);
  2277. memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster));
  2278. matroska->current_cluster_num_blocks = 0;
  2279. matroska->current_cluster_pos = avio_tell(matroska->ctx->pb);
  2280. matroska->prev_pkt = NULL;
  2281. /* sizeof the ID which was already read */
  2282. if (matroska->current_id)
  2283. matroska->current_cluster_pos -= 4;
  2284. res = ebml_parse(matroska,
  2285. matroska_clusters_incremental,
  2286. &matroska->current_cluster);
  2287. /* Try parsing the block again. */
  2288. if (res == 1)
  2289. res = ebml_parse(matroska,
  2290. matroska_cluster_incremental_parsing,
  2291. &matroska->current_cluster);
  2292. }
  2293. if (!res &&
  2294. matroska->current_cluster_num_blocks <
  2295. matroska->current_cluster.blocks.nb_elem) {
  2296. blocks_list = &matroska->current_cluster.blocks;
  2297. blocks = blocks_list->elem;
  2298. matroska->current_cluster_num_blocks = blocks_list->nb_elem;
  2299. i = blocks_list->nb_elem - 1;
  2300. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  2301. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  2302. if (!blocks[i].non_simple)
  2303. blocks[i].duration = AV_NOPTS_VALUE;
  2304. res = matroska_parse_block(matroska, blocks[i].bin.data,
  2305. blocks[i].bin.size, blocks[i].bin.pos,
  2306. matroska->current_cluster.timecode,
  2307. blocks[i].duration, is_keyframe,
  2308. matroska->current_cluster_pos);
  2309. }
  2310. }
  2311. if (res < 0)
  2312. matroska->done = 1;
  2313. return res;
  2314. }
  2315. static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
  2316. {
  2317. MatroskaCluster cluster = { 0 };
  2318. EbmlList *blocks_list;
  2319. MatroskaBlock *blocks;
  2320. int i, res;
  2321. int64_t pos;
  2322. if (!matroska->contains_ssa)
  2323. return matroska_parse_cluster_incremental(matroska);
  2324. pos = avio_tell(matroska->ctx->pb);
  2325. matroska->prev_pkt = NULL;
  2326. if (matroska->current_id)
  2327. pos -= 4; /* sizeof the ID which was already read */
  2328. res = ebml_parse(matroska, matroska_clusters, &cluster);
  2329. blocks_list = &cluster.blocks;
  2330. blocks = blocks_list->elem;
  2331. for (i = 0; i < blocks_list->nb_elem && !res; i++)
  2332. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  2333. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  2334. if (!blocks[i].non_simple)
  2335. blocks[i].duration = AV_NOPTS_VALUE;
  2336. res = matroska_parse_block(matroska, blocks[i].bin.data,
  2337. blocks[i].bin.size, blocks[i].bin.pos,
  2338. cluster.timecode, blocks[i].duration,
  2339. is_keyframe, pos);
  2340. }
  2341. ebml_free(matroska_cluster, &cluster);
  2342. return res;
  2343. }
  2344. static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
  2345. {
  2346. MatroskaDemuxContext *matroska = s->priv_data;
  2347. int ret = 0;
  2348. while (!ret && matroska_deliver_packet(matroska, pkt)) {
  2349. int64_t pos = avio_tell(matroska->ctx->pb);
  2350. if (matroska->done)
  2351. return AVERROR_EOF;
  2352. if (matroska_parse_cluster(matroska) < 0)
  2353. ret = matroska_resync(matroska, pos);
  2354. }
  2355. if (ret == AVERROR_INVALIDDATA && pkt->data) {
  2356. pkt->flags |= AV_PKT_FLAG_CORRUPT;
  2357. return 0;
  2358. }
  2359. return ret;
  2360. }
  2361. static int matroska_read_seek(AVFormatContext *s, int stream_index,
  2362. int64_t timestamp, int flags)
  2363. {
  2364. MatroskaDemuxContext *matroska = s->priv_data;
  2365. MatroskaTrack *tracks = NULL;
  2366. AVStream *st = s->streams[stream_index];
  2367. int i, index, index_sub, index_min;
  2368. /* Parse the CUES now since we need the index data to seek. */
  2369. if (matroska->cues_parsing_deferred) {
  2370. matroska_parse_cues(matroska);
  2371. matroska->cues_parsing_deferred = 0;
  2372. }
  2373. if (!st->nb_index_entries)
  2374. return 0;
  2375. timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
  2376. if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  2377. avio_seek(s->pb, st->index_entries[st->nb_index_entries - 1].pos,
  2378. SEEK_SET);
  2379. matroska->current_id = 0;
  2380. while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  2381. matroska_clear_queue(matroska);
  2382. if (matroska_parse_cluster(matroska) < 0)
  2383. break;
  2384. }
  2385. }
  2386. matroska_clear_queue(matroska);
  2387. if (index < 0)
  2388. return 0;
  2389. index_min = index;
  2390. tracks = matroska->tracks.elem;
  2391. for (i = 0; i < matroska->tracks.nb_elem; i++) {
  2392. tracks[i].audio.pkt_cnt = 0;
  2393. tracks[i].audio.sub_packet_cnt = 0;
  2394. tracks[i].audio.buf_timecode = AV_NOPTS_VALUE;
  2395. tracks[i].end_timecode = 0;
  2396. if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE &&
  2397. tracks[i].stream->discard != AVDISCARD_ALL) {
  2398. index_sub = av_index_search_timestamp(
  2399. tracks[i].stream, st->index_entries[index].timestamp,
  2400. AVSEEK_FLAG_BACKWARD);
  2401. if (index_sub >= 0 &&
  2402. st->index_entries[index_sub].pos < st->index_entries[index_min].pos &&
  2403. st->index_entries[index].timestamp -
  2404. st->index_entries[index_sub].timestamp < 30000000000 / matroska->time_scale)
  2405. index_min = index_sub;
  2406. }
  2407. }
  2408. avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
  2409. matroska->current_id = 0;
  2410. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  2411. matroska->skip_to_timecode = st->index_entries[index].timestamp;
  2412. matroska->done = 0;
  2413. ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
  2414. return 0;
  2415. }
  2416. static int matroska_read_close(AVFormatContext *s)
  2417. {
  2418. MatroskaDemuxContext *matroska = s->priv_data;
  2419. MatroskaTrack *tracks = matroska->tracks.elem;
  2420. int n;
  2421. matroska_clear_queue(matroska);
  2422. for (n = 0; n < matroska->tracks.nb_elem; n++)
  2423. if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
  2424. av_free(tracks[n].audio.buf);
  2425. ebml_free(matroska_cluster, &matroska->current_cluster);
  2426. ebml_free(matroska_segment, matroska);
  2427. return 0;
  2428. }
  2429. AVInputFormat ff_matroska_demuxer = {
  2430. .name = "matroska,webm",
  2431. .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
  2432. .extensions = "mkv,mk3d,mka,mks",
  2433. .priv_data_size = sizeof(MatroskaDemuxContext),
  2434. .read_probe = matroska_probe,
  2435. .read_header = matroska_read_header,
  2436. .read_packet = matroska_read_packet,
  2437. .read_close = matroska_read_close,
  2438. .read_seek = matroska_read_seek,
  2439. .mime_type = "audio/webm,audio/x-matroska,video/webm,video/x-matroska"
  2440. };