Statistics
| Branch: | Revision:

ffmpeg / libavformat / matroskadec.c @ 243cc4c3

History | View | Annotate | Download (87.2 KB)

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

    
22
/**
23
 * @file 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
    int ass;
99
    //..
100
} MatroskaSubtitleTrack;
101

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

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

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

    
117
typedef struct MatroskaDemuxContext {
118
    AVFormatContext *ctx;
119

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
194
    return num;
195
}
196

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

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

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

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

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

    
247
    *number = total;
248

    
249
    return read;
250
}
251

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

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

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

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

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

    
282
    return read;
283
}
284

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

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

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

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

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

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

    
316
    return id;
317
}
318

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

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

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

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

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

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

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

    
353
    url_fskip(pb, length);
354

    
355
    return 0;
356
}
357

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

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

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

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

    
389
    return 0;
390
}
391

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

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

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

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

    
428
    return 0;
429
}
430

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

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

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

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

    
462
    return 0;
463
}
464

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

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

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

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

    
498
    return 0;
499
}
500

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

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

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

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

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

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

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

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

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

    
558
    return 0;
559
}
560

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

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

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

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

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

    
594
    return 0;
595
}
596

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

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

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

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

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

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

    
638
    return read;
639
}
640

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

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

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

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

    
663
    return res;
664
}
665

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

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

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

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

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

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

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

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

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

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

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

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

    
748
            case EBML_ID_DOCTYPE: {
749
                char *text;
750

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

    
762
            case EBML_ID_DOCTYPEREADVERSION: {
763
                uint64_t num;
764

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

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

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

    
787
    return 0;
788
}
789

    
790

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

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

    
801
    return -1;
802
}
803

    
804

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

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

    
830
    return -1;
831
}
832

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

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

    
849
/*
850
 * Free all packets in our internal queue.
851
 */
852
static void
853
matroska_clear_queue (MatroskaDemuxContext *matroska)
854
{
855
    if (matroska->packets) {
856
        int n;
857
        for (n = 0; n < matroska->num_packets; n++) {
858
            av_free_packet(matroska->packets[n]);
859
            av_free(matroska->packets[n]);
860
        }
861
        av_free(matroska->packets);
862
        matroska->packets = NULL;
863
    }
864
}
865

    
866

    
867
/*
868
 * Autodetecting...
869
 */
870

    
871
static int
872
matroska_probe (AVProbeData *p)
873
{
874
    uint64_t total = 0;
875
    int len_mask = 0x80, size = 1, n = 1;
876
    uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
877

    
878
    /* ebml header? */
879
    if (AV_RB32(p->buf) != EBML_ID_HEADER)
880
        return 0;
881

    
882
    /* length of header */
883
    total = p->buf[4];
884
    while (size <= 8 && !(total & len_mask)) {
885
        size++;
886
        len_mask >>= 1;
887
    }
888
    if (size > 8)
889
      return 0;
890
    total &= (len_mask - 1);
891
    while (n < size)
892
        total = (total << 8) | p->buf[4 + n++];
893

    
894
    /* does the probe data contain the whole header? */
895
    if (p->buf_size < 4 + size + total)
896
      return 0;
897

    
898
    /* the header must contain the document type 'matroska'. For now,
899
     * we don't parse the whole header but simply check for the
900
     * availability of that array of characters inside the header.
901
     * Not fully fool-proof, but good enough. */
902
    for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
903
        if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
904
            return AVPROBE_SCORE_MAX;
905

    
906
    return 0;
907
}
908

    
909
/*
910
 * From here on, it's all XML-style DTD stuff... Needs no comments.
911
 */
912

    
913
static int
914
matroska_parse_info (MatroskaDemuxContext *matroska)
915
{
916
    int res = 0;
917
    uint32_t id;
918

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

    
921
    while (res == 0) {
922
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
923
            res = AVERROR(EIO);
924
            break;
925
        } else if (matroska->level_up) {
926
            matroska->level_up--;
927
            break;
928
        }
929

    
930
        switch (id) {
931
            /* cluster timecode */
932
            case MATROSKA_ID_TIMECODESCALE: {
933
                uint64_t num;
934
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
935
                    break;
936
                matroska->time_scale = num;
937
                break;
938
            }
939

    
940
            case MATROSKA_ID_DURATION: {
941
                double num;
942
                if ((res = ebml_read_float(matroska, &id, &num)) < 0)
943
                    break;
944
                matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
945
                break;
946
            }
947

    
948
            case MATROSKA_ID_TITLE: {
949
                char *text;
950
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
951
                    break;
952
                strncpy(matroska->ctx->title, text,
953
                        sizeof(matroska->ctx->title)-1);
954
                av_free(text);
955
                break;
956
            }
957

    
958
            case MATROSKA_ID_WRITINGAPP: {
959
                char *text;
960
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
961
                    break;
962
                matroska->writing_app = text;
963
                break;
964
            }
965

    
966
            case MATROSKA_ID_MUXINGAPP: {
967
                char *text;
968
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
969
                    break;
970
                matroska->muxing_app = text;
971
                break;
972
            }
973

    
974
            case MATROSKA_ID_DATEUTC: {
975
                int64_t time;
976
                if ((res = ebml_read_date(matroska, &id, &time)) < 0)
977
                    break;
978
                matroska->created = time;
979
                break;
980
            }
981

    
982
            default:
983
                av_log(matroska->ctx, AV_LOG_INFO,
984
                       "Unknown entry 0x%x in info header\n", id);
985
                /* fall-through */
986

    
987
            case EBML_ID_VOID:
988
                res = ebml_read_skip(matroska);
989
                break;
990
        }
991

    
992
        if (matroska->level_up) {
993
            matroska->level_up--;
994
            break;
995
        }
996
    }
997

    
998
    return res;
