FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/matroskadec.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 1576 2148 73.4%
Functions: 56 60 93.3%
Branches: 954 1543 61.8%

Line Branch Exec Source
1 /*
2 * Matroska file demuxer
3 * Copyright (c) 2003-2008 The FFmpeg Project
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 #include "config_components.h"
33
34 #include <inttypes.h>
35 #include <stdio.h>
36
37 #include "libavutil/avstring.h"
38 #include "libavutil/base64.h"
39 #include "libavutil/bprint.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/dict_internal.h"
42 #include "libavutil/display.h"
43 #include "libavutil/intfloat.h"
44 #include "libavutil/intreadwrite.h"
45 #include "libavutil/lzo.h"
46 #include "libavutil/mastering_display_metadata.h"
47 #include "libavutil/mathematics.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/pixdesc.h"
50 #include "libavutil/time_internal.h"
51 #include "libavutil/spherical.h"
52
53 #include "libavcodec/bytestream.h"
54 #include "libavcodec/flac.h"
55 #include "libavcodec/mpeg4audio.h"
56 #include "libavcodec/packet_internal.h"
57
58 #include "avformat.h"
59 #include "avio_internal.h"
60 #include "demux.h"
61 #include "dovi_isom.h"
62 #include "internal.h"
63 #include "isom.h"
64 #include "matroska.h"
65 #include "oggdec.h"
66 /* For ff_codec_get_id(). */
67 #include "riff.h"
68 #include "rmsipr.h"
69
70 #if CONFIG_BZLIB
71 #include <bzlib.h>
72 #endif
73 #if CONFIG_ZLIB
74 #include <zlib.h>
75 #endif
76
77 #include "qtpalette.h"
78
79 #define EBML_UNKNOWN_LENGTH UINT64_MAX /* EBML unknown length, in uint64_t */
80 #define NEEDS_CHECKING 2 /* Indicates that some error checks
81 * still need to be performed */
82 #define LEVEL_ENDED 3 /* return value of ebml_parse when the
83 * syntax level used for parsing ended. */
84 #define SKIP_THRESHOLD 1024 * 1024 /* In non-seekable mode, if more than SKIP_THRESHOLD
85 * of unkown, potentially damaged data is encountered,
86 * it is considered an error. */
87 #define UNKNOWN_EQUIV 50 * 1024 /* An unknown element is considered equivalent
88 * to this many bytes of unknown data for the
89 * SKIP_THRESHOLD check. */
90
91 typedef enum {
92 EBML_NONE,
93 EBML_UINT,
94 EBML_SINT,
95 EBML_FLOAT,
96 EBML_STR,
97 EBML_UTF8,
98 EBML_BIN,
99 EBML_NEST,
100 EBML_LEVEL1,
101 EBML_STOP,
102 EBML_TYPE_COUNT
103 } EbmlType;
104
105 typedef struct CountedElement {
106 union {
107 uint64_t u;
108 int64_t i;
109 double f;
110 char *s;
111 } el;
112 unsigned count;
113 } CountedElement;
114
115 typedef const struct EbmlSyntax {
116 uint32_t id;
117 uint8_t type;
118 uint8_t is_counted;
119 size_t list_elem_size;
120 size_t data_offset;
121 union {
122 int64_t i;
123 uint64_t u;
124 double f;
125 const char *s;
126 const struct EbmlSyntax *n;
127 } def;
128 } EbmlSyntax;
129
130 typedef struct EbmlList {
131 int nb_elem;
132 unsigned int alloc_elem_size;
133 void *elem;
134 } EbmlList;
135
136 typedef struct EbmlBin {
137 int size;
138 AVBufferRef *buf;
139 uint8_t *data;
140 int64_t pos;
141 } EbmlBin;
142
143 typedef struct Ebml {
144 uint64_t version;
145 uint64_t max_size;
146 uint64_t id_length;
147 char *doctype;
148 uint64_t doctype_version;
149 } Ebml;
150
151 typedef struct MatroskaTrackCompression {
152 uint64_t algo;
153 EbmlBin settings;
154 } MatroskaTrackCompression;
155
156 typedef struct MatroskaTrackEncryption {
157 uint64_t algo;
158 EbmlBin key_id;
159 } MatroskaTrackEncryption;
160
161 typedef struct MatroskaTrackEncoding {
162 uint64_t scope;
163 uint64_t type;
164 MatroskaTrackCompression compression;
165 MatroskaTrackEncryption encryption;
166 } MatroskaTrackEncoding;
167
168 typedef struct MatroskaMasteringMeta {
169 double r_x;
170 double r_y;
171 double g_x;
172 double g_y;
173 double b_x;
174 double b_y;
175 double white_x;
176 double white_y;
177 double max_luminance;
178 CountedElement min_luminance;
179 } MatroskaMasteringMeta;
180
181 typedef struct MatroskaTrackVideoColor {
182 uint64_t matrix_coefficients;
183 uint64_t bits_per_channel;
184 uint64_t chroma_sub_horz;
185 uint64_t chroma_sub_vert;
186 uint64_t cb_sub_horz;
187 uint64_t cb_sub_vert;
188 uint64_t chroma_siting_horz;
189 uint64_t chroma_siting_vert;
190 uint64_t range;
191 uint64_t transfer_characteristics;
192 uint64_t primaries;
193 uint64_t max_cll;
194 uint64_t max_fall;
195 MatroskaMasteringMeta mastering_meta;
196 } MatroskaTrackVideoColor;
197
198 typedef struct MatroskaTrackVideoProjection {
199 uint64_t type;
200 EbmlBin private;
201 double yaw;
202 double pitch;
203 double roll;
204 } MatroskaTrackVideoProjection;
205
206 typedef struct MatroskaTrackVideo {
207 double frame_rate;
208 uint64_t display_width;
209 uint64_t display_height;
210 uint64_t pixel_width;
211 uint64_t pixel_height;
212 EbmlBin color_space;
213 uint64_t display_unit;
214 uint64_t interlaced;
215 uint64_t field_order;
216 uint64_t stereo_mode;
217 uint64_t alpha_mode;
218 EbmlList color;
219 MatroskaTrackVideoProjection projection;
220 } MatroskaTrackVideo;
221
222 typedef struct MatroskaTrackAudio {
223 double samplerate;
224 double out_samplerate;
225 uint64_t bitdepth;
226 uint64_t channels;
227
228 /* real audio header (extracted from extradata) */
229 int coded_framesize;
230 int sub_packet_h;
231 int frame_size;
232 int sub_packet_size;
233 int sub_packet_cnt;
234 int pkt_cnt;
235 uint64_t buf_timecode;
236 uint8_t *buf;
237 } MatroskaTrackAudio;
238
239 typedef struct MatroskaTrackPlane {
240 uint64_t uid;
241 uint64_t type;
242 } MatroskaTrackPlane;
243
244 typedef struct MatroskaTrackOperation {
245 EbmlList combine_planes;
246 } MatroskaTrackOperation;
247
248 typedef struct MatroskaBlockAdditionMapping {
249 uint64_t value;
250 char *name;
251 uint64_t type;
252 EbmlBin extradata;
253 } MatroskaBlockAdditionMapping;
254
255 typedef struct MatroskaTrack {
256 uint64_t num;
257 uint64_t uid;
258 uint64_t type;
259 char *name;
260 char *codec_id;
261 EbmlBin codec_priv;
262 char *language;
263 double time_scale;
264 uint64_t default_duration;
265 uint64_t flag_default;
266 uint64_t flag_forced;
267 uint64_t flag_comment;
268 uint64_t flag_hearingimpaired;
269 uint64_t flag_visualimpaired;
270 uint64_t flag_textdescriptions;
271 CountedElement flag_original;
272 uint64_t seek_preroll;
273 MatroskaTrackVideo video;
274 MatroskaTrackAudio audio;
275 MatroskaTrackOperation operation;
276 EbmlList encodings;
277 uint64_t codec_delay;
278 uint64_t codec_delay_in_track_tb;
279
280 AVStream *stream;
281 int64_t end_timecode;
282 int ms_compat;
283 int needs_decoding;
284 uint64_t max_block_additional_id;
285 EbmlList block_addition_mappings;
286
287 uint32_t palette[AVPALETTE_COUNT];
288 int has_palette;
289 } MatroskaTrack;
290
291 typedef struct MatroskaAttachment {
292 uint64_t uid;
293 char *filename;
294 char *description;
295 char *mime;
296 EbmlBin bin;
297
298 AVStream *stream;
299 } MatroskaAttachment;
300
301 typedef struct MatroskaChapter {
302 uint64_t start;
303 uint64_t end;
304 uint64_t uid;
305 char *title;
306
307 AVChapter *chapter;
308 } MatroskaChapter;
309
310 typedef struct MatroskaIndexPos {
311 uint64_t track;
312 uint64_t pos;
313 } MatroskaIndexPos;
314
315 typedef struct MatroskaIndex {
316 uint64_t time;
317 EbmlList pos;
318 } MatroskaIndex;
319
320 typedef struct MatroskaTag {
321 char *name;
322 char *string;
323 char *lang;
324 uint64_t def;
325 EbmlList sub;
326 } MatroskaTag;
327
328 typedef struct MatroskaTagTarget {
329 char *type;
330 uint64_t typevalue;
331 uint64_t trackuid;
332 uint64_t chapteruid;
333 uint64_t attachuid;
334 } MatroskaTagTarget;
335
336 typedef struct MatroskaTags {
337 MatroskaTagTarget target;
338 EbmlList tag;
339 } MatroskaTags;
340
341 typedef struct MatroskaSeekhead {
342 uint64_t id;
343 uint64_t pos;
344 } MatroskaSeekhead;
345
346 typedef struct MatroskaLevel {
347 uint64_t start;
348 uint64_t length;
349 } MatroskaLevel;
350
351 typedef struct MatroskaBlock {
352 uint64_t duration;
353 CountedElement reference;
354 uint64_t non_simple;
355 EbmlBin bin;
356 uint64_t additional_id;
357 EbmlBin additional;
358 int64_t discard_padding;
359 } MatroskaBlock;
360
361 typedef struct MatroskaCluster {
362 MatroskaBlock block;
363 uint64_t timecode;
364 int64_t pos;
365 } MatroskaCluster;
366
367 typedef struct MatroskaLevel1Element {
368 int64_t pos;
369 uint32_t id;
370 int parsed;
371 } MatroskaLevel1Element;
372
373 typedef struct MatroskaDemuxContext {
374 const AVClass *class;
375 AVFormatContext *ctx;
376
377 /* EBML stuff */
378 MatroskaLevel levels[EBML_MAX_DEPTH];
379 int num_levels;
380 uint32_t current_id;
381 int64_t resync_pos;
382 int unknown_count;
383
384 uint64_t time_scale;
385 double duration;
386 char *title;
387 char *muxingapp;
388 EbmlBin date_utc;
389 EbmlList tracks;
390 EbmlList attachments;
391 EbmlList chapters;
392 EbmlList index;
393 EbmlList tags;
394 EbmlList seekhead;
395
396 /* byte position of the segment inside the stream */
397 int64_t segment_start;
398
399 /* This packet coincides with FFFormatContext.parse_pkt
400 * and is not owned by us. */
401 AVPacket *pkt;
402
403 /* the packet queue */
404 PacketList queue;
405
406 int done;
407
408 /* What to skip before effectively reading a packet. */
409 int skip_to_keyframe;
410 uint64_t skip_to_timecode;
411
412 /* File has a CUES element, but we defer parsing until it is needed. */
413 int cues_parsing_deferred;
414
415 /* Level1 elements and whether they were read yet */
416 MatroskaLevel1Element level1_elems[64];
417 int num_level1_elems;
418
419 MatroskaCluster current_cluster;
420
421 /* WebM DASH Manifest live flag */
422 int is_live;
423
424 /* Bandwidth value for WebM DASH Manifest */
425 int bandwidth;
426 } MatroskaDemuxContext;
427
428 #define CHILD_OF(parent) { .def = { .n = parent } }
429
430 // The following forward declarations need their size because
431 // a tentative definition with internal linkage must not be an
432 // incomplete type (6.7.2 in C90, 6.9.2 in C99).
433 // Removing the sizes breaks MSVC.
434 static EbmlSyntax ebml_syntax[3], matroska_segment[9], matroska_track_video_color[15], matroska_track_video[19],
435 matroska_track[33], matroska_track_encoding[6], matroska_track_encodings[2],
436 matroska_track_combine_planes[2], matroska_track_operation[2], matroska_block_addition_mapping[5], matroska_tracks[2],
437 matroska_attachments[2], matroska_chapter_entry[9], matroska_chapter[6], matroska_chapters[2],
438 matroska_index_entry[3], matroska_index[2], matroska_tag[3], matroska_tags[2], matroska_seekhead[2],
439 matroska_blockadditions[2], matroska_blockgroup[8], matroska_cluster_parsing[8];
440
441 static EbmlSyntax ebml_header[] = {
442 { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, 0, offsetof(Ebml, version), { .u = EBML_VERSION } },
443 { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, 0, offsetof(Ebml, max_size), { .u = 8 } },
444 { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, 0, offsetof(Ebml, id_length), { .u = 4 } },
445 { EBML_ID_DOCTYPE, EBML_STR, 0, 0, offsetof(Ebml, doctype), { .s = "(none)" } },
446 { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, 0, offsetof(Ebml, doctype_version), { .u = 1 } },
447 { EBML_ID_EBMLVERSION, EBML_NONE },
448 { EBML_ID_DOCTYPEVERSION, EBML_NONE },
449 CHILD_OF(ebml_syntax)
450 };
451
452 static EbmlSyntax ebml_syntax[] = {
453 { EBML_ID_HEADER, EBML_NEST, 0, 0, 0, { .n = ebml_header } },
454 { MATROSKA_ID_SEGMENT, EBML_STOP },
455 { 0 }
456 };
457
458 static EbmlSyntax matroska_info[] = {
459 { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, 0, offsetof(MatroskaDemuxContext, time_scale), { .u = 1000000 } },
460 { MATROSKA_ID_DURATION, EBML_FLOAT, 0, 0, offsetof(MatroskaDemuxContext, duration) },
461 { MATROSKA_ID_TITLE, EBML_UTF8, 0, 0, offsetof(MatroskaDemuxContext, title) },
462 { MATROSKA_ID_WRITINGAPP, EBML_NONE },
463 { MATROSKA_ID_MUXINGAPP, EBML_UTF8, 0, 0, offsetof(MatroskaDemuxContext, muxingapp) },
464 { MATROSKA_ID_DATEUTC, EBML_BIN, 0, 0, offsetof(MatroskaDemuxContext, date_utc) },
465 { MATROSKA_ID_SEGMENTUID, EBML_NONE },
466 CHILD_OF(matroska_segment)
467 };
468
469 static EbmlSyntax matroska_mastering_meta[] = {
470 { MATROSKA_ID_VIDEOCOLOR_RX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, r_x) },
471 { MATROSKA_ID_VIDEOCOLOR_RY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, r_y) },
472 { MATROSKA_ID_VIDEOCOLOR_GX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, g_x) },
473 { MATROSKA_ID_VIDEOCOLOR_GY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, g_y) },
474 { MATROSKA_ID_VIDEOCOLOR_BX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, b_x) },
475 { MATROSKA_ID_VIDEOCOLOR_BY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, b_y) },
476 { MATROSKA_ID_VIDEOCOLOR_WHITEX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, white_x) },
477 { MATROSKA_ID_VIDEOCOLOR_WHITEY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, white_y) },
478 { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN, EBML_FLOAT, 1, 0, offsetof(MatroskaMasteringMeta, min_luminance) },
479 { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, max_luminance) },
480 CHILD_OF(matroska_track_video_color)
481 };
482
483 static EbmlSyntax matroska_track_video_color[] = {
484 { MATROSKA_ID_VIDEOCOLORMATRIXCOEFF, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, matrix_coefficients), { .u = AVCOL_SPC_UNSPECIFIED } },
485 { MATROSKA_ID_VIDEOCOLORBITSPERCHANNEL, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, bits_per_channel), { .u = 0 } },
486 { MATROSKA_ID_VIDEOCOLORCHROMASUBHORZ, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_horz) },
487 { MATROSKA_ID_VIDEOCOLORCHROMASUBVERT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_vert) },
488 { MATROSKA_ID_VIDEOCOLORCBSUBHORZ, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, cb_sub_horz) },
489 { MATROSKA_ID_VIDEOCOLORCBSUBVERT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, cb_sub_vert) },
490 { MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_horz), { .u = MATROSKA_COLOUR_CHROMASITINGHORZ_UNDETERMINED } },
491 { MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_vert), { .u = MATROSKA_COLOUR_CHROMASITINGVERT_UNDETERMINED } },
492 { MATROSKA_ID_VIDEOCOLORRANGE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, range), { .u = AVCOL_RANGE_UNSPECIFIED } },
493 { MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, transfer_characteristics), { .u = AVCOL_TRC_UNSPECIFIED } },
494 { MATROSKA_ID_VIDEOCOLORPRIMARIES, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, primaries), { .u = AVCOL_PRI_UNSPECIFIED } },
495 { MATROSKA_ID_VIDEOCOLORMAXCLL, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, max_cll) },
496 { MATROSKA_ID_VIDEOCOLORMAXFALL, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, max_fall) },
497 { MATROSKA_ID_VIDEOCOLORMASTERINGMETA, EBML_NEST, 0, 0, offsetof(MatroskaTrackVideoColor, mastering_meta), { .n = matroska_mastering_meta } },
498 CHILD_OF(matroska_track_video)
499 };
500
501 static EbmlSyntax matroska_track_video_projection[] = {
502 { MATROSKA_ID_VIDEOPROJECTIONTYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoProjection, type), { .u = MATROSKA_VIDEO_PROJECTION_TYPE_RECTANGULAR } },
503 { MATROSKA_ID_VIDEOPROJECTIONPRIVATE, EBML_BIN, 0, 0, offsetof(MatroskaTrackVideoProjection, private) },
504 { MATROSKA_ID_VIDEOPROJECTIONPOSEYAW, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, yaw), { .f = 0.0 } },
505 { MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, pitch), { .f = 0.0 } },
506 { MATROSKA_ID_VIDEOPROJECTIONPOSEROLL, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, roll), { .f = 0.0 } },
507 CHILD_OF(matroska_track_video)
508 };
509
510 static EbmlSyntax matroska_track_video[] = {
511 { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideo, frame_rate) },
512 { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, display_width), { .u=-1 } },
513 { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, display_height), { .u=-1 } },
514 { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, pixel_width) },
515 { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, pixel_height) },
516 { MATROSKA_ID_VIDEOCOLORSPACE, EBML_BIN, 0, 0, offsetof(MatroskaTrackVideo, color_space) },
517 { MATROSKA_ID_VIDEOALPHAMODE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, alpha_mode), { .u = 0 } },
518 { MATROSKA_ID_VIDEOCOLOR, EBML_NEST, 0, sizeof(MatroskaTrackVideoColor), offsetof(MatroskaTrackVideo, color), { .n = matroska_track_video_color } },
519 { MATROSKA_ID_VIDEOPROJECTION, EBML_NEST, 0, 0, offsetof(MatroskaTrackVideo, projection), { .n = matroska_track_video_projection } },
520 { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE },
521 { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE },
522 { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE },
523 { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE },
524 { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, display_unit), { .u= MATROSKA_VIDEO_DISPLAYUNIT_PIXELS } },
525 { MATROSKA_ID_VIDEOFLAGINTERLACED, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, interlaced), { .u = MATROSKA_VIDEO_INTERLACE_FLAG_UNDETERMINED } },
526 { MATROSKA_ID_VIDEOFIELDORDER, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, field_order), { .u = MATROSKA_VIDEO_FIELDORDER_UNDETERMINED } },
527 { MATROSKA_ID_VIDEOSTEREOMODE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, stereo_mode), { .u = MATROSKA_VIDEO_STEREOMODE_TYPE_NB } },
528 { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE },
529 CHILD_OF(matroska_track)
530 };
531
532 static EbmlSyntax matroska_track_audio[] = {
533 { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackAudio, samplerate), { .f = 8000.0 } },
534 { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackAudio, out_samplerate) },
535 { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, 0, offsetof(MatroskaTrackAudio, bitdepth) },
536 { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, 0, offsetof(MatroskaTrackAudio, channels), { .u = 1 } },
537 CHILD_OF(matroska_track)
538 };
539
540 static EbmlSyntax matroska_track_encoding_compression[] = {
541 { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, 0, offsetof(MatroskaTrackCompression, algo), { .u = MATROSKA_TRACK_ENCODING_COMP_ZLIB } },
542 { MATROSKA_ID_ENCODINGCOMPSETTINGS, EBML_BIN, 0, 0, offsetof(MatroskaTrackCompression, settings) },
543 CHILD_OF(matroska_track_encoding)
544 };
545
546 static EbmlSyntax matroska_track_encoding_encryption[] = {
547 { MATROSKA_ID_ENCODINGENCALGO, EBML_UINT, 0, 0, offsetof(MatroskaTrackEncryption,algo), {.u = 0} },
548 { MATROSKA_ID_ENCODINGENCKEYID, EBML_BIN, 0, 0, offsetof(MatroskaTrackEncryption,key_id) },
549 { MATROSKA_ID_ENCODINGENCAESSETTINGS, EBML_NONE },
550 { MATROSKA_ID_ENCODINGSIGALGO, EBML_NONE },
551 { MATROSKA_ID_ENCODINGSIGHASHALGO, EBML_NONE },
552 { MATROSKA_ID_ENCODINGSIGKEYID, EBML_NONE },
553 { MATROSKA_ID_ENCODINGSIGNATURE, EBML_NONE },
554 CHILD_OF(matroska_track_encoding)
555 };
556 static EbmlSyntax matroska_track_encoding[] = {
557 { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackEncoding, scope), { .u = 1 } },
558 { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackEncoding, type), { .u = 0 } },
559 { MATROSKA_ID_ENCODINGCOMPRESSION, EBML_NEST, 0, 0, offsetof(MatroskaTrackEncoding, compression), { .n = matroska_track_encoding_compression } },
560 { MATROSKA_ID_ENCODINGENCRYPTION, EBML_NEST, 0, 0, offsetof(MatroskaTrackEncoding, encryption), { .n = matroska_track_encoding_encryption } },
561 { MATROSKA_ID_ENCODINGORDER, EBML_NONE },
562 CHILD_OF(matroska_track_encodings)
563 };
564
565 static EbmlSyntax matroska_track_encodings[] = {
566 { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, 0, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack, encodings), { .n = matroska_track_encoding } },
567 CHILD_OF(matroska_track)
568 };
569
570 static EbmlSyntax matroska_track_plane[] = {
571 { MATROSKA_ID_TRACKPLANEUID, EBML_UINT, 0, 0, offsetof(MatroskaTrackPlane,uid) },
572 { MATROSKA_ID_TRACKPLANETYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackPlane,type) },
573 CHILD_OF(matroska_track_combine_planes)
574 };
575
576 static EbmlSyntax matroska_track_combine_planes[] = {
577 { MATROSKA_ID_TRACKPLANE, EBML_NEST, 0, sizeof(MatroskaTrackPlane), offsetof(MatroskaTrackOperation,combine_planes), {.n = matroska_track_plane} },
578 CHILD_OF(matroska_track_operation)
579 };
580
581 static EbmlSyntax matroska_track_operation[] = {
582 { MATROSKA_ID_TRACKCOMBINEPLANES, EBML_NEST, 0, 0, 0, {.n = matroska_track_combine_planes} },
583 CHILD_OF(matroska_track)
584 };
585
586 static EbmlSyntax matroska_block_addition_mapping[] = {
587 { MATROSKA_ID_BLKADDIDVALUE, EBML_UINT, 0, 0, offsetof(MatroskaBlockAdditionMapping, value) },
588 { MATROSKA_ID_BLKADDIDNAME, EBML_STR, 0, 0, offsetof(MatroskaBlockAdditionMapping, name) },
589 { MATROSKA_ID_BLKADDIDTYPE, EBML_UINT, 0, 0, offsetof(MatroskaBlockAdditionMapping, type) },
590 { MATROSKA_ID_BLKADDIDEXTRADATA, EBML_BIN, 0, 0, offsetof(MatroskaBlockAdditionMapping, extradata) },
591 CHILD_OF(matroska_track)
592 };
593
594 static EbmlSyntax matroska_track[] = {
595 { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, 0, offsetof(MatroskaTrack, num) },
596 { MATROSKA_ID_TRACKNAME, EBML_UTF8, 0, 0, offsetof(MatroskaTrack, name) },
597 { MATROSKA_ID_TRACKUID, EBML_UINT, 0, 0, offsetof(MatroskaTrack, uid) },
598 { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrack, type) },
599 { MATROSKA_ID_CODECID, EBML_STR, 0, 0, offsetof(MatroskaTrack, codec_id) },
600 { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, 0, offsetof(MatroskaTrack, codec_priv) },
601 { MATROSKA_ID_CODECDELAY, EBML_UINT, 0, 0, offsetof(MatroskaTrack, codec_delay), { .u = 0 } },
602 { MATROSKA_ID_TRACKLANGUAGE, EBML_STR, 0, 0, offsetof(MatroskaTrack, language), { .s = "eng" } },
603 { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, 0, offsetof(MatroskaTrack, default_duration) },
604 { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT, 0, 0, offsetof(MatroskaTrack, time_scale), { .f = 1.0 } },
605 { MATROSKA_ID_TRACKFLAGCOMMENTARY, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_comment), { .u = 0 } },
606 { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_default), { .u = 1 } },
607 { MATROSKA_ID_TRACKFLAGFORCED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_forced), { .u = 0 } },
608 { MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_hearingimpaired), { .u = 0 } },
609 { MATROSKA_ID_TRACKFLAGVISUALIMPAIRED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_visualimpaired), { .u = 0 } },
610 { MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_textdescriptions), { .u = 0 } },
611 { MATROSKA_ID_TRACKFLAGORIGINAL, EBML_UINT, 1, 0, offsetof(MatroskaTrack, flag_original), {.u = 0 } },
612 { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, 0, offsetof(MatroskaTrack, video), { .n = matroska_track_video } },
613 { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, 0, offsetof(MatroskaTrack, audio), { .n = matroska_track_audio } },
614 { MATROSKA_ID_TRACKOPERATION, EBML_NEST, 0, 0, offsetof(MatroskaTrack, operation), { .n = matroska_track_operation } },
615 { MATROSKA_ID_TRACKCONTENTENCODINGS, EBML_NEST, 0, 0, 0, { .n = matroska_track_encodings } },
616 { MATROSKA_ID_TRACKMAXBLKADDID, EBML_UINT, 0, 0, offsetof(MatroskaTrack, max_block_additional_id), { .u = 0 } },
617 { MATROSKA_ID_TRACKBLKADDMAPPING, EBML_NEST, 0, sizeof(MatroskaBlockAdditionMapping), offsetof(MatroskaTrack, block_addition_mappings), { .n = matroska_block_addition_mapping } },
618 { MATROSKA_ID_SEEKPREROLL, EBML_UINT, 0, 0, offsetof(MatroskaTrack, seek_preroll), { .u = 0 } },
619 { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE },
620 { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE },
621 { MATROSKA_ID_CODECNAME, EBML_NONE },
622 { MATROSKA_ID_CODECDECODEALL, EBML_NONE },
623 { MATROSKA_ID_CODECINFOURL, EBML_NONE },
624 { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE },
625 { MATROSKA_ID_TRACKMINCACHE, EBML_NONE },
626 { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE },
627 CHILD_OF(matroska_tracks)
628 };
629
630 static EbmlSyntax matroska_tracks[] = {
631 { MATROSKA_ID_TRACKENTRY, EBML_NEST, 0, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext, tracks), { .n = matroska_track } },
632 CHILD_OF(matroska_segment)
633 };
634
635 static EbmlSyntax matroska_attachment[] = {
636 { MATROSKA_ID_FILEUID, EBML_UINT, 0, 0, offsetof(MatroskaAttachment, uid) },
637 { MATROSKA_ID_FILENAME, EBML_UTF8, 0, 0, offsetof(MatroskaAttachment, filename) },
638 { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, 0, offsetof(MatroskaAttachment, mime) },
639 { MATROSKA_ID_FILEDATA, EBML_BIN, 0, 0, offsetof(MatroskaAttachment, bin) },
640 { MATROSKA_ID_FILEDESC, EBML_UTF8, 0, 0, offsetof(MatroskaAttachment, description) },
641 CHILD_OF(matroska_attachments)
642 };
643
644 static EbmlSyntax matroska_attachments[] = {
645 { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, 0, sizeof(MatroskaAttachment), offsetof(MatroskaDemuxContext, attachments), { .n = matroska_attachment } },
646 CHILD_OF(matroska_segment)
647 };
648
649 static EbmlSyntax matroska_chapter_display[] = {
650 { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, 0, offsetof(MatroskaChapter, title) },
651 { MATROSKA_ID_CHAPLANG, EBML_NONE },
652 { MATROSKA_ID_CHAPCOUNTRY, EBML_NONE },
653 CHILD_OF(matroska_chapter_entry)
654 };
655
656 static EbmlSyntax matroska_chapter_entry[] = {
657 { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, 0, offsetof(MatroskaChapter, start), { .u = AV_NOPTS_VALUE } },
658 { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, 0, offsetof(MatroskaChapter, end), { .u = AV_NOPTS_VALUE } },
659 { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, 0, offsetof(MatroskaChapter, uid) },
660 { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, 0, { .n = matroska_chapter_display } },
661 { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE },
662 { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
663 { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE },
664 { MATROSKA_ID_CHAPTERATOM, EBML_NONE },
665 CHILD_OF(matroska_chapter)
666 };
667
668 static EbmlSyntax matroska_chapter[] = {
669 { MATROSKA_ID_CHAPTERATOM, EBML_NEST, 0, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext, chapters), { .n = matroska_chapter_entry } },
670 { MATROSKA_ID_EDITIONUID, EBML_NONE },
671 { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE },
672 { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
673 { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
674 CHILD_OF(matroska_chapters)
675 };
676
677 static EbmlSyntax matroska_chapters[] = {
678 { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, 0, { .n = matroska_chapter } },
679 CHILD_OF(matroska_segment)
680 };
681
682 static EbmlSyntax matroska_index_pos[] = {
683 { MATROSKA_ID_CUETRACK, EBML_UINT, 0, 0, offsetof(MatroskaIndexPos, track) },
684 { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, 0, offsetof(MatroskaIndexPos, pos) },
685 { MATROSKA_ID_CUERELATIVEPOSITION,EBML_NONE },
686 { MATROSKA_ID_CUEDURATION, EBML_NONE },
687 { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE },
688 CHILD_OF(matroska_index_entry)
689 };
690
691 static EbmlSyntax matroska_index_entry[] = {
692 { MATROSKA_ID_CUETIME, EBML_UINT, 0, 0, offsetof(MatroskaIndex, time) },
693 { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, 0, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex, pos), { .n = matroska_index_pos } },
694 CHILD_OF(matroska_index)
695 };
696
697 static EbmlSyntax matroska_index[] = {
698 { MATROSKA_ID_POINTENTRY, EBML_NEST, 0, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext, index), { .n = matroska_index_entry } },
699 CHILD_OF(matroska_segment)
700 };
701
702 static EbmlSyntax matroska_simpletag[] = {
703 { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, 0, offsetof(MatroskaTag, name) },
704 { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, 0, offsetof(MatroskaTag, string) },
705 { MATROSKA_ID_TAGLANG, EBML_STR, 0, 0, offsetof(MatroskaTag, lang), { .s = "und" } },
706 { MATROSKA_ID_TAGDEFAULT, EBML_UINT, 0, 0, offsetof(MatroskaTag, def) },
707 { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0, 0, offsetof(MatroskaTag, def) },
708 { MATROSKA_ID_SIMPLETAG, EBML_NEST, 0, sizeof(MatroskaTag), offsetof(MatroskaTag, sub), { .n = matroska_simpletag } },
709 CHILD_OF(matroska_tag)
710 };
711
712 static EbmlSyntax matroska_tagtargets[] = {
713 { MATROSKA_ID_TAGTARGETS_TYPE, EBML_STR, 0, 0, offsetof(MatroskaTagTarget, type) },
714 { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, typevalue), { .u = 50 } },
715 { MATROSKA_ID_TAGTARGETS_TRACKUID, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, trackuid), { .u = 0 } },
716 { MATROSKA_ID_TAGTARGETS_CHAPTERUID, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, chapteruid), { .u = 0 } },
717 { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, attachuid), { .u = 0 } },
718 CHILD_OF(matroska_tag)
719 };
720
721 static EbmlSyntax matroska_tag[] = {
722 { MATROSKA_ID_SIMPLETAG, EBML_NEST, 0, sizeof(MatroskaTag), offsetof(MatroskaTags, tag), { .n = matroska_simpletag } },
723 { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, 0, offsetof(MatroskaTags, target), { .n = matroska_tagtargets } },
724 CHILD_OF(matroska_tags)
725 };
726
727 static EbmlSyntax matroska_tags[] = {
728 { MATROSKA_ID_TAG, EBML_NEST, 0, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext, tags), { .n = matroska_tag } },
729 CHILD_OF(matroska_segment)
730 };
731
732 static EbmlSyntax matroska_seekhead_entry[] = {
733 { MATROSKA_ID_SEEKID, EBML_UINT, 0, 0, offsetof(MatroskaSeekhead, id) },
734 { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, 0, offsetof(MatroskaSeekhead, pos), { .u = -1 } },
735 CHILD_OF(matroska_seekhead)
736 };
737
738 static EbmlSyntax matroska_seekhead[] = {
739 { MATROSKA_ID_SEEKENTRY, EBML_NEST, 0, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext, seekhead), { .n = matroska_seekhead_entry } },
740 CHILD_OF(matroska_segment)
741 };
742
743 static EbmlSyntax matroska_segment[] = {
744 { MATROSKA_ID_CLUSTER, EBML_STOP },
745 { MATROSKA_ID_INFO, EBML_LEVEL1, 0, 0, 0, { .n = matroska_info } },
746 { MATROSKA_ID_TRACKS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_tracks } },
747 { MATROSKA_ID_ATTACHMENTS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_attachments } },
748 { MATROSKA_ID_CHAPTERS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_chapters } },
749 { MATROSKA_ID_CUES, EBML_LEVEL1, 0, 0, 0, { .n = matroska_index } },
750 { MATROSKA_ID_TAGS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_tags } },
751 { MATROSKA_ID_SEEKHEAD, EBML_LEVEL1, 0, 0, 0, { .n = matroska_seekhead } },
752 { 0 } /* We don't want to go back to level 0, so don't add the parent. */
753 };
754
755 static EbmlSyntax matroska_segments[] = {
756 { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, 0, { .n = matroska_segment } },
757 { 0 }
758 };
759
760 static EbmlSyntax matroska_blockmore[] = {
761 { MATROSKA_ID_BLOCKADDID, EBML_UINT, 0, 0, offsetof(MatroskaBlock,additional_id), { .u = 1 } },
762 { MATROSKA_ID_BLOCKADDITIONAL, EBML_BIN, 0, 0, offsetof(MatroskaBlock,additional) },
763 CHILD_OF(matroska_blockadditions)
764 };
765
766 static EbmlSyntax matroska_blockadditions[] = {
767 { MATROSKA_ID_BLOCKMORE, EBML_NEST, 0, 0, 0, {.n = matroska_blockmore} },
768 CHILD_OF(matroska_blockgroup)
769 };
770
771 static EbmlSyntax matroska_blockgroup[] = {
772 { MATROSKA_ID_BLOCK, EBML_BIN, 0, 0, offsetof(MatroskaBlock, bin) },
773 { MATROSKA_ID_BLOCKADDITIONS, EBML_NEST, 0, 0, 0, { .n = matroska_blockadditions} },
774 { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, 0, offsetof(MatroskaBlock, duration) },
775 { MATROSKA_ID_DISCARDPADDING, EBML_SINT, 0, 0, offsetof(MatroskaBlock, discard_padding) },
776 { MATROSKA_ID_BLOCKREFERENCE, EBML_SINT, 1, 0, offsetof(MatroskaBlock, reference) },
777 { MATROSKA_ID_CODECSTATE, EBML_NONE },
778 { 1, EBML_UINT, 0, 0, offsetof(MatroskaBlock, non_simple), { .u = 1 } },
779 CHILD_OF(matroska_cluster_parsing)
780 };
781
782 // The following array contains SimpleBlock and BlockGroup twice
783 // in order to reuse the other values for matroska_cluster_enter.
784 static EbmlSyntax matroska_cluster_parsing[] = {
785 { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, 0, offsetof(MatroskaBlock, bin) },
786 { MATROSKA_ID_BLOCKGROUP, EBML_NEST, 0, 0, 0, { .n = matroska_blockgroup } },
787 { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, 0, offsetof(MatroskaCluster, timecode) },
788 { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP },
789 { MATROSKA_ID_BLOCKGROUP, EBML_STOP },
790 { MATROSKA_ID_CLUSTERPOSITION, EBML_NONE },
791 { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
792 CHILD_OF(matroska_segment)
793 };
794
795 static EbmlSyntax matroska_cluster_enter[] = {
796 { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, 0, { .n = &matroska_cluster_parsing[2] } },
797 { 0 }
798 };
799 #undef CHILD_OF
800
801 static const CodecMime mkv_image_mime_tags[] = {
802 {"image/gif" , AV_CODEC_ID_GIF},
803 {"image/jpeg" , AV_CODEC_ID_MJPEG},
804 {"image/png" , AV_CODEC_ID_PNG},
805 {"image/tiff" , AV_CODEC_ID_TIFF},
806
807 {"" , AV_CODEC_ID_NONE}
808 };
809
810 static const CodecMime mkv_mime_tags[] = {
811 {"application/x-truetype-font", AV_CODEC_ID_TTF},
812 {"application/x-font" , AV_CODEC_ID_TTF},
813 {"application/vnd.ms-opentype", AV_CODEC_ID_OTF},
814 {"binary" , AV_CODEC_ID_BIN_DATA},
815
816 {"" , AV_CODEC_ID_NONE}
817 };
818
819 static const char *const matroska_doctypes[] = { "matroska", "webm" };
820
821 /*
822 * This function prepares the status for parsing of level 1 elements.
823 */
824 188 static int matroska_reset_status(MatroskaDemuxContext *matroska,
825 uint32_t id, int64_t position)
826 {
827 188 int64_t err = 0;
828
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 13 times.
188 if (position >= 0) {
829 175 err = avio_seek(matroska->ctx->pb, position, SEEK_SET);
830
1/2
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
175 if (err > 0)
831 175 err = 0;
832 } else
833 13 position = avio_tell(matroska->ctx->pb);
834
835 188 matroska->current_id = id;
836 188 matroska->num_levels = 1;
837 188 matroska->unknown_count = 0;
838 188 matroska->resync_pos = position;
839
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 159 times.
188 if (id)
840 29 matroska->resync_pos -= (av_log2(id) + 7) / 8;
841
842 188 return err;
843 }
844
845 9 static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos)
846 {
847 9 AVIOContext *pb = matroska->ctx->pb;
848 uint32_t id;
849
850 /* Try to seek to the last position to resync from. If this doesn't work,
851 * we resync from the earliest position available: The start of the buffer. */
852
3/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 7 times.
9 if (last_pos < avio_tell(pb) && avio_seek(pb, last_pos + 1, SEEK_SET) < 0) {
853 2 av_log(matroska->ctx, AV_LOG_WARNING,
854 "Seek to desired resync point failed. Seeking to "
855 "earliest point available instead.\n");
856
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 avio_seek(pb, FFMAX(avio_tell(pb) + (pb->buffer - pb->buf_ptr),
857 last_pos + 1), SEEK_SET);
858 }
859
860 9 id = avio_rb32(pb);
861
862 // try to find a toplevel element
863
2/2
✓ Branch 1 taken 657905 times.
✓ Branch 2 taken 6 times.
657911 while (!avio_feof(pb)) {
864
3/6
✓ Branch 0 taken 657905 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 657905 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 657905 times.
✗ Branch 5 not taken.
657905 if (id == MATROSKA_ID_INFO || id == MATROSKA_ID_TRACKS ||
865
3/4
✓ Branch 0 taken 657905 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 657903 times.
✓ Branch 3 taken 2 times.
657905 id == MATROSKA_ID_CUES || id == MATROSKA_ID_TAGS ||
866
3/4
✓ Branch 0 taken 657903 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 657902 times.
✓ Branch 3 taken 1 times.
657903 id == MATROSKA_ID_SEEKHEAD || id == MATROSKA_ID_ATTACHMENTS ||
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657902 times.
657902 id == MATROSKA_ID_CLUSTER || id == MATROSKA_ID_CHAPTERS) {
868 /* Prepare the context for parsing of a level 1 element. */
869 3 matroska_reset_status(matroska, id, -1);
870 /* Given that we are here means that an error has occurred,
871 * so treat the segment as unknown length in order not to
872 * discard valid data that happens to be beyond the designated
873 * end of the segment. */
874 3 matroska->levels[0].length = EBML_UNKNOWN_LENGTH;
875 3 return 0;
876 }
877 657902 id = (id << 8) | avio_r8(pb);
878 }
879
880 6 matroska->done = 1;
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 return pb->error ? pb->error : AVERROR_EOF;
882 }
883
884 /*
885 * Read: an "EBML number", which is defined as a variable-length
886 * array of bytes. The first byte indicates the length by giving a
887 * number of 0-bits followed by a one. The position of the first
888 * "one" bit inside the first byte indicates the length of this
889 * number.
890 * Returns: number of bytes read, < 0 on error
891 */
892 189787 static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb,
893 int max_size, uint64_t *number, int eof_forbidden)
894 {
895 189787 int read, n = 1;
896 uint64_t total;
897 int64_t pos;
898
899 /* The first byte tells us the length in bytes - except when it is zero. */
900 189787 total = avio_r8(pb);
901
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 189774 times.
189787 if (pb->eof_reached)
902 13 goto err;
903
904 /* get the length of the EBML number */
905 189774 read = 8 - ff_log2_tab[total];
906
907
2/4
✓ Branch 0 taken 189774 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 189774 times.
189774 if (!total || read > max_size) {
908 pos = avio_tell(pb) - 1;
909 if (!total) {
910 av_log(matroska->ctx, AV_LOG_ERROR,
911 "0x00 at pos %"PRId64" (0x%"PRIx64") invalid as first byte "
912 "of an EBML number\n", pos, pos);
913 } else {
914 av_log(matroska->ctx, AV_LOG_ERROR,
915 "Length %d indicated by an EBML number's first byte 0x%02x "
916 "at pos %"PRId64" (0x%"PRIx64") exceeds max length %d.\n",
917 read, (uint8_t) total, pos, pos, max_size);
918 }
919 return AVERROR_INVALIDDATA;
920 }
921
922 /* read out length */
923 189774 total ^= 1 << ff_log2_tab[total];
924
2/2
✓ Branch 0 taken 67159 times.
✓ Branch 1 taken 189774 times.
256933 while (n++ < read)
925 67159 total = (total << 8) | avio_r8(pb);
926
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 189774 times.
189774 if (pb->eof_reached) {
928 eof_forbidden = 1;
929 goto err;
930 }
931
932 189774 *number = total;
933
934 189774 return read;
935
936 13 err:
937 13 pos = avio_tell(pb);
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (pb->error) {
939 av_log(matroska->ctx, AV_LOG_ERROR,
940 "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
941 pos, pos);
942 return pb->error;
943 }
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (eof_forbidden) {
945 av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely "
946 "at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
947 return AVERROR(EIO);
948 }
949 13 return AVERROR_EOF;
950 }
951
952 /**
953 * Read a EBML length value.
954 * This needs special handling for the "unknown length" case which has multiple
955 * encodings.
956 */
957 79595 static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
958 uint64_t *number)
959 {
960 79595 int res = ebml_read_num(matroska, pb, 8, number, 1);
961
3/4
✓ Branch 0 taken 79595 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 79578 times.
79595 if (res > 0 && *number + 1 == 1ULL << (7 * res))
962 17 *number = EBML_UNKNOWN_LENGTH;
963 79595 return res;
964 }
965
966 /*
967 * Read the next element as an unsigned int.
968 * Returns NEEDS_CHECKING unless size == 0.
969 */
970 23095 static int ebml_read_uint(AVIOContext *pb, int size,
971 uint64_t default_value, uint64_t *num)
972 {
973 23095 int n = 0;
974
975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23095 times.
23095 if (size == 0) {
976 *num = default_value;
977 return 0;
978 }
979 /* big-endian ordering; build up number */
980 23095 *num = 0;
981
2/2
✓ Branch 0 taken 53049 times.
✓ Branch 1 taken 23095 times.
76144 while (n++ < size)
982 53049 *num = (*num << 8) | avio_r8(pb);
983
984 23095 return NEEDS_CHECKING;
985 }
986
987 /*
988 * Read the next element as a signed int.
989 * Returns NEEDS_CHECKING unless size == 0.
990 */
991 36 static int ebml_read_sint(AVIOContext *pb, int size,
992 int64_t default_value, int64_t *num)
993 {
994 36 int n = 1;
995
996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (size == 0) {
997 *num = default_value;
998 return 0;
999 } else {
1000 36 *num = sign_extend(avio_r8(pb), 8);
1001
1002 /* big-endian ordering; build up number */
1003
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 36 times.
117 while (n++ < size)
1004 81 *num = ((uint64_t)*num << 8) | avio_r8(pb);
1005 }
1006
1007 36 return NEEDS_CHECKING;
1008 }
1009
1010 /*
1011 * Read the next element as a float.
1012 * Returns 0 if size == 0, NEEDS_CHECKING or < 0 on obvious failure.
1013 */
1014 681 static int ebml_read_float(AVIOContext *pb, int size,
1015 double default_value, double *num)
1016 {
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 681 times.
681 if (size == 0) {
1018 *num = default_value;
1019 return 0;
1020
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 636 times.
681 } else if (size == 4) {
1021 45 *num = av_int2float(avio_rb32(pb));
1022
1/2
✓ Branch 0 taken 636 times.
✗ Branch 1 not taken.
636 } else if (size == 8) {
1023 636 *num = av_int2double(avio_rb64(pb));
1024 } else
1025 return AVERROR_INVALIDDATA;
1026
1027 681 return NEEDS_CHECKING;
1028 }
1029
1030 /*
1031 * Read the next element as an ASCII string.
1032 * 0 is success, < 0 or NEEDS_CHECKING is failure.
1033 */
1034 1898 static int ebml_read_ascii(AVIOContext *pb, int size,
1035 const char *default_value, char **str)
1036 {
1037 char *res;
1038 int ret;
1039
1040
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1898 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1898 if (size == 0 && default_value) {
1041 res = av_strdup(default_value);
1042 if (!res)
1043 return AVERROR(ENOMEM);
1044 } else {
1045 /* EBML strings are usually not 0-terminated, so we allocate one
1046 * byte more, read the string and NUL-terminate it ourselves. */
1047
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1898 times.
1898 if (!(res = av_malloc(size + 1)))
1048 return AVERROR(ENOMEM);
1049
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1898 times.
1898 if ((ret = avio_read(pb, (uint8_t *) res, size)) != size) {
1050 av_free(res);
1051 return ret < 0 ? ret : NEEDS_CHECKING;
1052 }
1053 1898 (res)[size] = '\0';
1054 }
1055 1898 av_free(*str);
1056 1898 *str = res;
1057
1058 1898 return 0;
1059 }
1060
1061 /*
1062 * Read the next element as binary data.
1063 * 0 is success, < 0 or NEEDS_CHECKING is failure.
1064 */
1065 30623 static int ebml_read_binary(AVIOContext *pb, int length,
1066 int64_t pos, EbmlBin *bin)
1067 {
1068 int ret;
1069
1070 30623 ret = av_buffer_realloc(&bin->buf, length + AV_INPUT_BUFFER_PADDING_SIZE);
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30623 times.
30623 if (ret < 0)
1072 return ret;
1073 30623 memset(bin->buf->data + length, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1074
1075 30623 bin->data = bin->buf->data;
1076 30623 bin->size = length;
1077 30623 bin->pos = pos;
1078
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 30617 times.
30623 if ((ret = avio_read(pb, bin->data, length)) != length) {
1079 6 av_buffer_unref(&bin->buf);
1080 6 bin->data = NULL;
1081 6 bin->size = 0;
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 return ret < 0 ? ret : NEEDS_CHECKING;
1083 }
1084
1085 30617 return 0;
1086 }
1087
1088 /*
1089 * Read the next element, but only the header. The contents
1090 * are supposed to be sub-elements which can be read separately.
1091 * 0 is success, < 0 is failure.
1092 */
1093 15059 static int ebml_read_master(MatroskaDemuxContext *matroska,
1094 uint64_t length, int64_t pos)
1095 {
1096 MatroskaLevel *level;
1097
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15059 times.
15059 if (matroska->num_levels >= EBML_MAX_DEPTH) {
1099 av_log(matroska->ctx, AV_LOG_ERROR,
1100 "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
1101 return AVERROR(ENOSYS);
1102 }
1103
1104 15059 level = &matroska->levels[matroska->num_levels++];
1105 15059 level->start = pos;
1106 15059 level->length = length;
1107
1108 15059 return 0;
1109 }
1110
1111 /*
1112 * Read a signed "EBML number"
1113 * Return: number of bytes processed, < 0 on error
1114 */
1115 180 static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
1116 AVIOContext *pb, int64_t *num)
1117 {
1118 uint64_t unum;
1119 int res;
1120
1121 /* read as unsigned number first */
1122
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
180 if ((res = ebml_read_num(matroska, pb, 8, &unum, 1)) < 0)
1123 return res;
1124
1125 /* make signed (weird way) */
1126 180 *num = unum - ((1LL << (7 * res - 1)) - 1);
1127
1128 180 return res;
1129 }
1130
1131 static int ebml_parse(MatroskaDemuxContext *matroska,
1132 EbmlSyntax *syntax, void *data);
1133
1134 81411 static EbmlSyntax *ebml_parse_id(EbmlSyntax *syntax, uint32_t id)
1135 {
1136 int i;
1137
1138 // Whoever touches this should be aware of the duplication
1139 // existing in matroska_cluster_parsing.
1140
2/2
✓ Branch 0 taken 153678 times.
✓ Branch 1 taken 573 times.
154251 for (i = 0; syntax[i].id; i++)
1141
2/2
✓ Branch 0 taken 80838 times.
✓ Branch 1 taken 72840 times.
153678 if (id == syntax[i].id)
1142 80838 break;
1143
1144 81411 return &syntax[i];
1145 }
1146
1147 15059 static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
1148 void *data)
1149 {
1150 int res;
1151
1152
1/2
✓ Branch 0 taken 15059 times.
✗ Branch 1 not taken.
15059 if (data) {
1153
2/2
✓ Branch 0 taken 76003 times.
✓ Branch 1 taken 15059 times.
91062 for (int i = 0; syntax[i].id; i++) {
1154 76003 void *dst = (char *)data + syntax[i].data_offset;
1155
5/5
✓ Branch 0 taken 30946 times.
✓ Branch 1 taken 624 times.
✓ Branch 2 taken 1267 times.
✓ Branch 3 taken 3330 times.
✓ Branch 4 taken 39836 times.
76003 switch (syntax[i].type) {
1156 30946 case EBML_UINT:
1157 30946 *(uint64_t *)dst = syntax[i].def.u;
1158 30946 break;
1159 624 case EBML_SINT:
1160 624 *(int64_t *) dst = syntax[i].def.i;
1161 624 break;
1162 1267 case EBML_FLOAT:
1163 1267 *(double *) dst = syntax[i].def.f;
1164 1267 break;
1165 3330 case EBML_STR:
1166 case EBML_UTF8:
1167 // the default may be NULL
1168
2/2
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 2274 times.
3330 if (syntax[i].def.s) {
1169 1056 *(char**)dst = av_strdup(syntax[i].def.s);
1170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1056 times.
1056 if (!*(char**)dst)
1171 return AVERROR(ENOMEM);
1172 }
1173 3330 break;
1174 }
1175 }
1176
1177
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 15016 times.
15059 if (!matroska->levels[matroska->num_levels - 1].length) {
1178 43 matroska->num_levels--;
1179 43 return 0;
1180 }
1181 }
1182
1183 do {
1184 48478 res = ebml_parse(matroska, syntax, data);
1185
2/2
✓ Branch 0 taken 33462 times.
✓ Branch 1 taken 15016 times.
48478 } while (!res);
1186
1187
2/2
✓ Branch 0 taken 1126 times.
✓ Branch 1 taken 13890 times.
15016 return res == LEVEL_ENDED ? 0 : res;
1188 }
1189
1190 2237 static int is_ebml_id_valid(uint32_t id)
1191 {
1192 // Due to endian nonsense in Matroska, the highest byte with any bits set
1193 // will contain the leading length bit. This bit in turn identifies the
1194 // total byte length of the element by its position within the byte.
1195 2237 unsigned int bits = av_log2(id);
1196
2/4
✓ Branch 0 taken 2237 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2237 times.
✗ Branch 3 not taken.
2237 return id && (bits + 7) / 8 == (8 - bits % 8);
1197 }
1198
1199 /*
1200 * Allocate and return the entry for the level1 element with the given ID. If
1201 * an entry already exists, return the existing entry.
1202 */
1203 2237 static MatroskaLevel1Element *matroska_find_level1_elem(MatroskaDemuxContext *matroska,
1204 uint32_t id, int64_t pos)
1205 {
1206 int i;
1207 MatroskaLevel1Element *elem;
1208
1209
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2237 times.
2237 if (!is_ebml_id_valid(id))
1210 return NULL;
1211
1212 // Some files link to all clusters; useless.
1213
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2212 times.
2237 if (id == MATROSKA_ID_CLUSTER)
1214 25 return NULL;
1215
1216 // There can be multiple SeekHeads and Tags.
1217
2/2
✓ Branch 0 taken 4403 times.
✓ Branch 1 taken 1436 times.
5839 for (i = 0; i < matroska->num_level1_elems; i++) {
1218
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 3625 times.
4403 if (matroska->level1_elems[i].id == id) {
1219
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 776 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
778 if (matroska->level1_elems[i].pos == pos ||
1220 id != MATROSKA_ID_SEEKHEAD && id != MATROSKA_ID_TAGS)
1221 776 return &matroska->level1_elems[i];
1222 }
1223 }
1224
1225 // Only a completely broken file would have more elements.
1226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1436 times.
1436 if (matroska->num_level1_elems >= FF_ARRAY_ELEMS(matroska->level1_elems)) {
1227 av_log(matroska->ctx, AV_LOG_ERROR, "Too many level1 elements.\n");
1228 return NULL;
1229 }
1230
1231 1436 elem = &matroska->level1_elems[matroska->num_level1_elems++];
1232 1436 *elem = (MatroskaLevel1Element){.id = id};
1233
1234 1436 return elem;
1235 }
1236
1237 81421 static int ebml_parse(MatroskaDemuxContext *matroska,
1238 EbmlSyntax *syntax, void *data)
1239 {
1240 static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
1241 // Forbid unknown-length EBML_NONE elements.
1242 [EBML_NONE] = EBML_UNKNOWN_LENGTH - 1,
1243 [EBML_UINT] = 8,
1244 [EBML_SINT] = 8,
1245 [EBML_FLOAT] = 8,
1246 // max. 16 MB for strings
1247 [EBML_STR] = 0x1000000,
1248 [EBML_UTF8] = 0x1000000,
1249 // max. 256 MB for binary data
1250 [EBML_BIN] = 0x10000000,
1251 // no limits for anything else
1252 };
1253 81421 AVIOContext *pb = matroska->ctx->pb;
1254 uint32_t id;
1255 uint64_t length;
1256 81421 int64_t pos = avio_tell(pb), pos_alt;
1257 81421 int res, update_pos = 1, level_check;
1258 MatroskaLevel1Element *level1_elem;
1259
2/2
✓ Branch 0 taken 80723 times.
✓ Branch 1 taken 698 times.
81421 MatroskaLevel *level = matroska->num_levels ? &matroska->levels[matroska->num_levels - 1] : NULL;
1260
1261
2/2
✓ Branch 0 taken 79527 times.
✓ Branch 1 taken 1894 times.
81421 if (!matroska->current_id) {
1262 uint64_t id;
1263 79527 res = ebml_read_num(matroska, pb, 4, &id, 0);
1264
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 79514 times.
79527 if (res < 0) {
1265
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 if (pb->eof_reached && res == AVERROR_EOF) {
1266
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 if (matroska->is_live)
1267 // in live mode, finish parsing if EOF is reached.
1268 13 return 1;
1269
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
9 if (level && pos == avio_tell(pb)) {
1270
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (level->length == EBML_UNKNOWN_LENGTH) {
1271 // Unknown-length levels automatically end at EOF.
1272 9 matroska->num_levels--;
1273 9 return LEVEL_ENDED;
1274 } else {
1275 av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely "
1276 "at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
1277 }
1278 }
1279 }
1280 return res;
1281 }
1282 79514 matroska->current_id = id | 1 << 7 * res;
1283 79514 pos_alt = pos + res;
1284 } else {
1285 1894 pos_alt = pos;
1286 1894 pos -= (av_log2(matroska->current_id) + 7) / 8;
1287 }
1288
1289 81408 id = matroska->current_id;
1290
1291 81408 syntax = ebml_parse_id(syntax, id);
1292
6/6
✓ Branch 0 taken 573 times.
✓ Branch 1 taken 80835 times.
✓ Branch 2 taken 434 times.
✓ Branch 3 taken 139 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 423 times.
81408 if (!syntax->id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
1293
3/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 4 times.
11 if (level && level->length == EBML_UNKNOWN_LENGTH) {
1294 // Unknown-length levels end when an element from an upper level
1295 // in the hierarchy is encountered.
1296
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 while (syntax->def.n) {
1297 3 syntax = ebml_parse_id(syntax->def.n, id);
1298
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (syntax->id) {
1299 3 matroska->num_levels--;
1300 3 return LEVEL_ENDED;
1301 }
1302 };
1303 }
1304
1305 8 av_log(matroska->ctx, AV_LOG_DEBUG, "Unknown entry 0x%"PRIX32" at pos. "
1306 "%"PRId64"\n", id, pos);
1307 8 update_pos = 0; /* Don't update resync_pos as an error might have happened. */
1308 }
1309
1310
2/2
✓ Branch 0 taken 80317 times.
✓ Branch 1 taken 1088 times.
81405 if (data) {
1311 80317 data = (char *) data + syntax->data_offset;
1312
2/2
✓ Branch 0 taken 11298 times.
✓ Branch 1 taken 69019 times.
80317 if (syntax->list_elem_size) {
1313 11298 EbmlList *list = data;
1314 void *newelem;
1315
1316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11298 times.
11298 if ((unsigned)list->nb_elem + 1 >= UINT_MAX / syntax->list_elem_size)
1317 return AVERROR(ENOMEM);
1318 11298 newelem = av_fast_realloc(list->elem,
1319 &list->alloc_elem_size,
1320 11298 (list->nb_elem + 1) * syntax->list_elem_size);
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11298 times.
11298 if (!newelem)
1322 return AVERROR(ENOMEM);
1323 11298 list->elem = newelem;
1324 11298 data = (char *) list->elem + list->nb_elem * syntax->list_elem_size;
1325 11298 memset(data, 0, syntax->list_elem_size);
1326 11298 list->nb_elem++;
1327 }
1328 }
1329
1330
2/2
✓ Branch 0 taken 79506 times.
✓ Branch 1 taken 1899 times.
81405 if (syntax->type != EBML_STOP) {
1331 79506 matroska->current_id = 0;
1332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 79506 times.
79506 if ((res = ebml_read_length(matroska, pb, &length)) < 0)
1333 return res;
1334
1335 79506 pos_alt += res;
1336
1337
2/2
✓ Branch 0 taken 78808 times.
✓ Branch 1 taken 698 times.
79506 if (matroska->num_levels > 0) {
1338
2/2
✓ Branch 0 taken 78802 times.
✓ Branch 1 taken 6 times.
78808 if (length != EBML_UNKNOWN_LENGTH &&
1339
2/2
✓ Branch 0 taken 78698 times.
✓ Branch 1 taken 104 times.
157500 level->length != EBML_UNKNOWN_LENGTH) {
1340 78698 uint64_t elem_end = pos_alt + length,
1341 78698 level_end = level->start + level->length;
1342
1343
2/2
✓ Branch 0 taken 63822 times.
✓ Branch 1 taken 14876 times.
78698 if (elem_end < level_end) {
1344 63822 level_check = 0;
1345
1/2
✓ Branch 0 taken 14876 times.
✗ Branch 1 not taken.
14876 } else if (elem_end == level_end) {
1346 14876 level_check = LEVEL_ENDED;
1347 } else {
1348 av_log(matroska->ctx, AV_LOG_ERROR,
1349 "Element at 0x%"PRIx64" ending at 0x%"PRIx64" exceeds "
1350 "containing master element ending at 0x%"PRIx64"\n",
1351 pos, elem_end, level_end);
1352 return AVERROR_INVALIDDATA;
1353 }
1354
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 6 times.
110 } else if (length != EBML_UNKNOWN_LENGTH) {
1355 104 level_check = 0;
1356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (level->length != EBML_UNKNOWN_LENGTH) {
1357 av_log(matroska->ctx, AV_LOG_ERROR, "Unknown-sized element "
1358 "at 0x%"PRIx64" inside parent with finite size\n", pos);
1359 return AVERROR_INVALIDDATA;
1360 } else {
1361 6 level_check = 0;
1362
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
6 if (id != MATROSKA_ID_CLUSTER && (syntax->type == EBML_LEVEL1
1363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 || syntax->type == EBML_NEST)) {
1364 // According to the current specifications only clusters and
1365 // segments are allowed to be unknown-length. We also accept
1366 // other unknown-length master elements.
1367 av_log(matroska->ctx, AV_LOG_WARNING,
1368 "Found unknown-length element 0x%"PRIX32" other than "
1369 "a cluster at 0x%"PRIx64". Spec-incompliant, but "
1370 "parsing will nevertheless be attempted.\n", id, pos);
1371 update_pos = -1;
1372 }
1373 }
1374 } else
1375 698 level_check = 0;
1376
1377
4/4
✓ Branch 0 taken 64142 times.
✓ Branch 1 taken 15364 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 64140 times.
79506 if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
1378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (length != EBML_UNKNOWN_LENGTH) {
1379 av_log(matroska->ctx, AV_LOG_ERROR,
1380 "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for element "
1381 "with ID 0x%"PRIX32" at 0x%"PRIx64"\n",
1382 length, max_lengths[syntax->type], id, pos);
1383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (syntax->type != EBML_NONE) {
1384 av_log(matroska->ctx, AV_LOG_ERROR,
1385 "Element with ID 0x%"PRIX32" at pos. 0x%"PRIx64" has "
1386 "unknown length, yet the length of an element of its "
1387 "type must be known.\n", id, pos);
1388 } else {
1389 2 av_log(matroska->ctx, AV_LOG_ERROR,
1390 "Found unknown-length element with ID 0x%"PRIX32" at "
1391 "pos. 0x%"PRIx64" for which no syntax for parsing is "
1392 "available.\n", id, pos);
1393 }
1394 2 return AVERROR_INVALIDDATA;
1395 }
1396
1397
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 79374 times.
79504 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1398 // Loosing sync will likely manifest itself as encountering unknown
1399 // elements which are not reliably distinguishable from elements
1400 // belonging to future extensions of the format.
1401 // We use a heuristic to detect such situations: If the current
1402 // element is not expected at the current syntax level and there
1403 // were only a few unknown elements in a row, then the element is
1404 // skipped or considered defective based upon the length of the
1405 // current element (i.e. how much would be skipped); if there were
1406 // more than a few skipped elements in a row and skipping the current
1407 // element would lead us more than SKIP_THRESHOLD away from the last
1408 // known good position, then it is inferred that an error occurred.
1409 // The dependency on the number of unknown elements in a row exists
1410 // because the distance to the last known good position is
1411 // automatically big if the last parsed element was big.
1412 // In both cases, each unknown element is considered equivalent to
1413 // UNKNOWN_EQUIV of skipped bytes for the check.
1414 // The whole check is only done for non-seekable output, because
1415 // in this situation skipped data can't simply be rechecked later.
1416 // This is especially important when using unkown length elements
1417 // as the check for whether a child exceeds its containing master
1418 // element is not effective in this situation.
1419
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 if (update_pos) {
1420 128 matroska->unknown_count = 0;
1421 } else {
1422 2 int64_t dist = length + UNKNOWN_EQUIV * matroska->unknown_count++;
1423
1424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (matroska->unknown_count > 3)
1425 dist += pos_alt - matroska->resync_pos;
1426
1427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (dist > SKIP_THRESHOLD) {
1428 av_log(matroska->ctx, AV_LOG_ERROR,
1429 "Unknown element %"PRIX32" at pos. 0x%"PRIx64" with "
1430 "length 0x%"PRIx64" considered as invalid data. Last "
1431 "known good position 0x%"PRIx64", %d unknown elements"
1432 " in a row\n", id, pos, length, matroska->resync_pos,
1433 matroska->unknown_count);
1434 return AVERROR_INVALIDDATA;
1435 }
1436 }
1437 }
1438
1439
2/2
✓ Branch 0 taken 79498 times.
✓ Branch 1 taken 6 times.
79504 if (update_pos > 0) {
1440 // We have found an element that is allowed at this place
1441 // in the hierarchy and it passed all checks, so treat the beginning
1442 // of the element as the "last known good" position.
1443 79498 matroska->resync_pos = pos;
1444 }
1445
1446
3/4
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 79195 times.
✓ Branch 2 taken 309 times.
✗ Branch 3 not taken.
79504 if (!data && length != EBML_UNKNOWN_LENGTH)
1447 309 goto skip;
1448 }
1449
1450
8/8
✓ Branch 0 taken 23095 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 681 times.
✓ Branch 3 taken 1898 times.
✓ Branch 4 taken 30623 times.
✓ Branch 5 taken 15059 times.
✓ Branch 6 taken 1899 times.
✓ Branch 7 taken 7803 times.
81094 switch (syntax->type) {
1451 23095 case EBML_UINT:
1452 23095 res = ebml_read_uint(pb, length, syntax->def.u, data);
1453 23095 break;
1454 36 case EBML_SINT:
1455 36 res = ebml_read_sint(pb, length, syntax->def.i, data);
1456 36 break;
1457 681 case EBML_FLOAT:
1458 681 res = ebml_read_float(pb, length, syntax->def.f, data);
1459 681 break;
1460 1898 case EBML_STR:
1461 case EBML_UTF8:
1462 1898 res = ebml_read_ascii(pb, length, syntax->def.s, data);
1463 1898 break;
1464 30623 case EBML_BIN:
1465 30623 res = ebml_read_binary(pb, length, pos_alt, data);
1466 30623 break;
1467 15059 case EBML_LEVEL1:
1468 case EBML_NEST:
1469
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15059 times.
15059 if ((res = ebml_read_master(matroska, length, pos_alt)) < 0)
1470 return res;
1471
2/2
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 14710 times.
15059 if (id == MATROSKA_ID_SEGMENT)
1472 349 matroska->segment_start = pos_alt;
1473
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 15039 times.
15059 if (id == MATROSKA_ID_CUES)
1474 20 matroska->cues_parsing_deferred = 0;
1475
3/4
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 13919 times.
✓ Branch 2 taken 1140 times.
✗ Branch 3 not taken.
16199 if (syntax->type == EBML_LEVEL1 &&
1476 1140 (level1_elem = matroska_find_level1_elem(matroska, syntax->id, pos))) {
1477
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 16 times.
1140 if (!level1_elem->pos) {
1478 // Zero is not a valid position for a level 1 element.
1479 1124 level1_elem->pos = pos;
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 } else if (level1_elem->pos != pos)
1481 av_log(matroska->ctx, AV_LOG_ERROR, "Duplicate element\n");
1482 1140 level1_elem->parsed = 1;
1483 }
1484
2/2
✓ Branch 1 taken 1126 times.
✓ Branch 2 taken 13933 times.
15059 if (res = ebml_parse_nest(matroska, syntax->def.n, data))
1485 1126 return res;
1486 13933 break;
1487 1899 case EBML_STOP:
1488 1899 return 1;
1489 309 skip:
1490 8112 default:
1491
2/2
✓ Branch 0 taken 8110 times.
✓ Branch 1 taken 2 times.
8112 if (length) {
1492 int64_t res2;
1493
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8110 times.
8110 if (ffio_limit(pb, length) != length) {
1494 // ffio_limit emits its own error message,
1495 // so we don't have to.
1496 return AVERROR(EIO);
1497 }
1498
1/2
✓ Branch 1 taken 8110 times.
✗ Branch 2 not taken.
8110 if ((res2 = avio_skip(pb, length - 1)) >= 0) {
1499 // avio_skip might take us past EOF. We check for this
1500 // by skipping only length - 1 bytes, reading a byte and
1501 // checking the error flags. This is done in order to check
1502 // that the element has been properly skipped even when
1503 // no filesize (that ffio_limit relies on) is available.
1504 8110 avio_r8(pb);
1505 8110 res = NEEDS_CHECKING;
1506 } else
1507 res = res2;
1508 } else
1509 2 res = 0;
1510 }
1511
2/2
✓ Branch 0 taken 31928 times.
✓ Branch 1 taken 46450 times.
78378 if (res) {
1512
1/2
✓ Branch 0 taken 31928 times.
✗ Branch 1 not taken.
31928 if (res == NEEDS_CHECKING) {
1513
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 31922 times.
31928 if (pb->eof_reached) {
1514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pb->error)
1515 res = pb->error;
1516 else
1517 6 res = AVERROR_EOF;
1518 } else
1519 31922 goto level_check;
1520 }
1521
1522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (res == AVERROR_INVALIDDATA)
1523 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
1524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 else if (res == AVERROR(EIO))
1525 av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
1526
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 else if (res == AVERROR_EOF) {
1527 6 av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely\n");
1528 6 res = AVERROR(EIO);
1529 }
1530
1531 6 return res;
1532 }
1533
1534 46450 level_check:
1535
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 78355 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
78372 if (syntax->is_counted && data) {
1536 17 CountedElement *elem = data;
1537
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (elem->count != UINT_MAX)
1538 17 elem->count++;
1539 }
1540
1541
4/4
✓ Branch 0 taken 14811 times.
✓ Branch 1 taken 63561 times.
✓ Branch 2 taken 14782 times.
✓ Branch 3 taken 29 times.
78372 if (level_check == LEVEL_ENDED && matroska->num_levels) {
1542 14782 level = &matroska->levels[matroska->num_levels - 1];
1543 14782 pos = avio_tell(pb);
1544
1545 // Given that pos >= level->start no check for
1546 // level->length != EBML_UNKNOWN_LENGTH is necessary.
1547
4/4
✓ Branch 0 taken 28952 times.
✓ Branch 1 taken 688 times.
✓ Branch 2 taken 14858 times.
✓ Branch 3 taken 14094 times.
29640 while (matroska->num_levels && pos == level->start + level->length) {
1548 14858 matroska->num_levels--;
1549 14858 level--;
1550 }
1551 }
1552
1553 78372 return level_check;
1554 }
1555
1556 108817 static void ebml_free(EbmlSyntax *syntax, void *data)
1557 {
1558 int i, j;
1559
2/2
✓ Branch 0 taken 383373 times.
✓ Branch 1 taken 108817 times.
492190 for (i = 0; syntax[i].id; i++) {
1560 383373 void *data_off = (char *) data + syntax[i].data_offset;
1561
4/4
✓ Branch 0 taken 3330 times.
✓ Branch 1 taken 62293 times.
✓ Branch 2 taken 73854 times.
✓ Branch 3 taken 243896 times.
383373 switch (syntax[i].type) {
1562 3330 case EBML_STR:
1563 case EBML_UTF8:
1564 3330 av_freep(data_off);
1565 3330 break;
1566 62293 case EBML_BIN:
1567 62293 av_buffer_unref(&((EbmlBin *) data_off)->buf);
1568 62293 break;
1569 73854 case EBML_LEVEL1:
1570 case EBML_NEST:
1571
2/2
✓ Branch 0 taken 7389 times.
✓ Branch 1 taken 66465 times.
73854 if (syntax[i].list_elem_size) {
1572 7389 EbmlList *list = data_off;
1573 7389 char *ptr = list->elem;
1574
2/2
✓ Branch 0 taken 11298 times.
✓ Branch 1 taken 7389 times.
18687 for (j = 0; j < list->nb_elem;
1575 11298 j++, ptr += syntax[i].list_elem_size)
1576 11298 ebml_free(syntax[i].def.n, ptr);
1577 7389 av_freep(&list->elem);
1578 7389 list->nb_elem = 0;
1579 7389 list->alloc_elem_size = 0;
1580 } else
1581 66465 ebml_free(syntax[i].def.n, data_off);
1582 default:
1583 317750 break;
1584 }
1585 }
1586 108817 }
1587
1588 /*
1589 * Autodetecting...
1590 */
1591 6782 static int matroska_probe(const AVProbeData *p)
1592 {
1593 6782 uint64_t total = 0;
1594 6782 int len_mask = 0x80, size = 1, n = 1, i;
1595
1596 /* EBML header? */
1597
2/2
✓ Branch 0 taken 6453 times.
✓ Branch 1 taken 329 times.
6782 if (AV_RB32(p->buf) != EBML_ID_HEADER)
1598 6453 return 0;
1599
1600 /* length of header */
1601 329 total = p->buf[4];
1602
3/4
✓ Branch 0 taken 2058 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1729 times.
✓ Branch 3 taken 329 times.
2058 while (size <= 8 && !(total & len_mask)) {
1603 1729 size++;
1604 1729 len_mask >>= 1;
1605 }
1606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 329 times.
329 if (size > 8)
1607 return 0;
1608 329 total &= (len_mask - 1);
1609
2/2
✓ Branch 0 taken 1729 times.
✓ Branch 1 taken 329 times.
2058 while (n < size)
1610 1729 total = (total << 8) | p->buf[4 + n++];
1611
1612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 329 times.
329 if (total + 1 == 1ULL << (7 * size)){
1613 /* Unknown-length header - simply parse the whole buffer. */
1614 total = p->buf_size - 4 - size;
1615 } else {
1616 /* Does the probe data contain the whole header? */
1617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 329 times.
329 if (p->buf_size < 4 + size + total)
1618 return 0;
1619 }
1620
1621 /* The header should contain a known document type. For now,
1622 * we don't parse the whole header but simply check for the
1623 * availability of that array of characters inside the header.
1624 * Not fully fool-proof, but good enough. */
1625
1/2
✓ Branch 0 taken 577 times.
✗ Branch 1 not taken.
577 for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
1626 577 size_t probelen = strlen(matroska_doctypes[i]);
1627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
577 if (total < probelen)
1628 continue;
1629
2/2
✓ Branch 0 taken 12490 times.
✓ Branch 1 taken 248 times.
12738 for (n = 4 + size; n <= 4 + size + total - probelen; n++)
1630
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 12161 times.
12490 if (!memcmp(p->buf + n, matroska_doctypes[i], probelen))
1631 329 return AVPROBE_SCORE_MAX;
1632 }
1633
1634 // probably valid EBML header but no recognized doctype
1635 return AVPROBE_SCORE_EXTENSION;
1636 }
1637
1638 36395 static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
1639 uint64_t num)
1640 {
1641 36395 MatroskaTrack *tracks = matroska->tracks.elem;
1642 int i;
1643
1644
2/2
✓ Branch 0 taken 42011 times.
✓ Branch 1 taken 1 times.
42012 for (i = 0; i < matroska->tracks.nb_elem; i++)
1645
2/2
✓ Branch 0 taken 36394 times.
✓ Branch 1 taken 5617 times.
42011 if (tracks[i].num == num)
1646 36394 return &tracks[i];
1647
1648 1 av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %"PRIu64"\n", num);
1649 1 return NULL;
1650 }
1651
1652 80 static int matroska_decode_buffer(uint8_t **buf, int *buf_size,
1653 MatroskaTrack *track)
1654 {
1655 80 MatroskaTrackEncoding *encodings = track->encodings.elem;
1656 80 uint8_t *data = *buf;
1657 80 int isize = *buf_size;
1658 80 uint8_t *pkt_data = NULL;
1659 uint8_t av_unused *newpktdata;
1660 80 int pkt_size = isize;
1661 80 int result = 0;
1662 int olen;
1663
1664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (pkt_size >= 10000000U)
1665 return AVERROR_INVALIDDATA;
1666
1667
4/5
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
80 switch (encodings[0].compression.algo) {
1668 68 case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
1669 {
1670 68 int header_size = encodings[0].compression.settings.size;
1671 68 uint8_t *header = encodings[0].compression.settings.data;
1672
1673
2/4
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 68 times.
68 if (header_size && !header) {
1674 av_log(NULL, AV_LOG_ERROR, "Compression size but no data in headerstrip\n");
1675 return -1;
1676 }
1677
1678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!header_size)
1679 return 0;
1680
1681 68 pkt_size = isize + header_size;
1682 68 pkt_data = av_malloc(pkt_size + AV_INPUT_BUFFER_PADDING_SIZE);
1683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!pkt_data)
1684 return AVERROR(ENOMEM);
1685
1686 68 memcpy(pkt_data, header, header_size);
1687 68 memcpy(pkt_data + header_size, data, isize);
1688 68 break;
1689 }
1690 15 case MATROSKA_TRACK_ENCODING_COMP_LZO:
1691 do {
1692 15 int insize = isize;
1693 15 olen = pkt_size *= 3;
1694 15 newpktdata = av_realloc(pkt_data, pkt_size + AV_LZO_OUTPUT_PADDING
1695 15 + AV_INPUT_BUFFER_PADDING_SIZE);
1696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!newpktdata) {
1697 result = AVERROR(ENOMEM);
1698 goto failed;
1699 }
1700 15 pkt_data = newpktdata;
1701 15 result = av_lzo1x_decode(pkt_data, &olen, data, &insize);
1702
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
15 } while (result == AV_LZO_OUTPUT_FULL && pkt_size < 10000000);
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (result) {
1704 result = AVERROR_INVALIDDATA;
1705 goto failed;
1706 }
1707 4 pkt_size -= olen;
1708 4 break;
1709 #if CONFIG_ZLIB
1710 6 case MATROSKA_TRACK_ENCODING_COMP_ZLIB:
1711 {
1712 6 z_stream zstream = { 0 };
1713
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
6 if (!pkt_size || inflateInit(&zstream) != Z_OK)
1714 return -1;
1715 6 zstream.next_in = data;
1716 6 zstream.avail_in = isize;
1717 do {
1718 8 pkt_size *= 3;
1719 8 newpktdata = av_realloc(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE);
1720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!newpktdata) {
1721 inflateEnd(&zstream);
1722 result = AVERROR(ENOMEM);
1723 goto failed;
1724 }
1725 8 pkt_data = newpktdata;
1726 8 zstream.avail_out = pkt_size - zstream.total_out;
1727 8 zstream.next_out = pkt_data + zstream.total_out;
1728 8 result = inflate(&zstream, Z_NO_FLUSH);
1729
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
8 } while (result == Z_OK && pkt_size < 10000000);
1730 6 pkt_size = zstream.total_out;
1731 6 inflateEnd(&zstream);
1732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (result != Z_STREAM_END) {
1733 if (result == Z_MEM_ERROR)
1734 result = AVERROR(ENOMEM);
1735 else
1736 result = AVERROR_INVALIDDATA;
1737 goto failed;
1738 }
1739 6 break;
1740 }
1741 #endif
1742 #if CONFIG_BZLIB
1743 2 case MATROSKA_TRACK_ENCODING_COMP_BZLIB:
1744 {
1745 2 bz_stream bzstream = { 0 };
1746
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (!pkt_size || BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1747 return -1;
1748 2 bzstream.next_in = data;
1749 2 bzstream.avail_in = isize;
1750 do {
1751 6 pkt_size *= 3;
1752 6 newpktdata = av_realloc(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE);
1753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!newpktdata) {
1754 BZ2_bzDecompressEnd(&bzstream);
1755 result = AVERROR(ENOMEM);
1756 goto failed;
1757 }
1758 6 pkt_data = newpktdata;
1759 6 bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1760 6 bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1761 6 result = BZ2_bzDecompress(&bzstream);
1762
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
6 } while (result == BZ_OK && pkt_size < 10000000);
1763 2 pkt_size = bzstream.total_out_lo32;
1764 2 BZ2_bzDecompressEnd(&bzstream);
1765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != BZ_STREAM_END) {
1766 if (result == BZ_MEM_ERROR)
1767 result = AVERROR(ENOMEM);
1768 else
1769 result = AVERROR_INVALIDDATA;
1770 goto failed;
1771 }
1772 2 break;
1773 }
1774 #endif
1775 default:
1776 return AVERROR_INVALIDDATA;
1777 }
1778
1779 80 memset(pkt_data + pkt_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1780
1781 80 *buf = pkt_data;
1782 80 *buf_size = pkt_size;
1783 80 return 0;
1784
1785 failed:
1786 av_free(pkt_data);
1787 return result;
1788 }
1789
1790 147 static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
1791 AVDictionary **metadata, char *prefix)
1792 {
1793 147 MatroskaTag *tags = list->elem;
1794 char key[1024];
1795 int i;
1796
1797
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 147 times.
453 for (i = 0; i < list->nb_elem; i++) {
1798 918 const char *lang = tags[i].lang &&
1799
3/4
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 292 times.
306 strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
1800
1801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306 times.
306 if (!tags[i].name) {
1802 av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
1803 continue;
1804 }
1805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306 times.
306 if (prefix)
1806 snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
1807 else
1808 306 av_strlcpy(key, tags[i].name, sizeof(key));
1809
3/4
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 292 times.
✓ Branch 3 taken 14 times.
306 if (tags[i].def || !lang) {
1810 292 av_dict_set(metadata, key, tags[i].string, 0);
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 292 times.
292 if (tags[i].sub.nb_elem)
1812 matroska_convert_tag(s, &tags[i].sub, metadata, key);
1813 }
1814
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 292 times.
306 if (lang) {
1815 14 av_strlcat(key, "-", sizeof(key));
1816 14 av_strlcat(key, lang, sizeof(key));
1817 14 av_dict_set(metadata, key, tags[i].string, 0);
1818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (tags[i].sub.nb_elem)
1819 matroska_convert_tag(s, &tags[i].sub, metadata, key);
1820 }
1821 }
1822 147 ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
1823 147 }
1824
1825 349 static void matroska_convert_tags(AVFormatContext *s)
1826 {
1827 349 MatroskaDemuxContext *matroska = s->priv_data;
1828 349 MatroskaTags *tags = matroska->tags.elem;
1829 int i, j;
1830
1831
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 349 times.
496 for (i = 0; i < matroska->tags.nb_elem; i++) {
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 if (tags[i].target.attachuid) {
1833 MatroskaAttachment *attachment = matroska->attachments.elem;
1834 int found = 0;
1835 for (j = 0; j < matroska->attachments.nb_elem; j++) {
1836 if (attachment[j].uid == tags[i].target.attachuid &&
1837 attachment[j].stream) {
1838 matroska_convert_tag(s, &tags[i].tag,
1839 &attachment[j].stream->metadata, NULL);
1840 found = 1;
1841 }
1842 }
1843 if (!found) {
1844 av_log(s, AV_LOG_WARNING,
1845 "The tags at index %d refer to a "
1846 "non-existent attachment %"PRId64".\n",
1847 i, tags[i].target.attachuid);
1848 }
1849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 } else if (tags[i].target.chapteruid) {
1850 MatroskaChapter *chapter = matroska->chapters.elem;
1851 int found = 0;
1852 for (j = 0; j < matroska->chapters.nb_elem; j++) {
1853 if (chapter[j].uid == tags[i].target.chapteruid &&
1854 chapter[j].chapter) {
1855 matroska_convert_tag(s, &tags[i].tag,
1856 &chapter[j].chapter->metadata, NULL);
1857 found = 1;
1858 }
1859 }
1860 if (!found) {
1861 av_log(s, AV_LOG_WARNING,
1862 "The tags at index %d refer to a non-existent chapter "
1863 "%"PRId64".\n",
1864 i, tags[i].target.chapteruid);
1865 }
1866
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 40 times.
147 } else if (tags[i].target.trackuid) {
1867 107 MatroskaTrack *track = matroska->tracks.elem;
1868 107 int found = 0;
1869
2/2
✓ Branch 0 taken 235 times.
✓ Branch 1 taken 107 times.
342 for (j = 0; j < matroska->tracks.nb_elem; j++) {
1870
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 128 times.
235 if (track[j].uid == tags[i].target.trackuid &&
1871
1/2
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
107 track[j].stream) {
1872 107 matroska_convert_tag(s, &tags[i].tag,
1873 107 &track[j].stream->metadata, NULL);
1874 107 found = 1;
1875 }
1876 }
1877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 if (!found) {
1878 av_log(s, AV_LOG_WARNING,
1879 "The tags at index %d refer to a non-existent track "
1880 "%"PRId64".\n",
1881 i, tags[i].target.trackuid);
1882 }
1883 } else {
1884 40 matroska_convert_tag(s, &tags[i].tag, &s->metadata,
1885 40 tags[i].target.type);
1886 }
1887 }
1888 349 }
1889
1890 17 static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska,
1891 int64_t pos)
1892 {
1893 17 uint32_t saved_id = matroska->current_id;
1894 17 int64_t before_pos = avio_tell(matroska->ctx->pb);
1895 17 int ret = 0;
1896 int ret2;
1897
1898 /* seek */
1899
1/2
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
17 if (avio_seek(matroska->ctx->pb, pos, SEEK_SET) == pos) {
1900 /* We don't want to lose our seekhead level, so we add
1901 * a dummy. This is a crude hack. */
1902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (matroska->num_levels == EBML_MAX_DEPTH) {
1903 av_log(matroska->ctx, AV_LOG_INFO,
1904 "Max EBML element depth (%d) reached, "
1905 "cannot parse further.\n", EBML_MAX_DEPTH);
1906 ret = AVERROR_INVALIDDATA;
1907 } else {
1908 17 matroska->levels[matroska->num_levels] = (MatroskaLevel) { 0, EBML_UNKNOWN_LENGTH };
1909 17 matroska->num_levels++;
1910 17 matroska->current_id = 0;
1911
1912 17 ret = ebml_parse(matroska, matroska_segment, matroska);
1913
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
17 if (ret == LEVEL_ENDED) {
1914 /* This can only happen if the seek brought us beyond EOF. */
1915 1 ret = AVERROR_EOF;
1916 }
1917 }
1918 }
1919 /* Seek back - notice that in all instances where this is used
1920 * it is safe to set the level to 1. */
1921 17 ret2 = matroska_reset_status(matroska, saved_id, before_pos);
1922
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 if (ret >= 0)
1923 16 ret = ret2;
1924
1925 17 return ret;
1926 }
1927
1928 349 static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
1929 {
1930 349 EbmlList *seekhead_list = &matroska->seekhead;
1931 int i;
1932
1933 // we should not do any seeking in the streaming case
1934
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 348 times.
349 if (!(matroska->ctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
1935 1 return;
1936
1937
2/2
✓ Branch 0 taken 1097 times.
✓ Branch 1 taken 347 times.
1444 for (i = 0; i < seekhead_list->nb_elem; i++) {
1938 1097 MatroskaSeekhead *seekheads = seekhead_list->elem;
1939 1097 uint32_t id = seekheads[i].id;
1940 1097 int64_t pos = seekheads[i].pos + matroska->segment_start;
1941 MatroskaLevel1Element *elem;
1942
1943
2/4
✓ Branch 0 taken 1097 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1097 times.
1097 if (id != seekheads[i].id || pos < matroska->segment_start)
1944 continue;
1945
1946 1097 elem = matroska_find_level1_elem(matroska, id, pos);
1947
4/4
✓ Branch 0 taken 1072 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 760 times.
✓ Branch 3 taken 312 times.
1097 if (!elem || elem->parsed)
1948 785 continue;
1949
1950 312 elem->pos = pos;
1951
1952 // defer cues parsing until we actually need cue data.
1953
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 6 times.
312 if (id == MATROSKA_ID_CUES)
1954 306 continue;
1955
1956
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
6 if (matroska_parse_seekhead_entry(matroska, pos) < 0) {
1957 // mark index as broken
1958 1 matroska->cues_parsing_deferred = -1;
1959 1 break;
1960 }
1961
1962 5 elem->parsed = 1;
1963 }
1964 }
1965
1966 361 static void matroska_add_index_entries(MatroskaDemuxContext *matroska)
1967 {
1968 EbmlList *index_list;
1969 MatroskaIndex *index;
1970 361 uint64_t index_scale = 1;
1971 int i, j;
1972
1973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 361 times.
361 if (matroska->ctx->flags & AVFMT_FLAG_IGNIDX)
1974 return;
1975
1976 361 index_list = &matroska->index;
1977 361 index = index_list->elem;
1978
2/2
✓ Branch 0 taken 343 times.
✓ Branch 1 taken 18 times.
361 if (index_list->nb_elem < 2)
1979 343 return;
1980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (index[1].time > 1E14 / matroska->time_scale) {
1981 av_log(matroska->ctx, AV_LOG_WARNING, "Dropping apparently-broken index.\n");
1982 return;
1983 }
1984
2/2
✓ Branch 0 taken 3236 times.
✓ Branch 1 taken 18 times.
3254 for (i = 0; i < index_list->nb_elem; i++) {
1985 3236 EbmlList *pos_list = &index[i].pos;
1986 3236 MatroskaIndexPos *pos = pos_list->elem;
1987
2/2
✓ Branch 0 taken 6050 times.
✓ Branch 1 taken 3236 times.
9286 for (j = 0; j < pos_list->nb_elem; j++) {
1988 6050 MatroskaTrack *track = matroska_find_track_by_num(matroska,
1989 6050 pos[j].track);
1990
2/4
✓ Branch 0 taken 6050 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6050 times.
✗ Branch 3 not taken.
6050 if (track && track->stream)
1991 6050 av_add_index_entry(track->stream,
1992 6050 pos[j].pos + matroska->segment_start,
1993 6050 index[i].time / index_scale, 0, 0,
1994 AVINDEX_KEYFRAME);
1995 }
1996 }
1997 }
1998
1999 12 static void matroska_parse_cues(MatroskaDemuxContext *matroska) {
2000 int i;
2001
2002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (matroska->ctx->flags & AVFMT_FLAG_IGNIDX)
2003 return;
2004
2005
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 1 times.
50 for (i = 0; i < matroska->num_level1_elems; i++) {
2006 49 MatroskaLevel1Element *elem = &matroska->level1_elems[i];
2007
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
49 if (elem->id == MATROSKA_ID_CUES && !elem->parsed) {
2008
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if (matroska_parse_seekhead_entry(matroska, elem->pos) < 0)
2009 matroska->cues_parsing_deferred = -1;
2010 11 elem->parsed = 1;
2011 11 break;
2012 }
2013 }
2014
2015 12 matroska_add_index_entries(matroska);
2016 }
2017
2018 static int matroska_aac_profile(char *codec_id)
2019 {
2020 static const char *const aac_profiles[] = { "MAIN", "LC", "SSR" };
2021 int profile;
2022
2023 for (profile = 0; profile < FF_ARRAY_ELEMS(aac_profiles); profile++)
2024 if (strstr(codec_id, aac_profiles[profile]))
2025 break;
2026 return profile + 1;
2027 }
2028
2029 static int matroska_aac_sri(int samplerate)
2030 {
2031 int sri;
2032
2033 for (sri = 0; sri < FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++)
2034 if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2035 break;
2036 return sri;
2037 }
2038
2039 12 static void matroska_metadata_creation_time(AVDictionary **metadata, int64_t date_utc)
2040 {
2041 /* Convert to seconds and adjust by number of seconds between 2001-01-01 and Epoch */
2042 12 avpriv_dict_set_timestamp(metadata, "creation_time", date_utc / 1000 + 978307200000000LL);
2043 12 }
2044
2045 9 static int matroska_parse_flac(AVFormatContext *s,
2046 MatroskaTrack *track,
2047 int *offset)
2048 {
2049 9 AVStream *st = track->stream;
2050 9 uint8_t *p = track->codec_priv.data;
2051 9 int size = track->codec_priv.size;
2052
2053
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (size < 8 + FLAC_STREAMINFO_SIZE || p[4] & 0x7f) {
2054 av_log(s, AV_LOG_WARNING, "Invalid FLAC private data\n");
2055 track->codec_priv.size = 0;
2056 return 0;
2057 }
2058 9 *offset = 8;
2059 9 track->codec_priv.size = 8 + FLAC_STREAMINFO_SIZE;
2060
2061 9 p += track->codec_priv.size;
2062 9 size -= track->codec_priv.size;
2063
2064 /* parse the remaining metadata blocks if present */
2065
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 while (size >= 4) {
2066 int block_last, block_type, block_size;
2067
2068 8 flac_parse_block_header(p, &block_last, &block_type, &block_size);
2069
2070 8 p += 4;
2071 8 size -= 4;
2072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (block_size > size)
2073 return 0;
2074
2075 /* check for the channel mask */
2076
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (block_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
2077 8 AVDictionary *dict = NULL;
2078 AVDictionaryEntry *chmask;
2079
2080 8 ff_vorbis_comment(s, &dict, p, block_size, 0);
2081 8 chmask = av_dict_get(dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
2082
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (chmask) {
2083 8 uint64_t mask = strtol(chmask->value, NULL, 0);
2084
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (!mask || mask & ~0x3ffffULL) {
2085 av_log(s, AV_LOG_WARNING,
2086 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK<