Statistics
| Branch: | Revision:

ffmpeg / libavformat / matroskadec.c @ f009e36f

History | View | Annotate | Download (105 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 "isom.h"
35
#include "matroska.h"
36
#include "libavcodec/mpeg4audio.h"
37
#include "libavutil/intfloat_readwrite.h"
38
#include "libavutil/lzo.h"
39
#ifdef CONFIG_ZLIB
40
#include <zlib.h>
41
#endif
42
#ifdef CONFIG_BZLIB
43
#include <bzlib.h>
44
#endif
45

    
46
typedef struct Track {
47
    MatroskaTrackType type;
48

    
49
    /* Unique track number and track ID. stream_index is the index that
50
     * the calling app uses for this track. */
51
    uint32_t num;
52
    uint32_t uid;
53
    int stream_index;
54

    
55
    char *name;
56
    char language[4];
57

    
58
    char *codec_id;
59

    
60
    unsigned char *codec_priv;
61
    int codec_priv_size;
62

    
63
    double time_scale;
64
    uint64_t default_duration;
65
    MatroskaTrackFlags flags;
66

    
67
    int encoding_scope;
68
    MatroskaTrackEncodingCompAlgo encoding_algo;
69
    uint8_t *encoding_settings;
70
    int encoding_settings_len;
71
} MatroskaTrack;
72

    
73
typedef struct MatroskaVideoTrack {
74
    MatroskaTrack track;
75

    
76
    int pixel_width;
77
    int pixel_height;
78
    int display_width;
79
    int display_height;
80

    
81
    uint32_t fourcc;
82

    
83
    //..
84
} MatroskaVideoTrack;
85

    
86
typedef struct MatroskaAudioTrack {
87
    MatroskaTrack track;
88

    
89
    int channels;
90
    int bitdepth;
91
    int internal_samplerate;
92
    int samplerate;
93
    int block_align;
94

    
95
    /* real audio header */
96
    int coded_framesize;
97
    int sub_packet_h;
98
    int frame_size;
99
    int sub_packet_size;
100
    int sub_packet_cnt;
101
    int pkt_cnt;
102
    uint8_t *buf;
103
    //..
104
} MatroskaAudioTrack;
105

    
106
typedef struct MatroskaSubtitleTrack {
107
    MatroskaTrack track;
108
    //..
109
} MatroskaSubtitleTrack;
110

    
111
#define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
112
                                    sizeof(MatroskaAudioTrack), \
113
                                    sizeof(MatroskaSubtitleTrack)))
114

    
115
typedef struct MatroskaLevel {
116
    uint64_t start;
117
    uint64_t length;
118
} MatroskaLevel;
119

    
120
typedef struct MatroskaDemuxIndex {
121
  uint64_t        pos;   /* of the corresponding *cluster*! */
122
  uint16_t        track; /* reference to 'num' */
123
  uint64_t        time;  /* in nanoseconds */
124
} MatroskaDemuxIndex;
125

    
126
typedef struct MatroskaDemuxContext {
127
    AVFormatContext *ctx;
128

    
129
    /* ebml stuff */
130
    int num_levels;
131
    MatroskaLevel levels[EBML_MAX_DEPTH];
132
    int level_up;
133

    
134
    /* timescale in the file */
135
    int64_t time_scale;
136

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

    
143
    /* cache for ID peeking */
144
    uint32_t peek_id;
145

    
146
    /* byte position of the segment inside the stream */
147
    offset_t segment_start;
148

    
149
    /* The packet queue. */
150
    AVPacket **packets;
151
    int num_packets;
152

    
153
    /* have we already parse metadata/cues/clusters? */
154
    int metadata_parsed;
155
    int index_parsed;
156
    int done;
157

    
158
    /* The index for seeking. */
159
    int num_indexes;
160
    MatroskaDemuxIndex *index;
161

    
162
    /* What to skip before effectively reading a packet. */
163
    int skip_to_keyframe;
164
    AVStream *skip_to_stream;
165
} MatroskaDemuxContext;
166

    
167
#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
168

    
169
/*
170
 * The first few functions handle EBML file parsing. The rest
171
 * is the document interpretation. Matroska really just is a
172
 * EBML file.
173
 */
174

    
175
/*
176
 * Return: the amount of levels in the hierarchy that the
177
 * current element lies higher than the previous one.
178
 * The opposite isn't done - that's auto-done using master
179
 * element reading.
180
 */
181

    
182
static int
183
ebml_read_element_level_up (MatroskaDemuxContext *matroska)
184
{
185
    ByteIOContext *pb = matroska->ctx->pb;
186
    offset_t pos = url_ftell(pb);
187
    int num = 0;
188

    
189
    while (matroska->num_levels > 0) {
190
        MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
191

    
192
        if (pos >= level->start + level->length) {
193
            matroska->num_levels--;
194
            num++;
195
        } else {
196
            break;
197
        }
198
    }
199

    
200
    return num;
201
}
202

    
203
/*
204
 * Read: an "EBML number", which is defined as a variable-length
205
 * array of bytes. The first byte indicates the length by giving a
206
 * number of 0-bits followed by a one. The position of the first
207
 * "one" bit inside the first byte indicates the length of this
208
 * number.
209
 * Returns: num. of bytes read. < 0 on error.
210
 */
211

    
212
static int
213
ebml_read_num (MatroskaDemuxContext *matroska,
214
               int                   max_size,
215
               uint64_t             *number)
216
{
217
    ByteIOContext *pb = matroska->ctx->pb;
218
    int len_mask = 0x80, read = 1, n = 1;
219
    int64_t total = 0;
220

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

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

    
248
    /* read out length */
249
    total &= ~len_mask;
250
    while (n++ < read)
251
        total = (total << 8) | get_byte(pb);
252

    
253
    *number = total;
254

    
255
    return read;
256
}
257

    
258
/*
259
 * Read: the element content data ID.
260
 * Return: the number of bytes read or < 0 on error.
261
 */
262

    
263
static int
264
ebml_read_element_id (MatroskaDemuxContext *matroska,
265
                      uint32_t             *id,
266
                      int                  *level_up)
267
{
268
    int read;
269
    uint64_t total;
270

    
271
    /* if we re-call this, use our cached ID */
272
    if (matroska->peek_id != 0) {
273
        if (level_up)
274
            *level_up = 0;
275
        *id = matroska->peek_id;
276
        return 0;
277
    }
278

    
279
    /* read out the "EBML number", include tag in ID */
280
    if ((read = ebml_read_num(matroska, 4, &total)) < 0)
281
        return read;
282
    *id = matroska->peek_id  = total | (1 << (read * 7));
283

    
284
    /* level tracking */
285
    if (level_up)
286
        *level_up = ebml_read_element_level_up(matroska);
287

    
288
    return read;
289
}
290

    
291
/*
292
 * Read: element content length.
293
 * Return: the number of bytes read or < 0 on error.
294
 */
295

    
296
static int
297
ebml_read_element_length (MatroskaDemuxContext *matroska,
298
                          uint64_t             *length)
299
{
300
    /* clear cache since we're now beyond that data point */
301
    matroska->peek_id = 0;
302

    
303
    /* read out the "EBML number", include tag in ID */
304
    return ebml_read_num(matroska, 8, length);
305
}
306

    
307
/*
308
 * Return: the ID of the next element, or 0 on error.
309
 * Level_up contains the amount of levels that this
310
 * next element lies higher than the previous one.
311
 */
312

    
313
static uint32_t
314
ebml_peek_id (MatroskaDemuxContext *matroska,
315
              int                  *level_up)
316
{
317
    uint32_t id;
318

    
319
    if (ebml_read_element_id(matroska, &id, level_up) < 0)
320
        return 0;
321

    
322
    return id;
323
}
324

    
325
/*
326
 * Seek to a given offset.
327
 * 0 is success, -1 is failure.
328
 */
329

    
330
static int
331
ebml_read_seek (MatroskaDemuxContext *matroska,
332
                offset_t              offset)
333
{
334
    ByteIOContext *pb = matroska->ctx->pb;
335

    
336
    /* clear ID cache, if any */
337
    matroska->peek_id = 0;
338

    
339
    return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
340
}
341

    
342
/*
343
 * Skip the next element.
344
 * 0 is success, -1 is failure.
345
 */
346

    
347
static int
348
ebml_read_skip (MatroskaDemuxContext *matroska)
349
{
350
    ByteIOContext *pb = matroska->ctx->pb;
351
    uint32_t id;
352
    uint64_t length;
353
    int res;
354

    
355
    if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
356
        (res = ebml_read_element_length(matroska, &length)) < 0)
357
        return res;
358

    
359
    url_fskip(pb, length);
360

    
361
    return 0;
362
}
363

    
364
/*
365
 * Read the next element as an unsigned int.
366
 * 0 is success, < 0 is failure.
367
 */
368

    
369
static int
370
ebml_read_uint (MatroskaDemuxContext *matroska,
371
                uint32_t             *id,
372
                uint64_t             *num)
373
{
374
    ByteIOContext *pb = matroska->ctx->pb;
375
    int n = 0, size, res;
376
    uint64_t rlength;
377

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

    
390
    /* big-endian ordening; build up number */
391
    *num = 0;
392
    while (n++ < size)
393
        *num = (*num << 8) | get_byte(pb);
394

    
395
    return 0;
396
}
397

    
398
/*
399
 * Read the next element as a signed int.
400
 * 0 is success, < 0 is failure.
401
 */
402

    
403
static int
404
ebml_read_sint (MatroskaDemuxContext *matroska,
405
                uint32_t             *id,
406
                int64_t              *num)
407
{
408
    ByteIOContext *pb = matroska->ctx->pb;
409
    int size, n = 1, negative = 0, res;
410
    uint64_t rlength;
411

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

    
430
    /* make signed */
431
    if (negative)
432
        *num = *num - (1LL << ((8 * size) - 1));
433

    
434
    return 0;
435
}
436

    
437
/*
438
 * Read the next element as a float.
439
 * 0 is success, < 0 is failure.
440
 */
441

    
442
static int
443
ebml_read_float (MatroskaDemuxContext *matroska,
444
                 uint32_t             *id,
445
                 double               *num)
446
{
447
    ByteIOContext *pb = matroska->ctx->pb;
448
    int size, res;
449
    uint64_t rlength;
450

    
451
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
452
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
453
        return res;
454
    size = rlength;
455

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

    
468
    return 0;
469
}
470

    
471
/*
472
 * Read the next element as an ASCII string.
473
 * 0 is success, < 0 is failure.
474
 */
475

    
476
static int
477
ebml_read_ascii (MatroskaDemuxContext *matroska,
478
                 uint32_t             *id,
479
                 char                **str)
480
{
481
    ByteIOContext *pb = matroska->ctx->pb;
482
    int size, res;
483
    uint64_t rlength;
484

    
485
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
486
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
487
        return res;
488
    size = rlength;
489

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

    
505
    return 0;
506
}
507

    
508
/*
509
 * Read the next element as a UTF-8 string.
510
 * 0 is success, < 0 is failure.
511
 */
512

    
513
static int
514
ebml_read_utf8 (MatroskaDemuxContext *matroska,
515
                uint32_t             *id,
516
                char                **str)
517
{
518
  return ebml_read_ascii(matroska, id, str);
519
}
520

    
521
/*
522
 * Read the next element, but only the header. The contents
523
 * are supposed to be sub-elements which can be read separately.
524
 * 0 is success, < 0 is failure.
525
 */
526

    
527
static int
528
ebml_read_master (MatroskaDemuxContext *matroska,
529
                  uint32_t             *id)
530
{
531
    ByteIOContext *pb = matroska->ctx->pb;
532
    uint64_t length;
533
    MatroskaLevel *level;
534
    int res;
535

    
536
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
537
        (res = ebml_read_element_length(matroska, &length)) < 0)
538
        return res;
539

    
540
    /* protect... (Heaven forbids that the '>' is true) */
