Statistics
| Branch: | Revision:

ffmpeg / libavformat / matroskadec.c @ f7e97aaf

History | View | Annotate | Download (87.1 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 matroskadec.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
#include "matroska.h"
36

    
37
typedef struct Track {
38
    MatroskaTrackType type;
39

    
40
    /* Unique track number and track ID. stream_index is the index that
41
     * the calling app uses for this track. */
42
    uint32_t num;
43
    uint32_t uid;
44
    int stream_index;
45

    
46
    char *name;
47
    char language[4];
48

    
49
    char *codec_id;
50
    char *codec_name;
51

    
52
    unsigned char *codec_priv;
53
    int codec_priv_size;
54

    
55
    uint64_t default_duration;
56
    MatroskaTrackFlags flags;
57
} MatroskaTrack;
58

    
59
typedef struct MatroskaVideoTrack {
60
    MatroskaTrack track;
61

    
62
    int pixel_width;
63
    int pixel_height;
64
    int display_width;
65
    int display_height;
66

    
67
    uint32_t fourcc;
68

    
69
    MatroskaAspectRatioMode ar_mode;
70
    MatroskaEyeMode eye_mode;
71

    
72
    //..
73
} MatroskaVideoTrack;
74

    
75
typedef struct MatroskaAudioTrack {
76
    MatroskaTrack track;
77

    
78
    int channels;
79
    int bitdepth;
80
    int internal_samplerate;
81
    int samplerate;
82
    int block_align;
83

    
84
    /* real audio header */
85
    int coded_framesize;
86
    int sub_packet_h;
87
    int frame_size;
88
    int sub_packet_size;
89
    int sub_packet_cnt;
90
    int pkt_cnt;
91
    uint8_t *buf;
92
    //..
93
} MatroskaAudioTrack;
94

    
95
typedef struct MatroskaSubtitleTrack {
96
    MatroskaTrack track;
97

    
98
    //..
99
} MatroskaSubtitleTrack;
100

    
101
#define MAX_TRACK_SIZE (FFMAX(FFMAX(sizeof(MatroskaVideoTrack), \
102
                                    sizeof(MatroskaAudioTrack)), \
103
                                    sizeof(MatroskaSubtitleTrack)))
104

    
105
typedef struct MatroskaLevel {
106
    uint64_t start;
107
    uint64_t length;
108
} MatroskaLevel;
109

    
110
typedef struct MatroskaDemuxIndex {
111
  uint64_t        pos;   /* of the corresponding *cluster*! */
112
  uint16_t        track; /* reference to 'num' */
113
  uint64_t        time;  /* in nanoseconds */
114
} MatroskaDemuxIndex;
115

    
116
typedef struct MatroskaDemuxContext {
117
    AVFormatContext *ctx;
118

    
119
    /* ebml stuff */
120
    int num_levels;
121
    MatroskaLevel levels[EBML_MAX_DEPTH];
122
    int level_up;
123

    
124
    /* matroska stuff */
125
    char *writing_app;
126
    char *muxing_app;
127
    int64_t created;
128

    
129
    /* timescale in the file */
130
    int64_t time_scale;
131

    
132
    /* num_streams is the number of streams that av_new_stream() was called
133
     * for ( = that are available to the calling program). */
134
    int num_tracks;
135
    int num_streams;
136
    MatroskaTrack *tracks[MAX_STREAMS];
137

    
138
    /* cache for ID peeking */
139
    uint32_t peek_id;
140

    
141
    /* byte position of the segment inside the stream */
142
    offset_t segment_start;
143

    
144
    /* The packet queue. */
145
    AVPacket **packets;
146
    int num_packets;
147

    
148
    /* have we already parse metadata/cues/clusters? */
149
    int metadata_parsed;
150
    int index_parsed;
151
    int done;
152

    
153
    /* The index for seeking. */
154
    int num_indexes;
155
    MatroskaDemuxIndex *index;
156

    
157
    /* What to skip before effectively reading a packet. */
158
    int skip_to_keyframe;
159
    AVStream *skip_to_stream;
160
} MatroskaDemuxContext;
161

    
162
/*
163
 * The first few functions handle EBML file parsing. The rest
164
 * is the document interpretation. Matroska really just is a
165
 * EBML file.
166
 */
167

    
168
/*
169
 * Return: the amount of levels in the hierarchy that the
170
 * current element lies higher than the previous one.
171
 * The opposite isn't done - that's auto-done using master
172
 * element reading.
173
 */
174

    
175
static int
176
ebml_read_element_level_up (MatroskaDemuxContext *matroska)
177
{
178
    ByteIOContext *pb = &matroska->ctx->pb;
179
    offset_t pos = url_ftell(pb);
180
    int num = 0;
181

    
182
    while (matroska->num_levels > 0) {
183
        MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
184

    
185
        if (pos >= level->start + level->length) {
186
            matroska->num_levels--;
187
            num++;
188
        } else {
189
            break;
190
        }
191
    }
192

    
193
    return num;
194
}
195

    
196
/*
197
 * Read: an "EBML number", which is defined as a variable-length
198
 * array of bytes. The first byte indicates the length by giving a
199
 * number of 0-bits followed by a one. The position of the first
200
 * "one" bit inside the first byte indicates the length of this
201
 * number.
202
 * Returns: num. of bytes read. < 0 on error.
203
 */
204

    
205
static int
206
ebml_read_num (MatroskaDemuxContext *matroska,
207
               int                   max_size,
208
               uint64_t             *number)
209
{
210
    ByteIOContext *pb = &matroska->ctx->pb;
211
    int len_mask = 0x80, read = 1, n = 1;
212
    int64_t total = 0;
213

    
214
    /* the first byte tells us the length in bytes - get_byte() can normally
215
     * return 0, but since that's not a valid first ebmlID byte, we can
216
     * use it safely here to catch EOS. */
217
    if (!(total = get_byte(pb))) {
218
        /* we might encounter EOS here */
219
        if (!url_feof(pb)) {
220
            offset_t pos = url_ftell(pb);
221
            av_log(matroska->ctx, AV_LOG_ERROR,
222
                   "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
223
                   pos, pos);
224
        }
225
        return AVERROR_IO; /* EOS or actual I/O error */
226
    }
227

    
228
    /* get the length of the EBML number */
229
    while (read <= max_size && !(total & len_mask)) {
230
        read++;
231
        len_mask >>= 1;
232
    }
233
    if (read > max_size) {
234
        offset_t pos = url_ftell(pb) - 1;
235
        av_log(matroska->ctx, AV_LOG_ERROR,
236
               "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
237
               (uint8_t) total, pos, pos);
238
        return AVERROR_INVALIDDATA;
239
    }
240

    
241
    /* read out length */
242
    total &= ~len_mask;
243
    while (n++ < read)
244
        total = (total << 8) | get_byte(pb);
245

    
246
    *number = total;
247

    
248
    return read;
249
}
250

    
251
/*
252
 * Read: the element content data ID.
253
 * Return: the number of bytes read or < 0 on error.
254
 */
255

    
256
static int
257
ebml_read_element_id (MatroskaDemuxContext *matroska,
258
                      uint32_t             *id,
259
                      int                  *level_up)
260
{
261
    int read;
262
    uint64_t total;
263

    
264
    /* if we re-call this, use our cached ID */
265
    if (matroska->peek_id != 0) {
266
        if (level_up)
267
            *level_up = 0;
268
        *id = matroska->peek_id;
269
        return 0;
270
    }
271

    
272
    /* read out the "EBML number", include tag in ID */
273
    if ((read = ebml_read_num(matroska, 4, &total)) < 0)
274
        return read;
275
    *id = matroska->peek_id  = total | (1 << (read * 7));
276

    
277
    /* level tracking */
278
    if (level_up)
279
        *level_up = ebml_read_element_level_up(matroska);
280

    
281
    return read;
282
}
283

    
284
/*
285
 * Read: element content length.
286
 * Return: the number of bytes read or < 0 on error.
287
 */
288

    
289
static int
290
ebml_read_element_length (MatroskaDemuxContext *matroska,
291
                          uint64_t             *length)
292
{
293
    /* clear cache since we're now beyond that data point */
294
    matroska->peek_id = 0;
295

    
296
    /* read out the "EBML number", include tag in ID */
297
    return ebml_read_num(matroska, 8, length);
298
}
299

    
300
/*
301
 * Return: the ID of the next element, or 0 on error.
302
 * Level_up contains the amount of levels that this
303
 * next element lies higher than the previous one.
304
 */
305

    
306
static uint32_t
307
ebml_peek_id (MatroskaDemuxContext *matroska,
308
              int                  *level_up)
309
{
310
    uint32_t id;
311

    
312
    assert(level_up != NULL);
313

    
314
    if (ebml_read_element_id(matroska, &id, level_up) < 0)
315
        return 0;
316

    
317
    return id;
318
}
319

    
320
/*
321
 * Seek to a given offset.
322
 * 0 is success, -1 is failure.
323
 */
324

    
325
static int
326
ebml_read_seek (MatroskaDemuxContext *matroska,
327
                offset_t              offset)
328
{
329
    ByteIOContext *pb = &matroska->ctx->pb;
330

    
331
    /* clear ID cache, if any */
332
    matroska->peek_id = 0;
333

    
334
    return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
335
}
336

    
337
/*
338
 * Skip the next element.
339
 * 0 is success, -1 is failure.
340
 */
341

    
342
static int
343
ebml_read_skip (MatroskaDemuxContext *matroska)
344
{
345
    ByteIOContext *pb = &matroska->ctx->pb;
346
    uint32_t id;
347
    uint64_t length;
348
    int res;
349

    
350
    if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
351
        (res = ebml_read_element_length(matroska, &length)) < 0)
352
        return res;
353

    
354
    url_fskip(pb, length);
355

    
356
    return 0;
357
}
358

    
359
/*
360
 * Read the next element as an unsigned int.
361
 * 0 is success, < 0 is failure.
362
 */
363

    
364
static int
365
ebml_read_uint (MatroskaDemuxContext *matroska,
366
                uint32_t             *id,
367
                uint64_t             *num)
368
{
369
    ByteIOContext *pb = &matroska->ctx->pb;
370
    int n = 0, size, res;
371
    uint64_t rlength;
372

    
373
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
374
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
375
        return res;
376
    size = rlength;
377
    if (size < 1 || size > 8) {
378
        offset_t pos = url_ftell(pb);
379
        av_log(matroska->ctx, AV_LOG_ERROR,
380
               "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
381
                size, pos, pos);
382
        return AVERROR_INVALIDDATA;
383
    }
384

    
385
    /* big-endian ordening; build up number */
386
    *num = 0;
387
    while (n++ < size)
388
        *num = (*num << 8) | get_byte(pb);
389

    
390
    return 0;
391
}
392

    
393
/*
394
 * Read the next element as a signed int.
395
 * 0 is success, < 0 is failure.
396
 */
