Statistics
| Branch: | Revision:

ffmpeg / libavformat / matroska.c @ 0ab07420

History | View | Annotate | Download (84.2 KB)

1
/*
2
 * Matroska file demuxer (no muxer yet)
3
 * Copyright (c) 2003-2004 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 matroska.c
24
 * Matroska file demuxer
25
 * by Ronald Bultje <rbultje@ronald.bitfreak.net>
26
 * with a little help from Moritz Bunkus <moritz@bunkus.org>
27
 * Specs available on the matroska project page:
28
 * http://www.matroska.org/.
29
 */
30

    
31
#include "avformat.h"
32
/* For codec_get_bmp_id and codec_get_wav_id. */
33
#include "riff.h"
34
#include "intfloat_readwrite.h"
35

    
36
/* EBML version supported */
37
#define EBML_VERSION 1
38

    
39
/* top-level master-IDs */
40
#define EBML_ID_HEADER             0x1A45DFA3
41

    
42
/* IDs in the HEADER master */
43
#define EBML_ID_EBMLVERSION        0x4286
44
#define EBML_ID_EBMLREADVERSION    0x42F7
45
#define EBML_ID_EBMLMAXIDLENGTH    0x42F2
46
#define EBML_ID_EBMLMAXSIZELENGTH  0x42F3
47
#define EBML_ID_DOCTYPE            0x4282
48
#define EBML_ID_DOCTYPEVERSION     0x4287
49
#define EBML_ID_DOCTYPEREADVERSION 0x4285
50

    
51
/* general EBML types */
52
#define EBML_ID_VOID               0xEC
53

    
54
/*
55
 * Matroska element IDs. max. 32-bit.
56
 */
57

    
58
/* toplevel segment */
59
#define MATROSKA_ID_SEGMENT    0x18538067
60

    
61
/* matroska top-level master IDs */
62
#define MATROSKA_ID_INFO       0x1549A966
63
#define MATROSKA_ID_TRACKS     0x1654AE6B
64
#define MATROSKA_ID_CUES       0x1C53BB6B
65
#define MATROSKA_ID_TAGS       0x1254C367
66
#define MATROSKA_ID_SEEKHEAD   0x114D9B74
67
#define MATROSKA_ID_CLUSTER    0x1F43B675
68

    
69
/* IDs in the info master */
70
#define MATROSKA_ID_TIMECODESCALE 0x2AD7B1
71
#define MATROSKA_ID_DURATION   0x4489
72
#define MATROSKA_ID_TITLE      0x7BA9
73
#define MATROSKA_ID_WRITINGAPP 0x5741
74
#define MATROSKA_ID_MUXINGAPP  0x4D80
75
#define MATROSKA_ID_DATEUTC    0x4461
76

    
77
/* ID in the tracks master */
78
#define MATROSKA_ID_TRACKENTRY 0xAE
79

    
80
/* IDs in the trackentry master */
81
#define MATROSKA_ID_TRACKNUMBER 0xD7
82
#define MATROSKA_ID_TRACKUID   0x73C5
83
#define MATROSKA_ID_TRACKTYPE  0x83
84
#define MATROSKA_ID_TRACKAUDIO 0xE1
85
#define MATROSKA_ID_TRACKVIDEO 0xE0
86
#define MATROSKA_ID_CODECID    0x86
87
#define MATROSKA_ID_CODECPRIVATE 0x63A2
88
#define MATROSKA_ID_CODECNAME  0x258688
89
#define MATROSKA_ID_CODECINFOURL 0x3B4040
90
#define MATROSKA_ID_CODECDOWNLOADURL 0x26B240
91
#define MATROSKA_ID_TRACKNAME  0x536E
92
#define MATROSKA_ID_TRACKLANGUAGE 0x22B59C
93
#define MATROSKA_ID_TRACKFLAGENABLED 0xB9
94
#define MATROSKA_ID_TRACKFLAGDEFAULT 0x88
95
#define MATROSKA_ID_TRACKFLAGLACING 0x9C
96
#define MATROSKA_ID_TRACKMINCACHE 0x6DE7
97
#define MATROSKA_ID_TRACKMAXCACHE 0x6DF8
98
#define MATROSKA_ID_TRACKDEFAULTDURATION 0x23E383
99

    
100
/* IDs in the trackvideo master */
101
#define MATROSKA_ID_VIDEOFRAMERATE 0x2383E3
102
#define MATROSKA_ID_VIDEODISPLAYWIDTH 0x54B0
103
#define MATROSKA_ID_VIDEODISPLAYHEIGHT 0x54BA
104
#define MATROSKA_ID_VIDEOPIXELWIDTH 0xB0
105
#define MATROSKA_ID_VIDEOPIXELHEIGHT 0xBA
106
#define MATROSKA_ID_VIDEOFLAGINTERLACED 0x9A
107
#define MATROSKA_ID_VIDEOSTEREOMODE 0x53B9
108
#define MATROSKA_ID_VIDEOASPECTRATIO 0x54B3
109
#define MATROSKA_ID_VIDEOCOLOURSPACE 0x2EB524
110

    
111
/* IDs in the trackaudio master */
112
#define MATROSKA_ID_AUDIOSAMPLINGFREQ 0xB5
113
#define MATROSKA_ID_AUDIOBITDEPTH 0x6264
114
#define MATROSKA_ID_AUDIOCHANNELS 0x9F
115

    
116
/* ID in the cues master */
117
#define MATROSKA_ID_POINTENTRY 0xBB
118

    
119
/* IDs in the pointentry master */
120
#define MATROSKA_ID_CUETIME    0xB3
121
#define MATROSKA_ID_CUETRACKPOSITION 0xB7
122

    
123
/* IDs in the cuetrackposition master */
124
#define MATROSKA_ID_CUETRACK   0xF7
125
#define MATROSKA_ID_CUECLUSTERPOSITION 0xF1
126

    
127
/* IDs in the tags master */
128
/* TODO */
129

    
130
/* IDs in the seekhead master */
131
#define MATROSKA_ID_SEEKENTRY  0x4DBB
132

    
133
/* IDs in the seekpoint master */
134
#define MATROSKA_ID_SEEKID     0x53AB
135
#define MATROSKA_ID_SEEKPOSITION 0x53AC
136

    
137
/* IDs in the cluster master */
138
#define MATROSKA_ID_CLUSTERTIMECODE 0xE7
139
#define MATROSKA_ID_BLOCKGROUP 0xA0
140

    
141
/* IDs in the blockgroup master */
142
#define MATROSKA_ID_BLOCK      0xA1
143
#define MATROSKA_ID_BLOCKDURATION 0x9B
144
#define MATROSKA_ID_BLOCKREFERENCE 0xFB
145

    
146
typedef enum {
147
  MATROSKA_TRACK_TYPE_VIDEO    = 0x1,
148
  MATROSKA_TRACK_TYPE_AUDIO    = 0x2,
149
  MATROSKA_TRACK_TYPE_COMPLEX  = 0x3,
150
  MATROSKA_TRACK_TYPE_LOGO     = 0x10,
151
  MATROSKA_TRACK_TYPE_SUBTITLE = 0x11,
152
  MATROSKA_TRACK_TYPE_CONTROL  = 0x20,
153
} MatroskaTrackType;
154

    
155
typedef enum {
156
  MATROSKA_EYE_MODE_MONO  = 0x0,
157
  MATROSKA_EYE_MODE_RIGHT = 0x1,
158
  MATROSKA_EYE_MODE_LEFT  = 0x2,
159
  MATROSKA_EYE_MODE_BOTH  = 0x3,
160
} MatroskaEyeMode;
161

    
162
typedef enum {
163
  MATROSKA_ASPECT_RATIO_MODE_FREE  = 0x0,
164
  MATROSKA_ASPECT_RATIO_MODE_KEEP  = 0x1,
165
  MATROSKA_ASPECT_RATIO_MODE_FIXED = 0x2,
166
} MatroskaAspectRatioMode;
167

    
168
/*
169
 * These aren't in any way "matroska-form" things,
170
 * it's just something I use in the muxer/demuxer.
171
 */
172

    
173
typedef enum {
174
  MATROSKA_TRACK_ENABLED = (1<<0),
175
  MATROSKA_TRACK_DEFAULT = (1<<1),
176
  MATROSKA_TRACK_LACING  = (1<<2),
177
  MATROSKA_TRACK_SHIFT   = (1<<16)
178
} MatroskaTrackFlags;
179

    
180
typedef enum {
181
  MATROSKA_VIDEOTRACK_INTERLACED = (MATROSKA_TRACK_SHIFT<<0)
182
} MatroskaVideoTrackFlags;
183

    
184
/*
185
 * Matroska Codec IDs. Strings.
186
 */