541
    if (matroska->num_levels >= EBML_MAX_DEPTH) {
542
        av_log(matroska->ctx, AV_LOG_ERROR,
543
               "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
544
        return AVERROR(ENOSYS);
545
    }
546

    
547
    /* remember level */
548
    level = &matroska->levels[matroska->num_levels++];
549
    level->start = url_ftell(pb);
550
    level->length = length;
551

    
552
    return 0;
553
}
554

    
555
/*
556
 * Read the next element as binary data.
557
 * 0 is success, < 0 is failure.
558
 */
559

    
560
static int
561
ebml_read_binary (MatroskaDemuxContext *matroska,
562
                  uint32_t             *id,
563
                  uint8_t             **binary,
564
                  int                  *size)
565
{
566
    ByteIOContext *pb = matroska->ctx->pb;
567
    uint64_t rlength;
568
    int res;
569

    
570
    if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
571
        (res = ebml_read_element_length(matroska, &rlength)) < 0)
572
        return res;
573
    *size = rlength;
574

    
575
    if (!(*binary = av_malloc(*size))) {
576
        av_log(matroska->ctx, AV_LOG_ERROR,
577
               "Memory allocation error\n");
578
        return AVERROR(ENOMEM);
579
    }
580

    
581
    if (get_buffer(pb, *binary, *size) != *size) {
582
        offset_t pos = url_ftell(pb);
583
        av_log(matroska->ctx, AV_LOG_ERROR,
584
               "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
585
        return AVERROR(EIO);
586
    }
587

    
588
    return 0;
589
}
590

    
591
/*
592
 * Read signed/unsigned "EBML" numbers.
593
 * Return: number of bytes processed, < 0 on error.
594
 * XXX: use ebml_read_num().
595
 */
596

    
597
static int
598
matroska_ebmlnum_uint (uint8_t  *data,
599
                       uint32_t  size,
600
                       uint64_t *num)
601
{
602
    int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
603
    uint64_t total;
604

    
605
    if (size <= 0)
606
        return AVERROR_INVALIDDATA;
607

    
608
    total = data[0];
609
    while (read <= 8 && !(total & len_mask)) {
610
        read++;
611
        len_mask >>= 1;
612
    }
613
    if (read > 8)
614
        return AVERROR_INVALIDDATA;
615

    
616
    if ((total &= (len_mask - 1)) == len_mask - 1)
617
        num_ffs++;
618
    if (size < read)
619
        return AVERROR_INVALIDDATA;
620
    while (n < read) {
621
        if (data[n] == 0xff)
622
            num_ffs++;
623
        total = (total << 8) | data[n];
624
        n++;
625
    }
626

    
627
    if (read == num_ffs)
628
        *num = (uint64_t)-1;
629
    else
630
        *num = total;
631

    
632
    return read;
633
}
634

    
635
/*
636
 * Same as above, but signed.
637
 */
638

    
639
static int
640
matroska_ebmlnum_sint (uint8_t  *data,
641
                       uint32_t  size,
642
                       int64_t  *num)
643
{
644
    uint64_t unum;
645
    int res;
646

    
647
    /* read as unsigned number first */
648
    if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
649
        return res;
650

    
651
    /* make signed (weird way) */
652
    if (unum == (uint64_t)-1)
653
        *num = INT64_MAX;
654
    else
655
        *num = unum - ((1LL << ((7 * res) - 1)) - 1);
656

    
657
    return res;
658
}
659

    
660
/*
661
 * Read an EBML header.
662
 * 0 is success, < 0 is failure.
663
 */
664

    
665
static int
666
ebml_read_header (MatroskaDemuxContext *matroska,
667
                  char                **doctype,
668
                  int                  *version)
669
{
670
    uint32_t id;
671
    int level_up, res = 0;
672

    
673
    /* default init */
674
    if (doctype)
675
        *doctype = NULL;
676
    if (version)
677
        *version = 1;
678

    
679
    if (!(id = ebml_peek_id(matroska, &level_up)) ||
680
        level_up != 0 || id != EBML_ID_HEADER) {
681
        av_log(matroska->ctx, AV_LOG_ERROR,
682
               "This is not an EBML file (id=0x%x/0x%x)\n", id, EBML_ID_HEADER);
683
        return AVERROR_INVALIDDATA;
684
    }
685
    if ((res = ebml_read_master(matroska, &id)) < 0)
686
        return res;
687

    
688
    while (res == 0) {
689
        if (!(id = ebml_peek_id(matroska, &level_up)))
690
            return AVERROR(EIO);
691

    
692
        /* end-of-header */
693
        if (level_up)
694
            break;
695

    
696
        switch (id) {
697
            /* is our read version uptodate? */
698
            case EBML_ID_EBMLREADVERSION: {
699
                uint64_t num;
700

    
701
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
702
                    return res;
703
                if (num > EBML_VERSION) {
704
                    av_log(matroska->ctx, AV_LOG_ERROR,
705
                           "EBML version %"PRIu64" (> %d) is not supported\n",
706
                           num, EBML_VERSION);
707
                    return AVERROR_INVALIDDATA;
708
                }
709
                break;
710
            }
711

    
712
            /* we only handle 8 byte lengths at max */
713
            case EBML_ID_EBMLMAXSIZELENGTH: {
714
                uint64_t num;
715

    
716
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
717
                    return res;
718
                if (num > sizeof(uint64_t)) {
719
                    av_log(matroska->ctx, AV_LOG_ERROR,
720
                           "Integers of size %"PRIu64" (> %zd) not supported\n",
721
                           num, sizeof(uint64_t));
722
                    return AVERROR_INVALIDDATA;
723
                }
724
                break;
725
            }
726

    
727
            /* we handle 4 byte IDs at max */
728
            case EBML_ID_EBMLMAXIDLENGTH: {
729
                uint64_t num;
730

    
731
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
732
                    return res;
733
                if (num > sizeof(uint32_t)) {
734
                    av_log(matroska->ctx, AV_LOG_ERROR,
735
                           "IDs of size %"PRIu64" (> %zu) not supported\n",
736
                            num, sizeof(uint32_t));
737
                    return AVERROR_INVALIDDATA;
738
                }
739
                break;
740
            }
741

    
742
            case EBML_ID_DOCTYPE: {
743
                char *text;
744

    
745
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
746
                    return res;
747
                if (doctype) {
748
                    if (*doctype)
749
                        av_free(*doctype);
750
                    *doctype = text;
751
                } else
752
                    av_free(text);
753
                break;
754
            }
755

    
756
            case EBML_ID_DOCTYPEREADVERSION: {
757
                uint64_t num;
758

    
759
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
760
                    return res;
761
                if (version)
762
                    *version = num;
763
                break;
764
            }
765

    
766
            default:
767
                av_log(matroska->ctx, AV_LOG_INFO,
768
                       "Unknown data type 0x%x in EBML header", id);
769
                /* pass-through */
770

    
771
            case EBML_ID_VOID:
772
            /* we ignore these two, as they don't tell us anything we
773
             * care about */
774
            case EBML_ID_EBMLVERSION:
775
            case EBML_ID_DOCTYPEVERSION:
776
                res = ebml_read_skip (matroska);
777
                break;
778
        }
779
    }
780

    
781
    return 0;
782
}
783

    
784

    
785
static int
786
matroska_find_track_by_num (MatroskaDemuxContext *matroska,
787
                            int                   num)
788
{
789
    int i;
790

    
791
    for (i = 0; i < matroska->num_tracks; i++)
792
        if (matroska->tracks[i]->num == num)
793
            return i;
794

    
795
    return -1;
796
}
797

    
798

    
799
/*
800
 * Put one packet in an application-supplied AVPacket struct.
801
 * Returns 0 on success or -1 on failure.
802
 */
803

    
804
static int
805
matroska_deliver_packet (MatroskaDemuxContext *matroska,
806
                         AVPacket             *pkt)
807
{
808
    if (matroska->num_packets > 0) {
809
        memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
810
        av_free(matroska->packets[0]);
811
        if (matroska->num_packets > 1) {
812
            memmove(&matroska->packets[0], &matroska->packets[1],
813
                    (matroska->num_packets - 1) * sizeof(AVPacket *));
814
            matroska->packets =
815
                av_realloc(matroska->packets, (matroska->num_packets - 1) *
816
                           sizeof(AVPacket *));
817
        } else {
818
            av_freep(&matroska->packets);
819
        }
820
        matroska->num_packets--;
821
        return 0;
822
    }
823

    
824
    return -1;
825
}
826

    
827
/*
828
 * Put a packet into our internal queue. Will be delivered to the
829
 * user/application during the next get_packet() call.
830
 */
831

    
832
static void
833
matroska_queue_packet (MatroskaDemuxContext *matroska,
834
                       AVPacket             *pkt)
835
{
836
    matroska->packets =
837
        av_realloc(matroska->packets, (matroska->num_packets + 1) *
838
                   sizeof(AVPacket *));
839
    matroska->packets[matroska->num_packets] = pkt;
840
    matroska->num_packets++;
841
}
842

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

    
861

    
862
/*
863
 * Autodetecting...
864
 */
865

    
866
static int
867
matroska_probe (AVProbeData *p)
868
{
869
    uint64_t total = 0;
870
    int len_mask = 0x80, size = 1, n = 1;
871
    uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
872

    
873
    /* ebml header? */
874
    if (AV_RB32(p->buf) != EBML_ID_HEADER)
875
        return 0;
876

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

    
889
    /* does the probe data contain the whole header? */
890
    if (p->buf_size < 4 + size + total)
891
      return 0;
892

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

    
901
    return 0;
902
}
903

    
904
/*
905
 * From here on, it's all XML-style DTD stuff... Needs no comments.
906
 */
907

    
908
static int
909
matroska_parse_info (MatroskaDemuxContext *matroska)
910
{
911
    int res = 0;
912
    uint32_t id;
913

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

    
916
    while (res == 0) {
917
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
918
            res = AVERROR(EIO);
919
            break;
920
        } else if (matroska->level_up) {
921
            matroska->level_up--;
922
            break;
923
        }
924

    
925
        switch (id) {
926
            /* cluster timecode */
927
            case MATROSKA_ID_TIMECODESCALE: {
928
                uint64_t num;
929
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
930
                    break;
931
                matroska->time_scale = num;
932
                break;
933
            }
934

    
935
            case MATROSKA_ID_DURATION: {
936
                double num;
937
                if ((res = ebml_read_float(matroska, &id, &num)) < 0)
938
                    break;
939
                matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
940
                break;
941
            }
942

    
943
            case MATROSKA_ID_TITLE: {
944
                char *text;
945
                if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
946
                    break;
947
                strncpy(matroska->ctx->title, text,
948
                        sizeof(matroska->ctx->title)-1);
949
                av_free(text);
950
                break;
951
            }
952

    
953
            default:
954
                av_log(matroska->ctx, AV_LOG_INFO,
955
                       "Unknown entry 0x%x in info header\n", id);
956
                /* fall-through */
957

    
958
            case MATROSKA_ID_WRITINGAPP:
959
            case MATROSKA_ID_MUXINGAPP:
960
            case MATROSKA_ID_DATEUTC:
961
            case MATROSKA_ID_SEGMENTUID:
962
            case EBML_ID_VOID:
963
                res = ebml_read_skip(matroska);
964
                break;
965
        }
966

    
967
        if (matroska->level_up) {
968
            matroska->level_up--;
969
            break;
970
        }
971
    }
972

    
973
    return res;