397

    
398
static int
399
ebml_read_sint (MatroskaDemuxContext *matroska,
400
                uint32_t             *id,
401
                int64_t              *num)
402
{
403
    ByteIOContext *pb = &matroska->ctx->pb;
404
    int size, n = 1, negative = 0, res;
405
    uint64_t rlength;
406

    
407
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
408
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
409
        return res;
410
    size = rlength;
411
    if (size < 1 || size > 8) {
412
        offset_t pos = url_ftell(pb);
413
        av_log(matroska->ctx, AV_LOG_ERROR,
414
               "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
415
                size, pos, pos);
416
        return AVERROR_INVALIDDATA;
417
    }
418
    if ((*num = get_byte(pb)) & 0x80) {
419
        negative = 1;
420
        *num &= ~0x80;
421
    }
422
    while (n++ < size)
423
        *num = (*num << 8) | get_byte(pb);
424

    
425
    /* make signed */
426
    if (negative)
427
        *num = *num - (1LL << ((8 * size) - 1));
428

    
429
    return 0;
430
}
431

    
432
/*
433
 * Read the next element as a float.
434
 * 0 is success, < 0 is failure.
435
 */
436

    
437
static int
438
ebml_read_float (MatroskaDemuxContext *matroska,
439
                 uint32_t             *id,
440
                 double               *num)
441
{
442
    ByteIOContext *pb = &matroska->ctx->pb;
443
    int size, res;
444
    uint64_t rlength;
445

    
446
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
447
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
448
        return res;
449
    size = rlength;
450

    
451
    if (size == 4) {
452
        *num= av_int2flt(get_be32(pb));
453
    } else if(size==8){
454
        *num= av_int2dbl(get_be64(pb));
455
    } else{
456
        offset_t pos = url_ftell(pb);
457
        av_log(matroska->ctx, AV_LOG_ERROR,
458
               "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
459
               size, pos, pos);
460
        return AVERROR_INVALIDDATA;
461
    }
462

    
463
    return 0;
464
}
465

    
466
/*
467
 * Read the next element as an ASCII string.
468
 * 0 is success, < 0 is failure.
469
 */
470

    
471
static int
472
ebml_read_ascii (MatroskaDemuxContext *matroska,
473
                 uint32_t             *id,
474
                 char                **str)
