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