Statistics
| Branch: | Revision:

ffmpeg / libavformat / matroska.c @ 8509fc7b

History | View | Annotate | Download (90 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_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_AUDIOOUTSAMPLINGFREQ 0x78B5
114

    
115
#define MATROSKA_ID_AUDIOBITDEPTH 0x6264
116
#define MATROSKA_ID_AUDIOCHANNELS 0x9F
117

    
118
/* ID in the cues master */
119
#define MATROSKA_ID_POINTENTRY 0xBB
120

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

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

    
129
/* IDs in the tags master */
130
/* TODO */
131

    
132
/* IDs in the seekhead master */
133
#define MATROSKA_ID_SEEKENTRY  0x4DBB
134

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

    
139
/* IDs in the cluster master */
140
#define MATROSKA_ID_CLUSTERTIMECODE 0xE7
141
#define MATROSKA_ID_BLOCKGROUP 0xA0
142
#define MATROSKA_ID_SIMPLEBLOCK 0xA3
143

    
144
/* IDs in the blockgroup master */
145
#define MATROSKA_ID_BLOCK      0xA1
146
#define MATROSKA_ID_BLOCKDURATION 0x9B
147
#define MATROSKA_ID_BLOCKREFERENCE 0xFB
148

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

    
158
typedef enum {
159
  MATROSKA_EYE_MODE_MONO  = 0x0,
160
  MATROSKA_EYE_MODE_RIGHT = 0x1,
161
  MATROSKA_EYE_MODE_LEFT  = 0x2,
162
  MATROSKA_EYE_MODE_BOTH  = 0x3,
163
} MatroskaEyeMode;
164

    
165
typedef enum {
166
  MATROSKA_ASPECT_RATIO_MODE_FREE  = 0x0,
167
  MATROSKA_ASPECT_RATIO_MODE_KEEP  = 0x1,
168
  MATROSKA_ASPECT_RATIO_MODE_FIXED = 0x2,
169
} MatroskaAspectRatioMode;
170

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

    
176
typedef enum {
177
  MATROSKA_TRACK_ENABLED = (1<<0),
178
  MATROSKA_TRACK_DEFAULT = (1<<1),
179
  MATROSKA_TRACK_LACING  = (1<<2),
180
  MATROSKA_TRACK_REAL_V  = (1<<4),
181
  MATROSKA_TRACK_REORDER = (1<<8),
182
  MATROSKA_TRACK_SHIFT   = (1<<16)
183
} MatroskaTrackFlags;
184

    
185
typedef enum {
186
  MATROSKA_VIDEOTRACK_INTERLACED = (MATROSKA_TRACK_SHIFT<<0)
187
} MatroskaVideoTrackFlags;
188

    
189
/*
190
 * Matroska Codec IDs. Strings.
191
 */
192

    
193
typedef struct CodecTags{
194
    const char *str;
195
    enum CodecID id;
196
}CodecTags;
197

    
198
#define MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC   "V_MS/VFW/FOURCC"
199
#define MATROSKA_CODEC_ID_AUDIO_ACM          "A_MS/ACM"
200

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

    
219
//    {"A_MS/ACM"         , CODEC_ID_NONE},
220
    {"A_MPEG/L1"        , CODEC_ID_MP3},
221
    {"A_MPEG/L2"        , CODEC_ID_MP3},
222
    {"A_MPEG/L3"        , CODEC_ID_MP3},
223
    {"A_PCM/INT/BIG"    , CODEC_ID_PCM_U16BE},
224
    {"A_PCM/INT/LIT"    , CODEC_ID_PCM_U16LE},
225
//    {"A_PCM/FLOAT/IEEE" , CODEC_ID_NONE},
226
    {"A_AC3"            , CODEC_ID_AC3},
227
    {"A_DTS"            , CODEC_ID_DTS},
228
    {"A_VORBIS"         , CODEC_ID_VORBIS},
229
    {"A_AAC"            , CODEC_ID_AAC},
230
    {"A_FLAC"           , CODEC_ID_FLAC},
231
    {"A_WAVPACK4"       , CODEC_ID_WAVPACK},
232
    {"A_TTA1"           , CODEC_ID_TTA},
233
    {NULL               , CODEC_ID_NONE}
234
/* TODO: AC3-9/10 (?), Real, Musepack, Quicktime */
235
};
236

    
237
/* max. depth in the EBML tree structure */
238
#define EBML_MAX_DEPTH 16
239

    
240
typedef struct Track {
241
    MatroskaTrackType type;
242

    
243
    /* Unique track number and track ID. stream_index is the index that
244
     * the calling app uses for this track. */
245
    uint32_t num,
246
        uid,
247
        stream_index;
248

    
249
    char *name,
250
        *language;
251

    
252
    char *codec_id,
253
        *codec_name;
254

    
255
    unsigned char *codec_priv;
256
    int codec_priv_size;
257

    
258
    uint64_t default_duration;
259
    MatroskaTrackFlags flags;
260
} MatroskaTrack;
261

    
262
typedef struct MatroskaVideoTrack {
263
    MatroskaTrack track;
264

    
265
    int pixel_width,
266
        pixel_height,
267
        display_width,
268
        display_height;
269

    
270
    uint32_t fourcc;
271

    
272
    MatroskaAspectRatioMode ar_mode;
273
    MatroskaEyeMode eye_mode;
274

    
275
    //..
276
} MatroskaVideoTrack;
277

    
278
typedef struct MatroskaAudioTrack {
279
    MatroskaTrack track;
280

    
281
    int channels,
282
        bitdepth,
283
        internal_samplerate,
284
        samplerate;
285
    //..
286
} MatroskaAudioTrack;
287

    
288
typedef struct MatroskaSubtitleTrack {
289
    MatroskaTrack track;
290

    
291
    //..
292
} MatroskaSubtitleTrack;
293

    
294
#define MAX_TRACK_SIZE (FFMAX(FFMAX(sizeof(MatroskaVideoTrack), \
295
                                    sizeof(MatroskaAudioTrack)), \
296
                                    sizeof(MatroskaSubtitleTrack)))
297

    
298
typedef struct MatroskaLevel {
299
    uint64_t start, length;
300
} MatroskaLevel;
301

    
302
typedef struct MatroskaDemuxIndex {
303
  uint64_t        pos;   /* of the corresponding *cluster*! */
304
  uint16_t        track; /* reference to 'num' */
305
  uint64_t        time;  /* in nanoseconds */
306
} MatroskaDemuxIndex;
307

    
308
typedef struct MatroskaDemuxContext {
309
    AVFormatContext *ctx;
310

    
311
    /* ebml stuff */
312
    int num_levels;
313
    MatroskaLevel levels[EBML_MAX_DEPTH];
314
    int level_up;
315

    
316
    /* matroska stuff */
317
    char *writing_app,
318
        *muxing_app;
319
    int64_t created;
320

    
321
    /* timescale in the file */
322
    int64_t time_scale;
323

    
324
    /* position (time, ns) */
325
    int64_t pos;
326

    
327
    /* num_streams is the number of streams that av_new_stream() was called
328
     * for ( = that are available to the calling program). */
329
    int num_tracks, num_streams;
330
    MatroskaTrack *tracks[MAX_STREAMS];
331

    
332
    /* cache for ID peeking */
333
    uint32_t peek_id;
334

    
335
    /* byte position of the segment inside the stream */
336
    offset_t segment_start;
337

    
338
    /* The packet queue. */
339
    AVPacket **packets;
340
    int num_packets;
341
    /* Second packet queue used to reorder pts of some video track. */
342
    AVPacket **packets_reorder;
343
    int num_packets_reorder;
344
    uint64_t reorder_max_pts;
345

    
346
    /* have we already parse metadata/cues/clusters? */
347
    int metadata_parsed,
348
        index_parsed,
349
        done;
350

    
351
    /* The index for seeking. */
352
    int num_indexes;
353
    MatroskaDemuxIndex *index;
354
} MatroskaDemuxContext;
355

    
356
/*
357
 * The first few functions handle EBML file parsing. The rest
358
 * is the document interpretation. Matroska really just is a
359
 * EBML file.
360
 */
361

    
362
/*
363
 * Return: the amount of levels in the hierarchy that the
364
 * current element lies higher than the previous one.
365
 * The opposite isn't done - that's auto-done using master
366
 * element reading.
367
 */
368

    
369
static int
370
ebml_read_element_level_up (MatroskaDemuxContext *matroska)
371
{
372
    ByteIOContext *pb = &matroska->ctx->pb;
373
    offset_t pos = url_ftell(pb);
374
    int num = 0;
375

    
376
    while (matroska->num_levels > 0) {
377
        MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
378

    
379
        if (pos >= level->start + level->length) {
380
            matroska->num_levels--;
381
            num++;
382
        } else {
383
            break;
384
        }
385
    }
386

    
387
    return num;
388
}
389

    
390
/*
391
 * Read: an "EBML number", which is defined as a variable-length
392
 * array of bytes. The first byte indicates the length by giving a
393
 * number of 0-bits followed by a one. The position of the first
394
 * "one" bit inside the first byte indicates the length of this
395
 * number.
396
 * Returns: num. of bytes read. < 0 on error.
397
 */
398

    
399
static int
400
ebml_read_num (MatroskaDemuxContext *matroska,
401
               int                   max_size,
402
               uint64_t             *number)
403
{
404
    ByteIOContext *pb = &matroska->ctx->pb;
405
    int len_mask = 0x80, read = 1, n = 1;
406
    int64_t total = 0;
407

    
408
    /* the first byte tells us the length in bytes - get_byte() can normally
409
     * return 0, but since that's not a valid first ebmlID byte, we can
410
     * use it safely here to catch EOS. */
411
    if (!(total = get_byte(pb))) {
412
        /* we might encounter EOS here */
413
        if (!url_feof(pb)) {
414
            offset_t pos = url_ftell(pb);
415
            av_log(matroska->ctx, AV_LOG_ERROR,
416
                   "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
417
                   pos, pos);
418
        }
419
        return AVERROR_IO; /* EOS or actual I/O error */
420
    }
421

    
422
    /* get the length of the EBML number */
423
    while (read <= max_size && !(total & len_mask)) {
424
        read++;
425
        len_mask >>= 1;
426
    }
427
    if (read > max_size) {
428
        offset_t pos = url_ftell(pb) - 1;
429
        av_log(matroska->ctx, AV_LOG_ERROR,
430
               "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
431
               (uint8_t) total, pos, pos);
432
        return AVERROR_INVALIDDATA;
433
    }
434

    
435
    /* read out length */
436
    total &= ~len_mask;
437
    while (n++ < read)
438
        total = (total << 8) | get_byte(pb);
439

    
440
    *number = total;
441

    
442
    return read;
443
}
444

    
445
/*
446
 * Read: the element content data ID.
447
 * Return: the number of bytes read or < 0 on error.
448
 */
449

    
450
static int
451
ebml_read_element_id (MatroskaDemuxContext *matroska,
452
                      uint32_t             *id,
453
                      int                  *level_up)