475
{
476
    ByteIOContext *pb = &matroska->ctx->pb;
477
    int size, res;
478
    uint64_t rlength;
479

    
480
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
481
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
482
        return res;
483
    size = rlength;
484

    
485
    /* ebml strings are usually not 0-terminated, so we allocate one
486
     * byte more, read the string and NULL-terminate it ourselves. */
487
    if (size < 0 || !(*str = av_malloc(size + 1))) {
488
        av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
489
        return AVERROR_NOMEM;
490
    }
491
    if (get_buffer(pb, (uint8_t *) *str, size) != size) {
492
        offset_t pos = url_ftell(pb);
493
        av_log(matroska->ctx, AV_LOG_ERROR,
494
               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
495
        return AVERROR_IO;
496
    }
497
    (*str)[size] = '\0';
498

    
499
    return 0;
500
}
501

    
502
/*
503
 * Read the next element as a UTF-8 string.
504
 * 0 is success, < 0 is failure.
505
 */
506

    
507
static int
508
ebml_read_utf8 (MatroskaDemuxContext *matroska,
509
                uint32_t             *id,
510
                char                **str)
511
{
512
  return ebml_read_ascii(matroska, id, str);
513
}
514

    
515
/*
516
 * Read the next element as a date (nanoseconds since 1/1/2000).
517
 * 0 is success, < 0 is failure.
518
 */
519

    
520
static int
521
ebml_read_date (MatroskaDemuxContext *matroska,
522
                uint32_t             *id,
523
                int64_t              *date)
524
{
525
  return ebml_read_sint(matroska, id, date);
526
}
527

    
528
/*
529
 * Read the next element, but only the header. The contents
530
 * are supposed to be sub-elements which can be read separately.
531
 * 0 is success, < 0 is failure.
532
 */
533

    
534
static int
535
ebml_read_master (MatroskaDemuxContext *matroska,
536
                  uint32_t             *id)
537
{
538
    ByteIOContext *pb = &matroska->ctx->pb;
539
    uint64_t length;
540
    MatroskaLevel *level;
541
    int res;
542

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

    
547
    /* protect... (Heaven forbids that the '>' is true) */
548
    if (matroska->num_levels >= EBML_MAX_DEPTH) {
549
        av_log(matroska->ctx, AV_LOG_ERROR,
550
               "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
551
        return AVERROR_NOTSUPP;
552
    }
553

    
554
    /* remember level */
555
    level = &matroska->levels[matroska->num_levels++];
556
    level->start = url_ftell(pb);
557
    level->length = length;
558

    
559
    return 0;
560
}
561

    
562
/*
563
 * Read the next element as binary data.
564
 * 0 is success, < 0 is failure.
565
 */
566

    
567
static int
568
ebml_read_binary (MatroskaDemuxContext *matroska,
569
                  uint32_t             *id,
570
                  uint8_t             **binary,
571
                  int                  *size)
572
{
573
    ByteIOContext *pb = &matroska->ctx->pb;
574
    uint64_t rlength;
575
    int res;
576

    
577
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
578
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
579
        return res;
580
    *size = rlength;
581

    
582
    if (!(*binary = av_malloc(*size))) {
583
        av_log(matroska->ctx, AV_LOG_ERROR,
584
               "Memory allocation error\n");
585
        return AVERROR_NOMEM;
586
    }
587

    
588
    if (get_buffer(pb, *binary, *size) != *size) {
589
        offset_t pos = url_ftell(pb);
590
        av_log(matroska->ctx, AV_LOG_ERROR,
591
               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
592
        return AVERROR_IO;
593
    }
594

    
595
    return 0;
596
}
597

    
598
/*
599
 * Read signed/unsigned "EBML" numbers.
600
 * Return: number of bytes processed, < 0 on error.
601
 * XXX: use ebml_read_num().
602
 */
603

    
604
static int
605
matroska_ebmlnum_uint (uint8_t  *data,
606
                       uint32_t  size,
607
                       uint64_t *num)
608
{
609
    int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
610
    uint64_t total;
611

    
612
    if (size <= 0)
613
        return AVERROR_INVALIDDATA;
614

    
615
    total = data[0];
616
    while (read <= 8 && !(total & len_mask)) {
617
        read++;
618
        len_mask >>= 1;
619
    }
620
    if (read > 8)
621
        return AVERROR_INVALIDDATA;
622

    
623
    if ((total &= (len_mask - 1)) == len_mask - 1)
624
        num_ffs++;
625
    if (size < read)
626
        return AVERROR_INVALIDDATA;
627
    while (n < read) {
628
        if (data[n] == 0xff)
629
            num_ffs++;
630
        total = (total << 8) | data[n];
631
        n++;
632
    }
633

    
634
    if (read == num_ffs)
635
        *num = (uint64_t)-1;
636
    else
637
        *num = total;
638

    
639
    return read;
640
}
641

    
642
/*
643
 * Same as above, but signed.
644
 */
645

    
646
static int
647
matroska_ebmlnum_sint (uint8_t  *data,
648
                       uint32_t  size,
649
                       int64_t  *num)
650
{
651
    uint64_t unum;
652
    int res;
653

    
654
    /* read as unsigned number first */
655
    if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
656
        return res;
657

    
658
    /* make signed (weird way) */
659
    if (unum == (uint64_t)-1)
660
        *num = INT64_MAX;
661
    else
662
        *num = unum - ((1LL << ((7 * res) - 1)) - 1);
663

    
664
    return res;
665
}
666

    
667
/*
668
 * Read an EBML header.
669
 * 0 is success, < 0 is failure.
670
 */
671

    
672
static int
673
ebml_read_header (MatroskaDemuxContext *matroska,
674
                  char                **doctype,
675
                  int                  *version)
676
{
677
    uint32_t id;
678
    int level_up, res = 0;
679

    
680
    /* default init */
681
    if (doctype)
682
        *doctype = NULL;
683
    if (version)
684
        *version = 1;
685

    
686
    if (!(id = ebml_peek_id(matroska, &level_up)) ||
687
        level_up != 0 || id != EBML_ID_HEADER) {
688
        av_log(matroska->ctx, AV_LOG_ERROR,
689
               "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
690
        return AVERROR_INVALIDDATA;
691
    }
692
    if ((res = ebml_read_master(matroska, &id)) < 0)
693
        return res;
694

    
695
    while (res == 0) {
696
        if (!(id = ebml_peek_id(matroska, &level_up)))
697
            return AVERROR_IO;
698

    
699
        /* end-of-header */
700
        if (level_up)
701
            break;
702

    
703
        switch (id) {
704
            /* is our read version uptodate? */
705
            case EBML_ID_EBMLREADVERSION: {
706
                uint64_t num;
707

    
708
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
709
                    return res;
710
                if (num > EBML_VERSION) {
711
                    av_log(matroska->ctx, AV_LOG_ERROR,
712
                           "EBML version %"PRIu64" (> %d) is not supported\n",
713
                           num, EBML_VERSION);
714
                    return AVERROR_INVALIDDATA;
715
                }
716
                break;
717
            }
718

    
719
            /* we only handle 8 byte lengths at max */
720
            case EBML_ID_EBMLMAXSIZELENGTH: {
721
                uint64_t num;
722

    
723
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
724
                    return res;
725
                if (num > sizeof(uint64_t)) {
726
                    av_log(matroska->ctx, AV_LOG_ERROR,
727
                           "Integers of size %"PRIu64" (> %zd) not supported\n",
728
                           num, sizeof(uint64_t));
729
                    return AVERROR_INVALIDDATA;
730
                }
731
                break;
732
            }
733

    
734
            /* we handle 4 byte IDs at max */
735
            case EBML_ID_EBMLMAXIDLENGTH: {
736
                uint64_t num;
737

    
738
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
739
                    return res;
740
                if (num > sizeof(uint32_t)) {
741
                    av_log(matroska->ctx, AV_LOG_ERROR,
742
                           "IDs of size %"PRIu64" (> %zu) not supported\n",
743
                            num, sizeof(uint32_t));
744
                    return AVERROR_INVALIDDATA;
745
                }
746
                break;
747
            }
748

    
749
            case EBML_ID_DOCTYPE: {
750
                char *text;
751

    
752
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
753
                    return res;
754
                if (doctype) {
755
                    if (*doctype)
756
                        av_free(*doctype);
757
                    *doctype = text;
758
                } else
759
                    av_free(text);
760
                break;
761
            }
762

    
763
            case EBML_ID_DOCTYPEREADVERSION: {
764
                uint64_t num;
765

    
766
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
767
                    return res;
768
                if (version)
769
                    *version = num;
770
                break;
771
            }
772

    
773
            default:
774
                av_log(matroska->ctx, AV_LOG_INFO,
775
                       "Unknown data type 0x%x in EBML header", id);
776
                /* pass-through */
777

    
778
            case EBML_ID_VOID:
779
            /* we ignore these two, as they don't tell us anything we
780
             * care about */
781
            case EBML_ID_EBMLVERSION:
782
            case EBML_ID_DOCTYPEVERSION:
783
                res = ebml_read_skip (matroska);
784
                break;
785
        }
786
    }
787

    
788
    return 0;
789
}
790

    
791

    
792
static int
793
matroska_find_track_by_num (MatroskaDemuxContext *matroska,
794
                            int                   num)
795
{
796
    int i;
797

    
798
    for (i = 0; i < matroska->num_tracks; i++)
799
        if (matroska->tracks[i]->num == num)
800
            return i;
801

    
802
    return -1;
803
}
804

    
805

    
806
/*
807
 * Put one packet in an application-supplied AVPacket struct.
808
 * Returns 0 on success or -1 on failure.
809
 */
810

    
811
static int
812
matroska_deliver_packet (MatroskaDemuxContext *matroska,
813
                         AVPacket             *pkt)
814
{
815
    if (matroska->num_packets > 0) {
816
        memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
817
        av_free(matroska->packets[0]);
818
        if (matroska->num_packets > 1) {
819
            memmove(&matroska->packets[0], &matroska->packets[1],
820
                    (matroska->num_packets - 1) * sizeof(AVPacket *));
821
            matroska->packets =
822
                av_realloc(matroska->packets, (matroska->num_packets - 1) *
823
                           sizeof(AVPacket *));
824
        } else {
825
            av_freep(&matroska->packets);
826
        }
827
        matroska->num_packets--;
828
        return 0;
829
    }
830

    
831
    return -1;
832
}
833

    
834
/*
835
 * Put a packet into our internal queue. Will be delivered to the
836
 * user/application during the next get_packet() call.
837
 */
838

    
839
static void
840
matroska_queue_packet (MatroskaDemuxContext *matroska,
841
                       AVPacket             *pkt)
842
{
843
    matroska->packets =
844
        av_realloc(matroska->packets, (matroska->num_packets + 1) *
845
                   sizeof(AVPacket *));
846
    matroska->packets[matroska->num_packets] = pkt;
847
    matroska->num_packets++;
848
}
849

    
850

    
851
/*
852
 * Autodetecting...
853
 */
854

    
855
static int
856
matroska_probe (AVProbeData *p)
857
{
858
    uint64_t total = 0;
859
    int len_mask = 0x80, size = 1, n = 1;
860
    uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
861

    
862
    /* ebml header? */
863
    if (AV_RB32(p->buf) != EBML_ID_HEADER)
864
        return 0;
865

    
866
    /* length of header */
867
    total = p->buf[4];
868
    while (size <= 8 && !(total & len_mask)) {
869
        size++;
870
        len_mask >>= 1;
871
    }
872
    if (size > 8)
873
      return 0;
874
    total &= (len_mask - 1);
875
    while (n < size)
876
        total = (total << 8) | p->buf[4 + n++];
877

    
878
    /* does the probe data contain the whole header? */
879
    if (p->buf_size < 4 + size + total)
880
      return 0;
881

    
882
    /* the header must contain the document type 'matroska'. For now,
883
     * we don't parse the whole header but simply check for the
884
     * availability of that array of characters inside the header.
885
     * Not fully fool-proof, but good enough. */
886
    for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
887
        if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
888
            return AVPROBE_SCORE_MAX;
889

    
890
    return 0;
891
}
892

    
893
/*
894
 * From here on, it's all XML-style DTD stuff... Needs no comments.
895
 */
896

    
897
static int
898
matroska_parse_info (MatroskaDemuxContext *matroska)
899
{
900
    int res = 0;
901
    uint32_t id;
902

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

    
905
    while (res == 0) {
906
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
907
            res = AVERROR_IO;
908
            break;
909
        } else if (matroska->level_up) {
910
            matroska->level_up--;
911
            break;
912
        }
913

    
914
        switch (id) {
915
            /* cluster timecode */
916
            case MATROSKA_ID_TIMECODESCALE: {
917
                uint64_t num;
918
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
919
                    break;
920
                matroska->time_scale = num;
921
                break;
922
            }
923

    
924
            case MATROSKA_ID_DURATION: {
925
                double num;
926
                if ((res = ebml_read_float(matroska, &id, &num)) < 0)
927
                    break;
928
                matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
929
                break;
930
            }
931

    
932
            case MATROSKA_ID_TITLE: {
933
                char *text;
934
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
935
                    break;
936
                strncpy(matroska->ctx->title, text,
937
                        sizeof(matroska->ctx->title)-1);
938
                av_free(text);
939
                break;
940
            }
941

    
942
            case MATROSKA_ID_WRITINGAPP: {
943
                char *text;
944
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
945
                    break;
946
                matroska->writing_app = text;
947
                break;
948
            }
949

    
950
            case MATROSKA_ID_MUXINGAPP: {
951
                char *text;
952
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
953
                    break;
954
                matroska->muxing_app = text;
955
                break;
956
            }
957

    
958
            case MATROSKA_ID_DATEUTC: {
959
                int64_t time;
960
                if ((res = ebml_read_date(matroska, &id, &time)) < 0)
961
                    break;
962
                matroska->created = time;
963
                break;
964
            }
965

    
966
            default:
967
                av_log(matroska->ctx, AV_LOG_INFO,
968
                       "Unknown entry 0x%x in info header\n", id);
969
                /* fall-through */
970

    
971
            case EBML_ID_VOID:
972
                res = ebml_read_skip(matroska);
973
                break;
974
        }
975

    
976
        if (matroska->level_up) {
977
            matroska->level_up--;
978
            break;
979
        }
980
    }
981

    
982
    return res;
983
}
984

    
985
static int
986
matroska_add_stream (MatroskaDemuxContext *matroska)
987
{
988
    int res = 0;
989
    uint32_t id;
990
    MatroskaTrack *track;
991

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

    
994
    /* Allocate a generic track. As soon as we know its type we'll realloc. */
995
    track = av_mallocz(MAX_TRACK_SIZE);
996
    matroska->num_tracks++;
997
    strcpy(track->language, "eng");
998

    
999
    /* start with the master */
1000
    if ((res = ebml_read_master(matroska, &id)) < 0)
1001
        return res;
1002

    
1003
    /* try reading the trackentry headers */
1004
    while (res == 0) {
1005
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1006
            res = AVERROR_IO;
1007
            break;
1008
        } else if (matroska->level_up > 0) {
1009
            matroska->level_up--;
1010
            break;
1011
        }
1012

    
1013
        switch (id) {
1014
            /* track number (unique stream ID) */
1015
            case MATROSKA_ID_TRACKNUMBER: {
1016
                uint64_t num;
1017
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1018
                    break;
1019
                track->num = num;
1020
                break;
1021
            }
1022

    
1023
            /* track UID (unique identifier) */
1024
            case MATROSKA_ID_TRACKUID: {
1025
                uint64_t num;
1026
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1027
                    break;
1028
                track->uid = num;
1029
                break;
1030
            }
1031

    
1032
            /* track type (video, audio, combined, subtitle, etc.) */
1033
            case MATROSKA_ID_TRACKTYPE: {
1034
                uint64_t num;
1035
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1036
                    break;
1037
                if (track->type && track->type != num) {
1038
                    av_log(matroska->ctx, AV_LOG_INFO,
1039
                           "More than one tracktype in an entry - skip\n");
1040
                    break;
1041
                }
1042
                track->type = num;
1043

    
1044
                switch (track->type) {
1045
                    case MATROSKA_TRACK_TYPE_VIDEO:
1046
                    case MATROSKA_TRACK_TYPE_AUDIO:
1047
                    case MATROSKA_TRACK_TYPE_SUBTITLE:
1048
                        break;
1049
                    case MATROSKA_TRACK_TYPE_COMPLEX:
1050
                    case MATROSKA_TRACK_TYPE_LOGO:
1051
                    case MATROSKA_TRACK_TYPE_CONTROL:
1052
                    default:
1053
                        av_log(matroska->ctx, AV_LOG_INFO,
1054
                               "Unknown or unsupported track type 0x%x\n",
1055
                               track->type);
1056
                        track->type = 0;
1057
                        break;
1058
                }
1059
                matroska->tracks[matroska->num_tracks - 1] = track;
1060
                break;
1061
            }
1062

    
1063
            /* tracktype specific stuff for video */
1064
            case MATROSKA_ID_TRACKVIDEO: {
1065
                MatroskaVideoTrack *videotrack;
1066
                if (!track->type)
1067
                    track->type = MATROSKA_TRACK_TYPE_VIDEO;
1068
                if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1069
                    av_log(matroska->ctx, AV_LOG_INFO,
1070
                           "video data in non-video track - ignoring\n");
1071
                    res = AVERROR_INVALIDDATA;
1072
                    break;
1073
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1074
                    break;
1075
                videotrack = (MatroskaVideoTrack *)track;
1076

    
1077
                while (res == 0) {
1078
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1079
                        res = AVERROR_IO;
1080
                        break;
1081
                    } else if (matroska->level_up > 0) {
1082
                        matroska->level_up--;
1083
                        break;
1084
                    }
1085

    
1086
                    switch (id) {
1087
                        /* fixme, this should be one-up, but I get it here */
1088
                        case MATROSKA_ID_TRACKDEFAULTDURATION: {
1089
                            uint64_t num;
1090
                            if ((res = ebml_read_uint (matroska, &id,
1091
                                                       &num)) < 0)
1092
                                break;
1093
                            track->default_duration = num/matroska->time_scale;
1094
                            break;
1095
                        }
1096

    
1097
                        /* video framerate */
1098
                        case MATROSKA_ID_VIDEOFRAMERATE: {
1099
                            double num;
1100
                            if ((res = ebml_read_float(matroska, &id,
1101
                                                       &num)) < 0)
1102
                                break;
1103
                            track->default_duration = 1000000000/(matroska->time_scale*num);
1104
                            break;
1105
                        }
1106

    
1107
                        /* width of the size to display the video at */
1108
                        case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1109
                            uint64_t num;
1110
                            if ((res = ebml_read_uint(matroska, &id,
1111
                                                      &num)) < 0)
1112
                                break;
1113
                            videotrack->display_width = num;
1114
                            break;
1115
                        }
1116

    
1117
                        /* height of the size to display the video at */
1118
                        case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1119
                            uint64_t num;
1120
                            if ((res = ebml_read_uint(matroska, &id,
1121
                                                      &num)) < 0)
1122
                                break;
1123
                            videotrack->display_height = num;
1124
                            break;
1125
                        }
1126

    
1127
                        /* width of the video in the file */
1128
                        case MATROSKA_ID_VIDEOPIXELWIDTH: {
1129
                            uint64_t num;
1130
                            if ((res = ebml_read_uint(matroska, &id,
1131
                                                      &num)) < 0)
1132
                                break;
1133
                            videotrack->pixel_width = num;
1134
                            break;
1135
                        }
1136

    
1137
                        /* height of the video in the file */
1138
                        case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1139
                            uint64_t num;
1140
                            if ((res = ebml_read_uint(matroska, &id,
1141
                                                      &num)) < 0)
1142
                                break;
1143
                            videotrack->pixel_height = num;
1144
                            break;
1145
                        }
1146

    
1147
                        /* whether the video is interlaced */
1148
                        case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1149
                            uint64_t num;
1150
                            if ((res = ebml_read_uint(matroska, &id,
1151
                                                      &num)) < 0)
1152
                                break;
1153
                            if (num)
1154
                                track->flags |=
1155
                                    MATROSKA_VIDEOTRACK_INTERLACED;
1156
                            else
1157
                                track->flags &=
1158
                                    ~MATROSKA_VIDEOTRACK_INTERLACED;
1159
                            break;
1160
                        }
1161

    
1162
                        /* stereo mode (whether the video has two streams,
1163
                         * where one is for the left eye and the other for
1164
                         * the right eye, which creates a 3D-like
1165
                         * effect) */
1166
                        case MATROSKA_ID_VIDEOSTEREOMODE: {
1167
                            uint64_t num;
1168
                            if ((res = ebml_read_uint(matroska, &id,
1169
                                                      &num)) < 0)
1170
                                break;
1171
                            if (num != MATROSKA_EYE_MODE_MONO &&
1172
                                num != MATROSKA_EYE_MODE_LEFT &&
1173
                                num != MATROSKA_EYE_MODE_RIGHT &&
1174
                                num != MATROSKA_EYE_MODE_BOTH) {
1175
                                av_log(matroska->ctx, AV_LOG_INFO,
1176
                                       "Ignoring unknown eye mode 0x%x\n",
1177
                                       (uint32_t) num);
1178
                                break;
1179
                            }
1180
                            videotrack->eye_mode = num;
1181
                            break;
1182
                        }
1183

    
1184
                        /* aspect ratio behaviour */
1185
                        case MATROSKA_ID_VIDEOASPECTRATIO: {
1186
                            uint64_t num;
1187
                            if ((res = ebml_read_uint(matroska, &id,
1188
                                                      &num)) < 0)
1189
                                break;
1190
                            if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1191
                                num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1192
                                num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1193
                                av_log(matroska->ctx, AV_LOG_INFO,
1194
                                       "Ignoring unknown aspect ratio 0x%x\n",
1195
                                       (uint32_t) num);
1196
                                break;
1197
                            }
1198
                            videotrack->ar_mode = num;
1199
                            break;
1200
                        }
1201

    
1202
                        /* colourspace (only matters for raw video)
1203
                         * fourcc */
1204
                        case MATROSKA_ID_VIDEOCOLOURSPACE: {
1205
                            uint64_t num;
1206
                            if ((res = ebml_read_uint(matroska, &id,
1207
                                                      &num)) < 0)
1208
                                break;
1209
                            videotrack->fourcc = num;
1210
                            break;
1211
                        }
1212

    
1213
                        default:
1214
                            av_log(matroska->ctx, AV_LOG_INFO,
1215
                                   "Unknown video track header entry "
1216
                                   "0x%x - ignoring\n", id);
1217
                            /* pass-through */
1218

    
1219
                        case EBML_ID_VOID:
1220
                            res = ebml_read_skip(matroska);
1221
                            break;
1222
                    }
1223

    
1224
                    if (matroska->level_up) {
1225
                        matroska->level_up--;
1226
                        break;
1227
                    }
1228
                }
