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