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