1229
                break;
1230
            }
1231

    
1232
            /* tracktype specific stuff for audio */
1233
            case MATROSKA_ID_TRACKAUDIO: {
1234
                MatroskaAudioTrack *audiotrack;
1235
                if (!track->type)
1236
                    track->type = MATROSKA_TRACK_TYPE_AUDIO;
1237
                if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1238
                    av_log(matroska->ctx, AV_LOG_INFO,
1239
                           "audio data in non-audio track - ignoring\n");
1240
                    res = AVERROR_INVALIDDATA;
1241
                    break;
1242
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1243
                    break;
1244
                audiotrack = (MatroskaAudioTrack *)track;
1245
                audiotrack->channels = 1;
1246
                audiotrack->samplerate = 8000;
1247

    
1248
                while (res == 0) {
1249
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1250
                        res = AVERROR_IO;
1251
                        break;
1252
                    } else if (matroska->level_up > 0) {
1253
                        matroska->level_up--;
1254
                        break;
1255
                    }
1256

    
1257
                    switch (id) {
1258
                        /* samplerate */
1259
                        case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1260
                            double num;
1261
                            if ((res = ebml_read_float(matroska, &id,
1262
                                                       &num)) < 0)
1263
                                break;
1264
                            audiotrack->internal_samplerate =
1265
                            audiotrack->samplerate = num;
1266
                            break;
1267
                        }
1268

    
1269
                        case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1270
                            double num;
1271
                            if ((res = ebml_read_float(matroska, &id,
1272
                                                       &num)) < 0)
1273
                                break;
1274
                            audiotrack->samplerate = num;
1275
                            break;
1276
                        }
1277

    
1278
                            /* bitdepth */
1279
                        case MATROSKA_ID_AUDIOBITDEPTH: {
1280
                            uint64_t num;
1281
                            if ((res = ebml_read_uint(matroska, &id,
1282
                                                      &num)) < 0)
1283
                                break;
1284
                            audiotrack->bitdepth = num;
1285
                            break;
1286
                        }
1287

    
1288
                            /* channels */
1289
                        case MATROSKA_ID_AUDIOCHANNELS: {
1290
                            uint64_t num;
1291
                            if ((res = ebml_read_uint(matroska, &id,
1292
                                                      &num)) < 0)
1293
                                break;
1294
                            audiotrack->channels = num;
1295
                            break;
1296
                        }
1297

    
1298
                        default:
1299
                            av_log(matroska->ctx, AV_LOG_INFO,
1300
                                   "Unknown audio track header entry "
1301
                                   "0x%x - ignoring\n", id);
1302
                            /* pass-through */
1303

    
1304
                        case EBML_ID_VOID:
1305
                            res = ebml_read_skip(matroska);
1306
                            break;
1307
                    }
1308

    
1309
                    if (matroska->level_up) {
1310
                        matroska->level_up--;
1311
                        break;
1312
                    }
1313
                }
1314
                break;
1315
            }
1316

    
1317
                /* codec identifier */
1318
            case MATROSKA_ID_CODECID: {
1319
                char *text;
1320
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1321
                    break;
1322
                track->codec_id = text;
1323
                break;
1324
            }
1325

    
1326
                /* codec private data */
1327
            case MATROSKA_ID_CODECPRIVATE: {
1328
                uint8_t *data;
1329
                int size;
1330
                if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1331
                    break;
1332
                track->codec_priv = data;
1333
                track->codec_priv_size = size;
1334
                break;
1335
            }
1336

    
1337
                /* name of the codec */
1338
            case MATROSKA_ID_CODECNAME: {
1339
                char *text;
1340
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1341
                    break;
1342
                track->codec_name = text;
1343
                break;
1344
            }
1345

    
1346
                /* name of this track */
1347
            case MATROSKA_ID_TRACKNAME: {
1348
                char *text;
1349
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1350
                    break;
1351
                track->name = text;
1352
                break;
1353
            }
1354

    
1355
                /* language (matters for audio/subtitles, mostly) */
1356
            case MATROSKA_ID_TRACKLANGUAGE: {
1357
                char *text, *end;
1358
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1359
                    break;
1360
                if ((end = strchr(text, '-')))
1361
                    *end = '\0';
1362
                if (strlen(text) == 3)
1363
                    strcpy(track->language, text);
1364
                av_free(text);
1365
                break;
1366
            }
1367

    
1368
                /* whether this is actually used */
1369
            case MATROSKA_ID_TRACKFLAGENABLED: {
1370
                uint64_t num;
1371
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1372
                    break;
1373
                if (num)
1374
                    track->flags |= MATROSKA_TRACK_ENABLED;
1375
                else
1376
                    track->flags &= ~MATROSKA_TRACK_ENABLED;
1377
                break;
1378
            }
1379

    
1380
                /* whether it's the default for this track type */
1381
            case MATROSKA_ID_TRACKFLAGDEFAULT: {
1382
                uint64_t num;
1383
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1384
                    break;
1385
                if (num)
1386
                    track->flags |= MATROSKA_TRACK_DEFAULT;
1387
                else
1388
                    track->flags &= ~MATROSKA_TRACK_DEFAULT;
1389
                break;
1390
            }
1391

    
1392
                /* lacing (like MPEG, where blocks don't end/start on frame
1393
                 * boundaries) */
1394
            case MATROSKA_ID_TRACKFLAGLACING: {
1395
                uint64_t num;
1396
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1397
                    break;
1398
                if (num)
1399
                    track->flags |= MATROSKA_TRACK_LACING;
1400
                else
1401
                    track->flags &= ~MATROSKA_TRACK_LACING;
1402
                break;
1403
            }
1404

    
1405
                /* default length (in time) of one data block in this track */
1406
            case MATROSKA_ID_TRACKDEFAULTDURATION: {
1407
                uint64_t num;
1408
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1409
                    break;
1410
                track->default_duration = num / matroska->time_scale;
1411
                break;
1412
            }
1413

    
1414
            default:
1415
                av_log(matroska->ctx, AV_LOG_INFO,
1416
                       "Unknown track header entry 0x%x - ignoring\n", id);
1417
                /* pass-through */
1418

    
1419
            case EBML_ID_VOID:
1420
            /* we ignore these because they're nothing useful. */
1421
            case MATROSKA_ID_CODECINFOURL:
1422
            case MATROSKA_ID_CODECDOWNLOADURL:
1423
            case MATROSKA_ID_TRACKMINCACHE:
1424
            case MATROSKA_ID_TRACKMAXCACHE:
1425
                res = ebml_read_skip(matroska);
1426
                break;
1427
        }
1428

    
1429
        if (matroska->level_up) {
1430
            matroska->level_up--;
1431
            break;
1432
        }
1433
    }
