matroskadec.c 147 KB
Newer Older
1
/*
2
 * Matroska file demuxer
Diego Biurrun's avatar
Diego Biurrun committed
3
 * Copyright (c) 2003-2008 The FFmpeg Project
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24
 * Matroska file demuxer
25 26 27 28
 * @author Ronald Bultje <rbultje@ronald.bitfreak.net>
 * @author with a little help from Moritz Bunkus <moritz@bunkus.org>
 * @author totally reworked by Aurelien Jacobs <aurel@gnuage.org>
 * @see specs available on the Matroska project page: http://www.matroska.org/
29 30
 */

31 32
#include "config.h"

33
#include <inttypes.h>
34
#include <stdio.h>
35 36

#include "libavutil/avstring.h"
37
#include "libavutil/base64.h"
38 39 40 41
#include "libavutil/dict.h"
#include "libavutil/intfloat.h"
#include "libavutil/intreadwrite.h"
#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 51
#include "libavcodec/mpeg4audio.h"

52
#include "avformat.h"
53
#include "avio_internal.h"
54 55 56
#include "internal.h"
#include "isom.h"
#include "matroska.h"
57
#include "oggdec.h"
58
/* For ff_codec_get_id(). */
59
#include "riff.h"
60
#include "rmsipr.h"
61

62 63 64 65 66 67 68
#if CONFIG_BZLIB
#include <bzlib.h>
#endif
#if CONFIG_ZLIB
#include <zlib.h>
#endif

69 70
#include "qtpalette.h"

71 72 73 74 75 76 77 78
typedef enum {
    EBML_NONE,
    EBML_UINT,
    EBML_FLOAT,
    EBML_STR,
    EBML_UTF8,
    EBML_BIN,
    EBML_NEST,
wm4's avatar
wm4 committed
79
    EBML_LEVEL1,
80 81
    EBML_PASS,
    EBML_STOP,
82
    EBML_SINT,
83
    EBML_TYPE_COUNT
84 85 86 87 88 89 90 91
} EbmlType;

typedef const struct EbmlSyntax {
    uint32_t id;
    EbmlType type;
    int list_elem_size;
    int data_offset;
    union {
92
        int64_t     i;
93 94 95 96 97 98 99
        uint64_t    u;
        double      f;
        const char *s;
        const struct EbmlSyntax *n;
    } def;
} EbmlSyntax;

100
typedef struct EbmlList {
101 102 103 104
    int nb_elem;
    void *elem;
} EbmlList;

105
typedef struct EbmlBin {
106 107 108 109 110
    int      size;
    uint8_t *data;
    int64_t  pos;
} EbmlBin;

111
typedef struct Ebml {
112 113 114 115 116 117 118
    uint64_t version;
    uint64_t max_size;
    uint64_t id_length;
    char    *doctype;
    uint64_t doctype_version;
} Ebml;

119
typedef struct MatroskaTrackCompression {
120 121 122
    uint64_t algo;
    EbmlBin  settings;
} MatroskaTrackCompression;
123

124
typedef struct MatroskaTrackEncryption {
125 126 127 128
    uint64_t algo;
    EbmlBin  key_id;
} MatroskaTrackEncryption;

129
typedef struct MatroskaTrackEncoding {
130 131 132
    uint64_t scope;
    uint64_t type;
    MatroskaTrackCompression compression;
133
    MatroskaTrackEncryption encryption;
134
} MatroskaTrackEncoding;
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
typedef struct MatroskaMasteringMeta {
    double r_x;
    double r_y;
    double g_x;
    double g_y;
    double b_x;
    double b_y;
    double white_x;
    double white_y;
    double max_luminance;
    double min_luminance;
} MatroskaMasteringMeta;

typedef struct MatroskaTrackVideoColor {
    uint64_t matrix_coefficients;
    uint64_t bits_per_channel;
    uint64_t chroma_sub_horz;
    uint64_t chroma_sub_vert;
    uint64_t cb_sub_horz;
    uint64_t cb_sub_vert;
    uint64_t chroma_siting_horz;
    uint64_t chroma_siting_vert;
    uint64_t range;
    uint64_t transfer_characteristics;
    uint64_t primaries;
    uint64_t max_cll;
    uint64_t max_fall;
    MatroskaMasteringMeta mastering_meta;
} MatroskaTrackVideoColor;

166 167 168 169 170 171 172 173
typedef struct MatroskaTrackVideoProjection {
    uint64_t type;
    EbmlBin private;
    double yaw;
    double pitch;
    double roll;
} MatroskaTrackVideoProjection;

174
typedef struct MatroskaTrackVideo {
175 176 177 178 179
    double   frame_rate;
    uint64_t display_width;
    uint64_t display_height;
    uint64_t pixel_width;
    uint64_t pixel_height;
180
    EbmlBin color_space;
181
    uint64_t display_unit;
182 183
    uint64_t interlaced;
    uint64_t field_order;
184
    uint64_t stereo_mode;
185
    uint64_t alpha_mode;
186
    EbmlList color;
187
    MatroskaTrackVideoProjection projection;
188
} MatroskaTrackVideo;
189

190
typedef struct MatroskaTrackAudio {
191 192 193 194 195 196 197 198 199 200 201 202
    double   samplerate;
    double   out_samplerate;
    uint64_t bitdepth;
    uint64_t channels;

    /* real audio header (extracted from extradata) */
    int      coded_framesize;
    int      sub_packet_h;
    int      frame_size;
    int      sub_packet_size;
    int      sub_packet_cnt;
    int      pkt_cnt;
203
    uint64_t buf_timecode;
204 205
    uint8_t *buf;
} MatroskaTrackAudio;
206

207
typedef struct MatroskaTrackPlane {
208 209 210 211
    uint64_t uid;
    uint64_t type;
} MatroskaTrackPlane;

212
typedef struct MatroskaTrackOperation {
213 214 215
    EbmlList combine_planes;
} MatroskaTrackOperation;