187

    
188
typedef struct CodecTags{
189
    const char *str;
190
    enum CodecID id;
191
}CodecTags;
192

    
193
#define MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC   "V_MS/VFW/FOURCC"
194
#define MATROSKA_CODEC_ID_AUDIO_ACM          "A_MS/ACM"
195

    
196
static CodecTags codec_tags[]={
197
//    {"V_MS/VFW/FOURCC"  , CODEC_ID_NONE},
198
    {"V_UNCOMPRESSED"   , CODEC_ID_RAWVIDEO},
199
    {"V_MPEG4/ISO/SP"   , CODEC_ID_MPEG4},
200
    {"V_MPEG4/ISO/ASP"  , CODEC_ID_MPEG4},
201
    {"V_MPEG4/ISO/AP"   , CODEC_ID_MPEG4},
202
    {"V_MPEG4/ISO/AVC"  , CODEC_ID_H264},
203
    {"V_MPEG4/MS/V3"    , CODEC_ID_MSMPEG4V3},
204
    {"V_MPEG1"          , CODEC_ID_MPEG1VIDEO},
205
    {"V_MPEG2"          , CODEC_ID_MPEG2VIDEO},
206
    {"V_MJPEG"          , CODEC_ID_MJPEG},
207
    {"V_REAL/RV10"      , CODEC_ID_RV10},
208
    {"V_REAL/RV20"      , CODEC_ID_RV20},
209
    {"V_REAL/RV30"      , CODEC_ID_RV30},
210
    {"V_REAL/RV40"      , CODEC_ID_RV40},
211
/* TODO: Real/Quicktime */
212

    
213
//    {"A_MS/ACM"         , CODEC_ID_NONE},
214
    {"A_MPEG/L1"        , CODEC_ID_MP3},
215
    {"A_MPEG/L2"        , CODEC_ID_MP3},
216
    {"A_MPEG/L3"        , CODEC_ID_MP3},
217
    {"A_PCM/INT/BIG"    , CODEC_ID_PCM_U16BE},
218
    {"A_PCM/INT/LIT"    , CODEC_ID_PCM_U16LE},
219
//    {"A_PCM/FLOAT/IEEE" , CODEC_ID_NONE},
220
    {"A_AC3"            , CODEC_ID_AC3},
221
    {"A_DTS"            , CODEC_ID_DTS},
222
    {"A_VORBIS"         , CODEC_ID_VORBIS},
223
    {"A_AAC/MPEG2/"     , CODEC_ID_AAC},
224
    {"A_AAC/MPEG4/"     , CODEC_ID_AAC},
225
    {"A_WAVPACK4"       , CODEC_ID_WAVPACK},
226
    {NULL               , CODEC_ID_NONE}
227
/* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
228
};
229

    
230
/* max. depth in the EBML tree structure */
231
#define EBML_MAX_DEPTH 16
232

    
233
typedef struct Track {
234
    MatroskaTrackType type;
235

    
236
    /* Unique track number and track ID. stream_index is the index that
237
     * the calling app uses for this track. */
238
    uint32_t num,
239
        uid,
240
        stream_index;
241

    
242
    char *name,
243
        *language;
244

    
245
    char *codec_id,
246
        *codec_name;
247

    
248
    unsigned char *codec_priv;
249
    int codec_priv_size;
250

    
251
    int64_t default_duration;
252
    MatroskaTrackFlags flags;
253
} MatroskaTrack;
254

    
255
typedef struct MatroskaVideoTrack {
256
    MatroskaTrack track;
257

    
258
    int pixel_width,
259
        pixel_height,
260
        display_width,
261
        display_height;
262

    
263
    uint32_t fourcc;
264

    
265
    MatroskaAspectRatioMode ar_mode;
266
    MatroskaEyeMode eye_mode;
267

    
268
    //..
269
} MatroskaVideoTrack;
270

    
271
typedef struct MatroskaAudioTrack {
272
    MatroskaTrack track;
273

    
274
    int channels,
275
        bitdepth,
276
        samplerate;
277
    //..
278
} MatroskaAudioTrack;
279

    
280
typedef struct MatroskaSubtitleTrack {
281
    MatroskaTrack track;
282

    
283
    //..
284
} MatroskaSubtitleTrack;
285

    
286
typedef struct MatroskaLevel {
287
    uint64_t start, length;
288
} MatroskaLevel;
289

    
290
typedef struct MatroskaDemuxIndex {
291
  uint64_t        pos;   /* of the corresponding *cluster*! */
292
  uint16_t        track; /* reference to 'num' */
293
  uint64_t        time;  /* in nanoseconds */
294
} MatroskaDemuxIndex;
295

    
296
typedef struct MatroskaDemuxContext {
297
    AVFormatContext *ctx;
298

    
299
    /* ebml stuff */
300
    int num_levels;
301
    MatroskaLevel levels[EBML_MAX_DEPTH];
302
    int level_up;
303

    
304
    /* matroska stuff */
305
    char *writing_app,
306
        *muxing_app;
307
    int64_t created;
308

    
309
    /* timescale in the file */
310
    int64_t time_scale;
311

    
312
    /* position (time, ns) */
313
    int64_t pos;
314

    
315
    /* num_streams is the number of streams that av_new_stream() was called
316
     * for ( = that are available to the calling program). */
317
    int num_tracks, num_streams;
318
    MatroskaTrack *tracks[MAX_STREAMS];
319

    
320
    /* cache for ID peeking */
321
    uint32_t peek_id;
322

    
323
    /* byte position of the segment inside the stream */
324
    offset_t segment_start;
325

    
326
    /* The packet queue. */
327
    AVPacket **packets;
328
    int num_packets;
329

    
330
    /* have we already parse metadata/cues/clusters? */
331
    int metadata_parsed,
332
        index_parsed,
333
        done;
334

    
335
    /* The index for seeking. */
336
    int num_indexes;
337
    MatroskaDemuxIndex *index;
338
} MatroskaDemuxContext;
339

    
340
/*
341
 * The first few functions handle EBML file parsing. The rest
342
 * is the document interpretation. Matroska really just is a
343
 * EBML file.
344
 */
345

    
346
/*
347
 * Return: the amount of levels in the hierarchy that the
348
 * current element lies higher than the previous one.
349
 * The opposite isn't done - that's auto-done using master
350
 * element reading.
351
 */
352

    
353
static int
354
ebml_read_element_level_up (MatroskaDemuxContext *matroska)
355
{
356
    ByteIOContext *pb = &matroska->ctx->pb;
357
    offset_t pos = url_ftell(pb);
358
    int num = 0;
359

    
360
    while (matroska->num_levels > 0) {
361
        MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
362

    
363
        if (pos >= level->start + level->length) {
364
            matroska->num_levels--;
365
            num++;
366
        } else {
367
            break;
368
        }
369
    }
370

    
371
    return num;
372
}
373

    
374
/*
375
 * Read: an "EBML number", which is defined as a variable-length
376
 * array of bytes. The first byte indicates the length by giving a
377
 * number of 0-bits followed by a one. The position of the first
378
 * "one" bit inside the first byte indicates the length of this
379
 * number.
380
 * Returns: num. of bytes read. < 0 on error.
381
 */
382

    
383
static int
384
ebml_read_num (MatroskaDemuxContext *matroska,
385
               int                   max_size,
386
               uint64_t             *number)
387
{
388
    ByteIOContext *pb = &matroska->ctx->pb;
389
    int len_mask = 0x80, read = 1, n = 1;
390
    int64_t total = 0;
391

    
392
    /* the first byte tells us the length in bytes - get_byte() can normally
393
     * return 0, but since that's not a valid first ebmlID byte, we can
394
     * use it safely here to catch EOS. */
395
    if (!(total = get_byte(pb))) {
396
        /* we might encounter EOS here */
397
        if (!url_feof(pb)) {
398
            offset_t pos = url_ftell(pb);
399
            av_log(matroska->ctx, AV_LOG_ERROR,
400
                   "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
401
                   pos, pos);
402
        }
403
        return AVERROR_IO; /* EOS or actual I/O error */
404
    }
405

    
406
    /* get the length of the EBML number */
407
    while (read <= max_size && !(total & len_mask)) {
408
        read++;
409
        len_mask >>= 1;
410
    }
411
    if (read > max_size) {
412
        offset_t pos = url_ftell(pb) - 1;
413
        av_log(matroska->ctx, AV_LOG_ERROR,
414
               "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
415
               (uint8_t) total, pos, pos);
416
        return AVERROR_INVALIDDATA;
417
    }
418

    
419
    /* read out length */
420
    total &= ~len_mask;
421
    while (n++ < read)
422
        total = (total << 8) | get_byte(pb);
423

    
424
    *number = total;
425

    
426
    return read;
427
}
428

    
429
/*
430
 * Read: the element content data ID.
431
 * Return: the number of bytes read or < 0 on error.
432
 */
433

    
434
static int
435
ebml_read_element_id (MatroskaDemuxContext *matroska,
436
                      uint32_t             *id,
437
                      int                  *level_up)
438
{
439
    int read;
440
    uint64_t total;
441

    
442
    /* if we re-call this, use our cached ID */
443
    if (matroska->peek_id != 0) {
444
        if (level_up)
445
            *level_up = 0;
446
        *id = matroska->peek_id;
447
        return 0;
448
    }
449

    
450
    /* read out the "EBML number", include tag in ID */
451
    if ((read = ebml_read_num(matroska, 4, &total)) < 0)
452
        return read;
453
    *id = matroska->peek_id  = total | (1 << (read * 7));
454

    
455
    /* level tracking */
456
    if (level_up)
457
        *level_up = ebml_read_element_level_up(matroska);
458

    
459
    return read;
460
}
461

    
462
/*
463
 * Read: element content length.
464
 * Return: the number of bytes read or < 0 on error.
465
 */
466

    
467
static int
468
ebml_read_element_length (MatroskaDemuxContext *matroska,
469
                          uint64_t             *length)
470
{
471
    /* clear cache since we're now beyond that data point */
472
    matroska->peek_id = 0;
473

    
474
    /* read out the "EBML number", include tag in ID */
475
    return ebml_read_num(matroska, 8, length);
476
}
477

    
478
/*
479
 * Return: the ID of the next element, or 0 on error.
480
 * Level_up contains the amount of levels that this
481
 * next element lies higher than the previous one.
482
 */
483

    
484
static uint32_t
485
ebml_peek_id (MatroskaDemuxContext *matroska,
486
              int                  *level_up)
487
{
488
    uint32_t id;
489

    
490
    assert(level_up != NULL);
491

    
492
    if (ebml_read_element_id(matroska, &id, level_up) < 0)
493
        return 0;
494

    
495
    return id;
496
}
497

    
498
/*
499
 * Seek to a given offset.
500
 * 0 is success, -1 is failure.
501
 */
502

    
503
static int
504
ebml_read_seek (MatroskaDemuxContext *matroska,
505
                offset_t              offset)
506
{
507
    ByteIOContext *pb = &matroska->ctx->pb;
508

    
509
    /* clear ID cache, if any */
510
    matroska->peek_id = 0;
511

    
512
    return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
513
}
514

    
515
/*
516
 * Skip the next element.
517
 * 0 is success, -1 is failure.
518
 */
519

    
520
static int
521
ebml_read_skip (MatroskaDemuxContext *matroska)
522
{
523
    ByteIOContext *pb = &matroska->ctx->pb;
524
    uint32_t id;
525
    uint64_t length;
526
    int res;
527

    
528
    if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
529
        (res = ebml_read_element_length(matroska, &length)) < 0)
530
        return res;
531

    
532
    url_fskip(pb, length);
533

    
534
    return 0;
535
}
536

    
537
/*
538
 * Read the next element as an unsigned int.
539
 * 0 is success, < 0 is failure.
540
 */
541

    
542
static int
543
ebml_read_uint (MatroskaDemuxContext *matroska,
544
                uint32_t             *id,
545
                uint64_t             *num)
546
{
547
    ByteIOContext *pb = &matroska->ctx->pb;
548
    int n = 0, size, res;
549
    uint64_t rlength;
550

    
551
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
552
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
553
        return res;
554
    size = rlength;
555
    if (size < 1 || size > 8) {
556
        offset_t pos = url_ftell(pb);
557
        av_log(matroska->ctx, AV_LOG_ERROR,
558
               "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
559
                size, pos, pos);
560
        return AVERROR_INVALIDDATA;
561
    }
562

    
563
    /* big-endian ordening; build up number */
564
    *num = 0;
565
    while (n++ < size)
566
        *num = (*num << 8) | get_byte(pb);
567

    
568
    return 0;
569
}
570

    
571
/*
572
 * Read the next element as a signed int.
573
 * 0 is success, < 0 is failure.
574
 */
575

    
576
static int
577
ebml_read_sint (MatroskaDemuxContext *matroska,
578
                uint32_t             *id,
579
                int64_t              *num)
580
{
581
    ByteIOContext *pb = &matroska->ctx->pb;
582
    int size, n = 1, negative = 0, res;
583
    uint64_t rlength;
584

    
585
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
586
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
587
        return res;
588
    size = rlength;
589
    if (size < 1 || size > 8) {
590
        offset_t pos = url_ftell(pb);
591
        av_log(matroska->ctx, AV_LOG_ERROR,
592
               "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
593
                size, pos, pos);
594
        return AVERROR_INVALIDDATA;
595
    }
596
    if ((*num = get_byte(pb)) & 0x80) {
597
        negative = 1;
598
        *num &= ~0x80;
599
    }
600
    *num = 0;
601
    while (n++ < size)
602
        *num = (*num << 8) | get_byte(pb);
603

    
604
    /* make signed */
605
    if (negative)
606
        *num = *num - (1LL << ((8 * size) - 1));
607

    
608
    return 0;
609
}
610

    
611
/*
612
 * Read the next element as a float.
613
 * 0 is success, < 0 is failure.
614
 */