974
}
975

    
976
static int
977
matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
978
{
979
    uint8_t* data = *buf;
980
    int isize = *buf_size;
981
    uint8_t* pkt_data = NULL;
982
    int pkt_size = isize;
983
    int result = 0;
984
    int olen;
985

    
986
    switch (track->encoding_algo) {
987
    case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
988
        return track->encoding_settings_len;
989
    case MATROSKA_TRACK_ENCODING_COMP_LZO:
990
        do {
991
            olen = pkt_size *= 3;
992
            pkt_data = av_realloc(pkt_data,
993
                                  pkt_size+LZO_OUTPUT_PADDING);
994
            result = lzo1x_decode(pkt_data, &olen, data, &isize);
995
        } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
996
        if (result)
997
            goto failed;
998
        pkt_size -= olen;
999
        break;
1000
#ifdef CONFIG_ZLIB
1001
    case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1002
        z_stream zstream = {0};
1003
        if (inflateInit(&zstream) != Z_OK)
1004
            return -1;
1005
        zstream.next_in = data;
1006
        zstream.avail_in = isize;
1007
        do {
1008
            pkt_size *= 3;
1009
            pkt_data = av_realloc(pkt_data, pkt_size);
1010
            zstream.avail_out = pkt_size - zstream.total_out;
1011
            zstream.next_out = pkt_data + zstream.total_out;
1012
            result = inflate(&zstream, Z_NO_FLUSH);
1013
        } while (result==Z_OK && pkt_size<10000000);
1014
        pkt_size = zstream.total_out;
1015
        inflateEnd(&zstream);
1016
        if (result != Z_STREAM_END)
1017
            goto failed;
1018
        break;
1019
    }
1020
#endif
1021
#ifdef CONFIG_BZLIB
1022
    case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1023
        bz_stream bzstream = {0};
1024
        if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1025
            return -1;
1026
        bzstream.next_in = data;
1027
        bzstream.avail_in = isize;
1028
        do {
1029
            pkt_size *= 3;
1030
            pkt_data = av_realloc(pkt_data, pkt_size);
1031
            bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1032
            bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1033
            result = BZ2_bzDecompress(&bzstream);
1034
        } while (result==BZ_OK && pkt_size<10000000);
1035
        pkt_size = bzstream.total_out_lo32;
1036
        BZ2_bzDecompressEnd(&bzstream);
1037
        if (result != BZ_STREAM_END)
1038
            goto failed;
1039
        break;
1040
    }
1041
#endif
1042
    }
1043

    
1044
    *buf = pkt_data;
1045
    *buf_size = pkt_size;
1046
    return 0;
1047
 failed:
1048
    av_free(pkt_data);
1049
    return -1;
1050
}
1051

    
1052
static int
1053
matroska_add_stream (MatroskaDemuxContext *matroska)
1054
{
1055
    int res = 0;
1056
    uint32_t id;
1057
    MatroskaTrack *track;
1058

    
1059
    /* start with the master */
1060
    if ((res = ebml_read_master(matroska, &id)) < 0)
1061
        return res;
1062

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

    
1065
    /* Allocate a generic track. */
1066
    track = av_mallocz(MAX_TRACK_SIZE);
1067
    track->time_scale = 1.0;
1068
    strcpy(track->language, "eng");
1069

    
1070
    /* try reading the trackentry headers */
1071
    while (res == 0) {
1072
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1073
            res = AVERROR(EIO);
1074
            break;
1075
        } else if (matroska->level_up > 0) {
1076
            matroska->level_up--;
1077
            break;
1078
        }
1079

    
1080
        switch (id) {
1081
            /* track number (unique stream ID) */
1082
            case MATROSKA_ID_TRACKNUMBER: {
1083
                uint64_t num;
1084
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1085
                    break;
1086
                track->num = num;
1087
                break;
1088
            }
1089

    
1090
            /* track UID (unique identifier) */
1091
            case MATROSKA_ID_TRACKUID: {
1092
                uint64_t num;
1093
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1094
                    break;
1095
                track->uid = num;
1096
                break;
1097
            }
1098

    
1099
            /* track type (video, audio, combined, subtitle, etc.) */
1100
            case MATROSKA_ID_TRACKTYPE: {
1101
                uint64_t num;
1102
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1103
                    break;
1104
                if (track->type && track->type != num) {
1105
                    av_log(matroska->ctx, AV_LOG_INFO,
1106
                           "More than one tracktype in an entry - skip\n");
1107
                    break;
1108
                }
1109
                track->type = num;
1110

    
1111
                switch (track->type) {
1112
                    case MATROSKA_TRACK_TYPE_VIDEO:
1113
                    case MATROSKA_TRACK_TYPE_AUDIO:
1114
                    case MATROSKA_TRACK_TYPE_SUBTITLE:
1115
                        break;
1116
                    case MATROSKA_TRACK_TYPE_COMPLEX:
1117
                    case MATROSKA_TRACK_TYPE_LOGO:
1118
                    case MATROSKA_TRACK_TYPE_CONTROL:
1119
                    default:
1120
                        av_log(matroska->ctx, AV_LOG_INFO,
1121
                               "Unknown or unsupported track type 0x%x\n",
1122
                               track->type);
1123
                        track->type = MATROSKA_TRACK_TYPE_NONE;
1124
                        break;
1125
                }
1126
                break;
1127
            }
1128

    
1129
            /* tracktype specific stuff for video */
1130
            case MATROSKA_ID_TRACKVIDEO: {
1131
                MatroskaVideoTrack *videotrack;
1132
                if (!track->type)
1133
                    track->type = MATROSKA_TRACK_TYPE_VIDEO;
1134
                if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1135
                    av_log(matroska->ctx, AV_LOG_INFO,
1136
                           "video data in non-video track - ignoring\n");
1137
                    res = AVERROR_INVALIDDATA;
1138
                    break;
1139
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1140
                    break;
1141
                videotrack = (MatroskaVideoTrack *)track;
1142

    
1143
                while (res == 0) {
1144
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1145
                        res = AVERROR(EIO);
1146
                        break;
1147
                    } else if (matroska->level_up > 0) {
1148
                        matroska->level_up--;
1149
                        break;
1150
                    }
1151

    
1152
                    switch (id) {
1153
                        /* fixme, this should be one-up, but I get it here */
1154
                        case MATROSKA_ID_TRACKDEFAULTDURATION: {
1155
                            uint64_t num;
1156
                            if ((res = ebml_read_uint (matroska, &id,
1157
                                                       &num)) < 0)
1158
                                break;
1159
                            track->default_duration = num;
1160
                            break;
1161
                        }
1162

    
1163
                        /* video framerate */
1164
                        case MATROSKA_ID_VIDEOFRAMERATE: {
1165
                            double num;
1166
                            if ((res = ebml_read_float(matroska, &id,
1167
                                                       &num)) < 0)
1168
                                break;
1169
                            if (!track->default_duration)
1170
                                track->default_duration = 1000000000/num;
1171
                            break;
1172
                        }
1173

    
1174
                        /* width of the size to display the video at */
1175
                        case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1176
                            uint64_t num;
1177
                            if ((res = ebml_read_uint(matroska, &id,
1178
                                                      &num)) < 0)
1179
                                break;
1180
                            videotrack->display_width = num;
1181
                            break;
1182
                        }
1183

    
1184
                        /* height of the size to display the video at */
1185
                        case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1186
                            uint64_t num;
1187
                            if ((res = ebml_read_uint(matroska, &id,
1188
                                                      &num)) < 0)
1189
                                break;
1190
                            videotrack->display_height = num;
1191
                            break;
1192
                        }
1193

    
1194
                        /* width of the video in the file */
1195
                        case MATROSKA_ID_VIDEOPIXELWIDTH: {
1196
                            uint64_t num;
1197
                            if ((res = ebml_read_uint(matroska, &id,
1198
                                                      &num)) < 0)
1199
                                break;
1200
                            videotrack->pixel_width = num;
1201
                            break;
1202
                        }
1203

    
1204
                        /* height of the video in the file */
1205
                        case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1206
                            uint64_t num;
1207
                            if ((res = ebml_read_uint(matroska, &id,
1208
                                                      &num)) < 0)
1209
                                break;
1210
                            videotrack->pixel_height = num;
1211
                            break;
1212
                        }
1213

    
1214
                        /* whether the video is interlaced */
1215
                        case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1216
                            uint64_t num;
1217
                            if ((res = ebml_read_uint(matroska, &id,
1218
                                                      &num)) < 0)
1219
                                break;
1220
                            if (num)
1221
                                track->flags |=
1222
                                    MATROSKA_VIDEOTRACK_INTERLACED;
1223
                            else
1224
                                track->flags &=
1225
                                    ~MATROSKA_VIDEOTRACK_INTERLACED;
1226
                            break;
1227
                        }
1228

    
1229
                        /* colorspace (only matters for raw video)
1230
                         * fourcc */
1231
                        case MATROSKA_ID_VIDEOCOLORSPACE: {
1232
                            uint64_t num;
1233
                            if ((res = ebml_read_uint(matroska, &id,
1234
                                                      &num)) < 0)
1235
                                break;
1236
                            videotrack->fourcc = num;
1237
                            break;
1238
                        }
1239

    
1240
                        default:
1241
                            av_log(matroska->ctx, AV_LOG_INFO,
1242
                                   "Unknown video track header entry "
1243
                                   "0x%x - ignoring\n", id);
1244
                            /* pass-through */
1245

    
1246
                        case MATROSKA_ID_VIDEOSTEREOMODE:
1247
                        case MATROSKA_ID_VIDEOASPECTRATIO:
1248
                        case EBML_ID_VOID:
1249
                            res = ebml_read_skip(matroska);
1250
                            break;
1251
                    }
1252

    
1253
                    if (matroska->level_up) {
1254
                        matroska->level_up--;
1255
                        break;
1256
                    }
1257
                }
1258
                break;
1259
            }
1260

    
1261
            /* tracktype specific stuff for audio */
1262
            case MATROSKA_ID_TRACKAUDIO: {
1263
                MatroskaAudioTrack *audiotrack;
1264
                if (!track->type)
1265
                    track->type = MATROSKA_TRACK_TYPE_AUDIO;
1266
                if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1267
                    av_log(matroska->ctx, AV_LOG_INFO,
1268
                           "audio data in non-audio track - ignoring\n");
1269
                    res = AVERROR_INVALIDDATA;
1270
                    break;
1271
                } else if ((res = ebml_read_master(matroska, &id)) < 0)
1272
                    break;
1273
                audiotrack = (MatroskaAudioTrack *)track;
1274
                audiotrack->channels = 1;
1275
                audiotrack->samplerate = 8000;
1276

    
1277
                while (res == 0) {
1278
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1279
                        res = AVERROR(EIO);
1280
                        break;
1281
                    } else if (matroska->level_up > 0) {
1282
                        matroska->level_up--;
1283
                        break;
1284
                    }
1285

    
1286
                    switch (id) {
1287
                        /* samplerate */
1288
                        case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1289
                            double num;
1290
                            if ((res = ebml_read_float(matroska, &id,
1291
                                                       &num)) < 0)
1292
                                break;
1293
                            audiotrack->internal_samplerate =
1294
                            audiotrack->samplerate = num;
1295
                            break;
1296
                        }
1297

    
1298
                        case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1299
                            double num;
1300
                            if ((res = ebml_read_float(matroska, &id,
1301
                                                       &num)) < 0)
1302
                                break;
1303
                            audiotrack->samplerate = num;
1304
                            break;
1305
                        }
1306

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

    
1317
                            /* channels */
1318
                        case MATROSKA_ID_AUDIOCHANNELS: {
1319
                            uint64_t num;
1320
                            if ((res = ebml_read_uint(matroska, &id,
1321
                                                      &num)) < 0)
1322
                                break;
1323
                            audiotrack->channels = num;
1324
                            break;
1325
                        }
1326

    
1327
                        default:
1328
                            av_log(matroska->ctx, AV_LOG_INFO,
1329
                                   "Unknown audio track header entry "
1330
                                   "0x%x - ignoring\n", id);
1331
                            /* pass-through */
1332

    
1333
                        case EBML_ID_VOID:
1334
                            res = ebml_read_skip(matroska);
1335
                            break;
1336
                    }
1337

    
1338
                    if (matroska->level_up) {
1339
                        matroska->level_up--;
1340
                        break;
1341
                    }
