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