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.

2032 lines
73KB

  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 <stdio.h>
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #include "avio_internal.h"
  33. /* For ff_codec_get_id(). */
  34. #include "riff.h"
  35. #include "isom.h"
  36. #include "rm.h"
  37. #include "matroska.h"
  38. #include "libavcodec/mpeg4audio.h"
  39. #include "libavutil/intfloat.h"
  40. #include "libavutil/intreadwrite.h"
  41. #include "libavutil/avstring.h"
  42. #include "libavutil/lzo.h"
  43. #include "libavutil/dict.h"
  44. #if CONFIG_ZLIB
  45. #include <zlib.h>
  46. #endif
  47. #if CONFIG_BZLIB
  48. #include <bzlib.h>
  49. #endif
  50. typedef enum {
  51. EBML_NONE,
  52. EBML_UINT,
  53. EBML_FLOAT,
  54. EBML_STR,
  55. EBML_UTF8,
  56. EBML_BIN,
  57. EBML_NEST,
  58. EBML_PASS,
  59. EBML_STOP,
  60. EBML_TYPE_COUNT
  61. } EbmlType;
  62. typedef const struct EbmlSyntax {
  63. uint32_t id;
  64. EbmlType type;
  65. int list_elem_size;
  66. int data_offset;
  67. union {
  68. uint64_t u;
  69. double f;
  70. const char *s;
  71. const struct EbmlSyntax *n;
  72. } def;
  73. } EbmlSyntax;
  74. typedef struct {
  75. int nb_elem;
  76. void *elem;
  77. } EbmlList;
  78. typedef struct {
  79. int size;
  80. uint8_t *data;
  81. int64_t pos;
  82. } EbmlBin;
  83. typedef struct {
  84. uint64_t version;
  85. uint64_t max_size;
  86. uint64_t id_length;
  87. char *doctype;
  88. uint64_t doctype_version;
  89. } Ebml;
  90. typedef struct {
  91. uint64_t algo;
  92. EbmlBin settings;
  93. } MatroskaTrackCompression;
  94. typedef struct {
  95. uint64_t scope;
  96. uint64_t type;
  97. MatroskaTrackCompression compression;
  98. } MatroskaTrackEncoding;
  99. typedef struct {
  100. double frame_rate;
  101. uint64_t display_width;
  102. uint64_t display_height;
  103. uint64_t pixel_width;
  104. uint64_t pixel_height;
  105. uint64_t fourcc;
  106. } MatroskaTrackVideo;
  107. typedef struct {
  108. double samplerate;
  109. double out_samplerate;
  110. uint64_t bitdepth;
  111. uint64_t channels;
  112. /* real audio header (extracted from extradata) */
  113. int coded_framesize;
  114. int sub_packet_h;
  115. int frame_size;
  116. int sub_packet_size;
  117. int sub_packet_cnt;
  118. int pkt_cnt;
  119. uint64_t buf_timecode;
  120. uint8_t *buf;
  121. } MatroskaTrackAudio;
  122. typedef struct {
  123. uint64_t num;
  124. uint64_t uid;
  125. uint64_t type;
  126. char *name;
  127. char *codec_id;
  128. EbmlBin codec_priv;
  129. char *language;
  130. double time_scale;
  131. uint64_t default_duration;
  132. uint64_t flag_default;
  133. uint64_t flag_forced;
  134. MatroskaTrackVideo video;
  135. MatroskaTrackAudio audio;
  136. EbmlList encodings;
  137. AVStream *stream;
  138. int64_t end_timecode;
  139. int ms_compat;
  140. } MatroskaTrack;
  141. typedef struct {
  142. uint64_t uid;
  143. char *filename;
  144. char *mime;
  145. EbmlBin bin;
  146. AVStream *stream;
  147. } MatroskaAttachement;
  148. typedef struct {
  149. uint64_t start;
  150. uint64_t end;
  151. uint64_t uid;
  152. char *title;
  153. AVChapter *chapter;
  154. } MatroskaChapter;
  155. typedef struct {
  156. uint64_t track;
  157. uint64_t pos;
  158. } MatroskaIndexPos;
  159. typedef struct {
  160. uint64_t time;
  161. EbmlList pos;
  162. } MatroskaIndex;
  163. typedef struct {
  164. char *name;
  165. char *string;
  166. char *lang;
  167. uint64_t def;
  168. EbmlList sub;
  169. } MatroskaTag;
  170. typedef struct {
  171. char *type;
  172. uint64_t typevalue;
  173. uint64_t trackuid;
  174. uint64_t chapteruid;
  175. uint64_t attachuid;
  176. } MatroskaTagTarget;
  177. typedef struct {
  178. MatroskaTagTarget target;
  179. EbmlList tag;
  180. } MatroskaTags;
  181. typedef struct {
  182. uint64_t id;
  183. uint64_t pos;
  184. } MatroskaSeekhead;
  185. typedef struct {
  186. uint64_t start;
  187. uint64_t length;
  188. } MatroskaLevel;
  189. typedef struct {
  190. AVFormatContext *ctx;
  191. /* EBML stuff */
  192. int num_levels;
  193. MatroskaLevel levels[EBML_MAX_DEPTH];
  194. int level_up;
  195. uint32_t current_id;
  196. uint64_t time_scale;
  197. double duration;
  198. char *title;
  199. EbmlList tracks;
  200. EbmlList attachments;
  201. EbmlList chapters;
  202. EbmlList index;
  203. EbmlList tags;
  204. EbmlList seekhead;
  205. /* byte position of the segment inside the stream */
  206. int64_t segment_start;
  207. /* the packet queue */
  208. AVPacket **packets;
  209. int num_packets;
  210. AVPacket *prev_pkt;
  211. int done;
  212. /* What to skip before effectively reading a packet. */
  213. int skip_to_keyframe;
  214. uint64_t skip_to_timecode;
  215. /* File has a CUES element, but we defer parsing until it is needed. */
  216. int cues_parsing_deferred;
  217. } MatroskaDemuxContext;
  218. typedef struct {
  219. uint64_t duration;
  220. int64_t reference;
  221. uint64_t non_simple;
  222. EbmlBin bin;
  223. } MatroskaBlock;
  224. typedef struct {
  225. uint64_t timecode;
  226. EbmlList blocks;
  227. } MatroskaCluster;
  228. static EbmlSyntax ebml_header[] = {
  229. { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
  230. { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
  231. { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
  232. { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} },
  233. { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
  234. { EBML_ID_EBMLVERSION, EBML_NONE },
  235. { EBML_ID_DOCTYPEVERSION, EBML_NONE },
  236. { 0 }
  237. };
  238. static EbmlSyntax ebml_syntax[] = {
  239. { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} },
  240. { 0 }
  241. };
  242. static EbmlSyntax matroska_info[] = {
  243. { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} },
  244. { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) },
  245. { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) },
  246. { MATROSKA_ID_WRITINGAPP, EBML_NONE },
  247. { MATROSKA_ID_MUXINGAPP, EBML_NONE },
  248. { MATROSKA_ID_DATEUTC, EBML_NONE },
  249. { MATROSKA_ID_SEGMENTUID, EBML_NONE },
  250. { 0 }
  251. };
  252. static EbmlSyntax matroska_track_video[] = {
  253. { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
  254. { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
  255. { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
  256. { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
  257. { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
  258. { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) },
  259. { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE },
  260. { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
  261. { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
  262. { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
  263. { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE },
  264. { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE },
  265. { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE },
  266. { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
  267. { 0 }
  268. };
  269. static EbmlSyntax matroska_track_audio[] = {
  270. { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} },
  271. { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) },
  272. { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) },
  273. { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} },
  274. { 0 }
  275. };
  276. static EbmlSyntax matroska_track_encoding_compression[] = {
  277. { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} },
  278. { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) },
  279. { 0 }
  280. };
  281. static EbmlSyntax matroska_track_encoding[] = {
  282. { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
  283. { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
  284. { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
  285. { MATROSKA_ID_ENCODINGORDER, EBML_NONE },
  286. { 0 }
  287. };
  288. static EbmlSyntax matroska_track_encodings[] = {
  289. { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} },
  290. { 0 }
  291. };
  292. static EbmlSyntax matroska_track[] = {
  293. { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) },
  294. { MATROSKA_ID_TRACKNAME, EBML_UTF8, 0, offsetof(MatroskaTrack,name) },
  295. { MATROSKA_ID_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTrack,uid) },
  296. { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) },
  297. { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) },
  298. { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) },
  299. { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} },
  300. { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) },
  301. { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} },
  302. { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} },
  303. { MATROSKA_ID_TRACKFLAGFORCED, EBML_UINT, 0, offsetof(MatroskaTrack,flag_forced), {.u=0} },
  304. { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} },
  305. { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} },
  306. { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} },
  307. { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE },
  308. { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
  309. { MATROSKA_ID_CODECNAME, EBML_NONE },
  310. { MATROSKA_ID_CODECDECODEALL, EBML_NONE },
  311. { MATROSKA_ID_CODECINFOURL, EBML_NONE },
  312. { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
  313. { MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
  314. { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
  315. { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE },
  316. { 0 }
  317. };
  318. static EbmlSyntax matroska_tracks[] = {
  319. { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} },
  320. { 0 }
  321. };
  322. static EbmlSyntax matroska_attachment[] = {
  323. { MATROSKA_ID_FILEUID, EBML_UINT, 0, offsetof(MatroskaAttachement,uid) },
  324. { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) },
  325. { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) },
  326. { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) },
  327. { MATROSKA_ID_FILEDESC, EBML_NONE },
  328. { 0 }
  329. };
  330. static EbmlSyntax matroska_attachments[] = {
  331. { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} },
  332. { 0 }
  333. };
  334. static EbmlSyntax matroska_chapter_display[] = {
  335. { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) },
  336. { MATROSKA_ID_CHAPLANG, EBML_NONE },
  337. { 0 }
  338. };
  339. static EbmlSyntax matroska_chapter_entry[] = {
  340. { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} },
  341. { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} },
  342. { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) },
  343. { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} },
  344. { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
  345. { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
  346. { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
  347. { MATROSKA_ID_CHAPTERATOM, EBML_NONE },
  348. { 0 }
  349. };
  350. static EbmlSyntax matroska_chapter[] = {
  351. { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} },
  352. { MATROSKA_ID_EDITIONUID, EBML_NONE },
  353. { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
  354. { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
  355. { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
  356. { 0 }
  357. };
  358. static EbmlSyntax matroska_chapters[] = {
  359. { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} },
  360. { 0 }
  361. };
  362. static EbmlSyntax matroska_index_pos[] = {
  363. { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) },
  364. { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) },
  365. { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE },
  366. { 0 }
  367. };
  368. static EbmlSyntax matroska_index_entry[] = {
  369. { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) },
  370. { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} },
  371. { 0 }
  372. };
  373. static EbmlSyntax matroska_index[] = {
  374. { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} },
  375. { 0 }
  376. };
  377. static EbmlSyntax matroska_simpletag[] = {
  378. { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) },
  379. { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) },
  380. { MATROSKA_ID_TAGLANG, EBML_STR, 0, offsetof(MatroskaTag,lang), {.s="und"} },
  381. { MATROSKA_ID_TAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTag,def) },
  382. { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0, offsetof(MatroskaTag,def) },
  383. { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} },
  384. { 0 }
  385. };
  386. static EbmlSyntax matroska_tagtargets[] = {
  387. { MATROSKA_ID_TAGTARGETS_TYPE, EBML_STR, 0, offsetof(MatroskaTagTarget,type) },
  388. { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, offsetof(MatroskaTagTarget,typevalue), {.u=50} },
  389. { MATROSKA_ID_TAGTARGETS_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,trackuid) },
  390. { MATROSKA_ID_TAGTARGETS_CHAPTERUID,EBML_UINT, 0, offsetof(MatroskaTagTarget,chapteruid) },
  391. { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,attachuid) },
  392. { 0 }
  393. };
  394. static EbmlSyntax matroska_tag[] = {
  395. { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTags,tag), {.n=matroska_simpletag} },
  396. { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, offsetof(MatroskaTags,target), {.n=matroska_tagtargets} },
  397. { 0 }
  398. };
  399. static EbmlSyntax matroska_tags[] = {
  400. { MATROSKA_ID_TAG, EBML_NEST, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} },
  401. { 0 }
  402. };
  403. static EbmlSyntax matroska_seekhead_entry[] = {
  404. { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) },
  405. { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} },
  406. { 0 }
  407. };
  408. static EbmlSyntax matroska_seekhead[] = {
  409. { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} },
  410. { 0 }
  411. };
  412. static EbmlSyntax matroska_segment[] = {
  413. { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } },
  414. { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } },
  415. { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} },
  416. { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } },
  417. { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } },
  418. { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } },
  419. { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } },
  420. { MATROSKA_ID_CLUSTER, EBML_STOP },
  421. { 0 }
  422. };
  423. static EbmlSyntax matroska_segments[] = {
  424. { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } },
  425. { 0 }
  426. };
  427. static EbmlSyntax matroska_blockgroup[] = {
  428. { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
  429. { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) },
  430. { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} },
  431. { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) },
  432. { 1, EBML_UINT, 0, offsetof(MatroskaBlock,non_simple), {.u=1} },
  433. { 0 }
  434. };
  435. static EbmlSyntax matroska_cluster[] = {
  436. { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) },
  437. { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  438. { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} },
  439. { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE },
  440. { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE },
  441. { 0 }
  442. };
  443. static EbmlSyntax matroska_clusters[] = {
  444. { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} },
  445. { MATROSKA_ID_INFO, EBML_NONE },
  446. { MATROSKA_ID_CUES, EBML_NONE },
  447. { MATROSKA_ID_TAGS, EBML_NONE },
  448. { MATROSKA_ID_SEEKHEAD, EBML_NONE },
  449. { 0 }
  450. };
  451. static const char *const matroska_doctypes[] = { "matroska", "webm" };
  452. /*
  453. * Return: Whether we reached the end of a level in the hierarchy or not.
  454. */
  455. static int ebml_level_end(MatroskaDemuxContext *matroska)
  456. {
  457. AVIOContext *pb = matroska->ctx->pb;
  458. int64_t pos = avio_tell(pb);
  459. if (matroska->num_levels > 0) {
  460. MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
  461. if (pos - level->start >= level->length || matroska->current_id) {
  462. matroska->num_levels--;
  463. return 1;
  464. }
  465. }
  466. return 0;
  467. }
  468. /*
  469. * Read: an "EBML number", which is defined as a variable-length
  470. * array of bytes. The first byte indicates the length by giving a
  471. * number of 0-bits followed by a one. The position of the first
  472. * "one" bit inside the first byte indicates the length of this
  473. * number.
  474. * Returns: number of bytes read, < 0 on error
  475. */
  476. static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb,
  477. int max_size, uint64_t *number)
  478. {
  479. int read = 1, n = 1;
  480. uint64_t total = 0;
  481. /* The first byte tells us the length in bytes - avio_r8() can normally
  482. * return 0, but since that's not a valid first ebmlID byte, we can
  483. * use it safely here to catch EOS. */
  484. if (!(total = avio_r8(pb))) {
  485. /* we might encounter EOS here */
  486. if (!pb->eof_reached) {
  487. int64_t pos = avio_tell(pb);
  488. av_log(matroska->ctx, AV_LOG_ERROR,
  489. "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
  490. pos, pos);
  491. }
  492. return AVERROR(EIO); /* EOS or actual I/O error */
  493. }
  494. /* get the length of the EBML number */
  495. read = 8 - ff_log2_tab[total];
  496. if (read > max_size) {
  497. int64_t pos = avio_tell(pb) - 1;
  498. av_log(matroska->ctx, AV_LOG_ERROR,
  499. "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
  500. (uint8_t) total, pos, pos);
  501. return AVERROR_INVALIDDATA;
  502. }
  503. /* read out length */
  504. total ^= 1 << ff_log2_tab[total];
  505. while (n++ < read)
  506. total = (total << 8) | avio_r8(pb);
  507. *number = total;
  508. return read;
  509. }
  510. /**
  511. * Read a EBML length value.
  512. * This needs special handling for the "unknown length" case which has multiple
  513. * encodings.
  514. */
  515. static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
  516. uint64_t *number)
  517. {
  518. int res = ebml_read_num(matroska, pb, 8, number);
  519. if (res > 0 && *number + 1 == 1ULL << (7 * res))
  520. *number = 0xffffffffffffffULL;
  521. return res;
  522. }
  523. /*
  524. * Read the next element as an unsigned int.
  525. * 0 is success, < 0 is failure.
  526. */
  527. static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
  528. {
  529. int n = 0;
  530. if (size > 8)
  531. return AVERROR_INVALIDDATA;
  532. /* big-endian ordering; build up number */
  533. *num = 0;
  534. while (n++ < size)
  535. *num = (*num << 8) | avio_r8(pb);
  536. return 0;
  537. }
  538. /*
  539. * Read the next element as a float.
  540. * 0 is success, < 0 is failure.
  541. */
  542. static int ebml_read_float(AVIOContext *pb, int size, double *num)
  543. {
  544. if (size == 0) {
  545. *num = 0;
  546. } else if (size == 4) {
  547. *num = av_int2float(avio_rb32(pb));
  548. } else if (size == 8){
  549. *num = av_int2double(avio_rb64(pb));
  550. } else
  551. return AVERROR_INVALIDDATA;
  552. return 0;
  553. }
  554. /*
  555. * Read the next element as an ASCII string.
  556. * 0 is success, < 0 is failure.
  557. */
  558. static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
  559. {
  560. av_free(*str);
  561. /* EBML strings are usually not 0-terminated, so we allocate one
  562. * byte more, read the string and NULL-terminate it ourselves. */
  563. if (!(*str = av_malloc(size + 1)))
  564. return AVERROR(ENOMEM);
  565. if (avio_read(pb, (uint8_t *) *str, size) != size) {
  566. av_freep(str);
  567. return AVERROR(EIO);
  568. }
  569. (*str)[size] = '\0';
  570. return 0;
  571. }
  572. /*
  573. * Read the next element as binary data.
  574. * 0 is success, < 0 is failure.
  575. */
  576. static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
  577. {
  578. av_free(bin->data);
  579. if (!(bin->data = av_malloc(length)))
  580. return AVERROR(ENOMEM);
  581. bin->size = length;
  582. bin->pos = avio_tell(pb);
  583. if (avio_read(pb, bin->data, length) != length) {
  584. av_freep(&bin->data);
  585. return AVERROR(EIO);
  586. }
  587. return 0;
  588. }
  589. /*
  590. * Read the next element, but only the header. The contents
  591. * are supposed to be sub-elements which can be read separately.
  592. * 0 is success, < 0 is failure.
  593. */
  594. static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length)
  595. {
  596. AVIOContext *pb = matroska->ctx->pb;
  597. MatroskaLevel *level;
  598. if (matroska->num_levels >= EBML_MAX_DEPTH) {
  599. av_log(matroska->ctx, AV_LOG_ERROR,
  600. "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
  601. return AVERROR(ENOSYS);
  602. }
  603. level = &matroska->levels[matroska->num_levels++];
  604. level->start = avio_tell(pb);
  605. level->length = length;
  606. return 0;
  607. }
  608. /*
  609. * Read signed/unsigned "EBML" numbers.
  610. * Return: number of bytes processed, < 0 on error
  611. */
  612. static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska,
  613. uint8_t *data, uint32_t size, uint64_t *num)
  614. {
  615. AVIOContext pb;
  616. ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
  617. return ebml_read_num(matroska, &pb, FFMIN(size, 8), num);
  618. }
  619. /*
  620. * Same as above, but signed.
  621. */
  622. static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
  623. uint8_t *data, uint32_t size, int64_t *num)
  624. {
  625. uint64_t unum;
  626. int res;
  627. /* read as unsigned number first */
  628. if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0)
  629. return res;
  630. /* make signed (weird way) */
  631. *num = unum - ((1LL << (7*res - 1)) - 1);
  632. return res;
  633. }
  634. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  635. EbmlSyntax *syntax, void *data);
  636. static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  637. uint32_t id, void *data)
  638. {
  639. int i;
  640. for (i=0; syntax[i].id; i++)
  641. if (id == syntax[i].id)
  642. break;
  643. if (!syntax[i].id && id == MATROSKA_ID_CLUSTER &&
  644. matroska->num_levels > 0 &&
  645. matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff)
  646. return 0; // we reached the end of an unknown size cluster
  647. if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32)
  648. av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
  649. return ebml_parse_elem(matroska, &syntax[i], data);
  650. }
  651. static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  652. void *data)
  653. {
  654. if (!matroska->current_id) {
  655. uint64_t id;
  656. int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id);
  657. if (res < 0)
  658. return res;
  659. matroska->current_id = id | 1 << 7*res;
  660. }
  661. return ebml_parse_id(matroska, syntax, matroska->current_id, data);
  662. }
  663. static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
  664. void *data)
  665. {
  666. int i, res = 0;
  667. for (i=0; syntax[i].id; i++)
  668. switch (syntax[i].type) {
  669. case EBML_UINT:
  670. *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
  671. break;
  672. case EBML_FLOAT:
  673. *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
  674. break;
  675. case EBML_STR:
  676. case EBML_UTF8:
  677. *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
  678. break;
  679. }
  680. while (!res && !ebml_level_end(matroska))
  681. res = ebml_parse(matroska, syntax, data);
  682. return res;
  683. }
  684. static int ebml_parse_elem(MatroskaDemuxContext *matroska,
  685. EbmlSyntax *syntax, void *data)
  686. {
  687. static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
  688. [EBML_UINT] = 8,
  689. [EBML_FLOAT] = 8,
  690. // max. 16 MB for strings
  691. [EBML_STR] = 0x1000000,
  692. [EBML_UTF8] = 0x1000000,
  693. // max. 256 MB for binary data
  694. [EBML_BIN] = 0x10000000,
  695. // no limits for anything else
  696. };
  697. AVIOContext *pb = matroska->ctx->pb;
  698. uint32_t id = syntax->id;
  699. uint64_t length;
  700. int res;
  701. void *newelem;
  702. data = (char *)data + syntax->data_offset;
  703. if (syntax->list_elem_size) {
  704. EbmlList *list = data;
  705. newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
  706. if (!newelem)
  707. return AVERROR(ENOMEM);
  708. list->elem = newelem;
  709. data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
  710. memset(data, 0, syntax->list_elem_size);
  711. list->nb_elem++;
  712. }
  713. if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {
  714. matroska->current_id = 0;
  715. if ((res = ebml_read_length(matroska, pb, &length)) < 0)
  716. return res;
  717. if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
  718. av_log(matroska->ctx, AV_LOG_ERROR,
  719. "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",
  720. length, max_lengths[syntax->type], syntax->type);
  721. return AVERROR_INVALIDDATA;
  722. }
  723. }
  724. switch (syntax->type) {
  725. case EBML_UINT: res = ebml_read_uint (pb, length, data); break;
  726. case EBML_FLOAT: res = ebml_read_float (pb, length, data); break;
  727. case EBML_STR:
  728. case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break;
  729. case EBML_BIN: res = ebml_read_binary(pb, length, data); break;
  730. case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0)
  731. return res;
  732. if (id == MATROSKA_ID_SEGMENT)
  733. matroska->segment_start = avio_tell(matroska->ctx->pb);
  734. return ebml_parse_nest(matroska, syntax->def.n, data);
  735. case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data);
  736. case EBML_STOP: return 1;
  737. default: return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;
  738. }
  739. if (res == AVERROR_INVALIDDATA)
  740. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
  741. else if (res == AVERROR(EIO))
  742. av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
  743. return res;
  744. }
  745. static void ebml_free(EbmlSyntax *syntax, void *data)
  746. {
  747. int i, j;
  748. for (i=0; syntax[i].id; i++) {
  749. void *data_off = (char *)data + syntax[i].data_offset;
  750. switch (syntax[i].type) {
  751. case EBML_STR:
  752. case EBML_UTF8: av_freep(data_off); break;
  753. case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break;
  754. case EBML_NEST:
  755. if (syntax[i].list_elem_size) {
  756. EbmlList *list = data_off;
  757. char *ptr = list->elem;
  758. for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
  759. ebml_free(syntax[i].def.n, ptr);
  760. av_free(list->elem);
  761. } else
  762. ebml_free(syntax[i].def.n, data_off);
  763. default: break;
  764. }
  765. }
  766. }
  767. /*
  768. * Autodetecting...
  769. */
  770. static int matroska_probe(AVProbeData *p)
  771. {
  772. uint64_t total = 0;
  773. int len_mask = 0x80, size = 1, n = 1, i;
  774. /* EBML header? */
  775. if (AV_RB32(p->buf) != EBML_ID_HEADER)
  776. return 0;
  777. /* length of header */
  778. total = p->buf[4];
  779. while (size <= 8 && !(total & len_mask)) {
  780. size++;
  781. len_mask >>= 1;
  782. }
  783. if (size > 8)
  784. return 0;
  785. total &= (len_mask - 1);
  786. while (n < size)
  787. total = (total << 8) | p->buf[4 + n++];
  788. /* Does the probe data contain the whole header? */
  789. if (p->buf_size < 4 + size + total)
  790. return 0;
  791. /* The header should contain a known document type. For now,
  792. * we don't parse the whole header but simply check for the
  793. * availability of that array of characters inside the header.
  794. * Not fully fool-proof, but good enough. */
  795. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
  796. int probelen = strlen(matroska_doctypes[i]);
  797. if (total < probelen)
  798. continue;
  799. for (n = 4+size; n <= 4+size+total-probelen; n++)
  800. if (!memcmp(p->buf+n, matroska_doctypes[i], probelen))
  801. return AVPROBE_SCORE_MAX;
  802. }
  803. // probably valid EBML header but no recognized doctype
  804. return AVPROBE_SCORE_MAX/2;
  805. }
  806. static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
  807. int num)
  808. {
  809. MatroskaTrack *tracks = matroska->tracks.elem;
  810. int i;
  811. for (i=0; i < matroska->tracks.nb_elem; i++)
  812. if (tracks[i].num == num)
  813. return &tracks[i];
  814. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num);
  815. return NULL;
  816. }
  817. static int matroska_decode_buffer(uint8_t** buf, int* buf_size,
  818. MatroskaTrack *track)
  819. {
  820. MatroskaTrackEncoding *encodings = track->encodings.elem;
  821. uint8_t* data = *buf;
  822. int isize = *buf_size;
  823. uint8_t* pkt_data = NULL;
  824. uint8_t av_unused *newpktdata;
  825. int pkt_size = isize;
  826. int result = 0;
  827. int olen;
  828. if (pkt_size >= 10000000)
  829. return -1;
  830. switch (encodings[0].compression.algo) {
  831. case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
  832. return encodings[0].compression.settings.size;
  833. case MATROSKA_TRACK_ENCODING_COMP_LZO:
  834. do {
  835. olen = pkt_size *= 3;
  836. pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING);
  837. result = av_lzo1x_decode(pkt_data, &olen, data, &isize);
  838. } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000);
  839. if (result)
  840. goto failed;
  841. pkt_size -= olen;
  842. break;
  843. #if CONFIG_ZLIB
  844. case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
  845. z_stream zstream = {0};
  846. if (inflateInit(&zstream) != Z_OK)
  847. return -1;
  848. zstream.next_in = data;
  849. zstream.avail_in = isize;
  850. do {
  851. pkt_size *= 3;
  852. newpktdata = av_realloc(pkt_data, pkt_size);
  853. if (!newpktdata) {
  854. inflateEnd(&zstream);
  855. goto failed;
  856. }
  857. pkt_data = newpktdata;
  858. zstream.avail_out = pkt_size - zstream.total_out;
  859. zstream.next_out = pkt_data + zstream.total_out;
  860. result = inflate(&zstream, Z_NO_FLUSH);
  861. } while (result==Z_OK && pkt_size<10000000);
  862. pkt_size = zstream.total_out;
  863. inflateEnd(&zstream);
  864. if (result != Z_STREAM_END)
  865. goto failed;
  866. break;
  867. }
  868. #endif
  869. #if CONFIG_BZLIB
  870. case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
  871. bz_stream bzstream = {0};
  872. if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
  873. return -1;
  874. bzstream.next_in = data;
  875. bzstream.avail_in = isize;
  876. do {
  877. pkt_size *= 3;
  878. newpktdata = av_realloc(pkt_data, pkt_size);
  879. if (!newpktdata) {
  880. BZ2_bzDecompressEnd(&bzstream);
  881. goto failed;
  882. }
  883. pkt_data = newpktdata;
  884. bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
  885. bzstream.next_out = pkt_data + bzstream.total_out_lo32;
  886. result = BZ2_bzDecompress(&bzstream);
  887. } while (result==BZ_OK && pkt_size<10000000);
  888. pkt_size = bzstream.total_out_lo32;
  889. BZ2_bzDecompressEnd(&bzstream);
  890. if (result != BZ_STREAM_END)
  891. goto failed;
  892. break;
  893. }
  894. #endif
  895. default:
  896. return -1;
  897. }
  898. *buf = pkt_data;
  899. *buf_size = pkt_size;
  900. return 0;
  901. failed:
  902. av_free(pkt_data);
  903. return -1;
  904. }
  905. static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
  906. AVPacket *pkt, uint64_t display_duration)
  907. {
  908. char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
  909. for (; *ptr!=',' && ptr<end-1; ptr++);
  910. if (*ptr == ',')
  911. layer = ++ptr;
  912. for (; *ptr!=',' && ptr<end-1; ptr++);
  913. if (*ptr == ',') {
  914. int64_t end_pts = pkt->pts + display_duration;
  915. int sc = matroska->time_scale * pkt->pts / 10000000;
  916. int ec = matroska->time_scale * end_pts / 10000000;
  917. int sh, sm, ss, eh, em, es, len;
  918. sh = sc/360000; sc -= 360000*sh;
  919. sm = sc/ 6000; sc -= 6000*sm;
  920. ss = sc/ 100; sc -= 100*ss;
  921. eh = ec/360000; ec -= 360000*eh;
  922. em = ec/ 6000; ec -= 6000*em;
  923. es = ec/ 100; ec -= 100*es;
  924. *ptr++ = '\0';
  925. len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
  926. if (!(line = av_malloc(len)))
  927. return;
  928. snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
  929. layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
  930. av_free(pkt->data);
  931. pkt->data = line;
  932. pkt->size = strlen(line);
  933. }
  934. }
  935. static int matroska_merge_packets(AVPacket *out, AVPacket *in)
  936. {
  937. void *newdata = av_realloc(out->data, out->size+in->size);
  938. if (!newdata)
  939. return AVERROR(ENOMEM);
  940. out->data = newdata;
  941. memcpy(out->data+out->size, in->data, in->size);
  942. out->size += in->size;
  943. av_destruct_packet(in);
  944. av_free(in);
  945. return 0;
  946. }
  947. static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
  948. AVDictionary **metadata, char *prefix)
  949. {
  950. MatroskaTag *tags = list->elem;
  951. char key[1024];
  952. int i;
  953. for (i=0; i < list->nb_elem; i++) {
  954. const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
  955. if (!tags[i].name) {
  956. av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
  957. continue;
  958. }
  959. if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
  960. else av_strlcpy(key, tags[i].name, sizeof(key));
  961. if (tags[i].def || !lang) {
  962. av_dict_set(metadata, key, tags[i].string, 0);
  963. if (tags[i].sub.nb_elem)
  964. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  965. }
  966. if (lang) {
  967. av_strlcat(key, "-", sizeof(key));
  968. av_strlcat(key, lang, sizeof(key));
  969. av_dict_set(metadata, key, tags[i].string, 0);
  970. if (tags[i].sub.nb_elem)
  971. matroska_convert_tag(s, &tags[i].sub, metadata, key);
  972. }
  973. }
  974. ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
  975. }
  976. static void matroska_convert_tags(AVFormatContext *s)
  977. {
  978. MatroskaDemuxContext *matroska = s->priv_data;
  979. MatroskaTags *tags = matroska->tags.elem;
  980. int i, j;
  981. for (i=0; i < matroska->tags.nb_elem; i++) {
  982. if (tags[i].target.attachuid) {
  983. MatroskaAttachement *attachment = matroska->attachments.elem;
  984. for (j=0; j<matroska->attachments.nb_elem; j++)
  985. if (attachment[j].uid == tags[i].target.attachuid
  986. && attachment[j].stream)
  987. matroska_convert_tag(s, &tags[i].tag,
  988. &attachment[j].stream->metadata, NULL);
  989. } else if (tags[i].target.chapteruid) {
  990. MatroskaChapter *chapter = matroska->chapters.elem;
  991. for (j=0; j<matroska->chapters.nb_elem; j++)
  992. if (chapter[j].uid == tags[i].target.chapteruid
  993. && chapter[j].chapter)
  994. matroska_convert_tag(s, &tags[i].tag,
  995. &chapter[j].chapter->metadata, NULL);
  996. } else if (tags[i].target.trackuid) {
  997. MatroskaTrack *track = matroska->tracks.elem;
  998. for (j=0; j<matroska->tracks.nb_elem; j++)
  999. if (track[j].uid == tags[i].target.trackuid && track[j].stream)
  1000. matroska_convert_tag(s, &tags[i].tag,
  1001. &track[j].stream->metadata, NULL);
  1002. } else {
  1003. matroska_convert_tag(s, &tags[i].tag, &s->metadata,
  1004. tags[i].target.type);
  1005. }
  1006. }
  1007. }
  1008. static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx)
  1009. {
  1010. EbmlList *seekhead_list = &matroska->seekhead;
  1011. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1012. uint32_t level_up = matroska->level_up;
  1013. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1014. uint32_t saved_id = matroska->current_id;
  1015. MatroskaLevel level;
  1016. int64_t offset;
  1017. int ret = 0;
  1018. if (idx >= seekhead_list->nb_elem
  1019. || seekhead[idx].id == MATROSKA_ID_SEEKHEAD
  1020. || seekhead[idx].id == MATROSKA_ID_CLUSTER)
  1021. return 0;
  1022. /* seek */
  1023. offset = seekhead[idx].pos + matroska->segment_start;
  1024. if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) {
  1025. /* We don't want to lose our seekhead level, so we add
  1026. * a dummy. This is a crude hack. */
  1027. if (matroska->num_levels == EBML_MAX_DEPTH) {
  1028. av_log(matroska->ctx, AV_LOG_INFO,
  1029. "Max EBML element depth (%d) reached, "
  1030. "cannot parse further.\n", EBML_MAX_DEPTH);
  1031. ret = AVERROR_INVALIDDATA;
  1032. } else {
  1033. level.start = 0;
  1034. level.length = (uint64_t)-1;
  1035. matroska->levels[matroska->num_levels] = level;
  1036. matroska->num_levels++;
  1037. matroska->current_id = 0;
  1038. ret = ebml_parse(matroska, matroska_segment, matroska);
  1039. /* remove dummy level */
  1040. while (matroska->num_levels) {
  1041. uint64_t length = matroska->levels[--matroska->num_levels].length;
  1042. if (length == (uint64_t)-1)
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. /* seek back */
  1048. avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
  1049. matroska->level_up = level_up;
  1050. matroska->current_id = saved_id;
  1051. return ret;
  1052. }
  1053. static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
  1054. {
  1055. EbmlList *seekhead_list = &matroska->seekhead;
  1056. int64_t before_pos = avio_tell(matroska->ctx->pb);
  1057. int i;
  1058. // we should not do any seeking in the streaming case
  1059. if (!matroska->ctx->pb->seekable ||
  1060. (matroska->ctx->flags & AVFMT_FLAG_IGNIDX))
  1061. return;
  1062. for (i = 0; i < seekhead_list->nb_elem; i++) {
  1063. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1064. if (seekhead[i].pos <= before_pos)
  1065. continue;
  1066. // defer cues parsing until we actually need cue data.
  1067. if (seekhead[i].id == MATROSKA_ID_CUES) {
  1068. matroska->cues_parsing_deferred = 1;
  1069. continue;
  1070. }
  1071. if (matroska_parse_seekhead_entry(matroska, i) < 0)
  1072. break;
  1073. }
  1074. }
  1075. static void matroska_parse_cues(MatroskaDemuxContext *matroska) {
  1076. EbmlList *seekhead_list = &matroska->seekhead;
  1077. MatroskaSeekhead *seekhead = seekhead_list->elem;
  1078. EbmlList *index_list;
  1079. MatroskaIndex *index;
  1080. int index_scale = 1;
  1081. int i, j;
  1082. for (i = 0; i < seekhead_list->nb_elem; i++)
  1083. if (seekhead[i].id == MATROSKA_ID_CUES)
  1084. break;
  1085. assert(i <= seekhead_list->nb_elem);
  1086. matroska_parse_seekhead_entry(matroska, i);
  1087. index_list = &matroska->index;
  1088. index = index_list->elem;
  1089. if (index_list->nb_elem
  1090. && index[0].time > 1E14/matroska->time_scale) {
  1091. av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n");
  1092. index_scale = matroska->time_scale;
  1093. }
  1094. for (i = 0; i < index_list->nb_elem; i++) {
  1095. EbmlList *pos_list = &index[i].pos;
  1096. MatroskaIndexPos *pos = pos_list->elem;
  1097. for (j = 0; j < pos_list->nb_elem; j++) {
  1098. MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track);
  1099. if (track && track->stream)
  1100. av_add_index_entry(track->stream,
  1101. pos[j].pos + matroska->segment_start,
  1102. index[i].time/index_scale, 0, 0,
  1103. AVINDEX_KEYFRAME);
  1104. }
  1105. }
  1106. }
  1107. static int matroska_aac_profile(char *codec_id)
  1108. {
  1109. static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" };
  1110. int profile;
  1111. for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++)
  1112. if (strstr(codec_id, aac_profiles[profile]))
  1113. break;
  1114. return profile + 1;
  1115. }
  1116. static int matroska_aac_sri(int samplerate)
  1117. {
  1118. int sri;
  1119. for (sri=0; sri<FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++)
  1120. if (avpriv_mpeg4audio_sample_rates[sri] == samplerate)
  1121. break;
  1122. return sri;
  1123. }
  1124. static int matroska_read_header(AVFormatContext *s)
  1125. {
  1126. MatroskaDemuxContext *matroska = s->priv_data;
  1127. EbmlList *attachements_list = &matroska->attachments;
  1128. MatroskaAttachement *attachements;
  1129. EbmlList *chapters_list = &matroska->chapters;
  1130. MatroskaChapter *chapters;
  1131. MatroskaTrack *tracks;
  1132. uint64_t max_start = 0;
  1133. Ebml ebml = { 0 };
  1134. AVStream *st;
  1135. int i, j, res;
  1136. matroska->ctx = s;
  1137. /* First read the EBML header. */
  1138. if (ebml_parse(matroska, ebml_syntax, &ebml)
  1139. || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t)
  1140. || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 2) {
  1141. av_log(matroska->ctx, AV_LOG_ERROR,
  1142. "EBML header using unsupported features\n"
  1143. "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
  1144. ebml.version, ebml.doctype, ebml.doctype_version);
  1145. ebml_free(ebml_syntax, &ebml);
  1146. return AVERROR_PATCHWELCOME;
  1147. }
  1148. for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
  1149. if (!strcmp(ebml.doctype, matroska_doctypes[i]))
  1150. break;
  1151. if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
  1152. av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
  1153. }
  1154. ebml_free(ebml_syntax, &ebml);
  1155. /* The next thing is a segment. */
  1156. if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0)
  1157. return res;
  1158. matroska_execute_seekhead(matroska);
  1159. if (!matroska->time_scale)
  1160. matroska->time_scale = 1000000;
  1161. if (matroska->duration)
  1162. matroska->ctx->duration = matroska->duration * matroska->time_scale
  1163. * 1000 / AV_TIME_BASE;
  1164. av_dict_set(&s->metadata, "title", matroska->title, 0);
  1165. tracks = matroska->tracks.elem;
  1166. for (i=0; i < matroska->tracks.nb_elem; i++) {
  1167. MatroskaTrack *track = &tracks[i];
  1168. enum CodecID codec_id = CODEC_ID_NONE;
  1169. EbmlList *encodings_list = &tracks->encodings;
  1170. MatroskaTrackEncoding *encodings = encodings_list->elem;
  1171. uint8_t *extradata = NULL;
  1172. int extradata_size = 0;
  1173. int extradata_offset = 0;
  1174. AVIOContext b;
  1175. /* Apply some sanity checks. */
  1176. if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
  1177. track->type != MATROSKA_TRACK_TYPE_AUDIO &&
  1178. track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1179. av_log(matroska->ctx, AV_LOG_INFO,
  1180. "Unknown or unsupported track type %"PRIu64"\n",
  1181. track->type);
  1182. continue;
  1183. }
  1184. if (track->codec_id == NULL)
  1185. continue;
  1186. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1187. if (!track->default_duration)
  1188. track->default_duration = 1000000000/track->video.frame_rate;
  1189. if (!track->video.display_width)
  1190. track->video.display_width = track->video.pixel_width;
  1191. if (!track->video.display_height)
  1192. track->video.display_height = track->video.pixel_height;
  1193. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1194. if (!track->audio.out_samplerate)
  1195. track->audio.out_samplerate = track->audio.samplerate;
  1196. }
  1197. if (encodings_list->nb_elem > 1) {
  1198. av_log(matroska->ctx, AV_LOG_ERROR,
  1199. "Multiple combined encodings not supported");
  1200. } else if (encodings_list->nb_elem == 1) {
  1201. if (encodings[0].type ||
  1202. (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
  1203. #if CONFIG_ZLIB
  1204. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
  1205. #endif
  1206. #if CONFIG_BZLIB
  1207. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
  1208. #endif
  1209. encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) {
  1210. encodings[0].scope = 0;
  1211. av_log(matroska->ctx, AV_LOG_ERROR,
  1212. "Unsupported encoding type");
  1213. } else if (track->codec_priv.size && encodings[0].scope&2) {
  1214. uint8_t *codec_priv = track->codec_priv.data;
  1215. int offset = matroska_decode_buffer(&track->codec_priv.data,
  1216. &track->codec_priv.size,
  1217. track);
  1218. if (offset < 0) {
  1219. track->codec_priv.data = NULL;
  1220. track->codec_priv.size = 0;
  1221. av_log(matroska->ctx, AV_LOG_ERROR,
  1222. "Failed to decode codec private data\n");
  1223. } else if (offset > 0) {
  1224. track->codec_priv.data = av_malloc(track->codec_priv.size + offset);
  1225. memcpy(track->codec_priv.data,
  1226. encodings[0].compression.settings.data, offset);
  1227. memcpy(track->codec_priv.data+offset, codec_priv,
  1228. track->codec_priv.size);
  1229. track->codec_priv.size += offset;
  1230. }
  1231. if (codec_priv != track->codec_priv.data)
  1232. av_free(codec_priv);
  1233. }
  1234. }
  1235. for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
  1236. if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
  1237. strlen(ff_mkv_codec_tags[j].str))){
  1238. codec_id= ff_mkv_codec_tags[j].id;
  1239. break;
  1240. }
  1241. }
  1242. st = track->stream = avformat_new_stream(s, NULL);
  1243. if (st == NULL)
  1244. return AVERROR(ENOMEM);
  1245. if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
  1246. && track->codec_priv.size >= 40
  1247. && track->codec_priv.data != NULL) {
  1248. track->ms_compat = 1;
  1249. track->video.fourcc = AV_RL32(track->codec_priv.data + 16);
  1250. codec_id = ff_codec_get_id(ff_codec_bmp_tags, track->video.fourcc);
  1251. extradata_offset = 40;
  1252. } else if (!strcmp(track->codec_id, "A_MS/ACM")
  1253. && track->codec_priv.size >= 14
  1254. && track->codec_priv.data != NULL) {
  1255. int ret;
  1256. ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size,
  1257. AVIO_FLAG_READ, NULL, NULL, NULL, NULL);
  1258. ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size);
  1259. if (ret < 0)
  1260. return ret;
  1261. codec_id = st->codec->codec_id;
  1262. extradata_offset = FFMIN(track->codec_priv.size, 18);
  1263. } else if (!strcmp(track->codec_id, "V_QUICKTIME")
  1264. && (track->codec_priv.size >= 86)
  1265. && (track->codec_priv.data != NULL)) {
  1266. track->video.fourcc = AV_RL32(track->codec_priv.data);
  1267. codec_id=ff_codec_get_id(ff_codec_movvideo_tags, track->video.fourcc);
  1268. } else if (codec_id == CODEC_ID_PCM_S16BE) {
  1269. switch (track->audio.bitdepth) {
  1270. case 8: codec_id = CODEC_ID_PCM_U8; break;
  1271. case 24: codec_id = CODEC_ID_PCM_S24BE; break;
  1272. case 32: codec_id = CODEC_ID_PCM_S32BE; break;
  1273. }
  1274. } else if (codec_id == CODEC_ID_PCM_S16LE) {
  1275. switch (track->audio.bitdepth) {
  1276. case 8: codec_id = CODEC_ID_PCM_U8; break;
  1277. case 24: codec_id = CODEC_ID_PCM_S24LE; break;
  1278. case 32: codec_id = CODEC_ID_PCM_S32LE; break;
  1279. }
  1280. } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) {
  1281. codec_id = CODEC_ID_PCM_F64LE;
  1282. } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) {
  1283. int profile = matroska_aac_profile(track->codec_id);
  1284. int sri = matroska_aac_sri(track->audio.samplerate);
  1285. extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE);
  1286. if (extradata == NULL)
  1287. return AVERROR(ENOMEM);
  1288. extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
  1289. extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3);
  1290. if (strstr(track->codec_id, "SBR")) {
  1291. sri = matroska_aac_sri(track->audio.out_samplerate);
  1292. extradata[2] = 0x56;
  1293. extradata[3] = 0xE5;
  1294. extradata[4] = 0x80 | (sri<<3);
  1295. extradata_size = 5;
  1296. } else
  1297. extradata_size = 2;
  1298. } else if (codec_id == CODEC_ID_TTA) {
  1299. extradata_size = 30;
  1300. extradata = av_mallocz(extradata_size);
  1301. if (extradata == NULL)
  1302. return AVERROR(ENOMEM);
  1303. ffio_init_context(&b, extradata, extradata_size, 1,
  1304. NULL, NULL, NULL, NULL);
  1305. avio_write(&b, "TTA1", 4);
  1306. avio_wl16(&b, 1);
  1307. avio_wl16(&b, track->audio.channels);
  1308. avio_wl16(&b, track->audio.bitdepth);
  1309. avio_wl32(&b, track->audio.out_samplerate);
  1310. avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate);
  1311. } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
  1312. codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
  1313. extradata_offset = 26;
  1314. } else if (codec_id == CODEC_ID_RA_144) {
  1315. track->audio.out_samplerate = 8000;
  1316. track->audio.channels = 1;
  1317. } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK ||
  1318. codec_id == CODEC_ID_ATRAC3 || codec_id == CODEC_ID_SIPR) {
  1319. int flavor;
  1320. ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size,
  1321. 0, NULL, NULL, NULL, NULL);
  1322. avio_skip(&b, 22);
  1323. flavor = avio_rb16(&b);
  1324. track->audio.coded_framesize = avio_rb32(&b);
  1325. avio_skip(&b, 12);
  1326. track->audio.sub_packet_h = avio_rb16(&b);
  1327. track->audio.frame_size = avio_rb16(&b);
  1328. track->audio.sub_packet_size = avio_rb16(&b);
  1329. track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h);
  1330. if (codec_id == CODEC_ID_RA_288) {
  1331. st->codec->block_align = track->audio.coded_framesize;
  1332. track->codec_priv.size = 0;
  1333. } else {
  1334. if (codec_id == CODEC_ID_SIPR && flavor < 4) {
  1335. const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
  1336. track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
  1337. st->codec->bit_rate = sipr_bit_rate[flavor];
  1338. }
  1339. st->codec->block_align = track->audio.sub_packet_size;
  1340. extradata_offset = 78;
  1341. }
  1342. }
  1343. track->codec_priv.size -= extradata_offset;
  1344. if (codec_id == CODEC_ID_NONE)
  1345. av_log(matroska->ctx, AV_LOG_INFO,
  1346. "Unknown/unsupported CodecID %s.\n", track->codec_id);
  1347. if (track->time_scale < 0.01)
  1348. track->time_scale = 1.0;
  1349. avpriv_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
  1350. st->codec->codec_id = codec_id;
  1351. st->start_time = 0;
  1352. if (strcmp(track->language, "und"))
  1353. av_dict_set(&st->metadata, "language", track->language, 0);
  1354. av_dict_set(&st->metadata, "title", track->name, 0);
  1355. if (track->flag_default)
  1356. st->disposition |= AV_DISPOSITION_DEFAULT;
  1357. if (track->flag_forced)
  1358. st->disposition |= AV_DISPOSITION_FORCED;
  1359. if (!st->codec->extradata) {
  1360. if(extradata){
  1361. st->codec->extradata = extradata;
  1362. st->codec->extradata_size = extradata_size;
  1363. } else if(track->codec_priv.data && track->codec_priv.size > 0){
  1364. st->codec->extradata = av_mallocz(track->codec_priv.size +
  1365. FF_INPUT_BUFFER_PADDING_SIZE);
  1366. if(st->codec->extradata == NULL)
  1367. return AVERROR(ENOMEM);
  1368. st->codec->extradata_size = track->codec_priv.size;
  1369. memcpy(st->codec->extradata,
  1370. track->codec_priv.data + extradata_offset,
  1371. track->codec_priv.size);
  1372. }
  1373. }
  1374. if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
  1375. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1376. st->codec->codec_tag = track->video.fourcc;
  1377. st->codec->width = track->video.pixel_width;
  1378. st->codec->height = track->video.pixel_height;
  1379. av_reduce(&st->sample_aspect_ratio.num,
  1380. &st->sample_aspect_ratio.den,
  1381. st->codec->height * track->video.display_width,
  1382. st->codec-> width * track->video.display_height,
  1383. 255);
  1384. if (st->codec->codec_id != CODEC_ID_H264)
  1385. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1386. if (track->default_duration)
  1387. st->avg_frame_rate = av_d2q(1000000000.0/track->default_duration, INT_MAX);
  1388. } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
  1389. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1390. st->codec->sample_rate = track->audio.out_samplerate;
  1391. st->codec->channels = track->audio.channels;
  1392. if (st->codec->codec_id != CODEC_ID_AAC)
  1393. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1394. } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
  1395. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1396. }
  1397. }
  1398. attachements = attachements_list->elem;
  1399. for (j=0; j<attachements_list->nb_elem; j++) {
  1400. if (!(attachements[j].filename && attachements[j].mime &&
  1401. attachements[j].bin.data && attachements[j].bin.size > 0)) {
  1402. av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
  1403. } else {
  1404. AVStream *st = avformat_new_stream(s, NULL);
  1405. if (st == NULL)
  1406. break;
  1407. av_dict_set(&st->metadata, "filename",attachements[j].filename, 0);
  1408. av_dict_set(&st->metadata, "mimetype", attachements[j].mime, 0);
  1409. st->codec->codec_id = CODEC_ID_NONE;
  1410. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  1411. st->codec->extradata = av_malloc(attachements[j].bin.size);
  1412. if(st->codec->extradata == NULL)
  1413. break;
  1414. st->codec->extradata_size = attachements[j].bin.size;
  1415. memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size);
  1416. for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
  1417. if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime,
  1418. strlen(ff_mkv_mime_tags[i].str))) {
  1419. st->codec->codec_id = ff_mkv_mime_tags[i].id;
  1420. break;
  1421. }
  1422. }
  1423. attachements[j].stream = st;
  1424. }
  1425. }
  1426. chapters = chapters_list->elem;
  1427. for (i=0; i<chapters_list->nb_elem; i++)
  1428. if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid
  1429. && (max_start==0 || chapters[i].start > max_start)) {
  1430. chapters[i].chapter =
  1431. avpriv_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000},
  1432. chapters[i].start, chapters[i].end,
  1433. chapters[i].title);
  1434. av_dict_set(&chapters[i].chapter->metadata,
  1435. "title", chapters[i].title, 0);
  1436. max_start = chapters[i].start;
  1437. }
  1438. matroska_convert_tags(s);
  1439. return 0;
  1440. }
  1441. /*
  1442. * Put one packet in an application-supplied AVPacket struct.
  1443. * Returns 0 on success or -1 on failure.
  1444. */
  1445. static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
  1446. AVPacket *pkt)
  1447. {
  1448. if (matroska->num_packets > 0) {
  1449. memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
  1450. av_free(matroska->packets[0]);
  1451. if (matroska->num_packets > 1) {
  1452. void *newpackets;
  1453. memmove(&matroska->packets[0], &matroska->packets[1],
  1454. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1455. newpackets = av_realloc(matroska->packets,
  1456. (matroska->num_packets - 1) * sizeof(AVPacket *));
  1457. if (newpackets)
  1458. matroska->packets = newpackets;
  1459. } else {
  1460. av_freep(&matroska->packets);
  1461. }
  1462. matroska->num_packets--;
  1463. return 0;
  1464. }
  1465. return -1;
  1466. }
  1467. /*
  1468. * Free all packets in our internal queue.
  1469. */
  1470. static void matroska_clear_queue(MatroskaDemuxContext *matroska)
  1471. {
  1472. if (matroska->packets) {
  1473. int n;
  1474. for (n = 0; n < matroska->num_packets; n++) {
  1475. av_free_packet(matroska->packets[n]);
  1476. av_free(matroska->packets[n]);
  1477. }
  1478. av_freep(&matroska->packets);
  1479. matroska->num_packets = 0;
  1480. }
  1481. }
  1482. static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data,
  1483. int size, int64_t pos, uint64_t cluster_time,
  1484. uint64_t duration, int is_keyframe,
  1485. int64_t cluster_pos)
  1486. {
  1487. uint64_t timecode = AV_NOPTS_VALUE;
  1488. MatroskaTrack *track;
  1489. int res = 0;
  1490. AVStream *st;
  1491. AVPacket *pkt;
  1492. int16_t block_time;
  1493. uint32_t *lace_size = NULL;
  1494. int n, flags, laces = 0;
  1495. uint64_t num;
  1496. if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) {
  1497. av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
  1498. return res;
  1499. }
  1500. data += n;
  1501. size -= n;
  1502. track = matroska_find_track_by_num(matroska, num);
  1503. if (!track || !track->stream) {
  1504. av_log(matroska->ctx, AV_LOG_INFO,
  1505. "Invalid stream %"PRIu64" or size %u\n", num, size);
  1506. return AVERROR_INVALIDDATA;
  1507. } else if (size <= 3)
  1508. return 0;
  1509. st = track->stream;
  1510. if (st->discard >= AVDISCARD_ALL)
  1511. return res;
  1512. if (duration == AV_NOPTS_VALUE)
  1513. duration = track->default_duration / matroska->time_scale;
  1514. block_time = AV_RB16(data);
  1515. data += 2;
  1516. flags = *data++;
  1517. size -= 3;
  1518. if (is_keyframe == -1)
  1519. is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
  1520. if (cluster_time != (uint64_t)-1
  1521. && (block_time >= 0 || cluster_time >= -block_time)) {
  1522. timecode = cluster_time + block_time;
  1523. if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE
  1524. && timecode < track->end_timecode)
  1525. is_keyframe = 0; /* overlapping subtitles are not key frame */
  1526. if (is_keyframe)
  1527. av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME);
  1528. track->end_timecode = FFMAX(track->end_timecode, timecode+duration);
  1529. }
  1530. if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
  1531. if (!is_keyframe || timecode < matroska->skip_to_timecode)
  1532. return res;
  1533. matroska->skip_to_keyframe = 0;
  1534. }
  1535. switch ((flags & 0x06) >> 1) {
  1536. case 0x0: /* no lacing */
  1537. laces = 1;
  1538. lace_size = av_mallocz(sizeof(int));
  1539. lace_size[0] = size;
  1540. break;
  1541. case 0x1: /* Xiph lacing */
  1542. case 0x2: /* fixed-size lacing */
  1543. case 0x3: /* EBML lacing */
  1544. assert(size>0); // size <=3 is checked before size-=3 above
  1545. laces = (*data) + 1;
  1546. data += 1;
  1547. size -= 1;
  1548. lace_size = av_mallocz(laces * sizeof(int));
  1549. switch ((flags & 0x06) >> 1) {
  1550. case 0x1: /* Xiph lacing */ {
  1551. uint8_t temp;
  1552. uint32_t total = 0;
  1553. for (n = 0; res == 0 && n < laces - 1; n++) {
  1554. while (1) {
  1555. if (size == 0) {
  1556. res = -1;
  1557. break;
  1558. }
  1559. temp = *data;
  1560. lace_size[n] += temp;
  1561. data += 1;
  1562. size -= 1;
  1563. if (temp != 0xff)
  1564. break;
  1565. }
  1566. total += lace_size[n];
  1567. }
  1568. lace_size[n] = size - total;
  1569. break;
  1570. }
  1571. case 0x2: /* fixed-size lacing */
  1572. for (n = 0; n < laces; n++)
  1573. lace_size[n] = size / laces;
  1574. break;
  1575. case 0x3: /* EBML lacing */ {
  1576. uint32_t total;
  1577. n = matroska_ebmlnum_uint(matroska, data, size, &num);
  1578. if (n < 0) {
  1579. av_log(matroska->ctx, AV_LOG_INFO,
  1580. "EBML block data error\n");
  1581. break;
  1582. }
  1583. data += n;
  1584. size -= n;
  1585. total = lace_size[0] = num;
  1586. for (n = 1; res == 0 && n < laces - 1; n++) {
  1587. int64_t snum;
  1588. int r;
  1589. r = matroska_ebmlnum_sint(matroska, data, size, &snum);
  1590. if (r < 0) {
  1591. av_log(matroska->ctx, AV_LOG_INFO,
  1592. "EBML block data error\n");
  1593. break;
  1594. }
  1595. data += r;
  1596. size -= r;
  1597. lace_size[n] = lace_size[n - 1] + snum;
  1598. total += lace_size[n];
  1599. }
  1600. lace_size[laces - 1] = size - total;
  1601. break;
  1602. }
  1603. }
  1604. break;
  1605. }
  1606. if (res == 0) {
  1607. for (n = 0; n < laces; n++) {
  1608. if ((st->codec->codec_id == CODEC_ID_RA_288 ||
  1609. st->codec->codec_id == CODEC_ID_COOK ||
  1610. st->codec->codec_id == CODEC_ID_SIPR ||
  1611. st->codec->codec_id == CODEC_ID_ATRAC3) &&
  1612. st->codec->block_align && track->audio.sub_packet_size) {
  1613. int a = st->codec->block_align;
  1614. int sps = track->audio.sub_packet_size;
  1615. int cfs = track->audio.coded_framesize;
  1616. int h = track->audio.sub_packet_h;
  1617. int y = track->audio.sub_packet_cnt;
  1618. int w = track->audio.frame_size;
  1619. int x;
  1620. if (!track->audio.pkt_cnt) {
  1621. if (track->audio.sub_packet_cnt == 0)
  1622. track->audio.buf_timecode = timecode;
  1623. if (st->codec->codec_id == CODEC_ID_RA_288)
  1624. for (x=0; x<h/2; x++)
  1625. memcpy(track->audio.buf+x*2*w+y*cfs,
  1626. data+x*cfs, cfs);
  1627. else if (st->codec->codec_id == CODEC_ID_SIPR)
  1628. memcpy(track->audio.buf + y*w, data, w);
  1629. else
  1630. for (x=0; x<w/sps; x++)
  1631. memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
  1632. if (++track->audio.sub_packet_cnt >= h) {
  1633. if (st->codec->codec_id == CODEC_ID_SIPR)
  1634. ff_rm_reorder_sipr_data(track->audio.buf, h, w);
  1635. track->audio.sub_packet_cnt = 0;
  1636. track->audio.pkt_cnt = h*w / a;
  1637. }
  1638. }
  1639. while (track->audio.pkt_cnt) {
  1640. pkt = av_mallocz(sizeof(AVPacket));
  1641. av_new_packet(pkt, a);
  1642. memcpy(pkt->data, track->audio.buf
  1643. + a * (h*w / a - track->audio.pkt_cnt--), a);
  1644. pkt->pts = track->audio.buf_timecode;
  1645. track->audio.buf_timecode = AV_NOPTS_VALUE;
  1646. pkt->pos = pos;
  1647. pkt->stream_index = st->index;
  1648. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1649. }
  1650. } else {
  1651. MatroskaTrackEncoding *encodings = track->encodings.elem;
  1652. int offset = 0, pkt_size = lace_size[n];
  1653. uint8_t *pkt_data = data;
  1654. if (pkt_size > size) {
  1655. av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n");
  1656. break;
  1657. }
  1658. if (encodings && encodings->scope & 1) {
  1659. offset = matroska_decode_buffer(&pkt_data,&pkt_size, track);
  1660. if (offset < 0)
  1661. continue;
  1662. }
  1663. pkt = av_mallocz(sizeof(AVPacket));
  1664. /* XXX: prevent data copy... */
  1665. if (av_new_packet(pkt, pkt_size+offset) < 0) {
  1666. av_free(pkt);
  1667. res = AVERROR(ENOMEM);
  1668. break;
  1669. }
  1670. if (offset)
  1671. memcpy (pkt->data, encodings->compression.settings.data, offset);
  1672. memcpy (pkt->data+offset, pkt_data, pkt_size);
  1673. if (pkt_data != data)
  1674. av_free(pkt_data);
  1675. if (n == 0)
  1676. pkt->flags = is_keyframe;
  1677. pkt->stream_index = st->index;
  1678. if (track->ms_compat)
  1679. pkt->dts = timecode;
  1680. else
  1681. pkt->pts = timecode;
  1682. pkt->pos = pos;
  1683. if (st->codec->codec_id == CODEC_ID_TEXT)
  1684. pkt->convergence_duration = duration;
  1685. else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE)
  1686. pkt->duration = duration;
  1687. if (st->codec->codec_id == CODEC_ID_SSA)
  1688. matroska_fix_ass_packet(matroska, pkt, duration);
  1689. if (matroska->prev_pkt &&
  1690. timecode != AV_NOPTS_VALUE &&
  1691. matroska->prev_pkt->pts == timecode &&
  1692. matroska->prev_pkt->stream_index == st->index &&
  1693. st->codec->codec_id == CODEC_ID_SSA)
  1694. matroska_merge_packets(matroska->prev_pkt, pkt);
  1695. else {
  1696. dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
  1697. matroska->prev_pkt = pkt;
  1698. }
  1699. }
  1700. if (timecode != AV_NOPTS_VALUE)
  1701. timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
  1702. data += lace_size[n];
  1703. size -= lace_size[n];
  1704. }
  1705. }
  1706. av_free(lace_size);
  1707. return res;
  1708. }
  1709. static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
  1710. {
  1711. MatroskaCluster cluster = { 0 };
  1712. EbmlList *blocks_list;
  1713. MatroskaBlock *blocks;
  1714. int i, res;
  1715. int64_t pos = avio_tell(matroska->ctx->pb);
  1716. matroska->prev_pkt = NULL;
  1717. if (matroska->current_id)
  1718. pos -= 4; /* sizeof the ID which was already read */
  1719. res = ebml_parse(matroska, matroska_clusters, &cluster);
  1720. blocks_list = &cluster.blocks;
  1721. blocks = blocks_list->elem;
  1722. for (i=0; i<blocks_list->nb_elem && !res; i++)
  1723. if (blocks[i].bin.size > 0 && blocks[i].bin.data) {
  1724. int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1;
  1725. if (!blocks[i].non_simple)
  1726. blocks[i].duration = AV_NOPTS_VALUE;
  1727. res=matroska_parse_block(matroska,
  1728. blocks[i].bin.data, blocks[i].bin.size,
  1729. blocks[i].bin.pos, cluster.timecode,
  1730. blocks[i].duration, is_keyframe,
  1731. pos);
  1732. }
  1733. ebml_free(matroska_cluster, &cluster);
  1734. if (res < 0) matroska->done = 1;
  1735. return res;
  1736. }
  1737. static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
  1738. {
  1739. MatroskaDemuxContext *matroska = s->priv_data;
  1740. int ret = 0;
  1741. while (!ret && matroska_deliver_packet(matroska, pkt)) {
  1742. if (matroska->done)
  1743. return AVERROR_EOF;
  1744. ret = matroska_parse_cluster(matroska);
  1745. }
  1746. return ret;
  1747. }
  1748. static int matroska_read_seek(AVFormatContext *s, int stream_index,
  1749. int64_t timestamp, int flags)
  1750. {
  1751. MatroskaDemuxContext *matroska = s->priv_data;
  1752. MatroskaTrack *tracks = matroska->tracks.elem;
  1753. AVStream *st = s->streams[stream_index];
  1754. int i, index, index_sub, index_min;
  1755. /* Parse the CUES now since we need the index data to seek. */
  1756. if (matroska->cues_parsing_deferred) {
  1757. matroska_parse_cues(matroska);
  1758. matroska->cues_parsing_deferred = 0;
  1759. }
  1760. if (!st->nb_index_entries)
  1761. return 0;
  1762. timestamp = FFMAX(timestamp, st->index_entries[0].timestamp);
  1763. if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  1764. avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET);
  1765. matroska->current_id = 0;
  1766. while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) {
  1767. matroska_clear_queue(matroska);
  1768. if (matroska_parse_cluster(matroska) < 0)
  1769. break;
  1770. }
  1771. }
  1772. matroska_clear_queue(matroska);
  1773. if (index < 0)
  1774. return 0;
  1775. index_min = index;
  1776. for (i=0; i < matroska->tracks.nb_elem; i++) {
  1777. tracks[i].audio.pkt_cnt = 0;
  1778. tracks[i].audio.sub_packet_cnt = 0;
  1779. tracks[i].audio.buf_timecode = AV_NOPTS_VALUE;
  1780. tracks[i].end_timecode = 0;
  1781. if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE
  1782. && !tracks[i].stream->discard != AVDISCARD_ALL) {
  1783. index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD);
  1784. if (index_sub >= 0
  1785. && st->index_entries[index_sub].pos < st->index_entries[index_min].pos
  1786. && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale)
  1787. index_min = index_sub;
  1788. }
  1789. }
  1790. avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET);
  1791. matroska->current_id = 0;
  1792. matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
  1793. matroska->skip_to_timecode = st->index_entries[index].timestamp;
  1794. matroska->done = 0;
  1795. ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
  1796. return 0;
  1797. }
  1798. static int matroska_read_close(AVFormatContext *s)
  1799. {
  1800. MatroskaDemuxContext *matroska = s->priv_data;
  1801. MatroskaTrack *tracks = matroska->tracks.elem;
  1802. int n;
  1803. matroska_clear_queue(matroska);
  1804. for (n=0; n < matroska->tracks.nb_elem; n++)
  1805. if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
  1806. av_free(tracks[n].audio.buf);
  1807. ebml_free(matroska_segment, matroska);
  1808. return 0;
  1809. }
  1810. AVInputFormat ff_matroska_demuxer = {
  1811. .name = "matroska,webm",
  1812. .long_name = NULL_IF_CONFIG_SMALL("Matroska/WebM file format"),
  1813. .priv_data_size = sizeof(MatroskaDemuxContext),
  1814. .read_probe = matroska_probe,
  1815. .read_header = matroska_read_header,
  1816. .read_packet = matroska_read_packet,
  1817. .read_close = matroska_read_close,
  1818. .read_seek = matroska_read_seek,
  1819. };