454
{
455
    int read;
456
    uint64_t total;
457

    
458
    /* if we re-call this, use our cached ID */
459
    if (matroska->peek_id != 0) {
460
        if (level_up)
461
            *level_up = 0;
462
        *id = matroska->peek_id;
463
        return 0;
464
    }
465

    
466
    /* read out the "EBML number", include tag in ID */
467
    if ((read = ebml_read_num(matroska, 4, &total)) < 0)
468
        return read;
469
    *id = matroska->peek_id  = total | (1 << (read * 7));
470

    
471
    /* level tracking */
472
    if (level_up)
473
        *level_up = ebml_read_element_level_up(matroska);
474

    
475
    return read;
476
}
477

    
478
/*
479
 * Read: element content length.
480
 * Return: the number of bytes read or < 0 on error.
481
 */
482

    
483
static int
484
ebml_read_element_length (MatroskaDemuxContext *matroska,
485
                          uint64_t             *length)
486
{
487
    /* clear cache since we're now beyond that data point */
488
    matroska->peek_id = 0;
489

    
490
    /* read out the "EBML number", include tag in ID */
491
    return ebml_read_num(matroska, 8, length);
492
}
493

    
494
/*
495
 * Return: the ID of the next element, or 0 on error.
496
 * Level_up contains the amount of levels that this
497
 * next element lies higher than the previous one.
498
 */
499

    
500
static uint32_t
501
ebml_peek_id (MatroskaDemuxContext *matroska,
502
              int                  *level_up)
503
{
504
    uint32_t id;
505

    
506
    assert(level_up != NULL);
507

    
508
    if (ebml_read_element_id(matroska, &id, level_up) < 0)
509
        return 0;
510

    
511
    return id;
512
}
513

    
514
/*
515
 * Seek to a given offset.
516
 * 0 is success, -1 is failure.
517
 */
518

    
519
static int
520
ebml_read_seek (MatroskaDemuxContext *matroska,
521
                offset_t              offset)
522
{
523
    ByteIOContext *pb = &matroska->ctx->pb;
524

    
525
    /* clear ID cache, if any */
526
    matroska->peek_id = 0;
527

    
528
    return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
529
}
530

    
531
/*
532
 * Skip the next element.
533
 * 0 is success, -1 is failure.
534
 */
535

    
536
static int
537
ebml_read_skip (MatroskaDemuxContext *matroska)
538
{
539
    ByteIOContext *pb = &matroska->ctx->pb;
540
    uint32_t id;
541
    uint64_t length;
542
    int res;
543

    
544
    if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
545
        (res = ebml_read_element_length(matroska, &length)) < 0)
546
        return res;
547

    
548
    url_fskip(pb, length);
549

    
550
    return 0;
551
}
552

    
553
/*
554
 * Read the next element as an unsigned int.
555
 * 0 is success, < 0 is failure.
556
 */
557

    
558
static int
559
ebml_read_uint (MatroskaDemuxContext *matroska,
560
                uint32_t             *id,
561
                uint64_t             *num)
562
{
563
    ByteIOContext *pb = &matroska->ctx->pb;
564
    int n = 0, size, res;
565
    uint64_t rlength;
566

    
567
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
568
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
569
        return res;
570
    size = rlength;
571
    if (size < 1 || size > 8) {
572
        offset_t pos = url_ftell(pb);
573
        av_log(matroska->ctx, AV_LOG_ERROR,
574
               "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
575
                size, pos, pos);
576
        return AVERROR_INVALIDDATA;
577
    }
578

    
579
    /* big-endian ordening; build up number */
580
    *num = 0;
581
    while (n++ < size)
582
        *num = (*num << 8) | get_byte(pb);
583

    
584
    return 0;
585
}
586

    
587
/*
588
 * Read the next element as a signed int.
589
 * 0 is success, < 0 is failure.
590
 */
591

    
592
static int
593
ebml_read_sint (MatroskaDemuxContext *matroska,
594
                uint32_t             *id,
595
                int64_t              *num)
596
{
597
    ByteIOContext *pb = &matroska->ctx->pb;
598
    int size, n = 1, negative = 0, res;
599
    uint64_t rlength;
600

    
601
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
602
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
603
        return res;
604
    size = rlength;
605
    if (size < 1 || size > 8) {
606
        offset_t pos = url_ftell(pb);
607
        av_log(matroska->ctx, AV_LOG_ERROR,
608
               "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
609
                size, pos, pos);
610
        return AVERROR_INVALIDDATA;
611
    }
612
    if ((*num = get_byte(pb)) & 0x80) {
613
        negative = 1;
614
        *num &= ~0x80;
615
    }
616
    while (n++ < size)
617
        *num = (*num << 8) | get_byte(pb);
618

    
619
    /* make signed */
620
    if (negative)
621
        *num = *num - (1LL << ((8 * size) - 1));
622

    
623
    return 0;
624
}
625

    
626
/*
627
 * Read the next element as a float.
628
 * 0 is success, < 0 is failure.
629
 */
630

    
631
static int
632
ebml_read_float (MatroskaDemuxContext *matroska,
633
                 uint32_t             *id,
634
                 double               *num)
635
{
636
    ByteIOContext *pb = &matroska->ctx->pb;
637
    int size, res;
638
    uint64_t rlength;
639

    
640
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
641
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
642
        return res;
643
    size = rlength;
644

    
645
    if (size == 4) {
646
        *num= av_int2flt(get_be32(pb));
647
    } else if(size==8){
648
        *num= av_int2dbl(get_be64(pb));
649
    } else{
650
        offset_t pos = url_ftell(pb);
651
        av_log(matroska->ctx, AV_LOG_ERROR,
652
               "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
653
               size, pos, pos);
654
        return AVERROR_INVALIDDATA;
655
    }
656

    
657
    return 0;
658
}
659

    
660
/*
661
 * Read the next element as an ASCII string.
662
 * 0 is success, < 0 is failure.
663
 */
664

    
665
static int
666
ebml_read_ascii (MatroskaDemuxContext *matroska,
667
                 uint32_t             *id,
668
                 char                **str)