1342
                }
1343
                break;
1344
            }
1345

    
1346
                /* codec identifier */
1347
            case MATROSKA_ID_CODECID: {
1348
                char *text;
1349
                if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1350
                    break;
1351
                track->codec_id = text;
1352
                break;
1353
            }
1354

    
1355
                /* codec private data */
1356
            case MATROSKA_ID_CODECPRIVATE: {
1357
                uint8_t *data;
1358
                int size;
1359
                if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1360
                    break;
1361
                track->codec_priv = data;
1362
                track->codec_priv_size = size;
1363
                break;
1364
            }
1365

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

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

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

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

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

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

    
1434
            case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1435
                if ((res = ebml_read_master(matroska, &id)) < 0)
1436
                    break;
1437

    
1438
                while (res == 0) {
1439
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1440
                        res = AVERROR(EIO);
1441
                        break;
1442
                    } else if (matroska->level_up > 0) {
1443
                        matroska->level_up--;
1444
                        break;
1445
                    }
1446

    
1447
                    switch (id) {
1448
                        case MATROSKA_ID_TRACKCONTENTENCODING: {
1449
                            int encoding_scope = 1;
1450
                            if ((res = ebml_read_master(matroska, &id)) < 0)
1451
                                break;
1452

    
1453
                            while (res == 0) {
1454
                                if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1455
                                    res = AVERROR(EIO);
1456
                                    break;
1457
                                } else if (matroska->level_up > 0) {
1458
                                    matroska->level_up--;
1459
                                    break;
1460
                                }
1461

    
1462
                                switch (id) {
1463
                                    case MATROSKA_ID_ENCODINGSCOPE: {
1464
                                        uint64_t num;
1465
                                        if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1466
                                            break;
1467
                                        encoding_scope = num;
1468
                                        break;
1469
                                    }
1470

    
1471
                                    case MATROSKA_ID_ENCODINGTYPE: {
1472
                                        uint64_t num;
1473
                                        if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1474
                                            break;
1475
                                        if (num)
1476
                                            av_log(matroska->ctx, AV_LOG_ERROR,
1477
                                                   "Unsupported encoding type");
1478
                                        break;
1479
                                    }
1480

    
1481
                                    case MATROSKA_ID_ENCODINGCOMPRESSION: {
1482
                                        if ((res = ebml_read_master(matroska, &id)) < 0)
1483
                                            break;
1484

    
1485
                                        while (res == 0) {
1486
                                            if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1487
                                                res = AVERROR(EIO);
1488
                                                break;
1489
                                            } else if (matroska->level_up > 0) {
1490
                                                matroska->level_up--;
1491
                                                break;
1492
                                            }
1493

    
1494
                                            switch (id) {
1495
                                                case MATROSKA_ID_ENCODINGCOMPALGO: {
1496
                                                    uint64_t num;
1497
                                                    if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1498
                                                        break;
1499
                                                    if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1500
#ifdef CONFIG_ZLIB
1501
                                                        num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1502
#endif
1503
#ifdef CONFIG_BZLIB
1504
                                                        num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1505
#endif
1506
                                                        num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1507
                                                        av_log(matroska->ctx, AV_LOG_ERROR,
1508
                                                               "Unsupported compression algo\n");
1509
                                                    track->encoding_algo = num;
1510
                                                    break;
1511
                                                }
1512

    
1513
                                                case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1514
                                                    uint8_t *data;
1515
                                                    int size;
1516
                                                    if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1517
                                                        break;
1518
                                                    track->encoding_settings = data;
1519
                                                    track->encoding_settings_len = size;
1520
                                                    break;
1521
                                                }
1522

    
1523
                                                default:
1524
                                                    av_log(matroska->ctx, AV_LOG_INFO,
1525
                                                           "Unknown compression header entry "
1526
                                                           "0x%x - ignoring\n", id);
1527
                                                    /* pass-through */
1528

    
1529
                                                case EBML_ID_VOID:
1530
                                                    res = ebml_read_skip(matroska);
1531
                                                    break;
1532
                                            }
1533

    
1534
                                            if (matroska->level_up) {
1535
                                                matroska->level_up--;
1536
                                                break;
1537
                                            }
1538
                                        }
1539
                                        break;
1540
                                    }
1541

    
1542
                                    default:
1543
                                        av_log(matroska->ctx, AV_LOG_INFO,
1544
                                               "Unknown content encoding header entry "
1545
                                               "0x%x - ignoring\n", id);
1546
                                        /* pass-through */
1547

    
1548
                                    case EBML_ID_VOID:
1549
                                        res = ebml_read_skip(matroska);
1550
                                        break;
1551
                                }
1552

    
1553
                                if (matroska->level_up) {
1554
                                    matroska->level_up--;
1555
                                    break;
1556
                                }
1557
                            }
1558

    
1559
                            track->encoding_scope = encoding_scope;
1560
                            break;
1561
                        }
1562

    
1563
                        default:
1564
                            av_log(matroska->ctx, AV_LOG_INFO,
1565
                                   "Unknown content encodings header entry "
1566
                                   "0x%x - ignoring\n", id);
1567
                            /* pass-through */
1568

    
1569
                        case EBML_ID_VOID:
1570
                            res = ebml_read_skip(matroska);
1571
                            break;
1572
                    }
1573

    
1574
                    if (matroska->level_up) {
1575
                        matroska->level_up--;
1576
                        break;
1577
                    }
1578
                }
1579
                break;
1580
            }
1581

    
1582
            case MATROSKA_ID_TRACKTIMECODESCALE: {
1583
                double num;
1584
                if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1585
                    break;
1586
                track->time_scale = num;
1587
                break;
1588
            }
1589

    
1590
            default:
1591
                av_log(matroska->ctx, AV_LOG_INFO,
1592
                       "Unknown track header entry 0x%x - ignoring\n", id);
1593
                /* pass-through */
1594

    
1595
            case EBML_ID_VOID:
1596
            /* we ignore these because they're nothing useful. */
1597
            case MATROSKA_ID_TRACKFLAGFORCED:
1598
            case MATROSKA_ID_CODECNAME:
1599
            case MATROSKA_ID_CODECDECODEALL:
1600
            case MATROSKA_ID_CODECINFOURL:
1601
            case MATROSKA_ID_CODECDOWNLOADURL:
1602
            case MATROSKA_ID_TRACKMINCACHE:
1603
            case MATROSKA_ID_TRACKMAXCACHE:
1604
                res = ebml_read_skip(matroska);
1605
                break;
1606
        }
1607

    
1608
        if (matroska->level_up) {
1609
            matroska->level_up--;
1610
            break;
1611
        }
1612
    }
1613

    
1614
    if (track->codec_priv_size && track->encoding_scope & 2) {
1615
        uint8_t *orig_priv = track->codec_priv;
1616
        int offset = matroska_decode_buffer(&track->codec_priv,
1617
                                            &track->codec_priv_size, track);
1618
        if (offset > 0) {
1619
            track->codec_priv = av_malloc(track->codec_priv_size + offset);
1620
            memcpy(track->codec_priv, track->encoding_settings, offset);
1621
            memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1622
            track->codec_priv_size += offset;
1623
            av_free(orig_priv);
1624
        } else if (!offset) {
1625
            av_free(orig_priv);
1626
        } else
1627
            av_log(matroska->ctx, AV_LOG_ERROR,
1628
                   "Failed to decode codec private data\n");
1629
    }
1630

    
1631
    if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1632
        matroska->tracks[matroska->num_tracks++] = track;
1633
    } else {
1634
        av_free(track);
1635
    }
1636
    return res;
1637
}
1638

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

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

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

    
1656
        switch (id) {
1657
            /* one track within the "all-tracks" header */
1658
            case MATROSKA_ID_TRACKENTRY:
1659
                res = matroska_add_stream(matroska);
1660
                break;
1661

    
1662
            default:
1663
                av_log(matroska->ctx, AV_LOG_INFO,
1664
                       "Unknown entry 0x%x in track header\n", id);
1665
                /* fall-through */
1666

    
1667
            case EBML_ID_VOID:
1668
                res = ebml_read_skip(matroska);
1669
                break;
1670
        }
1671

    
1672
        if (matroska->level_up) {
1673
            matroska->level_up--;
1674
            break;
1675
        }
1676
    }
1677

    
1678
    return res;
1679
}
1680

    
1681
static int
1682
matroska_parse_index (MatroskaDemuxContext *matroska)
1683
{
1684
    int res = 0;
1685
    uint32_t id;
1686
    MatroskaDemuxIndex idx;
1687

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

    
1690
    while (res == 0) {
1691
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1692
            res = AVERROR(EIO);
1693
            break;
1694
        } else if (matroska->level_up) {
1695
            matroska->level_up--;
1696
            break;
1697
        }
1698

    
1699
        switch (id) {
1700
            /* one single index entry ('point') */
1701
            case MATROSKA_ID_POINTENTRY:
1702
                if ((res = ebml_read_master(matroska, &id)) < 0)
1703
                    break;
1704

    
1705
                /* in the end, we hope to fill one entry with a
1706
                 * timestamp, a file position and a tracknum */
1707
                idx.pos   = (uint64_t) -1;
1708
                idx.time  = (uint64_t) -1;
1709
                idx.track = (uint16_t) -1;
1710

    
1711
                while (res == 0) {
1712
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1713
                        res = AVERROR(EIO);
1714
                        break;
1715
                    } else if (matroska->level_up) {
1716
                        matroska->level_up--;
1717
                        break;
1718
                    }
1719

    
1720
                    switch (id) {
1721
                        /* one single index entry ('point') */
1722
                        case MATROSKA_ID_CUETIME: {
1723
                            uint64_t time;
1724
                            if ((res = ebml_read_uint(matroska, &id,
1725
                                                      &time)) < 0)
1726
                                break;
1727
                            idx.time = time * matroska->time_scale;
1728
                            break;
1729
                        }
1730

    
1731
                        /* position in the file + track to which it
1732
                         * belongs */
1733
                        case MATROSKA_ID_CUETRACKPOSITION:
1734
                            if ((res = ebml_read_master(matroska, &id)) < 0)
1735
                                break;
1736

    
1737
                            while (res == 0) {
1738
                                if (!(id = ebml_peek_id (matroska,
1739
                                                    &matroska->level_up))) {
1740
                                    res = AVERROR(EIO);
1741
                                    break;
1742
                                } else if (matroska->level_up) {
1743
                                    matroska->level_up--;
1744
                                    break;
1745
                                }
1746

    
1747
                                switch (id) {
1748
                                    /* track number */
1749
                                    case MATROSKA_ID_CUETRACK: {
1750
                                        uint64_t num;
1751
                                        if ((res = ebml_read_uint(matroska,
1752
                                                          &id, &num)) < 0)
1753
                                            break;
1754
                                        idx.track = num;
1755
                                        break;
1756
                                    }
1757

    
1758
                                        /* position in file */
1759
                                    case MATROSKA_ID_CUECLUSTERPOSITION: {
1760
                                        uint64_t num;
1761
                                        if ((res = ebml_read_uint(matroska,
1762
                                                          &id, &num)) < 0)
1763
                                            break;
1764
                                        idx.pos = num+matroska->segment_start;
1765
                                        break;
1766
                                    }
1767

    
1768
                                    default:
1769
                                        av_log(matroska->ctx, AV_LOG_INFO,
1770
                                               "Unknown entry 0x%x in "
1771
                                               "CuesTrackPositions\n", id);
1772
                                        /* fall-through */
1773

    
1774
                                    case EBML_ID_VOID:
1775
                                        res = ebml_read_skip(matroska);
1776
                                        break;
1777
                                }
1778

    
1779
                                if (matroska->level_up) {
1780
                                    matroska->level_up--;
1781
                                    break;
1782
                                }
1783
                            }
1784

    
1785
                            break;
1786

    
1787
                        default:
1788
                            av_log(matroska->ctx, AV_LOG_INFO,
1789
                                   "Unknown entry 0x%x in cuespoint "
1790
                                   "index\n", id);
1791
                            /* fall-through */
1792

    
1793
                        case EBML_ID_VOID:
1794
                            res = ebml_read_skip(matroska);
1795
                            break;
1796
                    }
1797

    
1798
                    if (matroska->level_up) {
1799
                        matroska->level_up--;
1800
                        break;
1801
                    }
1802
                }
1803

    
1804
                /* so let's see if we got what we wanted */
1805
                if (idx.pos   != (uint64_t) -1 &&
1806
                    idx.time  != (uint64_t) -1 &&
1807
                    idx.track != (uint16_t) -1) {
1808
                    if (matroska->num_indexes % 32 == 0) {
1809
                        /* re-allocate bigger index */
1810
                        matroska->index =
1811
                            av_realloc(matroska->index,
1812
                                       (matroska->num_indexes + 32) *
1813
                                       sizeof(MatroskaDemuxIndex));
1814
                    }
1815
                    matroska->index[matroska->num_indexes] = idx;
1816
                    matroska->num_indexes++;
1817
                }
1818
                break;
1819

    
1820
            default:
1821
                av_log(matroska->ctx, AV_LOG_INFO,
1822
                       "Unknown entry 0x%x in cues header\n", id);
1823
                /* fall-through */
1824

    
1825
            case EBML_ID_VOID:
1826
                res = ebml_read_skip(matroska);
1827
                break;
1828
        }
