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