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