1829

    
1830
        if (matroska->level_up) {
1831
            matroska->level_up--;
1832
            break;
1833
        }
1834
    }
1835

    
1836
    return res;
1837
}
1838

    
1839
static int
1840
matroska_parse_metadata (MatroskaDemuxContext *matroska)
1841
{
1842
    int res = 0;
1843
    uint32_t id;
1844

    
1845
    while (res == 0) {
1846
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1847
            res = AVERROR(EIO);
1848
            break;
1849
        } else if (matroska->level_up) {
1850
            matroska->level_up--;
1851
            break;
1852
        }
1853

    
1854
        switch (id) {
1855
            /* Hm, this is unsupported... */
1856
            default:
1857
                av_log(matroska->ctx, AV_LOG_INFO,
1858
                       "Unknown entry 0x%x in metadata header\n", id);
1859
                /* fall-through */
1860

    
1861
            case EBML_ID_VOID:
1862
                res = ebml_read_skip(matroska);
1863
                break;
1864
        }
1865

    
1866
        if (matroska->level_up) {
1867
            matroska->level_up--;
1868
            break;
1869
        }
1870
    }
1871

    
1872
    return res;
1873
}
1874

    
1875
static int
1876
matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1877
{
1878
    int res = 0;
1879
    uint32_t id;
1880

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

    
1883
    while (res == 0) {
1884
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1885
            res = AVERROR(EIO);
1886
            break;
1887
        } else if (matroska->level_up) {
1888
            matroska->level_up--;
1889
            break;
1890
        }
1891

    
1892
        switch (id) {
1893
            case MATROSKA_ID_SEEKENTRY: {
1894
                uint32_t seek_id = 0, peek_id_cache = 0;
1895
                uint64_t seek_pos = (uint64_t) -1, t;
1896
                int dummy_level = 0;
1897

    
1898
                if ((res = ebml_read_master(matroska, &id)) < 0)
1899
                    break;
1900

    
1901
                while (res == 0) {
1902
                    if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1903
                        res = AVERROR(EIO);
1904
                        break;
1905
                    } else if (matroska->level_up) {
1906
                        matroska->level_up--;
1907
                        break;
1908
                    }
1909

    
1910
                    switch (id) {
1911
                        case MATROSKA_ID_SEEKID:
1912
                            res = ebml_read_uint(matroska, &id, &t);
1913
                            seek_id = t;
1914
                            break;
1915

    
1916
                        case MATROSKA_ID_SEEKPOSITION:
1917
                            res = ebml_read_uint(matroska, &id, &seek_pos);
1918
                            break;
1919

    
1920
                        default:
1921
                            av_log(matroska->ctx, AV_LOG_INFO,
1922
                                   "Unknown seekhead ID 0x%x\n", id);
1923
                            /* fall-through */
1924

    
1925
                        case EBML_ID_VOID:
1926
                            res = ebml_read_skip(matroska);
1927
                            break;
1928
                    }
1929

    
1930
                    if (matroska->level_up) {
1931
                        matroska->level_up--;
1932
                        break;
1933
                    }
1934
                }
1935

    
1936
                if (!seek_id || seek_pos == (uint64_t) -1) {
1937
                    av_log(matroska->ctx, AV_LOG_INFO,
1938
                           "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1939
                           seek_id, seek_pos);
1940
                    break;
1941
                }
1942

    
1943
                switch (seek_id) {
1944
                    case MATROSKA_ID_CUES:
1945
                    case MATROSKA_ID_TAGS: {
1946
                        uint32_t level_up = matroska->level_up;
1947
                        offset_t before_pos;
1948
                        uint64_t length;
1949
                        MatroskaLevel level;
1950

    
1951
                        /* remember the peeked ID and the current position */
1952
                        peek_id_cache = matroska->peek_id;
1953
                        before_pos = url_ftell(matroska->ctx->pb);
1954

    
1955
                        /* seek */
1956
                        if ((res = ebml_read_seek(matroska, seek_pos +
1957
                                               matroska->segment_start)) < 0)
1958
                            goto finish;
1959

    
1960
                        /* we don't want to lose our seekhead level, so we add
1961
                         * a dummy. This is a crude hack. */
1962
                        if (matroska->num_levels == EBML_MAX_DEPTH) {
1963
                            av_log(matroska->ctx, AV_LOG_INFO,
1964
                                   "Max EBML element depth (%d) reached, "
1965
                                   "cannot parse further.\n", EBML_MAX_DEPTH);
1966
                            return AVERROR_UNKNOWN;
1967
                        }
1968

    
1969
                        level.start = 0;
1970
                        level.length = (uint64_t)-1;
1971
                        matroska->levels[matroska->num_levels] = level;
1972
                        matroska->num_levels++;
1973
                        dummy_level = 1;
1974

    
1975
                        /* check ID */
1976
                        if (!(id = ebml_peek_id (matroska,
1977
                                                 &matroska->level_up)))
1978
                            goto finish;
1979
                        if (id != seek_id) {
1980
                            av_log(matroska->ctx, AV_LOG_INFO,
1981
                                   "We looked for ID=0x%x but got "
1982
                                   "ID=0x%x (pos=%"PRIu64")",
1983
                                   seek_id, id, seek_pos +
1984
                                   matroska->segment_start);
1985
                            goto finish;
1986
                        }
1987

    
1988
                        /* read master + parse */
1989
                        if ((res = ebml_read_master(matroska, &id)) < 0)
1990
                            goto finish;
1991
                        switch (id) {
1992
                            case MATROSKA_ID_CUES:
1993
                                if (!(res = matroska_parse_index(matroska)) ||
1994
                                    url_feof(matroska->ctx->pb)) {
1995
                                    matroska->index_parsed = 1;
1996
                                    res = 0;
1997
                                }
1998
                                break;
1999
                            case MATROSKA_ID_TAGS:
2000
                                if (!(res = matroska_parse_metadata(matroska)) ||
2001
                                   url_feof(matroska->ctx->pb)) {
2002
                                    matroska->metadata_parsed = 1;
2003
                                    res = 0;
2004
                                }
2005
                                break;
2006
                        }
2007

    
2008
                    finish:
2009
                        /* remove dummy level */
2010
                        if (dummy_level)
2011
                            while (matroska->num_levels) {
2012
                                matroska->num_levels--;
2013
                                length =
2014
                                  matroska->levels[matroska->num_levels].length;
2015
                                if (length == (uint64_t)-1)
2016
                                    break;
2017
                            }
2018

    
2019
                        /* seek back */
2020
                        if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2021
                            return res;
2022
                        matroska->peek_id = peek_id_cache;
2023
                        matroska->level_up = level_up;
2024
                        break;
2025
                    }
2026

    
2027
                    default:
2028
                        av_log(matroska->ctx, AV_LOG_INFO,
2029
                               "Ignoring seekhead entry for ID=0x%x\n",
2030
                               seek_id);
2031
                        break;
2032
                }
2033

    
2034
                break;
2035
            }
2036

    
2037
            default:
2038
                av_log(matroska->ctx, AV_LOG_INFO,
2039
                       "Unknown seekhead ID 0x%x\n", id);
2040
                /* fall-through */
2041

    
2042
            case EBML_ID_VOID:
2043
                res = ebml_read_skip(matroska);
2044
                break;
2045
        }
2046

    
2047
        if (matroska->level_up) {
2048
            matroska->level_up--;
2049
            break;
2050
        }
2051
    }
2052

    
2053
    return res;
2054
}
2055

    
2056
static int
2057
matroska_parse_attachments(AVFormatContext *s)
2058
{
2059
    MatroskaDemuxContext *matroska = s->priv_data;
2060
    int res = 0;
2061
    uint32_t id;
2062

    
2063
    av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2064

    
2065
    while (res == 0) {
2066
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2067
            res = AVERROR(EIO);
2068
            break;
2069
        } else if (matroska->level_up) {
2070
            matroska->level_up--;
2071
            break;
2072
        }
2073

    
2074
        switch (id) {
2075
        case MATROSKA_ID_ATTACHEDFILE: {
2076
            char* name = NULL;
2077
            char* mime = NULL;
2078
            uint8_t* data = NULL;
2079
            int i, data_size = 0;
2080
            AVStream *st;
2081

    
2082
            if ((res = ebml_read_master(matroska, &id)) < 0)
2083
                break;
2084

    
2085
            while (res == 0) {
2086
                if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2087
                    res = AVERROR(EIO);
2088
                    break;
2089
                } else if (matroska->level_up) {
2090
                    matroska->level_up--;
2091
                    break;
2092
                }
2093

    
2094
                switch (id) {
2095
                case MATROSKA_ID_FILENAME:
2096
                    res = ebml_read_utf8 (matroska, &id, &name);
2097
                    break;
2098

    
2099
                case MATROSKA_ID_FILEMIMETYPE:
2100
                    res = ebml_read_ascii (matroska, &id, &mime);
2101
                    break;
2102

    
2103
                case MATROSKA_ID_FILEDATA:
2104
                    res = ebml_read_binary(matroska, &id, &data, &data_size);
2105
                    break;
2106

    
2107
                default:
2108
                    av_log(matroska->ctx, AV_LOG_INFO,
2109
                           "Unknown attachedfile ID 0x%x\n", id);
2110
                case MATROSKA_ID_FILEUID:
2111
                case EBML_ID_VOID:
2112
                    res = ebml_read_skip(matroska);
2113
                    break;
2114
                }
2115

    
2116
                if (matroska->level_up) {
2117
                    matroska->level_up--;
2118
                    break;
2119
                }
2120
            }
2121

    
2122
            if (!(name && mime && data && data_size > 0)) {
2123
                av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2124
                break;
2125
            }
2126

    
2127
            st = av_new_stream(s, matroska->num_streams++);
2128
            if (st == NULL)
2129
                return AVERROR(ENOMEM);
2130
            st->filename = av_strdup(name);
2131
            st->codec->codec_id = CODEC_ID_NONE;
2132
            st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2133
            st->codec->extradata = av_malloc(data_size);
2134
            if(st->codec->extradata == NULL)
2135
                return AVERROR(ENOMEM);
2136
            st->codec->extradata_size = data_size;
2137
            memcpy(st->codec->extradata, data, data_size);
2138

    
2139
            for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2140
                if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2141
                             strlen(ff_mkv_mime_tags[i].str))) {
2142
                    st->codec->codec_id = ff_mkv_mime_tags[i].id;
2143
                    break;
2144
                }