216
typedef struct MatroskaTrack {
217
    uint64_t num;
218
    uint64_t uid;
219
    uint64_t type;
220
    char    *name;
221 222 223
    char    *codec_id;
    EbmlBin  codec_priv;
    char    *language;
224
    double time_scale;
225
    uint64_t default_duration;
226
    uint64_t flag_default;
227
    uint64_t flag_forced;
228
    uint64_t seek_preroll;
229 230
    MatroskaTrackVideo video;
    MatroskaTrackAudio audio;
231
    MatroskaTrackOperation operation;
232
    EbmlList encodings;
233
    uint64_t codec_delay;
234
    uint64_t codec_delay_in_track_tb;
235 236

    AVStream *stream;
237
    int64_t end_timecode;
238
    int ms_compat;
239
    uint64_t max_block_additional_id;
240 241 242

    uint32_t palette[AVPALETTE_COUNT];
    int has_palette;
243 244
} MatroskaTrack;

245
typedef struct MatroskaAttachment {
246
    uint64_t uid;
247 248 249
    char *filename;
    char *mime;
    EbmlBin bin;
250 251

    AVStream *stream;
252
} MatroskaAttachment;
253

254
typedef struct MatroskaChapter {
255 256 257 258
    uint64_t start;
    uint64_t end;
    uint64_t uid;
    char    *title;
259 260

    AVChapter *chapter;
261 262
} MatroskaChapter;

263
typedef struct MatroskaIndexPos {
264 265 266 267
    uint64_t track;
    uint64_t pos;
} MatroskaIndexPos;

268
typedef struct MatroskaIndex {
269 270 271 272
    uint64_t time;
    EbmlList pos;
} MatroskaIndex;

273
typedef struct MatroskaTag {
274 275
    char *name;
    char *string;
276 277
    char *lang;
    uint64_t def;
278 279 280
    EbmlList sub;
} MatroskaTag;

281
typedef struct MatroskaTagTarget {
282 283 284 285 286 287 288
    char    *type;
    uint64_t typevalue;
    uint64_t trackuid;
    uint64_t chapteruid;
    uint64_t attachuid;
} MatroskaTagTarget;

289
typedef struct MatroskaTags {
290 291 292 293
    MatroskaTagTarget target;
    EbmlList tag;
} MatroskaTags;

294
typedef struct MatroskaSeekhead {
295 296 297 298
    uint64_t id;
    uint64_t pos;
} MatroskaSeekhead;

299
typedef struct MatroskaLevel {
300 301
    uint64_t start;
    uint64_t length;
302 303
} MatroskaLevel;

304
typedef struct MatroskaCluster {
305 306 307 308
    uint64_t timecode;
    EbmlList blocks;
} MatroskaCluster;

309
typedef struct MatroskaLevel1Element {
wm4's avatar
wm4 committed
310 311 312 313 314
    uint64_t id;
    uint64_t pos;
    int parsed;
} MatroskaLevel1Element;

315
typedef struct MatroskaDemuxContext {
316
    const AVClass *class;
317 318
    AVFormatContext *ctx;

Diego Biurrun's avatar
Diego Biurrun committed
319
    /* EBML stuff */
320 321 322
    int num_levels;
    MatroskaLevel levels[EBML_MAX_DEPTH];
    int level_up;
323
    uint32_t current_id;
324

325 326 327
    uint64_t time_scale;
    double   duration;
    char    *title;
328
    char    *muxingapp;
329
    EbmlBin date_utc;
330
    EbmlList tracks;
331
    EbmlList attachments;
332
    EbmlList chapters;
333
    EbmlList index;
334
    EbmlList tags;
335
    EbmlList seekhead;
336 337

    /* byte position of the segment inside the stream */
338
    int64_t segment_start;
339

Diego Biurrun's avatar
Diego Biurrun committed
340
    /* the packet queue */
341 342
    AVPacket **packets;
    int num_packets;
343
    AVPacket *prev_pkt;
344

345
    int done;
346 347 348

    /* What to skip before effectively reading a packet. */
    int skip_to_keyframe;
349
    uint64_t skip_to_timecode;
350 351 352

    /* File has a CUES element, but we defer parsing until it is needed. */
    int cues_parsing_deferred;
353

wm4's avatar
wm4 committed
354 355 356 357
    /* Level1 elements and whether they were read yet */
    MatroskaLevel1Element level1_elems[64];
    int num_level1_elems;

358 359 360 361 362 363
    int current_cluster_num_blocks;
    int64_t current_cluster_pos;
    MatroskaCluster current_cluster;

    /* File has SSA subtitles which prevent incremental cluster parsing. */
    int contains_ssa;
364

365
    /* WebM DASH Manifest live flag */
366
    int is_live;
367 368 369

    /* Bandwidth value for WebM DASH Manifest */
    int bandwidth;
370 371
} MatroskaDemuxContext;

372
typedef struct MatroskaBlock {
373 374
    uint64_t duration;
    int64_t  reference;
375
    uint64_t non_simple;
376
    EbmlBin  bin;
377 378
    uint64_t additional_id;
    EbmlBin  additional;
379
    int64_t discard_padding;
380 381
} MatroskaBlock;

382
static const EbmlSyntax ebml_header[] = {
383 384 385 386 387 388 389
    { EBML_ID_EBMLREADVERSION,    EBML_UINT, 0, offsetof(Ebml, version),         { .u = EBML_VERSION } },
    { EBML_ID_EBMLMAXSIZELENGTH,  EBML_UINT, 0, offsetof(Ebml, max_size),        { .u = 8 } },
    { EBML_ID_EBMLMAXIDLENGTH,    EBML_UINT, 0, offsetof(Ebml, id_length),       { .u = 4 } },
    { EBML_ID_DOCTYPE,            EBML_STR,  0, offsetof(Ebml, doctype),         { .s = "(none)" } },
    { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml, doctype_version), { .u = 1 } },
    { EBML_ID_EBMLVERSION,        EBML_NONE },
    { EBML_ID_DOCTYPEVERSION,     EBML_NONE },