999
}
1000

    
1001
static int
1002
matroska_add_stream (MatroskaDemuxContext *matroska)
1003
{
1004
    int res = 0;
1005
    uint32_t id;
1006
    MatroskaTrack *track;
1007

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

    
1010
    /* Allocate a generic track. As soon as we know its type we'll realloc. */
1011
    track = av_mallocz(MAX_TRACK_SIZE);
1012
    matroska->num_tracks++;
1013
    strcpy(track->language, "eng");
1014

    
1015
    /* start with the master */
1016
    if ((res = ebml_read_master(matroska, &id)) < 0)
1017
        return res;
1018

    
1019
    /* try reading the trackentry headers */
1020
    while (res == 0) {
1021
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1022
            res = AVERROR(EIO);
1023
            break;
1024
        } else if (matroska->level_up > 0) {
1025
            matroska->level_up--;
1026
            break;
1027
        }
1028

    
1029
        switch (id) {
1030
            /* track number (unique stream ID) */
1031
            case MATROSKA_ID_TRACKNUMBER: {
1032
                uint64_t num;
1033
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1034
                    break;
1035
                track->num = num;
1036
                break;
1037
            }
1038

    
1039
            /* track UID (unique identifier) */
1040
            case MATROSKA_ID_TRACKUID: {
1041
                uint64_t num;
1042
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1043
                    break;
1044
                track->uid = num;
1045
                break;
1046
            }
1047

    
1048
            /* track type (video, audio, combined, subtitle, etc.) */
1049
            case MATROSKA_ID_TRACKTYPE: {
1050
                uint64_t num;
1051
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1052
                    break;
1053
                if (track->type && track->type != num) {
1054
                    av_log(matroska->ctx, AV_LOG_INFO,
1055
                           "More than one tracktype in an entry - skip\n");
1056
                    break;
1057
                }
1058
                track->type = num;
1059

    
1060
                switch (track->type) {
1061
                    case MATROSKA_TRACK_TYPE_VIDEO:
1062
                    case MATROSKA_TRACK_TYPE_AUDIO:
1063
                    case MATROSKA_TRACK_TYPE_SUBTITLE:
1064
                        break;
1065
                    case MATROSKA_TRACK_TYPE_COMPLEX:
1066
                    case MATROSKA_TRACK_TYPE_LOGO:
1067
                    case MATROSKA_TRACK_TYPE_CONTROL:
1068
                    default:
1069
                        av_log(matroska->ctx, AV_LOG_INFO,
1070
                               "Unknown or unsupported track type 0x%x\n",
1071
                               track->type);
1072
                        track->type = 0;
1073
                        break;
1074
                }
1075
                matroska->tracks[matroska->num_tracks - 1] = track;
1076
                break;
1077
            }
1078

    
1079
            /* tracktype specific stuff for video */
1080
            case MATROSKA_ID_TRACKVIDEO: {
1081
                MatroskaVideoTrack *videotrack;
1082
                if (!track->type)
1083
                    track->type = MATROSKA_TRACK_TYPE_VIDEO;
1084
                if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1085
                    av_log(matroska->ctx, AV_LOG_INFO,
1086
                           "video data in non-video track - ignoring\n");
1087
                    res = AVERROR_INVALIDDATA;
1088
                    break;
1089
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1090
                    break;
1091
                videotrack = (MatroskaVideoTrack *)track;
1092

    
1093
                while (res == 0) {
1094
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1095
                        res = AVERROR(EIO);
1096
                        break;
1097
                    } else if (matroska->level_up > 0) {
1098
                        matroska->level_up--;
1099
                        break;
1100
                    }
1101

    
1102
                    switch (id) {
1103
                        /* fixme, this should be one-up, but I get it here */
1104
                        case MATROSKA_ID_TRACKDEFAULTDURATION: {
1105
                            uint64_t num;
1106
                            if ((res = ebml_read_uint (matroska, &id,
1107
                                                       &num)) < 0)
1108
                                break;
1109
                            track->default_duration = num;
1110
                            break;
1111
                        }
1112

    
1113
                        /* video framerate */
1114
                        case MATROSKA_ID_VIDEOFRAMERATE: {
1115
                            double num;
1116
                            if ((res = ebml_read_float(matroska, &id,
1117
                                                       &num)) < 0)
1118
                                break;
1119
                            if (!track->default_duration)
1120
                                track->default_duration = 1000000000/num;
1121
                            break;
1122
                        }
1123

    
1124
                        /* width of the size to display the video at */
1125
                        case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1126
                            uint64_t num;
1127
                            if ((res = ebml_read_uint(matroska, &id,
1128
                                                      &num)) < 0)
1129
                                break;
1130
                            videotrack->display_width = num;
1131
                            break;
1132
                        }
1133

    
1134
                        /* height of the size to display the video at */
1135
                        case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1136
                            uint64_t num;
1137
                            if ((res = ebml_read_uint(matroska, &id,
1138
                                                      &num)) < 0)
1139
                                break;
1140
                            videotrack->display_height = num;
1141
                            break;
1142
                        }
1143

    
1144
                        /* width of the video in the file */
1145
                        case MATROSKA_ID_VIDEOPIXELWIDTH: {
1146
                            uint64_t num;
1147
                            if ((res = ebml_read_uint(matroska, &id,
1148
                                                      &num)) < 0)
1149
                                break;
1150
                            videotrack->pixel_width = num;
1151
                            break;
1152
                        }
1153

    
1154
                        /* height of the video in the file */
1155
                        case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1156
                            uint64_t num;
1157
                            if ((res = ebml_read_uint(matroska, &id,
1158
                                                      &num)) < 0)
1159
                                break;
1160
                            videotrack->pixel_height = num;
1161
                            break;
1162
                        }
1163

    
1164
                        /* whether the video is interlaced */
1165
                        case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1166
                            uint64_t num;
1167
                            if ((res = ebml_read_uint(matroska, &id,
1168
                                                      &num)) < 0)
1169
                                break;
1170
                            if (num)
1171
                                track->flags |=
1172
                                    MATROSKA_VIDEOTRACK_INTERLACED;
1173
                            else
1174
                                track->flags &=
1175
                                    ~MATROSKA_VIDEOTRACK_INTERLACED;
1176
                            break;
1177
                        }
1178

    
1179
                        /* stereo mode (whether the video has two streams,
1180
                         * where one is for the left eye and the other for
1181
                         * the right eye, which creates a 3D-like
1182
                         * effect) */
1183
                        case MATROSKA_ID_VIDEOSTEREOMODE: {
1184
                            uint64_t num;
1185
                            if ((res = ebml_read_uint(matroska, &id,
1186
                                                      &num)) < 0)
1187
                                break;
1188
                            if (num != MATROSKA_EYE_MODE_MONO &&
1189
                                num != MATROSKA_EYE_MODE_LEFT &&
1190
                                num != MATROSKA_EYE_MODE_RIGHT &&
1191
                                num != MATROSKA_EYE_MODE_BOTH) {
1192
                                av_log(matroska->ctx, AV_LOG_INFO,
1193
                                       "Ignoring unknown eye mode 0x%x\n",
1194
                                       (uint32_t) num);
1195
                                break;
1196
                            }
1197
                            videotrack->eye_mode = num;
1198
                            break;
1199
                        }
1200

    
1201
                        /* aspect ratio behaviour */
1202
                        case MATROSKA_ID_VIDEOASPECTRATIO: {
1203
                            uint64_t num;
1204
                            if ((res = ebml_read_uint(matroska, &id,
1205
                                                      &num)) < 0)
1206
                                break;
1207
                            if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
1208
                                num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
1209
                                num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
1210
                                av_log(matroska->ctx, AV_LOG_INFO,
1211
                                       "Ignoring unknown aspect ratio 0x%x\n",
1212
                                       (uint32_t) num);
1213
                                break;
1214
                            }
1215
                            videotrack->ar_mode = num;
1216
                            break;
1217
                        }
1218

    
1219
                        /* colorspace (only matters for raw video)
1220
                         * fourcc */
1221
                        case MATROSKA_ID_VIDEOCOLORSPACE: {
1222
                            uint64_t num;
1223
                            if ((res = ebml_read_uint(matroska, &id,
1224
                                                      &num)) < 0)
1225
                                break;
1226
                            videotrack->fourcc = num;
1227
                            break;
1228
                        }
1229

    
1230
                        default:
1231
                            av_log(matroska->ctx, AV_LOG_INFO,
1232
                                   "Unknown video track header entry "
1233
                                   "0x%x - ignoring\n", id);
1234
                            /* pass-through */
1235

    
1236
                        case EBML_ID_VOID:
1237
                            res = ebml_read_skip(matroska);
1238
                            break;
1239
                    }
1240

    
1241
                    if (matroska->level_up) {
1242
                        matroska->level_up--;
1243
                        break;
1244
                    }
1245
                }
1246
                break;
1247
            }
1248

    
1249
            /* tracktype specific stuff for audio */
1250
            case MATROSKA_ID_TRACKAUDIO: {
1251
                MatroskaAudioTrack *audiotrack;
1252
                if (!track->type)
1253
                    track->type = MATROSKA_TRACK_TYPE_AUDIO;
1254
                if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1255
                    av_log(matroska->ctx, AV_LOG_INFO,
1256
                           "audio data in non-audio track - ignoring\n");
1257
                    res = AVERROR_INVALIDDATA;
1258
                    break;
1259
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1260
                    break;
1261
                audiotrack = (MatroskaAudioTrack *)track;
1262
                audiotrack->channels = 1;
1263
                audiotrack->samplerate = 8000;
1264

    
1265
                while (res == 0) {
1266
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1267
                        res = AVERROR(EIO);
1268
                        break;
1269
                    } else if (matroska->level_up > 0) {
1270
                        matroska->level_up--;
1271
                        break;
1272
                    }
1273

    
1274
                    switch (id) {
1275
                        /* samplerate */
1276
                        case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1277
                            double num;
1278
                            if ((res = ebml_read_float(matroska, &id,
1279
                                                       &num)) < 0)
1280
                                break;
1281
                            audiotrack->internal_samplerate =
1282
                            audiotrack->samplerate = num;
1283
                            break;
1284
                        }
1285

    
1286
                        case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1287
                            double num;
1288
                            if ((res = ebml_read_float(matroska, &id,
1289
                                                       &num)) < 0)
1290
                                break;
1291
                            audiotrack->samplerate = num;
1292
                            break;
1293
                        }
1294

    
1295
                            /* bitdepth */