2145
            }
2146

    
2147
            av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2148
            break;
2149
        }
2150

    
2151
        default:
2152
            av_log(matroska->ctx, AV_LOG_INFO,
2153
                   "Unknown attachments ID 0x%x\n", id);
2154
            /* fall-through */
2155

    
2156
        case EBML_ID_VOID:
2157
            res = ebml_read_skip(matroska);
2158
            break;
2159
        }
2160

    
2161
        if (matroska->level_up) {
2162
            matroska->level_up--;
2163
            break;
2164
        }
2165
    }
2166

    
2167
    return res;
2168
}
2169

    
2170
static int
2171
matroska_parse_chapters(AVFormatContext *s)
2172
{
2173
    MatroskaDemuxContext *matroska = s->priv_data;
2174
    int res = 0;
2175
    uint32_t id;
2176

    
2177
    av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2178

    
2179
    while (res == 0) {
2180
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2181
            res = AVERROR(EIO);
2182
            break;
2183
        } else if (matroska->level_up) {
2184
            matroska->level_up--;
2185
            break;
2186
        }
2187

    
2188
        switch (id) {
2189
        case MATROSKA_ID_EDITIONENTRY: {
2190
            uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2191
            int64_t uid= -1;
2192
            char* title = NULL;
2193
            /* if there is more than one chapter edition
2194
               we take only the first one */
2195
            if(s->chapters) {
2196
                    ebml_read_skip(matroska);
2197
                    break;
2198
            }
2199

    
2200
            if ((res = ebml_read_master(matroska, &id)) < 0)
2201
                break;
2202

    
2203
            while (res == 0) {
2204
                if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2205
                    res = AVERROR(EIO);
2206
                    break;
2207
                } else if (matroska->level_up) {
2208
                    matroska->level_up--;
2209
                    break;
2210
                }
2211

    
2212
                switch (id) {
2213
                case MATROSKA_ID_CHAPTERATOM:
2214
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2215
                        break;
2216

    
2217
                    while (res == 0) {
2218
                        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2219
                            res = AVERROR(EIO);
2220
                            break;
2221
                        } else if (matroska->level_up) {
2222
                            matroska->level_up--;
2223
                            break;
2224
                        }
2225

    
2226
                        switch (id) {
2227
                        case MATROSKA_ID_CHAPTERTIMEEND:
2228
                            res = ebml_read_uint(matroska, &id, &end);
2229
                            break;
2230

    
2231
                        case MATROSKA_ID_CHAPTERTIMESTART:
2232
                            res = ebml_read_uint(matroska, &id, &start);
2233
                            break;
2234

    
2235
                        case MATROSKA_ID_CHAPTERDISPLAY:
2236
                            if ((res = ebml_read_master(matroska, &id)) < 0)
2237
                                break;
2238

    
2239
                            while (res == 0) {
2240
                                if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2241
                                    res = AVERROR(EIO);
2242
                                    break;
2243
                                } else if (matroska->level_up) {
2244
                                    matroska->level_up--;
2245
                                    break;
2246
                                }
2247

    
2248
                                switch (id) {
2249
                                case MATROSKA_ID_CHAPSTRING:
2250
                                    res = ebml_read_utf8(matroska, &id, &title);
2251
                                    break;
2252

    
2253
                                default:
2254
                                    av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2255
                                case EBML_ID_VOID:
2256
                                    res = ebml_read_skip(matroska);
2257
                                    break;
2258
                                }
2259

    
2260
                                if (matroska->level_up) {
2261
                                    matroska->level_up--;
2262
                                    break;
2263
                                }
2264
                            }
2265
                            break;
2266

    
2267
                        case MATROSKA_ID_CHAPTERUID:
2268
                            res = ebml_read_uint(matroska, &id, &uid);
2269
                            break;
2270
                        default:
2271
                            av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2272
                        case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2273
                        case EBML_ID_VOID:
2274
                            res = ebml_read_skip(matroska);
2275
                            break;
2276
                        }
2277

    
2278
                        if (matroska->level_up) {
2279
                            matroska->level_up--;
2280
                            break;
2281
                        }
2282
                    }
2283

    
2284
                    if (start != AV_NOPTS_VALUE && uid != -1) {
2285
                        if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2286
                            res= AVERROR(ENOMEM);
2287
                    }
2288
                    av_free(title);
2289
                    break;
2290

    
2291
                default:
2292
                    av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2293
                case MATROSKA_ID_EDITIONUID:
2294
                case MATROSKA_ID_EDITIONFLAGHIDDEN:
2295
                case MATROSKA_ID_EDITIONFLAGDEFAULT:
2296
                case EBML_ID_VOID:
2297
                    res = ebml_read_skip(matroska);
2298
                    break;
2299
                }
2300

    
2301

    
2302
                if (matroska->level_up) {
2303
                    matroska->level_up--;
2304
                    break;
2305
                }
2306
            }
2307
        break;
2308
        }
2309

    
2310
        default:
2311
            av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2312
        case EBML_ID_VOID:
2313
            res = ebml_read_skip(matroska);
2314
            break;
2315
        }
2316

    
2317
        if (matroska->level_up) {
2318
            matroska->level_up--;
2319
            break;
2320
        }
2321
    }
2322

    
2323
    return res;
2324
}
2325

    
2326
static int
2327
matroska_aac_profile (char *codec_id)
2328
{
2329
    static const char *aac_profiles[] = {
2330
        "MAIN", "LC", "SSR"
2331
    };
2332
    int profile;
2333

    
2334
    for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2335
        if (strstr(codec_id, aac_profiles[profile]))
2336
            break;
2337
    return profile + 1;
2338
}
2339

    
2340
static int
2341
matroska_aac_sri (int samplerate)
2342
{
2343
    int sri;
2344

    
2345
    for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2346
        if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2347
            break;
2348
    return sri;
2349
}
2350

    
2351
static int
2352
matroska_read_header (AVFormatContext    *s,
2353
                      AVFormatParameters *ap)
2354
{
2355
    MatroskaDemuxContext *matroska = s->priv_data;
2356
    char *doctype;
2357
    int version, last_level, res = 0;
2358
    uint32_t id;
2359

    
2360
    matroska->ctx = s;
2361

    
2362
    /* First read the EBML header. */
2363
    doctype = NULL;
2364
    if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
2365
        return res;
2366
    if ((doctype == NULL) || strcmp(doctype, "matroska")) {
2367
        av_log(matroska->ctx, AV_LOG_ERROR,
2368
               "Wrong EBML doctype ('%s' != 'matroska').\n",
2369
               doctype ? doctype : "(none)");
2370
        if (doctype)
2371
            av_free(doctype);
2372
        return AVERROR_NOFMT;
2373
    }
2374
    av_free(doctype);
2375
    if (version > 2) {
2376
        av_log(matroska->ctx, AV_LOG_ERROR,
2377
               "Matroska demuxer version 2 too old for file version %d\n",
2378
               version);
2379
        return AVERROR_NOFMT;
2380
    }
2381

    
2382
    /* The next thing is a segment. */
2383
    while (1) {
2384
        if (!(id = ebml_peek_id(matroska, &last_level)))
2385
            return AVERROR(EIO);
2386
        if (id == MATROSKA_ID_SEGMENT)
2387
            break;
2388

    
2389
        /* oi! */
2390
        av_log(matroska->ctx, AV_LOG_INFO,
2391
               "Expected a Segment ID (0x%x), but received 0x%x!\n",
2392
               MATROSKA_ID_SEGMENT, id);
2393
        if ((res = ebml_read_skip(matroska)) < 0)
2394
            return res;
2395
    }
2396

    
2397
    /* We now have a Matroska segment.
2398
     * Seeks are from the beginning of the segment,
2399
     * after the segment ID/length. */
2400
    if ((res = ebml_read_master(matroska, &id)) < 0)
2401
        return res;
2402
    matroska->segment_start = url_ftell(s->pb);
2403

    
2404
    matroska->time_scale = 1000000;
2405
    /* we've found our segment, start reading the different contents in here */
2406
    while (res == 0) {
2407
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2408
            res = AVERROR(EIO);
2409
            break;
2410
        } else if (matroska->level_up) {
2411
            matroska->level_up--;
2412
            break;
2413
        }
2414

    
2415
        switch (id) {
2416
            /* stream info */
2417
            case MATROSKA_ID_INFO: {
2418
                if ((res = ebml_read_master(matroska, &id)) < 0)
2419
                    break;
2420
                res = matroska_parse_info(matroska);
2421
                break;
2422
            }
2423

    
2424
            /* track info headers */
2425
            case MATROSKA_ID_TRACKS: {
2426
                if ((res = ebml_read_master(matroska, &id)) < 0)
2427
                    break;
2428
                res = matroska_parse_tracks(matroska);
2429
                break;
2430
            }
2431

    
2432
            /* stream index */
2433
            case MATROSKA_ID_CUES: {
2434
                if (!matroska->index_parsed) {
2435
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2436
                        break;
2437
                    res = matroska_parse_index(matroska);
2438
                } else
2439
                    res = ebml_read_skip(matroska);
2440
                break;
2441
            }
2442

    
2443
            /* metadata */
2444
            case MATROSKA_ID_TAGS: {
2445
                if (!matroska->metadata_parsed) {
2446
                    if ((res = ebml_read_master(matroska, &id)) < 0)
2447
                        break;
2448
                    res = matroska_parse_metadata(matroska);
2449
                } else
2450
                    res = ebml_read_skip(matroska);
2451
                break;
2452
            }
2453

    
2454
            /* file index (if seekable, seek to Cues/Tags to parse it) */
2455
            case MATROSKA_ID_SEEKHEAD: {
2456
                if ((res = ebml_read_master(matroska, &id)) < 0)
2457
                    break;
2458
                res = matroska_parse_seekhead(matroska);
2459
                break;
2460
            }
2461

    
2462
            case MATROSKA_ID_ATTACHMENTS: {
2463
                if ((res = ebml_read_master(matroska, &id)) < 0)
2464
                    break;
2465
                res = matroska_parse_attachments(s);
2466
                break;
2467
            }
2468

    
2469
            case MATROSKA_ID_CLUSTER: {
2470
                /* Do not read the master - this will be done in the next
2471
                 * call to matroska_read_packet. */
2472
                res = 1;
2473
                break;
2474
            }
2475

    
2476
            case MATROSKA_ID_CHAPTERS: {
2477
                if ((res = ebml_read_master(matroska, &id)) < 0)
2478
                    return res;
2479
                res = matroska_parse_chapters(s);
2480
                break;
2481
            }
2482

    
2483
            default:
2484
                av_log(matroska->ctx, AV_LOG_INFO,
2485
                       "Unknown matroska file header ID 0x%x\n", id);
2486
            /* fall-through */
2487

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

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

    
2499
    /* Have we found a cluster? */
2500
    if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2501
        int i, j;
2502
        MatroskaTrack *track;
2503
        AVStream *st;
2504

    
2505
        for (i = 0; i < matroska->num_tracks; i++) {
2506
            enum CodecID codec_id = CODEC_ID_NONE;
2507
            uint8_t *extradata = NULL;
2508
            int extradata_size = 0;
2509
            int extradata_offset = 0;
2510
            track = matroska->tracks[i];
2511
            track->stream_index = -1;
2512

    
2513
            /* Apply some sanity checks. */
2514
            if (track->codec_id == NULL)
2515
                continue;
2516

    
2517
            for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2518
                if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2519
                            strlen(ff_mkv_codec_tags[j].str))){
2520
                    codec_id= ff_mkv_codec_tags[j].id;
2521
                    break;
2522
                }
2523
            }
2524

    
2525
            /* Set the FourCC from the CodecID. */
2526
            /* This is the MS compatibility mode which stores a
2527
             * BITMAPINFOHEADER in the CodecPrivate. */
