FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/matroskadec.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 1687 2301 73.3%
Functions: 65 68 95.6%
Branches: 988 1608 61.4%

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