1296
                        case MATROSKA_ID_AUDIOBITDEPTH: {
1297
                            uint64_t num;
1298
                            if ((res = ebml_read_uint(matroska, &id,
1299
                                                      &num)) < 0)
1300
                                break;
1301
                            audiotrack->bitdepth = num;
1302
                            break;
1303
                        }
1304

    
1305
                            /* channels */
1306
                        case MATROSKA_ID_AUDIOCHANNELS: {
1307
                            uint64_t num;
1308
                            if ((res = ebml_read_uint(matroska, &id,
1309
                                                      &num)) < 0)
1310
                                break;
1311
                            audiotrack->channels = num;
1312
                            break;
1313
                        }
1314

    
1315
                        default:
1316
                            av_log(matroska->ctx, AV_LOG_INFO,
1317
                                   "Unknown audio track header entry "
1318
                                   "0x%x - ignoring\n", id);
1319
                            /* pass-through */
1320

    
1321
                        case EBML_ID_VOID:
1322
                            res = ebml_read_skip(matroska);
1323
                            break;
1324
                    }
1325

    
1326
                    if (matroska->level_up) {
1327
                        matroska->level_up--;
1328
                        break;
1329
                    }
1330
                }
1331
                break;
1332
            }
1333

    
1334
                /* codec identifier */
1335
            case MATROSKA_ID_CODECID: {
1336
                char *text;
1337
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1338
                    break;
1339
                track->codec_id = text;
1340
                break;
1341
            }
1342

    
1343
                /* codec private data */
1344
            case MATROSKA_ID_CODECPRIVATE: {
1345
                uint8_t *data;
1346
                int size;
1347
                if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1348
                    break;
1349
                track->codec_priv = data;
1350
                track->codec_priv_size = size;
1351
                break;
1352
            }
1353

    
1354
                /* name of the codec */
1355
            case MATROSKA_ID_CODECNAME: {
1356
                char *text;
1357
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1358
                    break;
1359
                track->codec_name = text;
1360
                break;
1361
            }
1362

    
1363
                /* name of this track */
1364
            case MATROSKA_ID_TRACKNAME: {
1365
                char *text;
1366
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1367
                    break;
1368
                track->name = text;
1369
                break;
1370
            }
1371

    
1372
                /* language (matters for audio/subtitles, mostly) */
1373
            case MATROSKA_ID_TRACKLANGUAGE: {
1374
                char *text, *end;
1375
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1376
                    break;
1377
                if ((end = strchr(text, '-')))
1378
                    *end = '\0';
1379
                if (strlen(text) == 3)
1380
                    strcpy(track->language, text);
1381
                av_free(text);
1382
                break;
1383
            }
1384

    
1385
                /* whether this is actually used */
1386
            case MATROSKA_ID_TRACKFLAGENABLED: {
1387
                uint64_t num;
1388
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1389
                    break;
1390
                if (num)
1391
                    track->flags |= MATROSKA_TRACK_ENABLED;
1392
                else
1393
                    track->flags &= ~MATROSKA_TRACK_ENABLED;
1394
                break;
1395
            }
1396

    
1397
                /* whether it's the default for this track type */
1398
            case MATROSKA_ID_TRACKFLAGDEFAULT: {
1399
                uint64_t num;
1400
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1401
                    break;
1402
                if (num)
1403
                    track->flags |= MATROSKA_TRACK_DEFAULT;
1404
                else
1405
                    track->flags &= ~MATROSKA_TRACK_DEFAULT;
1406
                break;
1407
            }
1408

    
1409
                /* lacing (like MPEG, where blocks don't end/start on frame
1410
                 * boundaries) */
1411
            case MATROSKA_ID_TRACKFLAGLACING: {
1412
                uint64_t num;
1413
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1414
                    break;
1415
                if (num)
1416
                    track->flags |= MATROSKA_TRACK_LACING;
1417
                else
1418
                    track->flags &= ~MATROSKA_TRACK_LACING;
1419
                break;
1420
            }
1421

    
1422
                /* default length (in time) of one data block in this track */
1423
            case MATROSKA_ID_TRACKDEFAULTDURATION: {
1424
                uint64_t num;
1425
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1426
                    break;
1427
                track->default_duration = num;
1428
                break;
1429
            }
1430

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

    
1436
            case EBML_ID_VOID:
1437
            /* we ignore these because they're nothing useful. */
1438
            case MATROSKA_ID_CODECINFOURL:
1439
            case MATROSKA_ID_CODECDOWNLOADURL:
1440
            case MATROSKA_ID_TRACKMINCACHE:
1441
            case MATROSKA_ID_TRACKMAXCACHE:
1442
                res = ebml_read_skip(matroska);
1443
                break;
1444
        }
1445

    
1446
        if (matroska->level_up) {
1447
            matroska->level_up--;
1448
            break;
1449
        }
1450
    }
1451

    
1452
    return res;
1453
}
1454

    
1455
static int
1456
matroska_parse_tracks (MatroskaDemuxContext *matroska)
1457
{
1458
    int res = 0;
1459
    uint32_t id;
1460

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

    
1463
    while (res == 0) {
1464
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1465
            res = AVERROR(EIO);
1466
            break;
1467
        } else if (matroska->level_up) {
1468
            matroska->level_up--;
1469
            break;
1470
        }
1471

    
1472
        switch (id) {
1473
            /* one track within the "all-tracks" header */
1474
            case MATROSKA_ID_TRACKENTRY:
1475
                res = matroska_add_stream(matroska);
1476
                break;
1477

    
1478
            default:
1479
                av_log(matroska->ctx, AV_LOG_INFO,
1480
                       "Unknown entry 0x%x in track header\n", id);
1481
                /* fall-through */
1482

    
1483
            case EBML_ID_VOID:
1484
                res = ebml_read_skip(matroska);
1485
                break;
1486
        }
1487

    
1488
        if (matroska->level_up) {
1489
            matroska->level_up--;
1490
            break;
1491
        }
1492
    }
1493

    
1494
    return res;