390 391 392
    { 0 }
};

393
static const EbmlSyntax ebml_syntax[] = {
394
    { EBML_ID_HEADER, EBML_NEST, 0, 0, { .n = ebml_header } },
395 396 397
    { 0 }
};

398
static const EbmlSyntax matroska_info[] = {
399 400 401 402
    { MATROSKA_ID_TIMECODESCALE, EBML_UINT,  0, offsetof(MatroskaDemuxContext, time_scale), { .u = 1000000 } },
    { MATROSKA_ID_DURATION,      EBML_FLOAT, 0, offsetof(MatroskaDemuxContext, duration) },
    { MATROSKA_ID_TITLE,         EBML_UTF8,  0, offsetof(MatroskaDemuxContext, title) },
    { MATROSKA_ID_WRITINGAPP,    EBML_NONE },
403 404
    { MATROSKA_ID_MUXINGAPP,     EBML_UTF8, 0, offsetof(MatroskaDemuxContext, muxingapp) },
    { MATROSKA_ID_DATEUTC,       EBML_BIN,  0, offsetof(MatroskaDemuxContext, date_utc) },
405
    { MATROSKA_ID_SEGMENTUID,    EBML_NONE },
406 407 408
    { 0 }
};

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
static const EbmlSyntax matroska_mastering_meta[] = {
    { MATROSKA_ID_VIDEOCOLOR_RX, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, r_x), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_RY, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, r_y), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_GX, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, g_x), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_GY, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, g_y), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_BX, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, b_x), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_BY, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, b_y), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_WHITEX, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, white_x), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_WHITEY, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, white_y), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, min_luminance), { .f=-1 } },
    { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX, EBML_FLOAT, 0, offsetof(MatroskaMasteringMeta, max_luminance), { .f=-1 } },
    { 0 }
};

static const EbmlSyntax matroska_track_video_color[] = {
424
    { MATROSKA_ID_VIDEOCOLORMATRIXCOEFF,      EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, matrix_coefficients), { .u = AVCOL_SPC_UNSPECIFIED } },
425
    { MATROSKA_ID_VIDEOCOLORBITSPERCHANNEL,   EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, bits_per_channel), { .u=0 } },
426 427 428 429
    { MATROSKA_ID_VIDEOCOLORCHROMASUBHORZ,    EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_horz), { .u=0 } },
    { MATROSKA_ID_VIDEOCOLORCHROMASUBVERT,    EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_vert), { .u=0 } },
    { MATROSKA_ID_VIDEOCOLORCBSUBHORZ,        EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, cb_sub_horz), { .u=0 } },
    { MATROSKA_ID_VIDEOCOLORCBSUBVERT,        EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, cb_sub_vert), { .u=0 } },
430 431 432 433 434
    { MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ, EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_horz), { .u = MATROSKA_COLOUR_CHROMASITINGHORZ_UNDETERMINED } },
    { MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT, EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_vert), { .u = MATROSKA_COLOUR_CHROMASITINGVERT_UNDETERMINED } },
    { MATROSKA_ID_VIDEOCOLORRANGE,            EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, range), { .u = AVCOL_RANGE_UNSPECIFIED } },
    { MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS, EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, transfer_characteristics), { .u = AVCOL_TRC_UNSPECIFIED } },
    { MATROSKA_ID_VIDEOCOLORPRIMARIES,        EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, primaries), { .u = AVCOL_PRI_UNSPECIFIED } },
435 436 437 438 439 440
    { MATROSKA_ID_VIDEOCOLORMAXCLL,           EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, max_cll), { .u=0 } },
    { MATROSKA_ID_VIDEOCOLORMAXFALL,          EBML_UINT, 0, offsetof(MatroskaTrackVideoColor, max_fall), { .u=0 } },
    { MATROSKA_ID_VIDEOCOLORMASTERINGMETA,    EBML_NEST, 0, offsetof(MatroskaTrackVideoColor, mastering_meta), { .n = matroska_mastering_meta } },
    { 0 }
};

441 442 443 444 445 446 447 448 449
static const EbmlSyntax matroska_track_video_projection[] = {
    { MATROSKA_ID_VIDEOPROJECTIONTYPE,        EBML_UINT,  0, offsetof(MatroskaTrackVideoProjection, type), { .u = MATROSKA_VIDEO_PROJECTION_TYPE_RECTANGULAR } },
    { MATROSKA_ID_VIDEOPROJECTIONPRIVATE,     EBML_BIN,   0, offsetof(MatroskaTrackVideoProjection, private) },
    { MATROSKA_ID_VIDEOPROJECTIONPOSEYAW,     EBML_FLOAT, 0, offsetof(MatroskaTrackVideoProjection, yaw), { .f=0.0 } },
    { MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH,   EBML_FLOAT, 0, offsetof(MatroskaTrackVideoProjection, pitch), { .f=0.0 } },
    { MATROSKA_ID_VIDEOPROJECTIONPOSEROLL,    EBML_FLOAT, 0, offsetof(MatroskaTrackVideoProjection, roll), { .f=0.0 } },
    { 0 }
};

