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