669
{
670
    ByteIOContext *pb = &matroska->ctx->pb;
671
    int size, res;
672
    uint64_t rlength;
673

    
674
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
675
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
676
        return res;
677
    size = rlength;
678

    
679
    /* ebml strings are usually not 0-terminated, so we allocate one
680
     * byte more, read the string and NULL-terminate it ourselves. */
681
    if (size < 0 || !(*str = av_malloc(size + 1))) {
682
        av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
683
        return AVERROR_NOMEM;
684
    }
685
    if (get_buffer(pb, (uint8_t *) *str, size) != size) {
686
        offset_t pos = url_ftell(pb);
687
        av_log(matroska->ctx, AV_LOG_ERROR,
688
               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
689
        return AVERROR_IO;
690
    }
691
    (*str)[size] = '\0';
692

    
693
    return 0;
694
}
695

    
696
/*
697
 * Read the next element as a UTF-8 string.
698
 * 0 is success, < 0 is failure.
699
 */
700

    
701
static int
702
ebml_read_utf8 (MatroskaDemuxContext *matroska,
703
                uint32_t             *id,
704
                char                **str)
705
{
706
  return ebml_read_ascii(matroska, id, str);
707
}
708

    
709
/*
710
 * Read the next element as a date (nanoseconds since 1/1/2000).
711
 * 0 is success, < 0 is failure.
712
 */
713

    
714
static int
715
ebml_read_date (MatroskaDemuxContext *matroska,
716
                uint32_t             *id,
717
                int64_t              *date)
718
{
719
  return ebml_read_sint(matroska, id, date);
720
}
721

    
722
/*
723
 * Read the next element, but only the header. The contents
724
 * are supposed to be sub-elements which can be read separately.
725
 * 0 is success, < 0 is failure.
726
 */
727

    
728
static int
729
ebml_read_master (MatroskaDemuxContext *matroska,
730
                  uint32_t             *id)
731
{
732
    ByteIOContext *pb = &matroska->ctx->pb;
733
    uint64_t length;
734
    MatroskaLevel *level;
735
    int res;
736

    
737
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
738
        (res = ebml_read_element_length(matroska, &length)) < 0)
739
        return res;
740

    
741
    /* protect... (Heaven forbids that the '>' is true) */
742
    if (matroska->num_levels >= EBML_MAX_DEPTH) {
743
        av_log(matroska->ctx, AV_LOG_ERROR,
744
               "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
745
        return AVERROR_NOTSUPP;
746
    }
747

    
748
    /* remember level */
749
    level = &matroska->levels[matroska->num_levels++];
750
    level->start = url_ftell(pb);
751
    level->length = length;
752

    
753
    return 0;
754
}
755

    
756
/*
757
 * Read the next element as binary data.
758
 * 0 is success, < 0 is failure.
759
 */
760

    
761
static int
762
ebml_read_binary (MatroskaDemuxContext *matroska,
763
                  uint32_t             *id,
764
                  uint8_t             **binary,
765
                  int                  *size)
766
{
767
    ByteIOContext *pb = &matroska->ctx->pb;
768
    uint64_t rlength;
769
    int res;
770

    
771
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
772
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
773
        return res;
774
    *size = rlength;
775

    
776
    if (!(*binary = av_malloc(*size))) {
777
        av_log(matroska->ctx, AV_LOG_ERROR,
778
               "Memory allocation error\n");
779
        return AVERROR_NOMEM;
780
    }
781

    
782
    if (get_buffer(pb, *binary, *size) != *size) {
783
        offset_t pos = url_ftell(pb);
784
        av_log(matroska->ctx, AV_LOG_ERROR,
785
               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
786
        return AVERROR_IO;
787
    }
788

    
789
    return 0;
790
}
791

    
792
/*
793
 * Read signed/unsigned "EBML" numbers.
794
 * Return: number of bytes processed, < 0 on error.
795
 * XXX: use ebml_read_num().
796
 */
797

    
798
static int
799
matroska_ebmlnum_uint (uint8_t  *data,
800
                       uint32_t  size,
801
                       uint64_t *num)
802
{
803
    int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
804
    uint64_t total;
805

    
806
    if (size <= 0)
807
        return AVERROR_INVALIDDATA;
808

    
809
    total = data[0];
810
    while (read <= 8 && !(total & len_mask)) {
811
        read++;
812
        len_mask >>= 1;
813
    }
814
    if (read > 8)
815
        return AVERROR_INVALIDDATA;
816

    
817
    if ((total &= (len_mask - 1)) == len_mask - 1)
818
        num_ffs++;
819
    if (size < read)
820
        return AVERROR_INVALIDDATA;
821
    while (n < read) {
822
        if (data[n] == 0xff)
823
            num_ffs++;
824
        total = (total << 8) | data[n];
825
        n++;
826
    }
827

    
828
    if (read == num_ffs)
829
        *num = (uint64_t)-1;
830
    else
831
        *num = total;
832

    
833
    return read;
834
}
835

    
836
/*
837
 * Same as above, but signed.
838
 */
839

    
840
static int
841
matroska_ebmlnum_sint (uint8_t  *data,
842
                       uint32_t  size,
843
                       int64_t  *num)
844
{
845
    uint64_t unum;
846
    int res;
847

    
848
    /* read as unsigned number first */
849
    if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
850
        return res;
851

    
852
    /* make signed (weird way) */
853
    if (unum == (uint64_t)-1)
854
        *num = INT64_MAX;
855
    else
856
        *num = unum - ((1LL << ((7 * res) - 1)) - 1);
857

    
858
    return res;
859
}
860

    
861
/*
862
 * Read an EBML header.
863
 * 0 is success, < 0 is failure.
864
 */
865

    
866
static int
867
ebml_read_header (MatroskaDemuxContext *matroska,
868
                  char                **doctype,
869
                  int                  *version)
870
{
871
    uint32_t id;
872
    int level_up, res = 0;
873

    
874
    /* default init */
875
    if (doctype)
876
        *doctype = NULL;
877
    if (version)
878
        *version = 1;
879

    
880
    if (!(id = ebml_peek_id(matroska, &level_up)) ||
881
        level_up != 0 || id != EBML_ID_HEADER) {
882
        av_log(matroska->ctx, AV_LOG_ERROR,
883
               "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
884
        return AVERROR_INVALIDDATA;
885
    }
886
    if ((res = ebml_read_master(matroska, &id)) < 0)
887
        return res;
888

    
889
    while (res == 0) {
890
        if (!(id = ebml_peek_id(matroska, &level_up)))
891
            return AVERROR_IO;
892

    
893
        /* end-of-header */
894
        if (level_up)
895
            break;
896

    
897
        switch (id) {
898
            /* is our read version uptodate? */
899
            case EBML_ID_EBMLREADVERSION: {
900
                uint64_t num;
901

    
902
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
903
                    return res;
904
                if (num > EBML_VERSION) {
905
                    av_log(matroska->ctx, AV_LOG_ERROR,
906
                           "EBML version %"PRIu64" (> %d) is not supported\n",
907
                           num, EBML_VERSION);
908
                    return AVERROR_INVALIDDATA;
909
                }
910
                break;
911
            }
912

    
913
            /* we only handle 8 byte lengths at max */
914
            case EBML_ID_EBMLMAXSIZELENGTH: {
915
                uint64_t num;
916

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

    
928
            /* we handle 4 byte IDs at max */
929
            case EBML_ID_EBMLMAXIDLENGTH: {
930
                uint64_t num;
931

    
932
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
933
                    return res;
934
                if (num > sizeof(uint32_t)) {
935
                    av_log(matroska->ctx, AV_LOG_ERROR,
936
                           "IDs of size %"PRIu64" (> %zu) not supported\n",
937
                            num, sizeof(uint32_t));
938
                    return AVERROR_INVALIDDATA;
939
                }
940
                break;
941
            }
942

    
943
            case EBML_ID_DOCTYPE: {
944
                char *text;
945

    
946
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
947
                    return res;
948
                if (doctype) {
949
                    if (*doctype)
950
                        av_free(*doctype);
951
                    *doctype = text;
952
                } else
953
                    av_free(text);
954
                break;
955
            }
956

    
957
            case EBML_ID_DOCTYPEREADVERSION: {
958
                uint64_t num;
959

    
960
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
961
                    return res;
962
                if (version)
963
                    *version = num;
964
                break;
965
            }
966

    
967
            default:
968
                av_log(matroska->ctx, AV_LOG_INFO,
969
                       "Unknown data type 0x%x in EBML header", id);
970
                /* pass-through */
971

    
972
            case EBML_ID_VOID:
973
            /* we ignore these two, as they don't tell us anything we
974
             * care about */
975
            case EBML_ID_EBMLVERSION:
976
            case EBML_ID_DOCTYPEVERSION:
977
                res = ebml_read_skip (matroska);
978
                break;
979
        }
980
    }
981

    
982
    return 0;
983
}
984

    
985
/*
986
 * Put one packet in an application-supplied AVPacket struct.
987
 * Returns 0 on success or -1 on failure.
988
 */
989

    
990
static int
991
matroska_deliver_packet (MatroskaDemuxContext *matroska,
992
                         AVPacket             *pkt)
993
{
994
    if (matroska->num_packets > 0) {
995
        memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
996
        av_free(matroska->packets[0]);
997
        if (matroska->num_packets > 1) {
998
            memmove(&matroska->packets[0], &matroska->packets[1],
999
                    (matroska->num_packets - 1) * sizeof(AVPacket *));
1000
            matroska->packets =
1001
                av_realloc(matroska->packets, (matroska->num_packets - 1) *
1002
                           sizeof(AVPacket *));
1003
        } else {
1004
            av_freep(&matroska->packets);
1005
        }
1006
        matroska->num_packets--;
1007
        return 0;
1008
    }
1009

    
1010
    return -1;
1011
}
1012

    
1013
/*
1014
 * Put a packet into our internal queue. Will be delivered to the
1015
 * user/application during the next get_packet() call.
1016
 */
1017

    
1018
static void
1019
matroska_queue_packet (MatroskaDemuxContext *matroska,
1020
                       AVPacket             *pkt)
1021
{
1022
    matroska->packets =
1023
        av_realloc(matroska->packets, (matroska->num_packets + 1) *
1024
                   sizeof(AVPacket *));
1025
    matroska->packets[matroska->num_packets] = pkt;
1026
    matroska->num_packets++;
1027
}
1028

    
1029
/*
1030
 * Put a packet into our internal reordering queue. Will be moved to the
1031
 * main packet queue when enough packets are available to reorder pts.
1032
 */
1033

    
1034
static void
1035
matroska_queue_packet_reordered (MatroskaDemuxContext *matroska,
1036
                                 AVPacket             *pkt,
1037
                                 int                   is_bframe)
1038
{
1039
    if (matroska->num_packets_reorder && !is_bframe
1040
        && pkt->pts > matroska->reorder_max_pts) {
1041
        /* reorder pts */
1042
        int i, j, k = 1;
1043
        for (j=matroska->num_packets_reorder-1; j && k; j--) {
1044
            k = 0;
1045
            for (i=0; i<j; i++) {
1046
                if (matroska->packets_reorder[i]->pts > matroska->packets_reorder[i+1]->pts) {
1047
                    FFSWAP(uint64_t, matroska->packets_reorder[i]->pts, matroska->packets_reorder[i+1]->pts);
1048
                    k = 1;
1049
                }
1050
            }
1051
        }
1052
        /* then really queue the packets */
1053
        for (i=0; i<matroska->num_packets_reorder; i++)
1054
            matroska_queue_packet (matroska, matroska->packets_reorder[i]);
1055
        matroska->num_packets_reorder = 0;
1056
    }
1057
    matroska->packets_reorder =
1058
        av_realloc(matroska->packets_reorder,
1059
                   (matroska->num_packets_reorder + 1) * sizeof(AVPacket *));
1060
    matroska->packets_reorder[matroska->num_packets_reorder++] = pkt;
1061
    if (pkt->pts > matroska->reorder_max_pts)
1062
        matroska->reorder_max_pts = pkt->pts;
1063
}
1064

    
1065

    
1066
/*
1067
 * Autodetecting...
1068
 */
1069

    
1070
static int
1071
matroska_probe (AVProbeData *p)
1072
{
1073
    uint64_t total = 0;
1074
    int len_mask = 0x80, size = 1, n = 1;
1075
    uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
1076

    
1077
    if (p->buf_size < 5)
1078
        return 0;
1079

    
1080
    /* ebml header? */
1081
    if ((p->buf[0] << 24 | p->buf[1] << 16 |
1082
         p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
1083
        return 0;
1084

    
1085
    /* length of header */
1086
    total = p->buf[4];
1087
    while (size <= 8 && !(total & len_mask)) {
1088
        size++;
1089
        len_mask >>= 1;
1090
    }
1091
    if (size > 8)
1092
      return 0;
1093
    total &= (len_mask - 1);
1094
    while (n < size)
1095
        total = (total << 8) | p->buf[4 + n++];
1096

    
1097
    /* does the probe data contain the whole header? */
1098
    if (p->buf_size < 4 + size + total)
1099
      return 0;
1100

    
1101
    /* the header must contain the document type 'matroska'. For now,
1102
     * we don't parse the whole header but simply check for the
1103
     * availability of that array of characters inside the header.
1104
     * Not fully fool-proof, but good enough. */
1105
    for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
1106
        if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
1107
            return AVPROBE_SCORE_MAX;
1108

    
1109
    return 0;
1110
}
1111

    
1112
/*
1113
 * From here on, it's all XML-style DTD stuff... Needs no comments.
1114
 */
1115

    
1116
static int
1117
matroska_parse_info (MatroskaDemuxContext *matroska)
1118
{
1119
    int res = 0;
1120
    uint32_t id;
1121

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

    
1124
    while (res == 0) {
1125
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1126
            res = AVERROR_IO;
1127
            break;
1128
        } else if (matroska->level_up) {
1129
            matroska->level_up--;
1130
            break;
1131
        }
1132

    
1133
        switch (id) {
1134
            /* cluster timecode */
1135
            case MATROSKA_ID_TIMECODESCALE: {
1136
                uint64_t num;
1137
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1138
                    break;
1139
                matroska->time_scale = num;
1140
                break;
1141
            }
1142

    
1143
            case MATROSKA_ID_DURATION: {
1144
                double num;
1145
                if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1146
                    break;
1147
                matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
1148
                break;
1149
            }
1150

    
1151
            case MATROSKA_ID_TITLE: {
1152
                char *text;
1153
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1154
                    break;
1155
                strncpy(matroska->ctx->title, text,
1156
                        sizeof(matroska->ctx->title)-1);
1157
                av_free(text);
1158
                break;
1159
            }
1160

    
1161
            case MATROSKA_ID_WRITINGAPP: {
1162
                char *text;
1163
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1164
                    break;
1165
                matroska->writing_app = text;
1166
                break;
1167
            }
1168

    
1169
            case MATROSKA_ID_MUXINGAPP: {
1170
                char *text;
1171
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1172
                    break;
1173
                matroska->muxing_app = text;
1174
                break;
1175
            }
1176

    
1177
            case MATROSKA_ID_DATEUTC: {
1178
                int64_t time;
1179
                if ((res = ebml_read_date(matroska, &id, &time)) < 0)
1180
                    break;
1181
                matroska->created = time;
1182
                break;
1183
            }
1184

    
1185
            default:
1186
                av_log(matroska->ctx, AV_LOG_INFO,
1187
                       "Unknown entry 0x%x in info header\n", id);
1188
                /* fall-through */
1189

    
1190
            case EBML_ID_VOID:
1191
                res = ebml_read_skip(matroska);
1192
                break;
1193
        }
1194

    
1195
        if (matroska->level_up) {
1196
            matroska->level_up--;
1197
            break;
1198
        }
1199
    }
1200

    
1201
    return res;
1202
}
1203

    
1204
static int
1205
matroska_add_stream (MatroskaDemuxContext *matroska)
1206
{
1207
    int res = 0;
1208
    uint32_t id;
1209
    MatroskaTrack *track;
1210

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

    
1213
    /* Allocate a generic track. As soon as we know its type we'll realloc. */
1214
    track = av_mallocz(MAX_TRACK_SIZE);
1215
    matroska->num_tracks++;
1216

    
1217
    /* start with the master */
1218
    if ((res = ebml_read_master(matroska, &id)) < 0)
1219
        return res;
1220

    
1221
    /* try reading the trackentry headers */
1222
    while (res == 0) {
1223
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1224
            res = AVERROR_IO;
1225
            break;
1226
        } else if (matroska->level_up > 0) {
1227
            matroska->level_up--;
1228
            break;
1229
        }
1230

    
1231
        switch (id) {
1232
            /* track number (unique stream ID) */
1233
            case MATROSKA_ID_TRACKNUMBER: {
1234
                uint64_t num;
1235
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1236
                    break;
1237
                track->num = num;
1238
                break;
1239
            }
1240

    
1241
            /* track UID (unique identifier) */
1242
            case MATROSKA_ID_TRACKUID: {
1243
                uint64_t num;
1244
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1245
                    break;
1246
                track->uid = num;
1247
                break;
1248
            }
1249

    
1250
            /* track type (video, audio, combined, subtitle, etc.) */
1251
            case MATROSKA_ID_TRACKTYPE: {
1252
                uint64_t num;
1253
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1254
                    break;
1255
                if (track->type && track->type != num) {
1256
                    av_log(matroska->ctx, AV_LOG_INFO,
1257
                           "More than one tracktype in an entry - skip\n");
1258
                    break;
1259
                }
1260
                track->type = num;
1261

    
1262
                switch (track->type) {
1263
                    case MATROSKA_TRACK_TYPE_VIDEO:
1264
                    case MATROSKA_TRACK_TYPE_AUDIO:
1265
                    case MATROSKA_TRACK_TYPE_SUBTITLE:
1266
                        break;
1267
                    case MATROSKA_TRACK_TYPE_COMPLEX:
1268
                    case MATROSKA_TRACK_TYPE_LOGO:
1269
                    case MATROSKA_TRACK_TYPE_CONTROL:
1270
                    default:
1271
                        av_log(matroska->ctx, AV_LOG_INFO,
1272
                               "Unknown or unsupported track type 0x%x\n",
1273
                               track->type);
1274
                        track->type = 0;
1275
                        break;
1276
                }
1277
                matroska->tracks[matroska->num_tracks - 1] = track;
1278
                break;
1279
            }
1280

    
1281
            /* tracktype specific stuff for video */
1282
            case MATROSKA_ID_TRACKVIDEO: {
1283
                MatroskaVideoTrack *videotrack;
1284
                if (!track->type)
1285
                    track->type = MATROSKA_TRACK_TYPE_VIDEO;
1286
                if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1287
                    av_log(matroska->ctx, AV_LOG_INFO,
1288
                           "video data in non-video track - ignoring\n");
1289
                    res = AVERROR_INVALIDDATA;
1290
                    break;
1291
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1292
                    break;
1293
                videotrack = (MatroskaVideoTrack *)track;
1294

    
1295
                while (res == 0) {
1296
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1297
                        res = AVERROR_IO;
1298
                        break;
1299
                    } else if (matroska->level_up > 0) {
1300
                        matroska->level_up--;
1301
                        break;
1302
                    }
1303

    
1304
                    switch (id) {
1305
                        /* fixme, this should be one-up, but I get it here */
1306
                        case MATROSKA_ID_TRACKDEFAULTDURATION: {
1307
                            uint64_t num;
1308
                            if ((res = ebml_read_uint (matroska, &id,
1309
                                                       &num)) < 0)
1310
                                break;
1311
                            track->default_duration = num/matroska->time_scale;
1312
                            break;
1313
                        }
1314

    
1315
                        /* video framerate */
1316
                        case MATROSKA_ID_VIDEOFRAMERATE: {
1317
                            double num;
1318
                            if ((res = ebml_read_float(matroska, &id,
1319
                                                       &num)) < 0)
1320
                                break;
1321
                            track->default_duration = 1000000000/(matroska->time_scale*num);
1322
                            break;
1323
                        }
1324

    
1325
                        /* width of the size to display the video at */
1326
                        case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1327
                            uint64_t num;
1328
                            if ((res = ebml_read_uint(matroska, &id,
1329
                                                      &num)) < 0)
1330
                                break;
1331
                            videotrack->display_width = num;
1332
                            break;
1333
                        }
1334

    
1335
                        /* height of the size to display the video at */
1336
                        case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1337
                            uint64_t num;
1338
                            if ((res = ebml_read_uint(matroska, &id,
1339
                                                      &num)) < 0)
1340
                                break;
1341
                            videotrack->display_height = num;
1342
                            break;
1343
                        }
1344

    
1345
                        /* width of the video in the file */
1346
                        case MATROSKA_ID_VIDEOPIXELWIDTH: {
1347
                            uint64_t num;
1348
                            if ((res = ebml_read_uint(matroska, &id,
1349
                                                      &num)) < 0)
1350
                                break;
1351
                            videotrack->pixel_width = num;
1352
                            break;
1353
                        }
1354

    
1355
                        /* height of the video in the file */
1356
                        case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1357
                            uint64_t num;
1358
                            if ((res = ebml_read_uint(matroska, &id,
1359
                                                      &num)) < 0)
1360
                                break;
1361
                            videotrack->pixel_height = num;
1362
                            break;
1363
                        }
1364

    
1365
                        /* whether the video is interlaced */
1366
                        case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1367
                            uint64_t num;
1368
                            if ((res = ebml_read_uint(matroska, &id,
1369
                                                      &num)) < 0)
1370
                                break;
1371
                            if (num)
1372
                                track->flags |=
1373
                                    MATROSKA_VIDEOTRACK_INTERLACED;
1374
                            else
1375
                                track->flags &=
1376
                                    ~MATROSKA_VIDEOTRACK_INTERLACED;
1377
                            break;
1378
                        }
1379

    
1380
                        /* stereo mode (whether the video has two streams,
1381
                         * where one is for the left eye and the other for
1382
                         * the right eye, which creates a 3D-like
1383
                         * effect) */
1384
                        case MATROSKA_ID_VIDEOSTEREOMODE: {
1385
                            uint64_t num;
1386
                            if ((res = ebml_read_uint(matroska, &id,
1387
                                                      &num)) < 0)
1388
                                break;
1389
                            if (num != MATROSKA_EYE_MODE_MONO &&
1390
                                num != MATROSKA_EYE_MODE_LEFT &&
1391
                                num != MATROSKA_EYE_MODE_RIGHT &&
1392
                                num != MATROSKA_EYE_MODE_BOTH) {
1393
                                av_log(matroska->ctx, AV_LOG_INFO,
1394
                                       "Ignoring unknown eye mode 0x%x\n",
1395
                                       (uint32_t) num);
1396
                                break;
1397
                            }
1398
                            videotrack->eye_mode = num;
1399
                            break;
1400
                        }
1401

    
1402
                        /* aspect ratio behaviour */
1403
                        case MATROSKA_ID_VIDEOASPECTRATIO: {
1404
                            uint64_t num;
1405
                            if ((res = ebml_read_uint(matroska, &id,
1406
                                                      &num)) < 0)
1407
                                break;
1408
                            if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1409
                                num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1410
                                num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1411
                                av_log(matroska->ctx, AV_LOG_INFO,
1412
                                       "Ignoring unknown aspect ratio 0x%x\n",
1413
                                       (uint32_t) num);
1414
                                break;
1415
                            }
1416
                            videotrack->ar_mode = num;
1417
                            break;
1418
                        }
1419

    
1420
                        /* colourspace (only matters for raw video)
1421
                         * fourcc */
1422
                        case MATROSKA_ID_VIDEOCOLOURSPACE: {
1423
                            uint64_t num;
1424
                            if ((res = ebml_read_uint(matroska, &id,
1425
                                                      &num)) < 0)
1426
                                break;
1427
                            videotrack->fourcc = num;
1428
                            break;
1429
                        }
1430

    
1431
                        default:
1432
                            av_log(matroska->ctx, AV_LOG_INFO,
1433
                                   "Unknown video track header entry "
1434
                                   "0x%x - ignoring\n", id);
1435
                            /* pass-through */
1436

    
1437
                        case EBML_ID_VOID:
1438
                            res = ebml_read_skip(matroska);
1439
                            break;
1440
                    }
1441

    
1442
                    if (matroska->level_up) {
1443
                        matroska->level_up--;
1444
                        break;
1445
                    }
1446
                }
1447
                break;
1448
            }
1449

    
1450
            /* tracktype specific stuff for audio */
1451
            case MATROSKA_ID_TRACKAUDIO: {
1452
                MatroskaAudioTrack *audiotrack;
1453
                if (!track->type)
1454
                    track->type = MATROSKA_TRACK_TYPE_AUDIO;
1455
                if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1456
                    av_log(matroska->ctx, AV_LOG_INFO,
1457
                           "audio data in non-audio track - ignoring\n");
1458
                    res = AVERROR_INVALIDDATA;
1459
                    break;
1460
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1461
                    break;
1462
                audiotrack = (MatroskaAudioTrack *)track;
1463
                audiotrack->channels = 1;
1464
                audiotrack->samplerate = 8000;
1465

    
1466
                while (res == 0) {
1467
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1468
                        res = AVERROR_IO;
1469
                        break;
1470
                    } else if (matroska->level_up > 0) {
1471
                        matroska->level_up--;
1472
                        break;
1473
                    }
1474

    
1475
                    switch (id) {
1476
                        /* samplerate */
1477
                        case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1478
                            double num;
1479
                            if ((res = ebml_read_float(matroska, &id,
1480
                                                       &num)) < 0)
1481
                                break;
1482
                            audiotrack->internal_samplerate =
1483
                            audiotrack->samplerate = num;
1484
                            break;
1485
                        }
1486

    
1487
                        case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1488
                            double num;
1489
                            if ((res = ebml_read_float(matroska, &id,
1490
                                                       &num)) < 0)
1491
                                break;
1492
                            audiotrack->samplerate = num;
1493
                            break;
1494
                        }
1495

    
1496
                            /* bitdepth */
1497
                        case MATROSKA_ID_AUDIOBITDEPTH: {
1498
                            uint64_t num;
1499
                            if ((res = ebml_read_uint(matroska, &id,
1500
                                                      &num)) < 0)
1501
                                break;
1502
                            audiotrack->bitdepth = num;
1503
                            break;
1504
                        }
1505

    
1506
                            /* channels */
1507
                        case MATROSKA_ID_AUDIOCHANNELS: {
1508
                            uint64_t num;
1509
                            if ((res = ebml_read_uint(matroska, &id,
1510
                                                      &num)) < 0)
1511
                                break;
1512
                            audiotrack->channels = num;
1513
                            break;
1514
                        }
1515

    
1516
                        default:
1517
                            av_log(matroska->ctx, AV_LOG_INFO,
1518
                                   "Unknown audio track header entry "
1519
                                   "0x%x - ignoring\n", id);
1520
                            /* pass-through */
1521

    
1522
                        case EBML_ID_VOID:
1523
                            res = ebml_read_skip(matroska);
1524
                            break;
1525
                    }
