]> git.sesse.net Git - ffmpeg/blob - libavformat/matroskadec.c
matroskadec: use generic ebml parser to parse ebml header
[ffmpeg] / libavformat / matroskadec.c
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/avstring.h"
39 #include "libavutil/lzo.h"
40 #ifdef CONFIG_ZLIB
41 #include <zlib.h>
42 #endif
43 #ifdef CONFIG_BZLIB
44 #include <bzlib.h>
45 #endif
46
47 typedef enum {
48     EBML_NONE,
49     EBML_UINT,
50     EBML_FLOAT,
51     EBML_STR,
52     EBML_UTF8,
53     EBML_BIN,
54     EBML_NEST,
55     EBML_PASS,
56     EBML_STOP,
57 } EbmlType;
58
59 typedef const struct EbmlSyntax {
60     uint32_t id;
61     EbmlType type;
62     int list_elem_size;
63     int data_offset;
64     union {
65         uint64_t    u;
66         double      f;
67         const char *s;
68         const struct EbmlSyntax *n;
69     } def;
70 } EbmlSyntax;
71
72 typedef struct {
73     int nb_elem;
74     void *elem;
75 } EbmlList;
76
77 typedef struct {
78     int      size;
79     uint8_t *data;
80     int64_t  pos;
81 } EbmlBin;
82
83 typedef struct {
84     uint64_t version;
85     uint64_t max_size;
86     uint64_t id_length;
87     char    *doctype;
88     uint64_t doctype_version;
89 } Ebml;
90
91 typedef struct Track {
92     MatroskaTrackType type;
93
94     /* Unique track number and track ID. stream_index is the index that
95      * the calling app uses for this track. */
96     uint32_t num;
97     uint32_t uid;
98     int stream_index;
99
100     char *name;
101     char language[4];
102
103     char *codec_id;
104
105     unsigned char *codec_priv;
106     int codec_priv_size;
107
108     double time_scale;
109     uint64_t default_duration;
110     uint64_t flag_default;
111
112     int encoding_scope;
113     MatroskaTrackEncodingCompAlgo encoding_algo;
114     uint8_t *encoding_settings;
115     int encoding_settings_len;
116 } MatroskaTrack;
117
118 typedef struct MatroskaVideoTrack {
119     MatroskaTrack track;
120
121     int pixel_width;
122     int pixel_height;
123     int display_width;
124     int display_height;
125
126     uint32_t fourcc;
127
128     //..
129 } MatroskaVideoTrack;
130
131 typedef struct MatroskaAudioTrack {
132     MatroskaTrack track;
133
134     int channels;
135     int bitdepth;
136     int internal_samplerate;
137     int samplerate;
138     int block_align;
139
140     /* real audio header */
141     int coded_framesize;
142     int sub_packet_h;
143     int frame_size;
144     int sub_packet_size;
145     int sub_packet_cnt;
146     int pkt_cnt;
147     uint8_t *buf;
148     //..
149 } MatroskaAudioTrack;
150
151 typedef struct MatroskaSubtitleTrack {
152     MatroskaTrack track;
153     //..
154 } MatroskaSubtitleTrack;
155
156 #define MAX_TRACK_SIZE (FFMAX3(sizeof(MatroskaVideoTrack), \
157                                     sizeof(MatroskaAudioTrack), \
158                                     sizeof(MatroskaSubtitleTrack)))
159
160 typedef struct MatroskaLevel {
161     uint64_t start;
162     uint64_t length;
163 } MatroskaLevel;
164
165 typedef struct MatroskaDemuxIndex {
166   uint64_t        pos;   /* of the corresponding *cluster*! */
167   uint16_t        track; /* reference to 'num' */
168   uint64_t        time;  /* in nanoseconds */
169 } MatroskaDemuxIndex;
170
171 typedef struct MatroskaDemuxContext {
172     AVFormatContext *ctx;
173
174     /* ebml stuff */
175     int num_levels;
176     MatroskaLevel levels[EBML_MAX_DEPTH];
177     int level_up;
178
179     /* timescale in the file */
180     int64_t time_scale;
181
182     /* num_streams is the number of streams that av_new_stream() was called
183      * for ( = that are available to the calling program). */
184     int num_tracks;
185     int num_streams;
186     MatroskaTrack *tracks[MAX_STREAMS];
187
188     /* cache for ID peeking */
189     uint32_t peek_id;
190
191     /* byte position of the segment inside the stream */
192     offset_t segment_start;
193
194     /* The packet queue. */
195     AVPacket **packets;
196     int num_packets;
197
198     /* have we already parse metadata/cues/clusters? */
199     int metadata_parsed;
200     int index_parsed;
201     int done;
202
203     /* The index for seeking. */
204     int num_indexes;
205     MatroskaDemuxIndex *index;
206
207     /* What to skip before effectively reading a packet. */
208     int skip_to_keyframe;
209     AVStream *skip_to_stream;
210 } MatroskaDemuxContext;
211
212 #define ARRAY_SIZE(x)  (sizeof(x)/sizeof(*x))
213
214 static EbmlSyntax ebml_header[] = {
215     { EBML_ID_EBMLREADVERSION,        EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} },
216     { EBML_ID_EBMLMAXSIZELENGTH,      EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} },
217     { EBML_ID_EBMLMAXIDLENGTH,        EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} },
218     { EBML_ID_DOCTYPE,                EBML_STR,  0, offsetof(Ebml,doctype), {.s="(none)"} },
219     { EBML_ID_DOCTYPEREADVERSION,     EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} },
220     { EBML_ID_EBMLVERSION,            EBML_NONE },
221     { EBML_ID_DOCTYPEVERSION,         EBML_NONE },
222     { EBML_ID_VOID,                   EBML_NONE },
223     { 0 }
224 };
225
226 static EbmlSyntax ebml_syntax[] = {
227     { EBML_ID_HEADER,                 EBML_NEST, 0, 0, {.n=ebml_header} },
228     { 0 }
229 };
230
231 /*
232  * The first few functions handle EBML file parsing. The rest
233  * is the document interpretation. Matroska really just is a
234  * EBML file.
235  */
236
237 /*
238  * Return: the amount of levels in the hierarchy that the
239  * current element lies higher than the previous one.
240  * The opposite isn't done - that's auto-done using master
241  * element reading.
242  */
243
244 static int
245 ebml_read_element_level_up (MatroskaDemuxContext *matroska)
246 {
247     ByteIOContext *pb = matroska->ctx->pb;
248     offset_t pos = url_ftell(pb);
249     int num = 0;
250
251     while (matroska->num_levels > 0) {
252         MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1];
253
254         if (pos >= level->start + level->length) {
255             matroska->num_levels--;
256             num++;
257         } else {
258             break;
259         }
260     }
261
262     return num;
263 }
264
265 /*
266  * Read: an "EBML number", which is defined as a variable-length
267  * array of bytes. The first byte indicates the length by giving a
268  * number of 0-bits followed by a one. The position of the first
269  * "one" bit inside the first byte indicates the length of this
270  * number.
271  * Returns: num. of bytes read. < 0 on error.
272  */
273
274 static int
275 ebml_read_num (MatroskaDemuxContext *matroska,
276                int                   max_size,
277                uint64_t             *number)
278 {
279     ByteIOContext *pb = matroska->ctx->pb;
280     int len_mask = 0x80, read = 1, n = 1;
281     int64_t total = 0;
282
283     /* the first byte tells us the length in bytes - get_byte() can normally
284      * return 0, but since that's not a valid first ebmlID byte, we can
285      * use it safely here to catch EOS. */
286     if (!(total = get_byte(pb))) {
287         /* we might encounter EOS here */
288         if (!url_feof(pb)) {
289             offset_t pos = url_ftell(pb);
290             av_log(matroska->ctx, AV_LOG_ERROR,
291                    "Read error at pos. %"PRIu64" (0x%"PRIx64")\n",
292                    pos, pos);
293         }
294         return AVERROR(EIO); /* EOS or actual I/O error */
295     }
296
297     /* get the length of the EBML number */
298     while (read <= max_size && !(total & len_mask)) {
299         read++;
300         len_mask >>= 1;
301     }
302     if (read > max_size) {
303         offset_t pos = url_ftell(pb) - 1;
304         av_log(matroska->ctx, AV_LOG_ERROR,
305                "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n",
306                (uint8_t) total, pos, pos);
307         return AVERROR_INVALIDDATA;
308     }
309
310     /* read out length */
311     total &= ~len_mask;
312     while (n++ < read)
313         total = (total << 8) | get_byte(pb);
314
315     *number = total;
316
317     return read;
318 }
319
320 /*
321  * Read: the element content data ID.
322  * Return: the number of bytes read or < 0 on error.
323  */
324
325 static int
326 ebml_read_element_id (MatroskaDemuxContext *matroska,
327                       uint32_t             *id,
328                       int                  *level_up)
329 {
330     int read;
331     uint64_t total;
332
333     /* if we re-call this, use our cached ID */
334     if (matroska->peek_id != 0) {
335         if (level_up)
336             *level_up = 0;
337         *id = matroska->peek_id;
338         return 0;
339     }
340
341     /* read out the "EBML number", include tag in ID */
342     if ((read = ebml_read_num(matroska, 4, &total)) < 0)
343         return read;
344     *id = matroska->peek_id  = total | (1 << (read * 7));
345
346     /* level tracking */
347     if (level_up)
348         *level_up = ebml_read_element_level_up(matroska);
349
350     return read;
351 }
352
353 /*
354  * Read: element content length.
355  * Return: the number of bytes read or < 0 on error.
356  */
357
358 static int
359 ebml_read_element_length (MatroskaDemuxContext *matroska,
360                           uint64_t             *length)
361 {
362     /* clear cache since we're now beyond that data point */
363     matroska->peek_id = 0;
364
365     /* read out the "EBML number", include tag in ID */
366     return ebml_read_num(matroska, 8, length);
367 }
368
369 /*
370  * Return: the ID of the next element, or 0 on error.
371  * Level_up contains the amount of levels that this
372  * next element lies higher than the previous one.
373  */
374
375 static uint32_t
376 ebml_peek_id (MatroskaDemuxContext *matroska,
377               int                  *level_up)
378 {
379     uint32_t id;
380
381     if (ebml_read_element_id(matroska, &id, level_up) < 0)
382         return 0;
383
384     return id;
385 }
386
387 /*
388  * Seek to a given offset.
389  * 0 is success, -1 is failure.
390  */
391
392 static int
393 ebml_read_seek (MatroskaDemuxContext *matroska,
394                 offset_t              offset)
395 {
396     ByteIOContext *pb = matroska->ctx->pb;
397
398     /* clear ID cache, if any */
399     matroska->peek_id = 0;
400
401     return (url_fseek(pb, offset, SEEK_SET) == offset) ? 0 : -1;
402 }
403
404 /*
405  * Skip the next element.
406  * 0 is success, -1 is failure.
407  */
408
409 static int
410 ebml_read_skip (MatroskaDemuxContext *matroska)
411 {
412     ByteIOContext *pb = matroska->ctx->pb;
413     uint32_t id;
414     uint64_t length;
415     int res;
416
417     if ((res = ebml_read_element_id(matroska, &id, NULL)) < 0 ||
418         (res = ebml_read_element_length(matroska, &length)) < 0)
419         return res;
420
421     url_fskip(pb, length);
422
423     return 0;
424 }
425
426 /*
427  * Read the next element as an unsigned int.
428  * 0 is success, < 0 is failure.
429  */
430
431 static int
432 ebml_read_uint (MatroskaDemuxContext *matroska,
433                 uint32_t             *id,
434                 uint64_t             *num)
435 {
436     ByteIOContext *pb = matroska->ctx->pb;
437     int n = 0, size, res;
438     uint64_t rlength;
439
440     if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
441         (res = ebml_read_element_length(matroska, &rlength)) < 0)
442         return res;
443     size = rlength;
444     if (size < 1 || size > 8) {
445         offset_t pos = url_ftell(pb);
446         av_log(matroska->ctx, AV_LOG_ERROR,
447                "Invalid uint element size %d at position %"PRId64" (0x%"PRIx64")\n",
448                 size, pos, pos);
449         return AVERROR_INVALIDDATA;
450     }
451
452     /* big-endian ordening; build up number */
453     *num = 0;
454     while (n++ < size)
455         *num = (*num << 8) | get_byte(pb);
456
457     return 0;
458 }
459
460 /*
461  * Read the next element as a signed int.
462  * 0 is success, < 0 is failure.
463  */
464
465 static int
466 ebml_read_sint (MatroskaDemuxContext *matroska,
467                 uint32_t             *id,
468                 int64_t              *num)
469 {
470     ByteIOContext *pb = matroska->ctx->pb;
471     int size, n = 1, negative = 0, res;
472     uint64_t rlength;
473
474     if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
475         (res = ebml_read_element_length(matroska, &rlength)) < 0)
476         return res;
477     size = rlength;
478     if (size < 1 || size > 8) {
479         offset_t pos = url_ftell(pb);
480         av_log(matroska->ctx, AV_LOG_ERROR,
481                "Invalid sint element size %d at position %"PRId64" (0x%"PRIx64")\n",
482                 size, pos, pos);
483         return AVERROR_INVALIDDATA;
484     }
485     if ((*num = get_byte(pb)) & 0x80) {
486         negative = 1;
487         *num &= ~0x80;
488     }
489     while (n++ < size)
490         *num = (*num << 8) | get_byte(pb);
491
492     /* make signed */
493     if (negative)
494         *num = *num - (1LL << ((8 * size) - 1));
495
496     return 0;
497 }
498
499 /*
500  * Read the next element as a float.
501  * 0 is success, < 0 is failure.
502  */
503
504 static int
505 ebml_read_float (MatroskaDemuxContext *matroska,
506                  uint32_t             *id,
507                  double               *num)
508 {
509     ByteIOContext *pb = matroska->ctx->pb;
510     int size, res;
511     uint64_t rlength;
512
513     if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
514         (res = ebml_read_element_length(matroska, &rlength)) < 0)
515         return res;
516     size = rlength;
517
518     if (size == 4) {
519         *num= av_int2flt(get_be32(pb));
520     } else if(size==8){
521         *num= av_int2dbl(get_be64(pb));
522     } else{
523         offset_t pos = url_ftell(pb);
524         av_log(matroska->ctx, AV_LOG_ERROR,
525                "Invalid float element size %d at position %"PRIu64" (0x%"PRIx64")\n",
526                size, pos, pos);
527         return AVERROR_INVALIDDATA;
528     }
529
530     return 0;
531 }
532
533 /*
534  * Read the next element as an ASCII string.
535  * 0 is success, < 0 is failure.
536  */
537
538 static int
539 ebml_read_ascii (MatroskaDemuxContext *matroska,
540                  uint32_t             *id,
541                  char                **str)
542 {
543     ByteIOContext *pb = matroska->ctx->pb;
544     int size, res;
545     uint64_t rlength;
546
547     if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
548         (res = ebml_read_element_length(matroska, &rlength)) < 0)
549         return res;
550     size = rlength;
551
552     /* ebml strings are usually not 0-terminated, so we allocate one
553      * byte more, read the string and NULL-terminate it ourselves. */
554     if (size < 0 || !(*str = av_malloc(size + 1))) {
555         av_log(matroska->ctx, AV_LOG_ERROR, "Memory allocation failed\n");
556         return AVERROR(ENOMEM);
557     }
558     if (get_buffer(pb, (uint8_t *) *str, size) != size) {
559         offset_t pos = url_ftell(pb);
560         av_log(matroska->ctx, AV_LOG_ERROR,
561                "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
562         av_free(*str);
563         return AVERROR(EIO);
564     }
565     (*str)[size] = '\0';
566
567     return 0;
568 }
569
570 /*
571  * Read the next element as a UTF-8 string.
572  * 0 is success, < 0 is failure.
573  */
574
575 static int
576 ebml_read_utf8 (MatroskaDemuxContext *matroska,
577                 uint32_t             *id,
578                 char                **str)
579 {
580   return ebml_read_ascii(matroska, id, str);
581 }
582
583 /*
584  * Read the next element, but only the header. The contents
585  * are supposed to be sub-elements which can be read separately.
586  * 0 is success, < 0 is failure.
587  */
588
589 static int
590 ebml_read_master (MatroskaDemuxContext *matroska,
591                   uint32_t             *id)
592 {
593     ByteIOContext *pb = matroska->ctx->pb;
594     uint64_t length;
595     MatroskaLevel *level;
596     int res;
597
598     if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
599         (res = ebml_read_element_length(matroska, &length)) < 0)
600         return res;
601
602     /* protect... (Heaven forbids that the '>' is true) */
603     if (matroska->num_levels >= EBML_MAX_DEPTH) {
604         av_log(matroska->ctx, AV_LOG_ERROR,
605                "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH);
606         return AVERROR(ENOSYS);
607     }
608
609     /* remember level */
610     level = &matroska->levels[matroska->num_levels++];
611     level->start = url_ftell(pb);
612     level->length = length;
613
614     return 0;
615 }
616
617 /*
618  * Read the next element as binary data.
619  * 0 is success, < 0 is failure.
620  */
621
622 static int
623 ebml_read_binary (MatroskaDemuxContext *matroska,
624                   uint32_t             *id,
625                   uint8_t             **binary,
626                   int                  *size)
627 {
628     ByteIOContext *pb = matroska->ctx->pb;
629     uint64_t rlength;
630     int res;
631
632     if ((res = ebml_read_element_id(matroska, id, NULL)) < 0 ||
633         (res = ebml_read_element_length(matroska, &rlength)) < 0)
634         return res;
635     *size = rlength;
636
637     if (!(*binary = av_malloc(*size))) {
638         av_log(matroska->ctx, AV_LOG_ERROR,
639                "Memory allocation error\n");
640         return AVERROR(ENOMEM);
641     }
642
643     if (get_buffer(pb, *binary, *size) != *size) {
644         offset_t pos = url_ftell(pb);
645         av_log(matroska->ctx, AV_LOG_ERROR,
646                "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos);
647         return AVERROR(EIO);
648     }
649
650     return 0;
651 }
652
653 /*
654  * Read signed/unsigned "EBML" numbers.
655  * Return: number of bytes processed, < 0 on error.
656  * XXX: use ebml_read_num().
657  */
658
659 static int
660 matroska_ebmlnum_uint (uint8_t  *data,
661                        uint32_t  size,
662                        uint64_t *num)
663 {
664     int len_mask = 0x80, read = 1, n = 1, num_ffs = 0;
665     uint64_t total;
666
667     if (size <= 0)
668         return AVERROR_INVALIDDATA;
669
670     total = data[0];
671     while (read <= 8 && !(total & len_mask)) {
672         read++;
673         len_mask >>= 1;
674     }
675     if (read > 8)
676         return AVERROR_INVALIDDATA;
677
678     if ((total &= (len_mask - 1)) == len_mask - 1)
679         num_ffs++;
680     if (size < read)
681         return AVERROR_INVALIDDATA;
682     while (n < read) {
683         if (data[n] == 0xff)
684             num_ffs++;
685         total = (total << 8) | data[n];
686         n++;
687     }
688
689     if (read == num_ffs)
690         *num = (uint64_t)-1;
691     else
692         *num = total;
693
694     return read;
695 }
696
697 /*
698  * Same as above, but signed.
699  */
700
701 static int
702 matroska_ebmlnum_sint (uint8_t  *data,
703                        uint32_t  size,
704                        int64_t  *num)
705 {
706     uint64_t unum;
707     int res;
708
709     /* read as unsigned number first */
710     if ((res = matroska_ebmlnum_uint(data, size, &unum)) < 0)
711         return res;
712
713     /* make signed (weird way) */
714     if (unum == (uint64_t)-1)
715         *num = INT64_MAX;
716     else
717         *num = unum - ((1LL << ((7 * res) - 1)) - 1);
718
719     return res;
720 }
721
722
723 static int
724 matroska_find_track_by_num (MatroskaDemuxContext *matroska,
725                             int                   num)
726 {
727     int i;
728
729     for (i = 0; i < matroska->num_tracks; i++)
730         if (matroska->tracks[i]->num == num)
731             return i;
732
733     return -1;
734 }
735
736
737 /*
738  * Put one packet in an application-supplied AVPacket struct.
739  * Returns 0 on success or -1 on failure.
740  */
741
742 static int
743 matroska_deliver_packet (MatroskaDemuxContext *matroska,
744                          AVPacket             *pkt)
745 {
746     if (matroska->num_packets > 0) {
747         memcpy(pkt, matroska->packets[0], sizeof(AVPacket));
748         av_free(matroska->packets[0]);
749         if (matroska->num_packets > 1) {
750             memmove(&matroska->packets[0], &matroska->packets[1],
751                     (matroska->num_packets - 1) * sizeof(AVPacket *));
752             matroska->packets =
753                 av_realloc(matroska->packets, (matroska->num_packets - 1) *
754                            sizeof(AVPacket *));
755         } else {
756             av_freep(&matroska->packets);
757         }
758         matroska->num_packets--;
759         return 0;
760     }
761
762     return -1;
763 }
764
765 /*
766  * Put a packet into our internal queue. Will be delivered to the
767  * user/application during the next get_packet() call.
768  */
769
770 static void
771 matroska_queue_packet (MatroskaDemuxContext *matroska,
772                        AVPacket             *pkt)
773 {
774     matroska->packets =
775         av_realloc(matroska->packets, (matroska->num_packets + 1) *
776                    sizeof(AVPacket *));
777     matroska->packets[matroska->num_packets] = pkt;
778     matroska->num_packets++;
779 }
780
781 /*
782  * Free all packets in our internal queue.
783  */
784 static void
785 matroska_clear_queue (MatroskaDemuxContext *matroska)
786 {
787     if (matroska->packets) {
788         int n;
789         for (n = 0; n < matroska->num_packets; n++) {
790             av_free_packet(matroska->packets[n]);
791             av_free(matroska->packets[n]);
792         }
793         av_free(matroska->packets);
794         matroska->packets = NULL;
795         matroska->num_packets = 0;
796     }
797 }
798
799
800 /*
801  * Autodetecting...
802  */
803
804 static int
805 matroska_probe (AVProbeData *p)
806 {
807     uint64_t total = 0;
808     int len_mask = 0x80, size = 1, n = 1;
809     uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
810
811     /* ebml header? */
812     if (AV_RB32(p->buf) != EBML_ID_HEADER)
813         return 0;
814
815     /* length of header */
816     total = p->buf[4];
817     while (size <= 8 && !(total & len_mask)) {
818         size++;
819         len_mask >>= 1;
820     }
821     if (size > 8)
822       return 0;
823     total &= (len_mask - 1);
824     while (n < size)
825         total = (total << 8) | p->buf[4 + n++];
826
827     /* does the probe data contain the whole header? */
828     if (p->buf_size < 4 + size + total)
829       return 0;
830
831     /* the header must contain the document type 'matroska'. For now,
832      * we don't parse the whole header but simply check for the
833      * availability of that array of characters inside the header.
834      * Not fully fool-proof, but good enough. */
835     for (n = 4 + size; n <= 4 + size + total - sizeof(probe_data); n++)
836         if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
837             return AVPROBE_SCORE_MAX;
838
839     return 0;
840 }
841
842 /*
843  * From here on, it's all XML-style DTD stuff... Needs no comments.
844  */
845
846 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
847                       void *data, uint32_t expected_id, int once);
848
849 static int ebml_parse_elem(MatroskaDemuxContext *matroska,
850                            EbmlSyntax *syntax, void *data)
851 {
852     uint32_t id = syntax->id;
853     EbmlBin *bin;
854     int res;
855
856     data = (char *)data + syntax->data_offset;
857     if (syntax->list_elem_size) {
858         EbmlList *list = data;
859         list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
860         data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;
861         memset(data, 0, syntax->list_elem_size);
862         list->nb_elem++;
863     }
864     bin = data;
865
866     switch (syntax->type) {
867     case EBML_UINT:  return ebml_read_uint (matroska, &id, data);
868     case EBML_FLOAT: return ebml_read_float(matroska, &id, data);
869     case EBML_STR:
870     case EBML_UTF8:  av_free(*(char **)data);
871                      return ebml_read_ascii(matroska, &id, data);
872     case EBML_BIN:   av_free(bin->data);
873                      bin->pos = url_ftell(matroska->ctx->pb);
874                      return ebml_read_binary(matroska, &id, &bin->data,
875                                                             &bin->size);
876     case EBML_NEST:  if ((res=ebml_read_master(matroska, &id)) < 0)
877                          return res;
878                      if (id == MATROSKA_ID_SEGMENT)
879                          matroska->segment_start = url_ftell(matroska->ctx->pb);
880                      return ebml_parse(matroska, syntax->def.n, data, 0, 0);
881     case EBML_PASS:  return ebml_parse(matroska, syntax->def.n, data, 0, 1);
882     case EBML_STOP:  *(int *)data = 1;      return 1;
883     default:         return ebml_read_skip(matroska);
884     }
885 }
886
887 static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
888                          uint32_t id, void *data)
889 {
890     int i;
891     for (i=0; syntax[i].id; i++)
892         if (id == syntax[i].id)
893             break;
894     if (!syntax[i].id)
895         av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id);
896     return ebml_parse_elem(matroska, &syntax[i], data);
897 }
898
899 static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax,
900                       void *data, uint32_t expected_id, int once)
901 {
902     int i, res = 0;
903     uint32_t id = 0;
904
905     for (i=0; syntax[i].id; i++)
906         switch (syntax[i].type) {
907         case EBML_UINT:
908             *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u;
909             break;
910         case EBML_FLOAT:
911             *(double   *)((char *)data+syntax[i].data_offset) = syntax[i].def.f;
912             break;
913         case EBML_STR:
914         case EBML_UTF8:
915             *(char    **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s);
916             break;
917         }
918
919     if (expected_id) {
920         res = ebml_read_master(matroska, &id);
921         if (id != expected_id)
922             return AVERROR_INVALIDDATA;
923         if (id == MATROSKA_ID_SEGMENT)
924             matroska->segment_start = url_ftell(matroska->ctx->pb);
925     }
926
927     while (!res) {
928         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
929             res = AVERROR(EIO);
930             break;
931         } else if (matroska->level_up) {
932             matroska->level_up--;
933             break;
934         }
935
936         res = ebml_parse_id(matroska, syntax, id, data);
937         if (once)
938             break;
939
940
941         if (matroska->level_up) {
942             matroska->level_up--;
943             break;
944         }
945     }
946
947     return res;
948 }
949
950 static void ebml_free(EbmlSyntax *syntax, void *data)
951 {
952     int i, j;
953     for (i=0; syntax[i].id; i++) {
954         void *data_off = (char *)data + syntax[i].data_offset;
955         switch (syntax[i].type) {
956         case EBML_STR:
957         case EBML_UTF8:  av_freep(data_off);                      break;
958         case EBML_BIN:   av_freep(&((EbmlBin *)data_off)->data);  break;
959         case EBML_NEST:
960             if (syntax[i].list_elem_size) {
961                 EbmlList *list = data_off;
962                 char *ptr = list->elem;
963                 for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size)
964                     ebml_free(syntax[i].def.n, ptr);
965                 av_free(list->elem);
966             } else
967                 ebml_free(syntax[i].def.n, data_off);
968         default:  break;
969         }
970     }
971 }
972
973 static int
974 matroska_parse_info (MatroskaDemuxContext *matroska)
975 {
976     int res = 0;
977     uint32_t id;
978
979     av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
980
981     while (res == 0) {
982         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
983             res = AVERROR(EIO);
984             break;
985         } else if (matroska->level_up) {
986             matroska->level_up--;
987             break;
988         }
989
990         switch (id) {
991             /* cluster timecode */
992             case MATROSKA_ID_TIMECODESCALE: {
993                 uint64_t num;
994                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
995                     break;
996                 matroska->time_scale = num;
997                 break;
998             }
999
1000             case MATROSKA_ID_DURATION: {
1001                 double num;
1002                 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1003                     break;
1004                 matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
1005                 break;
1006             }
1007
1008             case MATROSKA_ID_TITLE: {
1009                 char *text;
1010                 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1011                     break;
1012                 strncpy(matroska->ctx->title, text,
1013                         sizeof(matroska->ctx->title)-1);
1014                 av_free(text);
1015                 break;
1016             }
1017
1018             default:
1019                 av_log(matroska->ctx, AV_LOG_INFO,
1020                        "Unknown entry 0x%x in info header\n", id);
1021                 /* fall-through */
1022
1023             case MATROSKA_ID_WRITINGAPP:
1024             case MATROSKA_ID_MUXINGAPP:
1025             case MATROSKA_ID_DATEUTC:
1026             case MATROSKA_ID_SEGMENTUID:
1027             case EBML_ID_VOID:
1028                 res = ebml_read_skip(matroska);
1029                 break;
1030         }
1031
1032         if (matroska->level_up) {
1033             matroska->level_up--;
1034             break;
1035         }
1036     }
1037
1038     return res;
1039 }
1040
1041 static int
1042 matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track)
1043 {
1044     uint8_t* data = *buf;
1045     int isize = *buf_size;
1046     uint8_t* pkt_data = NULL;
1047     int pkt_size = isize;
1048     int result = 0;
1049     int olen;
1050
1051     switch (track->encoding_algo) {
1052     case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:
1053         return track->encoding_settings_len;
1054     case MATROSKA_TRACK_ENCODING_COMP_LZO:
1055         do {
1056             olen = pkt_size *= 3;
1057             pkt_data = av_realloc(pkt_data,
1058                                   pkt_size+LZO_OUTPUT_PADDING);
1059             result = lzo1x_decode(pkt_data, &olen, data, &isize);
1060         } while (result==LZO_OUTPUT_FULL && pkt_size<10000000);
1061         if (result)
1062             goto failed;
1063         pkt_size -= olen;
1064         break;
1065 #ifdef CONFIG_ZLIB
1066     case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {
1067         z_stream zstream = {0};
1068         if (inflateInit(&zstream) != Z_OK)
1069             return -1;
1070         zstream.next_in = data;
1071         zstream.avail_in = isize;
1072         do {
1073             pkt_size *= 3;
1074             pkt_data = av_realloc(pkt_data, pkt_size);
1075             zstream.avail_out = pkt_size - zstream.total_out;
1076             zstream.next_out = pkt_data + zstream.total_out;
1077             result = inflate(&zstream, Z_NO_FLUSH);
1078         } while (result==Z_OK && pkt_size<10000000);
1079         pkt_size = zstream.total_out;
1080         inflateEnd(&zstream);
1081         if (result != Z_STREAM_END)
1082             goto failed;
1083         break;
1084     }
1085 #endif
1086 #ifdef CONFIG_BZLIB
1087     case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {
1088         bz_stream bzstream = {0};
1089         if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)
1090             return -1;
1091         bzstream.next_in = data;
1092         bzstream.avail_in = isize;
1093         do {
1094             pkt_size *= 3;
1095             pkt_data = av_realloc(pkt_data, pkt_size);
1096             bzstream.avail_out = pkt_size - bzstream.total_out_lo32;
1097             bzstream.next_out = pkt_data + bzstream.total_out_lo32;
1098             result = BZ2_bzDecompress(&bzstream);
1099         } while (result==BZ_OK && pkt_size<10000000);
1100         pkt_size = bzstream.total_out_lo32;
1101         BZ2_bzDecompressEnd(&bzstream);
1102         if (result != BZ_STREAM_END)
1103             goto failed;
1104         break;
1105     }
1106 #endif
1107     }
1108
1109     *buf = pkt_data;
1110     *buf_size = pkt_size;
1111     return 0;
1112  failed:
1113     av_free(pkt_data);
1114     return -1;
1115 }
1116
1117 static int
1118 matroska_add_stream (MatroskaDemuxContext *matroska)
1119 {
1120     int res = 0;
1121     uint32_t id;
1122     MatroskaTrack *track;
1123
1124     /* start with the master */
1125     if ((res = ebml_read_master(matroska, &id)) < 0)
1126         return res;
1127
1128     av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
1129
1130     /* Allocate a generic track. */
1131     track = av_mallocz(MAX_TRACK_SIZE);
1132     track->time_scale = 1.0;
1133     strcpy(track->language, "eng");
1134
1135     /* try reading the trackentry headers */
1136     while (res == 0) {
1137         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1138             res = AVERROR(EIO);
1139             break;
1140         } else if (matroska->level_up > 0) {
1141             matroska->level_up--;
1142             break;
1143         }
1144
1145         switch (id) {
1146             /* track number (unique stream ID) */
1147             case MATROSKA_ID_TRACKNUMBER: {
1148                 uint64_t num;
1149                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1150                     break;
1151                 track->num = num;
1152                 break;
1153             }
1154
1155             /* track UID (unique identifier) */
1156             case MATROSKA_ID_TRACKUID: {
1157                 uint64_t num;
1158                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1159                     break;
1160                 track->uid = num;
1161                 break;
1162             }
1163
1164             /* track type (video, audio, combined, subtitle, etc.) */
1165             case MATROSKA_ID_TRACKTYPE: {
1166                 uint64_t num;
1167                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1168                     break;
1169                 if (track->type && track->type != num) {
1170                     av_log(matroska->ctx, AV_LOG_INFO,
1171                            "More than one tracktype in an entry - skip\n");
1172                     break;
1173                 }
1174                 track->type = num;
1175
1176                 switch (track->type) {
1177                     case MATROSKA_TRACK_TYPE_VIDEO:
1178                     case MATROSKA_TRACK_TYPE_AUDIO:
1179                     case MATROSKA_TRACK_TYPE_SUBTITLE:
1180                         break;
1181                     case MATROSKA_TRACK_TYPE_COMPLEX:
1182                     case MATROSKA_TRACK_TYPE_LOGO:
1183                     case MATROSKA_TRACK_TYPE_CONTROL:
1184                     default:
1185                         av_log(matroska->ctx, AV_LOG_INFO,
1186                                "Unknown or unsupported track type 0x%x\n",
1187                                track->type);
1188                         track->type = MATROSKA_TRACK_TYPE_NONE;
1189                         break;
1190                 }
1191                 break;
1192             }
1193
1194             /* tracktype specific stuff for video */
1195             case MATROSKA_ID_TRACKVIDEO: {
1196                 MatroskaVideoTrack *videotrack;
1197                 if (!track->type)
1198                     track->type = MATROSKA_TRACK_TYPE_VIDEO;
1199                 if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
1200                     av_log(matroska->ctx, AV_LOG_INFO,
1201                            "video data in non-video track - ignoring\n");
1202                     res = AVERROR_INVALIDDATA;
1203                     break;
1204                 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1205                     break;
1206                 videotrack = (MatroskaVideoTrack *)track;
1207
1208                 while (res == 0) {
1209                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1210                         res = AVERROR(EIO);
1211                         break;
1212                     } else if (matroska->level_up > 0) {
1213                         matroska->level_up--;
1214                         break;
1215                     }
1216
1217                     switch (id) {
1218                         /* fixme, this should be one-up, but I get it here */
1219                         case MATROSKA_ID_TRACKDEFAULTDURATION: {
1220                             uint64_t num;
1221                             if ((res = ebml_read_uint (matroska, &id,
1222                                                        &num)) < 0)
1223                                 break;
1224                             track->default_duration = num;
1225                             break;
1226                         }
1227
1228                         /* video framerate */
1229                         case MATROSKA_ID_VIDEOFRAMERATE: {
1230                             double num;
1231                             if ((res = ebml_read_float(matroska, &id,
1232                                                        &num)) < 0)
1233                                 break;
1234                             if (!track->default_duration)
1235                                 track->default_duration = 1000000000/num;
1236                             break;
1237                         }
1238
1239                         /* width of the size to display the video at */
1240                         case MATROSKA_ID_VIDEODISPLAYWIDTH: {
1241                             uint64_t num;
1242                             if ((res = ebml_read_uint(matroska, &id,
1243                                                       &num)) < 0)
1244                                 break;
1245                             videotrack->display_width = num;
1246                             break;
1247                         }
1248
1249                         /* height of the size to display the video at */
1250                         case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
1251                             uint64_t num;
1252                             if ((res = ebml_read_uint(matroska, &id,
1253                                                       &num)) < 0)
1254                                 break;
1255                             videotrack->display_height = num;
1256                             break;
1257                         }
1258
1259                         /* width of the video in the file */
1260                         case MATROSKA_ID_VIDEOPIXELWIDTH: {
1261                             uint64_t num;
1262                             if ((res = ebml_read_uint(matroska, &id,
1263                                                       &num)) < 0)
1264                                 break;
1265                             videotrack->pixel_width = num;
1266                             break;
1267                         }
1268
1269                         /* height of the video in the file */
1270                         case MATROSKA_ID_VIDEOPIXELHEIGHT: {
1271                             uint64_t num;
1272                             if ((res = ebml_read_uint(matroska, &id,
1273                                                       &num)) < 0)
1274                                 break;
1275                             videotrack->pixel_height = num;
1276                             break;
1277                         }
1278
1279                         /* whether the video is interlaced */
1280                         case MATROSKA_ID_VIDEOFLAGINTERLACED: {
1281                             uint64_t num;
1282                             if ((res = ebml_read_uint(matroska, &id,
1283                                                       &num)) < 0)
1284                                 break;
1285                             break;
1286                         }
1287
1288                         /* colorspace (only matters for raw video)
1289                          * fourcc */
1290                         case MATROSKA_ID_VIDEOCOLORSPACE: {
1291                             uint64_t num;
1292                             if ((res = ebml_read_uint(matroska, &id,
1293                                                       &num)) < 0)
1294                                 break;
1295                             videotrack->fourcc = num;
1296                             break;
1297                         }
1298
1299                         default:
1300                             av_log(matroska->ctx, AV_LOG_INFO,
1301                                    "Unknown video track header entry "
1302                                    "0x%x - ignoring\n", id);
1303                             /* pass-through */
1304
1305                         case MATROSKA_ID_VIDEOSTEREOMODE:
1306                         case MATROSKA_ID_VIDEOASPECTRATIO:
1307                         case EBML_ID_VOID:
1308                             res = ebml_read_skip(matroska);
1309                             break;
1310                     }
1311
1312                     if (matroska->level_up) {
1313                         matroska->level_up--;
1314                         break;
1315                     }
1316                 }
1317                 break;
1318             }
1319
1320             /* tracktype specific stuff for audio */
1321             case MATROSKA_ID_TRACKAUDIO: {
1322                 MatroskaAudioTrack *audiotrack;
1323                 if (!track->type)
1324                     track->type = MATROSKA_TRACK_TYPE_AUDIO;
1325                 if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
1326                     av_log(matroska->ctx, AV_LOG_INFO,
1327                            "audio data in non-audio track - ignoring\n");
1328                     res = AVERROR_INVALIDDATA;
1329                     break;
1330                 } else if ((res = ebml_read_master(matroska, &id)) < 0)
1331                     break;
1332                 audiotrack = (MatroskaAudioTrack *)track;
1333                 audiotrack->channels = 1;
1334                 audiotrack->samplerate = 8000;
1335
1336                 while (res == 0) {
1337                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1338                         res = AVERROR(EIO);
1339                         break;
1340                     } else if (matroska->level_up > 0) {
1341                         matroska->level_up--;
1342                         break;
1343                     }
1344
1345                     switch (id) {
1346                         /* samplerate */
1347                         case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
1348                             double num;
1349                             if ((res = ebml_read_float(matroska, &id,
1350                                                        &num)) < 0)
1351                                 break;
1352                             audiotrack->internal_samplerate =
1353                             audiotrack->samplerate = num;
1354                             break;
1355                         }
1356
1357                         case MATROSKA_ID_AUDIOOUTSAMPLINGFREQ: {
1358                             double num;
1359                             if ((res = ebml_read_float(matroska, &id,
1360                                                        &num)) < 0)
1361                                 break;
1362                             audiotrack->samplerate = num;
1363                             break;
1364                         }
1365
1366                             /* bitdepth */
1367                         case MATROSKA_ID_AUDIOBITDEPTH: {
1368                             uint64_t num;
1369                             if ((res = ebml_read_uint(matroska, &id,
1370                                                       &num)) < 0)
1371                                 break;
1372                             audiotrack->bitdepth = num;
1373                             break;
1374                         }
1375
1376                             /* channels */
1377                         case MATROSKA_ID_AUDIOCHANNELS: {
1378                             uint64_t num;
1379                             if ((res = ebml_read_uint(matroska, &id,
1380                                                       &num)) < 0)
1381                                 break;
1382                             audiotrack->channels = num;
1383                             break;
1384                         }
1385
1386                         default:
1387                             av_log(matroska->ctx, AV_LOG_INFO,
1388                                    "Unknown audio track header entry "
1389                                    "0x%x - ignoring\n", id);
1390                             /* pass-through */
1391
1392                         case EBML_ID_VOID:
1393                             res = ebml_read_skip(matroska);
1394                             break;
1395                     }
1396
1397                     if (matroska->level_up) {
1398                         matroska->level_up--;
1399                         break;
1400                     }
1401                 }
1402                 break;
1403             }
1404
1405                 /* codec identifier */
1406             case MATROSKA_ID_CODECID: {
1407                 char *text;
1408                 if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
1409                     break;
1410                 track->codec_id = text;
1411                 break;
1412             }
1413
1414                 /* codec private data */
1415             case MATROSKA_ID_CODECPRIVATE: {
1416                 uint8_t *data;
1417                 int size;
1418                 if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1419                     break;
1420                 track->codec_priv = data;
1421                 track->codec_priv_size = size;
1422                 break;
1423             }
1424
1425                 /* name of this track */
1426             case MATROSKA_ID_TRACKNAME: {
1427                 char *text;
1428                 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1429                     break;
1430                 track->name = text;
1431                 break;
1432             }
1433
1434                 /* language (matters for audio/subtitles, mostly) */
1435             case MATROSKA_ID_TRACKLANGUAGE: {
1436                 char *text, *end;
1437                 if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
1438                     break;
1439                 if ((end = strchr(text, '-')))
1440                     *end = '\0';
1441                 if (strlen(text) == 3)
1442                     strcpy(track->language, text);
1443                 av_free(text);
1444                 break;
1445             }
1446
1447                 /* whether this is actually used */
1448             case MATROSKA_ID_TRACKFLAGENABLED: {
1449                 uint64_t num;
1450                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1451                     break;
1452                 break;
1453             }
1454
1455                 /* whether it's the default for this track type */
1456             case MATROSKA_ID_TRACKFLAGDEFAULT: {
1457                 uint64_t num;
1458                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1459                     break;
1460                 track->flag_default = num;
1461                 break;
1462             }
1463
1464                 /* lacing (like MPEG, where blocks don't end/start on frame
1465                  * boundaries) */
1466             case MATROSKA_ID_TRACKFLAGLACING: {
1467                 uint64_t num;
1468                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1469                     break;
1470                 break;
1471             }
1472
1473                 /* default length (in time) of one data block in this track */
1474             case MATROSKA_ID_TRACKDEFAULTDURATION: {
1475                 uint64_t num;
1476                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1477                     break;
1478                 track->default_duration = num;
1479                 break;
1480             }
1481
1482             case MATROSKA_ID_TRACKCONTENTENCODINGS: {
1483                 if ((res = ebml_read_master(matroska, &id)) < 0)
1484                     break;
1485
1486                 while (res == 0) {
1487                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1488                         res = AVERROR(EIO);
1489                         break;
1490                     } else if (matroska->level_up > 0) {
1491                         matroska->level_up--;
1492                         break;
1493                     }
1494
1495                     switch (id) {
1496                         case MATROSKA_ID_TRACKCONTENTENCODING: {
1497                             int encoding_scope = 1;
1498                             if ((res = ebml_read_master(matroska, &id)) < 0)
1499                                 break;
1500
1501                             while (res == 0) {
1502                                 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1503                                     res = AVERROR(EIO);
1504                                     break;
1505                                 } else if (matroska->level_up > 0) {
1506                                     matroska->level_up--;
1507                                     break;
1508                                 }
1509
1510                                 switch (id) {
1511                                     case MATROSKA_ID_ENCODINGSCOPE: {
1512                                         uint64_t num;
1513                                         if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1514                                             break;
1515                                         encoding_scope = num;
1516                                         break;
1517                                     }
1518
1519                                     case MATROSKA_ID_ENCODINGTYPE: {
1520                                         uint64_t num;
1521                                         if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1522                                             break;
1523                                         if (num)
1524                                             av_log(matroska->ctx, AV_LOG_ERROR,
1525                                                    "Unsupported encoding type");
1526                                         break;
1527                                     }
1528
1529                                     case MATROSKA_ID_ENCODINGCOMPRESSION: {
1530                                         if ((res = ebml_read_master(matroska, &id)) < 0)
1531                                             break;
1532
1533                                         while (res == 0) {
1534                                             if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1535                                                 res = AVERROR(EIO);
1536                                                 break;
1537                                             } else if (matroska->level_up > 0) {
1538                                                 matroska->level_up--;
1539                                                 break;
1540                                             }
1541
1542                                             switch (id) {
1543                                                 case MATROSKA_ID_ENCODINGCOMPALGO: {
1544                                                     uint64_t num;
1545                                                     if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
1546                                                         break;
1547                                                     if (num != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP &&
1548 #ifdef CONFIG_ZLIB
1549                                                         num != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
1550 #endif
1551 #ifdef CONFIG_BZLIB
1552                                                         num != MATROSKA_TRACK_ENCODING_COMP_BZLIB &&
1553 #endif
1554                                                         num != MATROSKA_TRACK_ENCODING_COMP_LZO)
1555                                                         av_log(matroska->ctx, AV_LOG_ERROR,
1556                                                                "Unsupported compression algo\n");
1557                                                     track->encoding_algo = num;
1558                                                     break;
1559                                                 }
1560
1561                                                 case MATROSKA_ID_ENCODINGCOMPSETTINGS: {
1562                                                     uint8_t *data;
1563                                                     int size;
1564                                                     if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
1565                                                         break;
1566                                                     track->encoding_settings = data;
1567                                                     track->encoding_settings_len = size;
1568                                                     break;
1569                                                 }
1570
1571                                                 default:
1572                                                     av_log(matroska->ctx, AV_LOG_INFO,
1573                                                            "Unknown compression header entry "
1574                                                            "0x%x - ignoring\n", id);
1575                                                     /* pass-through */
1576
1577                                                 case EBML_ID_VOID:
1578                                                     res = ebml_read_skip(matroska);
1579                                                     break;
1580                                             }
1581
1582                                             if (matroska->level_up) {
1583                                                 matroska->level_up--;
1584                                                 break;
1585                                             }
1586                                         }
1587                                         break;
1588                                     }
1589
1590                                     default:
1591                                         av_log(matroska->ctx, AV_LOG_INFO,
1592                                                "Unknown content encoding header entry "
1593                                                "0x%x - ignoring\n", id);
1594                                         /* pass-through */
1595
1596                                     case EBML_ID_VOID:
1597                                         res = ebml_read_skip(matroska);
1598                                         break;
1599                                 }
1600
1601                                 if (matroska->level_up) {
1602                                     matroska->level_up--;
1603                                     break;
1604                                 }
1605                             }
1606
1607                             track->encoding_scope = encoding_scope;
1608                             break;
1609                         }
1610
1611                         default:
1612                             av_log(matroska->ctx, AV_LOG_INFO,
1613                                    "Unknown content encodings header entry "
1614                                    "0x%x - ignoring\n", id);
1615                             /* pass-through */
1616
1617                         case EBML_ID_VOID:
1618                             res = ebml_read_skip(matroska);
1619                             break;
1620                     }
1621
1622                     if (matroska->level_up) {
1623                         matroska->level_up--;
1624                         break;
1625                     }
1626                 }
1627                 break;
1628             }
1629
1630             case MATROSKA_ID_TRACKTIMECODESCALE: {
1631                 double num;
1632                 if ((res = ebml_read_float(matroska, &id, &num)) < 0)
1633                     break;
1634                 track->time_scale = num;
1635                 break;
1636             }
1637
1638             default:
1639                 av_log(matroska->ctx, AV_LOG_INFO,
1640                        "Unknown track header entry 0x%x - ignoring\n", id);
1641                 /* pass-through */
1642
1643             case EBML_ID_VOID:
1644             /* we ignore these because they're nothing useful. */
1645             case MATROSKA_ID_TRACKFLAGFORCED:
1646             case MATROSKA_ID_CODECNAME:
1647             case MATROSKA_ID_CODECDECODEALL:
1648             case MATROSKA_ID_CODECINFOURL:
1649             case MATROSKA_ID_CODECDOWNLOADURL:
1650             case MATROSKA_ID_TRACKMINCACHE:
1651             case MATROSKA_ID_TRACKMAXCACHE:
1652                 res = ebml_read_skip(matroska);
1653                 break;
1654         }
1655
1656         if (matroska->level_up) {
1657             matroska->level_up--;
1658             break;
1659         }
1660     }
1661
1662     if (track->codec_priv_size && track->encoding_scope & 2) {
1663         uint8_t *orig_priv = track->codec_priv;
1664         int offset = matroska_decode_buffer(&track->codec_priv,
1665                                             &track->codec_priv_size, track);
1666         if (offset > 0) {
1667             track->codec_priv = av_malloc(track->codec_priv_size + offset);
1668             memcpy(track->codec_priv, track->encoding_settings, offset);
1669             memcpy(track->codec_priv+offset, orig_priv, track->codec_priv_size);
1670             track->codec_priv_size += offset;
1671             av_free(orig_priv);
1672         } else if (!offset) {
1673             av_free(orig_priv);
1674         } else
1675             av_log(matroska->ctx, AV_LOG_ERROR,
1676                    "Failed to decode codec private data\n");
1677     }
1678
1679     if (track->type && matroska->num_tracks < ARRAY_SIZE(matroska->tracks)) {
1680         matroska->tracks[matroska->num_tracks++] = track;
1681     } else {
1682         av_free(track);
1683     }
1684     return res;
1685 }
1686
1687 static int
1688 matroska_parse_tracks (MatroskaDemuxContext *matroska)
1689 {
1690     int res = 0;
1691     uint32_t id;
1692
1693     av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
1694
1695     while (res == 0) {
1696         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1697             res = AVERROR(EIO);
1698             break;
1699         } else if (matroska->level_up) {
1700             matroska->level_up--;
1701             break;
1702         }
1703
1704         switch (id) {
1705             /* one track within the "all-tracks" header */
1706             case MATROSKA_ID_TRACKENTRY:
1707                 res = matroska_add_stream(matroska);
1708                 break;
1709
1710             default:
1711                 av_log(matroska->ctx, AV_LOG_INFO,
1712                        "Unknown entry 0x%x in track header\n", id);
1713                 /* fall-through */
1714
1715             case EBML_ID_VOID:
1716                 res = ebml_read_skip(matroska);
1717                 break;
1718         }
1719
1720         if (matroska->level_up) {
1721             matroska->level_up--;
1722             break;
1723         }
1724     }
1725
1726     return res;
1727 }
1728
1729 static int
1730 matroska_parse_index (MatroskaDemuxContext *matroska)
1731 {
1732     int res = 0;
1733     uint32_t id;
1734     MatroskaDemuxIndex idx;
1735
1736     av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
1737
1738     while (res == 0) {
1739         if (!(id = ebml_peek_id(matroska, &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             /* one single index entry ('point') */
1749             case MATROSKA_ID_POINTENTRY:
1750                 if ((res = ebml_read_master(matroska, &id)) < 0)
1751                     break;
1752
1753                 /* in the end, we hope to fill one entry with a
1754                  * timestamp, a file position and a tracknum */
1755                 idx.pos   = (uint64_t) -1;
1756                 idx.time  = (uint64_t) -1;
1757                 idx.track = (uint16_t) -1;
1758
1759                 while (res == 0) {
1760                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1761                         res = AVERROR(EIO);
1762                         break;
1763                     } else if (matroska->level_up) {
1764                         matroska->level_up--;
1765                         break;
1766                     }
1767
1768                     switch (id) {
1769                         /* one single index entry ('point') */
1770                         case MATROSKA_ID_CUETIME: {
1771                             uint64_t time;
1772                             if ((res = ebml_read_uint(matroska, &id,
1773                                                       &time)) < 0)
1774                                 break;
1775                             idx.time = time * matroska->time_scale;
1776                             break;
1777                         }
1778
1779                         /* position in the file + track to which it
1780                          * belongs */
1781                         case MATROSKA_ID_CUETRACKPOSITION:
1782                             if ((res = ebml_read_master(matroska, &id)) < 0)
1783                                 break;
1784
1785                             while (res == 0) {
1786                                 if (!(id = ebml_peek_id (matroska,
1787                                                     &matroska->level_up))) {
1788                                     res = AVERROR(EIO);
1789                                     break;
1790                                 } else if (matroska->level_up) {
1791                                     matroska->level_up--;
1792                                     break;
1793                                 }
1794
1795                                 switch (id) {
1796                                     /* track number */
1797                                     case MATROSKA_ID_CUETRACK: {
1798                                         uint64_t num;
1799                                         if ((res = ebml_read_uint(matroska,
1800                                                           &id, &num)) < 0)
1801                                             break;
1802                                         idx.track = num;
1803                                         break;
1804                                     }
1805
1806                                         /* position in file */
1807                                     case MATROSKA_ID_CUECLUSTERPOSITION: {
1808                                         uint64_t num;
1809                                         if ((res = ebml_read_uint(matroska,
1810                                                           &id, &num)) < 0)
1811                                             break;
1812                                         idx.pos = num+matroska->segment_start;
1813                                         break;
1814                                     }
1815
1816                                     default:
1817                                         av_log(matroska->ctx, AV_LOG_INFO,
1818                                                "Unknown entry 0x%x in "
1819                                                "CuesTrackPositions\n", id);
1820                                         /* fall-through */
1821
1822                                     case EBML_ID_VOID:
1823                                         res = ebml_read_skip(matroska);
1824                                         break;
1825                                 }
1826
1827                                 if (matroska->level_up) {
1828                                     matroska->level_up--;
1829                                     break;
1830                                 }
1831                             }
1832
1833                             break;
1834
1835                         default:
1836                             av_log(matroska->ctx, AV_LOG_INFO,
1837                                    "Unknown entry 0x%x in cuespoint "
1838                                    "index\n", id);
1839                             /* fall-through */
1840
1841                         case EBML_ID_VOID:
1842                             res = ebml_read_skip(matroska);
1843                             break;
1844                     }
1845
1846                     if (matroska->level_up) {
1847                         matroska->level_up--;
1848                         break;
1849                     }
1850                 }
1851
1852                 /* so let's see if we got what we wanted */
1853                 if (idx.pos   != (uint64_t) -1 &&
1854                     idx.time  != (uint64_t) -1 &&
1855                     idx.track != (uint16_t) -1) {
1856                     if (matroska->num_indexes % 32 == 0) {
1857                         /* re-allocate bigger index */
1858                         matroska->index =
1859                             av_realloc(matroska->index,
1860                                        (matroska->num_indexes + 32) *
1861                                        sizeof(MatroskaDemuxIndex));
1862                     }
1863                     matroska->index[matroska->num_indexes] = idx;
1864                     matroska->num_indexes++;
1865                 }
1866                 break;
1867
1868             default:
1869                 av_log(matroska->ctx, AV_LOG_INFO,
1870                        "Unknown entry 0x%x in cues header\n", id);
1871                 /* fall-through */
1872
1873             case EBML_ID_VOID:
1874                 res = ebml_read_skip(matroska);
1875                 break;
1876         }
1877
1878         if (matroska->level_up) {
1879             matroska->level_up--;
1880             break;
1881         }
1882     }
1883
1884     return res;
1885 }
1886
1887 static int
1888 matroska_parse_metadata (MatroskaDemuxContext *matroska)
1889 {
1890     int res = 0;
1891     uint32_t id;
1892
1893     while (res == 0) {
1894         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1895             res = AVERROR(EIO);
1896             break;
1897         } else if (matroska->level_up) {
1898             matroska->level_up--;
1899             break;
1900         }
1901
1902         switch (id) {
1903             /* Hm, this is unsupported... */
1904             default:
1905                 av_log(matroska->ctx, AV_LOG_INFO,
1906                        "Unknown entry 0x%x in metadata header\n", id);
1907                 /* fall-through */
1908
1909             case EBML_ID_VOID:
1910                 res = ebml_read_skip(matroska);
1911                 break;
1912         }
1913
1914         if (matroska->level_up) {
1915             matroska->level_up--;
1916             break;
1917         }
1918     }
1919
1920     return res;
1921 }
1922
1923 static int
1924 matroska_parse_seekhead (MatroskaDemuxContext *matroska)
1925 {
1926     int res = 0;
1927     uint32_t id;
1928
1929     av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
1930
1931     while (res == 0) {
1932         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1933             res = AVERROR(EIO);
1934             break;
1935         } else if (matroska->level_up) {
1936             matroska->level_up--;
1937             break;
1938         }
1939
1940         switch (id) {
1941             case MATROSKA_ID_SEEKENTRY: {
1942                 uint32_t seek_id = 0, peek_id_cache = 0;
1943                 uint64_t seek_pos = (uint64_t) -1, t;
1944                 int dummy_level = 0;
1945
1946                 if ((res = ebml_read_master(matroska, &id)) < 0)
1947                     break;
1948
1949                 while (res == 0) {
1950                     if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
1951                         res = AVERROR(EIO);
1952                         break;
1953                     } else if (matroska->level_up) {
1954                         matroska->level_up--;
1955                         break;
1956                     }
1957
1958                     switch (id) {
1959                         case MATROSKA_ID_SEEKID:
1960                             res = ebml_read_uint(matroska, &id, &t);
1961                             seek_id = t;
1962                             break;
1963
1964                         case MATROSKA_ID_SEEKPOSITION:
1965                             res = ebml_read_uint(matroska, &id, &seek_pos);
1966                             break;
1967
1968                         default:
1969                             av_log(matroska->ctx, AV_LOG_INFO,
1970                                    "Unknown seekhead ID 0x%x\n", id);
1971                             /* fall-through */
1972
1973                         case EBML_ID_VOID:
1974                             res = ebml_read_skip(matroska);
1975                             break;
1976                     }
1977
1978                     if (matroska->level_up) {
1979                         matroska->level_up--;
1980                         break;
1981                     }
1982                 }
1983
1984                 if (!seek_id || seek_pos == (uint64_t) -1) {
1985                     av_log(matroska->ctx, AV_LOG_INFO,
1986                            "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
1987                            seek_id, seek_pos);
1988                     break;
1989                 }
1990
1991                 switch (seek_id) {
1992                     case MATROSKA_ID_CUES:
1993                     case MATROSKA_ID_TAGS: {
1994                         uint32_t level_up = matroska->level_up;
1995                         offset_t before_pos;
1996                         uint64_t length;
1997                         MatroskaLevel level;
1998
1999                         /* remember the peeked ID and the current position */
2000                         peek_id_cache = matroska->peek_id;
2001                         before_pos = url_ftell(matroska->ctx->pb);
2002
2003                         /* seek */
2004                         if ((res = ebml_read_seek(matroska, seek_pos +
2005                                                matroska->segment_start)) < 0)
2006                             goto finish;
2007
2008                         /* we don't want to lose our seekhead level, so we add
2009                          * a dummy. This is a crude hack. */
2010                         if (matroska->num_levels == EBML_MAX_DEPTH) {
2011                             av_log(matroska->ctx, AV_LOG_INFO,
2012                                    "Max EBML element depth (%d) reached, "
2013                                    "cannot parse further.\n", EBML_MAX_DEPTH);
2014                             return AVERROR_UNKNOWN;
2015                         }
2016
2017                         level.start = 0;
2018                         level.length = (uint64_t)-1;
2019                         matroska->levels[matroska->num_levels] = level;
2020                         matroska->num_levels++;
2021                         dummy_level = 1;
2022
2023                         /* check ID */
2024                         if (!(id = ebml_peek_id (matroska,
2025                                                  &matroska->level_up)))
2026                             goto finish;
2027                         if (id != seek_id) {
2028                             av_log(matroska->ctx, AV_LOG_INFO,
2029                                    "We looked for ID=0x%x but got "
2030                                    "ID=0x%x (pos=%"PRIu64")",
2031                                    seek_id, id, seek_pos +
2032                                    matroska->segment_start);
2033                             goto finish;
2034                         }
2035
2036                         /* read master + parse */
2037                         if ((res = ebml_read_master(matroska, &id)) < 0)
2038                             goto finish;
2039                         switch (id) {
2040                             case MATROSKA_ID_CUES:
2041                                 if (!(res = matroska_parse_index(matroska)) ||
2042                                     url_feof(matroska->ctx->pb)) {
2043                                     matroska->index_parsed = 1;
2044                                     res = 0;
2045                                 }
2046                                 break;
2047                             case MATROSKA_ID_TAGS:
2048                                 if (!(res = matroska_parse_metadata(matroska)) ||
2049                                    url_feof(matroska->ctx->pb)) {
2050                                     matroska->metadata_parsed = 1;
2051                                     res = 0;
2052                                 }
2053                                 break;
2054                         }
2055
2056                     finish:
2057                         /* remove dummy level */
2058                         if (dummy_level)
2059                             while (matroska->num_levels) {
2060                                 matroska->num_levels--;
2061                                 length =
2062                                   matroska->levels[matroska->num_levels].length;
2063                                 if (length == (uint64_t)-1)
2064                                     break;
2065                             }
2066
2067                         /* seek back */
2068                         if ((res = ebml_read_seek(matroska, before_pos)) < 0)
2069                             return res;
2070                         matroska->peek_id = peek_id_cache;
2071                         matroska->level_up = level_up;
2072                         break;
2073                     }
2074
2075                     default:
2076                         av_log(matroska->ctx, AV_LOG_INFO,
2077                                "Ignoring seekhead entry for ID=0x%x\n",
2078                                seek_id);
2079                         break;
2080                 }
2081
2082                 break;
2083             }
2084
2085             default:
2086                 av_log(matroska->ctx, AV_LOG_INFO,
2087                        "Unknown seekhead ID 0x%x\n", id);
2088                 /* fall-through */
2089
2090             case EBML_ID_VOID:
2091                 res = ebml_read_skip(matroska);
2092                 break;
2093         }
2094
2095         if (matroska->level_up) {
2096             matroska->level_up--;
2097             break;
2098         }
2099     }
2100
2101     return res;
2102 }
2103
2104 static int
2105 matroska_parse_attachments(AVFormatContext *s)
2106 {
2107     MatroskaDemuxContext *matroska = s->priv_data;
2108     int res = 0;
2109     uint32_t id;
2110
2111     av_log(matroska->ctx, AV_LOG_DEBUG, "parsing attachments...\n");
2112
2113     while (res == 0) {
2114         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2115             res = AVERROR(EIO);
2116             break;
2117         } else if (matroska->level_up) {
2118             matroska->level_up--;
2119             break;
2120         }
2121
2122         switch (id) {
2123         case MATROSKA_ID_ATTACHEDFILE: {
2124             char* name = NULL;
2125             char* mime = NULL;
2126             uint8_t* data = NULL;
2127             int i, data_size = 0;
2128             AVStream *st;
2129
2130             if ((res = ebml_read_master(matroska, &id)) < 0)
2131                 break;
2132
2133             while (res == 0) {
2134                 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2135                     res = AVERROR(EIO);
2136                     break;
2137                 } else if (matroska->level_up) {
2138                     matroska->level_up--;
2139                     break;
2140                 }
2141
2142                 switch (id) {
2143                 case MATROSKA_ID_FILENAME:
2144                     res = ebml_read_utf8 (matroska, &id, &name);
2145                     break;
2146
2147                 case MATROSKA_ID_FILEMIMETYPE:
2148                     res = ebml_read_ascii (matroska, &id, &mime);
2149                     break;
2150
2151                 case MATROSKA_ID_FILEDATA:
2152                     res = ebml_read_binary(matroska, &id, &data, &data_size);
2153                     break;
2154
2155                 default:
2156                     av_log(matroska->ctx, AV_LOG_INFO,
2157                            "Unknown attachedfile ID 0x%x\n", id);
2158                 case MATROSKA_ID_FILEUID:
2159                 case EBML_ID_VOID:
2160                     res = ebml_read_skip(matroska);
2161                     break;
2162                 }
2163
2164                 if (matroska->level_up) {
2165                     matroska->level_up--;
2166                     break;
2167                 }
2168             }
2169
2170             if (!(name && mime && data && data_size > 0)) {
2171                 av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n");
2172                 break;
2173             }
2174
2175             st = av_new_stream(s, matroska->num_streams++);
2176             if (st == NULL)
2177                 return AVERROR(ENOMEM);
2178             st->filename = av_strdup(name);
2179             st->codec->codec_id = CODEC_ID_NONE;
2180             st->codec->codec_type = CODEC_TYPE_ATTACHMENT;
2181             st->codec->extradata = av_malloc(data_size);
2182             if(st->codec->extradata == NULL)
2183                 return AVERROR(ENOMEM);
2184             st->codec->extradata_size = data_size;
2185             memcpy(st->codec->extradata, data, data_size);
2186
2187             for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) {
2188                 if (!strncmp(ff_mkv_mime_tags[i].str, mime,
2189                              strlen(ff_mkv_mime_tags[i].str))) {
2190                     st->codec->codec_id = ff_mkv_mime_tags[i].id;
2191                     break;
2192                 }
2193             }
2194
2195             av_log(matroska->ctx, AV_LOG_DEBUG, "new attachment: %s, %s, size %d \n", name, mime, data_size);
2196             break;
2197         }
2198
2199         default:
2200             av_log(matroska->ctx, AV_LOG_INFO,
2201                    "Unknown attachments ID 0x%x\n", id);
2202             /* fall-through */
2203
2204         case EBML_ID_VOID:
2205             res = ebml_read_skip(matroska);
2206             break;
2207         }
2208
2209         if (matroska->level_up) {
2210             matroska->level_up--;
2211             break;
2212         }
2213     }
2214
2215     return res;
2216 }
2217
2218 static int
2219 matroska_parse_chapters(AVFormatContext *s)
2220 {
2221     MatroskaDemuxContext *matroska = s->priv_data;
2222     int res = 0;
2223     uint32_t id;
2224
2225     av_log(s, AV_LOG_DEBUG, "parsing chapters...\n");
2226
2227     while (res == 0) {
2228         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2229             res = AVERROR(EIO);
2230             break;
2231         } else if (matroska->level_up) {
2232             matroska->level_up--;
2233             break;
2234         }
2235
2236         switch (id) {
2237         case MATROSKA_ID_EDITIONENTRY: {
2238             uint64_t end = AV_NOPTS_VALUE, start = AV_NOPTS_VALUE;
2239             int64_t uid= -1;
2240             char* title = NULL;
2241             /* if there is more than one chapter edition
2242                we take only the first one */
2243             if(s->chapters) {
2244                     ebml_read_skip(matroska);
2245                     break;
2246             }
2247
2248             if ((res = ebml_read_master(matroska, &id)) < 0)
2249                 break;
2250
2251             while (res == 0) {
2252                 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2253                     res = AVERROR(EIO);
2254                     break;
2255                 } else if (matroska->level_up) {
2256                     matroska->level_up--;
2257                     break;
2258                 }
2259
2260                 switch (id) {
2261                 case MATROSKA_ID_CHAPTERATOM:
2262                     if ((res = ebml_read_master(matroska, &id)) < 0)
2263                         break;
2264
2265                     while (res == 0) {
2266                         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2267                             res = AVERROR(EIO);
2268                             break;
2269                         } else if (matroska->level_up) {
2270                             matroska->level_up--;
2271                             break;
2272                         }
2273
2274                         switch (id) {
2275                         case MATROSKA_ID_CHAPTERTIMEEND:
2276                             res = ebml_read_uint(matroska, &id, &end);
2277                             break;
2278
2279                         case MATROSKA_ID_CHAPTERTIMESTART:
2280                             res = ebml_read_uint(matroska, &id, &start);
2281                             break;
2282
2283                         case MATROSKA_ID_CHAPTERDISPLAY:
2284                             if ((res = ebml_read_master(matroska, &id)) < 0)
2285                                 break;
2286
2287                             while (res == 0) {
2288                                 if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2289                                     res = AVERROR(EIO);
2290                                     break;
2291                                 } else if (matroska->level_up) {
2292                                     matroska->level_up--;
2293                                     break;
2294                                 }
2295
2296                                 switch (id) {
2297                                 case MATROSKA_ID_CHAPSTRING:
2298                                     res = ebml_read_utf8(matroska, &id, &title);
2299                                     break;
2300
2301                                 default:
2302                                     av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter display ID 0x%x\n", id);
2303                                 case EBML_ID_VOID:
2304                                     res = ebml_read_skip(matroska);
2305                                     break;
2306                                 }
2307
2308                                 if (matroska->level_up) {
2309                                     matroska->level_up--;
2310                                     break;
2311                                 }
2312                             }
2313                             break;
2314
2315                         case MATROSKA_ID_CHAPTERUID:
2316                             res = ebml_read_uint(matroska, &id, &uid);
2317                             break;
2318                         default:
2319                             av_log(s, AV_LOG_INFO, "Ignoring unknown Chapter atom ID 0x%x\n", id);
2320                         case MATROSKA_ID_CHAPTERFLAGHIDDEN:
2321                         case EBML_ID_VOID:
2322                             res = ebml_read_skip(matroska);
2323                             break;
2324                         }
2325
2326                         if (matroska->level_up) {
2327                             matroska->level_up--;
2328                             break;
2329                         }
2330                     }
2331
2332                     if (start != AV_NOPTS_VALUE && uid != -1) {
2333                         if(!ff_new_chapter(s, uid, (AVRational){1, 1000000000}, start, end, title))
2334                             res= AVERROR(ENOMEM);
2335                     }
2336                     av_free(title);
2337                     break;
2338
2339                 default:
2340                     av_log(s, AV_LOG_INFO, "Ignoring unknown Edition entry ID 0x%x\n", id);
2341                 case MATROSKA_ID_EDITIONUID:
2342                 case MATROSKA_ID_EDITIONFLAGHIDDEN:
2343                 case MATROSKA_ID_EDITIONFLAGDEFAULT:
2344                 case EBML_ID_VOID:
2345                     res = ebml_read_skip(matroska);
2346                     break;
2347                 }
2348
2349
2350                 if (matroska->level_up) {
2351                     matroska->level_up--;
2352                     break;
2353                 }
2354             }
2355         break;
2356         }
2357
2358         default:
2359             av_log(s, AV_LOG_INFO, "Expected an Edition entry (0x%x), but found 0x%x\n", MATROSKA_ID_EDITIONENTRY, id);
2360         case EBML_ID_VOID:
2361             res = ebml_read_skip(matroska);
2362             break;
2363         }
2364
2365         if (matroska->level_up) {
2366             matroska->level_up--;
2367             break;
2368         }
2369     }
2370
2371     return res;
2372 }
2373
2374 static int
2375 matroska_aac_profile (char *codec_id)
2376 {
2377     static const char *aac_profiles[] = {
2378         "MAIN", "LC", "SSR"
2379     };
2380     int profile;
2381
2382     for (profile=0; profile<ARRAY_SIZE(aac_profiles); profile++)
2383         if (strstr(codec_id, aac_profiles[profile]))
2384             break;
2385     return profile + 1;
2386 }
2387
2388 static int
2389 matroska_aac_sri (int samplerate)
2390 {
2391     int sri;
2392
2393     for (sri=0; sri<ARRAY_SIZE(ff_mpeg4audio_sample_rates); sri++)
2394         if (ff_mpeg4audio_sample_rates[sri] == samplerate)
2395             break;
2396     return sri;
2397 }
2398
2399 static int
2400 matroska_read_header (AVFormatContext    *s,
2401                       AVFormatParameters *ap)
2402 {
2403     MatroskaDemuxContext *matroska = s->priv_data;
2404     int last_level, res = 0;
2405     Ebml ebml = { 0 };
2406     uint32_t id;
2407
2408     matroska->ctx = s;
2409
2410     /* First read the EBML header. */
2411     if (ebml_parse(matroska, ebml_syntax, &ebml, 0, 1)
2412         || ebml.version > EBML_VERSION       || ebml.max_size > sizeof(uint64_t)
2413         || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska")
2414         || ebml.doctype_version > 2) {
2415         av_log(matroska->ctx, AV_LOG_ERROR,
2416                "EBML header using unsupported features\n"
2417                "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n",
2418                ebml.version, ebml.doctype, ebml.doctype_version);
2419         return AVERROR_NOFMT;
2420     }
2421     ebml_free(ebml_syntax, &ebml);
2422
2423     /* The next thing is a segment. */
2424     while (1) {
2425         if (!(id = ebml_peek_id(matroska, &last_level)))
2426             return AVERROR(EIO);
2427         if (id == MATROSKA_ID_SEGMENT)
2428             break;
2429
2430         /* oi! */
2431         av_log(matroska->ctx, AV_LOG_INFO,
2432                "Expected a Segment ID (0x%x), but received 0x%x!\n",
2433                MATROSKA_ID_SEGMENT, id);
2434         if ((res = ebml_read_skip(matroska)) < 0)
2435             return res;
2436     }
2437
2438     /* We now have a Matroska segment.
2439      * Seeks are from the beginning of the segment,
2440      * after the segment ID/length. */
2441     if ((res = ebml_read_master(matroska, &id)) < 0)
2442         return res;
2443     matroska->segment_start = url_ftell(s->pb);
2444
2445     matroska->time_scale = 1000000;
2446     /* we've found our segment, start reading the different contents in here */
2447     while (res == 0) {
2448         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
2449             res = AVERROR(EIO);
2450             break;
2451         } else if (matroska->level_up) {
2452             matroska->level_up--;
2453             break;
2454         }
2455
2456         switch (id) {
2457             /* stream info */
2458             case MATROSKA_ID_INFO: {
2459                 if ((res = ebml_read_master(matroska, &id)) < 0)
2460                     break;
2461                 res = matroska_parse_info(matroska);
2462                 break;
2463             }
2464
2465             /* track info headers */
2466             case MATROSKA_ID_TRACKS: {
2467                 if ((res = ebml_read_master(matroska, &id)) < 0)
2468                     break;
2469                 res = matroska_parse_tracks(matroska);
2470                 break;
2471             }
2472
2473             /* stream index */
2474             case MATROSKA_ID_CUES: {
2475                 if (!matroska->index_parsed) {
2476                     if ((res = ebml_read_master(matroska, &id)) < 0)
2477                         break;
2478                     res = matroska_parse_index(matroska);
2479                 } else
2480                     res = ebml_read_skip(matroska);
2481                 break;
2482             }
2483
2484             /* metadata */
2485             case MATROSKA_ID_TAGS: {
2486                 if (!matroska->metadata_parsed) {
2487                     if ((res = ebml_read_master(matroska, &id)) < 0)
2488                         break;
2489                     res = matroska_parse_metadata(matroska);
2490                 } else
2491                     res = ebml_read_skip(matroska);
2492                 break;
2493             }
2494
2495             /* file index (if seekable, seek to Cues/Tags to parse it) */
2496             case MATROSKA_ID_SEEKHEAD: {
2497                 if ((res = ebml_read_master(matroska, &id)) < 0)
2498                     break;
2499                 res = matroska_parse_seekhead(matroska);
2500                 break;
2501             }
2502
2503             case MATROSKA_ID_ATTACHMENTS: {
2504                 if ((res = ebml_read_master(matroska, &id)) < 0)
2505                     break;
2506                 res = matroska_parse_attachments(s);
2507                 break;
2508             }
2509
2510             case MATROSKA_ID_CLUSTER: {
2511                 /* Do not read the master - this will be done in the next
2512                  * call to matroska_read_packet. */
2513                 res = 1;
2514                 break;
2515             }
2516
2517             case MATROSKA_ID_CHAPTERS: {
2518                 if ((res = ebml_read_master(matroska, &id)) < 0)
2519                     return res;
2520                 res = matroska_parse_chapters(s);
2521                 break;
2522             }
2523
2524             default:
2525                 av_log(matroska->ctx, AV_LOG_INFO,
2526                        "Unknown matroska file header ID 0x%x\n", id);
2527             /* fall-through */
2528
2529             case EBML_ID_VOID:
2530                 res = ebml_read_skip(matroska);
2531                 break;
2532         }
2533
2534         if (matroska->level_up) {
2535             matroska->level_up--;
2536             break;
2537         }
2538     }
2539
2540     /* Have we found a cluster? */
2541     if (ebml_peek_id(matroska, NULL) == MATROSKA_ID_CLUSTER) {
2542         int i, j;
2543         MatroskaTrack *track;
2544         AVStream *st;
2545
2546         for (i = 0; i < matroska->num_tracks; i++) {
2547             enum CodecID codec_id = CODEC_ID_NONE;
2548             uint8_t *extradata = NULL;
2549             int extradata_size = 0;
2550             int extradata_offset = 0;
2551             track = matroska->tracks[i];
2552             track->stream_index = -1;
2553
2554             /* Apply some sanity checks. */
2555             if (track->codec_id == NULL)
2556                 continue;
2557
2558             for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){
2559                 if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id,
2560                             strlen(ff_mkv_codec_tags[j].str))){
2561                     codec_id= ff_mkv_codec_tags[j].id;
2562                     break;
2563                 }
2564             }
2565
2566             /* Set the FourCC from the CodecID. */
2567             /* This is the MS compatibility mode which stores a
2568              * BITMAPINFOHEADER in the CodecPrivate. */
2569             if (!strcmp(track->codec_id,
2570                         MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
2571                 (track->codec_priv_size >= 40) &&
2572                 (track->codec_priv != NULL)) {
2573                 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2574
2575                 /* Offset of biCompression. Stored in LE. */
2576                 vtrack->fourcc = AV_RL32(track->codec_priv + 16);
2577                 codec_id = codec_get_id(codec_bmp_tags, vtrack->fourcc);
2578
2579             }
2580
2581             /* This is the MS compatibility mode which stores a
2582              * WAVEFORMATEX in the CodecPrivate. */
2583             else if (!strcmp(track->codec_id,
2584                              MATROSKA_CODEC_ID_AUDIO_ACM) &&
2585                 (track->codec_priv_size >= 18) &&
2586                 (track->codec_priv != NULL)) {
2587                 uint16_t tag;
2588
2589                 /* Offset of wFormatTag. Stored in LE. */
2590                 tag = AV_RL16(track->codec_priv);
2591                 codec_id = codec_get_id(codec_wav_tags, tag);
2592
2593             }
2594
2595             if (!strcmp(track->codec_id, "V_QUICKTIME") &&
2596                 (track->codec_priv_size >= 86) &&
2597                 (track->codec_priv != NULL)) {
2598                 MatroskaVideoTrack *vtrack = (MatroskaVideoTrack *) track;
2599
2600                 vtrack->fourcc = AV_RL32(track->codec_priv);
2601                 codec_id = codec_get_id(codec_movvideo_tags, vtrack->fourcc);
2602             }
2603
2604             else if (codec_id == CODEC_ID_AAC && !track->codec_priv_size) {
2605                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2606                 int profile = matroska_aac_profile(track->codec_id);
2607                 int sri = matroska_aac_sri(audiotrack->internal_samplerate);
2608                 extradata = av_malloc(5);
2609                 if (extradata == NULL)
2610                     return AVERROR(ENOMEM);
2611                 extradata[0] = (profile << 3) | ((sri&0x0E) >> 1);
2612                 extradata[1] = ((sri&0x01) << 7) | (audiotrack->channels<<3);
2613                 if (strstr(track->codec_id, "SBR")) {
2614                     sri = matroska_aac_sri(audiotrack->samplerate);
2615                     extradata[2] = 0x56;
2616                     extradata[3] = 0xE5;
2617                     extradata[4] = 0x80 | (sri<<3);
2618                     extradata_size = 5;
2619                 } else {
2620                     extradata_size = 2;
2621                 }
2622             }
2623
2624             else if (codec_id == CODEC_ID_TTA) {
2625                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *) track;
2626                 ByteIOContext b;
2627                 extradata_size = 30;
2628                 extradata = av_mallocz(extradata_size);
2629                 if (extradata == NULL)
2630                     return AVERROR(ENOMEM);
2631                 init_put_byte(&b, extradata, extradata_size, 1,
2632                               NULL, NULL, NULL, NULL);
2633                 put_buffer(&b, "TTA1", 4);
2634                 put_le16(&b, 1);
2635                 put_le16(&b, audiotrack->channels);
2636                 put_le16(&b, audiotrack->bitdepth);
2637                 put_le32(&b, audiotrack->samplerate);
2638                 put_le32(&b, matroska->ctx->duration * audiotrack->samplerate);
2639             }
2640
2641             else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 ||
2642                      codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) {
2643                 extradata_offset = 26;
2644                 track->codec_priv_size -= extradata_offset;
2645             }
2646
2647             else if (codec_id == CODEC_ID_RA_144) {
2648                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2649                 audiotrack->samplerate = 8000;
2650                 audiotrack->channels = 1;
2651             }
2652
2653             else if (codec_id == CODEC_ID_RA_288 ||
2654                      codec_id == CODEC_ID_COOK ||
2655                      codec_id == CODEC_ID_ATRAC3) {
2656                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2657                 ByteIOContext b;
2658
2659                 init_put_byte(&b, track->codec_priv, track->codec_priv_size, 0,
2660                               NULL, NULL, NULL, NULL);
2661                 url_fskip(&b, 24);
2662                 audiotrack->coded_framesize = get_be32(&b);
2663                 url_fskip(&b, 12);
2664                 audiotrack->sub_packet_h    = get_be16(&b);
2665                 audiotrack->frame_size      = get_be16(&b);
2666                 audiotrack->sub_packet_size = get_be16(&b);
2667                 audiotrack->buf = av_malloc(audiotrack->frame_size * audiotrack->sub_packet_h);
2668                 if (codec_id == CODEC_ID_RA_288) {
2669                     audiotrack->block_align = audiotrack->coded_framesize;
2670                     track->codec_priv_size = 0;
2671                 } else {
2672                     audiotrack->block_align = audiotrack->sub_packet_size;
2673                     extradata_offset = 78;
2674                     track->codec_priv_size -= extradata_offset;
2675                 }
2676             }
2677
2678             if (codec_id == CODEC_ID_NONE) {
2679                 av_log(matroska->ctx, AV_LOG_INFO,
2680                        "Unknown/unsupported CodecID %s.\n",
2681                        track->codec_id);
2682             }
2683
2684             track->stream_index = matroska->num_streams;
2685
2686             matroska->num_streams++;
2687             st = av_new_stream(s, track->stream_index);
2688             if (st == NULL)
2689                 return AVERROR(ENOMEM);
2690             av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
2691
2692             st->codec->codec_id = codec_id;
2693             st->start_time = 0;
2694             if (strcmp(track->language, "und"))
2695                 av_strlcpy(st->language, track->language, 4);
2696
2697             if (track->flag_default)
2698                 st->disposition |= AV_DISPOSITION_DEFAULT;
2699
2700             if (track->default_duration)
2701                 av_reduce(&st->codec->time_base.num, &st->codec->time_base.den,
2702                           track->default_duration, 1000000000, 30000);
2703
2704             if(extradata){
2705                 st->codec->extradata = extradata;
2706                 st->codec->extradata_size = extradata_size;
2707             } else if(track->codec_priv && track->codec_priv_size > 0){
2708                 st->codec->extradata = av_malloc(track->codec_priv_size);
2709                 if(st->codec->extradata == NULL)
2710                     return AVERROR(ENOMEM);
2711                 st->codec->extradata_size = track->codec_priv_size;
2712                 memcpy(st->codec->extradata,track->codec_priv+extradata_offset,
2713                        track->codec_priv_size);
2714             }
2715
2716             if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
2717                 MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
2718
2719                 st->codec->codec_type = CODEC_TYPE_VIDEO;
2720                 st->codec->codec_tag = videotrack->fourcc;
2721                 st->codec->width = videotrack->pixel_width;
2722                 st->codec->height = videotrack->pixel_height;
2723                 if (videotrack->display_width == 0)
2724                     videotrack->display_width= videotrack->pixel_width;
2725                 if (videotrack->display_height == 0)
2726                     videotrack->display_height= videotrack->pixel_height;
2727                 av_reduce(&st->codec->sample_aspect_ratio.num,
2728                           &st->codec->sample_aspect_ratio.den,
2729                           st->codec->height * videotrack->display_width,
2730                           st->codec-> width * videotrack->display_height,
2731                           255);
2732                 st->need_parsing = AVSTREAM_PARSE_HEADERS;
2733             } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
2734                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
2735
2736                 st->codec->codec_type = CODEC_TYPE_AUDIO;
2737                 st->codec->sample_rate = audiotrack->samplerate;
2738                 st->codec->channels = audiotrack->channels;
2739                 st->codec->block_align = audiotrack->block_align;
2740             } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
2741                 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
2742             }
2743
2744             /* What do we do with private data? E.g. for Vorbis. */
2745         }
2746         res = 0;
2747     }
2748
2749     if (matroska->index_parsed) {
2750         int i, track, stream;
2751         for (i=0; i<matroska->num_indexes; i++) {
2752             MatroskaDemuxIndex *idx = &matroska->index[i];
2753             track = matroska_find_track_by_num(matroska, idx->track);
2754             if (track < 0)  continue;
2755             stream = matroska->tracks[track]->stream_index;
2756             if (stream >= 0 && stream < matroska->ctx->nb_streams)
2757                 av_add_index_entry(matroska->ctx->streams[stream],
2758                                    idx->pos, idx->time/AV_TIME_BASE,
2759                                    0, 0, AVINDEX_KEYFRAME);
2760         }
2761     }
2762
2763     return res;
2764 }
2765
2766 static int
2767 matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, int size,
2768                      int64_t pos, uint64_t cluster_time, uint64_t duration,
2769                      int is_keyframe)
2770 {
2771     int res = 0;
2772     int track;
2773     AVStream *st;
2774     AVPacket *pkt;
2775     uint8_t *origdata = data;
2776     int16_t block_time;
2777     uint32_t *lace_size = NULL;
2778     int n, flags, laces = 0;
2779     uint64_t num;
2780     int stream_index;
2781
2782     /* first byte(s): tracknum */
2783     if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
2784         av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n");
2785         av_free(origdata);
2786         return res;
2787     }
2788     data += n;
2789     size -= n;
2790
2791     /* fetch track from num */
2792     track = matroska_find_track_by_num(matroska, num);
2793     if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
2794         av_log(matroska->ctx, AV_LOG_INFO,
2795                "Invalid stream %d or size %u\n", track, size);
2796         av_free(origdata);
2797         return res;
2798     }
2799     stream_index = matroska->tracks[track]->stream_index;
2800     if (stream_index < 0 || stream_index >= matroska->ctx->nb_streams) {
2801         av_free(origdata);
2802         return res;
2803     }
2804     st = matroska->ctx->streams[stream_index];
2805     if (st->discard >= AVDISCARD_ALL) {
2806         av_free(origdata);
2807         return res;
2808     }
2809     if (duration == AV_NOPTS_VALUE)
2810         duration = matroska->tracks[track]->default_duration / matroska->time_scale;
2811
2812     /* block_time (relative to cluster time) */
2813     block_time = AV_RB16(data);
2814     data += 2;
2815     flags = *data++;
2816     size -= 3;
2817     if (is_keyframe == -1)
2818         is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0;
2819
2820     if (matroska->skip_to_keyframe) {
2821         if (!is_keyframe || st != matroska->skip_to_stream) {
2822             av_free(origdata);
2823             return res;
2824         }
2825         matroska->skip_to_keyframe = 0;
2826     }
2827
2828     switch ((flags & 0x06) >> 1) {
2829         case 0x0: /* no lacing */
2830             laces = 1;
2831             lace_size = av_mallocz(sizeof(int));
2832             lace_size[0] = size;
2833             break;
2834
2835         case 0x1: /* xiph lacing */
2836         case 0x2: /* fixed-size lacing */
2837         case 0x3: /* EBML lacing */
2838             assert(size>0); // size <=3 is checked before size-=3 above
2839             laces = (*data) + 1;
2840             data += 1;
2841             size -= 1;
2842             lace_size = av_mallocz(laces * sizeof(int));
2843
2844             switch ((flags & 0x06) >> 1) {
2845                 case 0x1: /* xiph lacing */ {
2846                     uint8_t temp;
2847                     uint32_t total = 0;
2848                     for (n = 0; res == 0 && n < laces - 1; n++) {
2849                         while (1) {
2850                             if (size == 0) {
2851                                 res = -1;
2852                                 break;
2853                             }
2854                             temp = *data;
2855                             lace_size[n] += temp;
2856                             data += 1;
2857                             size -= 1;
2858                             if (temp != 0xff)
2859                                 break;
2860                         }
2861                         total += lace_size[n];
2862                     }
2863                     lace_size[n] = size - total;
2864                     break;
2865                 }
2866
2867                 case 0x2: /* fixed-size lacing */
2868                     for (n = 0; n < laces; n++)
2869                         lace_size[n] = size / laces;
2870                     break;
2871
2872                 case 0x3: /* EBML lacing */ {
2873                     uint32_t total;
2874                     n = matroska_ebmlnum_uint(data, size, &num);
2875                     if (n < 0) {
2876                         av_log(matroska->ctx, AV_LOG_INFO,
2877                                "EBML block data error\n");
2878                         break;
2879                     }
2880                     data += n;
2881                     size -= n;
2882                     total = lace_size[0] = num;
2883                     for (n = 1; res == 0 && n < laces - 1; n++) {
2884                         int64_t snum;
2885                         int r;
2886                         r = matroska_ebmlnum_sint (data, size, &snum);
2887                         if (r < 0) {
2888                             av_log(matroska->ctx, AV_LOG_INFO,
2889                                    "EBML block data error\n");
2890                             break;
2891                         }
2892                         data += r;
2893                         size -= r;
2894                         lace_size[n] = lace_size[n - 1] + snum;
2895                         total += lace_size[n];
2896                     }
2897                     lace_size[n] = size - total;
2898                     break;
2899                 }
2900             }
2901             break;
2902     }
2903
2904     if (res == 0) {
2905         uint64_t timecode = AV_NOPTS_VALUE;
2906
2907         if (cluster_time != (uint64_t)-1
2908             && (block_time >= 0 || cluster_time >= -block_time))
2909             timecode = cluster_time + block_time;
2910
2911         for (n = 0; n < laces; n++) {
2912             if (st->codec->codec_id == CODEC_ID_RA_288 ||
2913                 st->codec->codec_id == CODEC_ID_COOK ||
2914                 st->codec->codec_id == CODEC_ID_ATRAC3) {
2915                 MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)matroska->tracks[track];
2916                 int a = st->codec->block_align;
2917                 int sps = audiotrack->sub_packet_size;
2918                 int cfs = audiotrack->coded_framesize;
2919                 int h = audiotrack->sub_packet_h;
2920                 int y = audiotrack->sub_packet_cnt;
2921                 int w = audiotrack->frame_size;
2922                 int x;
2923
2924                 if (!audiotrack->pkt_cnt) {
2925                     if (st->codec->codec_id == CODEC_ID_RA_288)
2926                         for (x=0; x<h/2; x++)
2927                             memcpy(audiotrack->buf+x*2*w+y*cfs,
2928                                    data+x*cfs, cfs);
2929                     else
2930                         for (x=0; x<w/sps; x++)
2931                             memcpy(audiotrack->buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps);
2932
2933                     if (++audiotrack->sub_packet_cnt >= h) {
2934                         audiotrack->sub_packet_cnt = 0;
2935                         audiotrack->pkt_cnt = h*w / a;
2936                     }
2937                 }
2938                 while (audiotrack->pkt_cnt) {
2939                     pkt = av_mallocz(sizeof(AVPacket));
2940                     av_new_packet(pkt, a);
2941                     memcpy(pkt->data, audiotrack->buf
2942                            + a * (h*w / a - audiotrack->pkt_cnt--), a);
2943                     pkt->pos = pos;
2944                     pkt->stream_index = stream_index;
2945                     matroska_queue_packet(matroska, pkt);
2946                 }
2947             } else {
2948                 int offset = 0, pkt_size = lace_size[n];
2949                 uint8_t *pkt_data = data;
2950
2951                 if (matroska->tracks[track]->encoding_scope & 1) {
2952                     offset = matroska_decode_buffer(&pkt_data, &pkt_size,
2953                                                     matroska->tracks[track]);
2954                     if (offset < 0)
2955                         continue;
2956                 }
2957
2958                 pkt = av_mallocz(sizeof(AVPacket));
2959                 /* XXX: prevent data copy... */
2960                 if (av_new_packet(pkt, pkt_size+offset) < 0) {
2961                     av_free(pkt);
2962                     res = AVERROR(ENOMEM);
2963                     n = laces-1;
2964                     break;
2965                 }
2966                 if (offset)
2967                     memcpy (pkt->data, matroska->tracks[track]->encoding_settings, offset);
2968                 memcpy (pkt->data+offset, pkt_data, pkt_size);
2969
2970                 if (pkt_data != data)
2971                     av_free(pkt_data);
2972
2973                 if (n == 0)
2974                     pkt->flags = is_keyframe;
2975                 pkt->stream_index = stream_index;
2976
2977                 pkt->pts = timecode;
2978                 pkt->pos = pos;
2979                 pkt->duration = duration;
2980
2981                 matroska_queue_packet(matroska, pkt);
2982             }
2983
2984             if (timecode != AV_NOPTS_VALUE)
2985                 timecode = duration ? timecode + duration : AV_NOPTS_VALUE;
2986             data += lace_size[n];
2987         }
2988     }
2989
2990     av_free(lace_size);
2991     av_free(origdata);
2992     return res;
2993 }
2994
2995 static int
2996 matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
2997                            uint64_t              cluster_time)
2998 {
2999     int res = 0;
3000     uint32_t id;
3001     int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
3002     uint64_t duration = AV_NOPTS_VALUE;
3003     uint8_t *data;
3004     int size = 0;
3005     int64_t pos = 0;
3006
3007     av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
3008
3009     while (res == 0) {
3010         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3011             res = AVERROR(EIO);
3012             break;
3013         } else if (matroska->level_up) {
3014             matroska->level_up--;
3015             break;
3016         }
3017
3018         switch (id) {
3019             /* one block inside the group. Note, block parsing is one
3020              * of the harder things, so this code is a bit complicated.
3021              * See http://www.matroska.org/ for documentation. */
3022             case MATROSKA_ID_BLOCK: {
3023                 pos = url_ftell(matroska->ctx->pb);
3024                 res = ebml_read_binary(matroska, &id, &data, &size);
3025                 break;
3026             }
3027
3028             case MATROSKA_ID_BLOCKDURATION: {
3029                 if ((res = ebml_read_uint(matroska, &id, &duration)) < 0)
3030                     break;
3031                 break;
3032             }
3033
3034             case MATROSKA_ID_BLOCKREFERENCE: {
3035                 int64_t num;
3036                 /* We've found a reference, so not even the first frame in
3037                  * the lace is a key frame. */
3038                 is_keyframe = 0;
3039                 if (last_num_packets != matroska->num_packets)
3040                     matroska->packets[last_num_packets]->flags = 0;
3041                 if ((res = ebml_read_sint(matroska, &id, &num)) < 0)
3042                     break;
3043                 break;
3044             }
3045
3046             default:
3047                 av_log(matroska->ctx, AV_LOG_INFO,
3048                        "Unknown entry 0x%x in blockgroup data\n", id);
3049                 /* fall-through */
3050
3051             case EBML_ID_VOID:
3052                 res = ebml_read_skip(matroska);
3053                 break;
3054         }
3055
3056         if (matroska->level_up) {
3057             matroska->level_up--;
3058             break;
3059         }
3060     }
3061
3062     if (res)
3063         return res;
3064
3065     if (size > 0)
3066         res = matroska_parse_block(matroska, data, size, pos, cluster_time,
3067                                    duration, is_keyframe);
3068
3069     return res;
3070 }
3071
3072 static int
3073 matroska_parse_cluster (MatroskaDemuxContext *matroska)
3074 {
3075     int res = 0;
3076     uint32_t id;
3077     uint64_t cluster_time = 0;
3078     uint8_t *data;
3079     int64_t pos;
3080     int size;
3081
3082     av_log(matroska->ctx, AV_LOG_DEBUG,
3083            "parsing cluster at %"PRId64"\n", url_ftell(matroska->ctx->pb));
3084
3085     while (res == 0) {
3086         if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3087             res = AVERROR(EIO);
3088             break;
3089         } else if (matroska->level_up) {
3090             matroska->level_up--;
3091             break;
3092         }
3093
3094         switch (id) {
3095             /* cluster timecode */
3096             case MATROSKA_ID_CLUSTERTIMECODE: {
3097                 uint64_t num;
3098                 if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
3099                     break;
3100                 cluster_time = num;
3101                 break;
3102             }
3103
3104                 /* a group of blocks inside a cluster */
3105             case MATROSKA_ID_BLOCKGROUP:
3106                 if ((res = ebml_read_master(matroska, &id)) < 0)
3107                     break;
3108                 res = matroska_parse_blockgroup(matroska, cluster_time);
3109                 break;
3110
3111             case MATROSKA_ID_SIMPLEBLOCK:
3112                 pos = url_ftell(matroska->ctx->pb);
3113                 res = ebml_read_binary(matroska, &id, &data, &size);
3114                 if (res == 0)
3115                     res = matroska_parse_block(matroska, data, size, pos,
3116                                                cluster_time, AV_NOPTS_VALUE,
3117                                                -1);
3118                 break;
3119
3120             default:
3121                 av_log(matroska->ctx, AV_LOG_INFO,
3122                        "Unknown entry 0x%x in cluster data\n", id);
3123                 /* fall-through */
3124
3125             case EBML_ID_VOID:
3126                 res = ebml_read_skip(matroska);
3127                 break;
3128         }
3129
3130         if (matroska->level_up) {
3131             matroska->level_up--;
3132             break;
3133         }
3134     }
3135
3136     return res;
3137 }
3138
3139 static int
3140 matroska_read_packet (AVFormatContext *s,
3141                       AVPacket        *pkt)
3142 {
3143     MatroskaDemuxContext *matroska = s->priv_data;
3144     int res;
3145     uint32_t id;
3146
3147     /* Read stream until we have a packet queued. */
3148     while (matroska_deliver_packet(matroska, pkt)) {
3149
3150         /* Have we already reached the end? */
3151         if (matroska->done)
3152             return AVERROR(EIO);
3153
3154         res = 0;
3155         while (res == 0) {
3156             if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
3157                 return AVERROR(EIO);
3158             } else if (matroska->level_up) {
3159                 matroska->level_up--;
3160                 break;
3161             }
3162
3163             switch (id) {
3164                 case MATROSKA_ID_CLUSTER:
3165                     if ((res = ebml_read_master(matroska, &id)) < 0)
3166                         break;
3167                     if ((res = matroska_parse_cluster(matroska)) == 0)
3168                         res = 1; /* Parsed one cluster, let's get out. */
3169                     break;
3170
3171                 default:
3172                 case EBML_ID_VOID:
3173                     res = ebml_read_skip(matroska);
3174                     break;
3175             }
3176
3177             if (matroska->level_up) {
3178                 matroska->level_up--;
3179                 break;
3180             }
3181         }
3182
3183         if (res == -1)
3184             matroska->done = 1;
3185     }
3186
3187     return 0;
3188 }
3189
3190 static int
3191 matroska_read_seek (AVFormatContext *s, int stream_index, int64_t timestamp,
3192                     int flags)
3193 {
3194     MatroskaDemuxContext *matroska = s->priv_data;
3195     AVStream *st = s->streams[stream_index];
3196     int index;
3197
3198     /* find index entry */
3199     index = av_index_search_timestamp(st, timestamp, flags);
3200     if (index < 0)
3201         return 0;
3202
3203     matroska_clear_queue(matroska);
3204
3205     /* do the seek */
3206     url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
3207     matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY);
3208     matroska->skip_to_stream = st;
3209     matroska->peek_id = 0;
3210     av_update_cur_dts(s, st, st->index_entries[index].timestamp);
3211     return 0;
3212 }
3213
3214 static int
3215 matroska_read_close (AVFormatContext *s)
3216 {
3217     MatroskaDemuxContext *matroska = s->priv_data;
3218     int n = 0;
3219
3220     av_free(matroska->index);
3221
3222     matroska_clear_queue(matroska);
3223
3224     for (n = 0; n < matroska->num_tracks; n++) {
3225         MatroskaTrack *track = matroska->tracks[n];
3226         av_free(track->codec_id);
3227         av_free(track->codec_priv);
3228         av_free(track->name);
3229
3230         if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
3231             MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
3232             av_free(audiotrack->buf);
3233         }
3234
3235         av_free(track);
3236     }
3237
3238     return 0;
3239 }
3240
3241 AVInputFormat matroska_demuxer = {
3242     "matroska",
3243     NULL_IF_CONFIG_SMALL("Matroska file format"),
3244     sizeof(MatroskaDemuxContext),
3245     matroska_probe,
3246     matroska_read_header,
3247     matroska_read_packet,
3248     matroska_read_close,
3249     matroska_read_seek,
3250 };