1495
}
1496

    
1497
static int
1498
matroska_parse_index (MatroskaDemuxContext *matroska)
1499
{
1500
    int res = 0;
1501
    uint32_t id;
1502
    MatroskaDemuxIndex idx;
1503

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

    
1506
    while (res == 0) {
1507
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1508
            res = AVERROR(EIO);
1509
            break;
1510
        } else if (matroska->level_up) {
1511
            matroska->level_up--;
1512
            break;
1513
        }
1514

    
1515
        switch (id) {
1516
            /* one single index entry ('point') */
1517
            case MATROSKA_ID_POINTENTRY:
1518
                if ((res = ebml_read_master(matroska, &id)) < 0)
1519
                    break;
1520

    
1521
                /* in the end, we hope to fill one entry with a
1522
                 * timestamp, a file position and a tracknum */
1523
                idx.pos   = (uint64_t) -1;
1524
                idx.time  = (uint64_t) -1;
1525
                idx.track = (uint16_t) -1;
1526

    
1527
                while (res == 0) {
1528
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1529
                        res = AVERROR(EIO);
1530
                        break;
1531
                    } else if (matroska->level_up) {
1532
                        matroska->level_up--;
1533
                        break;
1534
                    }
1535

    
1536
                    switch (id) {
1537
                        /* one single index entry ('point') */
1538
                        case MATROSKA_ID_CUETIME: {
1539
                            uint64_t time;
1540
                            if ((res = ebml_read_uint(matroska, &id,
1541
                                                      &time)) < 0)
1542
                                break;
1543
                            idx.time = time * matroska->time_scale;
1544
                            break;
1545
                        }
1546

    
1547
                        /* position in the file + track to which it
1548
                         * belongs */
1549
                        case MATROSKA_ID_CUETRACKPOSITION:
1550
                            if ((res = ebml_read_master(matroska, &id)) < 0)
1551
                                break;
1552

    
1553
                            while (res == 0) {
1554
                                if (!(id = ebml_peek_id (matroska,
1555
                                                    &matroska->level_up))) {
1556
                                    res = AVERROR(EIO);
1557
                                    break;
1558
                                } else if (matroska->level_up) {
1559
                                    matroska->level_up--;
1560
                                    break;
1561
                                }
1562

    
1563
                                switch (id) {
1564
                                    /* track number */
1565
                                    case MATROSKA_ID_CUETRACK: {
1566
                                        uint64_t num;
1567
                                        if ((res = ebml_read_uint(matroska,
1568
                                                          &id, &num)) < 0)
1569
                                            break;
1570
                                        idx.track = num;
1571
                                        break;
1572
                                    }
1573

    
1574
                                        /* position in file */
1575
                                    case MATROSKA_ID_CUECLUSTERPOSITION: {
1576
                                        uint64_t num;
1577
                                        if ((res = ebml_read_uint(matroska,
1578
                                                          &id, &num)) < 0)
1579
                                            break;
1580
                                        idx.pos = num+matroska->segment_start;
1581
                                        break;
1582
                                    }
1583

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

    
1590
                                    case EBML_ID_VOID:
1591
                                        res = ebml_read_skip(matroska);
1592
                                        break;
1593
                                }
1594

    
1595
                                if (matroska->level_up) {
1596
                                    matroska->level_up--;
1597
                                    break;
1598
                                }
1599
                            }
1600

    
1601
                            break;
1602

    
1603
                        default:
1604
                            av_log(matroska->ctx, AV_LOG_INFO,
1605
                                   "Unknown entry 0x%x in cuespoint "
1606
                                   "index\n", id);
1607
                            /* fall-through */
1608

    
1609
                        case EBML_ID_VOID:
1610
                            res = ebml_read_skip(matroska);
1611
                            break;
1612
                    }
1613

    
1614
                    if (matroska->level_up) {
1615
                        matroska->level_up--;
1616
                        break;
1617
                    }
1618
                }
1619

    
1620
                /* so let's see if we got what we wanted */
1621
                if (idx.pos   != (uint64_t) -1 &&
1622
                    idx.time  != (uint64_t) -1 &&
1623
                    idx.track != (uint16_t) -1) {
1624
                    if (matroska->num_indexes % 32 == 0) {
1625
                        /* re-allocate bigger index */
1626
                        matroska->index =
1627
                            av_realloc(matroska->index,
1628
                                       (matroska->num_indexes + 32) *
1629
                                       sizeof(MatroskaDemuxIndex));
1630
                    }
1631
                    matroska->index[matroska->num_indexes] = idx;
1632
                    matroska->num_indexes++;
1633
                }
1634
                break;
1635

    
1636
            default:
1637
                av_log(matroska->ctx, AV_LOG_INFO,
1638
                       "Unknown entry 0x%x in cues header\n", id);
1639
                /* fall-through */
1640

    
1641
            case EBML_ID_VOID:
1642
                res = ebml_read_skip(matroska);
1643
                break;
1644
        }
1645

    
1646
        if (matroska->level_up) {
1647
            matroska->level_up--;
1648
            break;
1649
        }
1650
    }
1651

    
1652
    return res;
1653
}
1654

    
1655
static int
1656
matroska_parse_metadata (MatroskaDemuxContext *matroska)
1657
{
1658
    int res = 0;
1659
    uint32_t id;
1660

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

    
1670
        switch (id) {
1671
            /* Hm, this is unsupported... */
1672
            default:
1673
                av_log(matroska->ctx, AV_LOG_INFO,
1674
                       "Unknown entry 0x%x in metadata header\n", id);
1675
                /* fall-through */
1676

    
1677
            case EBML_ID_VOID:
1678
                res = ebml_read_skip(matroska);
1679
                break;
1680
        }
1681

    
1682
        if (matroska->level_up) {
1683
            matroska->level_up--;
1684
            break;
1685
        }
1686
    }
1687

    
1688
    return res;
1689
}
1690

    
1691
static int
1692
matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1693
{
1694
    int res = 0;
1695
    uint32_t id;
1696

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

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

    
1708
        switch (id) {
1709
            case MATROSKA_ID_SEEKENTRY: {
1710
                uint32_t seek_id = 0, peek_id_cache = 0;
1711
                uint64_t seek_pos = (uint64_t) -1, t;
1712

    
1713
                if ((res = ebml_read_master(matroska, &id)) < 0)
1714
                    break;
1715

    
1716
                while (res == 0) {
1717
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1718
                        res = AVERROR(EIO);
1719
                        break;
1720
                    } else if (matroska->level_up) {
1721
                        matroska->level_up--;
1722
                        break;
1723
                    }
1724

    
1725
                    switch (id) {
1726
                        case MATROSKA_ID_SEEKID:
1727
                            res = ebml_read_uint(matroska, &id, &t);
1728
                            seek_id = t;
1729
                            break;
1730

    
1731
                        case MATROSKA_ID_SEEKPOSITION:
1732
                            res = ebml_read_uint(matroska, &id, &seek_pos);
1733
                            break;
1734

    
1735
                        default:
1736
                            av_log(matroska->ctx, AV_LOG_INFO,
1737
                                   "Unknown seekhead ID 0x%x\n", id);
1738
                            /* fall-through */
1739

    
1740
                        case EBML_ID_VOID:
1741
                            res = ebml_read_skip(matroska);
1742
                            break;
1743
                    }
1744

    
1745
                    if (matroska->level_up) {
1746
                        matroska->level_up--;
1747
                        break;
1748
                    }
1749
                }
1750

    
1751
                if (!seek_id || seek_pos == (uint64_t) -1) {
1752
                    av_log(matroska->ctx, AV_LOG_INFO,
1753
                           "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1754
                           seek_id, seek_pos);
1755
                    break;
1756
                }
1757

    
1758
                switch (seek_id) {
1759
                    case MATROSKA_ID_CUES:
1760
                    case MATROSKA_ID_TAGS: {
1761
                        uint32_t level_up = matroska->level_up;
1762
                        offset_t before_pos;
1763
                        uint64_t length;
1764
                        MatroskaLevel level;
1765

    
1766
                        /* remember the peeked ID and the current position */
1767
                        peek_id_cache = matroska->peek_id;
1768
                        before_pos = url_ftell(matroska->ctx->pb);
1769

    
1770
                        /* seek */
1771
                        if ((res = ebml_read_seek(matroska, seek_pos +
1772
                                               matroska->segment_start)) < 0)
1773
                            return res;
1774

    
1775
                        /* we don't want to lose our seekhead level, so we add
1776
                         * a dummy. This is a crude hack. */
1777
                        if (matroska->num_levels == EBML_MAX_DEPTH) {
1778
                            av_log(matroska->ctx, AV_LOG_INFO,
1779
                                   "Max EBML element depth (%d) reached, "
1780
                                   "cannot parse further.\n", EBML_MAX_DEPTH);
1781
                            return AVERROR_UNKNOWN;
1782
                        }
1783

    
1784
                        level.start = 0;
1785
                        level.length = (uint64_t)-1;
1786
                        matroska->levels[matroska->num_levels] = level;
1787
                        matroska->num_levels++;
1788

    
1789
                        /* check ID */
1790
                        if (!(id = ebml_peek_id (matroska,
1791
                                                 &matroska->level_up)))
1792
                            goto finish;
1793
                        if (id != seek_id) {
1794
                            av_log(matroska->ctx, AV_LOG_INFO,
1795
                                   "We looked for ID=0x%x but got "
1796
                                   "ID=0x%x (pos=%"PRIu64")",
1797
                                   seek_id, id, seek_pos +
1798
                                   matroska->segment_start);
1799
                            goto finish;
1800
                        }
1801

    
1802
                        /* read master + parse */
1803
                        if ((res = ebml_read_master(matroska, &id)) < 0)
1804
                            goto finish;
1805
                        switch (id) {
1806
                            case MATROSKA_ID_CUES:
1807
                                if (!(res = matroska_parse_index(matroska)) ||
1808
                                    url_feof(matroska->ctx->pb)) {
1809
                                    matroska->index_parsed = 1;
1810
                                    res = 0;
1811
                                }
1812
                                break;
1813
                            case MATROSKA_ID_TAGS:
1814
                                if (!(res = matroska_parse_metadata(matroska)) ||
1815
                                   url_feof(matroska->ctx->pb)) {
1816
                                    matroska->metadata_parsed = 1;
1817
                                    res = 0;
1818
                                }
1819
                                break;
1820
                        }
1821

    
1822
                    finish:
1823
                        /* remove dummy level */
1824
                        while (matroska->num_levels) {
1825
                            matroska->num_levels--;
1826
                            length =
1827
                                matroska->levels[matroska->num_levels].length;
1828
                            if (length == (uint64_t)-1)
1829
                                break;
1830
                        }
1831

    
1832
                        /* seek back */
1833
                        if ((res = ebml_read_seek(matroska, before_pos)) < 0)
1834
                            return res;
1835
                        matroska->peek_id = peek_id_cache;
1836
                        matroska->level_up = level_up;
1837
                        break;
1838
                    }
1839

    
1840
                    default:
1841
                        av_log(matroska->ctx, AV_LOG_INFO,
1842
                               "Ignoring seekhead entry for ID=0x%x\n",
1843
                               seek_id);
1844
                        break;
1845
                }
1846

    
1847
                break;
1848
            }
1849

    
1850
            default:
1851
                av_log(matroska->ctx, AV_LOG_INFO,
1852
                       "Unknown seekhead ID 0x%x\n", id);
1853
                /* fall-through */
1854

    
1855
            case EBML_ID_VOID:
1856
                res = ebml_read_skip(matroska);
1857
                break;
1858
        }