1526

    
1527
                    if (matroska->level_up) {
1528
                        matroska->level_up--;
1529
                        break;
1530
                    }
1531
                }
1532
                break;
1533
            }
1534

    
1535
                /* codec identifier */
1536
            case MATROSKA_ID_CODECID: {
1537
                char *text;
1538
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1539
                    break;
1540
                track->codec_id = text;
1541
                break;
1542
            }
1543

    
1544
                /* codec private data */
1545
            case MATROSKA_ID_CODECPRIVATE: {
1546
                uint8_t *data;
1547
                int size;
1548
                if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1549
                    break;
1550
                track->codec_priv = data;
1551
                track->codec_priv_size = size;
1552
                break;
1553
            }
1554

    
1555
                /* name of the codec */
1556
            case MATROSKA_ID_CODECNAME: {
1557
                char *text;
1558
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1559
                    break;
1560
                track->codec_name = text;
1561
                break;
1562
            }
1563

    
1564
                /* name of this track */
1565
            case MATROSKA_ID_TRACKNAME: {
1566
                char *text;
1567
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1568
                    break;
1569
                track->name = text;
1570
                break;
1571
            }
1572

    
1573
                /* language (matters for audio/subtitles, mostly) */
1574
            case MATROSKA_ID_TRACKLANGUAGE: {
1575
                char *text;
1576
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1577
                    break;
1578
                track->language = text;
1579
                break;
1580
            }