450
static const EbmlSyntax matroska_track_video[] = {
451
    { MATROSKA_ID_VIDEOFRAMERATE,      EBML_FLOAT, 0, offsetof(MatroskaTrackVideo, frame_rate) },
452 453
    { MATROSKA_ID_VIDEODISPLAYWIDTH,   EBML_UINT,  0, offsetof(MatroskaTrackVideo, display_width), { .u=-1 } },
    { MATROSKA_ID_VIDEODISPLAYHEIGHT,  EBML_UINT,  0, offsetof(MatroskaTrackVideo, display_height), { .u=-1 } },
454 455
    { MATROSKA_ID_VIDEOPIXELWIDTH,     EBML_UINT,  0, offsetof(MatroskaTrackVideo, pixel_width) },
    { MATROSKA_ID_VIDEOPIXELHEIGHT,    EBML_UINT,  0, offsetof(MatroskaTrackVideo, pixel_height) },
456 457
    { MATROSKA_ID_VIDEOCOLORSPACE,     EBML_BIN,   0, offsetof(MatroskaTrackVideo, color_space) },
    { MATROSKA_ID_VIDEOALPHAMODE,      EBML_UINT,  0, offsetof(MatroskaTrackVideo, alpha_mode) },
458
    { MATROSKA_ID_VIDEOCOLOR,          EBML_NEST,  sizeof(MatroskaTrackVideoColor), offsetof(MatroskaTrackVideo, color), { .n = matroska_track_video_color } },
459
    { MATROSKA_ID_VIDEOPROJECTION,     EBML_NEST,  0, offsetof(MatroskaTrackVideo, projection), { .n = matroska_track_video_projection } },
460 461 462 463
    { MATROSKA_ID_VIDEOPIXELCROPB,     EBML_NONE },
    { MATROSKA_ID_VIDEOPIXELCROPT,     EBML_NONE },
    { MATROSKA_ID_VIDEOPIXELCROPL,     EBML_NONE },
    { MATROSKA_ID_VIDEOPIXELCROPR,     EBML_NONE },
464
    { MATROSKA_ID_VIDEODISPLAYUNIT,    EBML_UINT,  0, offsetof(MatroskaTrackVideo, display_unit), { .u= MATROSKA_VIDEO_DISPLAYUNIT_PIXELS } },
465 466
    { MATROSKA_ID_VIDEOFLAGINTERLACED, EBML_UINT,  0, offsetof(MatroskaTrackVideo, interlaced),  { .u = MATROSKA_VIDEO_INTERLACE_FLAG_UNDETERMINED } },
    { MATROSKA_ID_VIDEOFIELDORDER,     EBML_UINT,  0, offsetof(MatroskaTrackVideo, field_order), { .u = MATROSKA_VIDEO_FIELDORDER_UNDETERMINED } },
467
    { MATROSKA_ID_VIDEOSTEREOMODE,     EBML_UINT,  0, offsetof(MatroskaTrackVideo, stereo_mode), { .u = MATROSKA_VIDEO_STEREOMODE_TYPE_NB } },
468
    { MATROSKA_ID_VIDEOASPECTRATIO,    EBML_NONE },
469 470 471
    { 0 }
};

472
static const EbmlSyntax matroska_track_audio[] = {
473 474 475 476
    { MATROSKA_ID_AUDIOSAMPLINGFREQ,    EBML_FLOAT, 0, offsetof(MatroskaTrackAudio, samplerate), { .f = 8000.0 } },
    { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, EBML_FLOAT, 0, offsetof(MatroskaTrackAudio, out_samplerate) },
    { MATROSKA_ID_AUDIOBITDEPTH,        EBML_UINT,  0, offsetof(MatroskaTrackAudio, bitdepth) },
    { MATROSKA_ID_AUDIOCHANNELS,        EBML_UINT,  0, offsetof(MatroskaTrackAudio, channels),   { .u = 1 } },
477 478 479
    { 0 }
};

480
static const EbmlSyntax matroska_track_encoding_compression[] = {
481 482
    { MATROSKA_ID_ENCODINGCOMPALGO,     EBML_UINT, 0, offsetof(MatroskaTrackCompression, algo), { .u = 0 } },
    { MATROSKA_ID_ENCODINGCOMPSETTINGS, EBML_BIN,  0, offsetof(MatroskaTrackCompression, settings) },
483 484 485
    { 0 }
};

486
static const EbmlSyntax matroska_track_encoding_encryption[] = {
487
    { MATROSKA_ID_ENCODINGENCALGO,        EBML_UINT, 0, offsetof(MatroskaTrackEncryption,algo), {.u = 0} },
488 489 490 491 492 493 494 495
    { MATROSKA_ID_ENCODINGENCKEYID,       EBML_BIN, 0, offsetof(MatroskaTrackEncryption,key_id) },
    { MATROSKA_ID_ENCODINGENCAESSETTINGS, EBML_NONE },
    { MATROSKA_ID_ENCODINGSIGALGO,        EBML_NONE },
    { MATROSKA_ID_ENCODINGSIGHASHALGO,    EBML_NONE },
    { MATROSKA_ID_ENCODINGSIGKEYID,       EBML_NONE },
    { MATROSKA_ID_ENCODINGSIGNATURE,      EBML_NONE },
    { 0 }
};
496
static const EbmlSyntax matroska_track_encoding[] = {
497 498 499
    { MATROSKA_ID_ENCODINGSCOPE,       EBML_UINT, 0, offsetof(MatroskaTrackEncoding, scope),       { .u = 1 } },
    { MATROSKA_ID_ENCODINGTYPE,        EBML_UINT, 0, offsetof(MatroskaTrackEncoding, type),        { .u = 0 } },
    { MATROSKA_ID_ENCODINGCOMPRESSION, EBML_NEST, 0, offsetof(MatroskaTrackEncoding, compression), { .n = matroska_track_encoding_compression } },
500
    { MATROSKA_ID_ENCODINGENCRYPTION,  EBML_NEST, 0, offsetof(MatroskaTrackEncoding, encryption),  { .n = matroska_track_encoding_encryption } },
501
    { MATROSKA_ID_ENCODINGORDER,       EBML_NONE },
502 503 504
    { 0 }
};

505
static const EbmlSyntax matroska_track_encodings[] = {
506
    { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack, encodings), { .n = matroska_track_encoding } },
507 508 509
    { 0 }
};

510
static const EbmlSyntax matroska_track_plane[] = {
511 512 513 514 515
    { MATROSKA_ID_TRACKPLANEUID,  EBML_UINT, 0, offsetof(MatroskaTrackPlane,uid) },
    { MATROSKA_ID_TRACKPLANETYPE, EBML_UINT, 0, offsetof(MatroskaTrackPlane,type) },
    { 0 }
};