1859

    
1860
        if (matroska->level_up) {
1861
            matroska->level_up--;
1862
            break;
1863
        }
1864
    }
1865

    
1866
    return res;
1867
}
1868

    
1869
#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
1870

    
1871
static int
1872
matroska_aac_profile (char *codec_id)
1873
{
1874
    static const char *aac_profiles[] = {
1875
        "MAIN", "LC", "SSR"
1876
    };
1877
    int profile;
1878

    
1879
    for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
1880
        if (strstr(codec_id, aac_profiles[profile]))
1881
            break;
1882
    return profile + 1;
1883
}
1884

    
1885
static int
1886
matroska_aac_sri (int samplerate)
1887
{
1888
    static const int aac_sample_rates[] = {
1889
        96000, 88200, 64000, 48000, 44100, 32000,
1890
        24000, 22050, 16000, 12000, 11025,  8000,
1891
    };
1892
    int sri;
1893

    
1894
    for (sri=0; sri<ARRAY_SIZE(aac_sample_rates); sri++)
1895
        if (aac_sample_rates[sri] == samplerate)
1896
            break;
1897
    return sri;
1898
}
1899

    
1900
static int
1901
matroska_read_header (AVFormatContext    *s,
1902
                      AVFormatParameters *ap)
1903
{
1904
    MatroskaDemuxContext *matroska = s->priv_data;
1905
    char *doctype;
1906
    int version, last_level, res = 0;
1907
    uint32_t id;
1908

    
1909
    matroska->ctx = s;
1910

    
1911
    /* First read the EBML header. */
1912
    doctype = NULL;
1913
    if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
1914
        return res;
1915
    if ((doctype == NULL) || strcmp(doctype, "matroska")) {
1916
        av_log(matroska->ctx, AV_LOG_ERROR,
1917
               "Wrong EBML doctype ('%s' != 'matroska').\n",
1918
               doctype ? doctype : "(none)");
1919
        if (doctype)
1920
            av_free(doctype);
1921
        return AVERROR_NOFMT;
1922
    }
1923
    av_free(doctype);
1924
    if (version > 2) {
1925
        av_log(matroska->ctx, AV_LOG_ERROR,
1926
               "Matroska demuxer version 2 too old for file version %d\n",
1927
               version);
1928
        return AVERROR_NOFMT;
1929
    }
1930

    
1931
    /* The next thing is a segment. */
1932
    while (1) {
1933
        if (!(id = ebml_peek_id(matroska, &last_level)))
1934
            return AVERROR(EIO);
1935
        if (id == MATROSKA_ID_SEGMENT)
1936
            break;
1937

    
1938
        /* oi! */
1939
        av_log(matroska->ctx, AV_LOG_INFO,
1940
               "Expected a Segment ID (0x%x), but received 0x%x!\n",
1941
               MATROSKA_ID_SEGMENT, id);
1942
        if ((res = ebml_read_skip(matroska)) < 0)
1943
            return res;
1944
    }
1945

    
1946
    /* We now have a Matroska segment.
1947
     * Seeks are from the beginning of the segment,
1948
     * after the segment ID/length. */
1949
    if ((res = ebml_read_master(matroska, &id)) < 0)
1950
        return res;
1951
    matroska->segment_start = url_ftell(s->pb);
1952

    
1953
    matroska->time_scale = 1000000;
1954
    /* we've found our segment, start reading the different contents in here */
1955
    while (res == 0) {
1956
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1957
            res = AVERROR(EIO);
1958
            break;
1959
        } else if (matroska->level_up) {
1960
            matroska->level_up--;
1961
            break;
1962
        }
1963

    
1964
        switch (id) {
1965
            /* stream info */
1966
            case MATROSKA_ID_INFO: {
1967
                if ((res = ebml_read_master(matroska, &id)) < 0)
1968
                    break;
1969
                res = matroska_parse_info(matroska);
1970
                break;
1971
            }
1972

    
1973
            /* track info headers */
1974
            case MATROSKA_ID_TRACKS: {
1975
                if ((res = ebml_read_master(matroska, &id)) < 0)
1976
                    break;
1977
                res = matroska_parse_tracks(matroska);
1978
                break;
1979
            }
1980

    
1981
            /* stream index */
1982
            case MATROSKA_ID_CUES: {
1983
                if (!matroska->index_parsed) {
1984
                    if ((res = ebml_read_master(matroska, &id)) < 0)
1985
                        break;
1986
                    res = matroska_parse_index(matroska);
1987
                } else
1988
                    res = ebml_read_skip(matroska);
1989
                break;
1990
            }
1991

    
1992
            /* metadata */
1993
            case MATROSKA_ID_TAGS: {
1994
                if (!matroska->metadata_parsed) {
1995
                    if ((res = ebml_read_master(matroska, &id)) < 0)
1996
                        break;
1997
                    res = matroska_parse_metadata(matroska);
1998
                } else
1999
                    res = ebml_read_skip(matroska);
2000
                break;
2001
            }
2002

    
2003
            /* file index (if seekable, seek to Cues/Tags to parse it) */
2004
            case MATROSKA_ID_SEEKHEAD: {
2005
                if ((res = ebml_read_master(matroska, &id)) < 0)
2006
                    break;
2007
                res = matroska_parse_seekhead(matroska);
2008
                break;
2009
            }
2010

    
2011
            case MATROSKA_ID_CLUSTER: {
2012
                /* Do not read the master - this will be done in the next
2013
                 * call to matroska_read_packet. */
2014
                res = 1;
2015
                break;
2016
            }
2017

    
2018
            default:
2019
                av_log(matroska->ctx, AV_LOG_INFO,
2020
                       "Unknown matroska file header ID 0x%x\n", id);
2021
            /* fall-through */
2022

    
2023
            case EBML_ID_VOID:
2024
                res = ebml_read_skip(matroska);
2025
                break;
2026
        }
2027

    
2028
        if (matroska->level_up) {
2029
            matroska->level_up--;
2030
            break;
2031
        }
2032
    }
2033

    
2034
    /* Have we found a cluster? */
2035
    if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2036
        int i, j;
2037
        MatroskaTrack *track;
2038
        AVStream *st;
2039

    
2040
        for (i = 0; i < matroska->num_tracks; i++) {
2041
            enum CodecID codec_id = CODEC_ID_NONE;
2042
            uint8_t *extradata = NULL;
2043
            int extradata_size = 0;
2044
            int extradata_offset = 0;
2045
            track = matroska->tracks[i];
2046
            track->stream_index = -1;
2047

    
2048
            /* Apply some sanity checks. */
2049
            if (track->codec_id == NULL)
2050
                continue;
2051

    
2052
            for(j=0; ff_mkv_codec_tags[j].str; j++){
2053
                if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2054
                            strlen(ff_mkv_codec_tags[j].str))){
2055
                    codec_id= ff_mkv_codec_tags[j].id;
2056
                    break;
2057
                }