1581

    
1582
                /* whether this is actually used */
1583
            case MATROSKA_ID_TRACKFLAGENABLED: {
1584
                uint64_t num;
1585
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1586
                    break;
1587
                if (num)
1588
                    track->flags |= MATROSKA_TRACK_ENABLED;
1589
                else
1590
                    track->flags &= ~MATROSKA_TRACK_ENABLED;
1591
                break;
1592
            }
1593

    
1594
                /* whether it's the default for this track type */
1595
            case MATROSKA_ID_TRACKFLAGDEFAULT: {
1596
                uint64_t num;
1597
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1598
                    break;
1599
                if (num)
1600
                    track->flags |= MATROSKA_TRACK_DEFAULT;
1601
                else
1602
                    track->flags &= ~MATROSKA_TRACK_DEFAULT;
1603
                break;
1604
            }
1605

    
1606
                /* lacing (like MPEG, where blocks don't end/start on frame
1607
                 * boundaries) */
1608
            case MATROSKA_ID_TRACKFLAGLACING: {
1609
                uint64_t num;
1610
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1611
                    break;
1612
                if (num)
1613
                    track->flags |= MATROSKA_TRACK_LACING;
1614
                else
1615
                    track->flags &= ~MATROSKA_TRACK_LACING;
1616
                break;
1617
            }
1618

    
1619
                /* default length (in time) of one data block in this track */
1620
            case MATROSKA_ID_TRACKDEFAULTDURATION: {
1621
                uint64_t num;
1622
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1623
                    break;
1624
                track->default_duration = num / matroska->time_scale;
1625
                break;
1626
            }
1627

    
1628
            default:
1629
                av_log(matroska->ctx, AV_LOG_INFO,
1630
                       "Unknown track header entry 0x%x - ignoring\n", id);
1631
                /* pass-through */
1632

    
1633
            case EBML_ID_VOID:
1634
            /* we ignore these because they're nothing useful. */
1635
            case MATROSKA_ID_CODECINFOURL:
1636
            case MATROSKA_ID_CODECDOWNLOADURL:
1637
            case MATROSKA_ID_TRACKMINCACHE:
1638
            case MATROSKA_ID_TRACKMAXCACHE:
1639
                res = ebml_read_skip(matroska);
1640
                break;
1641
        }
1642

    
1643
        if (matroska->level_up) {
1644
            matroska->level_up--;
1645
            break;
1646
        }
1647
    }
1648

    
1649
    return res;
1650
}
1651

    
1652
static int
1653
matroska_parse_tracks (MatroskaDemuxContext *matroska)
1654
{
1655
    int res = 0;
1656
    uint32_t id;
1657

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

    
1660
    while (res == 0) {
1661
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1662
            res = AVERROR_IO;
1663
            break;
1664
        } else if (matroska->level_up) {
1665
            matroska->level_up--;
1666
            break;
1667
        }
1668

    
1669
        switch (id) {
1670
            /* one track within the "all-tracks" header */
1671
            case MATROSKA_ID_TRACKENTRY:
1672
                res = matroska_add_stream(matroska);
1673
                break;
1674

    
1675
            default:
1676
                av_log(matroska->ctx, AV_LOG_INFO,
1677
                       "Unknown entry 0x%x in track header\n", id);
1678
                /* fall-through */
1679

    
1680
            case EBML_ID_VOID:
1681
                res = ebml_read_skip(matroska);
1682
                break;
1683
        }
1684

    
1685
        if (matroska->level_up) {
1686
            matroska->level_up--;
1687
            break;
1688
        }
1689
    }
1690

    
1691
    return res;
1692
}
1693

    
1694
static int
1695
matroska_parse_index (MatroskaDemuxContext *matroska)
1696
{
1697
    int res = 0;
1698
    uint32_t id;
1699
    MatroskaDemuxIndex idx;
1700

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

    
1703
    while (res == 0) {
1704
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1705
            res = AVERROR_IO;
1706
            break;
1707
        } else if (matroska->level_up) {
1708
            matroska->level_up--;
1709
            break;
1710
        }
1711

    
1712
        switch (id) {
1713
            /* one single index entry ('point') */
1714
            case MATROSKA_ID_POINTENTRY:
1715
                if ((res = ebml_read_master(matroska, &id)) < 0)
1716
                    break;
1717

    
1718
                /* in the end, we hope to fill one entry with a
1719
                 * timestamp, a file position and a tracknum */
1720
                idx.pos   = (uint64_t) -1;
1721
                idx.time  = (uint64_t) -1;
1722
                idx.track = (uint16_t) -1;
1723

    
1724
                while (res == 0) {
1725
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1726
                        res = AVERROR_IO;
1727
                        break;
1728
                    } else if (matroska->level_up) {
1729
                        matroska->level_up--;
1730
                        break;
1731
                    }
1732

    
1733
                    switch (id) {
1734
                        /* one single index entry ('point') */
1735
                        case MATROSKA_ID_CUETIME: {
1736
                            uint64_t time;
1737
                            if ((res = ebml_read_uint(matroska, &id,
1738
                                                      &time)) < 0)
1739
                                break;
1740
                            idx.time = time * matroska->time_scale;
1741
                            break;
1742
                        }
1743

    
1744
                        /* position in the file + track to which it
1745
                         * belongs */
1746
                        case MATROSKA_ID_CUETRACKPOSITION:
1747
                            if ((res = ebml_read_master(matroska, &id)) < 0)
1748
                                break;
1749

    
1750
                            while (res == 0) {
1751
                                if (!(id = ebml_peek_id (matroska,
1752
                                                    &matroska->level_up))) {
1753
                                    res = AVERROR_IO;
1754
                                    break;
1755
                                } else if (matroska->level_up) {
1756
                                    matroska->level_up--;
1757
                                    break;
1758
                                }
1759

    
1760
                                switch (id) {
1761
                                    /* track number */
1762
                                    case MATROSKA_ID_CUETRACK: {
1763
                                        uint64_t num;
1764
                                        if ((res = ebml_read_uint(matroska,
1765
                                                          &id, &num)) < 0)
1766
                                            break;
1767
                                        idx.track = num;
1768
                                        break;
1769
                                    }
1770

    
1771
                                        /* position in file */
1772
                                    case MATROSKA_ID_CUECLUSTERPOSITION: {
1773
                                        uint64_t num;
1774
                                        if ((res = ebml_read_uint(matroska,
1775
                                                          &id, &num)) < 0)
1776
                                            break;
1777
                                        idx.pos = num;
1778
                                        break;
1779
                                    }
1780

    
1781
                                    default:
1782
                                        av_log(matroska->ctx, AV_LOG_INFO,
1783
                                               "Unknown entry 0x%x in "
1784
                                               "CuesTrackPositions\n", id);
1785
                                        /* fall-through */
1786

    
1787
                                    case EBML_ID_VOID:
1788
                                        res = ebml_read_skip(matroska);
1789
                                        break;
1790
                                }
1791

    
1792
                                if (matroska->level_up) {
1793
                                    matroska->level_up--;
1794
                                    break;
1795
                                }
1796
                            }
1797

    
1798
                            break;
1799

    
1800
                        default:
1801
                            av_log(matroska->ctx, AV_LOG_INFO,
1802
                                   "Unknown entry 0x%x in cuespoint "
1803
                                   "index\n", id);
1804
                            /* fall-through */
1805

    
1806
                        case EBML_ID_VOID:
1807
                            res = ebml_read_skip(matroska);
1808
                            break;
1809
                    }
1810

    
1811
                    if (matroska->level_up) {
1812
                        matroska->level_up--;
1813
                        break;
1814
                    }
1815
                }
1816

    
1817
                /* so let's see if we got what we wanted */
1818
                if (idx.pos   != (uint64_t) -1 &&
1819
                    idx.time  != (uint64_t) -1 &&
1820
                    idx.track != (uint16_t) -1) {
1821
                    if (matroska->num_indexes % 32 == 0) {
1822
                        /* re-allocate bigger index */
1823
                        matroska->index =
1824
                            av_realloc(matroska->index,
1825
                                       (matroska->num_indexes + 32) *
1826
                                       sizeof(MatroskaDemuxIndex));
1827
                    }
1828
                    matroska->index[matroska->num_indexes] = idx;
1829
                    matroska->num_indexes++;
1830
                }
1831
                break;
1832

    
1833
            default:
1834
                av_log(matroska->ctx, AV_LOG_INFO,
1835
                       "Unknown entry 0x%x in cues header\n", id);
1836
                /* fall-through */
1837

    
1838
            case EBML_ID_VOID:
1839
                res = ebml_read_skip(matroska);
1840
                break;
1841
        }
1842

    
1843
        if (matroska->level_up) {
1844
            matroska->level_up--;
1845
            break;
1846
        }
1847
    }
1848

    
1849
    return res;
1850
}
1851

    
1852
static int
1853
matroska_parse_metadata (MatroskaDemuxContext *matroska)
1854
{
1855
    int res = 0;
1856
    uint32_t id;
1857

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

    
1867
        switch (id) {
1868
            /* Hm, this is unsupported... */
1869
            default:
1870
                av_log(matroska->ctx, AV_LOG_INFO,
1871
                       "Unknown entry 0x%x in metadata header\n", id);
1872
                /* fall-through */
1873

    
1874
            case EBML_ID_VOID:
1875
                res = ebml_read_skip(matroska);
1876
                break;
1877
        }
1878

    
1879
        if (matroska->level_up) {
1880
            matroska->level_up--;
1881
            break;
1882
        }
1883
    }
1884

    
1885
    return res;