2528
            if (!strcmp(track->codec_id,
2529
                        MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2530
                (track->codec_priv_size >= 40) &&
2531
                (track->codec_priv != NULL)) {
2532
                MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2533

    
2534
                /* Offset of biCompression. Stored in LE. */
2535
                vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2536
                codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2537

    
2538
            }
2539

    
2540
            /* This is the MS compatibility mode which stores a
2541
             * WAVEFORMATEX in the CodecPrivate. */
2542
            else if (!strcmp(track->codec_id,
2543
                             MATROSKA_CODEC_ID_AUDIO_ACM) &&
2544
                (track->codec_priv_size >= 18) &&
2545
                (track->codec_priv != NULL)) {
2546
                uint16_t tag;
2547

    
2548
                /* Offset of wFormatTag. Stored in LE. */
2549
                tag = AV_RL16(track->codec_priv);
2550
                codec_id = codec_get_id(codec_wav_tags, tag);
2551

    
2552
            }
2553

    
2554
            if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2555
                (track->codec_priv_size >= 86) &&
2556
                (track->codec_priv != NULL)) {
2557
                MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2558

    
2559
                vtrack->fourcc = AV_RL32(track->codec_priv);
2560
                codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
2561
            }
2562

    
2563
            else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2564
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2565
                int profile = matroska_aac_profile(track->codec_id);
2566
                int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2567
                extradata = av_malloc(5);
2568
                if (extradata == NULL)
2569
                    return AVERROR(ENOMEM);
2570
                extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2571
                extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2572
                if (strstr(track->codec_id, "SBR")) {
2573
                    sri = matroska_aac_sri(audiotrack->samplerate);
2574
                    extradata[2] = 0x56;
2575
                    extradata[3] = 0xE5;
2576
                    extradata[4] = 0x80 | (sri<<3);
2577
                    extradata_size = 5;
2578
                } else {
2579
                    extradata_size = 2;
2580
                }
2581
            }
2582

    
2583
            else if (codec_id == CODEC_ID_TTA) {
2584
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2585
                ByteIOContext b;
2586
                extradata_size = 30;
2587
                extradata = av_mallocz(extradata_size);
2588
                if (extradata == NULL)
2589
                    return AVERROR(ENOMEM);
2590
                init_put_byte(&b, extradata, extradata_size, 1,
2591
                              NULL, NULL, NULL, NULL);
2592
                put_buffer(&b, "TTA1", 4);
2593
                put_le16(&b, 1);
2594
                put_le16(&b, audiotrack->channels);
2595
                put_le16(&b, audiotrack->bitdepth);
2596
                put_le32(&b, audiotrack->samplerate);
2597
                put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2598
            }
2599

    
2600
            else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2601
                     codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2602
                extradata_offset = 26;
2603
                track->codec_priv_size -= extradata_offset;
2604
            }
2605

    
2606
            else if (codec_id == CODEC_ID_RA_144) {
2607
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2608
                audiotrack->samplerate = 8000;
2609
                audiotrack->channels = 1;
2610
            }
2611

    
2612
            else if (codec_id == CODEC_ID_RA_288 ||
2613
                     codec_id == CODEC_ID_COOK ||
2614
                     codec_id == CODEC_ID_ATRAC3) {
2615
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2616
                ByteIOContext b;
2617

    
2618
                init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2619
                              NULL, NULL, NULL, NULL);
2620
                url_fskip(&b, 24);
2621
                audiotrack->coded_framesize = get_be32(&b);
2622
                url_fskip(&b, 12);
2623
                audiotrack->sub_packet_h    = get_be16(&b);
2624
                audiotrack->frame_size      = get_be16(&b);
2625
                audiotrack->sub_packet_size = get_be16(&b);
2626
                audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2627
                if (codec_id == CODEC_ID_RA_288) {
2628
                    audiotrack->block_align = audiotrack->coded_framesize;
2629
                    track->codec_priv_size = 0;
2630
                } else {
2631
                    audiotrack->block_align = audiotrack->sub_packet_size;
2632
                    extradata_offset = 78;
2633
                    track->codec_priv_size -= extradata_offset;
2634
                }
2635
            }
2636

    
2637
            if (codec_id == CODEC_ID_NONE) {
2638
                av_log(matroska->ctx, AV_LOG_INFO,
2639
                       "Unknown/unsupported CodecID %s.\n",
2640
                       track->codec_id);
2641
            }
2642

    
2643
            track->stream_index = matroska->num_streams;
2644

    
2645
            matroska->num_streams++;
2646
            st = av_new_stream(s, track->stream_index);
2647
            if (st == NULL)
2648
                return AVERROR(ENOMEM);
2649
            av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2650

    
2651
            st->codec->codec_id = codec_id;
2652
            st->start_time = 0;
2653
            if (strcmp(track->language, "und"))
2654
                strcpy(st->language, track->language);
2655

    
2656
            if (track->flags & MATROSKA_TRACK_DEFAULT)
2657
                st->disposition |= AV_DISPOSITION_DEFAULT;
2658

    
2659
            if (track->default_duration)
2660
                av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2661
                          track->default_duration, 1000000000, 30000);
2662

    
2663
            if(extradata){
2664
                st->codec->extradata = extradata;
2665
                st->codec->extradata_size = extradata_size;
2666
            } else if(track->codec_priv && track->codec_priv_size > 0){
2667
                st->codec->extradata = av_malloc(track->codec_priv_size);
2668
                if(st->codec->extradata == NULL)
2669
                    return AVERROR(ENOMEM);
2670
                st->codec->extradata_size = track->codec_priv_size;
2671
                memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2672
                       track->codec_priv_size);
2673
            }
2674

    
2675
            if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2676
                MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2677

    
2678
                st->codec->codec_type = CODEC_TYPE_VIDEO;
2679
                st->codec->codec_tag = videotrack->fourcc;
2680
                st->codec->width = videotrack->pixel_width;
2681
                st->codec->height = videotrack->pixel_height;
2682
                if (videotrack->display_width == 0)
2683
                    videotrack->display_width= videotrack->pixel_width;
2684
                if (videotrack->display_height == 0)
2685
                    videotrack->display_height= videotrack->pixel_height;
2686
                av_reduce(&st->codec->sample_aspect_ratio.num,
2687
                          &st->codec->sample_aspect_ratio.den,
2688
                          st->codec->height * videotrack->display_width,
2689
                          st->codec-> width * videotrack->display_height,
2690
                          255);
2691
                st->need_parsing = AVSTREAM_PARSE_HEADERS;
2692
            } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2693
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2694

    
2695
                st->codec->codec_type = CODEC_TYPE_AUDIO;
2696
                st->codec->sample_rate = audiotrack->samplerate;
2697
                st->codec->channels = audiotrack->channels;
2698
                st->codec->block_align = audiotrack->block_align;
2699
            } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2700
                st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2701
            }
2702

    
2703
            /* What do we do with private data? E.g. for Vorbis. */
2704
        }
2705
        res = 0;
2706
    }
2707

    
2708
    if (matroska->index_parsed) {
2709
        int i, track, stream;
2710
        for (i=0; i<matroska->num_indexes; i++) {
2711
            MatroskaDemuxIndex *idx = &matroska->index[i];
2712
            track = matroska_find_track_by_num(matroska, idx->track);
2713
            if (track < 0)  continue;
2714
            stream = matroska->tracks[track]->stream_index;
2715
            if (stream >= 0 && stream < matroska->ctx->nb_streams)
2716
                av_add_index_entry(matroska->ctx->streams[stream],
2717
                                   idx->pos, idx->time/AV_TIME_BASE,
2718
                                   0, 0, AVINDEX_KEYFRAME);
2719
        }
2720
    }
2721

    
2722
    return res;
2723
}
2724

    
2725
static int
2726
matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2727
                     int64_t pos, uint64_t cluster_time, uint64_t duration,
2728
                     int is_keyframe, int is_bframe)
2729
{
2730
    int res = 0;
2731
    int track;
2732
    AVStream *st;
2733
    AVPacket *pkt;
2734
    uint8_t *origdata = data;
2735
    int16_t block_time;
2736
    uint32_t *lace_size = NULL;
2737
    int n, flags, laces = 0;
2738
    uint64_t num;
2739
    int stream_index;
2740

    
2741
    /* first byte(s): tracknum */
2742
    if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2743
        av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2744
        av_free(origdata);
2745
        return res;
2746
    }
2747
    data += n;
2748
    size -= n;
2749

    
2750
    /* fetch track from num */
2751
    track = matroska_find_track_by_num(matroska, num);
2752
    if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2753
        av_log(matroska->ctx, AV_LOG_INFO,
2754
               "Invalid stream %d or size %u\n", track, size);
2755
        av_free(origdata);
2756
        return res;
2757
    }
2758
    stream_index = matroska->tracks[track]->stream_index;
2759
    if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2760
        av_free(origdata);
2761
        return res;
2762
    }
2763
    st = matroska->ctx->streams[stream_index];
2764
    if (st->discard >= AVDISCARD_ALL) {
2765
        av_free(origdata);
2766
        return res;
2767
    }
2768
    if (duration == AV_NOPTS_VALUE)
2769
        duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2770

    
2771
    /* block_time (relative to cluster time) */
2772
    block_time = AV_RB16(data);
2773
    data += 2;
2774
    flags = *data++;
2775
    size -= 3;
2776
    if (is_keyframe == -1)
2777
        is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2778

    
2779
    if (matroska->skip_to_keyframe) {
2780
        if (!is_keyframe || st != matroska->skip_to_stream) {
2781
            av_free(origdata);
2782
            return res;
2783
        }
2784
        matroska->skip_to_keyframe = 0;
2785
    }
2786

    
2787
    switch ((flags & 0x06) >> 1) {
2788
        case 0x0: /* no lacing */
2789
            laces = 1;
2790
            lace_size = av_mallocz(sizeof(int));
2791
            lace_size[0] = size;
2792
            break;
2793

    
2794
        case 0x1: /* xiph lacing */
2795
        case 0x2: /* fixed-size lacing */
2796
        case 0x3: /* EBML lacing */
2797
            assert(size>0); // size <=3 is checked before size-=3 above
2798
            laces = (*data) + 1;
2799
            data += 1;
2800
            size -= 1;
2801
            lace_size = av_mallocz(laces * sizeof(int));
2802

    
2803
            switch ((flags & 0x06) >> 1) {
2804
                case 0x1: /* xiph lacing */ {
2805
                    uint8_t temp;
2806
                    uint32_t total = 0;
2807
                    for (n = 0; res == 0 && n < laces - 1; n++) {
2808
                        while (1) {
2809
                            if (size == 0) {
2810
                                res = -1;
2811
                                break;
2812
                            }
2813
                            temp = *data;
2814
                            lace_size[n] += temp;
2815
                            data += 1;
2816
                            size -= 1;
2817
                            if (temp != 0xff)
2818
                                break;
2819
                        }
2820
                        total += lace_size[n];
2821
                    }
2822
                    lace_size[n] = size - total;
2823
                    break;
2824
                }
2825

    
2826
                case 0x2: /* fixed-size lacing */
2827
                    for (n = 0; n < laces; n++)
2828
                        lace_size[n] = size / laces;
2829
                    break;
2830

    
2831
                case 0x3: /* EBML lacing */ {
2832
                    uint32_t total;
2833
                    n = matroska_ebmlnum_uint(data, size, &num);
2834
                    if (n < 0) {
2835
                        av_log(matroska->ctx, AV_LOG_INFO,
2836
                               "EBML block data error\n");
2837
                        break;
2838
                    }
2839
                    data += n;
2840
                    size -= n;
2841
                    total = lace_size[0] = num;
2842
                    for (n = 1; res == 0 && n < laces - 1; n++) {
2843
                        int64_t snum;
2844
                        int r;
2845
                        r = matroska_ebmlnum_sint (data, size, &snum);
2846
                        if (r < 0) {
2847
                            av_log(matroska->ctx, AV_LOG_INFO,
2848
                                   "EBML block data error\n");
2849
                            break;
2850
                        }
2851
                        data += r;
2852
                        size -= r;
2853
                        lace_size[n] = lace_size[n - 1] + snum;
2854
                        total += lace_size[n];
2855
                    }
2856
                    lace_size[n] = size - total;
2857
                    break;
2858
                }
2859
            }
2860
            break;
2861
    }