2058
            }
2059

    
2060
            /* Set the FourCC from the CodecID. */
2061
            /* This is the MS compatibility mode which stores a
2062
             * BITMAPINFOHEADER in the CodecPrivate. */
2063
            if (!strcmp(track->codec_id,
2064
                        MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2065
                (track->codec_priv_size >= 40) &&
2066
                (track->codec_priv != NULL)) {
2067
                MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2068

    
2069
                /* Offset of biCompression. Stored in LE. */
2070
                vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2071
                codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2072

    
2073
            }
2074

    
2075
            /* This is the MS compatibility mode which stores a
2076
             * WAVEFORMATEX in the CodecPrivate. */
2077
            else if (!strcmp(track->codec_id,
2078
                             MATROSKA_CODEC_ID_AUDIO_ACM) &&
2079
                (track->codec_priv_size >= 18) &&
2080
                (track->codec_priv != NULL)) {
2081
                uint16_t tag;
2082

    
2083
                /* Offset of wFormatTag. Stored in LE. */
2084
                tag = AV_RL16(track->codec_priv);
2085
                codec_id = codec_get_id(codec_wav_tags, tag);
2086

    
2087
            }
2088

    
2089
            else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2090
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2091
                int profile = matroska_aac_profile(track->codec_id);
2092
                int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2093
                extradata = av_malloc(5);
2094
                if (extradata == NULL)
2095
                    return AVERROR(ENOMEM);
2096
                extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2097
                extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2098
                if (strstr(track->codec_id, "SBR")) {
2099
                    sri = matroska_aac_sri(audiotrack->samplerate);
2100
                    extradata[2] = 0x56;
2101
                    extradata[3] = 0xE5;
2102
                    extradata[4] = 0x80 | (sri<<3);
2103
                    extradata_size = 5;
2104
                } else {
2105
                    extradata_size = 2;
2106
                }
2107
            }
2108

    
2109
            else if (codec_id == CODEC_ID_TTA) {
2110
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2111
                ByteIOContext b;
2112
                extradata_size = 30;
2113
                extradata = av_mallocz(extradata_size);
2114
                if (extradata == NULL)
2115
                    return AVERROR(ENOMEM);
2116
                init_put_byte(&b, extradata, extradata_size, 1,
2117
                              NULL, NULL, NULL, NULL);
2118
                put_buffer(&b, (uint8_t *) "TTA1", 4);
2119
                put_le16(&b, 1);
2120
                put_le16(&b, audiotrack->channels);
2121
                put_le16(&b, audiotrack->bitdepth);
2122
                put_le32(&b, audiotrack->samplerate);
2123
                put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2124
            }
2125

    
2126
            else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2127
                     codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2128
                extradata_offset = 26;
2129
                track->codec_priv_size -= extradata_offset;
2130
            }
2131

    
2132
            else if (codec_id == CODEC_ID_RA_144) {
2133
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2134
                audiotrack->samplerate = 8000;
2135
                audiotrack->channels = 1;
2136
            }
2137

    
2138
            else if (codec_id == CODEC_ID_RA_288 ||
2139
                     codec_id == CODEC_ID_COOK ||
2140
                     codec_id == CODEC_ID_ATRAC3) {
2141
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2142
                ByteIOContext b;
2143

    
2144
                init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2145
                              NULL, NULL, NULL, NULL);
2146
                url_fskip(&b, 24);
2147
                audiotrack->coded_framesize = get_be32(&b);
2148
                url_fskip(&b, 12);
2149
                audiotrack->sub_packet_h    = get_be16(&b);
2150
                audiotrack->frame_size      = get_be16(&b);
2151
                audiotrack->sub_packet_size = get_be16(&b);
2152
                audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2153
                if (codec_id == CODEC_ID_RA_288) {
2154
                    audiotrack->block_align = audiotrack->coded_framesize;
2155
                    track->codec_priv_size = 0;
2156
                } else {
2157
                    audiotrack->block_align = audiotrack->sub_packet_size;
2158
                    extradata_offset = 78;
2159
                    track->codec_priv_size -= extradata_offset;
2160
                }
2161
            }
2162

    
2163
            else if (codec_id == CODEC_ID_TEXT) {
2164
                MatroskaSubtitleTrack *subtrack=(MatroskaSubtitleTrack *)track;
2165
                if (!strcmp(track->codec_id, "S_TEXT/ASS") ||
2166
                    !strcmp(track->codec_id, "S_TEXT/SSA") ||
2167
                    !strcmp(track->codec_id, "S_ASS") ||
2168
                    !strcmp(track->codec_id, "S_SSA"))
2169
                    subtrack->ass = 1;
2170
            }
2171

    
2172
            if (codec_id == CODEC_ID_NONE) {
2173
                av_log(matroska->ctx, AV_LOG_INFO,
2174
                       "Unknown/unsupported CodecID %s.\n",
2175
                       track->codec_id);
2176
            }
2177

    
2178
            track->stream_index = matroska->num_streams;
2179

    
2180
            matroska->num_streams++;
2181
            st = av_new_stream(s, track->stream_index);
2182
            if (st == NULL)
2183
                return AVERROR(ENOMEM);
2184
            av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2185

    
2186
            st->codec->codec_id = codec_id;
2187
            st->start_time = 0;
2188
            if (strcmp(track->language, "und"))
2189
                strcpy(st->language, track->language);
2190

    
2191
            if (track->default_duration)
2192
                av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2193
                          track->default_duration, 1000000000, 30000);
2194

    
2195
            if(extradata){
2196
                st->codec->extradata = extradata;
2197
                st->codec->extradata_size = extradata_size;
2198
            } else if(track->codec_priv && track->codec_priv_size > 0){
2199
                st->codec->extradata = av_malloc(track->codec_priv_size);
2200
                if(st->codec->extradata == NULL)
2201
                    return AVERROR(ENOMEM);
2202
                st->codec->extradata_size = track->codec_priv_size;
2203
                memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2204
                       track->codec_priv_size);
2205
            }
2206

    
2207
            if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2208
                MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2209

    
2210
                st->codec->codec_type = CODEC_TYPE_VIDEO;
2211
                st->codec->codec_tag = videotrack->fourcc;
2212
                st->codec->width = videotrack->pixel_width;
2213
                st->codec->height = videotrack->pixel_height;
2214
                if (videotrack->display_width == 0)
2215
                    videotrack->display_width= videotrack->pixel_width;
2216
                if (videotrack->display_height == 0)
2217
                    videotrack->display_height= videotrack->pixel_height;
2218
                av_reduce(&st->codec->sample_aspect_ratio.num,
2219
                          &st->codec->sample_aspect_ratio.den,
2220
                          st->codec->height * videotrack->display_width,
2221
                          st->codec-> width * videotrack->display_height,
2222
                          255);
2223
                st->need_parsing = AVSTREAM_PARSE_HEADERS;
2224
            } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2225
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2226

    
2227
                st->codec->codec_type = CODEC_TYPE_AUDIO;
2228
                st->codec->sample_rate = audiotrack->samplerate;
2229
                st->codec->channels = audiotrack->channels;
2230
                st->codec->block_align = audiotrack->block_align;
2231
            } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2232
                st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2233
            }
2234

    
2235
            /* What do we do with private data? E.g. for Vorbis. */
2236
        }
2237
        res = 0;
2238
    }
2239

    
2240
    if (matroska->index_parsed) {
2241
        int i, track, stream;
2242
        for (i=0; i<matroska->num_indexes; i++) {
2243
            MatroskaDemuxIndex *idx = &matroska->index[i];
2244
            track = matroska_find_track_by_num(matroska, idx->track);
2245
            stream = matroska->tracks[track]->stream_index;
2246
            if (stream >= 0)
2247
                av_add_index_entry(matroska->ctx->streams[stream],
2248
                                   idx->pos, idx->time/matroska->time_scale,
2249
                                   0, 0, AVINDEX_KEYFRAME);
2250
        }
2251
    }
2252

    
2253
    return res;