1434

    
1435
    return res;
1436
}
1437

    
1438
static int
1439
matroska_parse_tracks (MatroskaDemuxContext *matroska)
1440
{
1441
    int res = 0;
1442
    uint32_t id;
1443

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

    
1446
    while (res == 0) {
1447
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1448
            res = AVERROR_IO;
1449
            break;
1450
        } else if (matroska->level_up) {
1451
            matroska->level_up--;
1452
            break;
1453
        }
1454

    
1455
        switch (id) {
1456
            /* one track within the "all-tracks" header */
1457
            case MATROSKA_ID_TRACKENTRY:
1458
                res = matroska_add_stream(matroska);
1459
                break;
1460

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

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

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

    
1477
    return res;
1478
}
1479

    
1480
static int
1481
matroska_parse_index (MatroskaDemuxContext *matroska)
1482
{
1483
    int res = 0;
1484
    uint32_t id;
1485
    MatroskaDemuxIndex idx;
1486

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

    
1489
    while (res == 0) {
1490
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1491
            res = AVERROR_IO;
1492
            break;
1493
        } else if (matroska->level_up) {
1494
            matroska->level_up--;
1495
            break;
1496
        }
1497

    
1498
        switch (id) {
1499
            /* one single index entry ('point') */
1500
            case MATROSKA_ID_POINTENTRY:
1501
                if ((res = ebml_read_master(matroska, &id)) < 0)
1502
                    break;
1503

    
1504
                /* in the end, we hope to fill one entry with a
1505
                 * timestamp, a file position and a tracknum */
1506
                idx.pos   = (uint64_t) -1;
1507
                idx.time  = (uint64_t) -1;
1508
                idx.track = (uint16_t) -1;
1509

    
1510
                while (res == 0) {
1511
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1512
                        res = AVERROR_IO;
1513
                        break;
1514
                    } else if (matroska->level_up) {
1515
                        matroska->level_up--;
1516
                        break;
1517
                    }
1518

    
1519
                    switch (id) {
1520
                        /* one single index entry ('point') */
1521
                        case MATROSKA_ID_CUETIME: {
1522
                            uint64_t time;
1523
                            if ((res = ebml_read_uint(matroska, &id,
1524
                                                      &time)) < 0)
1525
                                break;
1526
                            idx.time = time * matroska->time_scale;
1527
                            break;
1528
                        }
1529

    
1530
                        /* position in the file + track to which it
1531
                         * belongs */
1532
                        case MATROSKA_ID_CUETRACKPOSITION:
1533
                            if ((res = ebml_read_master(matroska, &id)) < 0)
1534
                                break;
1535

    
1536
                            while (res == 0) {
1537
                                if (!(id = ebml_peek_id (matroska,
1538
                                                    &matroska->level_up))) {
1539
                                    res = AVERROR_IO;
1540
                                    break;
1541
                                } else if (matroska->level_up) {
1542
                                    matroska->level_up--;
1543
                                    break;
1544
                                }
1545

    
1546
                                switch (id) {
1547
                                    /* track number */
1548
                                    case MATROSKA_ID_CUETRACK: {
1549
                                        uint64_t num;
1550
                                        if ((res = ebml_read_uint(matroska,
1551
                                                          &id, &num)) < 0)
1552
                                            break;
1553
                                        idx.track = num;
1554
                                        break;
1555
                                    }
1556

    
1557
                                        /* position in file */
1558
                                    case MATROSKA_ID_CUECLUSTERPOSITION: {
1559
                                        uint64_t num;
1560
                                        if ((res = ebml_read_uint(matroska,
1561
                                                          &id, &num)) < 0)
1562
                                            break;
1563
                                        idx.pos = num+matroska->segment_start;
1564
                                        break;
1565
                                    }
1566

    
1567
                                    default:
1568
                                        av_log(matroska->ctx, AV_LOG_INFO,
1569
                                               "Unknown entry 0x%x in "
1570
                                               "CuesTrackPositions\n", id);
1571
                                        /* fall-through */
1572

    
1573
                                    case EBML_ID_VOID:
1574
                                        res = ebml_read_skip(matroska);
1575
                                        break;
1576
                                }
1577

    
1578
                                if (matroska->level_up) {
1579
                                    matroska->level_up--;
1580
                                    break;
1581
                                }
1582
                            }
1583

    
1584
                            break;
1585

    
1586
                        default:
1587
                            av_log(matroska->ctx, AV_LOG_INFO,
1588
                                   "Unknown entry 0x%x in cuespoint "
1589
                                   "index\n", id);
1590
                            /* fall-through */
1591

    
1592
                        case EBML_ID_VOID:
1593
                            res = ebml_read_skip(matroska);
1594
                            break;
1595
                    }
1596

    
1597
                    if (matroska->level_up) {
1598
                        matroska->level_up--;
1599
                        break;
1600
                    }
1601
                }
1602

    
1603
                /* so let's see if we got what we wanted */
1604
                if (idx.pos   != (uint64_t) -1 &&
1605
                    idx.time  != (uint64_t) -1 &&
1606
                    idx.track != (uint16_t) -1) {
1607
                    if (matroska->num_indexes % 32 == 0) {
1608
                        /* re-allocate bigger index */
1609
                        matroska->index =
1610
                            av_realloc(matroska->index,
1611
                                       (matroska->num_indexes + 32) *
1612
                                       sizeof(MatroskaDemuxIndex));
1613
                    }
1614
                    matroska->index[matroska->num_indexes] = idx;
1615
                    matroska->num_indexes++;
1616
                }
1617
                break;
1618

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

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

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

    
1635
    return res;
1636
}
1637

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

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

    
1653
        switch (id) {
1654
            /* Hm, this is unsupported... */
1655
            default:
1656
                av_log(matroska->ctx, AV_LOG_INFO,
1657
                       "Unknown entry 0x%x in metadata header\n", id);
1658
                /* fall-through */
1659

    
1660
            case EBML_ID_VOID:
1661
                res = ebml_read_skip(matroska);
1662
                break;
1663
        }
1664

    
1665
        if (matroska->level_up) {
1666
            matroska->level_up--;
1667
            break;
1668
        }
1669
    }
1670

    
1671
    return res;