516
static const EbmlSyntax matroska_track_combine_planes[] = {
517
    { MATROSKA_ID_TRACKPLANE, EBML_NEST, sizeof(MatroskaTrackPlane), offsetof(MatroskaTrackOperation,combine_planes), {.n = matroska_track_plane} },
518 519 520
    { 0 }
};

521
static const EbmlSyntax matroska_track_operation[] = {
522
    { MATROSKA_ID_TRACKCOMBINEPLANES, EBML_NEST, 0, 0, {.n = matroska_track_combine_planes} },
523 524 525
    { 0 }
};

526
static const EbmlSyntax matroska_track[] = {
527 528 529 530 531 532
    { MATROSKA_ID_TRACKNUMBER,           EBML_UINT,  0, offsetof(MatroskaTrack, num) },
    { MATROSKA_ID_TRACKNAME,             EBML_UTF8,  0, offsetof(MatroskaTrack, name) },
    { MATROSKA_ID_TRACKUID,              EBML_UINT,  0, offsetof(MatroskaTrack, uid) },
    { MATROSKA_ID_TRACKTYPE,             EBML_UINT,  0, offsetof(MatroskaTrack, type) },
    { MATROSKA_ID_CODECID,               EBML_STR,   0, offsetof(MatroskaTrack, codec_id) },
    { MATROSKA_ID_CODECPRIVATE,          EBML_BIN,   0, offsetof(MatroskaTrack, codec_priv) },
533
    { MATROSKA_ID_CODECDELAY,            EBML_UINT,  0, offsetof(MatroskaTrack, codec_delay) },
534
    { MATROSKA_ID_TRACKLANGUAGE,         EBML_UTF8,  0, offsetof(MatroskaTrack, language),     { .s = "eng" } },
535
    { MATROSKA_ID_TRACKDEFAULTDURATION,  EBML_UINT,  0, offsetof(MatroskaTrack, default_duration) },
536 537 538
    { MATROSKA_ID_TRACKTIMECODESCALE,    EBML_FLOAT, 0, offsetof(MatroskaTrack, time_scale),   { .f = 1.0 } },
    { MATROSKA_ID_TRACKFLAGDEFAULT,      EBML_UINT,  0, offsetof(MatroskaTrack, flag_default), { .u = 1 } },
    { MATROSKA_ID_TRACKFLAGFORCED,       EBML_UINT,  0, offsetof(MatroskaTrack, flag_forced),  { .u = 0 } },
539 540
    { MATROSKA_ID_TRACKVIDEO,            EBML_NEST,  0, offsetof(MatroskaTrack, video),        { .n = matroska_track_video } },
    { MATROSKA_ID_TRACKAUDIO,            EBML_NEST,  0, offsetof(MatroskaTrack, audio),        { .n = matroska_track_audio } },
541
    { MATROSKA_ID_TRACKOPERATION,        EBML_NEST,  0, offsetof(MatroskaTrack, operation),    { .n = matroska_track_operation } },
542
    { MATROSKA_ID_TRACKCONTENTENCODINGS, EBML_NEST,  0, 0,                                     { .n = matroska_track_encodings } },
543 544
    { MATROSKA_ID_TRACKMAXBLKADDID,      EBML_UINT,  0, offsetof(MatroskaTrack, max_block_additional_id) },
    { MATROSKA_ID_SEEKPREROLL,           EBML_UINT,  0, offsetof(MatroskaTrack, seek_preroll) },
545 546 547 548 549 550 551 552
    { MATROSKA_ID_TRACKFLAGENABLED,      EBML_NONE },
    { MATROSKA_ID_TRACKFLAGLACING,       EBML_NONE },
    { MATROSKA_ID_CODECNAME,             EBML_NONE },
    { MATROSKA_ID_CODECDECODEALL,        EBML_NONE },
    { MATROSKA_ID_CODECINFOURL,          EBML_NONE },
    { MATROSKA_ID_CODECDOWNLOADURL,      EBML_NONE },
    { MATROSKA_ID_TRACKMINCACHE,         EBML_NONE },
    { MATROSKA_ID_TRACKMAXCACHE,         EBML_NONE },
553 554 555
    { 0 }
};

556
static const EbmlSyntax matroska_tracks[] = {
557
    { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext, tracks), { .n = matroska_track } },
558 559 560
    { 0 }
};

561
static const EbmlSyntax matroska_attachment[] = {
562 563 564 565
    { MATROSKA_ID_FILEUID,      EBML_UINT, 0, offsetof(MatroskaAttachment, uid) },
    { MATROSKA_ID_FILENAME,     EBML_UTF8, 0, offsetof(MatroskaAttachment, filename) },
    { MATROSKA_ID_FILEMIMETYPE, EBML_STR,  0, offsetof(MatroskaAttachment, mime) },
    { MATROSKA_ID_FILEDATA,     EBML_BIN,  0, offsetof(MatroskaAttachment, bin) },
566
    { MATROSKA_ID_FILEDESC,     EBML_NONE },
567 568 569
    { 0 }
};

570
static const EbmlSyntax matroska_attachments[] = {
571
    { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachment), offsetof(MatroskaDemuxContext, attachments), { .n = matroska_attachment } },
572 573 574
    { 0 }
};

575
static const EbmlSyntax matroska_chapter_display[] = {
576 577 578
    { MATROSKA_ID_CHAPSTRING,  EBML_UTF8, 0, offsetof(MatroskaChapter, title) },
    { MATROSKA_ID_CHAPLANG,    EBML_NONE },
    { MATROSKA_ID_CHAPCOUNTRY, EBML_NONE },
579 580 581
    { 0 }
};

