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