2254
}
2255

    
2256
static int
2257
matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2258
                     int64_t pos, uint64_t cluster_time, uint64_t duration,
2259
                     int is_keyframe, int is_bframe)
2260
{
2261
    int res = 0;
2262
    int track;
2263
    AVStream *st;
2264
    AVPacket *pkt;
2265
    uint8_t *origdata = data;
2266
    int16_t block_time;
2267
    uint32_t *lace_size = NULL;
2268
    int n, flags, laces = 0;
2269
    uint64_t num;
2270

    
2271
    /* first byte(s): tracknum */
2272
    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2273
        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2274
        av_free(origdata);
2275
        return res;
2276
    }
2277
    data += n;
2278
    size -= n;
2279

    
2280
    /* fetch track from num */
2281
    track = matroska_find_track_by_num(matroska, num);
2282
    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2283
        av_log(matroska->ctx, AV_LOG_INFO,
2284
               "Invalid stream %d or size %u\n", track, size);
2285
        av_free(origdata);
2286
        return res;
2287
    }
2288
    if (matroska->tracks[track]->stream_index < 0) {
2289
        av_free(origdata);
2290
        return res;
2291
    }
2292
    st = matroska->ctx->streams[matroska->tracks[track]->stream_index];
2293
    if (st->discard >= AVDISCARD_ALL) {
2294
        av_free(origdata);
2295
        return res;
2296
    }
2297
    if (duration == AV_NOPTS_VALUE)
2298
        duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2299

    
2300
    /* block_time (relative to cluster time) */
2301
    block_time = AV_RB16(data);
2302
    data += 2;
2303
    flags = *data++;
2304
    size -= 3;
2305
    if (is_keyframe == -1)
2306
        is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2307

    
2308
    if (matroska->skip_to_keyframe) {
2309
        if (!is_keyframe || st != matroska->skip_to_stream) {
2310
            av_free(origdata);
2311
            return res;
2312
        }
2313
        matroska->skip_to_keyframe = 0;
2314
    }
2315

    
2316
    switch ((flags & 0x06) >> 1) {
2317
        case 0x0: /* no lacing */
2318
            laces = 1;
2319
            lace_size = av_mallocz(sizeof(int));
2320
            lace_size[0] = size;
2321
            break;
2322

    
2323
        case 0x1: /* xiph lacing */
2324
        case 0x2: /* fixed-size lacing */
2325
        case 0x3: /* EBML lacing */
2326
            if (size == 0) {
2327
                res = -1;
2328
                break;
2329
            }
2330
            laces = (*data) + 1;
2331
            data += 1;
2332
            size -= 1;
2333
            lace_size = av_mallocz(laces * sizeof(int));
2334

    
2335
            switch ((flags & 0x06) >> 1) {
2336
                case 0x1: /* xiph lacing */ {
2337
                    uint8_t temp;
2338
                    uint32_t total = 0;
2339
                    for (n = 0; res == 0 && n < laces - 1; n++) {
2340
                        while (1) {
2341
                            if (size == 0) {
2342
                                res = -1;
2343
                                break;
2344
                            }
2345
                            temp = *data;
2346
                            lace_size[n] += temp;
2347
                            data += 1;
2348
                            size -= 1;
2349
                            if (temp != 0xff)
2350
                                break;
2351
                        }
2352
                        total += lace_size[n];
2353
                    }
2354
                    lace_size[n] = size - total;
2355
                    break;
2356
                }
2357

    
2358
                case 0x2: /* fixed-size lacing */
2359
                    for (n = 0; n < laces; n++)
2360
                        lace_size[n] = size / laces;
2361
                    break;
2362

    
2363
                case 0x3: /* EBML lacing */ {
2364
                    uint32_t total;
2365
                    n = matroska_ebmlnum_uint(data, size, &num);
2366
                    if (n < 0) {
2367
                        av_log(matroska->ctx, AV_LOG_INFO,
2368
                               "EBML block data error\n");
2369
                        break;
2370
                    }
2371
                    data += n;
2372
                    size -= n;
2373
                    total = lace_size[0] = num;
2374
                    for (n = 1; res == 0 && n < laces - 1; n++) {
2375
                        int64_t snum;
2376
                        int r;
2377
                        r = matroska_ebmlnum_sint (data, size, &snum);
2378
                        if (r < 0) {
2379
                            av_log(matroska->ctx, AV_LOG_INFO,
2380
                                   "EBML block data error\n");
2381
                            break;
2382
                        }
2383
                        data += r;
2384
                        size -= r;
2385
                        lace_size[n] = lace_size[n - 1] + snum;
2386
                        total += lace_size[n];
2387
                    }
2388
                    lace_size[n] = size - total;
2389
                    break;
2390
                }
2391
            }
2392
            break;
2393
    }
2394

    
2395
    if (res == 0) {
2396
        uint64_t timecode = AV_NOPTS_VALUE;
2397

    
2398
        if (cluster_time != (uint64_t)-1
2399
            && (block_time >= 0 || cluster_time >= -block_time))
2400
            timecode = cluster_time + block_time;
2401

    
2402
        for (n = 0; n < laces; n++) {
2403
            if (st->codec->codec_id == CODEC_ID_RA_288 ||
2404
                st->codec->codec_id == CODEC_ID_COOK ||
2405
                st->codec->codec_id == CODEC_ID_ATRAC3) {
2406
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2407
                int a = st->codec->block_align;
2408
                int sps = audiotrack->sub_packet_size;
2409
                int cfs = audiotrack->coded_framesize;
2410
                int h = audiotrack->sub_packet_h;
2411
                int y = audiotrack->sub_packet_cnt;
2412
                int w = audiotrack->frame_size;
2413
                int x;
2414

    
2415
                if (!audiotrack->pkt_cnt) {
2416
                    if (st->codec->codec_id == CODEC_ID_RA_288)
2417
                        for (x=0; x<h/2; x++)
2418
                            memcpy(audiotrack->buf+x*2*w+y*cfs,
2419
                                   data+x*cfs, cfs);
2420
                    else
2421
                        for (x=0; x<w/sps; x++)
2422
                            memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2423

    
2424
                    if (++audiotrack->sub_packet_cnt >= h) {
2425
                        audiotrack->sub_packet_cnt = 0;
2426
                        audiotrack->pkt_cnt = h*w / a;
2427
                    }
2428
                }
2429
                while (audiotrack->pkt_cnt) {
2430
                    pkt = av_mallocz(sizeof(AVPacket));
2431
                    av_new_packet(pkt, a);
2432
                    memcpy(pkt->data, audiotrack->buf
2433
                           + a * (h*w / a - audiotrack->pkt_cnt--), a);
2434
                    pkt->pos = pos;
2435
                    pkt->stream_index = matroska->tracks[track]->stream_index;
2436
                    matroska_queue_packet(matroska, pkt);
2437
                }
2438
            } else {
2439
                int offset = 0;
2440

    
2441
                if (st->codec->codec_id == CODEC_ID_TEXT
2442
                    && ((MatroskaSubtitleTrack *)(matroska->tracks[track]))->ass) {
2443
                    int i;
2444
                    for (i=0; i<8 && data[offset]; offset++)
2445
                        if (data[offset] == ',')
2446
                            i++;
2447
                }
2448

    
2449
                pkt = av_mallocz(sizeof(AVPacket));
2450
                /* XXX: prevent data copy... */
2451
                if (av_new_packet(pkt, lace_size[n]-offset) < 0) {
2452
                    res = AVERROR(ENOMEM);
2453
                    n = laces-1;
2454
                    break;
2455
                }
2456
                memcpy (pkt->data, data+offset, lace_size[n]-offset);
2457

    
2458
                if (n == 0)
2459
                    pkt->flags = is_keyframe;
2460
                pkt->stream_index = matroska->tracks[track]->stream_index;
2461

    
2462
                pkt->pts = timecode;
2463
                pkt->pos = pos;
2464
                pkt->duration = duration;
2465

    
2466
                matroska_queue_packet(matroska, pkt);
2467
            }
2468

    
2469
            if (timecode != AV_NOPTS_VALUE)
2470
                timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2471
            data += lace_size[n];
2472
        }
2473
    }