1886
}
1887

    
1888
static int
1889
matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1890
{
1891
    int res = 0;
1892
    uint32_t id;
1893

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

    
1896
    while (res == 0) {
1897
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1898
            res = AVERROR_IO;
1899
            break;
1900
        } else if (matroska->level_up) {
1901
            matroska->level_up--;
1902
            break;
1903
        }
1904

    
1905
        switch (id) {
1906
            case MATROSKA_ID_SEEKENTRY: {
1907
                uint32_t seek_id = 0, peek_id_cache = 0;
1908
                uint64_t seek_pos = (uint64_t) -1, t;
1909

    
1910
                if ((res = ebml_read_master(matroska, &id)) < 0)
1911
                    break;
1912

    
1913
                while (res == 0) {
1914
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1915
                        res = AVERROR_IO;
1916
                        break;
1917
                    } else if (matroska->level_up) {
1918
                        matroska->level_up--;
1919
                        break;
1920
                    }
1921

    
1922
                    switch (id) {
1923
                        case MATROSKA_ID_SEEKID:
1924
                            res = ebml_read_uint(matroska, &id, &t);
1925
                            seek_id = t;
1926
                            break;
1927

    
1928
                        case MATROSKA_ID_SEEKPOSITION:
1929
                            res = ebml_read_uint(matroska, &id, &seek_pos);
1930
                            break;
1931

    
1932
                        default:
1933
                            av_log(matroska->ctx, AV_LOG_INFO,
1934
                                   "Unknown seekhead ID 0x%x\n", id);
1935
                            /* fall-through */
1936

    
1937
                        case EBML_ID_VOID:
1938
                            res = ebml_read_skip(matroska);
1939
                            break;
1940
                    }
1941

    
1942
                    if (matroska->level_up) {
1943
                        matroska->level_up--;
1944
                        break;
1945
                    }
1946
                }
1947

    
1948
                if (!seek_id || seek_pos == (uint64_t) -1) {
1949
                    av_log(matroska->ctx, AV_LOG_INFO,
1950
                           "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1951
                           seek_id, seek_pos);
1952
                    break;
1953
                }
1954

    
1955
                switch (seek_id) {
1956
                    case MATROSKA_ID_CUES:
1957
                    case MATROSKA_ID_TAGS: {
1958
                        uint32_t level_up = matroska->level_up;
1959
                        offset_t before_pos;
1960
                        uint64_t length;
1961
                        MatroskaLevel level;
1962

    
1963
                        /* remember the peeked ID and the current position */
1964
                        peek_id_cache = matroska->peek_id;
1965
                        before_pos = url_ftell(&matroska->ctx->pb);
1966

    
1967
                        /* seek */
1968
                        if ((res = ebml_read_seek(matroska, seek_pos +
1969
                                               matroska->segment_start)) < 0)
1970
                            return res;
1971

    
1972
                        /* we don't want to lose our seekhead level, so we add
1973
                         * a dummy. This is a crude hack. */
1974
                        if (matroska->num_levels == EBML_MAX_DEPTH) {
1975
                            av_log(matroska->ctx, AV_LOG_INFO,
1976
                                   "Max EBML element depth (%d) reached, "
1977
                                   "cannot parse further.\n", EBML_MAX_DEPTH);
1978
                            return AVERROR_UNKNOWN;
1979
                        }
1980

    
1981
                        level.start = 0;
1982
                        level.length = (uint64_t)-1;
1983
                        matroska->levels[matroska->num_levels] = level;
1984
                        matroska->num_levels++;
1985

    
1986
                        /* check ID */
1987
                        if (!(id = ebml_peek_id (matroska,
1988
                                                 &matroska->level_up)))
1989
                            goto finish;
1990
                        if (id != seek_id) {
1991
                            av_log(matroska->ctx, AV_LOG_INFO,
1992
                                   "We looked for ID=0x%x but got "
1993
                                   "ID=0x%x (pos=%"PRIu64")",
1994
                                   seek_id, id, seek_pos +
1995
                                   matroska->segment_start);
1996
                            goto finish;
1997
                        }
1998

    
1999
                        /* read master + parse */
2000
                        if ((res = ebml_read_master(matroska, &id)) < 0)
2001
                            goto finish;
2002
                        switch (id) {
2003
                            case MATROSKA_ID_CUES:
2004
                                if (!(res = matroska_parse_index(matroska)) ||
2005
                                    url_feof(&matroska->ctx->pb)) {
2006
                                    matroska->index_parsed = 1;
2007
                                    res = 0;
2008
                                }
2009
                                break;
2010
                            case MATROSKA_ID_TAGS:
2011
                                if (!(res = matroska_parse_metadata(matroska)) ||
2012
                                   url_feof(&matroska->ctx->pb)) {
2013
                                    matroska->metadata_parsed = 1;
2014
                                    res = 0;
2015
                                }
2016
                                break;
2017
                        }
2018

    
2019
                    finish:
2020
                        /* remove dummy level */
2021
                        while (matroska->num_levels) {
2022
                            matroska->num_levels--;
2023
                            length =
2024
                                matroska->levels[matroska->num_levels].length;
2025
                            if (length == (uint64_t)-1)
2026
                                break;
2027
                        }
2028

    
2029
                        /* seek back */
2030
                        if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2031
                            return res;
2032
                        matroska->peek_id = peek_id_cache;
2033
                        matroska->level_up = level_up;
2034
                        break;
2035
                    }
2036

    
2037
                    default:
2038
                        av_log(matroska->ctx, AV_LOG_INFO,
2039
                               "Ignoring seekhead entry for ID=0x%x\n",
2040
                               seek_id);
2041
                        break;
2042
                }
2043

    
2044
                break;
2045
            }
2046

    
2047
            default:
2048
                av_log(matroska->ctx, AV_LOG_INFO,
2049
                       "Unknown seekhead ID 0x%x\n", id);
2050
                /* fall-through */
2051

    
2052
            case EBML_ID_VOID:
2053
                res = ebml_read_skip(matroska);
2054
                break;
2055
        }
2056

    
2057
        if (matroska->level_up) {
2058
            matroska->level_up--;
2059
            break;
2060
        }
2061
    }
2062

    
2063
    return res;
2064
}
2065

    
2066
#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
2067

    
2068
static int
2069
matroska_aac_profile (char *codec_id)
2070
{
2071
    static const char *aac_profiles[] = {
2072
        "MAIN", "LC", "SSR"
2073
    };
2074
    int profile;
2075

    
2076
    for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2077
        if (strstr(codec_id, aac_profiles[profile]))
2078
            break;
2079
    return profile + 1;
2080
}
2081

    
2082
static int
2083
matroska_aac_sri (int samplerate)
2084
{
2085
    static const int aac_sample_rates[] = {
2086
        96000, 88200, 64000, 48000, 44100, 32000,
2087
        24000, 22050, 16000, 12000, 11025,  8000,
2088
    };
2089
    int sri;
2090

    
2091
    for (sri=0; sri<ARRAY_SIZE(aac_sample_rates); sri++)
2092
        if (aac_sample_rates[sri] == samplerate)
2093
            break;
2094
    return sri;
2095
}
2096

    
2097
static int
2098
matroska_read_header (AVFormatContext    *s,
2099
                      AVFormatParameters *ap)
2100
{
2101
    MatroskaDemuxContext *matroska = s->priv_data;
2102
    char *doctype;
2103
    int version, last_level, res = 0;
2104
    uint32_t id;
2105

    
2106
    matroska->ctx = s;
2107

    
2108
    /* First read the EBML header. */
2109
    doctype = NULL;
2110
    if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2111
        return res;
2112
    if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2113
        av_log(matroska->ctx, AV_LOG_ERROR,
2114
               "Wrong EBML doctype ('%s' != 'matroska').\n",
2115
               doctype ? doctype : "(none)");
2116
        if (doctype)
2117
            av_free(doctype);
2118
        return AVERROR_NOFMT;
2119
    }
2120
    av_free(doctype);
2121
    if (version > 2) {
2122
        av_log(matroska->ctx, AV_LOG_ERROR,
2123
               "Matroska demuxer version 2 too old for file version %d\n",
2124
               version);
2125
        return AVERROR_NOFMT;
2126
    }
2127

    
2128
    /* The next thing is a segment. */
2129
    while (1) {
2130
        if (!(id = ebml_peek_id(matroska, &last_level)))
2131
            return AVERROR_IO;
2132
        if (id == MATROSKA_ID_SEGMENT)
2133
            break;
2134

    
2135
        /* oi! */
2136
        av_log(matroska->ctx, AV_LOG_INFO,
2137
               "Expected a Segment ID (0x%x), but received 0x%x!\n",
2138
               MATROSKA_ID_SEGMENT, id);
2139
        if ((res = ebml_read_skip(matroska)) < 0)
2140
            return res;
2141
    }
2142

    
2143
    /* We now have a Matroska segment.
2144
     * Seeks are from the beginning of the segment,
2145
     * after the segment ID/length. */
2146
    if ((res = ebml_read_master(matroska, &id)) < 0)
2147
        return res;
2148
    matroska->segment_start = url_ftell(&s->pb);
2149

    
2150
    matroska->time_scale = 1000000;
2151
    /* we've found our segment, start reading the different contents in here */
2152
    while (res == 0) {
2153
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2154
            res = AVERROR_IO;
2155
            break;
2156
        } else if (matroska->level_up) {
2157
            matroska->level_up--;
2158
            break;
2159
        }
2160

    
2161
        switch (id) {
2162
            /* stream info */
2163
            case MATROSKA_ID_INFO: {
2164
                if ((res = ebml_read_master(matroska, &id)) < 0)
2165
                    break;
2166
                res = matroska_parse_info(matroska);
2167
                break;
2168
            }
2169

    
2170
            /* track info headers */
2171
            case MATROSKA_ID_TRACKS: {
2172
                if ((res = ebml_read_master(matroska, &id)) < 0)
2173
                    break;
2174
                res = matroska_parse_tracks(matroska);
2175
                break;
2176
            }
2177

    
2178
            /* stream index */
2179
            case MATROSKA_ID_CUES: {
2180
                if (!matroska->index_parsed) {
2181
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2182
                        break;
2183
                    res = matroska_parse_index(matroska);
2184
                } else
2185
                    res = ebml_read_skip(matroska);
2186
                break;
2187
            }
2188

    
2189
            /* metadata */
2190
            case MATROSKA_ID_TAGS: {
2191
                if (!matroska->metadata_parsed) {
2192
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2193
                        break;
2194
                    res = matroska_parse_metadata(matroska);
2195
                } else
2196
                    res = ebml_read_skip(matroska);
2197
                break;
2198
            }
2199

    
2200
            /* file index (if seekable, seek to Cues/Tags to parse it) */
2201
            case MATROSKA_ID_SEEKHEAD: {
2202
                if ((res = ebml_read_master(matroska, &id)) < 0)
2203
                    break;
2204
                res = matroska_parse_seekhead(matroska);
2205
                break;
2206
            }
2207

    
2208
            case MATROSKA_ID_CLUSTER: {
2209
                /* Do not read the master - this will be done in the next
2210
                 * call to matroska_read_packet. */
2211
                res = 1;
2212
                break;
2213
            }
2214

    
2215
            default:
2216
                av_log(matroska->ctx, AV_LOG_INFO,
2217
                       "Unknown matroska file header ID 0x%x\n", id);
2218
            /* fall-through */
2219

    
2220
            case EBML_ID_VOID:
2221
                res = ebml_read_skip(matroska);
2222
                break;
2223
        }