582
static const EbmlSyntax matroska_chapter_entry[] = {
583 584 585 586
    { MATROSKA_ID_CHAPTERTIMESTART,   EBML_UINT, 0, offsetof(MatroskaChapter, start), { .u = AV_NOPTS_VALUE } },
    { MATROSKA_ID_CHAPTERTIMEEND,     EBML_UINT, 0, offsetof(MatroskaChapter, end),   { .u = AV_NOPTS_VALUE } },
    { MATROSKA_ID_CHAPTERUID,         EBML_UINT, 0, offsetof(MatroskaChapter, uid) },
    { MATROSKA_ID_CHAPTERDISPLAY,     EBML_NEST, 0,                        0,         { .n = matroska_chapter_display } },
587
    { MATROSKA_ID_CHAPTERFLAGHIDDEN,  EBML_NONE },
588 589 590
    { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE },
    { MATROSKA_ID_CHAPTERPHYSEQUIV,   EBML_NONE },
    { MATROSKA_ID_CHAPTERATOM,        EBML_NONE },
591 592 593
    { 0 }
};

594
static const EbmlSyntax matroska_chapter[] = {
595
    { MATROSKA_ID_CHAPTERATOM,        EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext, chapters), { .n = matroska_chapter_entry } },
596 597 598
    { MATROSKA_ID_EDITIONUID,         EBML_NONE },
    { MATROSKA_ID_EDITIONFLAGHIDDEN,  EBML_NONE },
    { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE },
599
    { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE },
600 601 602
    { 0 }
};

603
static const EbmlSyntax matroska_chapters[] = {
604
    { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, { .n = matroska_chapter } },
605 606 607
    { 0 }
};

608
static const EbmlSyntax matroska_index_pos[] = {
609 610
    { MATROSKA_ID_CUETRACK,           EBML_UINT, 0, offsetof(MatroskaIndexPos, track) },
    { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos, pos) },
611
    { MATROSKA_ID_CUERELATIVEPOSITION,EBML_NONE },
612
    { MATROSKA_ID_CUEDURATION,        EBML_NONE },
613
    { MATROSKA_ID_CUEBLOCKNUMBER,     EBML_NONE },
614 615 616
    { 0 }
};

617
static const EbmlSyntax matroska_index_entry[] = {
618 619
    { MATROSKA_ID_CUETIME,          EBML_UINT, 0,                        offsetof(MatroskaIndex, time) },
    { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex, pos), { .n = matroska_index_pos } },
620 621 622
    { 0 }
};

623
static const EbmlSyntax matroska_index[] = {
624
    { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext, index), { .n = matroska_index_entry } },
625 626 627
    { 0 }
};

628
static const EbmlSyntax matroska_simpletag[] = {
629 630 631 632 633 634
    { MATROSKA_ID_TAGNAME,        EBML_UTF8, 0,                   offsetof(MatroskaTag, name) },
    { MATROSKA_ID_TAGSTRING,      EBML_UTF8, 0,                   offsetof(MatroskaTag, string) },
    { MATROSKA_ID_TAGLANG,        EBML_STR,  0,                   offsetof(MatroskaTag, lang), { .s = "und" } },
    { MATROSKA_ID_TAGDEFAULT,     EBML_UINT, 0,                   offsetof(MatroskaTag, def) },
    { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0,                   offsetof(MatroskaTag, def) },
    { MATROSKA_ID_SIMPLETAG,      EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag, sub),  { .n = matroska_simpletag } },
635 636 637
    { 0 }
};

638
static const EbmlSyntax matroska_tagtargets[] = {
639 640 641 642 643
    { MATROSKA_ID_TAGTARGETS_TYPE,       EBML_STR,  0, offsetof(MatroskaTagTarget, type) },
    { MATROSKA_ID_TAGTARGETS_TYPEVALUE,  EBML_UINT, 0, offsetof(MatroskaTagTarget, typevalue), { .u = 50 } },
    { MATROSKA_ID_TAGTARGETS_TRACKUID,   EBML_UINT, 0, offsetof(MatroskaTagTarget, trackuid) },
    { MATROSKA_ID_TAGTARGETS_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaTagTarget, chapteruid) },
    { MATROSKA_ID_TAGTARGETS_ATTACHUID,  EBML_UINT, 0, offsetof(MatroskaTagTarget, attachuid) },
644 645 646
    { 0 }
};

647
static const EbmlSyntax matroska_tag[] = {
648 649
    { MATROSKA_ID_SIMPLETAG,  EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTags, tag),    { .n = matroska_simpletag } },
    { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0,                   offsetof(MatroskaTags, target), { .n = matroska_tagtargets } },
650 651 652
    { 0 }
};

653
static const EbmlSyntax matroska_tags[] = {
654
    { MATROSKA_ID_TAG, EBML_NEST, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext, tags), { .n = matroska_tag } },
655 656 657
    { 0 }
};

658
static const EbmlSyntax matroska_seekhead_entry[] = {
659 660
    { MATROSKA_ID_SEEKID,       EBML_UINT, 0, offsetof(MatroskaSeekhead, id) },
    { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead, pos), { .u = -1 } },
661 662 663
    { 0 }
};

664
static const EbmlSyntax matroska_seekhead[] = {
665
    { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext, seekhead), { .n = matroska_seekhead_entry } },
666 667 668
    { 0 }
};

669
static const EbmlSyntax matroska_segment[] = {
wm4's avatar
wm4 committed
670 671 672 673 674 675 676
    { MATROSKA_ID_INFO,        EBML_LEVEL1, 0, 0, { .n = matroska_info } },
    { MATROSKA_ID_TRACKS,      EBML_LEVEL1, 0, 0, { .n = matroska_tracks } },
    { MATROSKA_ID_ATTACHMENTS, EBML_LEVEL1, 0, 0, { .n = matroska_attachments } },
    { MATROSKA_ID_CHAPTERS,    EBML_LEVEL1, 0, 0, { .n = matroska_chapters } },
    { MATROSKA_ID_CUES,        EBML_LEVEL1, 0, 0, { .n = matroska_index } },
    { MATROSKA_ID_TAGS,        EBML_LEVEL1, 0, 0, { .n = matroska_tags } },
    { MATROSKA_ID_SEEKHEAD,    EBML_LEVEL1, 0, 0, { .n = matroska_seekhead } },
677
    { MATROSKA_ID_CLUSTER,     EBML_STOP },
678 679 680
    { 0 }
};