1672
}
1673

    
1674
static int
1675
matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1676
{
1677
    int res = 0;
1678
    uint32_t id;
1679

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

    
1682
    while (res == 0) {
1683
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1684
            res = AVERROR_IO;
1685
            break;
1686
        } else if (matroska->level_up) {
1687
            matroska->level_up--;
1688
            break;
1689
        }
1690

    
1691
        switch (id) {
1692
            case MATROSKA_ID_SEEKENTRY: {
1693
                uint32_t seek_id = 0, peek_id_cache = 0;
1694
                uint64_t seek_pos = (uint64_t) -1, t;
1695

    
1696
                if ((res = ebml_read_master(matroska, &id)) < 0)
1697
                    break;
1698

    
1699
                while (res == 0) {
1700
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1701
                        res = AVERROR_IO;
1702
                        break;
1703
                    } else if (matroska->level_up) {
1704
                        matroska->level_up--;
1705
                        break;
1706
                    }
1707

    
1708
                    switch (id) {
1709
                        case MATROSKA_ID_SEEKID:
1710
                            res = ebml_read_uint(matroska, &id, &t);
1711
                            seek_id = t;
1712
                            break;
1713

    
1714
                        case MATROSKA_ID_SEEKPOSITION:
1715
                            res = ebml_read_uint(matroska, &id, &seek_pos);
1716
                            break;
1717

    
1718
                        default:
1719
                            av_log(matroska->ctx, AV_LOG_INFO,
1720
                                   "Unknown seekhead ID 0x%x\n", id);
1721
                            /* fall-through */
1722

    
1723
                        case EBML_ID_VOID:
1724
                            res = ebml_read_skip(matroska);
1725
                            break;
1726
                    }
1727

    
1728
                    if (matroska->level_up) {
1729
                        matroska->level_up--;
1730
                        break;
1731
                    }
1732
                }
1733

    
1734
                if (!seek_id || seek_pos == (uint64_t) -1) {
1735
                    av_log(matroska->ctx, AV_LOG_INFO,
1736
                           "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1737
                           seek_id, seek_pos);
1738
                    break;
1739
                }
1740

    
1741
                switch (seek_id) {
1742
                    case MATROSKA_ID_CUES:
1743
                    case MATROSKA_ID_TAGS: {
1744
                        uint32_t level_up = matroska->level_up;
1745
                        offset_t before_pos;
1746
                        uint64_t length;
1747
                        MatroskaLevel level;
1748

    
1749
                        /* remember the peeked ID and the current position */
1750
                        peek_id_cache = matroska->peek_id;
1751
                        before_pos = url_ftell(&matroska->ctx->pb);
1752

    
1753
                        /* seek */
1754
                        if ((res = ebml_read_seek(matroska, seek_pos +
1755
                                               matroska->segment_start)) < 0)
1756
                            return res;
1757

    
1758
                        /* we don't want to lose our seekhead level, so we add
1759
                         * a dummy. This is a crude hack. */
1760
                        if (matroska->num_levels == EBML_MAX_DEPTH) {
1761
                            av_log(matroska->ctx, AV_LOG_INFO,
1762
                                   "Max EBML element depth (%d) reached, "
1763
                                   "cannot parse further.\n", EBML_MAX_DEPTH);
1764
                            return AVERROR_UNKNOWN;
1765
                        }
1766

    
1767
                        level.start = 0;
1768
                        level.length = (uint64_t)-1;
1769
                        matroska->levels[matroska->num_levels] = level;
1770
                        matroska->num_levels++;
1771

    
1772
                        /* check ID */
1773
                        if (!(id = ebml_peek_id (matroska,
1774
                                                 &matroska->level_up)))
1775
                            goto finish;
1776
                        if (id != seek_id) {
1777
                            av_log(matroska->ctx, AV_LOG_INFO,
1778
                                   "We looked for ID=0x%x but got "
1779
                                   "ID=0x%x (pos=%"PRIu64")",
1780
                                   seek_id, id, seek_pos +
1781
                                   matroska->segment_start);
1782
                            goto finish;
1783
                        }
1784

    
1785
                        /* read master + parse */
1786
                        if ((res = ebml_read_master(matroska, &id)) < 0)
1787
                            goto finish;
1788
                        switch (id) {
1789
                            case MATROSKA_ID_CUES:
1790
                                if (!(res = matroska_parse_index(matroska)) ||
1791
                                    url_feof(&matroska->ctx->pb)) {
1792
                                    matroska->index_parsed = 1;
1793
                                    res = 0;
1794
                                }
1795
                                break;
1796
                            case MATROSKA_ID_TAGS:
1797
                                if (!(res = matroska_parse_metadata(matroska)) ||
1798
                                   url_feof(&matroska->ctx->pb)) {
1799
                                    matroska->metadata_parsed = 1;
1800
                                    res = 0;
1801
                                }
1802
                                break;
1803
                        }
1804

    
1805
                    finish:
1806
                        /* remove dummy level */
1807
                        while (matroska->num_levels) {
1808
                            matroska->num_levels--;
1809
                            length =
1810
                                matroska->levels[matroska->num_levels].length;
1811
                            if (length == (uint64_t)-1)
1812
                                break;
1813
                        }
1814

    
1815
                        /* seek back */
1816
                        if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1817
                            return res;
1818
                        matroska->peek_id = peek_id_cache;
1819
                        matroska->level_up = level_up;
1820
                        break;
1821
                    }
1822

    
1823
                    default:
1824
                        av_log(matroska->ctx, AV_LOG_INFO,
1825
                               "Ignoring seekhead entry for ID=0x%x\n",
1826
                               seek_id);
1827
                        break;
1828
                }
1829

    
1830
                break;
1831
            }
1832

    
1833
            default:
1834
                av_log(matroska->ctx, AV_LOG_INFO,
1835
                       "Unknown seekhead ID 0x%x\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
#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
1853

    
1854
static int
1855
matroska_aac_profile (char *codec_id)
1856
{
1857
    static const char *aac_profiles[] = {
1858
        "MAIN", "LC", "SSR"
1859
    };
1860
    int profile;
1861

    
1862
    for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
1863
        if (strstr(codec_id, aac_profiles[profile]))
1864
            break;
1865
    return profile + 1;
1866
}
1867

    
1868
static int
1869
matroska_aac_sri (int samplerate)
1870
{
1871
    static const int aac_sample_rates[] = {
1872
        96000, 88200, 64000, 48000, 44100, 32000,
1873
        24000, 22050, 16000, 12000, 11025,  8000,
1874
    };
1875
    int sri;
1876

    
1877
    for (sri=0; sri<ARRAY_SIZE(aac_sample_rates); sri++)
1878
        if (aac_sample_rates[sri] == samplerate)
1879
            break;
1880
    return sri;
1881
}
1882

    
1883
static int
1884
matroska_read_header (AVFormatContext    *s,
1885
                      AVFormatParameters *ap)
1886
{
1887
    MatroskaDemuxContext *matroska = s->priv_data;
1888
    char *doctype;
1889
    int version, last_level, res = 0;
1890
    uint32_t id;
1891

    
1892
    matroska->ctx = s;
1893

    
1894
    /* First read the EBML header. */
1895
    doctype = NULL;
1896
    if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
1897
        return res;
1898
    if ((doctype == NULL) || strcmp(doctype, "matroska")) {
1899
        av_log(matroska->ctx, AV_LOG_ERROR,
1900
               "Wrong EBML doctype ('%s' != 'matroska').\n",
1901
               doctype ? doctype : "(none)");
1902
        if (doctype)
1903
            av_free(doctype);
1904
        return AVERROR_NOFMT;
1905
    }
1906
    av_free(doctype);
1907
    if (version > 2) {
1908
        av_log(matroska->ctx, AV_LOG_ERROR,
1909
               "Matroska demuxer version 2 too old for file version %d\n",
1910
               version);
1911
        return AVERROR_NOFMT;
1912
    }
1913

    
1914
    /* The next thing is a segment. */
1915
    while (1) {
1916
        if (!(id = ebml_peek_id(matroska, &last_level)))
1917
            return AVERROR_IO;
1918
        if (id == MATROSKA_ID_SEGMENT)
1919
            break;
1920

    
1921
        /* oi! */
1922
        av_log(matroska->ctx, AV_LOG_INFO,
1923
               "Expected a Segment ID (0x%x), but received 0x%x!\n",
1924
               MATROSKA_ID_SEGMENT, id);
1925
        if ((res = ebml_read_skip(matroska)) < 0)
1926
            return res;
1927
    }
1928

    
1929
    /* We now have a Matroska segment.
1930
     * Seeks are from the beginning of the segment,
1931
     * after the segment ID/length. */
1932
    if ((res = ebml_read_master(matroska, &id)) < 0)
1933
        return res;
1934
    matroska->segment_start = url_ftell(&s->pb);
1935

    
1936
    matroska->time_scale = 1000000;
1937
    /* we've found our segment, start reading the different contents in here */
1938
    while (res == 0) {
1939
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1940
            res = AVERROR_IO;
1941
            break;
1942
        } else if (matroska->level_up) {
1943
            matroska->level_up--;
1944
            break;
1945
        }
1946

    
1947
        switch (id) {
1948
            /* stream info */
1949
            case MATROSKA_ID_INFO: {
1950
                if ((res = ebml_read_master(matroska, &id)) < 0)
1951
                    break;
1952
                res = matroska_parse_info(matroska);
1953
                break;
1954
            }
1955

    
1956
            /* track info headers */
1957
            case MATROSKA_ID_TRACKS: {
1958
                if ((res = ebml_read_master(matroska, &id)) < 0)
1959
                    break;
1960
                res = matroska_parse_tracks(matroska);
1961
                break;
1962
            }
1963

    
1964
            /* stream index */
1965
            case MATROSKA_ID_CUES: {
1966
                if (!matroska->index_parsed) {
1967
                    if ((res = ebml_read_master(matroska, &id)) < 0)
1968
                        break;
1969
                    res = matroska_parse_index(matroska);
1970
                } else
1971
                    res = ebml_read_skip(matroska);
1972
                break;
1973
            }
1974

    
1975
            /* metadata */
1976
            case MATROSKA_ID_TAGS: {
1977
                if (!matroska->metadata_parsed) {
1978
                    if ((res = ebml_read_master(matroska, &id)) < 0)
1979
                        break;
1980
                    res = matroska_parse_metadata(matroska);
1981
                } else
1982
                    res = ebml_read_skip(matroska);
1983
                break;
1984
            }
1985

    
1986
            /* file index (if seekable, seek to Cues/Tags to parse it) */
1987
            case MATROSKA_ID_SEEKHEAD: {
1988
                if ((res = ebml_read_master(matroska, &id)) < 0)
1989
                    break;
1990
                res = matroska_parse_seekhead(matroska);
1991
                break;
1992
            }
1993

    
1994
            case MATROSKA_ID_CLUSTER: {
1995
                /* Do not read the master - this will be done in the next
1996
                 * call to matroska_read_packet. */
1997
                res = 1;
1998
                break;
1999
            }
2000

    
2001
            default:
2002
                av_log(matroska->ctx, AV_LOG_INFO,
2003
                       "Unknown matroska file header ID 0x%x\n", id);
2004
            /* fall-through */
2005

    
2006
            case EBML_ID_VOID:
2007
                res = ebml_read_skip(matroska);
2008
                break;
2009
        }
2010

    
2011
        if (matroska->level_up) {
2012
            matroska->level_up--;
2013
            break;
2014
        }
2015
    }
2016

    
2017
    /* Have we found a cluster? */
2018
    if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2019
        int i, j;
2020
        MatroskaTrack *track;
2021
        AVStream *st;
2022

    
2023
        for (i = 0; i < matroska->num_tracks; i++) {
2024
            enum CodecID codec_id = CODEC_ID_NONE;
2025
            uint8_t *extradata = NULL;
2026
            int extradata_size = 0;
2027
            int extradata_offset = 0;
2028
            track = matroska->tracks[i];
2029
            track->stream_index = -1;
2030

    
2031
            /* Apply some sanity checks. */
2032
            if (track->codec_id == NULL)
2033
                continue;
2034

    
2035
            for(j=0; ff_mkv_codec_tags[j].str; j++){
2036
                if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2037
                            strlen(ff_mkv_codec_tags[j].str))){
2038
                    codec_id= ff_mkv_codec_tags[j].id;
2039
                    break;
2040
                }
2041
            }
2042

    
2043
            /* Set the FourCC from the CodecID. */
2044
            /* This is the MS compatibility mode which stores a
2045
             * BITMAPINFOHEADER in the CodecPrivate. */
2046
            if (!strcmp(track->codec_id,
2047
                        MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2048
                (track->codec_priv_size >= 40) &&
2049
                (track->codec_priv != NULL)) {
2050
                MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2051

    
2052
                /* Offset of biCompression. Stored in LE. */
2053
                vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2054
                codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2055

    
2056
            }
2057

    
2058
            /* This is the MS compatibility mode which stores a
2059
             * WAVEFORMATEX in the CodecPrivate. */
2060
            else if (!strcmp(track->codec_id,
2061
                             MATROSKA_CODEC_ID_AUDIO_ACM) &&
2062
                (track->codec_priv_size >= 18) &&
2063
                (track->codec_priv != NULL)) {
2064
                uint16_t tag;
2065

    
2066
                /* Offset of wFormatTag. Stored in LE. */
2067
                tag = AV_RL16(track->codec_priv);
2068
                codec_id = codec_get_id(codec_wav_tags, tag);
2069

    
2070
            }
2071

    
2072
            else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2073
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2074
                int profile = matroska_aac_profile(track->codec_id);
2075
                int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2076
                extradata = av_malloc(5);
2077
                if (extradata == NULL)
2078
                    return AVERROR_NOMEM;
2079
                extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2080
                extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2081
                if (strstr(track->codec_id, "SBR")) {
2082
                    sri = matroska_aac_sri(audiotrack->samplerate);
2083
                    extradata[2] = 0x56;
2084
                    extradata[3] = 0xE5;
2085
                    extradata[4] = 0x80 | (sri<<3);
2086
                    extradata_size = 5;
2087
                } else {
2088
                    extradata_size = 2;
2089
                }
2090
                track->default_duration = 1024*1000 / audiotrack->internal_samplerate;
2091
            }
2092

    
2093
            else if (codec_id == CODEC_ID_TTA) {
2094
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2095
                ByteIOContext b;
2096
                extradata_size = 30;
2097
                extradata = av_mallocz(extradata_size);
2098
                if (extradata == NULL)
2099
                    return AVERROR_NOMEM;
2100
                init_put_byte(&b, extradata, extradata_size, 1,
2101
                              NULL, NULL, NULL, NULL);
2102
                put_buffer(&b, (uint8_t *) "TTA1", 4);
2103
                put_le16(&b, 1);
2104
                put_le16(&b, audiotrack->channels);
2105
                put_le16(&b, audiotrack->bitdepth);
2106
                put_le32(&b, audiotrack->samplerate);
2107
                put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2108
            }
2109

    
2110
            else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2111
                     codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2112
                extradata_offset = 26;
2113
                track->codec_priv_size -= extradata_offset;
2114
                track->flags |= MATROSKA_TRACK_REAL_V;
2115
            }