2224

    
2225
        if (matroska->level_up) {
2226
            matroska->level_up--;
2227
            break;
2228
        }
2229
    }
2230

    
2231
    /* Have we found a cluster? */
2232
    if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2233
        int i, j;
2234
        MatroskaTrack *track;
2235
        AVStream *st;
2236

    
2237
        for (i = 0; i < matroska->num_tracks; i++) {
2238
            enum CodecID codec_id = CODEC_ID_NONE;
2239
            uint8_t *extradata = NULL;
2240
            int extradata_size = 0;
2241
            int extradata_offset = 0;
2242
            track = matroska->tracks[i];
2243

    
2244
            /* libavformat does not really support subtitles.
2245
             * Also apply some sanity checks. */
2246
            if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
2247
                (track->codec_id == NULL))
2248
                continue;
2249

    
2250
            for(j=0; codec_tags[j].str; j++){
2251
                if(!strncmp(codec_tags[j].str, track->codec_id,
2252
                            strlen(codec_tags[j].str))){
2253
                    codec_id= codec_tags[j].id;
2254
                    break;
2255
                }
2256
            }
2257

    
2258
            /* Set the FourCC from the CodecID. */
2259
            /* This is the MS compatibility mode which stores a
2260
             * BITMAPINFOHEADER in the CodecPrivate. */
2261
            if (!strcmp(track->codec_id,
2262
                        MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2263
                (track->codec_priv_size >= 40) &&
2264
                (track->codec_priv != NULL)) {
2265
                unsigned char *p;
2266

    
2267
                /* Offset of biCompression. Stored in LE. */
2268
                p = (unsigned char *)track->codec_priv + 16;
2269
                ((MatroskaVideoTrack *)track)->fourcc = (p[3] << 24) |
2270
                                 (p[2] << 16) | (p[1] << 8) | p[0];
2271
                codec_id = codec_get_id(codec_bmp_tags, ((MatroskaVideoTrack *)track)->fourcc);
2272

    
2273
            }
2274

    
2275
            /* This is the MS compatibility mode which stores a
2276
             * WAVEFORMATEX in the CodecPrivate. */
2277
            else if (!strcmp(track->codec_id,
2278
                             MATROSKA_CODEC_ID_AUDIO_ACM) &&
2279
                (track->codec_priv_size >= 18) &&
2280
                (track->codec_priv != NULL)) {
2281
                unsigned char *p;
2282
                uint16_t tag;
2283

    
2284
                /* Offset of wFormatTag. Stored in LE. */
2285
                p = (unsigned char *)track->codec_priv;
2286
                tag = (p[1] << 8) | p[0];
2287
                codec_id = codec_get_id(codec_wav_tags, tag);
2288

    
2289
            }
2290

    
2291
            else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2292
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2293
                int profile = matroska_aac_profile(track->codec_id);
2294
                int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2295
                extradata = av_malloc(5);
2296
                if (extradata == NULL)
2297
                    return AVERROR_NOMEM;
2298
                extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2299
                extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2300
                if (strstr(track->codec_id, "SBR")) {
2301
                    sri = matroska_aac_sri(audiotrack->samplerate);
2302
                    extradata[2] = 0x56;
2303
                    extradata[3] = 0xE5;
2304
                    extradata[4] = 0x80 | (sri<<3);
2305
                    extradata_size = 5;
2306
                } else {
2307
                    extradata_size = 2;
2308
                }
2309
                track->default_duration = 1024*1000 / audiotrack->internal_samplerate;
2310
            }
2311

    
2312
            else if (codec_id == CODEC_ID_TTA) {
2313
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2314
                ByteIOContext b;
2315
                extradata_size = 30;
2316
                extradata = av_mallocz(extradata_size);
2317
                if (extradata == NULL)
2318
                    return AVERROR_NOMEM;
2319
                init_put_byte(&b, extradata, extradata_size, 1,
2320
                              NULL, NULL, NULL, NULL);
2321
                put_buffer(&b, (uint8_t *) "TTA1", 4);
2322
                put_le16(&b, 1);
2323
                put_le16(&b, audiotrack->channels);
2324
                put_le16(&b, audiotrack->bitdepth);
2325
                put_le32(&b, audiotrack->samplerate);
2326
                put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2327
            }
2328

    
2329
            else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2330
                     codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2331
                extradata_offset = 26;
2332
                track->codec_priv_size -= extradata_offset;
2333
                track->flags |= MATROSKA_TRACK_REAL_V;
2334
            }
2335

    
2336
            if (codec_id == CODEC_ID_NONE) {
2337
                av_log(matroska->ctx, AV_LOG_INFO,
2338
                       "Unknown/unsupported CodecID %s.\n",
2339
                       track->codec_id);
2340
            }
2341

    
2342
            track->stream_index = matroska->num_streams;
2343

    
2344
            matroska->num_streams++;
2345
            st = av_new_stream(s, track->stream_index);
2346
            if (st == NULL)
2347
                return AVERROR_NOMEM;
2348
            av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2349

    
2350
            st->codec->codec_id = codec_id;
2351

    
2352
            if (track->default_duration)
2353
                av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2354
                          track->default_duration, 1000, 30000);
2355

    
2356
            if(extradata){
2357
                st->codec->extradata = extradata;
2358
                st->codec->extradata_size = extradata_size;
2359
            } else if(track->codec_priv && track->codec_priv_size > 0){
2360
                st->codec->extradata = av_malloc(track->codec_priv_size);
2361
                if(st->codec->extradata == NULL)
2362
                    return AVERROR_NOMEM;
2363
                st->codec->extradata_size = track->codec_priv_size;
2364
                memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2365
                       track->codec_priv_size);
2366
            }
2367

    
2368
            if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2369
                MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2370

    
2371
                st->codec->codec_type = CODEC_TYPE_VIDEO;
2372
                st->codec->codec_tag = videotrack->fourcc;
2373
                st->codec->width = videotrack->pixel_width;
2374
                st->codec->height = videotrack->pixel_height;
2375
                if (videotrack->display_width == 0)
2376
                    videotrack->display_width= videotrack->pixel_width;
2377
                if (videotrack->display_height == 0)
2378
                    videotrack->display_height= videotrack->pixel_height;
2379
                av_reduce(&st->codec->sample_aspect_ratio.num,
2380
                          &st->codec->sample_aspect_ratio.den,
2381
                          st->codec->height * videotrack->display_width,
2382
                          st->codec-> width * videotrack->display_height,
2383
                          255);
2384
                st->need_parsing = 2;
2385
            } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2386
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2387

    
2388
                st->codec->codec_type = CODEC_TYPE_AUDIO;
2389
                st->codec->sample_rate = audiotrack->samplerate;
2390
                st->codec->channels = audiotrack->channels;
2391
            } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2392
                st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2393
            }
2394

    
2395
            /* What do we do with private data? E.g. for Vorbis. */
2396
        }
2397
        res = 0;
2398
    }
2399

    
2400
    return res;
2401
}
2402

    
2403
static int
2404
matroska_find_track_by_num (MatroskaDemuxContext *matroska,
2405
                            int                   num)
2406
{
2407
    int i;
2408

    
2409
    for (i = 0; i < matroska->num_tracks; i++)
2410
        if (matroska->tracks[i]->num == num)
2411
            return i;
2412

    
2413
    return -1;
2414
}
2415

    
2416
static inline int
2417
rv_offset(uint8_t *data, int slice, int slices)
2418
{
2419
    return AV_RL32(data+8*slice+4) + 8*slices;
2420
}
2421

    
2422
static int
2423
matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2424
                     int64_t pos, uint64_t cluster_time, uint64_t duration,
2425
                     int is_keyframe, int is_bframe)
2426
{
2427
    int res = 0;
2428
    int track;
2429
    AVPacket *pkt;
2430
    uint8_t *origdata = data;
2431
    int16_t block_time;
2432
    uint32_t *lace_size = NULL;
2433
    int n, flags, laces = 0;
2434
    uint64_t num;
2435

    
2436
    /* first byte(s): tracknum */
2437
    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2438
        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2439
        av_free(origdata);
2440
        return res;
2441
    }
2442
    data += n;
2443
    size -= n;
2444

    
2445
    /* fetch track from num */
2446
    track = matroska_find_track_by_num(matroska, num);
2447
    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2448
        av_log(matroska->ctx, AV_LOG_INFO,
2449
               "Invalid stream %d or size %u\n", track, size);
2450
        av_free(origdata);
2451
        return res;
2452
    }
2453
    if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
2454
        av_free(origdata);
2455
        return res;
2456
    }
2457
    if (duration == AV_NOPTS_VALUE)
2458
        duration = matroska->tracks[track]->default_duration;
2459

    
2460
    /* block_time (relative to cluster time) */
2461
    block_time = (data[0] << 8) | data[1];
2462
    data += 2;
2463
    size -= 2;
2464
    flags = *data;
2465
    data += 1;
2466
    size -= 1;
2467
    if (is_keyframe == -1)
2468
        is_keyframe = flags & 1 ? PKT_FLAG_KEY : 0;
2469
    switch ((flags & 0x06) >> 1) {
2470
        case 0x0: /* no lacing */
2471
            laces = 1;
2472
            lace_size = av_mallocz(sizeof(int));
2473
            lace_size[0] = size;
2474
            break;
2475

    
2476
        case 0x1: /* xiph lacing */
2477
        case 0x2: /* fixed-size lacing */
2478
        case 0x3: /* EBML lacing */
2479
            if (size == 0) {
2480
                res = -1;
2481
                break;
2482
            }
2483
            laces = (*data) + 1;
2484
            data += 1;
2485
            size -= 1;
2486
            lace_size = av_mallocz(laces * sizeof(int));
2487

    
2488
            switch ((flags & 0x06) >> 1) {
2489
                case 0x1: /* xiph lacing */ {
2490
                    uint8_t temp;
2491
                    uint32_t total = 0;
2492
                    for (n = 0; res == 0 && n < laces - 1; n++) {
2493
                        while (1) {
2494
                            if (size == 0) {
2495
                                res = -1;
2496
                                break;
2497
                            }
2498
                            temp = *data;
2499
                            lace_size[n] += temp;
2500
                            data += 1;
2501
                            size -= 1;
2502
                            if (temp != 0xff)
2503
                                break;
2504
                        }
2505
                        total += lace_size[n];
2506
                    }
2507
                    lace_size[n] = size - total;
2508
                    break;
2509
                }
2510

    
2511
                case 0x2: /* fixed-size lacing */
2512
                    for (n = 0; n < laces; n++)
2513
                        lace_size[n] = size / laces;
2514
                    break;
2515

    
2516
                case 0x3: /* EBML lacing */ {
2517
                    uint32_t total;
2518
                    n = matroska_ebmlnum_uint(data, size, &num);
2519
                    if (n < 0) {
2520
                        av_log(matroska->ctx, AV_LOG_INFO,
2521
                               "EBML block data error\n");
2522
                        break;
2523
                    }
2524
                    data += n;
2525
                    size -= n;
2526
                    total = lace_size[0] = num;
2527
                    for (n = 1; res == 0 && n < laces - 1; n++) {
2528
                        int64_t snum;
2529
                        int r;
2530
                        r = matroska_ebmlnum_sint (data, size, &snum);
2531
                        if (r < 0) {
2532
                            av_log(matroska->ctx, AV_LOG_INFO,
2533
                                   "EBML block data error\n");
2534
                            break;
2535
                        }
2536
                        data += r;
2537
                        size -= r;
2538
                        lace_size[n] = lace_size[n - 1] + snum;
2539
                        total += lace_size[n];
2540
                    }
2541
                    lace_size[n] = size - total;
2542
                    break;
2543
                }
2544
            }
2545
            break;
2546
    }