2474

    
2475
    av_free(lace_size);
2476
    av_free(origdata);
2477
    return res;
2478
}
2479

    
2480
static int
2481
matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2482
                           uint64_t              cluster_time)
2483
{
2484
    int res = 0;
2485
    uint32_t id;
2486
    int is_bframe = 0;
2487
    int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2488
    uint64_t duration = AV_NOPTS_VALUE;
2489
    uint8_t *data;
2490
    int size = 0;
2491
    int64_t pos = 0;
2492

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

    
2495
    while (res == 0) {
2496
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2497
            res = AVERROR(EIO);
2498
            break;
2499
        } else if (matroska->level_up) {
2500
            matroska->level_up--;
2501
            break;
2502
        }
2503

    
2504
        switch (id) {
2505
            /* one block inside the group. Note, block parsing is one
2506
             * of the harder things, so this code is a bit complicated.
2507
             * See http://www.matroska.org/ for documentation. */
2508
            case MATROSKA_ID_BLOCK: {
2509
                pos = url_ftell(matroska->ctx->pb);
2510
                res = ebml_read_binary(matroska, &id, &data, &size);
2511
                break;
2512
            }
2513

    
2514
            case MATROSKA_ID_BLOCKDURATION: {
2515
                if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2516
                    break;
2517
                break;
2518
            }
2519

    
2520
            case MATROSKA_ID_BLOCKREFERENCE: {
2521
                int64_t num;
2522
                /* We've found a reference, so not even the first frame in
2523
                 * the lace is a key frame. */
2524
                is_keyframe = 0;
2525
                if (last_num_packets != matroska->num_packets)
2526
                    matroska->packets[last_num_packets]->flags = 0;
2527
                if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
2528
                    break;
2529
                if (num > 0)
2530
                    is_bframe = 1;
2531
                break;
2532
            }
2533

    
2534
            default:
2535
                av_log(matroska->ctx, AV_LOG_INFO,
2536
                       "Unknown entry 0x%x in blockgroup data\n", id);
2537
                /* fall-through */
2538

    
2539
            case EBML_ID_VOID:
2540
                res = ebml_read_skip(matroska);
2541
                break;
2542
        }
2543

    
2544
        if (matroska->level_up) {
2545
            matroska->level_up--;
2546
            break;
2547
        }
2548
    }
2549

    
2550
    if (res)
2551
        return res;
2552

    
2553
    if (size > 0)
2554
        res = matroska_parse_block(matroska, data, size, pos, cluster_time,
2555
                                   duration, is_keyframe, is_bframe);
2556

    
2557
    return res;
2558
}
2559

    
2560
static int
2561
matroska_parse_cluster (MatroskaDemuxContext *matroska)
2562
{
2563
    int res = 0;
2564
    uint32_t id;
2565
    uint64_t cluster_time = 0;
2566
    uint8_t *data;
2567
    int64_t pos;
2568
    int size;
2569

    
2570
    av_log(matroska->ctx, AV_LOG_DEBUG,
2571
           "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
2572

    
2573
    while (res == 0) {
2574
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2575
            res = AVERROR(EIO);
2576
            break;
2577
        } else if (matroska->level_up) {
2578
            matroska->level_up--;
2579
            break;
2580
        }
2581

    
2582
        switch (id) {
2583
            /* cluster timecode */
2584
            case MATROSKA_ID_CLUSTERTIMECODE: {
2585
                uint64_t num;
2586
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
2587
                    break;
2588
                cluster_time = num;
2589
                break;
2590
            }
2591

    
2592
                /* a group of blocks inside a cluster */
2593
            case MATROSKA_ID_BLOCKGROUP:
2594
                if ((res = ebml_read_master(matroska, &id)) < 0)
2595
                    break;
2596
                res = matroska_parse_blockgroup(matroska, cluster_time);
2597
                break;
2598

    
2599
            case MATROSKA_ID_SIMPLEBLOCK:
2600
                pos = url_ftell(matroska->ctx->pb);
2601
                res = ebml_read_binary(matroska, &id, &data, &size);
2602
                if (res == 0)
2603
                    res = matroska_parse_block(matroska, data, size, pos,
2604
                                               cluster_time, AV_NOPTS_VALUE,
2605
                                               -1, 0);
2606
                break;
2607

    
2608
            default:
2609
                av_log(matroska->ctx, AV_LOG_INFO,
2610
                       "Unknown entry 0x%x in cluster data\n", id);
2611
                /* fall-through */
2612

    
2613
            case EBML_ID_VOID:
2614
                res = ebml_read_skip(matroska);
2615
                break;
2616
        }
2617

    
2618
        if (matroska->level_up) {
2619
            matroska->level_up--;
2620
            break;
2621
        }
2622
    }
2623

    
2624
    return res;
2625
}
2626

    
2627
static int
2628
matroska_read_packet (AVFormatContext *s,
2629
                      AVPacket        *pkt)
2630
{
2631
    MatroskaDemuxContext *matroska = s->priv_data;
2632
    int res;
2633
    uint32_t id;
2634

    
2635
    /* Read stream until we have a packet queued. */
2636
    while (matroska_deliver_packet(matroska, pkt)) {
2637

    
2638
        /* Have we already reached the end? */
2639
        if (matroska->done)
2640
            return AVERROR(EIO);
2641

    
2642
        res = 0;
2643
        while (res == 0) {
2644
            if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2645
                return AVERROR(EIO);
2646
            } else if (matroska->level_up) {
2647
                matroska->level_up--;
2648
                break;
2649
            }
2650

    
2651
            switch (id) {
2652
                case MATROSKA_ID_CLUSTER:
2653
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2654
                        break;
2655
                    if ((res = matroska_parse_cluster(matroska)) == 0)
2656
                        res = 1; /* Parsed one cluster, let's get out. */
2657
                    break;
2658

    
2659
                default:
2660
                case EBML_ID_VOID:
2661
                    res = ebml_read_skip(matroska);
2662
                    break;
2663
            }
2664

    
2665
            if (matroska->level_up) {
2666
                matroska->level_up--;
2667
                break;
2668
            }
2669
        }
2670

    
2671
        if (res == -1)
2672
            matroska->done = 1;
2673
    }
2674

    
2675
    return 0;
2676
}
2677

    
2678
static int
2679
matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
2680
                    int flags)
2681
{
2682
    MatroskaDemuxContext *matroska = s->priv_data;
2683
    AVStream *st = s->streams[stream_index];
2684
    int index;
2685

    
2686
    /* find index entry */
2687
    index = av_index_search_timestamp(st, timestamp, flags);
2688
    if (index < 0)
2689
        return 0;
2690

    
2691
    matroska_clear_queue(matroska);
2692

    
2693
    /* do the seek */
2694
    url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
2695
    matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
2696
    matroska->skip_to_stream = st;
2697
    matroska->num_packets = 0;
2698
    matroska->peek_id = 0;
2699
    return 0;
2700
}
2701

    
2702
static int
2703
matroska_read_close (AVFormatContext *s)
2704
{
2705
    MatroskaDemuxContext *matroska = s->priv_data;
2706
    int n = 0;
2707

    
2708
    av_free(matroska->writing_app);
2709
    av_free(matroska->muxing_app);
2710
    av_free(matroska->index);
2711

    
2712
    matroska_clear_queue(matroska);
2713

    
2714
    for (n = 0; n < matroska->num_tracks; n++) {
2715
        MatroskaTrack *track = matroska->tracks[n];
2716
        av_free(track->codec_id);
2717
        av_free(track->codec_name);
2718
        av_free(track->codec_priv);
2719
        av_free(track->name);
2720

    
2721
        if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2722
            MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2723
            av_free(audiotrack->buf);
2724
        }
2725

    
2726
        av_free(track);
2727
    }
2728

    
2729
    return 0;
2730
}
2731

    
2732
AVInputFormat matroska_demuxer = {
2733
    "matroska",
2734
    "Matroska file format",
2735
    sizeof(MatroskaDemuxContext),
2736
    matroska_probe,
2737
    matroska_read_header,
2738
    matroska_read_packet,
2739
    matroska_read_close,
2740
    matroska_read_seek,
2741
};