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