615

    
616
static int
617
ebml_read_float (MatroskaDemuxContext *matroska,
618
                 uint32_t             *id,
619
                 double               *num)
620
{
621
    ByteIOContext *pb = &matroska->ctx->pb;
622
    int size, res;
623
    uint64_t rlength;
624

    
625
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
626
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
627
        return res;
628
    size = rlength;
629

    
630
    if (size == 4) {
631
        *num= av_int2flt(get_be32(pb));
632
    } else if(size==8){
633
        *num= av_int2dbl(get_be64(pb));
634
    } else{
635
        offset_t pos = url_ftell(pb);
636
        av_log(matroska->ctx, AV_LOG_ERROR,
637
               "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
638
               size, pos, pos);
639
        return AVERROR_INVALIDDATA;
640
    }
641

    
642
    return 0;
643
}
644

    
645
/*
646
 * Read the next element as an ASCII string.
647
 * 0 is success, < 0 is failure.
648
 */
649

    
650
static int
651
ebml_read_ascii (MatroskaDemuxContext *matroska,
652
                 uint32_t             *id,
653
                 char                **str)
654
{
655
    ByteIOContext *pb = &matroska->ctx->pb;
656
    int size, res;
657
    uint64_t rlength;
658

    
659
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
660
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
661
        return res;
662
    size = rlength;
663

    
664
    /* ebml strings are usually not 0-terminated, so we allocate one
665
     * byte more, read the string and NULL-terminate it ourselves. */
666
    if (size < 0 || !(*str = av_malloc(size + 1))) {
667
        av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
668
        return AVERROR_NOMEM;
669
    }
670
    if (get_buffer(pb, (uint8_t *) *str, size) != size) {
671
        offset_t pos = url_ftell(pb);
672
        av_log(matroska->ctx, AV_LOG_ERROR,
673
               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
674
        return AVERROR_IO;
675
    }
676
    (*str)[size] = '\0';
677

    
678
    return 0;
679
}
680

    
681
/*
682
 * Read the next element as a UTF-8 string.
683
 * 0 is success, < 0 is failure.
684
 */
685

    
686
static int
687
ebml_read_utf8 (MatroskaDemuxContext *matroska,
688
                uint32_t             *id,
689
                char                **str)
690
{
691
  return ebml_read_ascii(matroska, id, str);
692
}
693

    
694
/*
695
 * Read the next element as a date (nanoseconds since 1/1/2000).
696
 * 0 is success, < 0 is failure.
697
 */
698

    
699
static int
700
ebml_read_date (MatroskaDemuxContext *matroska,
701
                uint32_t             *id,
702
                int64_t              *date)
703
{
704
  return ebml_read_sint(matroska, id, date);
705
}
706

    
707
/*
708
 * Read the next element, but only the header. The contents
709
 * are supposed to be sub-elements which can be read separately.
710
 * 0 is success, < 0 is failure.
711
 */
712

    
713
static int
714
ebml_read_master (MatroskaDemuxContext *matroska,
715
                  uint32_t             *id)
716
{
717
    ByteIOContext *pb = &matroska->ctx->pb;
718
    uint64_t length;
719
    MatroskaLevel *level;
720
    int res;
721

    
722
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
723
        (res = ebml_read_element_length(matroska, &length)) < 0)
724
        return res;
725

    
726
    /* protect... (Heaven forbids that the '>' is true) */
727
    if (matroska->num_levels >= EBML_MAX_DEPTH) {
728
        av_log(matroska->ctx, AV_LOG_ERROR,
729
               "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
730
        return AVERROR_NOTSUPP;
731
    }
732

    
733
    /* remember level */
734
    level = &matroska->levels[matroska->num_levels++];
735
    level->start = url_ftell(pb);
736
    level->length = length;
737

    
738
    return 0;
739
}
740

    
741
/*
742
 * Read the next element as binary data.
743
 * 0 is success, < 0 is failure.
744
 */
745

    
746
static int
747
ebml_read_binary (MatroskaDemuxContext *matroska,
748
                  uint32_t             *id,
749
                  uint8_t             **binary,
750
                  int                  *size)
751
{
752
    ByteIOContext *pb = &matroska->ctx->pb;
753
    uint64_t rlength;
754
    int res;
755

    
756
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
757
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
758
        return res;
759
    *size = rlength;
760

    
761
    if (!(*binary = av_malloc(*size))) {
762
        av_log(matroska->ctx, AV_LOG_ERROR,
763
               "Memory allocation error\n");
764
        return AVERROR_NOMEM;
765
    }
766

    
767
    if (get_buffer(pb, *binary, *size) != *size) {
768
        offset_t pos = url_ftell(pb);
769
        av_log(matroska->ctx, AV_LOG_ERROR,
770
               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
771
        return AVERROR_IO;
772
    }
773

    
774
    return 0;
775
}
776

    
777
/*
778
 * Read signed/unsigned "EBML" numbers.
779
 * Return: number of bytes processed, < 0 on error.
780
 * XXX: use ebml_read_num().
781
 */
782

    
783
static int
784
matroska_ebmlnum_uint (uint8_t  *data,
785
                       uint32_t  size,
786
                       uint64_t *num)
787
{
788
    int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
789
    uint64_t total;
790

    
791
    if (size <= 0)
792
        return AVERROR_INVALIDDATA;
793

    
794
    total = data[0];
795
    while (read <= 8 && !(total & len_mask)) {
796
        read++;
797
        len_mask >>= 1;
798
    }
799
    if (read > 8)
800
        return AVERROR_INVALIDDATA;
801

    
802
    if ((total &= (len_mask - 1)) == len_mask - 1)
803
        num_ffs++;
804
    if (size < read)
805
        return AVERROR_INVALIDDATA;
806
    while (n < read) {
807
        if (data[n] == 0xff)
808
            num_ffs++;
809
        total = (total << 8) | data[n];
810
        n++;
811
    }
812

    
813
    if (read == num_ffs)
814
        *num = (uint64_t)-1;
815
    else
816
        *num = total;
817

    
818
    return read;
819
}
820

    
821
/*
822
 * Same as above, but signed.
823
 */
824

    
825
static int
826
matroska_ebmlnum_sint (uint8_t  *data,
827
                       uint32_t  size,
828
                       int64_t  *num)
829
{
830
    uint64_t unum;
831
    int res;
832

    
833
    /* read as unsigned number first */
834
    if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
835
        return res;
836

    
837
    /* make signed (weird way) */
838
    if (unum == (uint64_t)-1)
839
        *num = INT64_MAX;
840
    else
841
        *num = unum - ((1LL << ((7 * res) - 1)) - 1);
842

    
843
    return res;
844
}
845

    
846
/*
847
 * Read an EBML header.
848
 * 0 is success, < 0 is failure.
849
 */
850

    
851
static int
852
ebml_read_header (MatroskaDemuxContext *matroska,
853
                  char                **doctype,
854
                  int                  *version)
855
{
856
    uint32_t id;
857
    int level_up, res = 0;
858

    
859
    /* default init */
860
    if (doctype)
861
        *doctype = NULL;
862
    if (version)
863
        *version = 1;
864

    
865
    if (!(id = ebml_peek_id(matroska, &level_up)) ||
866
        level_up != 0 || id != EBML_ID_HEADER) {
867
        av_log(matroska->ctx, AV_LOG_ERROR,
868
               "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
869
        return AVERROR_INVALIDDATA;
870
    }
871
    if ((res = ebml_read_master(matroska, &id)) < 0)
872
        return res;
873

    
874
    while (res == 0) {
875
        if (!(id = ebml_peek_id(matroska, &level_up)))
876
            return AVERROR_IO;
877

    
878
        /* end-of-header */
879
        if (level_up)
880
            break;
881

    
882
        switch (id) {
883
            /* is our read version uptodate? */
884
            case EBML_ID_EBMLREADVERSION: {
885
                uint64_t num;
886

    
887
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
888
                    return res;
889
                if (num > EBML_VERSION) {
890
                    av_log(matroska->ctx, AV_LOG_ERROR,
891
                           "EBML version %"PRIu64" (> %d) is not supported\n",
892
                           num, EBML_VERSION);
893
                    return AVERROR_INVALIDDATA;
894
                }
895
                break;
896
            }
897

    
898
            /* we only handle 8 byte lengths at max */
899
            case EBML_ID_EBMLMAXSIZELENGTH: {
900
                uint64_t num;
901

    
902
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
903
                    return res;
904
                if (num > sizeof(uint64_t)) {
905
                    av_log(matroska->ctx, AV_LOG_ERROR,
906
                           "Integers of size %"PRIu64" (> %zd) not supported\n",
907
                           num, sizeof(uint64_t));
908
                    return AVERROR_INVALIDDATA;
909
                }
910
                break;
911
            }
912

    
913
            /* we handle 4 byte IDs at max */
914
            case EBML_ID_EBMLMAXIDLENGTH: {
915
                uint64_t num;
916

    
917
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
918
                    return res;
919
                if (num > sizeof(uint32_t)) {
920
                    av_log(matroska->ctx, AV_LOG_ERROR,
921
                           "IDs of size %"PRIu64" (> %zu) not supported\n",
922
                            num, sizeof(uint32_t));
923
                    return AVERROR_INVALIDDATA;
924
                }
925
                break;
926
            }
927

    
928
            case EBML_ID_DOCTYPE: {
929
                char *text;
930

    
931
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
932
                    return res;
933
                if (doctype) {
934
                    if (*doctype)
935
                        av_free(*doctype);
936
                    *doctype = text;
937
                } else
938
                    av_free(text);
939
                break;
940
            }
941

    
942
            case EBML_ID_DOCTYPEREADVERSION: {
943
                uint64_t num;
944

    
945
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
946
                    return res;
947
                if (version)
948
                    *version = num;
949
                break;
950
            }
951

    
952
            default:
953
                av_log(matroska->ctx, AV_LOG_INFO,
954
                       "Unknown data type 0x%x in EBML header", id);
955
                /* pass-through */
956

    
957
            case EBML_ID_VOID:
958
            /* we ignore these two, as they don't tell us anything we
959
             * care about */
960
            case EBML_ID_EBMLVERSION:
961
            case EBML_ID_DOCTYPEVERSION:
962
                res = ebml_read_skip (matroska);
963
                break;
964
        }
965
    }
966

    
967
    return 0;
968
}
969

    
970
/*
971
 * Put one packet in an application-supplied AVPacket struct.
972
 * Returns 0 on success or -1 on failure.
973
 */
974

    
975
static int
976
matroska_deliver_packet (MatroskaDemuxContext *matroska,
977
                         AVPacket             *pkt)
978
{
979
    if (matroska->num_packets > 0) {
980
        memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
981
        av_free(matroska->packets[0]);
982
        if (matroska->num_packets > 1) {
983
            memmove(&matroska->packets[0], &matroska->packets[1],
984
                    (matroska->num_packets - 1) * sizeof(AVPacket *));
985
            matroska->packets =
986
                av_realloc(matroska->packets, (matroska->num_packets - 1) *
987
                           sizeof(AVPacket *));
988
        } else {
989
            av_free(matroska->packets);
990
            matroska->packets = NULL;
991
        }
992
        matroska->num_packets--;
993
        return 0;
994
    }
995

    
996
    return -1;
997
}
998

    
999
/*
1000
 * Put a packet into our internal queue. Will be delivered to the
1001
 * user/application during the next get_packet() call.
1002
 */
1003

    
1004
static void
1005
matroska_queue_packet (MatroskaDemuxContext *matroska,
1006
                       AVPacket             *pkt)
1007
{
1008
    matroska->packets =
1009
        av_realloc(matroska->packets, (matroska->num_packets + 1) *
1010
                   sizeof(AVPacket *));
1011
    matroska->packets[matroska->num_packets] = pkt;
1012
    matroska->num_packets++;
1013
}
1014

    
1015
/*
1016
 * Autodetecting...
1017
 */
1018

    
1019
static int
1020
matroska_probe (AVProbeData *p)
1021
{
1022
    uint64_t total = 0;
1023
    int len_mask = 0x80, size = 1, n = 1;
1024
    uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
1025

    
1026
    if (p->buf_size < 5)
1027
        return 0;
1028

    
1029
    /* ebml header? */
1030
    if ((p->buf[0] << 24 | p->buf[1] << 16 |
1031
         p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
1032
        return 0;
1033

    
1034
    /* length of header */
1035
    total = p->buf[4];
1036
    while (size <= 8 && !(total & len_mask)) {
1037
        size++;
1038
        len_mask >>= 1;
1039
    }
1040
    if (size > 8)
1041
      return 0;
1042
    total &= (len_mask - 1);
1043
    while (n < size)
1044
        total = (total << 8) | p->buf[4 + n++];
1045

    
1046
    /* does the probe data contain the whole header? */
1047
    if (p->buf_size < 4 + size + total)
1048
      return 0;
1049

    
1050
    /* the header must contain the document type 'matroska'. For now,
1051
     * we don't parse the whole header but simply check for the
1052
     * availability of that array of characters inside the header.
1053
     * Not fully fool-proof, but good enough. */
1054
    for (n = 4 + size; n < 4 + size + total - sizeof(probe_data); n++)
1055
        if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
1056
            return AVPROBE_SCORE_MAX;
1057

    
1058
    return 0;
1059
}
1060

    
1061
/*
1062
 * From here on, it's all XML-style DTD stuff... Needs no comments.
1063
 */
1064

    
1065
static int
1066
matroska_parse_info (MatroskaDemuxContext *matroska)
1067
{
1068
    int res = 0;
1069
    uint32_t id;
1070

    
1071
    av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
1072

    
1073
    while (res == 0) {
1074
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1075
            res = AVERROR_IO;
1076
            break;
1077
        } else if (matroska->level_up) {
1078
            matroska->level_up--;
1079
            break;
1080
        }
1081

    
1082
        switch (id) {
1083
            /* cluster timecode */
1084
            case MATROSKA_ID_TIMECODESCALE: {
1085
                uint64_t num;
1086
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1087
                    break;
1088
                matroska->time_scale = num;
1089
                break;
1090
            }
1091

    
1092
            case MATROSKA_ID_DURATION: {
1093
                double num;
1094
                if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1095
                    break;
1096
                matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
1097
                break;
1098
            }
1099

    
1100
            case MATROSKA_ID_TITLE: {
1101
                char *text;
1102
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1103
                    break;
1104
                strncpy(matroska->ctx->title, text,
1105
                        sizeof(matroska->ctx->title)-1);
1106
                av_free(text);
1107
                break;
1108
            }
1109

    
1110
            case MATROSKA_ID_WRITINGAPP: {
1111
                char *text;
1112
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1113
                    break;
1114
                matroska->writing_app = text;
1115
                break;
1116
            }
1117

    
1118
            case MATROSKA_ID_MUXINGAPP: {
1119
                char *text;
1120
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1121
                    break;
1122
                matroska->muxing_app = text;
1123
                break;
1124
            }
1125

    
1126
            case MATROSKA_ID_DATEUTC: {
1127
                int64_t time;
1128
                if ((res = ebml_read_date(matroska, &id, &time)) < 0)
1129
                    break;
1130
                matroska->created = time;
1131
                break;
1132
            }
1133

    
1134
            default:
1135
                av_log(matroska->ctx, AV_LOG_INFO,
1136
                       "Unknown entry 0x%x in info header\n", id);
1137
                /* fall-through */
1138

    
1139
            case EBML_ID_VOID:
1140
                res = ebml_read_skip(matroska);
1141
                break;
1142
        }
1143

    
1144
        if (matroska->level_up) {
1145
            matroska->level_up--;
1146
            break;
1147
        }
1148
    }
1149

    
1150
    return res;
1151
}
1152

    
1153
static int
1154
matroska_add_stream (MatroskaDemuxContext *matroska)
1155
{
1156
    int res = 0;
1157
    uint32_t id;
1158
    MatroskaTrack *track;
1159

    
1160
    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1161

    
1162
    /* Allocate a generic track. As soon as we know its type we'll realloc. */
1163
    track = av_mallocz(sizeof(MatroskaTrack));
1164
    matroska->num_tracks++;
1165

    
1166
    /* start with the master */
1167
    if ((res = ebml_read_master(matroska, &id)) < 0)
1168
        return res;
1169

    
1170
    /* try reading the trackentry headers */
1171
    while (res == 0) {
1172
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1173
            res = AVERROR_IO;
1174
            break;
1175
        } else if (matroska->level_up > 0) {
1176
            matroska->level_up--;
1177
            break;
1178
        }
1179

    
1180
        switch (id) {
1181
            /* track number (unique stream ID) */
1182
            case MATROSKA_ID_TRACKNUMBER: {
1183
                uint64_t num;
1184
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1185
                    break;
1186
                track->num = num;
1187
                break;
1188
            }
1189

    
1190
            /* track UID (unique identifier) */
1191
            case MATROSKA_ID_TRACKUID: {
1192
                uint64_t num;
1193
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1194
                    break;
1195
                track->uid = num;
1196
                break;
1197
            }
1198

    
1199
            /* track type (video, audio, combined, subtitle, etc.) */
1200
            case MATROSKA_ID_TRACKTYPE: {
1201
                uint64_t num;
1202
                if (track->type != 0) {
1203
                    av_log(matroska->ctx, AV_LOG_INFO,
1204
                           "More than one tracktype in an entry - skip\n");
1205
                    break;
1206
                }
1207
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1208
                    break;
1209
                track->type = num;
1210

    
1211
                /* ok, so we're actually going to reallocate this thing */
1212
                switch (track->type) {
1213
                    case MATROSKA_TRACK_TYPE_VIDEO:
1214
                        track = (MatroskaTrack *)
1215
                            av_realloc(track, sizeof(MatroskaVideoTrack));
1216
                        break;
1217
                    case MATROSKA_TRACK_TYPE_AUDIO:
1218
                        track = (MatroskaTrack *)
1219
                            av_realloc(track, sizeof(MatroskaAudioTrack));
1220
                        ((MatroskaAudioTrack *)track)->channels = 1;
1221
                        ((MatroskaAudioTrack *)track)->samplerate = 8000;
1222
                        break;
1223
                    case MATROSKA_TRACK_TYPE_SUBTITLE:
1224
                        track = (MatroskaTrack *)
1225
                            av_realloc(track, sizeof(MatroskaSubtitleTrack));
1226
                        break;
1227
                    case MATROSKA_TRACK_TYPE_COMPLEX:
1228
                    case MATROSKA_TRACK_TYPE_LOGO:
1229
                    case MATROSKA_TRACK_TYPE_CONTROL:
1230
                    default:
1231
                        av_log(matroska->ctx, AV_LOG_INFO,
1232
                               "Unknown or unsupported track type 0x%x\n",
1233
                               track->type);
1234
                        track->type = 0;
1235
                        break;
1236
                }
1237
                matroska->tracks[matroska->num_tracks - 1] = track;
1238
                break;
1239
            }
1240

    
1241
            /* tracktype specific stuff for video */
1242
            case MATROSKA_ID_TRACKVIDEO: {
1243
                MatroskaVideoTrack *videotrack;
1244
                if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1245
                    av_log(matroska->ctx, AV_LOG_INFO,
1246
                           "video data in non-video track - ignoring\n");
1247
                    res = AVERROR_INVALIDDATA;
1248
                    break;
1249
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1250
                    break;
1251
                videotrack = (MatroskaVideoTrack *)track;
1252

    
1253
                while (res == 0) {
1254
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1255
                        res = AVERROR_IO;
1256
                        break;
1257
                    } else if (matroska->level_up > 0) {
1258
                        matroska->level_up--;
1259
                        break;
1260
                    }
1261

    
1262
                    switch (id) {
1263
                        /* fixme, this should be one-up, but I get it here */
1264
                        case MATROSKA_ID_TRACKDEFAULTDURATION: {
1265
                            uint64_t num;
1266
                            if ((res = ebml_read_uint (matroska, &id,
1267
                                                       &num)) < 0)
1268
                                break;
1269
                            track->default_duration = num;
1270
                            break;
1271
                        }
1272

    
1273
                        /* video framerate */
1274
                        case MATROSKA_ID_VIDEOFRAMERATE: {
1275
                            double num;
1276
                            if ((res = ebml_read_float(matroska, &id,
1277
                                                       &num)) < 0)
1278
                                break;
1279
                            track->default_duration = 1000000000 * (1. / num);
1280
                            break;
1281
                        }
1282

    
1283
                        /* width of the size to display the video at */
1284
                        case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1285
                            uint64_t num;
1286
                            if ((res = ebml_read_uint(matroska, &id,
1287
                                                      &num)) < 0)
1288
                                break;
1289
                            videotrack->display_width = num;
1290
                            break;
1291
                        }
1292

    
1293
                        /* height of the size to display the video at */
1294
                        case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1295
                            uint64_t num;
1296
                            if ((res = ebml_read_uint(matroska, &id,
1297
                                                      &num)) < 0)
1298
                                break;
1299
                            videotrack->display_height = num;
1300
                            break;
1301
                        }
1302

    
1303
                        /* width of the video in the file */
1304
                        case MATROSKA_ID_VIDEOPIXELWIDTH: {
1305
                            uint64_t num;
1306
                            if ((res = ebml_read_uint(matroska, &id,
1307
                                                      &num)) < 0)
1308
                                break;
1309
                            videotrack->pixel_width = num;
1310
                            break;
1311
                        }
1312

    
1313
                        /* height of the video in the file */
1314
                        case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1315
                            uint64_t num;
1316
                            if ((res = ebml_read_uint(matroska, &id,
1317
                                                      &num)) < 0)
1318
                                break;
1319
                            videotrack->pixel_height = num;
1320
                            break;
1321
                        }
1322

    
1323
                        /* whether the video is interlaced */
1324
                        case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1325
                            uint64_t num;
1326
                            if ((res = ebml_read_uint(matroska, &id,
1327
                                                      &num)) < 0)
1328
                                break;
1329
                            if (num)
1330
                                track->flags |=
1331
                                    MATROSKA_VIDEOTRACK_INTERLACED;
1332
                            else
1333
                                track->flags &=
1334
                                    ~MATROSKA_VIDEOTRACK_INTERLACED;
1335
                            break;
1336
                        }
1337

    
1338
                        /* stereo mode (whether the video has two streams,
1339
                         * where one is for the left eye and the other for
1340
                         * the right eye, which creates a 3D-like
1341
                         * effect) */
1342
                        case MATROSKA_ID_VIDEOSTEREOMODE: {
1343
                            uint64_t num;
1344
                            if ((res = ebml_read_uint(matroska, &id,
1345
                                                      &num)) < 0)
1346
                                break;
1347
                            if (num != MATROSKA_EYE_MODE_MONO &&
1348
                                num != MATROSKA_EYE_MODE_LEFT &&
1349
                                num != MATROSKA_EYE_MODE_RIGHT &&
1350
                                num != MATROSKA_EYE_MODE_BOTH) {
1351
                                av_log(matroska->ctx, AV_LOG_INFO,
1352
                                       "Ignoring unknown eye mode 0x%x\n",
1353
                                       (uint32_t) num);
1354
                                break;
1355
                            }
1356
                            videotrack->eye_mode = num;
1357
                            break;
1358
                        }
1359

    
1360
                        /* aspect ratio behaviour */
1361
                        case MATROSKA_ID_VIDEOASPECTRATIO: {
1362
                            uint64_t num;
1363
                            if ((res = ebml_read_uint(matroska, &id,
1364
                                                      &num)) < 0)
1365
                                break;
1366
                            if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1367
                                num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1368
                                num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1369
                                av_log(matroska->ctx, AV_LOG_INFO,
1370
                                       "Ignoring unknown aspect ratio 0x%x\n",
1371
                                       (uint32_t) num);
1372
                                break;
1373
                            }
1374
                            videotrack->ar_mode = num;
1375
                            break;
1376
                        }
1377

    
1378
                        /* colourspace (only matters for raw video)
1379
                         * fourcc */
1380
                        case MATROSKA_ID_VIDEOCOLOURSPACE: {
1381
                            uint64_t num;
1382
                            if ((res = ebml_read_uint(matroska, &id,
1383
                                                      &num)) < 0)
1384
                                break;
1385
                            videotrack->fourcc = num;
1386
                            break;
1387
                        }
1388

    
1389
                        default:
1390
                            av_log(matroska->ctx, AV_LOG_INFO,
1391
                                   "Unknown video track header entry "
1392
                                   "0x%x - ignoring\n", id);
1393
                            /* pass-through */
1394

    
1395
                        case EBML_ID_VOID:
1396
                            res = ebml_read_skip(matroska);
1397
                            break;
1398
                    }
1399

    
1400
                    if (matroska->level_up) {
1401
                        matroska->level_up--;
1402
                        break;
1403
                    }
1404
                }
1405
                break;
1406
            }