2862

    
2863
    if (res == 0) {
2864
        uint64_t timecode = AV_NOPTS_VALUE;
2865

    
2866
        if (cluster_time != (uint64_t)-1
2867
            && (block_time >= 0 || cluster_time >= -block_time))
2868
            timecode = cluster_time + block_time;
2869

    
2870
        for (n = 0; n < laces; n++) {
2871
            if (st->codec->codec_id == CODEC_ID_RA_288 ||
2872
                st->codec->codec_id == CODEC_ID_COOK ||
2873
                st->codec->codec_id == CODEC_ID_ATRAC3) {
2874
                MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2875
                int a = st->codec->block_align;
2876
                int sps = audiotrack->sub_packet_size;
2877
                int cfs = audiotrack->coded_framesize;
2878
                int h = audiotrack->sub_packet_h;
2879
                int y = audiotrack->sub_packet_cnt;
2880
                int w = audiotrack->frame_size;
2881
                int x;
2882

    
2883
                if (!audiotrack->pkt_cnt) {
2884
                    if (st->codec->codec_id == CODEC_ID_RA_288)
2885
                        for (x=0; x<h/2; x++)
2886
                            memcpy(audiotrack->buf+x*2*w+y*cfs,
2887
                                   data+x*cfs, cfs);
2888
                    else
2889
                        for (x=0; x<w/sps; x++)
2890
                            memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2891

    
2892
                    if (++audiotrack->sub_packet_cnt >= h) {
2893
                        audiotrack->sub_packet_cnt = 0;
2894
                        audiotrack->pkt_cnt = h*w / a;
2895
                    }
2896
                }
2897
                while (audiotrack->pkt_cnt) {
2898
                    pkt = av_mallocz(sizeof(AVPacket));
2899
                    av_new_packet(pkt, a);
2900
                    memcpy(pkt->data, audiotrack->buf
2901
                           + a * (h*w / a - audiotrack->pkt_cnt--), a);
2902
                    pkt->pos = pos;
2903
                    pkt->stream_index = stream_index;
2904
                    matroska_queue_packet(matroska, pkt);
2905
                }
2906
            } else {
2907
                int offset = 0, pkt_size = lace_size[n];
2908
                uint8_t *pkt_data = data;
2909

    
2910
                if (matroska->tracks[track]->encoding_scope & 1) {
2911
                    offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2912
                                                    matroska->tracks[track]);
2913
                    if (offset < 0)
2914
                        continue;
2915
                }
2916

    
2917
                pkt = av_mallocz(sizeof(AVPacket));
2918
                /* XXX: prevent data copy... */
2919
                if (av_new_packet(pkt, pkt_size+offset) < 0) {
2920
                    av_free(pkt);
2921
                    res = AVERROR(ENOMEM);
2922
                    n = laces-1;
2923
                    break;
2924
                }
2925
                if (offset)
2926
                    memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2927
                memcpy (pkt->data+offset, pkt_data, pkt_size);
2928

    
2929
                if (pkt_data != data)
2930
                    av_free(pkt_data);
2931

    
2932
                if (n == 0)
2933
                    pkt->flags = is_keyframe;
2934
                pkt->stream_index = stream_index;
2935

    
2936
                pkt->pts = timecode;
2937
                pkt->pos = pos;
2938
                pkt->duration = duration;
2939

    
2940
                matroska_queue_packet(matroska, pkt);
2941
            }
2942

    
2943
            if (timecode != AV_NOPTS_VALUE)
2944
                timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2945
            data += lace_size[n];
2946
        }
2947
    }
2948

    
2949
    av_free(lace_size);
2950
    av_free(origdata);
2951
    return res;
2952
}
2953

    
2954
static int
2955
matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2956
                           uint64_t              cluster_time)
2957
{
2958
    int res = 0;
2959
    uint32_t id;
2960
    int is_bframe = 0;
2961
    int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
2962
    uint64_t duration = AV_NOPTS_VALUE;
2963
    uint8_t *data;
2964
    int size = 0;
2965
    int64_t pos = 0;
2966

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

    
2969
    while (res == 0) {
2970
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2971
            res = AVERROR(EIO);
2972
            break;
2973
        } else if (matroska->level_up) {
2974
            matroska->level_up--;
2975
            break;
2976
        }
2977

    
2978
        switch (id) {
2979
            /* one block inside the group. Note, block parsing is one
2980
             * of the harder things, so this code is a bit complicated.
2981
             * See http://www.matroska.org/ for documentation. */
2982
            case MATROSKA_ID_BLOCK: {
2983
                pos = url_ftell(matroska->ctx->pb);
2984
                res = ebml_read_binary(matroska, &id, &data, &size);
2985
                break;
2986
            }
2987

    
2988
            case MATROSKA_ID_BLOCKDURATION: {
2989
                if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
2990
                    break;
2991
                break;
2992
            }
2993

    
2994
            case MATROSKA_ID_BLOCKREFERENCE: {
2995
                int64_t num;
2996
                /* We've found a reference, so not even the first frame in
2997
                 * the lace is a key frame. */
2998
                is_keyframe = 0;
2999
                if (last_num_packets != matroska->num_packets)
3000
                    matroska->packets[last_num_packets]->flags = 0;
3001
                if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
3002
                    break;
3003
                if (num > 0)
3004
                    is_bframe = 1;
3005
                break;
3006
            }
3007

    
3008
            default:
3009
                av_log(matroska->ctx, AV_LOG_INFO,
3010
                       "Unknown entry 0x%x in blockgroup data\n", id);
3011
                /* fall-through */
3012

    
3013
            case EBML_ID_VOID:
3014
                res = ebml_read_skip(matroska);
3015
                break;
3016
        }
3017

    
3018
        if (matroska->level_up) {
3019
            matroska->level_up--;
3020
            break;
3021
        }
3022
    }
3023

    
3024
    if (res)
3025
        return res;
3026

    
3027
    if (size > 0)
3028
        res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3029
                                   duration, is_keyframe, is_bframe);
3030

    
3031
    return res;
3032
}
3033

    
3034
static int
3035
matroska_parse_cluster (MatroskaDemuxContext *matroska)
3036
{
3037
    int res = 0;
3038
    uint32_t id;
3039
    uint64_t cluster_time = 0;
3040
    uint8_t *data;
3041
    int64_t pos;
3042
    int size;
3043

    
3044
    av_log(matroska->ctx, AV_LOG_DEBUG,
3045
           "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3046

    
3047
    while (res == 0) {
3048
        if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3049
            res = AVERROR(EIO);
3050
            break;
3051
        } else if (matroska->level_up) {
3052
            matroska->level_up--;
3053
            break;
3054
        }
3055

    
3056
        switch (id) {
3057
            /* cluster timecode */
3058
            case MATROSKA_ID_CLUSTERTIMECODE: {
3059
                uint64_t num;
3060
                if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3061
                    break;
3062
                cluster_time = num;
3063
                break;
3064
            }
3065

    
3066
                /* a group of blocks inside a cluster */
3067
            case MATROSKA_ID_BLOCKGROUP:
3068
                if ((res = ebml_read_master(matroska, &id)) < 0)
3069
                    break;
3070
                res = matroska_parse_blockgroup(matroska, cluster_time);
3071
                break;
3072

    
3073
            case MATROSKA_ID_SIMPLEBLOCK:
3074
                pos = url_ftell(matroska->ctx->pb);
3075
                res = ebml_read_binary(matroska, &id, &data, &size);
3076
                if (res == 0)
3077
                    res = matroska_parse_block(matroska, data, size, pos,
3078
                                               cluster_time, AV_NOPTS_VALUE,
3079
                                               -1, 0);
3080
                break;
3081

    
3082
            default:
3083
                av_log(matroska->ctx, AV_LOG_INFO,
3084
                       "Unknown entry 0x%x in cluster data\n", id);
3085
                /* fall-through */
3086

    
3087
            case EBML_ID_VOID:
3088
                res = ebml_read_skip(matroska);
3089
                break;
3090
        }
3091

    
3092
        if (matroska->level_up) {
3093
            matroska->level_up--;
3094
            break;
3095
        }
3096
    }
3097

    
3098
    return res;
3099
}
3100

    
3101
static int
3102
matroska_read_packet (AVFormatContext *s,
3103
                      AVPacket        *pkt)
3104
{
3105
    MatroskaDemuxContext *matroska = s->priv_data;
3106
    int res;
3107
    uint32_t id;
3108

    
3109
    /* Read stream until we have a packet queued. */
3110
    while (matroska_deliver_packet(matroska, pkt)) {
3111

    
3112
        /* Have we already reached the end? */
3113
        if (matroska->done)
3114
            return AVERROR(EIO);
3115

    
3116
        res = 0;
3117
        while (res == 0) {
3118
            if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3119
                return AVERROR(EIO);
3120
            } else if (matroska->level_up) {
3121
                matroska->level_up--;
3122
                break;
3123
            }
3124

    
3125
            switch (id) {
3126
                case MATROSKA_ID_CLUSTER:
3127
                    if ((res = ebml_read_master(matroska, &id)) < 0)
3128
                        break;
3129
                    if ((res = matroska_parse_cluster(matroska)) == 0)
3130
                        res = 1; /* Parsed one cluster, let's get out. */
3131
                    break;
3132

    
3133
                default:
3134
                case EBML_ID_VOID:
3135
                    res = ebml_read_skip(matroska);
3136
                    break;
3137
            }
3138

    
3139
            if (matroska->level_up) {
3140
                matroska->level_up--;
3141
                break;
3142
            }
3143
        }
3144

    
3145
        if (res == -1)
3146
            matroska->done = 1;
3147
    }
3148

    
3149
    return 0;
3150
}
3151

    
3152
static int
3153
matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3154
                    int flags)
3155
{
3156
    MatroskaDemuxContext *matroska = s->priv_data;
3157
    AVStream *st = s->streams[stream_index];
3158
    int index;
3159

    
3160
    /* find index entry */
3161
    index = av_index_search_timestamp(st, timestamp, flags);
3162
    if (index < 0)
3163
        return 0;
3164

    
3165
    matroska_clear_queue(matroska);
3166

    
3167
    /* do the seek */
3168
    url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3169
    matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3170
    matroska->skip_to_stream = st;
3171
    matroska->peek_id = 0;
3172
    av_update_cur_dts(s, st, st->index_entries[index].timestamp);
3173
    return 0;
3174
}
3175

    
3176
static int
3177
matroska_read_close (AVFormatContext *s)
3178
{
3179
    MatroskaDemuxContext *matroska = s->priv_data;
3180
    int n = 0;
3181

    
3182
    av_free(matroska->index);
3183

    
3184
    matroska_clear_queue(matroska);
3185

    
3186
    for (n = 0; n < matroska->num_tracks; n++) {
3187
        MatroskaTrack *track = matroska->tracks[n];
3188
        av_free(track->codec_id);
3189
        av_free(track->codec_priv);
3190
        av_free(track->name);
3191

    
3192
        if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3193
            MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3194
            av_free(audiotrack->buf);
3195
        }
3196

    
3197
        av_free(track);
3198
    }
3199

    
3200
    return 0;
3201
}
3202

    
3203
AVInputFormat matroska_demuxer = {
3204
    "matroska",
3205
    NULL_IF_CONFIG_SMALL("Matroska file format"),
3206
    sizeof(MatroskaDemuxContext),
3207
    matroska_probe,
3208
    matroska_read_header,
3209
    matroska_read_packet,
3210
    matroska_read_close,
3211
    matroska_read_seek,
3212
};