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