1407

    
1408
            /* tracktype specific stuff for audio */
1409
            case MATROSKA_ID_TRACKAUDIO: {
1410
                MatroskaAudioTrack *audiotrack;
1411
                if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1412
                    av_log(matroska->ctx, AV_LOG_INFO,
1413
                           "audio data in non-audio track - ignoring\n");
1414
                    res = AVERROR_INVALIDDATA;
1415
                    break;
1416
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1417
                    break;
1418
                audiotrack = (MatroskaAudioTrack *)track;
1419

    
1420
                while (res == 0) {
1421
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1422
                        res = AVERROR_IO;
1423
                        break;
1424
                    } else if (matroska->level_up > 0) {
1425
                        matroska->level_up--;
1426
                        break;
1427
                    }
1428

    
1429
                    switch (id) {
1430
                        /* samplerate */
1431
                        case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1432
                            double num;
1433
                            if ((res = ebml_read_float(matroska, &id,
1434
                                                       &num)) < 0)
1435
                                break;
1436
                            audiotrack->samplerate = num;
1437
                            break;
1438
                        }
1439

    
1440
                            /* bitdepth */
1441
                        case MATROSKA_ID_AUDIOBITDEPTH: {
1442
                            uint64_t num;
1443
                            if ((res = ebml_read_uint(matroska, &id,
1444
                                                      &num)) < 0)
1445
                                break;
1446
                            audiotrack->bitdepth = num;
1447
                            break;
1448
                        }
1449

    
1450
                            /* channels */
1451
                        case MATROSKA_ID_AUDIOCHANNELS: {
1452
                            uint64_t num;
1453
                            if ((res = ebml_read_uint(matroska, &id,
1454
                                                      &num)) < 0)
1455
                                break;
1456
                            audiotrack->channels = num;
1457
                            break;
1458
                        }
1459

    
1460
                        default:
1461
                            av_log(matroska->ctx, AV_LOG_INFO,
1462
                                   "Unknown audio track header entry "
1463
                                   "0x%x - ignoring\n", id);
1464
                            /* pass-through */
1465

    
1466
                        case EBML_ID_VOID:
1467
                            res = ebml_read_skip(matroska);
1468
                            break;
1469
                    }
