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.

2772 lines
96KB

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