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