1470

    
1471
                    if (matroska->level_up) {
1472
                        matroska->level_up--;
1473
                        break;
1474
                    }
1475
                }
1476
                break;
1477
            }
1478

    
1479
                /* codec identifier */
1480
            case MATROSKA_ID_CODECID: {
1481
                char *text;
1482
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1483
                    break;
1484
                track->codec_id = text;
1485
                break;
1486
            }
1487

    
1488
                /* codec private data */
1489
            case MATROSKA_ID_CODECPRIVATE: {
1490
                uint8_t *data;
1491
                int size;
1492
                if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1493
                    break;
1494
                track->codec_priv = data;
1495
                track->codec_priv_size = size;
1496
                break;
1497
            }
1498

    
1499
                /* name of the codec */
1500
            case MATROSKA_ID_CODECNAME: {
1501
                char *text;
1502
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1503
                    break;
1504
                track->codec_name = text;
1505
                break;
1506
            }
1507

    
1508
                /* name of this track */
1509
            case MATROSKA_ID_TRACKNAME: {
1510
                char *text;
1511
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1512
                    break;
1513
                track->name = text;
1514
                break;
1515
            }
1516

    
1517
                /* language (matters for audio/subtitles, mostly) */
1518
            case MATROSKA_ID_TRACKLANGUAGE: {
1519
                char *text;
1520
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1521
                    break;
1522
                track->language = text;
1523
                break;
1524
            }
1525

    
1526
                /* whether this is actually used */
1527
            case MATROSKA_ID_TRACKFLAGENABLED: {
1528
                uint64_t num;
1529
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1530
                    break;
1531
                if (num)
1532
                    track->flags |= MATROSKA_TRACK_ENABLED;
1533
                else
1534
                    track->flags &= ~MATROSKA_TRACK_ENABLED;
1535
                break;
1536
            }
1537

    
1538
                /* whether it's the default for this track type */
1539
            case MATROSKA_ID_TRACKFLAGDEFAULT: {
1540
                uint64_t num;
1541
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1542
                    break;
1543
                if (num)
1544
                    track->flags |= MATROSKA_TRACK_DEFAULT;
1545
                else
1546
                    track->flags &= ~MATROSKA_TRACK_DEFAULT;
1547
                break;
1548
            }
1549

    
1550
                /* lacing (like MPEG, where blocks don't end/start on frame
1551
                 * boundaries) */
1552
            case MATROSKA_ID_TRACKFLAGLACING: {
1553
                uint64_t num;
1554
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1555
                    break;
1556
                if (num)
1557
                    track->flags |= MATROSKA_TRACK_LACING;
1558
                else
1559
                    track->flags &= ~MATROSKA_TRACK_LACING;
1560
                break;
1561
            }
1562

    
1563
                /* default length (in time) of one data block in this track */
1564
            case MATROSKA_ID_TRACKDEFAULTDURATION: {
1565
                uint64_t num;
1566
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1567
                    break;
1568
                track->default_duration = num;
1569
                break;
1570
            }
1571

    
1572
            default:
1573
                av_log(matroska->ctx, AV_LOG_INFO,
1574
                       "Unknown track header entry 0x%x - ignoring\n", id);
1575
                /* pass-through */
1576

    
1577
            case EBML_ID_VOID:
1578
            /* we ignore these because they're nothing useful. */
1579
            case MATROSKA_ID_CODECINFOURL:
1580
            case MATROSKA_ID_CODECDOWNLOADURL:
1581
            case MATROSKA_ID_TRACKMINCACHE:
1582
            case MATROSKA_ID_TRACKMAXCACHE:
1583
                res = ebml_read_skip(matroska);
1584
                break;
1585
        }
1586

    
1587
        if (matroska->level_up) {
1588
            matroska->level_up--;
1589
            break;
1590
        }
1591
    }
1592

    
1593
    return res;
1594
}
1595

    
1596
static int
1597
matroska_parse_tracks (MatroskaDemuxContext *matroska)
1598
{
1599
    int res = 0;
1600
    uint32_t id;
1601

    
1602
    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1603

    
1604
    while (res == 0) {
1605
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1606
            res = AVERROR_IO;
1607
            break;
1608
        } else if (matroska->level_up) {
1609
            matroska->level_up--;
1610
            break;
1611
        }
1612

    
1613
        switch (id) {
1614
            /* one track within the "all-tracks" header */
1615
            case MATROSKA_ID_TRACKENTRY:
1616
                res = matroska_add_stream(matroska);
1617
                break;
1618

    
1619
            default:
1620
                av_log(matroska->ctx, AV_LOG_INFO,
1621
                       "Unknown entry 0x%x in track header\n", id);
1622
                /* fall-through */
1623

    
1624
            case EBML_ID_VOID:
1625
                res = ebml_read_skip(matroska);
1626
                break;
1627
        }
1628

    
1629
        if (matroska->level_up) {
1630
            matroska->level_up--;
1631
            break;
1632
        }
1633
    }
1634

    
1635
    return res;