2116

    
2117
            else if (codec_id == CODEC_ID_RA_144) {
2118
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2119
                audiotrack->samplerate = 8000;
2120
                audiotrack->channels = 1;
2121
            }
2122

    
2123
            else if (codec_id == CODEC_ID_RA_288 ||
2124
                     codec_id == CODEC_ID_COOK ||
2125
                     codec_id == CODEC_ID_ATRAC3) {
2126
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2127
                ByteIOContext b;
2128

    
2129
                init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2130
                              NULL, NULL, NULL, NULL);
2131
                url_fskip(&b, 24);
2132
                audiotrack->coded_framesize = get_be32(&b);
2133
                url_fskip(&b, 12);
2134
                audiotrack->sub_packet_h    = get_be16(&b);
2135
                audiotrack->frame_size      = get_be16(&b);
2136
                audiotrack->sub_packet_size = get_be16(&b);
2137
                audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2138
                if (codec_id == CODEC_ID_RA_288) {
2139
                    audiotrack->block_align = audiotrack->coded_framesize;
2140
                    track->codec_priv_size = 0;
2141
                } else {
2142
                    audiotrack->block_align = audiotrack->sub_packet_size;
2143
                    extradata_offset = 78;
2144
                    track->codec_priv_size -= extradata_offset;
2145
                }
2146
            }
2147

    
2148
            if (codec_id == CODEC_ID_NONE) {
2149
                av_log(matroska->ctx, AV_LOG_INFO,
2150
                       "Unknown/unsupported CodecID %s.\n",
2151
                       track->codec_id);
2152
            }
2153

    
2154
            track->stream_index = matroska->num_streams;
2155

    
2156
            matroska->num_streams++;
2157
            st = av_new_stream(s, track->stream_index);
2158
            if (st == NULL)
2159
                return AVERROR_NOMEM;
2160
            av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2161

    
2162
            st->codec->codec_id = codec_id;
2163
            st->start_time = 0;
2164
            if (strcmp(track->language, "und"))
2165
                strcpy(st->language, track->language);
2166

    
2167
            if (track->default_duration)
2168
                av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2169
                          track->default_duration, 1000, 30000);
2170

    
2171
            if(extradata){
2172
                st->codec->extradata = extradata;
2173
                st->codec->extradata_size = extradata_size;
2174
            } else if(track->codec_priv && track->codec_priv_size > 0){
2175
                st->codec->extradata = av_malloc(track->codec_priv_size);
2176
                if(st->codec->extradata == NULL)
2177
                    return AVERROR_NOMEM;
2178
                st->codec->extradata_size = track->codec_priv_size;
2179
                memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2180
                       track->codec_priv_size);
2181
            }
2182

    
2183
            if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2184
                MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2185

    
2186
                st->codec->codec_type = CODEC_TYPE_VIDEO;
2187
                st->codec->codec_tag = videotrack->fourcc;
2188
                st->codec->width = videotrack->pixel_width;
2189
                st->codec->height = videotrack->pixel_height;
2190
                if (videotrack->display_width == 0)
2191
                    videotrack->display_width= videotrack->pixel_width;
2192
                if (videotrack->display_height == 0)
2193
                    videotrack->display_height= videotrack->pixel_height;
2194
                av_reduce(&st->codec->sample_aspect_ratio.num,
2195
                          &st->codec->sample_aspect_ratio.den,
2196
                          st->codec->height * videotrack->display_width,
2197
                          st->codec-> width * videotrack->display_height,
2198
                          255);
2199
                st->need_parsing = AVSTREAM_PARSE_HEADERS;
2200
            } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2201
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2202

    
2203
                st->codec->codec_type = CODEC_TYPE_AUDIO;
2204
                st->codec->sample_rate = audiotrack->samplerate;
2205
                st->codec->channels = audiotrack->channels;
2206
                st->codec->block_align = audiotrack->block_align;
2207
            } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2208
                st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2209
            }
2210

    
2211
            /* What do we do with private data? E.g. for Vorbis. */
2212
        }
2213
        res = 0;
2214
    }
2215

    
2216
    if (matroska->index_parsed) {
2217
        int i, track, stream;
2218
        for (i=0; i<matroska->num_indexes; i++) {
2219
            MatroskaDemuxIndex *idx = &matroska->index[i];
2220
            track = matroska_find_track_by_num(matroska, idx->track);
2221
            stream = matroska->tracks[track]->stream_index;
2222
            if (stream >= 0)
2223
                av_add_index_entry(matroska->ctx->streams[stream],
2224
                                   idx->pos, idx->time/matroska->time_scale,
2225
                                   0, 0, AVINDEX_KEYFRAME);
2226
        }
2227
    }
2228

    
2229
    return res;
2230
}
2231

    
2232
static inline int
2233
rv_offset(uint8_t *data, int slice, int slices)
2234
{
2235
    return AV_RL32(data+8*slice+4) + 8*slices;
2236
}
2237

    
2238
static int
2239
matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2240
                     int64_t pos, uint64_t cluster_time, uint64_t duration,
2241
                     int is_keyframe, int is_bframe)
2242
{
2243
    int res = 0;
2244
    int track;
2245
    AVStream *st;
2246
    AVPacket *pkt;
2247
    uint8_t *origdata = data;
2248
    int16_t block_time;
2249
    uint32_t *lace_size = NULL;
2250
    int n, flags, laces = 0;
2251
    uint64_t num;
2252

    
2253
    /* first byte(s): tracknum */
2254
    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2255
        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2256
        av_free(origdata);
2257
        return res;
2258
    }
2259
    data += n;
2260
    size -= n;
2261

    
2262
    /* fetch track from num */
2263
    track = matroska_find_track_by_num(matroska, num);
2264
    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2265
        av_log(matroska->ctx, AV_LOG_INFO,
2266
               "Invalid stream %d or size %u\n", track, size);
2267
        av_free(origdata);
2268
        return res;
2269
    }
2270
    if (matroska->tracks[track]->stream_index < 0)
2271
        return res;
2272
    st = matroska->ctx->streams[matroska->tracks[track]->stream_index];
2273
    if (st->discard >= AVDISCARD_ALL) {
2274
        av_free(origdata);
2275
        return res;
2276
    }
2277
    if (duration == AV_NOPTS_VALUE)
2278
        duration = matroska->tracks[track]->default_duration;
2279

    
2280
    /* block_time (relative to cluster time) */
2281
    block_time = AV_RB16(data);
2282
    data += 2;
2283
    flags = *data++;
2284
    size -= 3;
2285
    if (is_keyframe == -1)
2286
        is_keyframe = flags & 1 ? PKT_FLAG_KEY : 0;
2287

    
2288
    if (matroska->skip_to_keyframe) {
2289
        if (!is_keyframe || st != matroska->skip_to_stream)
2290
            return res;
2291
        matroska->skip_to_keyframe = 0;
2292
    }
2293

    
2294
    switch ((flags & 0x06) >> 1) {
2295
        case 0x0: /* no lacing */
2296
            laces = 1;
2297
            lace_size = av_mallocz(sizeof(int));
2298
            lace_size[0] = size;
2299
            break;
2300

    
2301
        case 0x1: /* xiph lacing */
2302
        case 0x2: /* fixed-size lacing */
2303
        case 0x3: /* EBML lacing */
2304
            if (size == 0) {
2305
                res = -1;
2306
                break;
2307
            }
2308
            laces = (*data) + 1;
2309
            data += 1;
2310
            size -= 1;
2311
            lace_size = av_mallocz(laces * sizeof(int));
2312

    
2313
            switch ((flags & 0x06) >> 1) {
2314
                case 0x1: /* xiph lacing */ {
2315
                    uint8_t temp;
2316
                    uint32_t total = 0;
2317
                    for (n = 0; res == 0 && n < laces - 1; n++) {
2318
                        while (1) {
2319
                            if (size == 0) {
2320
                                res = -1;
2321
                                break;
2322
                            }
2323
                            temp = *data;
2324
                            lace_size[n] += temp;
2325
                            data += 1;
2326
                            size -= 1;
2327
                            if (temp != 0xff)
2328
                                break;
2329
                        }
2330
                        total += lace_size[n];
2331
                    }
2332
                    lace_size[n] = size - total;
2333
                    break;
2334
                }
2335

    
2336
                case 0x2: /* fixed-size lacing */
2337
                    for (n = 0; n < laces; n++)
2338
                        lace_size[n] = size / laces;
2339
                    break;
2340

    
2341
                case 0x3: /* EBML lacing */ {
2342
                    uint32_t total;
2343
                    n = matroska_ebmlnum_uint(data, size, &num);
2344
                    if (n < 0) {
2345
                        av_log(matroska->ctx, AV_LOG_INFO,
2346
                               "EBML block data error\n");
2347
                        break;
2348
                    }
2349
                    data += n;
2350
                    size -= n;
2351
                    total = lace_size[0] = num;
2352
                    for (n = 1; res == 0 && n < laces - 1; n++) {
2353
                        int64_t snum;
2354
                        int r;
2355
                        r = matroska_ebmlnum_sint (data, size, &snum);
2356
                        if (r < 0) {
2357
                            av_log(matroska->ctx, AV_LOG_INFO,
2358
                                   "EBML block data error\n");
2359
                            break;
2360
                        }
2361
                        data += r;
2362
                        size -= r;
2363
                        lace_size[n] = lace_size[n - 1] + snum;
2364
                        total += lace_size[n];
2365
                    }
2366
                    lace_size[n] = size - total;
2367
                    break;
2368
                }
2369
            }
2370
            break;
2371
    }
