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