1636
}
1637

    
1638
static int
1639
matroska_parse_index (MatroskaDemuxContext *matroska)
1640
{
1641
    int res = 0;
1642
    uint32_t id;
1643
    MatroskaDemuxIndex idx;
1644

    
1645
    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1646

    
1647
    while (res == 0) {
1648
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1649
            res = AVERROR_IO;
1650
            break;
1651
        } else if (matroska->level_up) {
1652
            matroska->level_up--;
1653
            break;
1654
        }
1655

    
1656
        switch (id) {
1657
            /* one single index entry ('point') */
1658
            case MATROSKA_ID_POINTENTRY:
1659
                if ((res = ebml_read_master(matroska, &id)) < 0)
1660
                    break;
1661

    
1662
                /* in the end, we hope to fill one entry with a
1663
                 * timestamp, a file position and a tracknum */
1664
                idx.pos   = (uint64_t) -1;
1665
                idx.time  = (uint64_t) -1;
1666
                idx.track = (uint16_t) -1;
1667

    
1668
                while (res == 0) {
1669
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1670
                        res = AVERROR_IO;
1671
                        break;
1672
                    } else if (matroska->level_up) {
1673
                        matroska->level_up--;
1674
                        break;
1675
                    }
1676

    
1677
                    switch (id) {
1678
                        /* one single index entry ('point') */
1679
                        case MATROSKA_ID_CUETIME: {
1680
                            uint64_t time;
1681
                            if ((res = ebml_read_uint(matroska, &id,
1682
                                                      &time)) < 0)
1683
                                break;
1684
                            idx.time = time * matroska->time_scale;
1685
                            break;
1686
                        }
1687

    
1688
                        /* position in the file + track to which it
1689
                         * belongs */
1690
                        case MATROSKA_ID_CUETRACKPOSITION:
1691
                            if ((res = ebml_read_master(matroska, &id)) < 0)
1692
                                break;
1693

    
1694
                            while (res == 0) {
1695
                                if (!(id = ebml_peek_id (matroska,
1696
                                                    &matroska->level_up))) {
1697
                                    res = AVERROR_IO;
1698
                                    break;
1699
                                } else if (matroska->level_up) {
1700
                                    matroska->level_up--;
1701
                                    break;
1702
                                }
1703

    
1704
                                switch (id) {
1705
                                    /* track number */
1706
                                    case MATROSKA_ID_CUETRACK: {
1707
                                        uint64_t num;
1708
                                        if ((res = ebml_read_uint(matroska,
1709
                                                          &id, &num)) < 0)
1710
                                            break;
1711
                                        idx.track = num;
1712
                                        break;
1713
                                    }
1714

    
1715
                                        /* position in file */
1716
                                    case MATROSKA_ID_CUECLUSTERPOSITION: {
1717
                                        uint64_t num;
1718
                                        if ((res = ebml_read_uint(matroska,
1719
                                                          &id, &num)) < 0)
1720
                                            break;
1721
                                        idx.pos = num;
1722
                                        break;
1723
                                    }
1724

    
1725
                                    default:
1726
                                        av_log(matroska->ctx, AV_LOG_INFO,
1727
                                               "Unknown entry 0x%x in "
1728
                                               "CuesTrackPositions\n", id);
1729
                                        /* fall-through */
1730

    
1731
                                    case EBML_ID_VOID:
1732
                                        res = ebml_read_skip(matroska);
1733
                                        break;
1734
                                }
1735

    
1736
                                if (matroska->level_up) {
1737
                                    matroska->level_up--;
1738
                                    break;
1739
                                }
1740
                            }
1741

    
1742
                            break;
1743

    
1744
                        default:
1745
                            av_log(matroska->ctx, AV_LOG_INFO,
1746
                                   "Unknown entry 0x%x in cuespoint "
1747
                                   "index\n", id);
1748
                            /* fall-through */
1749

    
1750
                        case EBML_ID_VOID:
1751
                            res = ebml_read_skip(matroska);
1752
                            break;
1753
                    }
1754

    
1755
                    if (matroska->level_up) {
1756
                        matroska->level_up--;
1757
                        break;
1758
                    }
1759
                }
1760

    
1761
                /* so let's see if we got what we wanted */
1762
                if (idx.pos   != (uint64_t) -1 &&
1763
                    idx.time  != (uint64_t) -1 &&
1764
                    idx.track != (uint16_t) -1) {
1765
                    if (matroska->num_indexes % 32 == 0) {
1766
                        /* re-allocate bigger index */
1767
                        matroska->index =
1768
                            av_realloc(matroska->index,
1769
                                       (matroska->num_indexes + 32) *
1770
                                       sizeof(MatroskaDemuxIndex));
1771
                    }
1772
                    matroska->index[matroska->num_indexes] = idx;
1773
                    matroska->num_indexes++;
1774
                }
1775
                break;
1776

    
1777
            default:
1778
                av_log(matroska->ctx, AV_LOG_INFO,
1779
                       "Unknown entry 0x%x in cues header\n", id);
1780
                /* fall-through */
1781

    
1782
            case EBML_ID_VOID:
1783
                res = ebml_read_skip(matroska);
1784
                break;
1785
        }
1786

    
1787
        if (matroska->level_up) {
1788
            matroska->level_up--;
1789
            break;
1790
        }
1791
    }
1792

    
1793
    return res;
1794
}
1795

    
1796
static int
1797
matroska_parse_metadata (MatroskaDemuxContext *matroska)
1798
{
1799
    int res = 0;
1800
    uint32_t id;
1801

    
1802
    while (res == 0) {
1803
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1804
            res = AVERROR_IO;
1805
            break;
1806
        } else if (matroska->level_up) {
1807
            matroska->level_up--;
1808
            break;
1809
        }
1810

    
1811
        switch (id) {
1812
            /* Hm, this is unsupported... */
1813
            default:
1814
                av_log(matroska->ctx, AV_LOG_INFO,
1815
                       "Unknown entry 0x%x in metadata header\n", id);
1816
                /* fall-through */
1817

    
1818
            case EBML_ID_VOID:
1819
                res = ebml_read_skip(matroska);
1820
                break;
1821
        }
1822

    
1823
        if (matroska->level_up) {
1824
            matroska->level_up--;
1825
            break;
1826
        }
1827
    }
1828

    
1829
    return res;
1830
}
1831

    
1832
static int
1833
matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1834
{
1835
    int res = 0;
1836
    uint32_t id;
1837

    
1838
    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1839

    
1840
    while (res == 0) {
1841
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1842
            res = AVERROR_IO;
1843
            break;
1844
        } else if (matroska->level_up) {
1845
            matroska->level_up--;
1846
            break;
1847
        }
1848

    
1849
        switch (id) {
1850
            case MATROSKA_ID_SEEKENTRY: {
1851
                uint32_t seek_id = 0, peek_id_cache = 0;
1852
                uint64_t seek_pos = (uint64_t) -1, t;
1853

    
1854
                if ((res = ebml_read_master(matroska, &id)) < 0)
1855
                    break;
1856

    
1857
                while (res == 0) {
1858
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1859
                        res = AVERROR_IO;
1860
                        break;
1861
                    } else if (matroska->level_up) {
1862
                        matroska->level_up--;
1863
                        break;
1864
                    }
1865

    
1866
                    switch (id) {
1867
                        case MATROSKA_ID_SEEKID:
1868
                            res = ebml_read_uint(matroska, &id, &t);
1869
                            seek_id = t;
1870
                            break;
1871

    
1872
                        case MATROSKA_ID_SEEKPOSITION:
1873
                            res = ebml_read_uint(matroska, &id, &seek_pos);
1874
                            break;
1875

    
1876
                        default:
1877
                            av_log(matroska->ctx, AV_LOG_INFO,
1878
                                   "Unknown seekhead ID 0x%x\n", id);
1879
                            /* fall-through */
1880

    
1881
                        case EBML_ID_VOID:
1882
                            res = ebml_read_skip(matroska);
1883
                            break;
1884
                    }
1885

    
1886
                    if (matroska->level_up) {
1887
                        matroska->level_up--;
1888
                        break;
1889
                    }
1890
                }
1891

    
1892
                if (!seek_id || seek_pos == (uint64_t) -1) {
1893
                    av_log(matroska->ctx, AV_LOG_INFO,
1894
                           "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1895
                           seek_id, seek_pos);
1896
                    break;
1897
                }
1898

    
1899
                switch (seek_id) {
1900
                    case MATROSKA_ID_CUES:
1901
                    case MATROSKA_ID_TAGS: {
1902
                        uint32_t level_up = matroska->level_up;
1903
                        offset_t before_pos;
1904
                        uint64_t length;
1905
                        MatroskaLevel level;
1906

    
1907
                        /* remember the peeked ID and the current position */
1908
                        peek_id_cache = matroska->peek_id;
1909
                        before_pos = url_ftell(&matroska->ctx->pb);
1910

    
1911
                        /* seek */
1912
                        if ((res = ebml_read_seek(matroska, seek_pos +
1913
                                               matroska->segment_start)) < 0)
1914
                            return res;
1915

    
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
                            return AVERROR_UNKNOWN;
1923
                        }
1924

    
1925
                        level.start = 0;
1926
                        level.length = (uint64_t)-1;
1927
                        matroska->levels[matroska->num_levels] = level;
1928
                        matroska->num_levels++;
1929

    
1930
                        /* check ID */
1931
                        if (!(id = ebml_peek_id (matroska,
1932
                                                 &matroska->level_up)))
1933
                            break;
1934
                        if (id != seek_id) {
1935
                            av_log(matroska->ctx, AV_LOG_INFO,
1936
                                   "We looked for ID=0x%x but got "
1937
                                   "ID=0x%x (pos=%"PRIu64")",
1938
                                   seek_id, id, seek_pos +
1939
                                   matroska->segment_start);
1940
                            goto finish;
1941
                        }
1942

    
1943
                        /* read master + parse */
1944
                        if ((res = ebml_read_master(matroska, &id)) < 0)
1945
                            break;
1946
                        switch (id) {
1947
                            case MATROSKA_ID_CUES:
1948
                                if (!(res = matroska_parse_index(matroska)) ||
1949
                                    url_feof(&matroska->ctx->pb)) {
1950
                                    matroska->index_parsed = 1;
1951
                                    res = 0;
1952
                                }
1953
                                break;
1954
                            case MATROSKA_ID_TAGS:
1955
                                if (!(res = matroska_parse_metadata(matroska)) ||
1956
                                   url_feof(&matroska->ctx->pb)) {
1957
                                    matroska->metadata_parsed = 1;
1958
                                    res = 0;
1959
                                }
1960
                                break;
1961
                        }
1962
                        if (res < 0)
1963
                            break;
1964

    
1965
                    finish:
1966
                        /* remove dummy level */
1967
                        while (matroska->num_levels) {
1968
                            matroska->num_levels--;
1969
                            length =
1970
                                matroska->levels[matroska->num_levels].length;
1971
                            if (length == (uint64_t)-1)
1972
                                break;
1973
                        }
1974

    
1975
                        /* seek back */
1976
                        if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1977
                            return res;
1978
                        matroska->peek_id = peek_id_cache;
1979
                        matroska->level_up = level_up;
1980
                        break;
1981
                    }
1982

    
1983
                    default:
1984
                        av_log(matroska->ctx, AV_LOG_INFO,
1985
                               "Ignoring seekhead entry for ID=0x%x\n",
1986
                               seek_id);
1987
                        break;
1988
                }
1989

    
1990
                break;
1991
            }
1992

    
1993
            default:
1994
                av_log(matroska->ctx, AV_LOG_INFO,
1995
                       "Unknown seekhead ID 0x%x\n", id);
1996
                /* fall-through */
1997

    
1998
            case EBML_ID_VOID:
1999
                res = ebml_read_skip(matroska);
2000
                break;
2001
        }
2002

    
2003
        if (matroska->level_up) {
2004
            matroska->level_up--;
2005
            break;
2006
        }
