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