681
static const EbmlSyntax matroska_segments[] = {
682
    { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, { .n = matroska_segment } },
683 684 685
    { 0 }
};

686
static const EbmlSyntax matroska_blockmore[] = {
687 688 689 690 691
    { MATROSKA_ID_BLOCKADDID,      EBML_UINT, 0, offsetof(MatroskaBlock,additional_id) },
    { MATROSKA_ID_BLOCKADDITIONAL, EBML_BIN,  0, offsetof(MatroskaBlock,additional) },
    { 0 }
};

692
static const EbmlSyntax matroska_blockadditions[] = {
693
    { MATROSKA_ID_BLOCKMORE, EBML_NEST, 0, 0, {.n = matroska_blockmore} },
694 695 696
    { 0 }
};

697
static const EbmlSyntax matroska_blockgroup[] = {
698
    { MATROSKA_ID_BLOCK,          EBML_BIN,  0, offsetof(MatroskaBlock, bin) },
699
    { MATROSKA_ID_BLOCKADDITIONS, EBML_NEST, 0, 0, { .n = matroska_blockadditions} },
700
    { MATROSKA_ID_SIMPLEBLOCK,    EBML_BIN,  0, offsetof(MatroskaBlock, bin) },
701 702
    { MATROSKA_ID_BLOCKDURATION,  EBML_UINT, 0, offsetof(MatroskaBlock, duration) },
    { MATROSKA_ID_DISCARDPADDING, EBML_SINT, 0, offsetof(MatroskaBlock, discard_padding) },
703
    { MATROSKA_ID_BLOCKREFERENCE, EBML_SINT, 0, offsetof(MatroskaBlock, reference), { .i = INT64_MIN } },
704
    { MATROSKA_ID_CODECSTATE,     EBML_NONE },
705
    {                          1, EBML_UINT, 0, offsetof(MatroskaBlock, non_simple), { .u = 1 } },
706 707 708
    { 0 }
};

709
static const EbmlSyntax matroska_cluster[] = {
710 711 712 713 714
    { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0,                     offsetof(MatroskaCluster, timecode) },
    { MATROSKA_ID_BLOCKGROUP,      EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
    { MATROSKA_ID_SIMPLEBLOCK,     EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
    { MATROSKA_ID_CLUSTERPOSITION, EBML_NONE },
    { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
715 716 717
    { 0 }
};

718
static const EbmlSyntax matroska_clusters[] = {
719 720 721 722 723
    { MATROSKA_ID_CLUSTER,  EBML_NEST, 0, 0, { .n = matroska_cluster } },
    { MATROSKA_ID_INFO,     EBML_NONE },
    { MATROSKA_ID_CUES,     EBML_NONE },
    { MATROSKA_ID_TAGS,     EBML_NONE },
    { MATROSKA_ID_SEEKHEAD, EBML_NONE },
724 725 726
    { 0 }
};

727
static const EbmlSyntax matroska_cluster_incremental_parsing[] = {
728 729 730 731 732 733 734 735 736 737
    { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0,                     offsetof(MatroskaCluster, timecode) },
    { MATROSKA_ID_BLOCKGROUP,      EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
    { MATROSKA_ID_SIMPLEBLOCK,     EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster, blocks), { .n = matroska_blockgroup } },
    { MATROSKA_ID_CLUSTERPOSITION, EBML_NONE },
    { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
    { MATROSKA_ID_INFO,            EBML_NONE },
    { MATROSKA_ID_CUES,            EBML_NONE },
    { MATROSKA_ID_TAGS,            EBML_NONE },
    { MATROSKA_ID_SEEKHEAD,        EBML_NONE },
    { MATROSKA_ID_CLUSTER,         EBML_STOP },
738 739 740
    { 0 }
};

741
static const EbmlSyntax matroska_cluster_incremental[] = {
742 743 744 745 746
    { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, offsetof(MatroskaCluster, timecode) },
    { MATROSKA_ID_BLOCKGROUP,      EBML_STOP },
    { MATROSKA_ID_SIMPLEBLOCK,     EBML_STOP },
    { MATROSKA_ID_CLUSTERPOSITION, EBML_NONE },
    { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE },
747 748 749
    { 0 }
};

750
static const EbmlSyntax matroska_clusters_incremental[] = {
751 752 753 754 755
    { MATROSKA_ID_CLUSTER,  EBML_NEST, 0, 0, { .n = matroska_cluster_incremental } },
    { MATROSKA_ID_INFO,     EBML_NONE },
    { MATROSKA_ID_CUES,     EBML_NONE },
    { MATROSKA_ID_TAGS,     EBML_NONE },
    { MATROSKA_ID_SEEKHEAD, EBML_NONE },
756 757 758
    { 0 }
};

759
static const char *const matroska_doctypes[] = { "matroska", "webm" };
760

761 762
static int matroska_read_close(AVFormatContext *s);

763 764 765
static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos)
{
    AVIOContext *pb = matroska->ctx->pb;
766
    int64_t ret;
767 768 769 770
    uint32_t id;
    matroska->current_id = 0;
    matroska->num_levels = 0;

771
    /* seek to next position to resync from */
772 773 774 775
    if ((ret = avio_seek(pb, last_pos + 1, SEEK_SET)) < 0) {
        matroska->done = 1;
        return ret;
    }
776 777 778 779

    id = avio_rb32(pb);

    // try to find a toplevel element
780
    while (!avio_feof(pb)) {
781 782
        if (id == MATROSKA_ID_INFO     || id == MATROSKA_ID_TRACKS      ||
            id == MATROSKA_ID_CUES     || id == MATROSKA_ID_TAGS        ||
783
            id == MATROSKA_ID_SEEKHEAD || id == MATROSKA_ID_ATTACHMENTS ||
784
            id == MATROSKA_ID_CLUSTER  || id == MATROSKA_ID_CHAPTERS) {
785 786
            matroska->current_id = id;
            return 0;
787 788 789
        }
        id = (id << 8) | avio_r8(pb);
    }
790

791 792 793 794
    matroska->done = 1;
    return AVERROR_EOF;
}

795
/*
Diego Biurrun's avatar
Diego Biurrun committed
796
 * Return: Whether we reached the end of a level in the hierarchy or not.
797
 */
798
static int ebml_level_end(MatroskaDemuxContext *matroska)
799
{
800
    AVIOContext *pb = matroska->ctx->pb;
801
    int64_t pos = avio_tell(pb);
802

803
    if (matroska->num_levels > 0) {
804
        MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
805
        if (pos - level->start >= level->length || matroska->current_id) {
806
            matroska->num_levels--;
807
            return 1;
808 809
        }
    }
810
    return (matroska->is_live && matroska->ctx->pb->eof_reached) ? 1 : 0;
811 812 813 814 815 816 817 818
}

/*
 * Read: an "EBML number", which is defined as a variable-length
 * array of bytes. The first byte indicates the length by giving a
 * number of 0-bits followed by a one. The position of the first
 * "one" bit inside the first byte indicates the length of this
 * number.
Diego Biurrun's avatar
Diego Biurrun committed
819
 * Returns: number of bytes read, < 0 on error
820
 */
821
static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb,
822
                         int max_size, uint64_t *number)
823
{
824 825
    int read = 1, n = 1;
    uint64_t total = 0;
826

827
    /* The first byte tells us the length in bytes - avio_r8() can normally
828 829
     * return 0, but since that's not a valid first ebmlID byte, we can
     * use it safely here to catch EOS. */
830
    if (!(total = avio_r8(pb))) {
831
        /* we might encounter EOS here */
832
        if (!avio_feof(pb)) {
833
            int64_t pos = avio_tell(pb);
834 835 836
            av_log(matroska->ctx, AV_LOG_ERROR,
                   "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
                   pos, pos);
837
            return pb->error ? pb->error : AVERROR(EIO);
838
        }
839
        return AVERROR_EOF;
840 841 842
    }

    /* get the length of the EBML number */
843
    read = 8 - ff_log2_tab[total];
844
    if (read > max_size) {
845
        int64_t pos = avio_tell(pb) - 1;
846 847 848 849 850 851 852
        av_log(matroska->ctx, AV_LOG_ERROR,
               "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
               (uint8_t) total, pos, pos);
        return AVERROR_INVALIDDATA;
    }

    /* read out length */
853
    total ^= 1 << ff_log2_tab[total];
854
    while (n++ < read)
855
        total = (total << 8) | avio_r8(pb);
856 857 858 859 860 861

    *number = total;

    return read;
}

862 863 864 865 866
/**
 * Read a EBML length value.
 * This needs special handling for the "unknown length" case which has multiple
 * encodings.
 */
867
static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb,
868 869 870 871 872 873 874 875
                            uint64_t *number)
{
    int res = ebml_read_num(matroska, pb, 8, number);
    if (res > 0 && *number + 1 == 1ULL << (7 * res))
        *number = 0xffffffffffffffULL;
    return res;
}

876 877 878 879
/*
 * Read the next element as an unsigned int.
 * 0 is success, < 0 is failure.
 */
880
static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num)
881
{
882
    int n = 0;
883

884
    if (size > 8)
885 886
        return AVERROR_INVALIDDATA;

Diego Biurrun's avatar
Diego Biurrun committed
887
    /* big-endian ordering; build up number */
888 889
    *num = 0;
    while (n++ < size)
890
        *num = (*num << 8) | avio_r8(pb);
891 892 893 894

    return 0;
}