2007
    }
2008

    
2009
    return res;
2010
}
2011

    
2012
static int
2013
matroska_read_header (AVFormatContext    *s,
2014
                      AVFormatParameters *ap)
2015
{
2016
    MatroskaDemuxContext *matroska = s->priv_data;
2017
    char *doctype;
2018
    int version, last_level, res = 0;
2019
    uint32_t id;
2020

    
2021
    matroska->ctx = s;
2022

    
2023
    /* First read the EBML header. */
2024
    doctype = NULL;
2025
    if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2026
        return res;
2027
    if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2028
        av_log(matroska->ctx, AV_LOG_ERROR,
2029
               "Wrong EBML doctype ('%s' != 'matroska').\n",
2030
               doctype ? doctype : "(none)");
2031
        if (doctype)
2032
            av_free(doctype);
2033
        return AVERROR_NOFMT;
2034
    }
2035
    av_free(doctype);
2036
    if (version != 1) {
2037
        av_log(matroska->ctx, AV_LOG_ERROR,
2038
               "Matroska demuxer version 1 too old for file version %d\n",
2039
               version);
2040
        return AVERROR_NOFMT;
2041
    }
2042

    
2043
    /* The next thing is a segment. */
2044
    while (1) {
2045
        if (!(id = ebml_peek_id(matroska, &last_level)))
2046
            return AVERROR_IO;
2047
        if (id == MATROSKA_ID_SEGMENT)
2048
            break;
2049

    
2050
        /* oi! */
2051
        av_log(matroska->ctx, AV_LOG_INFO,
2052
               "Expected a Segment ID (0x%x), but received 0x%x!\n",
2053
               MATROSKA_ID_SEGMENT, id);
2054
        if ((res = ebml_read_skip(matroska)) < 0)
2055
            return res;
2056
    }
2057

    
2058
    /* We now have a Matroska segment.
2059
     * Seeks are from the beginning of the segment,
2060
     * after the segment ID/length. */
2061
    if ((res = ebml_read_master(matroska, &id)) < 0)
2062
        return res;
2063
    matroska->segment_start = url_ftell(&s->pb);
2064

    
2065
    matroska->time_scale = 1000000;
2066
    /* we've found our segment, start reading the different contents in here */
2067
    while (res == 0) {
2068
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2069
            res = AVERROR_IO;
2070
            break;
2071
        } else if (matroska->level_up) {
2072
            matroska->level_up--;
2073
            break;
2074
        }
2075

    
2076
        switch (id) {
2077
            /* stream info */
2078
            case MATROSKA_ID_INFO: {
2079
                if ((res = ebml_read_master(matroska, &id)) < 0)
2080
                    break;
2081
                res = matroska_parse_info(matroska);
2082
                break;
2083
            }
2084

    
2085
            /* track info headers */
2086
            case MATROSKA_ID_TRACKS: {
2087
                if ((res = ebml_read_master(matroska, &id)) < 0)
2088
                    break;
2089
                res = matroska_parse_tracks(matroska);
2090
                break;
2091
            }
2092

    
2093
            /* stream index */
2094
            case MATROSKA_ID_CUES: {
2095
                if (!matroska->index_parsed) {
2096
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2097
                        break;
2098
                    res = matroska_parse_index(matroska);
2099
                } else
2100
                    res = ebml_read_skip(matroska);
2101
                break;
2102
            }
2103

    
2104
            /* metadata */
2105
            case MATROSKA_ID_TAGS: {
2106
                if (!matroska->metadata_parsed) {
2107
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2108
                        break;
2109
                    res = matroska_parse_metadata(matroska);
2110
                } else
2111
                    res = ebml_read_skip(matroska);
2112
                break;
2113
            }
2114

    
2115
            /* file index (if seekable, seek to Cues/Tags to parse it) */
2116
            case MATROSKA_ID_SEEKHEAD: {
2117
                if ((res = ebml_read_master(matroska, &id)) < 0)
2118
                    break;
2119
                res = matroska_parse_seekhead(matroska);
2120
                break;
2121
            }
2122

    
2123
            case MATROSKA_ID_CLUSTER: {
2124
                /* Do not read the master - this will be done in the next
2125
                 * call to matroska_read_packet. */
2126
                res = 1;
2127
                break;
2128
            }
2129

    
2130
            default:
2131
                av_log(matroska->ctx, AV_LOG_INFO,
2132
                       "Unknown matroska file header ID 0x%x\n", id);
2133
            /* fall-through */
2134

    
2135
            case EBML_ID_VOID:
2136
                res = ebml_read_skip(matroska);
2137
                break;
2138
        }
2139

    
2140
        if (matroska->level_up) {
2141
            matroska->level_up--;
2142
            break;
2143
        }
2144
    }
2145

    
2146
    if (res < 0)
2147
        return res;
2148

    
2149
    /* Have we found a cluster? */
2150
    if (res == 1) {
2151
        int i, j;
2152
        MatroskaTrack *track;
2153
        AVStream *st;
2154

    
2155
        for (i = 0; i < matroska->num_tracks; i++) {
2156
            enum CodecID codec_id = CODEC_ID_NONE;
2157
            void *extradata = NULL;
2158
            int extradata_size = 0;
2159
            track = matroska->tracks[i];
2160

    
2161
            /* libavformat does not really support subtitles.
2162
             * Also apply some sanity checks. */
2163
            if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
2164
                (track->codec_id == NULL))
2165
                continue;
2166

    
2167
            for(j=0; codec_tags[j].str; j++){
2168
                if(!strcmp(codec_tags[j].str, track->codec_id)){
2169
                    codec_id= codec_tags[j].id;
2170
                    break;
2171
                }
2172
            }
2173

    
2174
            /* Set the FourCC from the CodecID. */
2175
            /* This is the MS compatibility mode which stores a
2176
             * BITMAPINFOHEADER in the CodecPrivate. */
2177
            if (!strcmp(track->codec_id,
2178
                        MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2179
                (track->codec_priv_size >= 40) &&
2180
                (track->codec_priv != NULL)) {
2181
                unsigned char *p;
2182

    
2183
                /* Offset of biCompression. Stored in LE. */
2184
                p = (unsigned char *)track->codec_priv + 16;
2185
                ((MatroskaVideoTrack *)track)->fourcc = (p[3] << 24) |
2186
                                 (p[2] << 16) | (p[1] << 8) | p[0];
2187
                codec_id = codec_get_bmp_id(((MatroskaVideoTrack *)track)->fourcc);
2188

    
2189
            }
2190

    
2191
            /* This is the MS compatibility mode which stores a
2192
             * WAVEFORMATEX in the CodecPrivate. */
2193
            else if (!strcmp(track->codec_id,
2194
                             MATROSKA_CODEC_ID_AUDIO_ACM) &&
2195
                (track->codec_priv_size >= 18) &&
2196
                (track->codec_priv != NULL)) {
2197
                unsigned char *p;
2198
                uint16_t tag;
2199

    
2200
                /* Offset of wFormatTag. Stored in LE. */
2201
                p = (unsigned char *)track->codec_priv;
2202
                tag = (p[1] << 8) | p[0];
2203
                codec_id = codec_get_wav_id(tag);
2204

    
2205
            }
2206

    
2207
            if (codec_id == CODEC_ID_NONE) {
2208
                av_log(matroska->ctx, AV_LOG_INFO,
2209
                       "Unknown/unsupported CodecID %s.\n",
2210
                       track->codec_id);
2211
            }
2212

    
2213
            track->stream_index = matroska->num_streams;
2214

    
2215
            matroska->num_streams++;
2216
            st = av_new_stream(s, track->stream_index);
2217
            if (st == NULL)
2218
                return AVERROR_NOMEM;
2219
            av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2220

    
2221
            st->codec->codec_id = codec_id;
2222

    
2223
            if (track->default_duration)
2224
                av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2225
                          track->default_duration, 1000000000, 30000);
2226

    
2227
            if(extradata){
2228
                st->codec->extradata = extradata;
2229
                st->codec->extradata_size = extradata_size;
2230
            } else if(track->codec_priv && track->codec_priv_size > 0){
2231
                st->codec->extradata = av_malloc(track->codec_priv_size);
2232
                if(st->codec->extradata == NULL)
2233
                    return AVERROR_NOMEM;
2234
                st->codec->extradata_size = track->codec_priv_size;
2235
                memcpy(st->codec->extradata, track->codec_priv,
2236
                       track->codec_priv_size);
2237
            }
2238

    
2239
            if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2240
                MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2241

    
2242
                st->codec->codec_type = CODEC_TYPE_VIDEO;
2243
                st->codec->codec_tag = videotrack->fourcc;
2244
                st->codec->width = videotrack->pixel_width;
2245
                st->codec->height = videotrack->pixel_height;
2246
                if (videotrack->display_width == 0)
2247
                    videotrack->display_width= videotrack->pixel_width;
2248
                if (videotrack->display_height == 0)
2249
                    videotrack->display_height= videotrack->pixel_height;
2250
                av_reduce(&st->codec->sample_aspect_ratio.num,
2251
                          &st->codec->sample_aspect_ratio.den,
2252
                          st->codec->height * videotrack->display_width,
2253
                          st->codec-> width * videotrack->display_height,
2254
                          255);
2255
            } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2256
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2257

    
2258
                st->codec->codec_type = CODEC_TYPE_AUDIO;
2259
                st->codec->sample_rate = audiotrack->samplerate;
2260
                st->codec->channels = audiotrack->channels;
2261
            } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2262
                st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2263
            }
2264

    
2265
            /* What do we do with private data? E.g. for Vorbis. */
2266
        }
2267
    }
2268

    
2269
    return 0;
2270
}
2271

    
2272
static int
2273
matroska_find_track_by_num (MatroskaDemuxContext *matroska,
2274
                            int                   num)
2275
{
2276
    int i;
2277

    
2278
    for (i = 0; i < matroska->num_tracks; i++)
2279
        if (matroska->tracks[i]->num == num)
2280
            return i;
2281

    
2282
    return -1;
2283
}
2284

    
2285
static int
2286
matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2287
                           uint64_t              cluster_time)
2288
{
2289
    int res = 0;
2290
    uint32_t id;
2291
    AVPacket *pkt = NULL;
2292
    int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2293
    uint64_t duration = AV_NOPTS_VALUE;
2294
    int track = -1;
2295

    
2296
    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
2297

    
2298
    while (res == 0) {
2299
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2300
            res = AVERROR_IO;
2301
            break;
2302
        } else if (matroska->level_up) {
2303
            matroska->level_up--;
2304
            break;
2305
        }
2306

    
2307
        switch (id) {
2308
            /* one block inside the group. Note, block parsing is one
2309
             * of the harder things, so this code is a bit complicated.
2310
             * See http://www.matroska.org/ for documentation. */
2311
            case MATROSKA_ID_BLOCK: {
2312
                uint8_t *data, *origdata;
2313
                int size;
2314
                int16_t block_time;
2315
                uint32_t *lace_size = NULL;
2316
                int n, flags, laces = 0;
2317
                uint64_t num;
2318
                int64_t pos= url_ftell(&matroska->ctx->pb);
2319

    
2320
                if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
2321
                    break;
2322
                origdata = data;
2323

    
2324
                /* first byte(s): blocknum */
2325
                if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2326
                    av_log(matroska->ctx, AV_LOG_ERROR,
2327
                           "EBML block data error\n");
2328
                    av_free(origdata);
2329
                    break;
2330
                }
2331
                data += n;
2332
                size -= n;
2333

    
2334
                /* fetch track from num */
2335
                track = matroska_find_track_by_num(matroska, num);
2336
                if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2337
                    av_log(matroska->ctx, AV_LOG_INFO,
2338
                           "Invalid stream %d or size %u\n", track, size);
2339
                    av_free(origdata);
2340
                    break;
2341
                }
2342
                if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
2343
                    av_free(origdata);
2344
                    break;
2345
                }