2547

    
2548
    if (res == 0) {
2549
        int real_v = matroska->tracks[track]->flags & MATROSKA_TRACK_REAL_V;
2550
        uint64_t timecode = AV_NOPTS_VALUE;
2551

    
2552
        if (cluster_time != (uint64_t)-1 && cluster_time + block_time >= 0)
2553
            timecode = cluster_time + block_time;
2554

    
2555
        for (n = 0; n < laces; n++) {
2556
            int slice, slices = 1;
2557

    
2558
            if (real_v) {
2559
                slices = *data++ + 1;
2560
                lace_size[n]--;
2561
            }
2562

    
2563
            for (slice=0; slice<slices; slice++) {
2564
                int slice_size, slice_offset = 0;
2565
                if (real_v)
2566
                    slice_offset = rv_offset(data, slice, slices);
2567
                if (slice+1 == slices)
2568
                    slice_size = lace_size[n] - slice_offset;
2569
                else
2570
                    slice_size = rv_offset(data, slice+1, slices) - slice_offset;
2571
                pkt = av_mallocz(sizeof(AVPacket));
2572
                /* XXX: prevent data copy... */
2573
                if (av_new_packet(pkt, slice_size) < 0) {
2574
                    res = AVERROR_NOMEM;
2575
                    n = laces-1;
2576
                    break;
2577
                }
2578
                memcpy (pkt->data, data+slice_offset, slice_size);
2579

    
2580
                if (n == 0)
2581
                    pkt->flags = is_keyframe;
2582
                pkt->stream_index = matroska->tracks[track]->stream_index;
2583

    
2584
                pkt->pts = timecode;
2585
                pkt->pos = pos;
2586
                pkt->duration = duration;
2587

    
2588
                if (matroska->tracks[track]->flags & MATROSKA_TRACK_REORDER)
2589
                    matroska_queue_packet_reordered(matroska, pkt, is_bframe);
2590
                else
2591
                    matroska_queue_packet(matroska, pkt);
2592

    
2593
                if (timecode != AV_NOPTS_VALUE)
2594
                    timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2595
            }
2596
            data += lace_size[n];
2597
        }
2598
    }
2599

    
2600
    av_free(lace_size);
2601
    av_free(origdata);
2602
    return res;
2603
}
2604

    
2605
static int
2606
matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2607
                           uint64_t              cluster_time)
2608
{
2609
    int res = 0;
2610
    uint32_t id;
2611
    int is_bframe = 0;
2612
    int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2613
    uint64_t duration = AV_NOPTS_VALUE;
2614
    uint8_t *data;
2615
    int size = 0;
2616
    int64_t pos = 0;
2617

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

    
2620
    while (res == 0) {
2621
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2622
            res = AVERROR_IO;
2623
            break;
2624
        } else if (matroska->level_up) {
2625
            matroska->level_up--;
2626
            break;
2627
        }
2628

    
2629
        switch (id) {
2630
            /* one block inside the group. Note, block parsing is one
2631
             * of the harder things, so this code is a bit complicated.
2632
             * See http://www.matroska.org/ for documentation. */
2633
            case MATROSKA_ID_BLOCK: {
2634
                pos = url_ftell(&matroska->ctx->pb);
2635
                res = ebml_read_binary(matroska, &id, &data, &size);
2636
                break;
2637
            }
2638

    
2639
            case MATROSKA_ID_BLOCKDURATION: {
2640
                if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2641
                    break;
2642
                duration /= matroska->time_scale;
2643
                break;
2644
            }
2645

    
2646
            case MATROSKA_ID_BLOCKREFERENCE: {
2647
                int64_t num;
2648
                /* We've found a reference, so not even the first frame in
2649
                 * the lace is a key frame. */
2650
                is_keyframe = 0;
2651
                if (last_num_packets != matroska->num_packets)
2652
                    matroska->packets[last_num_packets]->flags = 0;
2653
                if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
2654
                    break;
2655
                if (num > 0)
2656
                    is_bframe = 1;
2657
                break;
2658
            }
2659

    
2660
            default:
2661
                av_log(matroska->ctx, AV_LOG_INFO,
2662
                       "Unknown entry 0x%x in blockgroup data\n", id);
2663
                /* fall-through */
2664

    
2665
            case EBML_ID_VOID:
2666
                res = ebml_read_skip(matroska);
2667
                break;
2668
        }
2669

    
2670
        if (matroska->level_up) {
2671
            matroska->level_up--;
2672
            break;
2673
        }
2674
    }
2675

    
2676
    if (res)
2677
        return res;
2678

    
2679
    if (size > 0)
2680
        res = matroska_parse_block(matroska, data, size, pos, cluster_time,
2681
                                   duration, is_keyframe, is_bframe);
2682

    
2683
    return res;
2684
}
2685

    
2686
static int
2687
matroska_parse_cluster (MatroskaDemuxContext *matroska)
2688
{
2689
    int res = 0;
2690
    uint32_t id;
2691
    uint64_t cluster_time = 0;
2692
    uint8_t *data;
2693
    int64_t pos;
2694
    int size;
2695

    
2696
    av_log(matroska->ctx, AV_LOG_DEBUG,
2697
           "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
2698

    
2699
    while (res == 0) {
2700
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2701
            res = AVERROR_IO;
2702
            break;
2703
        } else if (matroska->level_up) {
2704
            matroska->level_up--;
2705
            break;
2706
        }
2707

    
2708
        switch (id) {
2709
            /* cluster timecode */
2710
            case MATROSKA_ID_CLUSTERTIMECODE: {
2711
                uint64_t num;
2712
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2713
                    break;
2714
                cluster_time = num;
2715
                break;
2716
            }
2717

    
2718
                /* a group of blocks inside a cluster */
2719
            case MATROSKA_ID_BLOCKGROUP:
2720
                if ((res = ebml_read_master(matroska, &id)) < 0)
2721
                    break;
2722
                res = matroska_parse_blockgroup(matroska, cluster_time);
2723
                break;
2724

    
2725
            case MATROSKA_ID_SIMPLEBLOCK:
2726
                pos = url_ftell(&matroska->ctx->pb);
2727
                res = ebml_read_binary(matroska, &id, &data, &size);
2728
                if (res == 0)
2729
                    res = matroska_parse_block(matroska, data, size, pos,
2730
                                               cluster_time, AV_NOPTS_VALUE,
2731
                                               -1, 0);
2732
                break;
2733

    
2734
            default:
2735
                av_log(matroska->ctx, AV_LOG_INFO,
2736
                       "Unknown entry 0x%x in cluster data\n", id);
2737
                /* fall-through */
2738

    
2739
            case EBML_ID_VOID:
2740
                res = ebml_read_skip(matroska);
2741
                break;
2742
        }
2743

    
2744
        if (matroska->level_up) {
2745
            matroska->level_up--;
2746
            break;
2747
        }
2748
    }
2749

    
2750
    return res;
2751
}
2752

    
2753
static int
2754
matroska_read_packet (AVFormatContext *s,
2755
                      AVPacket        *pkt)
2756
{
2757
    MatroskaDemuxContext *matroska = s->priv_data;
2758
    int res = 0;
2759
    uint32_t id;
2760

    
2761
    /* Read stream until we have a packet queued. */
2762
    while (matroska_deliver_packet(matroska, pkt)) {
2763

    
2764
        /* Have we already reached the end? */
2765
        if (matroska->done)
2766
            return AVERROR_IO;
2767

    
2768
        while (res == 0) {
2769
            if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2770
                return AVERROR_IO;
2771
            } else if (matroska->level_up) {
2772
                matroska->level_up--;
2773
                break;
2774
            }
2775

    
2776
            switch (id) {
2777
                case MATROSKA_ID_CLUSTER:
2778
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2779
                        break;
2780
                    if ((res = matroska_parse_cluster(matroska)) == 0)
2781
                        res = 1; /* Parsed one cluster, let's get out. */
2782
                    break;
2783

    
2784
                default:
2785
                case EBML_ID_VOID:
2786
                    res = ebml_read_skip(matroska);
2787
                    break;
2788
            }
2789

    
2790
            if (matroska->level_up) {
2791
                matroska->level_up--;
2792
                break;
2793
            }
2794
        }
2795

    
2796
        if (res == -1)
2797
            matroska->done = 1;
2798
    }
2799

    
2800
    return 0;
2801
}
2802

    
2803
static int
2804
matroska_read_close (AVFormatContext *s)
2805
{
2806
    MatroskaDemuxContext *matroska = s->priv_data;
2807
    int n = 0;
2808

    
2809
    av_free(matroska->writing_app);
2810
    av_free(matroska->muxing_app);
2811
    av_free(matroska->index);
2812

    
2813
    if (matroska->packets != NULL) {
2814
        for (n = 0; n < matroska->num_packets; n++) {
2815
            av_free_packet(matroska->packets[n]);
2816
            av_free(matroska->packets[n]);
2817
        }
2818
        av_free(matroska->packets);
2819
    }
2820
    if (matroska->packets_reorder) {
2821
        for (n = 0; n < matroska->num_packets_reorder; n++) {
2822
            av_free_packet(matroska->packets_reorder[n]);
2823
            av_free(matroska->packets_reorder[n]);
2824
        }
2825
        av_free(matroska->packets_reorder);
2826
    }
2827

    
2828
    for (n = 0; n < matroska->num_tracks; n++) {
2829
        MatroskaTrack *track = matroska->tracks[n];
2830
        av_free(track->codec_id);
2831
        av_free(track->codec_name);
2832
        av_free(track->codec_priv);
2833
        av_free(track->name);
2834
        av_free(track->language);
2835

    
2836
        av_free(track);
2837
    }
2838

    
2839
    return 0;
2840
}
2841

    
2842
AVInputFormat matroska_demuxer = {
2843
    "matroska",
2844
    "Matroska file format",
2845
    sizeof(MatroskaDemuxContext),
2846
    matroska_probe,
2847
    matroska_read_header,
2848
    matroska_read_packet,
2849
    matroska_read_close,
2850
};