2372

    
2373
    if (res == 0) {
2374
        int real_v = matroska->tracks[track]->flags & MATROSKA_TRACK_REAL_V;
2375
        uint64_t timecode = AV_NOPTS_VALUE;
2376

    
2377
        if (cluster_time != (uint64_t)-1 && cluster_time + block_time >= 0)
2378
            timecode = cluster_time + block_time;
2379

    
2380
        for (n = 0; n < laces; n++) {
2381
            int slice, slices = 1;
2382

    
2383
            if (real_v) {
2384
                slices = *data++ + 1;
2385
                lace_size[n]--;
2386
            }
2387

    
2388
            for (slice=0; slice<slices; slice++) {
2389
                int slice_size, slice_offset = 0;
2390
                if (real_v)
2391
                    slice_offset = rv_offset(data, slice, slices);
2392
                if (slice+1 == slices)
2393
                    slice_size = lace_size[n] - slice_offset;
2394
                else
2395
                    slice_size = rv_offset(data, slice+1, slices) - slice_offset;
2396

    
2397
                if (st->codec->codec_id == CODEC_ID_RA_288 ||
2398
                    st->codec->codec_id == CODEC_ID_COOK ||
2399
                    st->codec->codec_id == CODEC_ID_ATRAC3) {
2400
                    MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2401
                    int a = st->codec->block_align;
2402
                    int sps = audiotrack->sub_packet_size;
2403
                    int cfs = audiotrack->coded_framesize;
2404
                    int h = audiotrack->sub_packet_h;
2405
                    int y = audiotrack->sub_packet_cnt;
2406
                    int w = audiotrack->frame_size;
2407
                    int x;
2408

    
2409
                    if (!audiotrack->pkt_cnt) {
2410
                        if (st->codec->codec_id == CODEC_ID_RA_288)
2411
                            for (x=0; x<h/2; x++)
2412
                                memcpy(audiotrack->buf+x*2*w+y*cfs,
2413
                                       data+x*cfs, cfs);
2414
                        else
2415
                            for (x=0; x<w/sps; x++)
2416
                                memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2417

    
2418
                        if (++audiotrack->sub_packet_cnt >= h) {
2419
                            audiotrack->sub_packet_cnt = 0;
2420
                            audiotrack->pkt_cnt = h*w / a;
2421
                        }
2422
                    }
2423
                    while (audiotrack->pkt_cnt) {
2424
                        pkt = av_mallocz(sizeof(AVPacket));
2425
                        av_new_packet(pkt, a);
2426
                        memcpy(pkt->data, audiotrack->buf
2427
                               + a * (h*w / a - audiotrack->pkt_cnt--), a);
2428
                        pkt->pos = pos;
2429
                        pkt->stream_index = matroska->tracks[track]->stream_index;
2430
                        matroska_queue_packet(matroska, pkt);
2431
                    }
2432
                } else {
2433
                    pkt = av_mallocz(sizeof(AVPacket));
2434
                    /* XXX: prevent data copy... */
2435
                    if (av_new_packet(pkt, slice_size) < 0) {
2436
                        res = AVERROR_NOMEM;
2437
                        n = laces-1;
2438
                        break;
2439
                    }
2440
                    memcpy (pkt->data, data+slice_offset, slice_size);
2441

    
2442
                    if (n == 0)
2443
                        pkt->flags = is_keyframe;
2444
                    pkt->stream_index = matroska->tracks[track]->stream_index;
2445

    
2446
                    pkt->pts = timecode;
2447
                    pkt->pos = pos;
2448
                    pkt->duration = duration;
2449

    
2450
                    matroska_queue_packet(matroska, pkt);
2451
                }
2452

    
2453
                if (timecode != AV_NOPTS_VALUE)
2454
                    timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2455
            }
2456
            data += lace_size[n];
2457
        }
2458
    }
2459

    
2460
    av_free(lace_size);
2461
    av_free(origdata);
2462
    return res;
2463
}
2464

    
2465
static int
2466
matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2467
                           uint64_t              cluster_time)
2468
{
2469
    int res = 0;
2470
    uint32_t id;
2471
    int is_bframe = 0;
2472
    int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2473
    uint64_t duration = AV_NOPTS_VALUE;
2474
    uint8_t *data;
2475
    int size = 0;
2476
    int64_t pos = 0;
2477

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

    
2480
    while (res == 0) {
2481
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2482
            res = AVERROR_IO;
2483
            break;
2484
        } else if (matroska->level_up) {
2485
            matroska->level_up--;
2486
            break;
2487
        }
2488

    
2489
        switch (id) {
2490
            /* one block inside the group. Note, block parsing is one
2491
             * of the harder things, so this code is a bit complicated.
2492
             * See http://www.matroska.org/ for documentation. */
2493
            case MATROSKA_ID_BLOCK: {
2494
                pos = url_ftell(&matroska->ctx->pb);
2495
                res = ebml_read_binary(matroska, &id, &data, &size);
2496
                break;
2497
            }
2498

    
2499
            case MATROSKA_ID_BLOCKDURATION: {
2500
                if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2501
                    break;
2502
                break;
2503
            }
2504

    
2505
            case MATROSKA_ID_BLOCKREFERENCE: {
2506
                int64_t num;
2507
                /* We've found a reference, so not even the first frame in
2508
                 * the lace is a key frame. */
2509
                is_keyframe = 0;
2510
                if (last_num_packets != matroska->num_packets)
2511
                    matroska->packets[last_num_packets]->flags = 0;
2512
                if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
2513
                    break;
2514
                if (num > 0)
2515
                    is_bframe = 1;
2516
                break;
2517
            }
2518

    
2519
            default:
2520
                av_log(matroska->ctx, AV_LOG_INFO,
2521
                       "Unknown entry 0x%x in blockgroup data\n", id);
2522
                /* fall-through */
2523

    
2524
            case EBML_ID_VOID:
2525
                res = ebml_read_skip(matroska);
2526
                break;
2527
        }
2528

    
2529
        if (matroska->level_up) {
2530
            matroska->level_up--;
2531
            break;
2532
        }
2533
    }
2534

    
2535
    if (res)
2536
        return res;
2537

    
2538
    if (size > 0)
2539
        res = matroska_parse_block(matroska, data, size, pos, cluster_time,
2540
                                   duration, is_keyframe, is_bframe);
2541

    
2542
    return res;
2543
}
2544

    
2545
static int
2546
matroska_parse_cluster (MatroskaDemuxContext *matroska)
2547
{
2548
    int res = 0;
2549
    uint32_t id;
2550
    uint64_t cluster_time = 0;
2551
    uint8_t *data;
2552
    int64_t pos;
2553
    int size;
2554

    
2555
    av_log(matroska->ctx, AV_LOG_DEBUG,
2556
           "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
2557

    
2558
    while (res == 0) {
2559
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2560
            res = AVERROR_IO;
2561
            break;
2562
        } else if (matroska->level_up) {
2563
            matroska->level_up--;
2564
            break;
2565
        }
2566

    
2567
        switch (id) {
2568
            /* cluster timecode */
2569
            case MATROSKA_ID_CLUSTERTIMECODE: {
2570
                uint64_t num;
2571
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2572
                    break;
2573
                cluster_time = num;
2574
                break;
2575
            }
2576

    
2577
                /* a group of blocks inside a cluster */
2578
            case MATROSKA_ID_BLOCKGROUP:
2579
                if ((res = ebml_read_master(matroska, &id)) < 0)
2580
                    break;
2581
                res = matroska_parse_blockgroup(matroska, cluster_time);
2582
                break;
2583

    
2584
            case MATROSKA_ID_SIMPLEBLOCK:
2585
                pos = url_ftell(&matroska->ctx->pb);
2586
                res = ebml_read_binary(matroska, &id, &data, &size);
2587
                if (res == 0)
2588
                    res = matroska_parse_block(matroska, data, size, pos,
2589
                                               cluster_time, AV_NOPTS_VALUE,
2590
                                               -1, 0);
2591
                break;
2592

    
2593
            default:
2594
                av_log(matroska->ctx, AV_LOG_INFO,
2595
                       "Unknown entry 0x%x in cluster data\n", id);
2596
                /* fall-through */
2597

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

    
2603
        if (matroska->level_up) {
2604
            matroska->level_up--;
2605
            break;
2606
        }
2607
    }
2608

    
2609
    return res;
2610
}
2611

    
2612
static int
2613
matroska_read_packet (AVFormatContext *s,
2614
                      AVPacket        *pkt)
2615
{
2616
    MatroskaDemuxContext *matroska = s->priv_data;
2617
    int res;
2618
    uint32_t id;
2619

    
2620
    /* Read stream until we have a packet queued. */
2621
    while (matroska_deliver_packet(matroska, pkt)) {
2622

    
2623
        /* Have we already reached the end? */
2624
        if (matroska->done)
2625
            return AVERROR_IO;
2626

    
2627
        res = 0;
2628
        while (res == 0) {
2629
            if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2630
                return AVERROR_IO;
2631
            } else if (matroska->level_up) {
2632
                matroska->level_up--;
2633
                break;
2634
            }
2635

    
2636
            switch (id) {
2637
                case MATROSKA_ID_CLUSTER:
2638
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2639
                        break;
2640
                    if ((res = matroska_parse_cluster(matroska)) == 0)
2641
                        res = 1; /* Parsed one cluster, let's get out. */
2642
                    break;
2643

    
2644
                default:
2645
                case EBML_ID_VOID:
2646
                    res = ebml_read_skip(matroska);
2647
                    break;
2648
            }
2649

    
2650
            if (matroska->level_up) {
2651
                matroska->level_up--;
2652
                break;
2653
            }
2654
        }
2655

    
2656
        if (res == -1)
2657
            matroska->done = 1;
2658
    }
2659

    
2660
    return 0;
2661
}
2662

    
2663
static int
2664
matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
2665
                    int flags)
2666
{
2667
    MatroskaDemuxContext *matroska = s->priv_data;
2668
    AVStream *st = s->streams[stream_index];
2669
    int index;
2670

    
2671
    /* find index entry */
2672
    index = av_index_search_timestamp(st, timestamp, flags);
2673
    if (index < 0)
2674
        return 0;
2675

    
2676
    /* do the seek */
2677
    url_fseek(&s->pb, st->index_entries[index].pos, SEEK_SET);
2678
    matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
2679
    matroska->skip_to_stream = st;
2680
    matroska->num_packets = 0;
2681
    matroska->peek_id = 0;
2682
    return 0;
2683
}
2684

    
2685
static int
2686
matroska_read_close (AVFormatContext *s)
2687
{
2688
    MatroskaDemuxContext *matroska = s->priv_data;
2689
    int n = 0;
2690

    
2691
    av_free(matroska->writing_app);
2692
    av_free(matroska->muxing_app);
2693
    av_free(matroska->index);
2694

    
2695
    if (matroska->packets != NULL) {
2696
        for (n = 0; n < matroska->num_packets; n++) {
2697
            av_free_packet(matroska->packets[n]);
2698
            av_free(matroska->packets[n]);
2699
        }
2700
        av_free(matroska->packets);
2701
    }
2702

    
2703
    for (n = 0; n < matroska->num_tracks; n++) {
2704
        MatroskaTrack *track = matroska->tracks[n];
2705
        av_free(track->codec_id);
2706
        av_free(track->codec_name);
2707
        av_free(track->codec_priv);
2708
        av_free(track->name);
2709

    
2710
        if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2711
            MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2712
            av_free(audiotrack->buf);
2713
        }
2714

    
2715
        av_free(track);
2716
    }
2717

    
2718
    return 0;
2719
}
2720

    
2721
AVInputFormat matroska_demuxer = {
2722
    "matroska",
2723
    "Matroska file format",
2724
    sizeof(MatroskaDemuxContext),
2725
    matroska_probe,
2726
    matroska_read_header,
2727
    matroska_read_packet,
2728
    matroska_read_close,
2729
    matroska_read_seek,
2730
};