2346

    
2347
                /* block_time (relative to cluster time) */
2348
                block_time = (data[0] << 8) | data[1];
2349
                data += 2;
2350
                size -= 2;
2351
                flags = *data;
2352
                data += 1;
2353
                size -= 1;
2354
                switch ((flags & 0x06) >> 1) {
2355
                    case 0x0: /* no lacing */
2356
                        laces = 1;
2357
                        lace_size = av_mallocz(sizeof(int));
2358
                        lace_size[0] = size;
2359
                        break;
2360

    
2361
                    case 0x1: /* xiph lacing */
2362
                    case 0x2: /* fixed-size lacing */
2363
                    case 0x3: /* EBML lacing */
2364
                        if (size == 0) {
2365
                            res = -1;
2366
                            break;
2367
                        }
2368
                        laces = (*data) + 1;
2369
                        data += 1;
2370
                        size -= 1;
2371
                        lace_size = av_mallocz(laces * sizeof(int));
2372

    
2373
                        switch ((flags & 0x06) >> 1) {
2374
                            case 0x1: /* xiph lacing */ {
2375
                                uint8_t temp;
2376
                                uint32_t total = 0;
2377
                                for (n = 0; res == 0 && n < laces - 1; n++) {
2378
                                    while (1) {
2379
                                        if (size == 0) {
2380
                                            res = -1;
2381
                                            break;
2382
                                        }
2383
                                        temp = *data;
2384
                                        lace_size[n] += temp;
2385
                                        data += 1;
2386
                                        size -= 1;
2387
                                        if (temp != 0xff)
2388
                                            break;
2389
                                    }
2390
                                    total += lace_size[n];
2391
                                }
2392
                                lace_size[n] = size - total;
2393
                                break;
2394
                            }
2395

    
2396
                            case 0x2: /* fixed-size lacing */
2397
                                for (n = 0; n < laces; n++)
2398
                                    lace_size[n] = size / laces;
2399
                                break;
2400

    
2401
                            case 0x3: /* EBML lacing */ {
2402
                                uint32_t total;
2403
                                n = matroska_ebmlnum_uint(data, size, &num);
2404
                                if (n < 0) {
2405
                                    av_log(matroska->ctx, AV_LOG_INFO,
2406
                                           "EBML block data error\n");
2407
                                    break;
2408
                                }
2409
                                data += n;
2410
                                size -= n;
2411
                                total = lace_size[0] = num;
2412
                                for (n = 1; res == 0 && n < laces - 1; n++) {
2413
                                    int64_t snum;
2414
                                    int r;
2415
                                    r = matroska_ebmlnum_sint (data, size,
2416
                                                               &snum);
2417
                                    if (r < 0) {
2418
                                        av_log(matroska->ctx, AV_LOG_INFO,
2419
                                               "EBML block data error\n");
2420
                                        break;
2421
                                    }
2422
                                    data += r;
2423
                                    size -= r;
2424
                                    lace_size[n] = lace_size[n - 1] + snum;
2425
                                    total += lace_size[n];
2426
                                }
2427
                                lace_size[n] = size - total;
2428
                                break;
2429
                            }
2430
                        }
2431
                        break;
2432
                }
2433

    
2434
                if (res == 0) {
2435
                    for (n = 0; n < laces; n++) {
2436
                        uint64_t timecode = AV_NOPTS_VALUE;
2437

    
2438
                        pkt = av_mallocz(sizeof(AVPacket));
2439
                        /* XXX: prevent data copy... */
2440
                        if (av_new_packet(pkt,lace_size[n]) < 0) {
2441
                            res = AVERROR_NOMEM;
2442
                            break;
2443
                        }
2444
                        if (cluster_time != (uint64_t)-1 && n == 0) {
2445
                            if (cluster_time + block_time >= 0)
2446
                                timecode = cluster_time + block_time;
2447
                        }
2448
                        /* FIXME: duration */
2449

    
2450
                        memcpy(pkt->data, data, lace_size[n]);
2451
                        data += lace_size[n];
2452
                        if (n == 0)
2453
                            pkt->flags = is_keyframe;
2454
                        pkt->stream_index =
2455
                            matroska->tracks[track]->stream_index;
2456

    
2457
                        pkt->pts = timecode;
2458
                        pkt->pos= pos;
2459

    
2460
                        matroska_queue_packet(matroska, pkt);
2461
                    }
2462
                }
2463

    
2464
                av_free(lace_size);
2465
                av_free(origdata);
2466
                break;
2467
            }
2468

    
2469
            case MATROSKA_ID_BLOCKDURATION: {
2470
                if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2471
                    break;
2472
                break;
2473
            }
2474

    
2475
            case MATROSKA_ID_BLOCKREFERENCE:
2476
                /* We've found a reference, so not even the first frame in
2477
                 * the lace is a key frame. */
2478
                is_keyframe = 0;
2479
                if (last_num_packets != matroska->num_packets)
2480
                    matroska->packets[last_num_packets]->flags = 0;
2481
                res = ebml_read_skip(matroska);
2482
                break;
2483

    
2484
            default:
2485
                av_log(matroska->ctx, AV_LOG_INFO,
2486
                       "Unknown entry 0x%x in blockgroup data\n", id);
2487
                /* fall-through */
2488

    
2489
            case EBML_ID_VOID:
2490
                res = ebml_read_skip(matroska);
2491
                break;
2492
        }
2493

    
2494
        if (matroska->level_up) {
2495
            matroska->level_up--;
2496
            break;
2497
        }
2498
    }
2499

    
2500
    if (pkt)
2501
    {
2502
        if (duration != AV_NOPTS_VALUE)
2503
            pkt->duration = duration;
2504
        else if (track >= 0 && track < matroska->num_tracks)
2505
            pkt->duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2506
    }
2507

    
2508
    return res;
2509
}
2510

    
2511
static int
2512
matroska_parse_cluster (MatroskaDemuxContext *matroska)
2513
{
2514
    int res = 0;
2515
    uint32_t id;
2516
    uint64_t cluster_time = 0;
2517

    
2518
    av_log(matroska->ctx, AV_LOG_DEBUG,
2519
           "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
2520

    
2521
    while (res == 0) {
2522
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2523
            res = AVERROR_IO;
2524
            break;
2525
        } else if (matroska->level_up) {
2526
            matroska->level_up--;
2527
            break;
2528
        }
2529

    
2530
        switch (id) {
2531
            /* cluster timecode */
2532
            case MATROSKA_ID_CLUSTERTIMECODE: {
2533
                uint64_t num;
2534
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2535
                    break;
2536
                cluster_time = num;
2537
                break;
2538
            }
2539

    
2540
                /* a group of blocks inside a cluster */
2541
            case MATROSKA_ID_BLOCKGROUP:
2542
                if ((res = ebml_read_master(matroska, &id)) < 0)
2543
                    break;
2544
                res = matroska_parse_blockgroup(matroska, cluster_time);
2545
                break;
2546

    
2547
            default:
2548
                av_log(matroska->ctx, AV_LOG_INFO,
2549
                       "Unknown entry 0x%x in cluster data\n", id);
2550
                /* fall-through */
2551

    
2552
            case EBML_ID_VOID:
2553
                res = ebml_read_skip(matroska);
2554
                break;
2555
        }
2556

    
2557
        if (matroska->level_up) {
2558
            matroska->level_up--;
2559
            break;
2560
        }
2561
    }
2562

    
2563
    return res;
2564
}
2565

    
2566
static int
2567
matroska_read_packet (AVFormatContext *s,
2568
                      AVPacket        *pkt)
2569
{
2570
    MatroskaDemuxContext *matroska = s->priv_data;
2571
    int res = 0;
2572
    uint32_t id;
2573

    
2574
    /* Do we still have a packet queued? */
2575
    if (matroska_deliver_packet(matroska, pkt) == 0)
2576
        return 0;
2577

    
2578
    /* Have we already reached the end? */
2579
    if (matroska->done)
2580
        return AVERROR_IO;
2581

    
2582
    while (res == 0) {
2583
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2584
            res = AVERROR_IO;
2585
            break;
2586
        } else if (matroska->level_up) {
2587
            matroska->level_up--;
2588
            break;
2589
        }
2590

    
2591
        switch (id) {
2592
            case MATROSKA_ID_CLUSTER:
2593
                if ((res = ebml_read_master(matroska, &id)) < 0)
2594
                    break;
2595
                if ((res = matroska_parse_cluster(matroska)) == 0)
2596
                    res = 1; /* Parsed one cluster, let's get out. */
2597
                break;
2598

    
2599
            default:
2600
            case EBML_ID_VOID:
2601
                res = ebml_read_skip(matroska);
2602
                break;
2603
        }
2604

    
2605
        if (matroska->level_up) {
2606
            matroska->level_up--;
2607
            break;
2608
        }
2609
    }
2610

    
2611
    if (res == -1)
2612
        matroska->done = 1;
2613

    
2614
    return matroska_deliver_packet(matroska, pkt);
2615
}
2616

    
2617
static int
2618
matroska_read_close (AVFormatContext *s)
2619
{
2620
    MatroskaDemuxContext *matroska = s->priv_data;
2621
    int n = 0;
2622

    
2623
    av_free(matroska->writing_app);
2624
    av_free(matroska->muxing_app);
2625
    av_free(matroska->index);
2626

    
2627
    if (matroska->packets != NULL) {
2628
        for (n = 0; n < matroska->num_packets; n++) {
2629
            av_free_packet(matroska->packets[n]);
2630
            av_free(matroska->packets[n]);
2631
        }
2632
        av_free(matroska->packets);
2633
    }
2634

    
2635
    for (n = 0; n < matroska->num_tracks; n++) {
2636
        MatroskaTrack *track = matroska->tracks[n];
2637
        av_free(track->codec_id);
2638
        av_free(track->codec_name);
2639
        av_free(track->codec_priv);
2640
        av_free(track->name);
2641
        av_free(track->language);
2642

    
2643
        av_free(track);
2644
    }
2645

    
2646
    return 0;
2647
}
2648

    
2649
AVInputFormat matroska_demuxer = {
2650
    "matroska",
2651
    "Matroska file format",
2652
    sizeof(MatroskaDemuxContext),
2653
    matroska_probe,
2654
    matroska_read_header,
2655
    matroska_read_packet,
2656
    matroska_read_close,
2657
};