895 896 897 898 899 900 901 902 903 904 905 906 907 908
/*
 * Read the next element as a signed int.
 * 0 is success, < 0 is failure.
 */
static int ebml_read_sint(AVIOContext *pb, int size, int64_t *num)
{
    int n = 1;

    if (size > 8)
        return AVERROR_INVALIDDATA;

    if (size == 0) {
        *num = 0;
    } else {
909
        *num = sign_extend(avio_r8(pb), 8);
910 911 912

        /* big-endian ordering; build up number */
        while (n++ < size)
913
            *num = ((uint64_t)*num << 8) | avio_r8(pb);
914 915 916 917 918
    }

    return 0;
}

919 920 921 922
/*
 * Read the next element as a float.
 * 0 is success, < 0 is failure.
 */
923
static int ebml_read_float(AVIOContext *pb, int size, double *num)
924
{
925
    if (size == 0)
926
        *num = 0;
927
    else if (size == 4)
928
        *num = av_int2float(avio_rb32(pb));
929
    else if (size == 8)
930
        *num = av_int2double(avio_rb64(pb));
931
    else
932 933 934 935 936 937 938 939 940
        return AVERROR_INVALIDDATA;

    return 0;
}

/*
 * Read the next element as an ASCII string.
 * 0 is success, < 0 is failure.
 */
941
static int ebml_read_ascii(AVIOContext *pb, int size, char **str)
942
{
943 944
    char *res;

Diego Biurrun's avatar
Diego Biurrun committed
945
    /* EBML strings are usually not 0-terminated, so we allocate one
946
     * byte more, read the string and NULL-terminate it ourselves. */
947
    if (!(res = av_malloc(size + 1)))
948
        return AVERROR(ENOMEM);
949 950
    if (avio_read(pb, (uint8_t *) res, size) != size) {
        av_free(res);
951
        return AVERROR(EIO);
952
    }
953 954 955
    (res)[size] = '\0';
    av_free(*str);
    *str = res;
956 957 958 959

    return 0;
}

960 961 962 963
/*
 * Read the next element as binary data.
 * 0 is success, < 0 is failure.
 */
964
static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin)
965
{
966 967
    av_fast_padded_malloc(&bin->data, &bin->size, length);
    if (!bin->data)
968 969 970
        return AVERROR(ENOMEM);

    bin->size = length;
971
    bin->pos  = avio_tell(pb);
972
    if (avio_read(pb, bin->data, length) != length) {
973
        av_freep(&bin->data);
974
        bin->size = 0;
975
        return AVERROR(EIO);
976
    }