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/display.h"
42 #include "libavutil/intfloat.h"
43 #include "libavutil/intreadwrite.h"
44 #include "libavutil/lzo.h"
45 #include "libavutil/mastering_display_metadata.h"
46 #include "libavutil/mathematics.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/time_internal.h"
49 #include "libavutil/spherical.h"
50 
51 #include "libavcodec/bytestream.h"
52 #include "libavcodec/flac.h"
53 #include "libavcodec/mpeg4audio.h"
54 #include "libavcodec/packet_internal.h"
55 
56 #include "avformat.h"
57 #include "avio_internal.h"
58 #include "demux.h"
59 #include "dovi_isom.h"
60 #include "internal.h"
61 #include "isom.h"
62 #include "matroska.h"
63 #include "oggdec.h"
64 /* For ff_codec_get_id(). */
65 #include "riff.h"
66 #include "rmsipr.h"
67 
68 #if CONFIG_BZLIB
69 #include <bzlib.h>
70 #endif
71 #if CONFIG_ZLIB
72 #include <zlib.h>
73 #endif
74 
75 #include "qtpalette.h"
76 
77 #define EBML_UNKNOWN_LENGTH  UINT64_MAX /* EBML unknown length, in uint64_t */
78 #define NEEDS_CHECKING                2 /* Indicates that some error checks
79                                          * still need to be performed */
80 #define LEVEL_ENDED                   3 /* return value of ebml_parse when the
81                                          * syntax level used for parsing ended. */
82 #define SKIP_THRESHOLD      1024 * 1024 /* In non-seekable mode, if more than SKIP_THRESHOLD
83                                          * of unkown, potentially damaged data is encountered,
84                                          * it is considered an error. */
85 #define UNKNOWN_EQUIV         50 * 1024 /* An unknown element is considered equivalent
86                                          * to this many bytes of unknown data for the
87                                          * SKIP_THRESHOLD check. */
88 
89 typedef enum {
90     EBML_NONE,
91     EBML_UINT,
92     EBML_SINT,
93     EBML_FLOAT,
94     EBML_STR,
95     EBML_UTF8,
96     EBML_BIN,
97     EBML_NEST,
98     EBML_LEVEL1,
99     EBML_STOP,
100     EBML_TYPE_COUNT
101 } EbmlType;
102 
103 typedef struct CountedElement {
104     union {
105         uint64_t  u;
106         int64_t   i;
107         double    f;
108         char     *s;
109     } el;
110     unsigned count;
111 } CountedElement;
112 
113 typedef const struct EbmlSyntax {
114     uint32_t id;
115     uint8_t type;
116     uint8_t is_counted;
117     size_t list_elem_size;
118     size_t data_offset;
119     union {
120         int64_t     i;
121         uint64_t    u;
122         double      f;
123         const char *s;
124         const struct EbmlSyntax *n;
125     } def;
126 } EbmlSyntax;
127 
128 typedef struct EbmlList {
129     int nb_elem;
130     unsigned int alloc_elem_size;
131     void *elem;
132 } EbmlList;
133 
134 typedef struct EbmlBin {
135     int      size;
136     AVBufferRef *buf;
137     uint8_t *data;
138     int64_t  pos;
139 } EbmlBin;
140 
141 typedef struct Ebml {
142     uint64_t version;
143     uint64_t max_size;
144     uint64_t id_length;
145     char    *doctype;
146     uint64_t doctype_version;
147 } Ebml;
148 
149 typedef struct MatroskaTrackCompression {
150     uint64_t algo;
151     EbmlBin  settings;
152 } MatroskaTrackCompression;
153 
154 typedef struct MatroskaTrackEncryption {
155     uint64_t algo;
156     EbmlBin  key_id;
157 } MatroskaTrackEncryption;
158 
159 typedef struct MatroskaTrackEncoding {
160     uint64_t scope;
161     uint64_t type;
162     MatroskaTrackCompression compression;
163     MatroskaTrackEncryption encryption;
164 } MatroskaTrackEncoding;
165 
166 typedef struct MatroskaMasteringMeta {
167     double r_x;
168     double r_y;
169     double g_x;
170     double g_y;
171     double b_x;
172     double b_y;
173     double white_x;
174     double white_y;
175     double max_luminance;
176     CountedElement min_luminance;
177 } MatroskaMasteringMeta;
178 
179 typedef struct MatroskaTrackVideoColor {
180     uint64_t matrix_coefficients;
181     uint64_t bits_per_channel;
182     uint64_t chroma_sub_horz;
183     uint64_t chroma_sub_vert;
184     uint64_t cb_sub_horz;
185     uint64_t cb_sub_vert;
186     uint64_t chroma_siting_horz;
187     uint64_t chroma_siting_vert;
188     uint64_t range;
189     uint64_t transfer_characteristics;
190     uint64_t primaries;
191     uint64_t max_cll;
192     uint64_t max_fall;
193     MatroskaMasteringMeta mastering_meta;
194 } MatroskaTrackVideoColor;
195 
196 typedef struct MatroskaTrackVideoProjection {
197     uint64_t type;
198     EbmlBin private;
199     double yaw;
200     double pitch;
201     double roll;
202 } MatroskaTrackVideoProjection;
203 
204 typedef struct MatroskaTrackVideo {
205     double   frame_rate;
206     uint64_t display_width;
207     uint64_t display_height;
208     uint64_t pixel_width;
209     uint64_t pixel_height;
210     EbmlBin  color_space;
211     uint64_t display_unit;
212     uint64_t interlaced;
213     uint64_t field_order;
214     uint64_t stereo_mode;
215     uint64_t alpha_mode;
216     EbmlList color;
217     MatroskaTrackVideoProjection projection;
218 } MatroskaTrackVideo;
219 
220 typedef struct MatroskaTrackAudio {
221     double   samplerate;
222     double   out_samplerate;
223     uint64_t bitdepth;
224     uint64_t channels;
225 
226     /* real audio header (extracted from extradata) */
227     int      coded_framesize;
228     int      sub_packet_h;
229     int      frame_size;
230     int      sub_packet_size;
231     int      sub_packet_cnt;
232     int      pkt_cnt;
233     uint64_t buf_timecode;
234     uint8_t *buf;
235 } MatroskaTrackAudio;
236 
237 typedef struct MatroskaTrackPlane {
238     uint64_t uid;
239     uint64_t type;
240 } MatroskaTrackPlane;
241 
242 typedef struct MatroskaTrackOperation {
243     EbmlList combine_planes;
244 } MatroskaTrackOperation;
245 
246 typedef struct MatroskaBlockAdditionMapping {
247     uint64_t value;
248     char *name;
249     uint64_t type;
250     EbmlBin extradata;
251 } MatroskaBlockAdditionMapping;
252 
253 typedef struct MatroskaTrack {
254     uint64_t num;
255     uint64_t uid;
256     uint64_t type;
257     char    *name;
258     char    *codec_id;
259     EbmlBin  codec_priv;
260     char    *language;
261     double time_scale;
262     uint64_t default_duration;
263     uint64_t flag_default;
264     uint64_t flag_forced;
265     uint64_t flag_comment;
266     uint64_t flag_hearingimpaired;
267     uint64_t flag_visualimpaired;
268     uint64_t flag_textdescriptions;
269     CountedElement flag_original;
270     uint64_t seek_preroll;
271     MatroskaTrackVideo video;
272     MatroskaTrackAudio audio;
273     MatroskaTrackOperation operation;
274     EbmlList encodings;
275     uint64_t codec_delay;
276     uint64_t codec_delay_in_track_tb;
277 
278     AVStream *stream;
279     int64_t end_timecode;
280     int ms_compat;
281     int needs_decoding;
282     uint64_t max_block_additional_id;
283     EbmlList block_addition_mappings;
284 
285     uint32_t palette[AVPALETTE_COUNT];
286     int has_palette;
287 } MatroskaTrack;
288 
289 typedef struct MatroskaAttachment {
290     uint64_t uid;
291     char *filename;
292     char *description;
293     char *mime;
294     EbmlBin bin;
295 
296     AVStream *stream;
297 } MatroskaAttachment;
298 
299 typedef struct MatroskaChapter {
300     uint64_t start;
301     uint64_t end;
302     uint64_t uid;
303     char    *title;
304 
305     AVChapter *chapter;
306 } MatroskaChapter;
307 
308 typedef struct MatroskaIndexPos {
309     uint64_t track;
310     uint64_t pos;
311 } MatroskaIndexPos;
312 
313 typedef struct MatroskaIndex {
314     uint64_t time;
315     EbmlList pos;
316 } MatroskaIndex;
317 
318 typedef struct MatroskaTag {
319     char *name;
320     char *string;
321     char *lang;
322     uint64_t def;
323     EbmlList sub;
324 } MatroskaTag;
325 
326 typedef struct MatroskaTagTarget {
327     char    *type;
328     uint64_t typevalue;
329     uint64_t trackuid;
330     uint64_t chapteruid;
331     uint64_t attachuid;
332 } MatroskaTagTarget;
333 
334 typedef struct MatroskaTags {
335     MatroskaTagTarget target;
336     EbmlList tag;
337 } MatroskaTags;
338 
339 typedef struct MatroskaSeekhead {
340     uint64_t id;
341     uint64_t pos;
342 } MatroskaSeekhead;
343 
344 typedef struct MatroskaLevel {
345     uint64_t start;
346     uint64_t length;
347 } MatroskaLevel;
348 
349 typedef struct MatroskaBlock {
350     uint64_t duration;
351     CountedElement reference;
352     uint64_t non_simple;
353     EbmlBin  bin;
354     uint64_t additional_id;
355     EbmlBin  additional;
356     int64_t  discard_padding;
357 } MatroskaBlock;
358 
359 typedef struct MatroskaCluster {
360     MatroskaBlock block;
361     uint64_t timecode;
362     int64_t pos;
363 } MatroskaCluster;
364 
365 typedef struct MatroskaLevel1Element {
366     int64_t  pos;
367     uint32_t id;
368     int parsed;
369 } MatroskaLevel1Element;
370 
371 typedef struct MatroskaDemuxContext {
372     const AVClass *class;
373     AVFormatContext *ctx;
374 
375     /* EBML stuff */
376     MatroskaLevel levels[EBML_MAX_DEPTH];
377     int      num_levels;
378     uint32_t current_id;
379     int64_t  resync_pos;
380     int      unknown_count;
381 
382     uint64_t time_scale;
383     double   duration;
384     char    *title;
385     char    *muxingapp;
386     EbmlBin  date_utc;
387     EbmlList tracks;
388     EbmlList attachments;
389     EbmlList chapters;
390     EbmlList index;
391     EbmlList tags;
392     EbmlList seekhead;
393 
394     /* byte position of the segment inside the stream */
395     int64_t segment_start;
396 
397     /* This packet coincides with FFFormatContext.parse_pkt
398      * and is not owned by us. */
399     AVPacket *pkt;
400 
401     /* the packet queue */
402     PacketList queue;
403 
404     int done;
405 
406     /* What to skip before effectively reading a packet. */
407     int skip_to_keyframe;
408     uint64_t skip_to_timecode;
409 
410     /* File has a CUES element, but we defer parsing until it is needed. */
411     int cues_parsing_deferred;
412 
413     /* Level1 elements and whether they were read yet */
414     MatroskaLevel1Element level1_elems[64];
415     int num_level1_elems;
416 
417     MatroskaCluster current_cluster;
418 
419     /* WebM DASH Manifest live flag */
420     int is_live;
421 
422     /* Bandwidth value for WebM DASH Manifest */
423     int bandwidth;
424 } MatroskaDemuxContext;
425 
426 #define CHILD_OF(parent) { .def = { .n = parent } }
427 
428 // The following forward declarations need their size because
429 // a tentative definition with internal linkage must not be an
430 // incomplete type (6.7.2 in C90, 6.9.2 in C99).
431 // Removing the sizes breaks MSVC.
432 static EbmlSyntax ebml_syntax[3], matroska_segment[9], matroska_track_video_color[15], matroska_track_video[19],
433                   matroska_track[33], matroska_track_encoding[6], matroska_track_encodings[2],
434                   matroska_track_combine_planes[2], matroska_track_operation[2], matroska_block_addition_mapping[5], matroska_tracks[2],
435                   matroska_attachments[2], matroska_chapter_entry[9], matroska_chapter[6], matroska_chapters[2],
436                   matroska_index_entry[3], matroska_index[2], matroska_tag[3], matroska_tags[2], matroska_seekhead[2],
437                   matroska_blockadditions[2], matroska_blockgroup[8], matroska_cluster_parsing[8];
438 
439 static EbmlSyntax ebml_header[] = {
440     { EBML_ID_EBMLREADVERSION,    EBML_UINT, 0, 0, offsetof(Ebml, version),         { .u = EBML_VERSION } },
441     { EBML_ID_EBMLMAXSIZELENGTH,  EBML_UINT, 0, 0, offsetof(Ebml, max_size),        { .u = 8 } },
442     { EBML_ID_EBMLMAXIDLENGTH,    EBML_UINT, 0, 0, offsetof(Ebml, id_length),       { .u = 4 } },
443     { EBML_ID_DOCTYPE,            EBML_STR,  0, 0, offsetof(Ebml, doctype),         { .s = "(none)" } },
444     { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, 0, offsetof(Ebml, doctype_version), { .u = 1 } },
445     { EBML_ID_EBMLVERSION,        EBML_NONE },
446     { EBML_ID_DOCTYPEVERSION,     EBML_NONE },
447     CHILD_OF(ebml_syntax)
448 };
449 
450 static EbmlSyntax ebml_syntax[] = {
451     { EBML_ID_HEADER,      EBML_NEST, 0, 0, 0, { .n = ebml_header } },
452     { MATROSKA_ID_SEGMENT, EBML_STOP },
453     { 0 }
454 };
455 
456 static EbmlSyntax matroska_info[] = {
457     { MATROSKA_ID_TIMECODESCALE, EBML_UINT,  0, 0, offsetof(MatroskaDemuxContext, time_scale), { .u = 1000000 } },
458     { MATROSKA_ID_DURATION,      EBML_FLOAT, 0, 0, offsetof(MatroskaDemuxContext, duration) },
459     { MATROSKA_ID_TITLE,         EBML_UTF8,  0, 0, offsetof(MatroskaDemuxContext, title) },
460     { MATROSKA_ID_WRITINGAPP,    EBML_NONE },
461     { MATROSKA_ID_MUXINGAPP,     EBML_UTF8, 0, 0, offsetof(MatroskaDemuxContext, muxingapp) },
462     { MATROSKA_ID_DATEUTC,       EBML_BIN,  0, 0, offsetof(MatroskaDemuxContext, date_utc) },
463     { MATROSKA_ID_SEGMENTUID,    EBML_NONE },
464     CHILD_OF(matroska_segment)
465 };
466 
467 static EbmlSyntax matroska_mastering_meta[] = {
468     { MATROSKA_ID_VIDEOCOLOR_RX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, r_x) },
469     { MATROSKA_ID_VIDEOCOLOR_RY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, r_y) },
470     { MATROSKA_ID_VIDEOCOLOR_GX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, g_x) },
471     { MATROSKA_ID_VIDEOCOLOR_GY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, g_y) },
472     { MATROSKA_ID_VIDEOCOLOR_BX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, b_x) },
473     { MATROSKA_ID_VIDEOCOLOR_BY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, b_y) },
474     { MATROSKA_ID_VIDEOCOLOR_WHITEX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, white_x) },
475     { MATROSKA_ID_VIDEOCOLOR_WHITEY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, white_y) },
476     { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN, EBML_FLOAT, 1, 0, offsetof(MatroskaMasteringMeta, min_luminance) },
477     { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, max_luminance) },
478     CHILD_OF(matroska_track_video_color)
479 };
480 
481 static EbmlSyntax matroska_track_video_color[] = {
482     { MATROSKA_ID_VIDEOCOLORMATRIXCOEFF,      EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, matrix_coefficients), { .u = AVCOL_SPC_UNSPECIFIED } },
483     { MATROSKA_ID_VIDEOCOLORBITSPERCHANNEL,   EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, bits_per_channel), { .u = 0 } },
484     { MATROSKA_ID_VIDEOCOLORCHROMASUBHORZ,    EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_horz) },
485     { MATROSKA_ID_VIDEOCOLORCHROMASUBVERT,    EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_vert) },
486     { MATROSKA_ID_VIDEOCOLORCBSUBHORZ,        EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, cb_sub_horz) },
487     { MATROSKA_ID_VIDEOCOLORCBSUBVERT,        EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, cb_sub_vert) },
488     { MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_horz), { .u = MATROSKA_COLOUR_CHROMASITINGHORZ_UNDETERMINED } },
489     { MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_vert), { .u = MATROSKA_COLOUR_CHROMASITINGVERT_UNDETERMINED } },
490     { MATROSKA_ID_VIDEOCOLORRANGE,            EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, range), { .u = AVCOL_RANGE_UNSPECIFIED } },
491     { MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, transfer_characteristics), { .u = AVCOL_TRC_UNSPECIFIED } },
492     { MATROSKA_ID_VIDEOCOLORPRIMARIES,        EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, primaries), { .u = AVCOL_PRI_UNSPECIFIED } },
493     { MATROSKA_ID_VIDEOCOLORMAXCLL,           EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, max_cll) },
494     { MATROSKA_ID_VIDEOCOLORMAXFALL,          EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, max_fall) },
495     { MATROSKA_ID_VIDEOCOLORMASTERINGMETA,    EBML_NEST, 0, 0, offsetof(MatroskaTrackVideoColor, mastering_meta), { .n = matroska_mastering_meta } },
496     CHILD_OF(matroska_track_video)
497 };
498 
499 static EbmlSyntax matroska_track_video_projection[] = {
500     { MATROSKA_ID_VIDEOPROJECTIONTYPE,        EBML_UINT,  0, 0, offsetof(MatroskaTrackVideoProjection, type), { .u = MATROSKA_VIDEO_PROJECTION_TYPE_RECTANGULAR } },
501     { MATROSKA_ID_VIDEOPROJECTIONPRIVATE,     EBML_BIN,   0, 0, offsetof(MatroskaTrackVideoProjection, private) },
502     { MATROSKA_ID_VIDEOPROJECTIONPOSEYAW,     EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, yaw),   { .f = 0.0 } },
503     { MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH,   EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, pitch), { .f = 0.0 } },
504     { MATROSKA_ID_VIDEOPROJECTIONPOSEROLL,    EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, roll),  { .f = 0.0 } },
505     CHILD_OF(matroska_track_video)
506 };
507 
508 static EbmlSyntax matroska_track_video[] = {
509     { MATROSKA_ID_VIDEOFRAMERATE,      EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideo, frame_rate) },
510     { MATROSKA_ID_VIDEODISPLAYWIDTH,   EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, display_width), { .u=-1 } },
511     { MATROSKA_ID_VIDEODISPLAYHEIGHT,  EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, display_height), { .u=-1 } },
512     { MATROSKA_ID_VIDEOPIXELWIDTH,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, pixel_width) },
513     { MATROSKA_ID_VIDEOPIXELHEIGHT,    EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, pixel_height) },
514     { MATROSKA_ID_VIDEOCOLORSPACE,     EBML_BIN,   0, 0, offsetof(MatroskaTrackVideo, color_space) },
515     { MATROSKA_ID_VIDEOALPHAMODE,      EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, alpha_mode), { .u = 0 } },
516     { MATROSKA_ID_VIDEOCOLOR,          EBML_NEST,  0, sizeof(MatroskaTrackVideoColor), offsetof(MatroskaTrackVideo, color), { .n = matroska_track_video_color } },
517     { MATROSKA_ID_VIDEOPROJECTION,     EBML_NEST,  0, 0, offsetof(MatroskaTrackVideo, projection), { .n = matroska_track_video_projection } },
518     { MATROSKA_ID_VIDEOPIXELCROPB,     EBML_NONE },
519     { MATROSKA_ID_VIDEOPIXELCROPT,     EBML_NONE },
520     { MATROSKA_ID_VIDEOPIXELCROPL,     EBML_NONE },
521     { MATROSKA_ID_VIDEOPIXELCROPR,     EBML_NONE },
522     { MATROSKA_ID_VIDEODISPLAYUNIT,    EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, display_unit), { .u= MATROSKA_VIDEO_DISPLAYUNIT_PIXELS } },
523     { MATROSKA_ID_VIDEOFLAGINTERLACED, EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, interlaced),  { .u = MATROSKA_VIDEO_INTERLACE_FLAG_UNDETERMINED } },
524     { MATROSKA_ID_VIDEOFIELDORDER,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, field_order), { .u = MATROSKA_VIDEO_FIELDORDER_UNDETERMINED } },
525     { MATROSKA_ID_VIDEOSTEREOMODE,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, stereo_mode), { .u = MATROSKA_VIDEO_STEREOMODE_TYPE_NB } },
526     { MATROSKA_ID_VIDEOASPECTRATIO,    EBML_NONE },
527     CHILD_OF(matroska_track)
528 };
529 
530 static EbmlSyntax matroska_track_audio[] = {
531     { MATROSKA_ID_AUDIOSAMPLINGFREQ,    EBML_FLOAT, 0, 0, offsetof(MatroskaTrackAudio, samplerate), { .f = 8000.0 } },
532     { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackAudio, out_samplerate) },
533     { MATROSKA_ID_AUDIOBITDEPTH,        EBML_UINT,  0, 0, offsetof(MatroskaTrackAudio, bitdepth) },
534     { MATROSKA_ID_AUDIOCHANNELS,        EBML_UINT,  0, 0, offsetof(MatroskaTrackAudio, channels),   { .u = 1 } },
535     CHILD_OF(matroska_track)
536 };
537 
538 static EbmlSyntax matroska_track_encoding_compression[] = {
539     { MATROSKA_ID_ENCODINGCOMPALGO,     EBML_UINT, 0, 0, offsetof(MatroskaTrackCompression, algo), { .u = MATROSKA_TRACK_ENCODING_COMP_ZLIB } },
540     { MATROSKA_ID_ENCODINGCOMPSETTINGS, EBML_BIN,  0, 0, offsetof(MatroskaTrackCompression, settings) },
541     CHILD_OF(matroska_track_encoding)
542 };
543 
544 static EbmlSyntax matroska_track_encoding_encryption[] = {
545     { MATROSKA_ID_ENCODINGENCALGO,        EBML_UINT, 0, 0, offsetof(MatroskaTrackEncryption,algo), {.u = 0} },
546     { MATROSKA_ID_ENCODINGENCKEYID,       EBML_BIN, 0, 0, offsetof(MatroskaTrackEncryption,key_id) },
547     { MATROSKA_ID_ENCODINGENCAESSETTINGS, EBML_NONE },
548     { MATROSKA_ID_ENCODINGSIGALGO,        EBML_NONE },
549     { MATROSKA_ID_ENCODINGSIGHASHALGO,    EBML_NONE },
550     { MATROSKA_ID_ENCODINGSIGKEYID,       EBML_NONE },
551     { MATROSKA_ID_ENCODINGSIGNATURE,      EBML_NONE },
552     CHILD_OF(matroska_track_encoding)
553 };
554 static EbmlSyntax matroska_track_encoding[] = {
555     { MATROSKA_ID_ENCODINGSCOPE,       EBML_UINT, 0, 0, offsetof(MatroskaTrackEncoding, scope),       { .u = 1 } },
556     { MATROSKA_ID_ENCODINGTYPE,        EBML_UINT, 0, 0, offsetof(MatroskaTrackEncoding, type),        { .u = 0 } },
557     { MATROSKA_ID_ENCODINGCOMPRESSION, EBML_NEST, 0, 0, offsetof(MatroskaTrackEncoding, compression), { .n = matroska_track_encoding_compression } },
558     { MATROSKA_ID_ENCODINGENCRYPTION,  EBML_NEST, 0, 0, offsetof(MatroskaTrackEncoding, encryption),  { .n = matroska_track_encoding_encryption } },
559     { MATROSKA_ID_ENCODINGORDER,       EBML_NONE },
560     CHILD_OF(matroska_track_encodings)
561 };
562 
563 static EbmlSyntax matroska_track_encodings[] = {
564     { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, 0, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack, encodings), { .n = matroska_track_encoding } },
565     CHILD_OF(matroska_track)
566 };
567 
568 static EbmlSyntax matroska_track_plane[] = {
569     { MATROSKA_ID_TRACKPLANEUID,  EBML_UINT, 0, 0, offsetof(MatroskaTrackPlane,uid) },
570     { MATROSKA_ID_TRACKPLANETYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackPlane,type) },
571     CHILD_OF(matroska_track_combine_planes)
572 };
573 
574 static EbmlSyntax matroska_track_combine_planes[] = {
575     { MATROSKA_ID_TRACKPLANE, EBML_NEST, 0, sizeof(MatroskaTrackPlane), offsetof(MatroskaTrackOperation,combine_planes), {.n = matroska_track_plane} },
576     CHILD_OF(matroska_track_operation)
577 };
578 
579 static EbmlSyntax matroska_track_operation[] = {
580     { MATROSKA_ID_TRACKCOMBINEPLANES, EBML_NEST, 0, 0, 0, {.n = matroska_track_combine_planes} },
581     CHILD_OF(matroska_track)
582 };
583 
584 static EbmlSyntax matroska_block_addition_mapping[] = {
585     { MATROSKA_ID_BLKADDIDVALUE,      EBML_UINT, 0, 0, offsetof(MatroskaBlockAdditionMapping, value) },
586     { MATROSKA_ID_BLKADDIDNAME,       EBML_STR,  0, 0, offsetof(MatroskaBlockAdditionMapping, name) },
587     { MATROSKA_ID_BLKADDIDTYPE,       EBML_UINT, 0, 0, offsetof(MatroskaBlockAdditionMapping, type) },
588     { MATROSKA_ID_BLKADDIDEXTRADATA,  EBML_BIN,  0, 0, offsetof(MatroskaBlockAdditionMapping, extradata) },
589     CHILD_OF(matroska_track)
590 };
591 
592 static EbmlSyntax matroska_track[] = {
593     { MATROSKA_ID_TRACKNUMBER,           EBML_UINT,  0, 0, offsetof(MatroskaTrack, num) },
594     { MATROSKA_ID_TRACKNAME,             EBML_UTF8,  0, 0, offsetof(MatroskaTrack, name) },
595     { MATROSKA_ID_TRACKUID,              EBML_UINT,  0, 0, offsetof(MatroskaTrack, uid) },
596     { MATROSKA_ID_TRACKTYPE,             EBML_UINT,  0, 0, offsetof(MatroskaTrack, type) },
597     { MATROSKA_ID_CODECID,               EBML_STR,   0, 0, offsetof(MatroskaTrack, codec_id) },
598     { MATROSKA_ID_CODECPRIVATE,          EBML_BIN,   0, 0, offsetof(MatroskaTrack, codec_priv) },
599     { MATROSKA_ID_CODECDELAY,            EBML_UINT,  0, 0, offsetof(MatroskaTrack, codec_delay),  { .u = 0 } },
600     { MATROSKA_ID_TRACKLANGUAGE,         EBML_STR,   0, 0, offsetof(MatroskaTrack, language),     { .s = "eng" } },
601     { MATROSKA_ID_TRACKDEFAULTDURATION,  EBML_UINT,  0, 0, offsetof(MatroskaTrack, default_duration) },
602     { MATROSKA_ID_TRACKTIMECODESCALE,    EBML_FLOAT, 0, 0, offsetof(MatroskaTrack, time_scale),   { .f = 1.0 } },
603     { MATROSKA_ID_TRACKFLAGCOMMENTARY,   EBML_UINT,  0, 0, offsetof(MatroskaTrack, flag_comment), { .u = 0 } },
604     { MATROSKA_ID_TRACKFLAGDEFAULT,      EBML_UINT,  0, 0, offsetof(MatroskaTrack, flag_default), { .u = 1 } },
605     { MATROSKA_ID_TRACKFLAGFORCED,       EBML_UINT,  0, 0, offsetof(MatroskaTrack, flag_forced),  { .u = 0 } },
606     { MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_hearingimpaired), { .u = 0 } },
607     { MATROSKA_ID_TRACKFLAGVISUALIMPAIRED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_visualimpaired), { .u = 0 } },
608     { MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_textdescriptions), { .u = 0 } },
609     { MATROSKA_ID_TRACKFLAGORIGINAL,     EBML_UINT,  1, 0, offsetof(MatroskaTrack, flag_original), {.u = 0 } },
610     { MATROSKA_ID_TRACKVIDEO,            EBML_NEST,  0, 0, offsetof(MatroskaTrack, video),        { .n = matroska_track_video } },
611     { MATROSKA_ID_TRACKAUDIO,            EBML_NEST,  0, 0, offsetof(MatroskaTrack, audio),        { .n = matroska_track_audio } },
612     { MATROSKA_ID_TRACKOPERATION,        EBML_NEST,  0, 0, offsetof(MatroskaTrack, operation),    { .n = matroska_track_operation } },
613     { MATROSKA_ID_TRACKCONTENTENCODINGS, EBML_NEST,  0, 0, 0,                                     { .n = matroska_track_encodings } },
614     { MATROSKA_ID_TRACKMAXBLKADDID,      EBML_UINT,  0, 0, offsetof(MatroskaTrack, max_block_additional_id), { .u = 0 } },
615     { MATROSKA_ID_TRACKBLKADDMAPPING,    EBML_NEST,  0, sizeof(MatroskaBlockAdditionMapping), offsetof(MatroskaTrack, block_addition_mappings), { .n = matroska_block_addition_mapping } },
616     { MATROSKA_ID_SEEKPREROLL,           EBML_UINT,  0, 0, offsetof(MatroskaTrack, seek_preroll), { .u = 0 } },
617     { MATROSKA_ID_TRACKFLAGENABLED,      EBML_NONE },
618     { MATROSKA_ID_TRACKFLAGLACING,       EBML_NONE },
619     { MATROSKA_ID_CODECNAME,             EBML_NONE },
620     { MATROSKA_ID_CODECDECODEALL,        EBML_NONE },
621     { MATROSKA_ID_CODECINFOURL,          EBML_NONE },
622     { MATROSKA_ID_CODECDOWNLOADURL,      EBML_NONE },
623     { MATROSKA_ID_TRACKMINCACHE,         EBML_NONE },
624     { MATROSKA_ID_TRACKMAXCACHE,         EBML_NONE },
625     CHILD_OF(matroska_tracks)
626 };
627 
628 static EbmlSyntax matroska_tracks[] = {
629     { MATROSKA_ID_TRACKENTRY, EBML_NEST, 0, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext, tracks), { .n = matroska_track } },
630     CHILD_OF(matroska_segment)
631 };
632 
633 static EbmlSyntax matroska_attachment[] = {
634     { MATROSKA_ID_FILEUID,      EBML_UINT, 0, 0, offsetof(MatroskaAttachment, uid) },
635     { MATROSKA_ID_FILENAME,     EBML_UTF8, 0, 0, offsetof(MatroskaAttachment, filename) },
636     { MATROSKA_ID_FILEMIMETYPE, EBML_STR,  0, 0, offsetof(MatroskaAttachment, mime) },
637     { MATROSKA_ID_FILEDATA,     EBML_BIN,  0, 0, offsetof(MatroskaAttachment, bin) },
638     { MATROSKA_ID_FILEDESC,     EBML_UTF8, 0, 0, offsetof(MatroskaAttachment, description) },
639     CHILD_OF(matroska_attachments)
640 };
641 
642 static EbmlSyntax matroska_attachments[] = {
643     { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, 0, sizeof(MatroskaAttachment), offsetof(MatroskaDemuxContext, attachments), { .n = matroska_attachment } },
644     CHILD_OF(matroska_segment)
645 };
646 
647 static EbmlSyntax matroska_chapter_display[] = {
648     { MATROSKA_ID_CHAPSTRING,  EBML_UTF8, 0, 0, offsetof(MatroskaChapter, title) },
649     { MATROSKA_ID_CHAPLANG,    EBML_NONE },
650     { MATROSKA_ID_CHAPCOUNTRY, EBML_NONE },
651     CHILD_OF(matroska_chapter_entry)
652 };
653 
654 static EbmlSyntax matroska_chapter_entry[] = {
655     { MATROSKA_ID_CHAPTERTIMESTART,   EBML_UINT, 0, 0, offsetof(MatroskaChapter, start), { .u = AV_NOPTS_VALUE } },
656     { MATROSKA_ID_CHAPTERTIMEEND,     EBML_UINT, 0, 0, offsetof(MatroskaChapter, end),   { .u = AV_NOPTS_VALUE } },
657     { MATROSKA_ID_CHAPTERUID,         EBML_UINT, 0, 0, offsetof(MatroskaChapter, uid) },
658     { MATROSKA_ID_CHAPTERDISPLAY,     EBML_NEST, 0, 0,                        0,         { .n = matroska_chapter_display } },
659     { MATROSKA_ID_CHAPTERFLAGHIDDEN,  EBML_NONE },
660     { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
661     { MATROSKA_ID_CHAPTERPHYSEQUIV,   EBML_NONE },
662     { MATROSKA_ID_CHAPTERATOM,        EBML_NONE },
663     CHILD_OF(matroska_chapter)
664 };
665 
666 static EbmlSyntax matroska_chapter[] = {
667     { MATROSKA_ID_CHAPTERATOM,        EBML_NEST, 0, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext, chapters), { .n = matroska_chapter_entry } },
668     { MATROSKA_ID_EDITIONUID,         EBML_NONE },
669     { MATROSKA_ID_EDITIONFLAGHIDDEN,  EBML_NONE },
670     { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
671     { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
672     CHILD_OF(matroska_chapters)
673 };
674 
675 static EbmlSyntax matroska_chapters[] = {
676     { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, 0, { .n = matroska_chapter } },
677     CHILD_OF(matroska_segment)
678 };
679 
680 static EbmlSyntax matroska_index_pos[] = {
681     { MATROSKA_ID_CUETRACK,           EBML_UINT, 0, 0, offsetof(MatroskaIndexPos, track) },
682     { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, 0, offsetof(MatroskaIndexPos, pos) },
683     { MATROSKA_ID_CUERELATIVEPOSITION,EBML_NONE },
684     { MATROSKA_ID_CUEDURATION,        EBML_NONE },
685     { MATROSKA_ID_CUEBLOCKNUMBER,     EBML_NONE },
686     CHILD_OF(matroska_index_entry)
687 };
688 
689 static EbmlSyntax matroska_index_entry[] = {
690     { MATROSKA_ID_CUETIME,          EBML_UINT, 0, 0,                        offsetof(MatroskaIndex, time) },
691     { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, 0, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex, pos), { .n = matroska_index_pos } },
692     CHILD_OF(matroska_index)
693 };
694 
695 static EbmlSyntax matroska_index[] = {
696     { MATROSKA_ID_POINTENTRY, EBML_NEST, 0, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext, index), { .n = matroska_index_entry } },
697     CHILD_OF(matroska_segment)
698 };
699 
700 static EbmlSyntax matroska_simpletag[] = {
701     { MATROSKA_ID_TAGNAME,        EBML_UTF8, 0, 0,                   offsetof(MatroskaTag, name) },
702     { MATROSKA_ID_TAGSTRING,      EBML_UTF8, 0, 0,                   offsetof(MatroskaTag, string) },
703     { MATROSKA_ID_TAGLANG,        EBML_STR,  0, 0,                   offsetof(MatroskaTag, lang), { .s = "und" } },
704     { MATROSKA_ID_TAGDEFAULT,     EBML_UINT, 0, 0,                   offsetof(MatroskaTag, def) },
705     { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0, 0,                   offsetof(MatroskaTag, def) },
706     { MATROSKA_ID_SIMPLETAG,      EBML_NEST, 0, sizeof(MatroskaTag), offsetof(MatroskaTag, sub),  { .n = matroska_simpletag } },
707     CHILD_OF(matroska_tag)
708 };
709 
710 static EbmlSyntax matroska_tagtargets[] = {
711     { MATROSKA_ID_TAGTARGETS_TYPE,       EBML_STR,  0, 0, offsetof(MatroskaTagTarget, type) },
712     { MATROSKA_ID_TAGTARGETS_TYPEVALUE,  EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, typevalue),  { .u = 50 } },
713     { MATROSKA_ID_TAGTARGETS_TRACKUID,   EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, trackuid),   { .u = 0 } },
714     { MATROSKA_ID_TAGTARGETS_CHAPTERUID, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, chapteruid), { .u = 0 } },
715     { MATROSKA_ID_TAGTARGETS_ATTACHUID,  EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, attachuid),  { .u = 0 } },
716     CHILD_OF(matroska_tag)
717 };
718 
719 static EbmlSyntax matroska_tag[] = {
720     { MATROSKA_ID_SIMPLETAG,  EBML_NEST, 0, sizeof(MatroskaTag), offsetof(MatroskaTags, tag),    { .n = matroska_simpletag } },
721     { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, 0,                   offsetof(MatroskaTags, target), { .n = matroska_tagtargets } },
722     CHILD_OF(matroska_tags)
723 };
724 
725 static EbmlSyntax matroska_tags[] = {
726     { MATROSKA_ID_TAG, EBML_NEST, 0, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext, tags), { .n = matroska_tag } },
727     CHILD_OF(matroska_segment)
728 };
729 
730 static EbmlSyntax matroska_seekhead_entry[] = {
731     { MATROSKA_ID_SEEKID,       EBML_UINT, 0, 0, offsetof(MatroskaSeekhead, id) },
732     { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, 0, offsetof(MatroskaSeekhead, pos), { .u = -1 } },
733     CHILD_OF(matroska_seekhead)
734 };
735 
736 static EbmlSyntax matroska_seekhead[] = {
737     { MATROSKA_ID_SEEKENTRY, EBML_NEST, 0, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext, seekhead), { .n = matroska_seekhead_entry } },
738     CHILD_OF(matroska_segment)
739 };
740 
741 static EbmlSyntax matroska_segment[] = {
742     { MATROSKA_ID_CLUSTER,     EBML_STOP },
743     { MATROSKA_ID_INFO,        EBML_LEVEL1, 0, 0, 0, { .n = matroska_info } },
744     { MATROSKA_ID_TRACKS,      EBML_LEVEL1, 0, 0, 0, { .n = matroska_tracks } },
745     { MATROSKA_ID_ATTACHMENTS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_attachments } },
746     { MATROSKA_ID_CHAPTERS,    EBML_LEVEL1, 0, 0, 0, { .n = matroska_chapters } },
747     { MATROSKA_ID_CUES,        EBML_LEVEL1, 0, 0, 0, { .n = matroska_index } },
748     { MATROSKA_ID_TAGS,        EBML_LEVEL1, 0, 0, 0, { .n = matroska_tags } },
749     { MATROSKA_ID_SEEKHEAD,    EBML_LEVEL1, 0, 0, 0, { .n = matroska_seekhead } },
750     { 0 }   /* We don't want to go back to level 0, so don't add the parent. */
751 };
752 
753 static EbmlSyntax matroska_segments[] = {
754     { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, 0, { .n = matroska_segment } },
755     { 0 }
756 };
757 
758 static EbmlSyntax matroska_blockmore[] = {
759     { MATROSKA_ID_BLOCKADDID,      EBML_UINT, 0, 0, offsetof(MatroskaBlock,additional_id), { .u = 1 } },
760     { MATROSKA_ID_BLOCKADDITIONAL, EBML_BIN,  0, 0, offsetof(MatroskaBlock,additional) },
761     CHILD_OF(matroska_blockadditions)
762 };
763 
764 static EbmlSyntax matroska_blockadditions[] = {
765     { MATROSKA_ID_BLOCKMORE, EBML_NEST, 0, 0, 0, {.n = matroska_blockmore} },
766     CHILD_OF(matroska_blockgroup)
767 };
768 
769 static EbmlSyntax matroska_blockgroup[] = {
770     { MATROSKA_ID_BLOCK,          EBML_BIN,  0, 0, offsetof(MatroskaBlock, bin) },
771     { MATROSKA_ID_BLOCKADDITIONS, EBML_NEST, 0, 0, 0, { .n = matroska_blockadditions} },
772     { MATROSKA_ID_BLOCKDURATION,  EBML_UINT, 0, 0, offsetof(MatroskaBlock, duration) },
773     { MATROSKA_ID_DISCARDPADDING, EBML_SINT, 0, 0, offsetof(MatroskaBlock, discard_padding) },
774     { MATROSKA_ID_BLOCKREFERENCE, EBML_SINT, 1, 0, offsetof(MatroskaBlock, reference) },
775     { MATROSKA_ID_CODECSTATE,     EBML_NONE },
776     {                          1, EBML_UINT, 0, 0, offsetof(MatroskaBlock, non_simple), { .u = 1 } },
777     CHILD_OF(matroska_cluster_parsing)
778 };
779 
780 // The following array contains SimpleBlock and BlockGroup twice
781 // in order to reuse the other values for matroska_cluster_enter.
782 static EbmlSyntax matroska_cluster_parsing[] = {
783     { MATROSKA_ID_SIMPLEBLOCK,     EBML_BIN,  0, 0, offsetof(MatroskaBlock, bin) },
784     { MATROSKA_ID_BLOCKGROUP,      EBML_NEST, 0, 0, 0, { .n = matroska_blockgroup } },
785     { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, 0, offsetof(MatroskaCluster, timecode) },
786     { MATROSKA_ID_SIMPLEBLOCK,     EBML_STOP },
787     { MATROSKA_ID_BLOCKGROUP,      EBML_STOP },
788     { MATROSKA_ID_CLUSTERPOSITION, EBML_NONE },
789     { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
790     CHILD_OF(matroska_segment)
791 };
792 
793 static EbmlSyntax matroska_cluster_enter[] = {
794     { MATROSKA_ID_CLUSTER,     EBML_NEST, 0, 0, 0, { .n = &matroska_cluster_parsing[2] } },
795     { 0 }
796 };
797 #undef CHILD_OF
798 
799 static const CodecMime mkv_image_mime_tags[] = {
800     {"image/gif"                  , AV_CODEC_ID_GIF},
801     {"image/jpeg"                 , AV_CODEC_ID_MJPEG},
802     {"image/png"                  , AV_CODEC_ID_PNG},
803     {"image/tiff"                 , AV_CODEC_ID_TIFF},
804 
805     {""                           , AV_CODEC_ID_NONE}
806 };
807 
808 static const CodecMime mkv_mime_tags[] = {
809     {"application/x-truetype-font", AV_CODEC_ID_TTF},
810     {"application/x-font"         , AV_CODEC_ID_TTF},
811     {"application/vnd.ms-opentype", AV_CODEC_ID_OTF},
812     {"binary"                     , AV_CODEC_ID_BIN_DATA},
813 
814     {""                           , AV_CODEC_ID_NONE}
815 };
816 
817 static const char *const matroska_doctypes[] = { "matroska", "webm" };
818 
819 /*
820  * This function prepares the status for parsing of level 1 elements.
821  */
matroska_reset_status(MatroskaDemuxContext *matroska, uint32_t id, int64_t position)822 static int matroska_reset_status(MatroskaDemuxContext *matroska,
823                                  uint32_t id, int64_t position)
824 {
825     int64_t err = 0;
826     if (position >= 0) {
827         err = avio_seek(matroska->ctx->pb, position, SEEK_SET);
828         if (err > 0)
829             err = 0;
830     } else
831         position = avio_tell(matroska->ctx->pb);
832 
833     matroska->current_id    = id;
834     matroska->num_levels    = 1;
835     matroska->unknown_count = 0;
836     matroska->resync_pos    = position;
837     if (id)
838         matroska->resync_pos -= (av_log2(id) + 7) / 8;
839 
840     return err;
841 }
842 
matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos)843 static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos)
844 {
845     AVIOContext *pb = matroska->ctx->pb;
846     uint32_t id;
847 
848     /* Try to seek to the last position to resync from. If this doesn't work,
849      * we resync from the earliest position available: The start of the buffer. */
850     if (last_pos < avio_tell(pb) && avio_seek(pb, last_pos + 1, SEEK_SET) < 0) {
851         av_log(matroska->ctx, AV_LOG_WARNING,
852                "Seek to desired resync point failed. Seeking to "
853                "earliest point available instead.\n");
854         avio_seek(pb, FFMAX(avio_tell(pb) + (pb->buffer - pb->buf_ptr),
855                             last_pos + 1), SEEK_SET);
856     }
857 
858     id = avio_rb32(pb);
859 
860     // try to find a toplevel element
861     while (!avio_feof(pb)) {
862         if (id == MATROSKA_ID_INFO     || id == MATROSKA_ID_TRACKS      ||
863             id == MATROSKA_ID_CUES     || id == MATROSKA_ID_TAGS        ||
864             id == MATROSKA_ID_SEEKHEAD || id == MATROSKA_ID_ATTACHMENTS ||
865             id == MATROSKA_ID_CLUSTER  || id == MATROSKA_ID_CHAPTERS) {
866             /* Prepare the context for parsing of a level 1 element. */
867             matroska_reset_status(matroska, id, -1);
868             /* Given that we are here means that an error has occurred,
869              * so treat the segment as unknown length in order not to
870              * discard valid data that happens to be beyond the designated
871              * end of the segment. */
872             matroska->levels[0].length = EBML_UNKNOWN_LENGTH;
873             return 0;
874         }
875         id = (id << 8) | avio_r8(pb);
876     }
877 
878     matroska->done = 1;
879     return pb->error ? pb->error : AVERROR_EOF;
880 }
881 
882 /*
883  * Read: an "EBML number", which is defined as a variable-length
884  * array of bytes. The first byte indicates the length by giving a
885  * number of 0-bits followed by a one. The position of the first
886  * "one" bit inside the first byte indicates the length of this
887  * number.
888  * Returns: number of bytes read, < 0 on error
889  */
ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb, int max_size, uint64_t *number, int eof_forbidden)890 static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb,
891                          int max_size, uint64_t *number, int eof_forbidden)
892 {
893     int read, n = 1;
894     uint64_t total;
895     int64_t pos;
896 
897     /* The first byte tells us the length in bytes - except when it is zero. */
898     total = avio_r8(pb);
899     if (pb->eof_reached)
900         goto err;
901 
902     /* get the length of the EBML number */
903     read = 8 - ff_log2_tab[total];
904 
905     if (!total || read > max_size) {
906         pos = avio_tell(pb) - 1;
907         if (!total) {
908             av_log(matroska->ctx, AV_LOG_ERROR,
909                    "0x00 at pos %"PRId64" (0x%"PRIx64") invalid as first byte "
910                    "of an EBML number\n", pos, pos);
911         } else {
912             av_log(matroska->ctx, AV_LOG_ERROR,
913                    "Length %d indicated by an EBML number's first byte 0x%02x "
914                    "at pos %"PRId64" (0x%"PRIx64") exceeds max length %d.\n",
915                    read, (uint8_t) total, pos, pos, max_size);
916         }
917         return AVERROR_INVALIDDATA;
918     }
919 
920     /* read out length */
921     total ^= 1 << ff_log2_tab[total];
922     while (n++ < read)
923         total = (total << 8) | avio_r8(pb);
924 
925     if (pb->eof_reached) {
926         eof_forbidden = 1;
927         goto err;
928     }
929 
930     *number = total;
931 
932     return read;
933 
934 err:
935     pos = avio_tell(pb);
936     if (pb->error) {
937         av_log(matroska->ctx, AV_LOG_ERROR,
938                "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
939                pos, pos);
940         return pb->error;
941     }
942     if (eof_forbidden) {
943         av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely "
944                "at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
945         return AVERROR(EIO);
946     }
947     return AVERROR_EOF;
948 }
949 
950 /**
951  * Read a EBML length value.
952  * This needs special handling for the "unknown length" case which has multiple
953  * encodings.
954  */
ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb, uint64_t *number)955 static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
956                             uint64_t *number)
957 {
958     int res = ebml_read_num(matroska, pb, 8, number, 1);
959     if (res > 0 && *number + 1 == 1ULL << (7 * res))
960         *number = EBML_UNKNOWN_LENGTH;
961     return res;
962 }
963 
964 /*
965  * Read the next element as an unsigned int.
966  * Returns NEEDS_CHECKING unless size == 0.
967  */
ebml_read_uint(AVIOContext *pb, int size, uint64_t default_value, uint64_t *num)968 static int ebml_read_uint(AVIOContext *pb, int size,
969                           uint64_t default_value, uint64_t *num)
970 {
971     int n = 0;
972 
973     if (size == 0) {
974         *num = default_value;
975         return 0;
976     }
977     /* big-endian ordering; build up number */
978     *num = 0;
979     while (n++ < size)
980         *num = (*num << 8) | avio_r8(pb);
981 
982     return NEEDS_CHECKING;
983 }
984 
985 /*
986  * Read the next element as a signed int.
987  * Returns NEEDS_CHECKING unless size == 0.
988  */
ebml_read_sint(AVIOContext *pb, int size, int64_t default_value, int64_t *num)989 static int ebml_read_sint(AVIOContext *pb, int size,
990                           int64_t default_value, int64_t *num)
991 {
992     int n = 1;
993 
994     if (size == 0) {
995         *num = default_value;
996         return 0;
997     } else {
998         *num = sign_extend(avio_r8(pb), 8);
999 
1000         /* big-endian ordering; build up number */
1001         while (n++ < size)
1002             *num = ((uint64_t)*num << 8) | avio_r8(pb);
1003     }
1004 
1005     return NEEDS_CHECKING;
1006 }
1007 
1008 /*
1009  * Read the next element as a float.
1010  * Returns 0 if size == 0, NEEDS_CHECKING or < 0 on obvious failure.
1011  */
ebml_read_float(AVIOContext *pb, int size, double default_value, double *num)1012 static int ebml_read_float(AVIOContext *pb, int size,
1013                            double default_value, double *num)
1014 {
1015     if (size == 0) {
1016         *num = default_value;
1017         return 0;
1018     } else if (size == 4) {
1019         *num = av_int2float(avio_rb32(pb));
1020     } else if (size == 8) {
1021         *num = av_int2double(avio_rb64(pb));
1022     } else
1023         return AVERROR_INVALIDDATA;
1024 
1025     return NEEDS_CHECKING;
1026 }
1027 
1028 /*
1029  * Read the next element as an ASCII string.
1030  * 0 is success, < 0 or NEEDS_CHECKING is failure.
1031  */
ebml_read_ascii(AVIOContext *pb, int size, const char *default_value, char **str)1032 static int ebml_read_ascii(AVIOContext *pb, int size,
1033                            const char *default_value, char **str)
1034 {
1035     char *res;
1036     int ret;
1037 
1038     if (size == 0 && default_value) {
1039         res = av_strdup(default_value);
1040         if (!res)
1041             return AVERROR(ENOMEM);
1042     } else {
1043         /* EBML strings are usually not 0-terminated, so we allocate one
1044          * byte more, read the string and NUL-terminate it ourselves. */
1045         if (!(res = av_malloc(size + 1)))
1046             return AVERROR(ENOMEM);
1047         if ((ret = avio_read(pb, (uint8_t *) res, size)) != size) {
1048             av_free(res);
1049             return ret < 0 ? ret : NEEDS_CHECKING;
1050         }
1051         (res)[size] = '\0';
1052     }
1053     av_free(*str);
1054     *str = res;
1055 
1056     return 0;
1057 }
1058 
1059 /*
1060  * Read the next element as binary data.
1061  * 0 is success, < 0 or NEEDS_CHECKING is failure.
1062  */
ebml_read_binary(AVIOContext *pb, int length, int64_t pos, EbmlBin *bin)1063 static int ebml_read_binary(AVIOContext *pb, int length,
1064                             int64_t pos, EbmlBin *bin)
1065 {
1066     int ret;
1067 
1068     ret = av_buffer_realloc(&bin->buf, length + AV_INPUT_BUFFER_PADDING_SIZE);
1069     if (ret < 0)
1070         return ret;
1071     memset(bin->buf->data + length, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1072 
1073     bin->data = bin->buf->data;
1074     bin->size = length;
1075     bin->pos  = pos;
1076     if ((ret = avio_read(pb, bin->data, length)) != length) {
1077         av_buffer_unref(&bin->buf);
1078         bin->data = NULL;
1079         bin->size = 0;
1080         return ret < 0 ? ret : NEEDS_CHECKING;
1081     }
1082 
1083     return 0;
1084 }
1085 
1086 /*
1087  * Read the next element, but only the header. The contents
1088  * are supposed to be sub-elements which can be read separately.
1089  * 0 is success, < 0 is failure.
1090  */
ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length, int64_t pos)1091 static int ebml_read_master(MatroskaDemuxContext *matroska,
1092                             uint64_t length, int64_t pos)
1093 {
1094     MatroskaLevel *level;
1095 
1096     if (matroska->num_levels >= EBML_MAX_DEPTH) {
1097         av_log(matroska->ctx, AV_LOG_ERROR,
1098                "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
1099         return AVERROR(ENOSYS);
1100     }
1101 
1102     level         = &matroska->levels[matroska->num_levels++];
1103     level->start  = pos;
1104     level->length = length;
1105 
1106     return 0;
1107 }
1108 
1109 /*
1110  * Read a signed "EBML number"
1111  * Return: number of bytes processed, < 0 on error
1112  */
matroska_ebmlnum_sint(MatroskaDemuxContext *matroska, AVIOContext *pb, int64_t *num)1113 static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska,
1114                                  AVIOContext *pb, int64_t *num)
1115 {
1116     uint64_t unum;
1117     int res;
1118 
1119     /* read as unsigned number first */
1120     if ((res = ebml_read_num(matroska, pb, 8, &unum, 1)) < 0)
1121         return res;
1122 
1123     /* make signed (weird way) */
1124     *num = unum - ((1LL << (7 * res - 1)) - 1);
1125 
1126     return res;
1127 }
1128 
1129 static int ebml_parse(MatroskaDemuxContext *matroska,
1130                       EbmlSyntax *syntax, void *data);
1131 
ebml_parse_id(EbmlSyntax *syntax, uint32_t id)1132 static EbmlSyntax *ebml_parse_id(EbmlSyntax *syntax, uint32_t id)
1133 {
1134     int i;
1135 
1136     // Whoever touches this should be aware of the duplication
1137     // existing in matroska_cluster_parsing.
1138     for (i = 0; syntax[i].id; i++)
1139         if (id == syntax[i].id)
1140             break;
1141 
1142     return &syntax[i];
1143 }
1144 
ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, void *data)1145 static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
1146                            void *data)
1147 {
1148     int res;
1149 
1150     if (data) {
1151         for (int i = 0; syntax[i].id; i++) {
1152             void *dst = (char *)data + syntax[i].data_offset;
1153             switch (syntax[i].type) {
1154             case EBML_UINT:
1155                 *(uint64_t *)dst = syntax[i].def.u;
1156                 break;
1157             case EBML_SINT:
1158                 *(int64_t *) dst = syntax[i].def.i;
1159                 break;
1160             case EBML_FLOAT:
1161                 *(double *)  dst = syntax[i].def.f;
1162                 break;
1163             case EBML_STR:
1164             case EBML_UTF8:
1165                 // the default may be NULL
1166                 if (syntax[i].def.s) {
1167                     *(char**)dst = av_strdup(syntax[i].def.s);
1168                     if (!*(char**)dst)
1169                         return AVERROR(ENOMEM);
1170                 }
1171                 break;
1172             }
1173         }
1174 
1175         if (!matroska->levels[matroska->num_levels - 1].length) {
1176             matroska->num_levels--;
1177             return 0;
1178         }
1179     }
1180 
1181     do {
1182         res = ebml_parse(matroska, syntax, data);
1183     } while (!res);
1184 
1185     return res == LEVEL_ENDED ? 0 : res;
1186 }
1187 
is_ebml_id_valid(uint32_t id)1188 static int is_ebml_id_valid(uint32_t id)
1189 {
1190     // Due to endian nonsense in Matroska, the highest byte with any bits set
1191     // will contain the leading length bit. This bit in turn identifies the
1192     // total byte length of the element by its position within the byte.
1193     unsigned int bits = av_log2(id);
1194     return id && (bits + 7) / 8 ==  (8 - bits % 8);
1195 }
1196 
1197 /*
1198  * Allocate and return the entry for the level1 element with the given ID. If
1199  * an entry already exists, return the existing entry.
1200  */
matroska_find_level1_elem(MatroskaDemuxContext *matroska, uint32_t id, int64_t pos)1201 static MatroskaLevel1Element *matroska_find_level1_elem(MatroskaDemuxContext *matroska,
1202                                                         uint32_t id, int64_t pos)
1203 {
1204     int i;
1205     MatroskaLevel1Element *elem;
1206 
1207     if (!is_ebml_id_valid(id))
1208         return NULL;
1209 
1210     // Some files link to all clusters; useless.
1211     if (id == MATROSKA_ID_CLUSTER)
1212         return NULL;
1213 
1214     // There can be multiple SeekHeads and Tags.
1215     for (i = 0; i < matroska->num_level1_elems; i++) {
1216         if (matroska->level1_elems[i].id == id) {
1217             if (matroska->level1_elems[i].pos == pos ||
1218                 id != MATROSKA_ID_SEEKHEAD && id != MATROSKA_ID_TAGS)
1219                 return &matroska->level1_elems[i];
1220         }
1221     }
1222 
1223     // Only a completely broken file would have more elements.
1224     if (matroska->num_level1_elems >= FF_ARRAY_ELEMS(matroska->level1_elems)) {
1225         av_log(matroska->ctx, AV_LOG_ERROR, "Too many level1 elements.\n");
1226         return NULL;
1227     }
1228 
1229     elem = &matroska->level1_elems[matroska->num_level1_elems++];
1230     *elem = (MatroskaLevel1Element){.id = id};
1231 
1232     return elem;
1233 }
1234 
ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, void *data)1235 static int ebml_parse(MatroskaDemuxContext *matroska,
1236                       EbmlSyntax *syntax, void *data)
1237 {
1238     static const uint64_t max_lengths[EBML_TYPE_COUNT] = {
1239         // Forbid unknown-length EBML_NONE elements.
1240         [EBML_NONE]  = EBML_UNKNOWN_LENGTH - 1,
1241         [EBML_UINT]  = 8,
1242         [EBML_SINT]  = 8,
1243         [EBML_FLOAT] = 8,
1244         // max. 16 MB for strings
1245         [EBML_STR]   = 0x1000000,
1246         [EBML_UTF8]  = 0x1000000,
1247         // max. 256 MB for binary data
1248         [EBML_BIN]   = 0x10000000,
1249         // no limits for anything else
1250     };
1251     AVIOContext *pb = matroska->ctx->pb;
1252     uint32_t id;
1253     uint64_t length;
1254     int64_t pos = avio_tell(pb), pos_alt;
1255     int res, update_pos = 1, level_check;
1256     MatroskaLevel1Element *level1_elem;
1257     MatroskaLevel *level = matroska->num_levels ? &matroska->levels[matroska->num_levels - 1] : NULL;
1258 
1259     if (!matroska->current_id) {
1260         uint64_t id;
1261         res = ebml_read_num(matroska, pb, 4, &id, 0);
1262         if (res < 0) {
1263             if (pb->eof_reached && res == AVERROR_EOF) {
1264                 if (matroska->is_live)
1265                     // in live mode, finish parsing if EOF is reached.
1266                     return 1;
1267                 if (level && pos == avio_tell(pb)) {
1268                     if (level->length == EBML_UNKNOWN_LENGTH) {
1269                         // Unknown-length levels automatically end at EOF.
1270                         matroska->num_levels--;
1271                         return LEVEL_ENDED;
1272                     } else {
1273                         av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely "
1274                                "at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
1275                     }
1276                 }
1277             }
1278             return res;
1279         }
1280         matroska->current_id = id | 1 << 7 * res;
1281         pos_alt = pos + res;
1282     } else {
1283         pos_alt = pos;
1284         pos    -= (av_log2(matroska->current_id) + 7) / 8;
1285     }
1286 
1287     id = matroska->current_id;
1288 
1289     syntax = ebml_parse_id(syntax, id);
1290     if (!syntax->id && id != EBML_ID_VOID && id != EBML_ID_CRC32) {
1291         if (level && level->length == EBML_UNKNOWN_LENGTH) {
1292             // Unknown-length levels end when an element from an upper level
1293             // in the hierarchy is encountered.
1294             while (syntax->def.n) {
1295                 syntax = ebml_parse_id(syntax->def.n, id);
1296                 if (syntax->id) {
1297                     matroska->num_levels--;
1298                     return LEVEL_ENDED;
1299                 }
1300             };
1301         }
1302 
1303         av_log(matroska->ctx, AV_LOG_DEBUG, "Unknown entry 0x%"PRIX32" at pos. "
1304                                             "%"PRId64"\n", id, pos);
1305         update_pos = 0; /* Don't update resync_pos as an error might have happened. */
1306     }
1307 
1308     if (data) {
1309         data = (char *) data + syntax->data_offset;
1310         if (syntax->list_elem_size) {
1311             EbmlList *list = data;
1312             void *newelem;
1313 
1314             if ((unsigned)list->nb_elem + 1 >= UINT_MAX / syntax->list_elem_size)
1315                 return AVERROR(ENOMEM);
1316             newelem = av_fast_realloc(list->elem,
1317                                       &list->alloc_elem_size,
1318                                       (list->nb_elem + 1) * syntax->list_elem_size);
1319             if (!newelem)
1320                 return AVERROR(ENOMEM);
1321             list->elem = newelem;
1322             data = (char *) list->elem + list->nb_elem * syntax->list_elem_size;
1323             memset(data, 0, syntax->list_elem_size);
1324             list->nb_elem++;
1325         }
1326     }
1327 
1328     if (syntax->type != EBML_STOP) {
1329         matroska->current_id = 0;
1330         if ((res = ebml_read_length(matroska, pb, &length)) < 0)
1331             return res;
1332 
1333         pos_alt += res;
1334 
1335         if (matroska->num_levels > 0) {
1336             if (length != EBML_UNKNOWN_LENGTH &&
1337                 level->length != EBML_UNKNOWN_LENGTH) {
1338                 uint64_t elem_end = pos_alt + length,
1339                         level_end = level->start + level->length;
1340 
1341                 if (elem_end < level_end) {
1342                     level_check = 0;
1343                 } else if (elem_end == level_end) {
1344                     level_check = LEVEL_ENDED;
1345                 } else {
1346                     av_log(matroska->ctx, AV_LOG_ERROR,
1347                            "Element at 0x%"PRIx64" ending at 0x%"PRIx64" exceeds "
1348                            "containing master element ending at 0x%"PRIx64"\n",
1349                            pos, elem_end, level_end);
1350                     return AVERROR_INVALIDDATA;
1351                 }
1352             } else if (length != EBML_UNKNOWN_LENGTH) {
1353                 level_check = 0;
1354             } else if (level->length != EBML_UNKNOWN_LENGTH) {
1355                 av_log(matroska->ctx, AV_LOG_ERROR, "Unknown-sized element "
1356                        "at 0x%"PRIx64" inside parent with finite size\n", pos);
1357                 return AVERROR_INVALIDDATA;
1358             } else {
1359                 level_check = 0;
1360                 if (id != MATROSKA_ID_CLUSTER && (syntax->type == EBML_LEVEL1
1361                                               ||  syntax->type == EBML_NEST)) {
1362                     // According to the current specifications only clusters and
1363                     // segments are allowed to be unknown-length. We also accept
1364                     // other unknown-length master elements.
1365                     av_log(matroska->ctx, AV_LOG_WARNING,
1366                            "Found unknown-length element 0x%"PRIX32" other than "
1367                            "a cluster at 0x%"PRIx64". Spec-incompliant, but "
1368                            "parsing will nevertheless be attempted.\n", id, pos);
1369                     update_pos = -1;
1370                 }
1371             }
1372         } else
1373             level_check = 0;
1374 
1375         if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {
1376             if (length != EBML_UNKNOWN_LENGTH) {
1377                 av_log(matroska->ctx, AV_LOG_ERROR,
1378                        "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for element "
1379                        "with ID 0x%"PRIX32" at 0x%"PRIx64"\n",
1380                        length, max_lengths[syntax->type], id, pos);
1381             } else if (syntax->type != EBML_NONE) {
1382                 av_log(matroska->ctx, AV_LOG_ERROR,
1383                        "Element with ID 0x%"PRIX32" at pos. 0x%"PRIx64" has "
1384                        "unknown length, yet the length of an element of its "
1385                        "type must be known.\n", id, pos);
1386             } else {
1387                 av_log(matroska->ctx, AV_LOG_ERROR,
1388                        "Found unknown-length element with ID 0x%"PRIX32" at "
1389                        "pos. 0x%"PRIx64" for which no syntax for parsing is "
1390                        "available.\n", id, pos);
1391             }
1392             return AVERROR_INVALIDDATA;
1393         }
1394 
1395         if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1396             // Loosing sync will likely manifest itself as encountering unknown
1397             // elements which are not reliably distinguishable from elements
1398             // belonging to future extensions of the format.
1399             // We use a heuristic to detect such situations: If the current
1400             // element is not expected at the current syntax level and there
1401             // were only a few unknown elements in a row, then the element is
1402             // skipped or considered defective based upon the length of the
1403             // current element (i.e. how much would be skipped); if there were
1404             // more than a few skipped elements in a row and skipping the current
1405             // element would lead us more than SKIP_THRESHOLD away from the last
1406             // known good position, then it is inferred that an error occurred.
1407             // The dependency on the number of unknown elements in a row exists
1408             // because the distance to the last known good position is
1409             // automatically big if the last parsed element was big.
1410             // In both cases, each unknown element is considered equivalent to
1411             // UNKNOWN_EQUIV of skipped bytes for the check.
1412             // The whole check is only done for non-seekable output, because
1413             // in this situation skipped data can't simply be rechecked later.
1414             // This is especially important when using unkown length elements
1415             // as the check for whether a child exceeds its containing master
1416             // element is not effective in this situation.
1417             if (update_pos) {
1418                 matroska->unknown_count = 0;
1419             } else {
1420                 int64_t dist = length + UNKNOWN_EQUIV * matroska->unknown_count++;
1421 
1422                 if (matroska->unknown_count > 3)
1423                     dist += pos_alt - matroska->resync_pos;
1424 
1425                 if (dist > SKIP_THRESHOLD) {
1426                     av_log(matroska->ctx, AV_LOG_ERROR,
1427                            "Unknown element %"PRIX32" at pos. 0x%"PRIx64" with "
1428                            "length 0x%"PRIx64" considered as invalid data. Last "
1429                            "known good position 0x%"PRIx64", %d unknown elements"
1430                            " in a row\n", id, pos, length, matroska->resync_pos,
1431                            matroska->unknown_count);
1432                     return AVERROR_INVALIDDATA;
1433                 }
1434             }
1435         }
1436 
1437         if (update_pos > 0) {
1438             // We have found an element that is allowed at this place
1439             // in the hierarchy and it passed all checks, so treat the beginning
1440             // of the element as the "last known good" position.
1441             matroska->resync_pos = pos;
1442         }
1443 
1444         if (!data && length != EBML_UNKNOWN_LENGTH)
1445             goto skip;
1446     }
1447 
1448     switch (syntax->type) {
1449     case EBML_UINT:
1450         res = ebml_read_uint(pb, length, syntax->def.u, data);
1451         break;
1452     case EBML_SINT:
1453         res = ebml_read_sint(pb, length, syntax->def.i, data);
1454         break;
1455     case EBML_FLOAT:
1456         res = ebml_read_float(pb, length, syntax->def.f, data);
1457         break;
1458     case EBML_STR:
1459     case EBML_UTF8:
1460         res = ebml_read_ascii(pb, length, syntax->def.s, data);
1461         break;
1462     case EBML_BIN:
1463         res = ebml_read_binary(pb, length, pos_alt, data);
1464         break;
1465     case EBML_LEVEL1:
1466     case EBML_NEST:
1467         if ((res = ebml_read_master(matroska, length, pos_alt)) < 0)
1468             return res;
1469         if (id == MATROSKA_ID_SEGMENT)
1470             matroska->segment_start = pos_alt;
1471         if (id == MATROSKA_ID_CUES)
1472             matroska->cues_parsing_deferred = 0;
1473         if (syntax->type == EBML_LEVEL1 &&
1474             (level1_elem = matroska_find_level1_elem(matroska, syntax->id, pos))) {
1475             if (!level1_elem->pos) {
1476                 // Zero is not a valid position for a level 1 element.
1477                 level1_elem->pos = pos;
1478             } else if (level1_elem->pos != pos)
1479                 av_log(matroska->ctx, AV_LOG_ERROR, "Duplicate element\n");
1480             level1_elem->parsed = 1;
1481         }
1482         if (res = ebml_parse_nest(matroska, syntax->def.n, data))
1483             return res;
1484         break;
1485     case EBML_STOP:
1486         return 1;
1487     skip:
1488     default:
1489         if (length) {
1490             int64_t res2;
1491             if (ffio_limit(pb, length) != length) {
1492                 // ffio_limit emits its own error message,
1493                 // so we don't have to.
1494                 return AVERROR(EIO);
1495             }
1496             if ((res2 = avio_skip(pb, length - 1)) >= 0) {
1497                 // avio_skip might take us past EOF. We check for this
1498                 // by skipping only length - 1 bytes, reading a byte and
1499                 // checking the error flags. This is done in order to check
1500                 // that the element has been properly skipped even when
1501                 // no filesize (that ffio_limit relies on) is available.
1502                 avio_r8(pb);
1503                 res = NEEDS_CHECKING;
1504             } else
1505                 res = res2;
1506         } else
1507             res = 0;
1508     }
1509     if (res) {
1510         if (res == NEEDS_CHECKING) {
1511             if (pb->eof_reached) {
1512                 if (pb->error)
1513                     res = pb->error;
1514                 else
1515                     res = AVERROR_EOF;
1516             } else
1517                 goto level_check;
1518         }
1519 
1520         if (res == AVERROR_INVALIDDATA)
1521             av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");
1522         else if (res == AVERROR(EIO))
1523             av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");
1524         else if (res == AVERROR_EOF) {
1525             av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely\n");
1526             res = AVERROR(EIO);
1527         }
1528 
1529         return res;
1530     }
1531 
1532 level_check:
1533     if (syntax->is_counted && data) {
1534         CountedElement *elem = data;
1535         if (elem->count != UINT_MAX)
1536             elem->count++;
1537     }
1538 
1539     if (level_check == LEVEL_ENDED && matroska->num_levels) {
1540         level = &matroska->levels[matroska->num_levels - 1];
1541         pos   = avio_tell(pb);
1542 
1543         // Given that pos >= level->start no check for
1544         // level->length != EBML_UNKNOWN_LENGTH is necessary.
1545         while (matroska->num_levels && pos == level->start + level->length) {
1546             matroska->num_levels--;
1547             level--;
1548         }
1549     }
1550 
1551     return level_check;
1552 }
1553 
ebml_free(EbmlSyntax *syntax, void *data)1554 static void ebml_free(EbmlSyntax *syntax, void *data)
1555 {
1556     int i, j;
1557     for (i = 0; syntax[i].id; i++) {
1558         void *data_off = (char *) data + syntax[i].data_offset;
1559         switch (syntax[i].type) {
1560         case EBML_STR:
1561         case EBML_UTF8:
1562             av_freep(data_off);
1563             break;
1564         case EBML_BIN:
1565             av_buffer_unref(&((EbmlBin *) data_off)->buf);
1566             break;
1567         case EBML_LEVEL1:
1568         case EBML_NEST:
1569             if (syntax[i].list_elem_size) {
1570                 EbmlList *list = data_off;
1571                 char *ptr = list->elem;
1572                 for (j = 0; j < list->nb_elem;
1573                      j++, ptr += syntax[i].list_elem_size)
1574                     ebml_free(syntax[i].def.n, ptr);
1575                 av_freep(&list->elem);
1576                 list->nb_elem = 0;
1577                 list->alloc_elem_size = 0;
1578             } else
1579                 ebml_free(syntax[i].def.n, data_off);
1580         default:
1581             break;
1582         }
1583     }
1584 }
1585 
1586 /*
1587  * Autodetecting...
1588  */
matroska_probe(const AVProbeData *p)1589 static int matroska_probe(const AVProbeData *p)
1590 {
1591     uint64_t total = 0;
1592     int len_mask = 0x80, size = 1, n = 1, i;
1593 
1594     /* EBML header? */
1595     if (AV_RB32(p->buf) != EBML_ID_HEADER)
1596         return 0;
1597 
1598     /* length of header */
1599     total = p->buf[4];
1600     while (size <= 8 && !(total & len_mask)) {
1601         size++;
1602         len_mask >>= 1;
1603     }
1604     if (size > 8)
1605         return 0;
1606     total &= (len_mask - 1);
1607     while (n < size)
1608         total = (total << 8) | p->buf[4 + n++];
1609 
1610     if (total + 1 == 1ULL << (7 * size)){
1611         /* Unknown-length header - simply parse the whole buffer. */
1612         total = p->buf_size - 4 - size;
1613     } else {
1614         /* Does the probe data contain the whole header? */
1615         if (p->buf_size < 4 + size + total)
1616             return 0;
1617     }
1618 
1619     /* The header should contain a known document type. For now,
1620      * we don't parse the whole header but simply check for the
1621      * availability of that array of characters inside the header.
1622      * Not fully fool-proof, but good enough. */
1623     for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) {
1624         size_t probelen = strlen(matroska_doctypes[i]);
1625         if (total < probelen)
1626             continue;
1627         for (n = 4 + size; n <= 4 + size + total - probelen; n++)
1628             if (!memcmp(p->buf + n, matroska_doctypes[i], probelen))
1629                 return AVPROBE_SCORE_MAX;
1630     }
1631 
1632     // probably valid EBML header but no recognized doctype
1633     return AVPROBE_SCORE_EXTENSION;
1634 }
1635 
matroska_find_track_by_num(MatroskaDemuxContext *matroska, uint64_t num)1636 static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska,
1637                                                  uint64_t num)
1638 {
1639     MatroskaTrack *tracks = matroska->tracks.elem;
1640     int i;
1641 
1642     for (i = 0; i < matroska->tracks.nb_elem; i++)
1643         if (tracks[i].num == num)
1644             return &tracks[i];
1645 
1646     av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %"PRIu64"\n", num);
1647     return NULL;
1648 }
1649 
matroska_decode_buffer(uint8_t **buf, int *buf_size, MatroskaTrack *track)1650 static int matroska_decode_buffer(uint8_t **buf, int *buf_size,
1651                                   MatroskaTrack *track)
1652 {
1653     MatroskaTrackEncoding *encodings = track->encodings.elem;
1654     uint8_t *data = *buf;
1655     int isize = *buf_size;
1656     uint8_t *pkt_data = NULL;
1657     uint8_t av_unused *newpktdata;
1658     int pkt_size = isize;
1659     int result = 0;
1660 #if CONFIG_LZO
1661     int olen;
1662 #endif
1663 
1664     if (pkt_size >= 10000000U)
1665         return AVERROR_INVALIDDATA;
1666 
1667     switch (encodings[0].compression.algo) {
1668     case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
1669     {
1670         int header_size = encodings[0].compression.settings.size;
1671         uint8_t *header = encodings[0].compression.settings.data;
1672 
1673         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         if (!header_size)
1679             return 0;
1680 
1681         pkt_size = isize + header_size;
1682         pkt_data = av_malloc(pkt_size + AV_INPUT_BUFFER_PADDING_SIZE);
1683         if (!pkt_data)
1684             return AVERROR(ENOMEM);
1685 
1686         memcpy(pkt_data, header, header_size);
1687         memcpy(pkt_data + header_size, data, isize);
1688         break;
1689     }
1690 #if CONFIG_LZO
1691     case MATROSKA_TRACK_ENCODING_COMP_LZO:
1692         do {
1693             int insize = isize;
1694             olen       = pkt_size *= 3;
1695             newpktdata = av_realloc(pkt_data, pkt_size + AV_LZO_OUTPUT_PADDING
1696                                                        + AV_INPUT_BUFFER_PADDING_SIZE);
1697             if (!newpktdata) {
1698                 result = AVERROR(ENOMEM);
1699                 goto failed;
1700             }
1701             pkt_data = newpktdata;
1702             result   = av_lzo1x_decode(pkt_data, &olen, data, &insize);
1703         } while (result == AV_LZO_OUTPUT_FULL && pkt_size < 10000000);
1704         if (result) {
1705             result = AVERROR_INVALIDDATA;
1706             goto failed;
1707         }
1708         pkt_size -= olen;
1709         break;
1710 #endif
1711 #if CONFIG_ZLIB
1712     case MATROSKA_TRACK_ENCODING_COMP_ZLIB:
1713     {
1714         z_stream zstream = { 0 };
1715         if (!pkt_size || inflateInit(&zstream) != Z_OK)
1716             return -1;
1717         zstream.next_in  = data;
1718         zstream.avail_in = isize;
1719         do {
1720             pkt_size  *= 3;
1721             newpktdata = av_realloc(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE);
1722             if (!newpktdata) {
1723                 inflateEnd(&zstream);
1724                 result = AVERROR(ENOMEM);
1725                 goto failed;
1726             }
1727             pkt_data          = newpktdata;
1728             zstream.avail_out = pkt_size - zstream.total_out;
1729             zstream.next_out  = pkt_data + zstream.total_out;
1730             result = inflate(&zstream, Z_NO_FLUSH);
1731         } while (result == Z_OK && pkt_size < 10000000);
1732         pkt_size = zstream.total_out;
1733         inflateEnd(&zstream);
1734         if (result != Z_STREAM_END) {
1735             if (result == Z_MEM_ERROR)
1736                 result = AVERROR(ENOMEM);
1737             else
1738                 result = AVERROR_INVALIDDATA;
1739             goto failed;
1740         }
1741         break;
1742     }
1743 #endif
1744 #if CONFIG_BZLIB
1745     case MATROSKA_TRACK_ENCODING_COMP_BZLIB:
1746     {
1747         bz_stream bzstream = { 0 };
1748         if (!pkt_size || BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1749             return -1;
1750         bzstream.next_in  = data;
1751         bzstream.avail_in = isize;
1752         do {
1753             pkt_size  *= 3;
1754             newpktdata = av_realloc(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE);
1755             if (!newpktdata) {
1756                 BZ2_bzDecompressEnd(&bzstream);
1757                 result = AVERROR(ENOMEM);
1758                 goto failed;
1759             }
1760             pkt_data           = newpktdata;
1761             bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1762             bzstream.next_out  = pkt_data + bzstream.total_out_lo32;
1763             result = BZ2_bzDecompress(&bzstream);
1764         } while (result == BZ_OK && pkt_size < 10000000);
1765         pkt_size = bzstream.total_out_lo32;
1766         BZ2_bzDecompressEnd(&bzstream);
1767         if (result != BZ_STREAM_END) {
1768             if (result == BZ_MEM_ERROR)
1769                 result = AVERROR(ENOMEM);
1770             else
1771                 result = AVERROR_INVALIDDATA;
1772             goto failed;
1773         }
1774         break;
1775     }
1776 #endif
1777     default:
1778         return AVERROR_INVALIDDATA;
1779     }
1780 
1781     memset(pkt_data + pkt_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1782 
1783     *buf      = pkt_data;
1784     *buf_size = pkt_size;
1785     return 0;
1786 
1787 failed:
1788     av_free(pkt_data);
1789     return result;
1790 }
1791 
matroska_convert_tag(AVFormatContext *s, EbmlList *list, AVDictionary **metadata, char *prefix)1792 static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
1793                                  AVDictionary **metadata, char *prefix)
1794 {
1795     MatroskaTag *tags = list->elem;
1796     char key[1024];
1797     int i;
1798 
1799     for (i = 0; i < list->nb_elem; i++) {
1800         const char *lang = tags[i].lang &&
1801                            strcmp(tags[i].lang, "und") ? tags[i].lang : NULL;
1802 
1803         if (!tags[i].name) {
1804             av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n");
1805             continue;
1806         }
1807         if (prefix)
1808             snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name);
1809         else
1810             av_strlcpy(key, tags[i].name, sizeof(key));
1811         if (tags[i].def || !lang) {
1812             av_dict_set(metadata, key, tags[i].string, 0);
1813             if (tags[i].sub.nb_elem)
1814                 matroska_convert_tag(s, &tags[i].sub, metadata, key);
1815         }
1816         if (lang) {
1817             av_strlcat(key, "-", sizeof(key));
1818             av_strlcat(key, lang, sizeof(key));
1819             av_dict_set(metadata, key, tags[i].string, 0);
1820             if (tags[i].sub.nb_elem)
1821                 matroska_convert_tag(s, &tags[i].sub, metadata, key);
1822         }
1823     }
1824     ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv);
1825 }
1826 
matroska_convert_tags(AVFormatContext *s)1827 static void matroska_convert_tags(AVFormatContext *s)
1828 {
1829     MatroskaDemuxContext *matroska = s->priv_data;
1830     MatroskaTags *tags = matroska->tags.elem;
1831     int i, j;
1832 
1833     for (i = 0; i < matroska->tags.nb_elem; i++) {
1834         if (tags[i].target.attachuid) {
1835             MatroskaAttachment *attachment = matroska->attachments.elem;
1836             int found = 0;
1837             for (j = 0; j < matroska->attachments.nb_elem; j++) {
1838                 if (attachment[j].uid == tags[i].target.attachuid &&
1839                     attachment[j].stream) {
1840                     matroska_convert_tag(s, &tags[i].tag,
1841                                          &attachment[j].stream->metadata, NULL);
1842                     found = 1;
1843                 }
1844             }
1845             if (!found) {
1846                 av_log(s, AV_LOG_WARNING,
1847                        "The tags at index %d refer to a "
1848                        "non-existent attachment %"PRId64".\n",
1849                        i, tags[i].target.attachuid);
1850             }
1851         } else if (tags[i].target.chapteruid) {
1852             MatroskaChapter *chapter = matroska->chapters.elem;
1853             int found = 0;
1854             for (j = 0; j < matroska->chapters.nb_elem; j++) {
1855                 if (chapter[j].uid == tags[i].target.chapteruid &&
1856                     chapter[j].chapter) {
1857                     matroska_convert_tag(s, &tags[i].tag,
1858                                          &chapter[j].chapter->metadata, NULL);
1859                     found = 1;
1860                 }
1861             }
1862             if (!found) {
1863                 av_log(s, AV_LOG_WARNING,
1864                        "The tags at index %d refer to a non-existent chapter "
1865                        "%"PRId64".\n",
1866                        i, tags[i].target.chapteruid);
1867             }
1868         } else if (tags[i].target.trackuid) {
1869             MatroskaTrack *track = matroska->tracks.elem;
1870             int found = 0;
1871             for (j = 0; j < matroska->tracks.nb_elem; j++) {
1872                 if (track[j].uid == tags[i].target.trackuid &&
1873                     track[j].stream) {
1874                     matroska_convert_tag(s, &tags[i].tag,
1875                                          &track[j].stream->metadata, NULL);
1876                     found = 1;
1877                }
1878             }
1879             if (!found) {
1880                 av_log(s, AV_LOG_WARNING,
1881                        "The tags at index %d refer to a non-existent track "
1882                        "%"PRId64".\n",
1883                        i, tags[i].target.trackuid);
1884             }
1885         } else {
1886             matroska_convert_tag(s, &tags[i].tag, &s->metadata,
1887                                  tags[i].target.type);
1888         }
1889     }
1890 }
1891 
matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int64_t pos)1892 static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska,
1893                                          int64_t pos)
1894 {
1895     uint32_t saved_id  = matroska->current_id;
1896     int64_t before_pos = avio_tell(matroska->ctx->pb);
1897     int ret = 0;
1898     int ret2;
1899 
1900     /* seek */
1901     if (avio_seek(matroska->ctx->pb, pos, SEEK_SET) == pos) {
1902         /* We don't want to lose our seekhead level, so we add
1903          * a dummy. This is a crude hack. */
1904         if (matroska->num_levels == EBML_MAX_DEPTH) {
1905             av_log(matroska->ctx, AV_LOG_INFO,
1906                    "Max EBML element depth (%d) reached, "
1907                    "cannot parse further.\n", EBML_MAX_DEPTH);
1908             ret = AVERROR_INVALIDDATA;
1909         } else {
1910             matroska->levels[matroska->num_levels] = (MatroskaLevel) { 0, EBML_UNKNOWN_LENGTH };
1911             matroska->num_levels++;
1912             matroska->current_id                   = 0;
1913 
1914             ret = ebml_parse(matroska, matroska_segment, matroska);
1915             if (ret == LEVEL_ENDED) {
1916                 /* This can only happen if the seek brought us beyond EOF. */
1917                 ret = AVERROR_EOF;
1918             }
1919         }
1920     }
1921     /* Seek back - notice that in all instances where this is used
1922      * it is safe to set the level to 1. */
1923     ret2 = matroska_reset_status(matroska, saved_id, before_pos);
1924     if (ret >= 0)
1925         ret = ret2;
1926 
1927     return ret;
1928 }
1929 
matroska_execute_seekhead(MatroskaDemuxContext *matroska)1930 static void matroska_execute_seekhead(MatroskaDemuxContext *matroska)
1931 {
1932     EbmlList *seekhead_list = &matroska->seekhead;
1933     int i;
1934 
1935     // we should not do any seeking in the streaming case
1936     if (!(matroska->ctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
1937         return;
1938 
1939     for (i = 0; i < seekhead_list->nb_elem; i++) {
1940         MatroskaSeekhead *seekheads = seekhead_list->elem;
1941         uint32_t id = seekheads[i].id;
1942         int64_t pos = seekheads[i].pos + matroska->segment_start;
1943         MatroskaLevel1Element *elem;
1944 
1945         if (id != seekheads[i].id || pos < matroska->segment_start)
1946             continue;
1947 
1948         elem = matroska_find_level1_elem(matroska, id, pos);
1949         if (!elem || elem->parsed)
1950             continue;
1951 
1952         elem->pos = pos;
1953 
1954         // defer cues parsing until we actually need cue data.
1955         if (id == MATROSKA_ID_CUES)
1956             continue;
1957 
1958         if (matroska_parse_seekhead_entry(matroska, pos) < 0) {
1959             // mark index as broken
1960             matroska->cues_parsing_deferred = -1;
1961             break;
1962         }
1963 
1964         elem->parsed = 1;
1965     }
1966 }
1967 
matroska_add_index_entries(MatroskaDemuxContext *matroska)1968 static void matroska_add_index_entries(MatroskaDemuxContext *matroska)
1969 {
1970     EbmlList *index_list;
1971     MatroskaIndex *index;
1972     uint64_t index_scale = 1;
1973     int i, j;
1974 
1975     if (matroska->ctx->flags & AVFMT_FLAG_IGNIDX)
1976         return;
1977 
1978     index_list = &matroska->index;
1979     index      = index_list->elem;
1980     if (index_list->nb_elem < 2)
1981         return;
1982     if (index[1].time > 1E14 / matroska->time_scale) {
1983         av_log(matroska->ctx, AV_LOG_WARNING, "Dropping apparently-broken index.\n");
1984         return;
1985     }
1986     for (i = 0; i < index_list->nb_elem; i++) {
1987         EbmlList *pos_list    = &index[i].pos;
1988         MatroskaIndexPos *pos = pos_list->elem;
1989         for (j = 0; j < pos_list->nb_elem; j++) {
1990             MatroskaTrack *track = matroska_find_track_by_num(matroska,
1991                                                               pos[j].track);
1992             if (track && track->stream)
1993                 av_add_index_entry(track->stream,
1994                                    pos[j].pos + matroska->segment_start,
1995                                    index[i].time / index_scale, 0, 0,
1996                                    AVINDEX_KEYFRAME);
1997         }
1998     }
1999 }
2000 
matroska_parse_cues(MatroskaDemuxContext *matroska)2001 static void matroska_parse_cues(MatroskaDemuxContext *matroska) {
2002     int i;
2003 
2004     if (matroska->ctx->flags & AVFMT_FLAG_IGNIDX)
2005         return;
2006 
2007     for (i = 0; i < matroska->num_level1_elems; i++) {
2008         MatroskaLevel1Element *elem = &matroska->level1_elems[i];
2009         if (elem->id == MATROSKA_ID_CUES && !elem->parsed) {
2010             if (matroska_parse_seekhead_entry(matroska, elem->pos) < 0)
2011                 matroska->cues_parsing_deferred = -1;
2012             elem->parsed = 1;
2013             break;
2014         }
2015     }
2016 
2017     matroska_add_index_entries(matroska);
2018 }
2019 
matroska_aac_profile(char *codec_id)2020 static int matroska_aac_profile(char *codec_id)
2021 {
2022     static const char *const aac_profiles[] = { "MAIN", "LC", "SSR" };
2023     int profile;
2024 
2025     for (profile = 0; profile < FF_ARRAY_ELEMS(aac_profiles); profile++)
2026         if (strstr(codec_id, aac_profiles[profile]))
2027             break;
2028     return profile + 1;
2029 }
2030 
matroska_aac_sri(int samplerate)2031 static int matroska_aac_sri(int samplerate)
2032 {
2033     int sri;
2034 
2035     for (sri = 0; sri < FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++)
2036         if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2037             break;
2038     return sri;
2039 }
2040 
matroska_metadata_creation_time(AVDictionary **metadata, int64_t date_utc)2041 static void matroska_metadata_creation_time(AVDictionary **metadata, int64_t date_utc)
2042 {
2043     /* Convert to seconds and adjust by number of seconds between 2001-01-01 and Epoch */
2044     avpriv_dict_set_timestamp(metadata, "creation_time", date_utc / 1000 + 978307200000000LL);
2045 }
2046 
matroska_parse_flac(AVFormatContext *s, MatroskaTrack *track, int *offset)2047 static int matroska_parse_flac(AVFormatContext *s,
2048                                MatroskaTrack *track,
2049                                int *offset)
2050 {
2051     AVStream *st = track->stream;
2052     uint8_t *p = track->codec_priv.data;
2053     int size   = track->codec_priv.size;
2054 
2055     if (size < 8 + FLAC_STREAMINFO_SIZE || p[4] & 0x7f) {
2056         av_log(s, AV_LOG_WARNING, "Invalid FLAC private data\n");
2057         track->codec_priv.size = 0;
2058         return 0;
2059     }
2060     *offset = 8;
2061     track->codec_priv.size = 8 + FLAC_STREAMINFO_SIZE;
2062 
2063     p    += track->codec_priv.size;
2064     size -= track->codec_priv.size;
2065 
2066     /* parse the remaining metadata blocks if present */
2067     while (size >= 4) {
2068         int block_last, block_type, block_size;
2069 
2070         flac_parse_block_header(p, &block_last, &block_type, &block_size);
2071 
2072         p    += 4;
2073         size -= 4;
2074         if (block_size > size)
2075             return 0;
2076 
2077         /* check for the channel mask */
2078         if (block_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
2079             AVDictionary *dict = NULL;
2080             AVDictionaryEntry *chmask;
2081 
2082             ff_vorbis_comment(s, &dict, p, block_size, 0);
2083             chmask = av_dict_get(dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
2084             if (chmask) {
2085                 uint64_t mask = strtol(chmask->value, NULL, 0);
2086                 if (!mask || mask & ~0x3ffffULL) {
2087                     av_log(s, AV_LOG_WARNING,
2088                            "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
2089                 } else
2090                     av_channel_layout_from_mask(&st->codecpar->ch_layout, mask);
2091             }
2092             av_dict_free(&dict);
2093         }
2094 
2095         p    += block_size;
2096         size -= block_size;
2097     }
2098 
2099     return 0;
2100 }
2101 
mkv_field_order(MatroskaDemuxContext *matroska, uint64_t field_order)2102 static int mkv_field_order(MatroskaDemuxContext *matroska, uint64_t field_order)
2103 {
2104     int minor, micro, bttb = 0;
2105 
2106     /* workaround a bug in our Matroska muxer, introduced in version 57.36 alongside
2107      * this function, and fixed in 57.52 */
2108     if (matroska->muxingapp && sscanf(matroska->muxingapp, "Lavf57.%d.%d", &minor, &micro) == 2)
2109         bttb = (minor >= 36 && minor <= 51 && micro >= 100);
2110 
2111     switch (field_order) {
2112     case MATROSKA_VIDEO_FIELDORDER_PROGRESSIVE:
2113         return AV_FIELD_PROGRESSIVE;
2114     case MATROSKA_VIDEO_FIELDORDER_UNDETERMINED:
2115         return AV_FIELD_UNKNOWN;
2116     case MATROSKA_VIDEO_FIELDORDER_TT:
2117         return AV_FIELD_TT;
2118     case MATROSKA_VIDEO_FIELDORDER_BB:
2119         return AV_FIELD_BB;
2120     case MATROSKA_VIDEO_FIELDORDER_BT:
2121         return bttb ? AV_FIELD_TB : AV_FIELD_BT;
2122     case MATROSKA_VIDEO_FIELDORDER_TB:
2123         return bttb ? AV_FIELD_BT : AV_FIELD_TB;
2124     default:
2125         return AV_FIELD_UNKNOWN;
2126     }
2127 }
2128 
mkv_stereo_mode_display_mul(int stereo_mode, int *h_width, int *h_height)2129 static void mkv_stereo_mode_display_mul(int stereo_mode,
2130                                         int *h_width, int *h_height)
2131 {
2132     switch (stereo_mode) {
2133         case MATROSKA_VIDEO_STEREOMODE_TYPE_MONO:
2134         case MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_RL:
2135         case MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR:
2136         case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_RL:
2137         case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR:
2138             break;
2139         case MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT:
2140         case MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT:
2141         case MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_RL:
2142         case MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR:
2143             *h_width = 2;
2144             break;
2145         case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTTOM_TOP:
2146         case MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM:
2147         case MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_RL:
2148         case MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR:
2149             *h_height = 2;
2150             break;
2151     }
2152 }
2153 
mkv_parse_video_color(AVStream *st, const MatroskaTrack *track)2154 static int mkv_parse_video_color(AVStream *st, const MatroskaTrack *track) {
2155     const MatroskaTrackVideoColor *color = track->video.color.elem;
2156     const MatroskaMasteringMeta *mastering_meta;
2157     int has_mastering_primaries, has_mastering_luminance;
2158 
2159     if (!track->video.color.nb_elem)
2160         return 0;
2161 
2162     mastering_meta = &color->mastering_meta;
2163     // Mastering primaries are CIE 1931 coords, and must be > 0.
2164     has_mastering_primaries =
2165         mastering_meta->r_x > 0 && mastering_meta->r_y > 0 &&
2166         mastering_meta->g_x > 0 && mastering_meta->g_y > 0 &&
2167         mastering_meta->b_x > 0 && mastering_meta->b_y > 0 &&
2168         mastering_meta->white_x > 0 && mastering_meta->white_y > 0;
2169     has_mastering_luminance = mastering_meta->max_luminance >
2170                                   mastering_meta->min_luminance.el.f  &&
2171                               mastering_meta->min_luminance.el.f >= 0 &&
2172                               mastering_meta->min_luminance.count;
2173 
2174     if (color->matrix_coefficients != AVCOL_SPC_RESERVED)
2175         st->codecpar->color_space = color->matrix_coefficients;
2176     if (color->primaries != AVCOL_PRI_RESERVED &&
2177         color->primaries != AVCOL_PRI_RESERVED0)
2178         st->codecpar->color_primaries = color->primaries;
2179     if (color->transfer_characteristics != AVCOL_TRC_RESERVED &&
2180         color->transfer_characteristics != AVCOL_TRC_RESERVED0)
2181         st->codecpar->color_trc = color->transfer_characteristics;
2182     if (color->range != AVCOL_RANGE_UNSPECIFIED &&
2183         color->range <= AVCOL_RANGE_JPEG)
2184         st->codecpar->color_range = color->range;
2185     if (color->chroma_siting_horz != MATROSKA_COLOUR_CHROMASITINGHORZ_UNDETERMINED &&
2186         color->chroma_siting_vert != MATROSKA_COLOUR_CHROMASITINGVERT_UNDETERMINED &&
2187         color->chroma_siting_horz  < MATROSKA_COLOUR_CHROMASITINGHORZ_NB &&
2188         color->chroma_siting_vert  < MATROSKA_COLOUR_CHROMASITINGVERT_NB) {
2189         st->codecpar->chroma_location =
2190             avcodec_chroma_pos_to_enum((color->chroma_siting_horz - 1) << 7,
2191                                        (color->chroma_siting_vert - 1) << 7);
2192     }
2193     if (color->max_cll && color->max_fall) {
2194         size_t size = 0;
2195         int ret;
2196         AVContentLightMetadata *metadata = av_content_light_metadata_alloc(&size);
2197         if (!metadata)
2198             return AVERROR(ENOMEM);
2199         ret = av_stream_add_side_data(st, AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
2200                                       (uint8_t *)metadata, size);
2201         if (ret < 0) {
2202             av_freep(&metadata);
2203             return ret;
2204         }
2205         metadata->MaxCLL  = color->max_cll;
2206         metadata->MaxFALL = color->max_fall;
2207     }
2208 
2209     if (has_mastering_primaries || has_mastering_luminance) {
2210         AVMasteringDisplayMetadata *metadata =
2211             (AVMasteringDisplayMetadata*) av_stream_new_side_data(
2212                 st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
2213                 sizeof(AVMasteringDisplayMetadata));
2214         if (!metadata) {
2215             return AVERROR(ENOMEM);
2216         }
2217         memset(metadata, 0, sizeof(AVMasteringDisplayMetadata));
2218         if (has_mastering_primaries) {
2219             metadata->display_primaries[0][0] = av_d2q(mastering_meta->r_x, INT_MAX);
2220             metadata->display_primaries[0][1] = av_d2q(mastering_meta->r_y, INT_MAX);
2221             metadata->display_primaries[1][0] = av_d2q(mastering_meta->g_x, INT_MAX);
2222             metadata->display_primaries[1][1] = av_d2q(mastering_meta->g_y, INT_MAX);
2223             metadata->display_primaries[2][0] = av_d2q(mastering_meta->b_x, INT_MAX);
2224             metadata->display_primaries[2][1] = av_d2q(mastering_meta->b_y, INT_MAX);
2225             metadata->white_point[0] = av_d2q(mastering_meta->white_x, INT_MAX);
2226             metadata->white_point[1] = av_d2q(mastering_meta->white_y, INT_MAX);
2227             metadata->has_primaries = 1;
2228         }
2229         if (has_mastering_luminance) {
2230             metadata->max_luminance = av_d2q(mastering_meta->max_luminance, INT_MAX);
2231             metadata->min_luminance = av_d2q(mastering_meta->min_luminance.el.f, INT_MAX);
2232             metadata->has_luminance = 1;
2233         }
2234     }
2235     return 0;
2236 }
2237 
mkv_create_display_matrix(AVStream *st, const MatroskaTrackVideoProjection *proj, void *logctx)2238 static int mkv_create_display_matrix(AVStream *st,
2239                                      const MatroskaTrackVideoProjection *proj,
2240                                      void *logctx)
2241 {
2242     double pitch = proj->pitch, yaw = proj->yaw, roll = proj->roll;
2243     int32_t *matrix;
2244     int hflip;
2245 
2246     if (pitch == 0.0 && yaw == 0.0 && roll == 0.0)
2247         return 0;
2248 
2249     /* Note: The following constants are exactly representable
2250      * as floating-point numbers. */
2251     if (pitch != 0.0 || (yaw != 0.0 && yaw != 180.0 && yaw != -180.0) ||
2252         isnan(roll)) {
2253         av_log(logctx, AV_LOG_WARNING, "Ignoring non-2D rectangular "
2254                "projection in stream %u (yaw %f, pitch %f, roll %f)\n",
2255                st->index, yaw, pitch, roll);
2256         return 0;
2257     }
2258     matrix = (int32_t*)av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,
2259                                                9 * sizeof(*matrix));
2260     if (!matrix)
2261         return AVERROR(ENOMEM);
2262 
2263     hflip = yaw != 0.0;
2264     /* ProjectionPoseRoll is in the counter-clockwise direction
2265      * whereas av_display_rotation_set() expects its argument
2266      * to be oriented clockwise, so we need to negate roll.
2267      * Furthermore, if hflip is set, we need to negate it again
2268      * to account for the fact that the Matroska specifications
2269      * require the yaw rotation to be applied first. */
2270     av_display_rotation_set(matrix, roll * (2 * hflip - 1));
2271     av_display_matrix_flip(matrix, hflip, 0);
2272 
2273     return 0;
2274 }
2275 
mkv_parse_video_projection(AVStream *st, const MatroskaTrack *track, void *logctx)2276 static int mkv_parse_video_projection(AVStream *st, const MatroskaTrack *track,
2277                                       void *logctx)
2278 {
2279     AVSphericalMapping *spherical;
2280     const MatroskaTrackVideoProjection *mkv_projection = &track->video.projection;
2281     const uint8_t *priv_data = mkv_projection->private.data;
2282     enum AVSphericalProjection projection;
2283     size_t spherical_size;
2284     uint32_t l = 0, t = 0, r = 0, b = 0;
2285     uint32_t padding = 0;
2286     int ret;
2287 
2288     if (mkv_projection->private.size && priv_data[0] != 0) {
2289         av_log(logctx, AV_LOG_WARNING, "Unknown spherical metadata\n");
2290         return 0;
2291     }
2292 
2293     switch (track->video.projection.type) {
2294     case MATROSKA_VIDEO_PROJECTION_TYPE_RECTANGULAR:
2295         return mkv_create_display_matrix(st, mkv_projection, logctx);
2296     case MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR:
2297         if (track->video.projection.private.size == 20) {
2298             t = AV_RB32(priv_data +  4);
2299             b = AV_RB32(priv_data +  8);
2300             l = AV_RB32(priv_data + 12);
2301             r = AV_RB32(priv_data + 16);
2302 
2303             if (b >= UINT_MAX - t || r >= UINT_MAX - l) {
2304                 av_log(logctx, AV_LOG_ERROR,
2305                        "Invalid bounding rectangle coordinates "
2306                        "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n",
2307                        l, t, r, b);
2308                 return AVERROR_INVALIDDATA;
2309             }
2310         } else if (track->video.projection.private.size != 0) {
2311             av_log(logctx, AV_LOG_ERROR, "Unknown spherical metadata\n");
2312             return AVERROR_INVALIDDATA;
2313         }
2314 
2315         if (l || t || r || b)
2316             projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE;
2317         else
2318             projection = AV_SPHERICAL_EQUIRECTANGULAR;
2319         break;
2320     case MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP:
2321         if (track->video.projection.private.size < 4) {
2322             av_log(logctx, AV_LOG_ERROR, "Missing projection private properties\n");
2323             return AVERROR_INVALIDDATA;
2324         } else if (track->video.projection.private.size == 12) {
2325             uint32_t layout = AV_RB32(priv_data + 4);
2326             if (layout) {
2327                 av_log(logctx, AV_LOG_WARNING,
2328                        "Unknown spherical cubemap layout %"PRIu32"\n", layout);
2329                 return 0;
2330             }
2331             projection = AV_SPHERICAL_CUBEMAP;
2332             padding = AV_RB32(priv_data + 8);
2333         } else {
2334             av_log(logctx, AV_LOG_ERROR, "Unknown spherical metadata\n");
2335             return AVERROR_INVALIDDATA;
2336         }
2337         break;
2338     default:
2339         av_log(logctx, AV_LOG_WARNING,
2340                "Unknown spherical metadata type %"PRIu64"\n",
2341                track->video.projection.type);
2342         return 0;
2343     }
2344 
2345     spherical = av_spherical_alloc(&spherical_size);
2346     if (!spherical)
2347         return AVERROR(ENOMEM);
2348 
2349     spherical->projection = projection;
2350 
2351     spherical->yaw   = (int32_t) (track->video.projection.yaw   * (1 << 16));
2352     spherical->pitch = (int32_t) (track->video.projection.pitch * (1 << 16));
2353     spherical->roll  = (int32_t) (track->video.projection.roll  * (1 << 16));
2354 
2355     spherical->padding = padding;
2356 
2357     spherical->bound_left   = l;
2358     spherical->bound_top    = t;
2359     spherical->bound_right  = r;
2360     spherical->bound_bottom = b;
2361 
2362     ret = av_stream_add_side_data(st, AV_PKT_DATA_SPHERICAL, (uint8_t *)spherical,
2363                                   spherical_size);
2364     if (ret < 0) {
2365         av_freep(&spherical);
2366         return ret;
2367     }
2368 
2369     return 0;
2370 }
2371 
mkv_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const MatroskaTrack *track, EbmlBin *bin)2372 static int mkv_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const MatroskaTrack *track,
2373                                EbmlBin *bin)
2374 {
2375     return ff_isom_parse_dvcc_dvvc(s, st, bin->data, bin->size);
2376 }
2377 
mkv_parse_block_addition_mappings(AVFormatContext *s, AVStream *st, const MatroskaTrack *track)2378 static int mkv_parse_block_addition_mappings(AVFormatContext *s, AVStream *st, const MatroskaTrack *track)
2379 {
2380     const EbmlList *mappings_list = &track->block_addition_mappings;
2381     MatroskaBlockAdditionMapping *mappings = mappings_list->elem;
2382     int ret;
2383 
2384     for (int i = 0; i < mappings_list->nb_elem; i++) {
2385         MatroskaBlockAdditionMapping *mapping = &mappings[i];
2386 
2387         switch (mapping->type) {
2388         case MKBETAG('d','v','c','C'):
2389         case MKBETAG('d','v','v','C'):
2390             if ((ret = mkv_parse_dvcc_dvvc(s, st, track, &mapping->extradata)) < 0)
2391                 return ret;
2392 
2393             break;
2394         default:
2395             av_log(s, AV_LOG_DEBUG,
2396                    "Unknown block additional mapping type 0x%"PRIx64", value %"PRIu64", name \"%s\"\n",
2397                    mapping->type, mapping->value, mapping->name ? mapping->name : "");
2398         }
2399     }
2400 
2401     return 0;
2402 }
2403 
get_qt_codec(MatroskaTrack *track, uint32_t *fourcc, enum AVCodecID *codec_id)2404 static int get_qt_codec(MatroskaTrack *track, uint32_t *fourcc, enum AVCodecID *codec_id)
2405 {
2406     const AVCodecTag *codec_tags;
2407 
2408     codec_tags = track->type == MATROSKA_TRACK_TYPE_VIDEO ?
2409             ff_codec_movvideo_tags : ff_codec_movaudio_tags;
2410 
2411     /* Normalize noncompliant private data that starts with the fourcc
2412      * by expanding/shifting the data by 4 bytes and storing the data
2413      * size at the start. */
2414     if (ff_codec_get_id(codec_tags, AV_RL32(track->codec_priv.data))) {
2415         int ret = av_buffer_realloc(&track->codec_priv.buf,
2416                                     track->codec_priv.size + 4 + AV_INPUT_BUFFER_PADDING_SIZE);
2417         if (ret < 0)
2418             return ret;
2419 
2420         track->codec_priv.data = track->codec_priv.buf->data;
2421         memmove(track->codec_priv.data + 4, track->codec_priv.data, track->codec_priv.size);
2422         track->codec_priv.size += 4;
2423         AV_WB32(track->codec_priv.data, track->codec_priv.size);
2424     }
2425 
2426     *fourcc = AV_RL32(track->codec_priv.data + 4);
2427     *codec_id = ff_codec_get_id(codec_tags, *fourcc);
2428 
2429     return 0;
2430 }
2431 
matroska_parse_tracks(AVFormatContext *s)2432 static int matroska_parse_tracks(AVFormatContext *s)
2433 {
2434     MatroskaDemuxContext *matroska = s->priv_data;
2435     MatroskaTrack *tracks = matroska->tracks.elem;
2436     int i, j, ret;
2437     int k;
2438 
2439     for (i = 0; i < matroska->tracks.nb_elem; i++) {
2440         MatroskaTrack *track = &tracks[i];
2441         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
2442         EbmlList *encodings_list = &track->encodings;
2443         MatroskaTrackEncoding *encodings = encodings_list->elem;
2444         uint8_t *extradata = NULL;
2445         int extradata_size = 0;
2446         int extradata_offset = 0;
2447         uint32_t fourcc = 0;
2448         FFIOContext b;
2449         AVStream *st;
2450         FFStream *sti;
2451         char* key_id_base64 = NULL;
2452         int bit_depth = -1;
2453 
2454         /* Apply some sanity checks. */
2455         if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
2456             track->type != MATROSKA_TRACK_TYPE_AUDIO &&
2457             track->type != MATROSKA_TRACK_TYPE_SUBTITLE &&
2458             track->type != MATROSKA_TRACK_TYPE_METADATA) {
2459             av_log(matroska->ctx, AV_LOG_INFO,
2460                    "Unknown or unsupported track type %"PRIu64"\n",
2461                    track->type);
2462             continue;
2463         }
2464         if (!track->codec_id)
2465             continue;
2466 
2467         if (   track->type == MATROSKA_TRACK_TYPE_AUDIO && track->codec_id[0] != 'A'
2468             || track->type == MATROSKA_TRACK_TYPE_VIDEO && track->codec_id[0] != 'V'
2469             || track->type == MATROSKA_TRACK_TYPE_SUBTITLE && track->codec_id[0] != 'D' && track->codec_id[0] != 'S'
2470             || track->type == MATROSKA_TRACK_TYPE_METADATA && track->codec_id[0] != 'D' && track->codec_id[0] != 'S'
2471         ) {
2472             av_log(matroska->ctx, AV_LOG_INFO, "Inconsistent track type\n");
2473             continue;
2474         }
2475 
2476         if (track->audio.samplerate < 0 || track->audio.samplerate > INT_MAX ||
2477             isnan(track->audio.samplerate)) {
2478             av_log(matroska->ctx, AV_LOG_WARNING,
2479                    "Invalid sample rate %f, defaulting to 8000 instead.\n",
2480                    track->audio.samplerate);
2481             track->audio.samplerate = 8000;
2482         }
2483 
2484         if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2485             if (!track->default_duration && track->video.frame_rate > 0) {
2486                 double default_duration = 1000000000 / track->video.frame_rate;
2487                 if (default_duration > UINT64_MAX || default_duration < 0) {
2488                     av_log(matroska->ctx, AV_LOG_WARNING,
2489                          "Invalid frame rate %e. Cannot calculate default duration.\n",
2490                          track->video.frame_rate);
2491                 } else {
2492                     track->default_duration = default_duration;
2493                 }
2494             }
2495             if (track->video.display_width == -1)
2496                 track->video.display_width = track->video.pixel_width;
2497             if (track->video.display_height == -1)
2498                 track->video.display_height = track->video.pixel_height;
2499             if (track->video.color_space.size == 4)
2500                 fourcc = AV_RL32(track->video.color_space.data);
2501         } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2502             if (!track->audio.out_samplerate)
2503                 track->audio.out_samplerate = track->audio.samplerate;
2504         }
2505         if (encodings_list->nb_elem > 1) {
2506             av_log(matroska->ctx, AV_LOG_ERROR,
2507                    "Multiple combined encodings not supported");
2508         } else if (encodings_list->nb_elem == 1) {
2509             if (encodings[0].type) {
2510                 if (encodings[0].encryption.key_id.size > 0) {
2511                     /* Save the encryption key id to be stored later as a
2512                        metadata tag. */
2513                     const int b64_size = AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
2514                     key_id_base64 = av_malloc(b64_size);
2515                     if (key_id_base64 == NULL)
2516                         return AVERROR(ENOMEM);
2517 
2518                     av_base64_encode(key_id_base64, b64_size,
2519                                      encodings[0].encryption.key_id.data,
2520                                      encodings[0].encryption.key_id.size);
2521                 } else {
2522                     encodings[0].scope = 0;
2523                     av_log(matroska->ctx, AV_LOG_ERROR,
2524                            "Unsupported encoding type");
2525                 }
2526             } else if (
2527 #if CONFIG_ZLIB
2528                  encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB  &&
2529 #endif
2530 #if CONFIG_BZLIB
2531                  encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
2532 #endif
2533 #if CONFIG_LZO
2534                  encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO   &&
2535 #endif
2536                  encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP) {
2537                 encodings[0].scope = 0;
2538                 av_log(matroska->ctx, AV_LOG_ERROR,
2539                        "Unsupported encoding type");
2540             } else if (track->codec_priv.size && encodings[0].scope & 2) {
2541                 uint8_t *codec_priv = track->codec_priv.data;
2542                 int ret = matroska_decode_buffer(&track->codec_priv.data,
2543                                                  &track->codec_priv.size,
2544                                                  track);
2545                 if (ret < 0) {
2546                     track->codec_priv.data = NULL;
2547                     track->codec_priv.size = 0;
2548                     av_log(matroska->ctx, AV_LOG_ERROR,
2549                            "Failed to decode codec private data\n");
2550                 }
2551 
2552                 if (codec_priv != track->codec_priv.data) {
2553                     av_buffer_unref(&track->codec_priv.buf);
2554                     if (track->codec_priv.data) {
2555                         track->codec_priv.buf = av_buffer_create(track->codec_priv.data,
2556                                                                  track->codec_priv.size + AV_INPUT_BUFFER_PADDING_SIZE,
2557                                                                  NULL, NULL, 0);
2558                         if (!track->codec_priv.buf) {
2559                             av_freep(&track->codec_priv.data);
2560                             track->codec_priv.size = 0;
2561                             return AVERROR(ENOMEM);
2562                         }
2563                     }
2564                 }
2565             }
2566         }
2567         track->needs_decoding = encodings && !encodings[0].type &&
2568                                 encodings[0].scope & 1          &&
2569                                 (encodings[0].compression.algo !=
2570                                    MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP ||
2571                                  encodings[0].compression.settings.size);
2572 
2573         for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
2574             if (av_strstart(track->codec_id, ff_mkv_codec_tags[j].str, NULL)) {
2575                 codec_id = ff_mkv_codec_tags[j].id;
2576                 break;
2577             }
2578         }
2579 
2580         st = track->stream = avformat_new_stream(s, NULL);
2581         if (!st) {
2582             av_free(key_id_base64);
2583             return AVERROR(ENOMEM);
2584         }
2585         sti = ffstream(st);
2586 
2587         if (key_id_base64) {
2588             /* export encryption key id as base64 metadata tag */
2589             av_dict_set(&st->metadata, "enc_key_id", key_id_base64,
2590                         AV_DICT_DONT_STRDUP_VAL);
2591         }
2592 
2593         if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") &&
2594              track->codec_priv.size >= 40               &&
2595             track->codec_priv.data) {
2596             track->ms_compat    = 1;
2597             bit_depth           = AV_RL16(track->codec_priv.data + 14);
2598             fourcc              = AV_RL32(track->codec_priv.data + 16);
2599             codec_id            = ff_codec_get_id(ff_codec_bmp_tags,
2600                                                   fourcc);
2601             if (!codec_id)
2602                 codec_id        = ff_codec_get_id(ff_codec_movvideo_tags,
2603                                                   fourcc);
2604             extradata_offset    = 40;
2605         } else if (!strcmp(track->codec_id, "A_MS/ACM") &&
2606                    track->codec_priv.size >= 14         &&
2607                    track->codec_priv.data) {
2608             int ret;
2609             ffio_init_context(&b, track->codec_priv.data,
2610                               track->codec_priv.size,
2611                               0, NULL, NULL, NULL, NULL);
2612             ret = ff_get_wav_header(s, &b.pub, st->codecpar,
2613                                     track->codec_priv.size, 0);
2614             if (ret < 0)
2615                 return ret;
2616             codec_id         = st->codecpar->codec_id;
2617             fourcc           = st->codecpar->codec_tag;
2618             extradata_offset = FFMIN(track->codec_priv.size, 18);
2619         } else if (!strcmp(track->codec_id, "A_QUICKTIME")
2620                    /* Normally 36, but allow noncompliant private data */
2621                    && (track->codec_priv.size >= 32)
2622                    && (track->codec_priv.data)) {
2623             uint16_t sample_size;
2624             int ret = get_qt_codec(track, &fourcc, &codec_id);
2625             if (ret < 0)
2626                 return ret;
2627             sample_size = AV_RB16(track->codec_priv.data + 26);
2628             if (fourcc == 0) {
2629                 if (sample_size == 8) {
2630                     fourcc = MKTAG('r','a','w',' ');
2631                     codec_id = ff_codec_get_id(ff_codec_movaudio_tags, fourcc);
2632                 } else if (sample_size == 16) {
2633                     fourcc = MKTAG('t','w','o','s');
2634                     codec_id = ff_codec_get_id(ff_codec_movaudio_tags, fourcc);
2635                 }
2636             }
2637             if ((fourcc == MKTAG('t','w','o','s') ||
2638                     fourcc == MKTAG('s','o','w','t')) &&
2639                     sample_size == 8)
2640                 codec_id = AV_CODEC_ID_PCM_S8;
2641         } else if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2642                    (track->codec_priv.size >= 21)          &&
2643                    (track->codec_priv.data)) {
2644             int ret = get_qt_codec(track, &fourcc, &codec_id);
2645             if (ret < 0)
2646                 return ret;
2647             if (codec_id == AV_CODEC_ID_NONE && AV_RL32(track->codec_priv.data+4) == AV_RL32("SMI ")) {
2648                 fourcc = MKTAG('S','V','Q','3');
2649                 codec_id = ff_codec_get_id(ff_codec_movvideo_tags, fourcc);
2650             }
2651             if (codec_id == AV_CODEC_ID_NONE)
2652                 av_log(matroska->ctx, AV_LOG_ERROR,
2653                        "mov FourCC not found %s.\n", av_fourcc2str(fourcc));
2654             if (track->codec_priv.size >= 86) {
2655                 bit_depth = AV_RB16(track->codec_priv.data + 82);
2656                 ffio_init_context(&b, track->codec_priv.data,
2657                                   track->codec_priv.size,
2658                                   0, NULL, NULL, NULL, NULL);
2659                 if (ff_get_qtpalette(codec_id, &b.pub, track->palette)) {
2660                     bit_depth &= 0x1F;
2661                     track->has_palette = 1;
2662                 }
2663             }
2664         } else if (codec_id == AV_CODEC_ID_PCM_S16BE) {
2665             switch (track->audio.bitdepth) {
2666             case  8:
2667                 codec_id = AV_CODEC_ID_PCM_U8;
2668                 break;
2669             case 24:
2670                 codec_id = AV_CODEC_ID_PCM_S24BE;
2671                 break;
2672             case 32:
2673                 codec_id = AV_CODEC_ID_PCM_S32BE;
2674                 break;
2675             }
2676         } else if (codec_id == AV_CODEC_ID_PCM_S16LE) {
2677             switch (track->audio.bitdepth) {
2678             case  8:
2679                 codec_id = AV_CODEC_ID_PCM_U8;
2680                 break;
2681             case 24:
2682                 codec_id = AV_CODEC_ID_PCM_S24LE;
2683                 break;
2684             case 32:
2685                 codec_id = AV_CODEC_ID_PCM_S32LE;
2686                 break;
2687             }
2688         } else if (codec_id == AV_CODEC_ID_PCM_F32LE &&
2689                    track->audio.bitdepth == 64) {
2690             codec_id = AV_CODEC_ID_PCM_F64LE;
2691         } else if (codec_id == AV_CODEC_ID_AAC && !track->codec_priv.size) {
2692             int profile = matroska_aac_profile(track->codec_id);
2693             int sri     = matroska_aac_sri(track->audio.samplerate);
2694             extradata   = av_mallocz(5 + AV_INPUT_BUFFER_PADDING_SIZE);
2695             if (!extradata)
2696                 return AVERROR(ENOMEM);
2697             extradata[0] = (profile << 3) | ((sri & 0x0E) >> 1);
2698             extradata[1] = ((sri & 0x01) << 7) | (track->audio.channels << 3);
2699             if (strstr(track->codec_id, "SBR")) {
2700                 sri            = matroska_aac_sri(track->audio.out_samplerate);
2701                 extradata[2]   = 0x56;
2702                 extradata[3]   = 0xE5;
2703                 extradata[4]   = 0x80 | (sri << 3);
2704                 extradata_size = 5;
2705             } else
2706                 extradata_size = 2;
2707         } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size && track->codec_priv.size < INT_MAX - 12 - AV_INPUT_BUFFER_PADDING_SIZE) {
2708             /* Only ALAC's magic cookie is stored in Matroska's track headers.
2709              * Create the "atom size", "tag", and "tag version" fields the
2710              * decoder expects manually. */
2711             extradata_size = 12 + track->codec_priv.size;
2712             extradata      = av_mallocz(extradata_size +
2713                                         AV_INPUT_BUFFER_PADDING_SIZE);
2714             if (!extradata)
2715                 return AVERROR(ENOMEM);
2716             AV_WB32(extradata, extradata_size);
2717             memcpy(&extradata[4], "alac", 4);
2718             AV_WB32(&extradata[8], 0);
2719             memcpy(&extradata[12], track->codec_priv.data,
2720                    track->codec_priv.size);
2721         } else if (codec_id == AV_CODEC_ID_TTA) {
2722             uint8_t *ptr;
2723             if (track->audio.channels > UINT16_MAX ||
2724                 track->audio.bitdepth > UINT16_MAX) {
2725                 av_log(matroska->ctx, AV_LOG_WARNING,
2726                        "Too large audio channel number %"PRIu64
2727                        " or bitdepth %"PRIu64". Skipping track.\n",
2728                        track->audio.channels, track->audio.bitdepth);
2729                 if (matroska->ctx->error_recognition & AV_EF_EXPLODE)
2730                     return AVERROR_INVALIDDATA;
2731                 else
2732                     continue;
2733             }
2734             if (track->audio.out_samplerate < 0 || track->audio.out_samplerate > INT_MAX)
2735                 return AVERROR_INVALIDDATA;
2736             extradata_size = 22;
2737             extradata      = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2738             if (!extradata)
2739                 return AVERROR(ENOMEM);
2740             ptr = extradata;
2741             bytestream_put_be32(&ptr, AV_RB32("TTA1"));
2742             bytestream_put_le16(&ptr, 1);
2743             bytestream_put_le16(&ptr, track->audio.channels);
2744             bytestream_put_le16(&ptr, track->audio.bitdepth);
2745             bytestream_put_le32(&ptr, track->audio.out_samplerate);
2746             bytestream_put_le32(&ptr, av_rescale(matroska->duration * matroska->time_scale,
2747                                                  track->audio.out_samplerate,
2748                                                  AV_TIME_BASE * 1000));
2749         } else if (codec_id == AV_CODEC_ID_RV10 ||
2750                    codec_id == AV_CODEC_ID_RV20 ||
2751                    codec_id == AV_CODEC_ID_RV30 ||
2752                    codec_id == AV_CODEC_ID_RV40) {
2753             extradata_offset = 26;
2754         } else if (codec_id == AV_CODEC_ID_RA_144) {
2755             track->audio.out_samplerate = 8000;
2756             track->audio.channels       = 1;
2757         } else if ((codec_id == AV_CODEC_ID_RA_288 ||
2758                     codec_id == AV_CODEC_ID_COOK   ||
2759                     codec_id == AV_CODEC_ID_ATRAC3 ||
2760                     codec_id == AV_CODEC_ID_SIPR)
2761                       && track->codec_priv.data) {
2762             const uint8_t *ptr = track->codec_priv.data;
2763             int flavor;
2764 
2765             if (track->codec_priv.size < 46)
2766                 return AVERROR_INVALIDDATA;
2767             ptr += 22;
2768             flavor                       = bytestream_get_be16(&ptr);
2769             track->audio.coded_framesize = bytestream_get_be32(&ptr);
2770             ptr += 12;
2771             track->audio.sub_packet_h    = bytestream_get_be16(&ptr);
2772             track->audio.frame_size      = bytestream_get_be16(&ptr);
2773             track->audio.sub_packet_size = bytestream_get_be16(&ptr);
2774             if (track->audio.coded_framesize <= 0 ||
2775                 track->audio.sub_packet_h    <= 0 ||
2776                 track->audio.frame_size      <= 0)
2777                 return AVERROR_INVALIDDATA;
2778 
2779             if (codec_id == AV_CODEC_ID_RA_288) {
2780                 if (track->audio.sub_packet_h & 1 || 2 * track->audio.frame_size
2781                     != (int64_t)track->audio.sub_packet_h * track->audio.coded_framesize)
2782                     return AVERROR_INVALIDDATA;
2783                 st->codecpar->block_align = track->audio.coded_framesize;
2784                 track->codec_priv.size = 0;
2785             } else {
2786                 if (codec_id == AV_CODEC_ID_SIPR) {
2787                     static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 };
2788                     if (flavor > 3)
2789                         return AVERROR_INVALIDDATA;
2790                     track->audio.sub_packet_size = ff_sipr_subpk_size[flavor];
2791                     st->codecpar->bit_rate          = sipr_bit_rate[flavor];
2792                 } else if (track->audio.sub_packet_size <= 0 ||
2793                            track->audio.frame_size % track->audio.sub_packet_size)
2794                     return AVERROR_INVALIDDATA;
2795                 st->codecpar->block_align = track->audio.sub_packet_size;
2796                 extradata_offset       = 78;
2797             }
2798             track->audio.buf = av_malloc_array(track->audio.sub_packet_h,
2799                                                track->audio.frame_size);
2800             if (!track->audio.buf)
2801                 return AVERROR(ENOMEM);
2802         } else if (codec_id == AV_CODEC_ID_FLAC && track->codec_priv.size) {
2803             ret = matroska_parse_flac(s, track, &extradata_offset);
2804             if (ret < 0)
2805                 return ret;
2806         } else if (codec_id == AV_CODEC_ID_WAVPACK && track->codec_priv.size < 2) {
2807             av_log(matroska->ctx, AV_LOG_INFO, "Assuming WavPack version 4.10 "
2808                    "in absence of valid CodecPrivate.\n");
2809             extradata_size = 2;
2810             extradata = av_mallocz(2 + AV_INPUT_BUFFER_PADDING_SIZE);
2811             if (!extradata)
2812                 return AVERROR(ENOMEM);
2813             AV_WL16(extradata, 0x410);
2814         } else if (codec_id == AV_CODEC_ID_PRORES && track->codec_priv.size == 4) {
2815             fourcc = AV_RL32(track->codec_priv.data);
2816         } else if (codec_id == AV_CODEC_ID_VP9 && track->codec_priv.size) {
2817             /* we don't need any value stored in CodecPrivate.
2818                make sure that it's not exported as extradata. */
2819             track->codec_priv.size = 0;
2820         }
2821         track->codec_priv.size -= extradata_offset;
2822 
2823         if (codec_id == AV_CODEC_ID_NONE)
2824             av_log(matroska->ctx, AV_LOG_INFO,
2825                    "Unknown/unsupported AVCodecID %s.\n", track->codec_id);
2826 
2827         if (track->time_scale < 0.01) {
2828             av_log(matroska->ctx, AV_LOG_WARNING,
2829                    "Track TimestampScale too small %f, assuming 1.0.\n",
2830                    track->time_scale);
2831             track->time_scale = 1.0;
2832         }
2833         avpriv_set_pts_info(st, 64, matroska->time_scale * track->time_scale,
2834                             1000 * 1000 * 1000);    /* 64 bit pts in ns */
2835 
2836         /* convert the delay from ns to the track timebase */
2837         track->codec_delay_in_track_tb = av_rescale_q(track->codec_delay,
2838                                           (AVRational){ 1, 1000000000 },
2839                                           st->time_base);
2840 
2841         st->codecpar->codec_id = codec_id;
2842 
2843         if (strcmp(track->language, "und"))
2844             av_dict_set(&st->metadata, "language", track->language, 0);
2845         av_dict_set(&st->metadata, "title", track->name, 0);
2846 
2847         if (track->flag_default)
2848             st->disposition |= AV_DISPOSITION_DEFAULT;
2849         if (track->flag_forced)
2850             st->disposition |= AV_DISPOSITION_FORCED;
2851         if (track->flag_comment)
2852             st->disposition |= AV_DISPOSITION_COMMENT;
2853         if (track->flag_hearingimpaired)
2854             st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
2855         if (track->flag_visualimpaired)
2856             st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
2857         if (track->flag_original.count > 0)
2858             st->disposition |= track->flag_original.el.u ? AV_DISPOSITION_ORIGINAL
2859                                                          : AV_DISPOSITION_DUB;
2860 
2861         if (!st->codecpar->extradata) {
2862             if (extradata) {
2863                 st->codecpar->extradata      = extradata;
2864                 st->codecpar->extradata_size = extradata_size;
2865             } else if (track->codec_priv.data && track->codec_priv.size > 0) {
2866                 if (ff_alloc_extradata(st->codecpar, track->codec_priv.size))
2867                     return AVERROR(ENOMEM);
2868                 memcpy(st->codecpar->extradata,
2869                        track->codec_priv.data + extradata_offset,
2870                        track->codec_priv.size);
2871             }
2872         }
2873 
2874         if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2875             MatroskaTrackPlane *planes = track->operation.combine_planes.elem;
2876             int display_width_mul  = 1;
2877             int display_height_mul = 1;
2878 
2879             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
2880             st->codecpar->codec_tag  = fourcc;
2881             if (bit_depth >= 0)
2882                 st->codecpar->bits_per_coded_sample = bit_depth;
2883             st->codecpar->width      = track->video.pixel_width;
2884             st->codecpar->height     = track->video.pixel_height;
2885 
2886             if (track->video.interlaced == MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED)
2887                 st->codecpar->field_order = mkv_field_order(matroska, track->video.field_order);
2888             else if (track->video.interlaced == MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE)
2889                 st->codecpar->field_order = AV_FIELD_PROGRESSIVE;
2890 
2891             if (track->video.stereo_mode && track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
2892                 mkv_stereo_mode_display_mul(track->video.stereo_mode, &display_width_mul, &display_height_mul);
2893 
2894             if (track->video.display_unit < MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN) {
2895                 if (track->video.display_width && track->video.display_height &&
2896                     st->codecpar->height  < INT64_MAX / track->video.display_width  / display_width_mul &&
2897                     st->codecpar->width   < INT64_MAX / track->video.display_height / display_height_mul)
2898                     av_reduce(&st->sample_aspect_ratio.num,
2899                               &st->sample_aspect_ratio.den,
2900                               st->codecpar->height * track->video.display_width  * display_width_mul,
2901                               st->codecpar->width  * track->video.display_height * display_height_mul,
2902                               INT_MAX);
2903             }
2904             if (st->codecpar->codec_id != AV_CODEC_ID_HEVC)
2905                 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
2906 
2907             if (track->default_duration) {
2908                 int div = track->default_duration <= INT64_MAX ? 1 : 2;
2909                 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
2910                           1000000000 / div, track->default_duration / div, 30000);
2911 #if FF_API_R_FRAME_RATE
2912                 if (   st->avg_frame_rate.num < st->avg_frame_rate.den * 1000LL
2913                     && st->avg_frame_rate.num > st->avg_frame_rate.den * 5LL)
2914                     st->r_frame_rate = st->avg_frame_rate;
2915 #endif
2916             }
2917 
2918             /* export stereo mode flag as metadata tag */
2919             if (track->video.stereo_mode && track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
2920                 av_dict_set(&st->metadata, "stereo_mode", ff_matroska_video_stereo_mode[track->video.stereo_mode], 0);
2921 
2922             /* export alpha mode flag as metadata tag  */
2923             if (track->video.alpha_mode)
2924                 av_dict_set(&st->metadata, "alpha_mode", "1", 0);
2925 
2926             /* if we have virtual track, mark the real tracks */
2927             for (j=0; j < track->operation.combine_planes.nb_elem; j++) {
2928                 char buf[32];
2929                 if (planes[j].type >= MATROSKA_VIDEO_STEREO_PLANE_COUNT)
2930                     continue;
2931                 snprintf(buf, sizeof(buf), "%s_%d",
2932                          ff_matroska_video_stereo_plane[planes[j].type], i);
2933                 for (k=0; k < matroska->tracks.nb_elem; k++)
2934                     if (planes[j].uid == tracks[k].uid && tracks[k].stream) {
2935                         av_dict_set(&tracks[k].stream->metadata,
2936                                     "stereo_mode", buf, 0);
2937                         break;
2938                     }
2939             }
2940             // add stream level stereo3d side data if it is a supported format
2941             if (track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
2942                 track->video.stereo_mode != 10 && track->video.stereo_mode != 12) {
2943                 int ret = ff_mkv_stereo3d_conv(st, track->video.stereo_mode);
2944                 if (ret < 0)
2945                     return ret;
2946             }
2947 
2948             ret = mkv_parse_video_color(st, track);
2949             if (ret < 0)
2950                 return ret;
2951             ret = mkv_parse_video_projection(st, track, matroska->ctx);
2952             if (ret < 0)
2953                 return ret;
2954         } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2955             st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
2956             st->codecpar->codec_tag   = fourcc;
2957             st->codecpar->sample_rate = track->audio.out_samplerate;
2958             // channel layout may be already set by codec private checks above
2959             if (!av_channel_layout_check(&st->codecpar->ch_layout)) {
2960                 st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
2961                 st->codecpar->ch_layout.nb_channels = track->audio.channels;
2962             }
2963             if (!st->codecpar->bits_per_coded_sample)
2964                 st->codecpar->bits_per_coded_sample = track->audio.bitdepth;
2965             if (st->codecpar->codec_id == AV_CODEC_ID_MP3 ||
2966                 st->codecpar->codec_id == AV_CODEC_ID_MLP ||
2967                 st->codecpar->codec_id == AV_CODEC_ID_TRUEHD)
2968                 sti->need_parsing = AVSTREAM_PARSE_FULL;
2969             else if (st->codecpar->codec_id != AV_CODEC_ID_AAC)
2970                 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
2971             if (track->codec_delay > 0) {
2972                 st->codecpar->initial_padding = av_rescale_q(track->codec_delay,
2973                                                              (AVRational){1, 1000000000},
2974                                                              (AVRational){1, st->codecpar->codec_id == AV_CODEC_ID_OPUS ?
2975                                                                              48000 : st->codecpar->sample_rate});
2976             }
2977             if (track->seek_preroll > 0) {
2978                 st->codecpar->seek_preroll = av_rescale_q(track->seek_preroll,
2979                                                           (AVRational){1, 1000000000},
2980                                                           (AVRational){1, st->codecpar->sample_rate});
2981             }
2982         } else if (codec_id == AV_CODEC_ID_WEBVTT) {
2983             st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
2984 
2985             if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) {
2986                 st->disposition |= AV_DISPOSITION_CAPTIONS;
2987             } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) {
2988                 st->disposition |= AV_DISPOSITION_DESCRIPTIONS;
2989             } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) {
2990                 st->disposition |= AV_DISPOSITION_METADATA;
2991             }
2992         } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2993             st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
2994 
2995             if (track->flag_textdescriptions)
2996                 st->disposition |= AV_DISPOSITION_DESCRIPTIONS;
2997         }
2998 
2999         ret = mkv_parse_block_addition_mappings(s, st, track);
3000         if (ret < 0)
3001             return ret;
3002     }
3003 
3004     return 0;
3005 }
3006 
matroska_read_header(AVFormatContext *s)3007 static int matroska_read_header(AVFormatContext *s)
3008 {
3009     FFFormatContext *const si = ffformatcontext(s);
3010     MatroskaDemuxContext *matroska = s->priv_data;
3011     EbmlList *attachments_list = &matroska->attachments;
3012     EbmlList *chapters_list    = &matroska->chapters;
3013     MatroskaAttachment *attachments;
3014     MatroskaChapter *chapters;
3015     uint64_t max_start = 0;
3016     int64_t pos;
3017     Ebml ebml = { 0 };
3018     int i, j, res;
3019 
3020     matroska->ctx = s;
3021     matroska->cues_parsing_deferred = 1;
3022 
3023     /* First read the EBML header. */
3024     if (ebml_parse(matroska, ebml_syntax, &ebml) || !ebml.doctype) {
3025         av_log(matroska->ctx, AV_LOG_ERROR, "EBML header parsing failed\n");
3026         ebml_free(ebml_syntax, &ebml);
3027         return AVERROR_INVALIDDATA;
3028     }
3029     if (ebml.version         > EBML_VERSION      ||
3030         ebml.max_size        > sizeof(uint64_t)  ||
3031         ebml.id_length       > sizeof(uint32_t)  ||
3032         ebml.doctype_version > 3) {
3033         avpriv_report_missing_feature(matroska->ctx,
3034                                       "EBML version %"PRIu64", doctype %s, doc version %"PRIu64,
3035                                       ebml.version, ebml.doctype, ebml.doctype_version);
3036         ebml_free(ebml_syntax, &ebml);
3037         return AVERROR_PATCHWELCOME;
3038     } else if (ebml.doctype_version == 3) {
3039         av_log(matroska->ctx, AV_LOG_WARNING,
3040                "EBML header using unsupported features\n"
3041                "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
3042                ebml.version, ebml.doctype, ebml.doctype_version);
3043     }
3044     for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++)
3045         if (!strcmp(ebml.doctype, matroska_doctypes[i]))
3046             break;
3047     if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) {
3048         av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype);
3049         if (matroska->ctx->error_recognition & AV_EF_EXPLODE) {
3050             ebml_free(ebml_syntax, &ebml);
3051             return AVERROR_INVALIDDATA;
3052         }
3053     }
3054     ebml_free(ebml_syntax, &ebml);
3055 
3056     matroska->pkt = si->parse_pkt;
3057 
3058     /* The next thing is a segment. */
3059     pos = avio_tell(matroska->ctx->pb);
3060     res = ebml_parse(matroska, matroska_segments, matroska);
3061     // Try resyncing until we find an EBML_STOP type element.
3062     while (res != 1) {
3063         res = matroska_resync(matroska, pos);
3064         if (res < 0)
3065             return res;
3066         pos = avio_tell(matroska->ctx->pb);
3067         res = ebml_parse(matroska, matroska_segment, matroska);
3068         if (res == AVERROR(EIO)) // EOF is translated to EIO, this exists the loop on EOF
3069             return res;
3070     }
3071     /* Set data_offset as it might be needed later by seek_frame_generic. */
3072     if (matroska->current_id == MATROSKA_ID_CLUSTER)
3073         si->data_offset = avio_tell(matroska->ctx->pb) - 4;
3074     matroska_execute_seekhead(matroska);
3075 
3076     if (!matroska->time_scale)
3077         matroska->time_scale = 1000000;
3078     if (isnan(matroska->duration))
3079         matroska->duration = 0;
3080     if (matroska->duration)
3081         matroska->ctx->duration = matroska->duration * matroska->time_scale *
3082                                   1000 / AV_TIME_BASE;
3083     av_dict_set(&s->metadata, "title", matroska->title, 0);
3084     av_dict_set(&s->metadata, "encoder", matroska->muxingapp, 0);
3085 
3086     if (matroska->date_utc.size == 8)
3087         matroska_metadata_creation_time(&s->metadata, AV_RB64(matroska->date_utc.data));
3088 
3089     res = matroska_parse_tracks(s);
3090     if (res < 0)
3091         return res;
3092 
3093     attachments = attachments_list->elem;
3094     for (j = 0; j < attachments_list->nb_elem; j++) {
3095         if (!(attachments[j].filename && attachments[j].mime &&
3096               attachments[j].bin.data && attachments[j].bin.size > 0)) {
3097             av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
3098         } else {
3099             AVStream *st = avformat_new_stream(s, NULL);
3100             if (!st)
3101                 break;
3102             av_dict_set(&st->metadata, "filename", attachments[j].filename, 0);
3103             av_dict_set(&st->metadata, "mimetype", attachments[j].mime, 0);
3104             if (attachments[j].description)
3105                 av_dict_set(&st->metadata, "title", attachments[j].description, 0);
3106             st->codecpar->codec_id   = AV_CODEC_ID_NONE;
3107 
3108             for (i = 0; mkv_image_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
3109                 if (av_strstart(attachments[j].mime, mkv_image_mime_tags[i].str, NULL)) {
3110                     st->codecpar->codec_id = mkv_image_mime_tags[i].id;
3111                     break;
3112                 }
3113             }
3114 
3115             attachments[j].stream = st;
3116 
3117             if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
3118                 res = ff_add_attached_pic(s, st, NULL, &attachments[j].bin.buf, 0);
3119                 if (res < 0)
3120                     return res;
3121             } else {
3122                 st->codecpar->codec_type = AVMEDIA_TYPE_ATTACHMENT;
3123                 if (ff_alloc_extradata(st->codecpar, attachments[j].bin.size))
3124                     break;
3125                 memcpy(st->codecpar->extradata, attachments[j].bin.data,
3126                        attachments[j].bin.size);
3127 
3128                 for (i = 0; mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++) {
3129                     if (av_strstart(attachments[j].mime, mkv_mime_tags[i].str, NULL)) {
3130                         st->codecpar->codec_id = mkv_mime_tags[i].id;
3131                         break;
3132                     }
3133                 }
3134             }
3135         }
3136     }
3137 
3138     chapters = chapters_list->elem;
3139     for (i = 0; i < chapters_list->nb_elem; i++)
3140         if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid &&
3141             (max_start == 0 || chapters[i].start > max_start)) {
3142             chapters[i].chapter =
3143                 avpriv_new_chapter(s, chapters[i].uid,
3144                                    (AVRational) { 1, 1000000000 },
3145                                    chapters[i].start, chapters[i].end,
3146                                    chapters[i].title);
3147             max_start = chapters[i].start;
3148         }
3149 
3150     matroska_add_index_entries(matroska);
3151 
3152     matroska_convert_tags(s);
3153 
3154     return 0;
3155 }
3156 
3157 /*
3158  * Put one packet in an application-supplied AVPacket struct.
3159  * Returns 0 on success or -1 on failure.
3160  */
matroska_deliver_packet(MatroskaDemuxContext *matroska, AVPacket *pkt)3161 static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
3162                                    AVPacket *pkt)
3163 {
3164     if (matroska->queue.head) {
3165         MatroskaTrack *tracks = matroska->tracks.elem;
3166         MatroskaTrack *track;
3167 
3168         avpriv_packet_list_get(&matroska->queue, pkt);
3169         track = &tracks[pkt->stream_index];
3170         if (track->has_palette) {
3171             uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
3172             if (!pal) {
3173                 av_log(matroska->ctx, AV_LOG_ERROR, "Cannot append palette to packet\n");
3174             } else {
3175                 memcpy(pal, track->palette, AVPALETTE_SIZE);
3176             }
3177             track->has_palette = 0;
3178         }
3179         return 0;
3180     }
3181 
3182     return -1;
3183 }
3184 
3185 /*
3186  * Free all packets in our internal queue.
3187  */
matroska_clear_queue(MatroskaDemuxContext *matroska)3188 static void matroska_clear_queue(MatroskaDemuxContext *matroska)
3189 {
3190     avpriv_packet_list_free(&matroska->queue);
3191 }
3192 
matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf, int size, int type, AVIOContext *pb, uint32_t lace_size[256], int *laces)3193 static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
3194                                 int size, int type, AVIOContext *pb,
3195                                 uint32_t lace_size[256], int *laces)
3196 {
3197     int n;
3198     uint8_t *data = *buf;
3199 
3200     if (!type) {
3201         *laces    = 1;
3202         lace_size[0] = size;
3203         return 0;
3204     }
3205 
3206     if (size <= 0)
3207         return AVERROR_INVALIDDATA;
3208 
3209     *laces = *data + 1;
3210     data  += 1;
3211     size  -= 1;
3212 
3213     switch (type) {
3214     case 0x1: /* Xiph lacing */
3215     {
3216         uint8_t temp;
3217         uint32_t total = 0;
3218         for (n = 0; n < *laces - 1; n++) {
3219             lace_size[n] = 0;
3220 
3221             do {
3222                 if (size <= total)
3223                     return AVERROR_INVALIDDATA;
3224                 temp          = *data;
3225                 total        += temp;
3226                 lace_size[n] += temp;
3227                 data         += 1;
3228                 size         -= 1;
3229             } while (temp ==  0xff);
3230         }
3231         if (size < total)
3232             return AVERROR_INVALIDDATA;
3233 
3234         lace_size[n] = size - total;
3235         break;
3236     }
3237 
3238     case 0x2: /* fixed-size lacing */
3239         if (size % (*laces))
3240             return AVERROR_INVALIDDATA;
3241         for (n = 0; n < *laces; n++)
3242             lace_size[n] = size / *laces;
3243         break;
3244 
3245     case 0x3: /* EBML lacing */
3246     {
3247         uint64_t num;
3248         uint64_t total;
3249         int offset;
3250 
3251         avio_skip(pb, 4);
3252 
3253         n = ebml_read_num(matroska, pb, 8, &num, 1);
3254         if (n < 0)
3255             return n;
3256         if (num > INT_MAX)
3257             return AVERROR_INVALIDDATA;
3258 
3259         total = lace_size[0] = num;
3260         offset = n;
3261         for (n = 1; n < *laces - 1; n++) {
3262             int64_t snum;
3263             int r;
3264             r = matroska_ebmlnum_sint(matroska, pb, &snum);
3265             if (r < 0)
3266                 return r;
3267             if (lace_size[n - 1] + snum > (uint64_t)INT_MAX)
3268                 return AVERROR_INVALIDDATA;
3269 
3270             lace_size[n] = lace_size[n - 1] + snum;
3271             total       += lace_size[n];
3272             offset      += r;
3273         }
3274         data += offset;
3275         size -= offset;
3276         if (size < total)
3277             return AVERROR_INVALIDDATA;
3278 
3279         lace_size[*laces - 1] = size - total;
3280         break;
3281     }
3282     }
3283 
3284     *buf = data;
3285 
3286     return 0;
3287 }
3288 
matroska_parse_rm_audio(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, uint8_t *data, int size, uint64_t timecode, int64_t pos)3289 static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska,
3290                                    MatroskaTrack *track, AVStream *st,
3291                                    uint8_t *data, int size, uint64_t timecode,
3292                                    int64_t pos)
3293 {
3294     const int a   = st->codecpar->block_align;
3295     const int sps = track->audio.sub_packet_size;
3296     const int cfs = track->audio.coded_framesize;
3297     const int h   = track->audio.sub_packet_h;
3298     const int w   = track->audio.frame_size;
3299     int y   = track->audio.sub_packet_cnt;
3300     int x;
3301 
3302     if (!track->audio.pkt_cnt) {
3303         if (track->audio.sub_packet_cnt == 0)
3304             track->audio.buf_timecode = timecode;
3305         if (st->codecpar->codec_id == AV_CODEC_ID_RA_288) {
3306             if (size < cfs * h / 2) {
3307                 av_log(matroska->ctx, AV_LOG_ERROR,
3308                        "Corrupt int4 RM-style audio packet size\n");
3309                 return AVERROR_INVALIDDATA;
3310             }
3311             for (x = 0; x < h / 2; x++)
3312                 memcpy(track->audio.buf + x * 2 * w + y * cfs,
3313                        data + x * cfs, cfs);
3314         } else if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) {
3315             if (size < w) {
3316                 av_log(matroska->ctx, AV_LOG_ERROR,
3317                        "Corrupt sipr RM-style audio packet size\n");
3318                 return AVERROR_INVALIDDATA;
3319             }
3320             memcpy(track->audio.buf + y * w, data, w);
3321         } else {
3322             if (size < w) {
3323                 av_log(matroska->ctx, AV_LOG_ERROR,
3324                        "Corrupt generic RM-style audio packet size\n");
3325                 return AVERROR_INVALIDDATA;
3326             }
3327             for (x = 0; x < w / sps; x++)
3328                 memcpy(track->audio.buf +
3329                        sps * (h * x + ((h + 1) / 2) * (y & 1) + (y >> 1)),
3330                        data + x * sps, sps);
3331         }
3332 
3333         if (++track->audio.sub_packet_cnt >= h) {
3334             if (st->codecpar->codec_id == AV_CODEC_ID_SIPR)
3335                 ff_rm_reorder_sipr_data(track->audio.buf, h, w);
3336             track->audio.sub_packet_cnt = 0;
3337             track->audio.pkt_cnt        = h * w / a;
3338         }
3339     }
3340 
3341     while (track->audio.pkt_cnt) {
3342         int ret;
3343         AVPacket *pkt = matroska->pkt;
3344 
3345         ret = av_new_packet(pkt, a);
3346         if (ret < 0) {
3347             return ret;
3348         }
3349         memcpy(pkt->data,
3350                track->audio.buf + a * (h * w / a - track->audio.pkt_cnt--),
3351                a);
3352         pkt->pts                  = track->audio.buf_timecode;
3353         track->audio.buf_timecode = AV_NOPTS_VALUE;
3354         pkt->pos                  = pos;
3355         pkt->stream_index         = st->index;
3356         ret = avpriv_packet_list_put(&matroska->queue, pkt, NULL, 0);
3357         if (ret < 0) {
3358             av_packet_unref(pkt);
3359             return AVERROR(ENOMEM);
3360         }
3361     }
3362 
3363     return 0;
3364 }
3365 
3366 /* reconstruct full wavpack blocks from mangled matroska ones */
matroska_parse_wavpack(MatroskaTrack *track, uint8_t **data, int *size)3367 static int matroska_parse_wavpack(MatroskaTrack *track,
3368                                   uint8_t **data, int *size)
3369 {
3370     uint8_t *dst = NULL;
3371     uint8_t *src = *data;
3372     int dstlen   = 0;
3373     int srclen   = *size;
3374     uint32_t samples;
3375     uint16_t ver;
3376     int ret, offset = 0;
3377 
3378     if (srclen < 12)
3379         return AVERROR_INVALIDDATA;
3380 
3381     av_assert1(track->stream->codecpar->extradata_size >= 2);
3382     ver = AV_RL16(track->stream->codecpar->extradata);
3383 
3384     samples = AV_RL32(src);
3385     src    += 4;
3386     srclen -= 4;
3387 
3388     while (srclen >= 8) {
3389         int multiblock;
3390         uint32_t blocksize;
3391         uint8_t *tmp;
3392 
3393         uint32_t flags = AV_RL32(src);
3394         uint32_t crc   = AV_RL32(src + 4);
3395         src    += 8;
3396         srclen -= 8;
3397 
3398         multiblock = (flags & 0x1800) != 0x1800;
3399         if (multiblock) {
3400             if (srclen < 4) {
3401                 ret = AVERROR_INVALIDDATA;
3402                 goto fail;
3403             }
3404             blocksize = AV_RL32(src);
3405             src      += 4;
3406             srclen   -= 4;
3407         } else
3408             blocksize = srclen;
3409 
3410         if (blocksize > srclen) {
3411             ret = AVERROR_INVALIDDATA;
3412             goto fail;
3413         }
3414 
3415         tmp = av_realloc(dst, dstlen + blocksize + 32 + AV_INPUT_BUFFER_PADDING_SIZE);
3416         if (!tmp) {
3417             ret = AVERROR(ENOMEM);
3418             goto fail;
3419         }
3420         dst     = tmp;
3421         dstlen += blocksize + 32;
3422 
3423         AV_WL32(dst + offset, MKTAG('w', 'v', 'p', 'k'));   // tag
3424         AV_WL32(dst + offset +  4, blocksize + 24);         // blocksize - 8
3425         AV_WL16(dst + offset +  8, ver);                    // version
3426         AV_WL16(dst + offset + 10, 0);                      // track/index_no
3427         AV_WL32(dst + offset + 12, 0);                      // total samples
3428         AV_WL32(dst + offset + 16, 0);                      // block index
3429         AV_WL32(dst + offset + 20, samples);                // number of samples
3430         AV_WL32(dst + offset + 24, flags);                  // flags
3431         AV_WL32(dst + offset + 28, crc);                    // crc
3432         memcpy(dst + offset + 32, src, blocksize);          // block data
3433 
3434         src    += blocksize;
3435         srclen -= blocksize;
3436         offset += blocksize + 32;
3437     }
3438 
3439     memset(dst + dstlen, 0, AV_INPUT_BUFFER_PADDING_SIZE);
3440 
3441     *data = dst;
3442     *size = dstlen;
3443 
3444     return 0;
3445 
3446 fail:
3447     av_freep(&dst);
3448     return ret;
3449 }
3450 
matroska_parse_prores(MatroskaTrack *track, uint8_t **data, int *size)3451 static int matroska_parse_prores(MatroskaTrack *track,
3452                                  uint8_t **data, int *size)
3453 {
3454     uint8_t *dst;
3455     int dstlen = *size + 8;
3456 
3457     dst = av_malloc(dstlen + AV_INPUT_BUFFER_PADDING_SIZE);
3458     if (!dst)
3459         return AVERROR(ENOMEM);
3460 
3461     AV_WB32(dst, dstlen);
3462     AV_WB32(dst + 4, MKBETAG('i', 'c', 'p', 'f'));
3463     memcpy(dst + 8, *data, dstlen - 8);
3464     memset(dst + dstlen, 0, AV_INPUT_BUFFER_PADDING_SIZE);
3465 
3466     *data = dst;
3467     *size = dstlen;
3468 
3469     return 0;
3470 }
3471 
matroska_parse_webvtt(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, uint8_t *data, int data_len, uint64_t timecode, uint64_t duration, int64_t pos)3472 static int matroska_parse_webvtt(MatroskaDemuxContext *matroska,
3473                                  MatroskaTrack *track,
3474                                  AVStream *st,
3475                                  uint8_t *data, int data_len,
3476                                  uint64_t timecode,
3477                                  uint64_t duration,
3478                                  int64_t pos)
3479 {
3480     AVPacket *pkt = matroska->pkt;
3481     uint8_t *id, *settings, *text, *buf;
3482     int id_len, settings_len, text_len;
3483     uint8_t *p, *q;
3484     int err;
3485 
3486     if (data_len <= 0)
3487         return AVERROR_INVALIDDATA;
3488 
3489     p = data;
3490     q = data + data_len;
3491 
3492     id = p;
3493     id_len = -1;
3494     while (p < q) {
3495         if (*p == '\r' || *p == '\n') {
3496             id_len = p - id;
3497             if (*p == '\r')
3498                 p++;
3499             break;
3500         }
3501         p++;
3502     }
3503 
3504     if (p >= q || *p != '\n')
3505         return AVERROR_INVALIDDATA;
3506     p++;
3507 
3508     settings = p;
3509     settings_len = -1;
3510     while (p < q) {
3511         if (*p == '\r' || *p == '\n') {
3512             settings_len = p - settings;
3513             if (*p == '\r')
3514                 p++;
3515             break;
3516         }
3517         p++;
3518     }
3519 
3520     if (p >= q || *p != '\n')
3521         return AVERROR_INVALIDDATA;
3522     p++;
3523 
3524     text = p;
3525     text_len = q - p;
3526     while (text_len > 0) {
3527         const int len = text_len - 1;
3528         const uint8_t c = p[len];
3529         if (c != '\r' && c != '\n')
3530             break;
3531         text_len = len;
3532     }
3533 
3534     if (text_len <= 0)
3535         return AVERROR_INVALIDDATA;
3536 
3537     err = av_new_packet(pkt, text_len);
3538     if (err < 0) {
3539         return err;
3540     }
3541 
3542     memcpy(pkt->data, text, text_len);
3543 
3544     if (id_len > 0) {
3545         buf = av_packet_new_side_data(pkt,
3546                                       AV_PKT_DATA_WEBVTT_IDENTIFIER,
3547                                       id_len);
3548         if (!buf) {
3549             av_packet_unref(pkt);
3550             return AVERROR(ENOMEM);
3551         }
3552         memcpy(buf, id, id_len);
3553     }
3554 
3555     if (settings_len > 0) {
3556         buf = av_packet_new_side_data(pkt,
3557                                       AV_PKT_DATA_WEBVTT_SETTINGS,
3558                                       settings_len);
3559         if (!buf) {
3560             av_packet_unref(pkt);
3561             return AVERROR(ENOMEM);
3562         }
3563         memcpy(buf, settings, settings_len);
3564     }
3565 
3566     // Do we need this for subtitles?
3567     // pkt->flags = AV_PKT_FLAG_KEY;
3568 
3569     pkt->stream_index = st->index;
3570     pkt->pts = timecode;
3571 
3572     // Do we need this for subtitles?
3573     // pkt->dts = timecode;
3574 
3575     pkt->duration = duration;
3576     pkt->pos = pos;
3577 
3578     err = avpriv_packet_list_put(&matroska->queue, pkt, NULL, 0);
3579     if (err < 0) {
3580         av_packet_unref(pkt);
3581         return AVERROR(ENOMEM);
3582     }
3583 
3584     return 0;
3585 }
3586 
matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, AVBufferRef *buf, uint8_t *data, int pkt_size, uint64_t timecode, uint64_t lace_duration, int64_t pos, int is_keyframe, uint8_t *additional, uint64_t additional_id, int additional_size, int64_t discard_padding)3587 static int matroska_parse_frame(MatroskaDemuxContext *matroska,
3588                                 MatroskaTrack *track, AVStream *st,
3589                                 AVBufferRef *buf, uint8_t *data, int pkt_size,
3590                                 uint64_t timecode, uint64_t lace_duration,
3591                                 int64_t pos, int is_keyframe,
3592                                 uint8_t *additional, uint64_t additional_id, int additional_size,
3593                                 int64_t discard_padding)
3594 {
3595     uint8_t *pkt_data = data;
3596     int res = 0;
3597     AVPacket *pkt = matroska->pkt;
3598 
3599     if (st->codecpar->codec_id == AV_CODEC_ID_WAVPACK) {
3600         res = matroska_parse_wavpack(track, &pkt_data, &pkt_size);
3601         if (res < 0) {
3602             av_log(matroska->ctx, AV_LOG_ERROR,
3603                    "Error parsing a wavpack block.\n");
3604             goto fail;
3605         }
3606         if (!buf)
3607             av_freep(&data);
3608         buf = NULL;
3609     }
3610 
3611     if (st->codecpar->codec_id == AV_CODEC_ID_PRORES &&
3612         AV_RB32(pkt_data + 4)  != MKBETAG('i', 'c', 'p', 'f')) {
3613         res = matroska_parse_prores(track, &pkt_data, &pkt_size);
3614         if (res < 0) {
3615             av_log(matroska->ctx, AV_LOG_ERROR,
3616                    "Error parsing a prores block.\n");
3617             goto fail;
3618         }
3619         if (!buf)
3620             av_freep(&data);
3621         buf = NULL;
3622     }
3623 
3624     if (!pkt_size && !additional_size)
3625         goto no_output;
3626 
3627     if (!buf)
3628         pkt->buf = av_buffer_create(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE,
3629                                     NULL, NULL, 0);
3630     else
3631         pkt->buf = av_buffer_ref(buf);
3632 
3633     if (!pkt->buf) {
3634         res = AVERROR(ENOMEM);
3635         goto fail;
3636     }
3637 
3638     pkt->data         = pkt_data;
3639     pkt->size         = pkt_size;
3640     pkt->flags        = is_keyframe;
3641     pkt->stream_index = st->index;
3642 
3643     if (additional_size > 0) {
3644         uint8_t *side_data = av_packet_new_side_data(pkt,
3645                                                      AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
3646                                                      additional_size + 8);
3647         if (!side_data) {
3648             av_packet_unref(pkt);
3649             return AVERROR(ENOMEM);
3650         }
3651         AV_WB64(side_data, additional_id);
3652         memcpy(side_data + 8, additional, additional_size);
3653     }
3654 
3655     if (discard_padding) {
3656         uint8_t *side_data = av_packet_new_side_data(pkt,
3657                                                      AV_PKT_DATA_SKIP_SAMPLES,
3658                                                      10);
3659         if (!side_data) {
3660             av_packet_unref(pkt);
3661             return AVERROR(ENOMEM);
3662         }
3663         discard_padding = av_rescale_q(discard_padding,
3664                                             (AVRational){1, 1000000000},
3665                                             (AVRational){1, st->codecpar->sample_rate});
3666         if (discard_padding > 0) {
3667             AV_WL32(side_data + 4, discard_padding);
3668         } else {
3669             AV_WL32(side_data, -discard_padding);
3670         }
3671     }
3672 
3673     if (track->ms_compat)
3674         pkt->dts = timecode;
3675     else
3676         pkt->pts = timecode;
3677     pkt->pos = pos;
3678     pkt->duration = lace_duration;
3679 
3680     res = avpriv_packet_list_put(&matroska->queue, pkt, NULL, 0);
3681     if (res < 0) {
3682         av_packet_unref(pkt);
3683         return AVERROR(ENOMEM);
3684     }
3685 
3686     return 0;
3687 
3688 no_output:
3689 fail:
3690     if (!buf)
3691         av_free(pkt_data);
3692     return res;
3693 }
3694 
matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf, uint8_t *data, int size, int64_t pos, uint64_t cluster_time, uint64_t block_duration, int is_keyframe, uint8_t *additional, uint64_t additional_id, int additional_size, int64_t cluster_pos, int64_t discard_padding)3695 static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf, uint8_t *data,
3696                                 int size, int64_t pos, uint64_t cluster_time,
3697                                 uint64_t block_duration, int is_keyframe,
3698                                 uint8_t *additional, uint64_t additional_id, int additional_size,
3699                                 int64_t cluster_pos, int64_t discard_padding)
3700 {
3701     uint64_t timecode = AV_NOPTS_VALUE;
3702     MatroskaTrack *track;
3703     FFIOContext pb;
3704     int res = 0;
3705     AVStream *st;
3706     int16_t block_time;
3707     uint32_t lace_size[256];
3708     int n, flags, laces = 0;
3709     uint64_t num;
3710     int trust_default_duration;
3711 
3712     av_assert1(buf);
3713 
3714     ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL);
3715 
3716     if ((n = ebml_read_num(matroska, &pb.pub, 8, &num, 1)) < 0)
3717         return n;
3718     data += n;
3719     size -= n;
3720 
3721     track = matroska_find_track_by_num(matroska, num);
3722     if (!track || size < 3)
3723         return AVERROR_INVALIDDATA;
3724 
3725     if (!(st = track->stream)) {
3726         av_log(matroska->ctx, AV_LOG_VERBOSE,
3727                "No stream associated to TrackNumber %"PRIu64". "
3728                "Ignoring Block with this TrackNumber.\n", num);
3729         return 0;
3730     }
3731 
3732     if (st->discard >= AVDISCARD_ALL)
3733         return res;
3734     if (block_duration > INT64_MAX)
3735         block_duration = INT64_MAX;
3736 
3737     block_time = sign_extend(AV_RB16(data), 16);
3738     data      += 2;
3739     flags      = *data++;
3740     size      -= 3;
3741     if (is_keyframe == -1)
3742         is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0;
3743 
3744     if (cluster_time != (uint64_t) -1 &&
3745         (block_time >= 0 || cluster_time >= -block_time)) {
3746         uint64_t timecode_cluster_in_track_tb = (double) cluster_time / track->time_scale;
3747         timecode = timecode_cluster_in_track_tb + block_time - track->codec_delay_in_track_tb;
3748         if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE &&
3749             timecode < track->end_timecode)
3750             is_keyframe = 0;  /* overlapping subtitles are not key frame */
3751         if (is_keyframe) {
3752             ff_reduce_index(matroska->ctx, st->index);
3753             av_add_index_entry(st, cluster_pos, timecode, 0, 0,
3754                                AVINDEX_KEYFRAME);
3755         }
3756     }
3757 
3758     if (matroska->skip_to_keyframe &&
3759         track->type != MATROSKA_TRACK_TYPE_SUBTITLE) {
3760         // Compare signed timecodes. Timecode may be negative due to codec delay
3761         // offset. We don't support timestamps greater than int64_t anyway - see
3762         // AVPacket's pts.
3763         if ((int64_t)timecode < (int64_t)matroska->skip_to_timecode)
3764             return res;
3765         if (is_keyframe)
3766             matroska->skip_to_keyframe = 0;
3767         else if (!ffstream(st)->skip_to_keyframe) {
3768             av_log(matroska->ctx, AV_LOG_ERROR, "File is broken, keyframes not correctly marked!\n");
3769             matroska->skip_to_keyframe = 0;
3770         }
3771     }
3772 
3773     res = matroska_parse_laces(matroska, &data, size, (flags & 0x06) >> 1,
3774                                &pb.pub, lace_size, &laces);
3775     if (res < 0) {
3776         av_log(matroska->ctx, AV_LOG_ERROR, "Error parsing frame sizes.\n");
3777         return res;
3778     }
3779 
3780     trust_default_duration = track->default_duration != 0;
3781     if (track->audio.samplerate == 8000 && trust_default_duration) {
3782         // If this is needed for more codecs, then add them here
3783         if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
3784             if (track->audio.samplerate != st->codecpar->sample_rate || !st->codecpar->frame_size)
3785                 trust_default_duration = 0;
3786         }
3787     }
3788 
3789     if (!block_duration && trust_default_duration)
3790         block_duration = track->default_duration * laces / matroska->time_scale;
3791 
3792     if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time))
3793         track->end_timecode =
3794             FFMAX(track->end_timecode, timecode + block_duration);
3795 
3796     for (n = 0; n < laces; n++) {
3797         int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces;
3798         uint8_t *out_data = data;
3799         int      out_size = lace_size[n];
3800 
3801         if (track->needs_decoding) {
3802             res = matroska_decode_buffer(&out_data, &out_size, track);
3803             if (res < 0)
3804                 return res;
3805             /* Given that we are here means that out_data is no longer
3806              * owned by buf, so set it to NULL. This depends upon
3807              * zero-length header removal compression being ignored. */
3808             av_assert1(out_data != data);
3809             buf = NULL;
3810         }
3811 
3812         if (track->audio.buf) {
3813             res = matroska_parse_rm_audio(matroska, track, st,
3814                                           out_data, out_size,
3815                                           timecode, pos);
3816             if (!buf)
3817                 av_free(out_data);
3818             if (res)
3819                 return res;
3820         } else if (st->codecpar->codec_id == AV_CODEC_ID_WEBVTT) {
3821             res = matroska_parse_webvtt(matroska, track, st,
3822                                         out_data, out_size,
3823                                         timecode, lace_duration,
3824                                         pos);
3825             if (!buf)
3826                 av_free(out_data);
3827             if (res)
3828                 return res;
3829         } else {
3830             res = matroska_parse_frame(matroska, track, st, buf, out_data,
3831                                        out_size, timecode, lace_duration,
3832                                        pos, !n ? is_keyframe : 0,
3833                                        additional, additional_id, additional_size,
3834                                        discard_padding);
3835             if (res)
3836                 return res;
3837         }
3838 
3839         if (timecode != AV_NOPTS_VALUE)
3840             timecode = lace_duration ? timecode + lace_duration : AV_NOPTS_VALUE;
3841         data += lace_size[n];
3842     }
3843 
3844     return 0;
3845 }
3846 
matroska_parse_cluster(MatroskaDemuxContext *matroska)3847 static int matroska_parse_cluster(MatroskaDemuxContext *matroska)
3848 {
3849     MatroskaCluster *cluster = &matroska->current_cluster;
3850     MatroskaBlock     *block = &cluster->block;
3851     int res;
3852 
3853     av_assert0(matroska->num_levels <= 2);
3854 
3855     if (matroska->num_levels == 1) {
3856         res = ebml_parse(matroska, matroska_segment, NULL);
3857 
3858         if (res == 1) {
3859             /* Found a cluster: subtract the size of the ID already read. */
3860             cluster->pos = avio_tell(matroska->ctx->pb) - 4;
3861 
3862             res = ebml_parse(matroska, matroska_cluster_enter, cluster);
3863             if (res < 0)
3864                 return res;
3865         }
3866     }
3867 
3868     if (matroska->num_levels == 2) {
3869         /* We are inside a cluster. */
3870         res = ebml_parse(matroska, matroska_cluster_parsing, cluster);
3871 
3872         if (res >= 0 && block->bin.size > 0) {
3873             int is_keyframe = block->non_simple ? block->reference.count == 0 : -1;
3874             uint8_t* additional = block->additional.size > 0 ?
3875                                     block->additional.data : NULL;
3876 
3877             res = matroska_parse_block(matroska, block->bin.buf, block->bin.data,
3878                                        block->bin.size, block->bin.pos,
3879                                        cluster->timecode, block->duration,
3880                                        is_keyframe, additional, block->additional_id,
3881                                        block->additional.size, cluster->pos,
3882                                        block->discard_padding);
3883         }
3884 
3885         ebml_free(matroska_blockgroup, block);
3886         memset(block, 0, sizeof(*block));
3887     } else if (!matroska->num_levels) {
3888         if (!avio_feof(matroska->ctx->pb)) {
3889             avio_r8(matroska->ctx->pb);
3890             if (!avio_feof(matroska->ctx->pb)) {
3891                 av_log(matroska->ctx, AV_LOG_WARNING, "File extends beyond "
3892                        "end of segment.\n");
3893                 return AVERROR_INVALIDDATA;
3894             }
3895         }
3896         matroska->done = 1;
3897         return AVERROR_EOF;
3898     }
3899 
3900     return res;
3901 }
3902 
matroska_read_packet(AVFormatContext *s, AVPacket *pkt)3903 static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt)
3904 {
3905     MatroskaDemuxContext *matroska = s->priv_data;
3906     int ret = 0;
3907 
3908     if (matroska->resync_pos == -1) {
3909         // This can only happen if generic seeking has been used.
3910         matroska->resync_pos = avio_tell(s->pb);
3911     }
3912 
3913     while (matroska_deliver_packet(matroska, pkt)) {
3914         if (matroska->done)
3915             return (ret < 0) ? ret : AVERROR_EOF;
3916         if (matroska_parse_cluster(matroska) < 0 && !matroska->done)
3917             ret = matroska_resync(matroska, matroska->resync_pos);
3918     }
3919 
3920     return 0;
3921 }
3922 
matroska_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)3923 static int matroska_read_seek(AVFormatContext *s, int stream_index,
3924                               int64_t timestamp, int flags)
3925 {
3926     MatroskaDemuxContext *matroska = s->priv_data;
3927     MatroskaTrack *tracks = NULL;
3928     AVStream *st = s->streams[stream_index];
3929     FFStream *const sti = ffstream(st);
3930     int i, index;
3931 
3932     /* Parse the CUES now since we need the index data to seek. */
3933     if (matroska->cues_parsing_deferred > 0) {
3934         matroska->cues_parsing_deferred = 0;
3935         matroska_parse_cues(matroska);
3936     }
3937 
3938     if (!sti->nb_index_entries)
3939         goto err;
3940     timestamp = FFMAX(timestamp, sti->index_entries[0].timestamp);
3941 
3942     if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 ||
3943          index == sti->nb_index_entries - 1) {
3944         matroska_reset_status(matroska, 0, sti->index_entries[sti->nb_index_entries - 1].pos);
3945         while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 ||
3946                index == sti->nb_index_entries - 1) {
3947             matroska_clear_queue(matroska);
3948             if (matroska_parse_cluster(matroska) < 0)
3949                 break;
3950         }
3951     }
3952 
3953     matroska_clear_queue(matroska);
3954     if (index < 0 || (matroska->cues_parsing_deferred < 0 &&
3955                       index == sti->nb_index_entries - 1))
3956         goto err;
3957 
3958     tracks = matroska->tracks.elem;
3959     for (i = 0; i < matroska->tracks.nb_elem; i++) {
3960         tracks[i].audio.pkt_cnt        = 0;
3961         tracks[i].audio.sub_packet_cnt = 0;
3962         tracks[i].audio.buf_timecode   = AV_NOPTS_VALUE;
3963         tracks[i].end_timecode         = 0;
3964     }
3965 
3966     /* We seek to a level 1 element, so set the appropriate status. */
3967     matroska_reset_status(matroska, 0, sti->index_entries[index].pos);
3968     if (flags & AVSEEK_FLAG_ANY) {
3969         sti->skip_to_keyframe = 0;
3970         matroska->skip_to_timecode = timestamp;
3971     } else {
3972         sti->skip_to_keyframe = 1;
3973         matroska->skip_to_timecode = sti->index_entries[index].timestamp;
3974     }
3975     matroska->skip_to_keyframe = 1;
3976     matroska->done             = 0;
3977     avpriv_update_cur_dts(s, st, sti->index_entries[index].timestamp);
3978     return 0;
3979 err:
3980     // slightly hackish but allows proper fallback to
3981     // the generic seeking code.
3982     matroska_reset_status(matroska, 0, -1);
3983     matroska->resync_pos = -1;
3984     matroska_clear_queue(matroska);
3985     sti->skip_to_keyframe =
3986     matroska->skip_to_keyframe = 0;
3987     matroska->done = 0;
3988     return -1;
3989 }
3990 
matroska_read_close(AVFormatContext *s)3991 static int matroska_read_close(AVFormatContext *s)
3992 {
3993     MatroskaDemuxContext *matroska = s->priv_data;
3994     MatroskaTrack *tracks = matroska->tracks.elem;
3995     int n;
3996 
3997     matroska_clear_queue(matroska);
3998 
3999     for (n = 0; n < matroska->tracks.nb_elem; n++)
4000         if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO)
4001             av_freep(&tracks[n].audio.buf);
4002     ebml_free(matroska_segment, matroska);
4003 
4004     return 0;
4005 }
4006 
4007 #if CONFIG_WEBM_DASH_MANIFEST_DEMUXER
4008 typedef struct {
4009     int64_t start_time_ns;
4010     int64_t end_time_ns;
4011     int64_t start_offset;
4012     int64_t end_offset;
4013 } CueDesc;
4014 
4015 /* This function searches all the Cues and returns the CueDesc corresponding to
4016  * the timestamp ts. Returned CueDesc will be such that start_time_ns <= ts <
4017  * end_time_ns. All 4 fields will be set to -1 if ts >= file's duration.
4018  */
get_cue_desc(AVFormatContext *s, int64_t ts, int64_t cues_start)4019 static CueDesc get_cue_desc(AVFormatContext *s, int64_t ts, int64_t cues_start) {
4020     MatroskaDemuxContext *matroska = s->priv_data;
4021     FFStream *const sti = ffstream(s->streams[0]);
4022     AVIndexEntry *const index_entries = sti->index_entries;
4023     int nb_index_entries = sti->nb_index_entries;
4024     CueDesc cue_desc;
4025     int i;
4026 
4027     if (ts >= (int64_t)(matroska->duration * matroska->time_scale))
4028         return (CueDesc) {-1, -1, -1, -1};
4029     for (i = 1; i < nb_index_entries; i++) {
4030         if (index_entries[i - 1].timestamp * matroska->time_scale <= ts &&
4031             index_entries[i].timestamp * matroska->time_scale > ts) {
4032             break;
4033         }
4034     }
4035     --i;
4036     cue_desc.start_time_ns = index_entries[i].timestamp * matroska->time_scale;
4037     cue_desc.start_offset = index_entries[i].pos - matroska->segment_start;
4038     if (i != nb_index_entries - 1) {
4039         cue_desc.end_time_ns = index_entries[i + 1].timestamp * matroska->time_scale;
4040         cue_desc.end_offset = index_entries[i + 1].pos - matroska->segment_start;
4041     } else {
4042         cue_desc.end_time_ns = matroska->duration * matroska->time_scale;
4043         // FIXME: this needs special handling for files where Cues appear
4044         // before Clusters. the current logic assumes Cues appear after
4045         // Clusters.
4046         cue_desc.end_offset = cues_start - matroska->segment_start;
4047     }
4048     return cue_desc;
4049 }
4050 
webm_clusters_start_with_keyframe(AVFormatContext *s)4051 static int webm_clusters_start_with_keyframe(AVFormatContext *s)
4052 {
4053     MatroskaDemuxContext *matroska = s->priv_data;
4054     AVStream *const st  = s->streams[0];
4055     FFStream *const sti = ffstream(st);
4056     uint32_t id = matroska->current_id;
4057     int64_t cluster_pos, before_pos;
4058     int index, rv = 1;
4059 
4060     if (sti->nb_index_entries <= 0)
4061         return 0;
4062 
4063     // seek to the first cluster using cues.
4064     index = av_index_search_timestamp(st, 0, 0);
4065     if (index < 0)
4066         return 0;
4067     cluster_pos = sti->index_entries[index].pos;
4068     before_pos = avio_tell(s->pb);
4069     while (1) {
4070         uint64_t cluster_id, cluster_length;
4071         int read;
4072         AVPacket *pkt;
4073         avio_seek(s->pb, cluster_pos, SEEK_SET);
4074         // read cluster id and length
4075         read = ebml_read_num(matroska, matroska->ctx->pb, 4, &cluster_id, 1);
4076         if (read < 0 || cluster_id != 0xF43B675) // done with all clusters
4077             break;
4078         read = ebml_read_length(matroska, matroska->ctx->pb, &cluster_length);
4079         if (read < 0)
4080             break;
4081 
4082         matroska_reset_status(matroska, 0, cluster_pos);
4083         matroska_clear_queue(matroska);
4084         if (matroska_parse_cluster(matroska) < 0 ||
4085             !matroska->queue.head) {
4086             break;
4087         }
4088         pkt = &matroska->queue.head->pkt;
4089         // 4 + read is the length of the cluster id and the cluster length field.
4090         cluster_pos += 4 + read + cluster_length;
4091         if (!(pkt->flags & AV_PKT_FLAG_KEY)) {
4092             rv = 0;
4093             break;
4094         }
4095     }
4096 
4097     /* Restore the status after matroska_read_header: */
4098     matroska_reset_status(matroska, id, before_pos);
4099 
4100     return rv;
4101 }
4102 
buffer_size_after_time_downloaded(int64_t time_ns, double search_sec, int64_t bps, double min_buffer, double* buffer, double* sec_to_download, AVFormatContext *s, int64_t cues_start)4103 static int buffer_size_after_time_downloaded(int64_t time_ns, double search_sec, int64_t bps,
4104                                              double min_buffer, double* buffer,
4105                                              double* sec_to_download, AVFormatContext *s,
4106                                              int64_t cues_start)
4107 {
4108     double nano_seconds_per_second = 1000000000.0;
4109     double time_sec = time_ns / nano_seconds_per_second;
4110     int rv = 0;
4111     int64_t time_to_search_ns = (int64_t)(search_sec * nano_seconds_per_second);
4112     int64_t end_time_ns = time_ns + time_to_search_ns;
4113     double sec_downloaded = 0.0;
4114     CueDesc desc_curr = get_cue_desc(s, time_ns, cues_start);
4115     if (desc_curr.start_time_ns == -1)
4116       return -1;
4117     *sec_to_download = 0.0;
4118 
4119     // Check for non cue start time.
4120     if (time_ns > desc_curr.start_time_ns) {
4121       int64_t cue_nano = desc_curr.end_time_ns - time_ns;
4122       double percent = (double)(cue_nano) / (desc_curr.end_time_ns - desc_curr.start_time_ns);
4123       double cueBytes = (desc_curr.end_offset - desc_curr.start_offset) * percent;
4124       double timeToDownload = (cueBytes * 8.0) / bps;
4125 
4126       sec_downloaded += (cue_nano / nano_seconds_per_second) - timeToDownload;
4127       *sec_to_download += timeToDownload;
4128 
4129       // Check if the search ends within the first cue.
4130       if (desc_curr.end_time_ns >= end_time_ns) {
4131           double desc_end_time_sec = desc_curr.end_time_ns / nano_seconds_per_second;
4132           double percent_to_sub = search_sec / (desc_end_time_sec - time_sec);
4133           sec_downloaded = percent_to_sub * sec_downloaded;
4134           *sec_to_download = percent_to_sub * *sec_to_download;
4135       }
4136 
4137       if ((sec_downloaded + *buffer) <= min_buffer) {
4138           return 1;
4139       }
4140 
4141       // Get the next Cue.
4142       desc_curr = get_cue_desc(s, desc_curr.end_time_ns, cues_start);
4143     }
4144 
4145     while (desc_curr.start_time_ns != -1) {
4146         int64_t desc_bytes = desc_curr.end_offset - desc_curr.start_offset;
4147         int64_t desc_ns = desc_curr.end_time_ns - desc_curr.start_time_ns;
4148         double desc_sec = desc_ns / nano_seconds_per_second;
4149         double bits = (desc_bytes * 8.0);
4150         double time_to_download = bits / bps;
4151 
4152         sec_downloaded += desc_sec - time_to_download;
4153         *sec_to_download += time_to_download;
4154 
4155         if (desc_curr.end_time_ns >= end_time_ns) {
4156             double desc_end_time_sec = desc_curr.end_time_ns / nano_seconds_per_second;
4157             double percent_to_sub = search_sec / (desc_end_time_sec - time_sec);
4158             sec_downloaded = percent_to_sub * sec_downloaded;
4159             *sec_to_download = percent_to_sub * *sec_to_download;
4160 
4161             if ((sec_downloaded + *buffer) <= min_buffer)
4162                 rv = 1;
4163             break;
4164         }
4165 
4166         if ((sec_downloaded + *buffer) <= min_buffer) {
4167             rv = 1;
4168             break;
4169         }
4170 
4171         desc_curr = get_cue_desc(s, desc_curr.end_time_ns, cues_start);
4172     }
4173     *buffer = *buffer + sec_downloaded;
4174     return rv;
4175 }
4176 
4177 /* This function computes the bandwidth of the WebM file with the help of
4178  * buffer_size_after_time_downloaded() function. Both of these functions are
4179  * adapted from WebM Tools project and are adapted to work with FFmpeg's
4180  * Matroska parsing mechanism.
4181  *
4182  * Returns the bandwidth of the file on success; -1 on error.
4183  * */
webm_dash_manifest_compute_bandwidth(AVFormatContext *s, int64_t cues_start)4184 static int64_t webm_dash_manifest_compute_bandwidth(AVFormatContext *s, int64_t cues_start)
4185 {
4186     MatroskaDemuxContext *matroska = s->priv_data;
4187     AVStream *st = s->streams[0];
4188     FFStream *const sti = ffstream(st);
4189     double bandwidth = 0.0;
4190 
4191     for (int i = 0; i < sti->nb_index_entries; i++) {
4192         int64_t prebuffer_ns = 1000000000;
4193         int64_t time_ns = sti->index_entries[i].timestamp * matroska->time_scale;
4194         double nano_seconds_per_second = 1000000000.0;
4195         int64_t prebuffered_ns;
4196         double prebuffer_bytes = 0.0;
4197         int64_t temp_prebuffer_ns = prebuffer_ns;
4198         int64_t pre_bytes, pre_ns;
4199         double pre_sec, prebuffer, bits_per_second;
4200         CueDesc desc_beg = get_cue_desc(s, time_ns, cues_start);
4201         // Start with the first Cue.
4202         CueDesc desc_end = desc_beg;
4203 
4204         if (time_ns > INT64_MAX - prebuffer_ns)
4205             return -1;
4206         prebuffered_ns = time_ns + prebuffer_ns;
4207 
4208         // Figure out how much data we have downloaded for the prebuffer. This will
4209         // be used later to adjust the bits per sample to try.
4210         while (desc_end.start_time_ns != -1 && desc_end.end_time_ns < prebuffered_ns) {
4211             // Prebuffered the entire Cue.
4212             prebuffer_bytes += desc_end.end_offset - desc_end.start_offset;
4213             temp_prebuffer_ns -= desc_end.end_time_ns - desc_end.start_time_ns;
4214             desc_end = get_cue_desc(s, desc_end.end_time_ns, cues_start);
4215         }
4216         if (desc_end.start_time_ns == -1) {
4217             // The prebuffer is larger than the duration.
4218             if (matroska->duration * matroska->time_scale >= prebuffered_ns)
4219               return -1;
4220             bits_per_second = 0.0;
4221         } else {
4222             // The prebuffer ends in the last Cue. Estimate how much data was
4223             // prebuffered.
4224             pre_bytes = desc_end.end_offset - desc_end.start_offset;
4225             pre_ns = desc_end.end_time_ns - desc_end.start_time_ns;
4226             if (pre_ns <= 0)
4227                 return -1;
4228             pre_sec = pre_ns / nano_seconds_per_second;
4229             prebuffer_bytes +=
4230                 pre_bytes * ((temp_prebuffer_ns / nano_seconds_per_second) / pre_sec);
4231 
4232             prebuffer = prebuffer_ns / nano_seconds_per_second;
4233 
4234             // Set this to 0.0 in case our prebuffer buffers the entire video.
4235             bits_per_second = 0.0;
4236             do {
4237                 int64_t desc_bytes = desc_end.end_offset - desc_beg.start_offset;
4238                 int64_t desc_ns = desc_end.end_time_ns - desc_beg.start_time_ns;
4239                 double desc_sec, calc_bits_per_second, percent, mod_bits_per_second;
4240                 if (desc_bytes <= 0)
4241                     return -1;
4242 
4243                 desc_sec = desc_ns / nano_seconds_per_second;
4244                 calc_bits_per_second = (desc_bytes * 8) / desc_sec;
4245 
4246                 // Drop the bps by the percentage of bytes buffered.
4247                 percent = (desc_bytes - prebuffer_bytes) / desc_bytes;
4248                 mod_bits_per_second = calc_bits_per_second * percent;
4249 
4250                 if (prebuffer < desc_sec) {
4251                     double search_sec =
4252                         (double)(matroska->duration * matroska->time_scale) / nano_seconds_per_second;
4253 
4254                     // Add 1 so the bits per second should be a little bit greater than file
4255                     // datarate.
4256                     int64_t bps = (int64_t)(mod_bits_per_second) + 1;
4257                     const double min_buffer = 0.0;
4258                     double buffer = prebuffer;
4259                     double sec_to_download = 0.0;
4260 
4261                     int rv = buffer_size_after_time_downloaded(prebuffered_ns, search_sec, bps,
4262                                                                min_buffer, &buffer, &sec_to_download,
4263                                                                s, cues_start);
4264                     if (rv < 0) {
4265                         return -1;
4266                     } else if (rv == 0) {
4267                         bits_per_second = (double)(bps);
4268                         break;
4269                     }
4270                 }
4271 
4272                 desc_end = get_cue_desc(s, desc_end.end_time_ns, cues_start);
4273             } while (desc_end.start_time_ns != -1);
4274         }
4275         if (bandwidth < bits_per_second) bandwidth = bits_per_second;
4276     }
4277     return (int64_t)bandwidth;
4278 }
4279 
webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range)4280 static int webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range)
4281 {
4282     MatroskaDemuxContext *matroska = s->priv_data;
4283     EbmlList *seekhead_list = &matroska->seekhead;
4284     MatroskaSeekhead *seekhead = seekhead_list->elem;
4285     AVStream *const st = s->streams[0];
4286     FFStream *const sti = ffstream(st);
4287     AVBPrint bprint;
4288     char *buf;
4289     int64_t cues_start = -1, cues_end = -1, before_pos, bandwidth;
4290     int i;
4291     int ret;
4292 
4293     // determine cues start and end positions
4294     for (i = 0; i < seekhead_list->nb_elem; i++)
4295         if (seekhead[i].id == MATROSKA_ID_CUES)
4296             break;
4297 
4298     if (i >= seekhead_list->nb_elem) return -1;
4299 
4300     before_pos = avio_tell(matroska->ctx->pb);
4301     cues_start = seekhead[i].pos + matroska->segment_start;
4302     if (avio_seek(matroska->ctx->pb, cues_start, SEEK_SET) == cues_start) {
4303         // cues_end is computed as cues_start + cues_length + length of the
4304         // Cues element ID (i.e. 4) + EBML length of the Cues element.
4305         // cues_end is inclusive and the above sum is reduced by 1.
4306         uint64_t cues_length, cues_id;
4307         int bytes_read;
4308         bytes_read = ebml_read_num   (matroska, matroska->ctx->pb, 4, &cues_id, 1);
4309         if (bytes_read < 0 || cues_id != (MATROSKA_ID_CUES & 0xfffffff))
4310             return bytes_read < 0 ? bytes_read : AVERROR_INVALIDDATA;
4311         bytes_read = ebml_read_length(matroska, matroska->ctx->pb, &cues_length);
4312         if (bytes_read < 0)
4313             return bytes_read;
4314         cues_end = cues_start + 4 + bytes_read + cues_length - 1;
4315     }
4316     avio_seek(matroska->ctx->pb, before_pos, SEEK_SET);
4317     if (cues_start == -1 || cues_end == -1) return -1;
4318 
4319     // parse the cues
4320     matroska_parse_cues(matroska);
4321 
4322     if (!sti->nb_index_entries)
4323         return AVERROR_INVALIDDATA;
4324 
4325     // cues start
4326     av_dict_set_int(&s->streams[0]->metadata, CUES_START, cues_start, 0);
4327 
4328     // cues end
4329     av_dict_set_int(&s->streams[0]->metadata, CUES_END, cues_end, 0);
4330 
4331     // if the file has cues at the start, fix up the init range so that
4332     // it does not include it
4333     if (cues_start <= init_range)
4334         av_dict_set_int(&s->streams[0]->metadata, INITIALIZATION_RANGE, cues_start - 1, 0);
4335 
4336     // bandwidth
4337     bandwidth = webm_dash_manifest_compute_bandwidth(s, cues_start);
4338     if (bandwidth < 0) return -1;
4339     av_dict_set_int(&s->streams[0]->metadata, BANDWIDTH, bandwidth, 0);
4340 
4341     // check if all clusters start with key frames
4342     av_dict_set_int(&s->streams[0]->metadata, CLUSTER_KEYFRAME, webm_clusters_start_with_keyframe(s), 0);
4343 
4344     // Store cue point timestamps as a comma separated list
4345     // for checking subsegment alignment in the muxer.
4346     av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
4347     for (int i = 0; i < sti->nb_index_entries; i++)
4348         av_bprintf(&bprint, "%" PRId64",", sti->index_entries[i].timestamp);
4349     if (!av_bprint_is_complete(&bprint)) {
4350         av_bprint_finalize(&bprint, NULL);
4351         return AVERROR(ENOMEM);
4352     }
4353     // Remove the trailing ','
4354     bprint.str[--bprint.len] = '\0';
4355     if ((ret = av_bprint_finalize(&bprint, &buf)) < 0)
4356         return ret;
4357     av_dict_set(&s->streams[0]->metadata, CUE_TIMESTAMPS,
4358                 buf, AV_DICT_DONT_STRDUP_VAL);
4359 
4360     return 0;
4361 }
4362 
webm_dash_manifest_read_header(AVFormatContext *s)4363 static int webm_dash_manifest_read_header(AVFormatContext *s)
4364 {
4365     char *buf;
4366     int ret = matroska_read_header(s);
4367     int64_t init_range;
4368     MatroskaTrack *tracks;
4369     MatroskaDemuxContext *matroska = s->priv_data;
4370     if (ret) {
4371         av_log(s, AV_LOG_ERROR, "Failed to read file headers\n");
4372         return -1;
4373     }
4374     if (!matroska->tracks.nb_elem || !s->nb_streams) {
4375         av_log(s, AV_LOG_ERROR, "No track found\n");
4376         return AVERROR_INVALIDDATA;
4377     }
4378 
4379     if (!matroska->is_live) {
4380         buf = av_asprintf("%g", matroska->duration);
4381         if (!buf)
4382             return AVERROR(ENOMEM);
4383         av_dict_set(&s->streams[0]->metadata, DURATION,
4384                     buf, AV_DICT_DONT_STRDUP_VAL);
4385 
4386         // initialization range
4387         // 5 is the offset of Cluster ID.
4388         init_range = avio_tell(s->pb) - 5;
4389         av_dict_set_int(&s->streams[0]->metadata, INITIALIZATION_RANGE, init_range, 0);
4390     }
4391 
4392     // basename of the file
4393     buf = strrchr(s->url, '/');
4394     av_dict_set(&s->streams[0]->metadata, FILENAME, buf ? ++buf : s->url, 0);
4395 
4396     // track number
4397     tracks = matroska->tracks.elem;
4398     av_dict_set_int(&s->streams[0]->metadata, TRACK_NUMBER, tracks[0].num, 0);
4399 
4400     // parse the cues and populate Cue related fields
4401     if (!matroska->is_live) {
4402         ret = webm_dash_manifest_cues(s, init_range);
4403         if (ret < 0) {
4404             av_log(s, AV_LOG_ERROR, "Error parsing Cues\n");
4405             return ret;
4406         }
4407     }
4408 
4409     // use the bandwidth from the command line if it was provided
4410     if (matroska->bandwidth > 0) {
4411         av_dict_set_int(&s->streams[0]->metadata, BANDWIDTH,
4412                         matroska->bandwidth, 0);
4413     }
4414     return 0;
4415 }
4416 
webm_dash_manifest_read_packet(AVFormatContext *s, AVPacket *pkt)4417 static int webm_dash_manifest_read_packet(AVFormatContext *s, AVPacket *pkt)
4418 {
4419     return AVERROR_EOF;
4420 }
4421 
4422 #define OFFSET(x) offsetof(MatroskaDemuxContext, x)
4423 static const AVOption options[] = {
4424     { "live", "flag indicating that the input is a live file that only has the headers.", OFFSET(is_live), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
4425     { "bandwidth", "bandwidth of this stream to be specified in the DASH manifest.", OFFSET(bandwidth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
4426     { NULL },
4427 };
4428 
4429 static const AVClass webm_dash_class = {
4430     .class_name = "WebM DASH Manifest demuxer",
4431     .item_name  = av_default_item_name,
4432     .option     = options,
4433     .version    = LIBAVUTIL_VERSION_INT,
4434 };
4435 
4436 const AVInputFormat ff_webm_dash_manifest_demuxer = {
4437     .name           = "webm_dash_manifest",
4438     .long_name      = NULL_IF_CONFIG_SMALL("WebM DASH Manifest"),
4439     .priv_class     = &webm_dash_class,
4440     .priv_data_size = sizeof(MatroskaDemuxContext),
4441     .flags_internal = FF_FMT_INIT_CLEANUP,
4442     .read_header    = webm_dash_manifest_read_header,
4443     .read_packet    = webm_dash_manifest_read_packet,
4444     .read_close     = matroska_read_close,
4445 };
4446 #endif
4447 
4448 const AVInputFormat ff_matroska_demuxer = {
4449     .name           = "matroska,webm",
4450     .long_name      = NULL_IF_CONFIG_SMALL("Matroska / WebM"),
4451     .extensions     = "mkv,mk3d,mka,mks,webm",
4452     .priv_data_size = sizeof(MatroskaDemuxContext),
4453     .flags_internal = FF_FMT_INIT_CLEANUP,
4454     .read_probe     = matroska_probe,
4455     .read_header    = matroska_read_header,
4456     .read_packet    = matroska_read_packet,
4457     .read_close     = matroska_read_close,
4458     .read_seek      = matroska_read_seek,
4459     .mime_type      = "audio/webm,audio/x-matroska,video/webm,video/x-matroska"
4460 };
4461