Update to MPlayer SVN rev 30134 and FFmpeg SVN rev 20947.
[vaapi:mplayer.git] / libmpdemux / demux_mkv.c
1 /*
2  * Matroska demuxer
3  * Copyright (C) 2004 Aurelien Jacobs <aurel@gnuage.org>
4  * Based on the one written by Ronald Bultje for gstreamer
5  * and on demux_mkv.cpp from Moritz Bunkus.
6  *
7  * This file is part of MPlayer.
8  *
9  * MPlayer is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * MPlayer is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <inttypes.h>
30
31 #include "stream/stream.h"
32 #include "demuxer.h"
33 #include "stheader.h"
34 #include "ebml.h"
35 #include "matroska.h"
36
37 #include "mp_msg.h"
38 #include "help_mp.h"
39
40 #include "vobsub.h"
41 #include "subreader.h"
42 #include "libvo/sub.h"
43
44 #include "libass/ass_mp.h"
45
46 #include "libavutil/common.h"
47
48 #ifdef CONFIG_QTX_CODECS
49 #include "loader/qtx/qtxsdk/components.h"
50 #endif
51
52 #if CONFIG_ZLIB
53 #include <zlib.h>
54 #endif
55
56 #include "libavutil/lzo.h"
57 #include "libavutil/intreadwrite.h"
58 #include "libavutil/avstring.h"
59
60 static const unsigned char sipr_swaps[38][2]={
61     {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
62     {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
63     {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
64     {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
65     {77,80} };
66
67 // Map flavour to bytes per second
68 #define SIPR_FLAVORS 4
69 #define ATRC_FLAVORS 8
70 #define COOK_FLAVORS 34
71 static const int sipr_fl2bps[SIPR_FLAVORS] = {813, 1062, 625, 2000};
72 static const int atrc_fl2bps[ATRC_FLAVORS] =
73     {8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100};
74 static const int cook_fl2bps[COOK_FLAVORS] =
75     { 1000,  1378,  2024,  2584, 4005,  5513, 8010, 4005,   750, 2498,
76       4048,  5513,  8010, 11973, 8010,  2584, 4005, 2067,  2584, 2584,
77       4005,  4005,  5513,  5513, 8010, 12059, 1550, 8010, 12059, 5513,
78      12016, 16408, 22911, 33506};
79
80 typedef struct
81 {
82   uint32_t order, type, scope;
83   uint32_t comp_algo;
84   uint8_t *comp_settings;
85   int comp_settings_len;
86 } mkv_content_encoding_t;
87
88 typedef struct mkv_track
89 {
90   int tnum;
91   char *name;
92
93   char *codec_id;
94   int ms_compat;
95   char *language;
96
97   int type;
98
99   uint32_t v_width, v_height, v_dwidth, v_dheight;
100   float v_frate;
101
102   uint32_t a_formattag;
103   uint32_t a_channels, a_bps;
104   float a_sfreq;
105
106   float default_duration;
107
108   int default_track;
109
110   void *private_data;
111   unsigned int private_size;
112
113   /* stuff for realmedia */
114   int realmedia;
115   int64_t rv_kf_base;
116   int rv_kf_pts;
117   float rv_pts;  /* previous video timestamp */
118   float ra_pts;  /* previous audio timestamp */
119
120   /** realaudio descrambling */
121   int sub_packet_size; ///< sub packet size, per stream
122   int sub_packet_h; ///< number of coded frames per block
123   int coded_framesize; ///< coded frame size, per stream
124   int audiopk_size; ///< audio packet size
125   unsigned char *audio_buf; ///< place to store reordered audio data
126   float *audio_timestamp; ///< timestamp for each audio packet
127   int sub_packet_cnt; ///< number of subpacket already received
128   int audio_filepos; ///< file position of first audio packet in block
129
130   /* stuff for quicktime */
131   int fix_i_bps;
132   float qt_last_a_pts;
133
134   int subtitle_type;
135
136   /* The timecodes of video frames might have to be reordered if they're
137      in display order (the timecodes, not the frames themselves!). In this
138      case demux packets have to be cached with the help of these variables. */
139   int reorder_timecodes;
140   demux_packet_t **cached_dps;
141   int num_cached_dps, num_allocated_dps;
142   float max_pts;
143
144   /* generic content encoding support */
145   mkv_content_encoding_t *encodings;
146   int num_encodings;
147
148   /* For VobSubs and SSA/ASS */
149   sh_sub_t *sh_sub;
150 } mkv_track_t;
151
152 typedef struct mkv_index
153 {
154   int tnum;
155   uint64_t timecode, filepos;
156 } mkv_index_t;
157
158 typedef struct mkv_demuxer
159 {
160   off_t segment_start;
161
162   float duration, last_pts;
163   uint64_t last_filepos;
164
165   mkv_track_t **tracks;
166   int num_tracks;
167
168   uint64_t tc_scale, cluster_tc, first_tc;
169   int has_first_tc;
170
171   uint64_t cluster_size;
172   uint64_t blockgroup_size;
173
174   mkv_index_t *indexes;
175   int num_indexes;
176
177   off_t *parsed_cues;
178   int parsed_cues_num;
179   off_t *parsed_seekhead;
180   int parsed_seekhead_num;
181
182   uint64_t *cluster_positions;
183   int num_cluster_pos;
184
185   int64_t skip_to_timecode;
186   int v_skip_to_keyframe, a_skip_to_keyframe;
187
188   int64_t stop_timecode;
189
190   int last_aid;
191   int audio_tracks[MAX_A_STREAMS];
192 } mkv_demuxer_t;
193
194 #define REALHEADER_SIZE    16
195 #define RVPROPERTIES_SIZE  34
196 #define RAPROPERTIES4_SIZE 56
197 #define RAPROPERTIES5_SIZE 70
198
199 /* for e.g. "-slang ger" */
200 extern char *dvdsub_lang;
201 extern char *audio_lang;
202 extern int dvdsub_id;
203
204 /**
205  * \brief ensures there is space for at least one additional element
206  * \param arrayp array to grow
207  * \param nelem current number of elements in array
208  * \param elsize size of one array element
209  */
210 static void av_noinline grow_array(void *arrayp, int nelem, size_t elsize) {
211   void **array = arrayp;
212   void *oldp = *array;
213   if (nelem & 31)
214     return;
215   if (nelem > UINT_MAX / elsize - 32)
216     *array = NULL;
217   else
218     *array = realloc(*array, (nelem + 32) * elsize);
219   if (!*array)
220     free(oldp);
221 }
222
223 static mkv_track_t *
224 demux_mkv_find_track_by_num (mkv_demuxer_t *d, int n, int type)
225 {
226   int i, id;
227
228   for (i=0, id=0; i < d->num_tracks; i++)
229     if (d->tracks[i] != NULL && d->tracks[i]->type == type)
230       if (id++ == n)
231         return d->tracks[i];
232
233   return NULL;
234 }
235
236 static void
237 add_cluster_position (mkv_demuxer_t *mkv_d, uint64_t position)
238 {
239   int i = mkv_d->num_cluster_pos;
240
241   while (i--)
242     if (mkv_d->cluster_positions[i] == position)
243       return;
244
245   grow_array(&mkv_d->cluster_positions, mkv_d->num_cluster_pos,
246              sizeof(uint64_t));
247   if (!mkv_d->cluster_positions) {
248     mkv_d->num_cluster_pos = 0;
249     return;
250   }
251   mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = position;
252 }
253
254
255 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
256 static int
257 aac_get_sample_rate_index (uint32_t sample_rate)
258 {
259   static const int srates[] = {92017, 75132, 55426, 46009, 37566, 27713, 23004, 18783, 13856, 11502, 9391, 0};
260   int i = 0;
261   while (sample_rate < srates[i]) i++;
262   return i;
263 }
264
265 /** \brief Free cached demux packets
266  *
267  * Reordering the timecodes requires caching of demux packets. This function
268  * frees all these cached packets and the memory for the cached pointers
269  * itself.
270  *
271  * \param demuxer The demuxer for which the cache is to be freed.
272  */
273 static void
274 free_cached_dps (demuxer_t *demuxer)
275 {
276   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
277   mkv_track_t *track;
278   int i, k;
279
280   for (k = 0; k < mkv_d->num_tracks; k++)
281     {
282       track = mkv_d->tracks[k];
283       for (i = 0; i < track->num_cached_dps; i++)
284         free_demux_packet (track->cached_dps[i]);
285       free(track->cached_dps);
286       track->cached_dps = NULL;
287       track->num_cached_dps = 0;
288       track->num_allocated_dps = 0;
289       track->max_pts = 0;
290     }
291 }
292
293 static int
294 demux_mkv_decode (mkv_track_t *track, uint8_t *src, uint8_t **dest,
295                   uint32_t *size, uint32_t type)
296 {
297   int i, result;
298   int modified = 0;
299
300   *dest = src;
301   if (track->num_encodings <= 0)
302     return 0;
303
304   for (i=0; i<track->num_encodings; i++)
305     {
306       if (!(track->encodings[i].scope & type))
307         continue;
308
309 #if CONFIG_ZLIB
310       if (track->encodings[i].comp_algo == 0)
311         {
312           /* zlib encoded track */
313           z_stream zstream;
314
315           zstream.zalloc = (alloc_func) 0;
316           zstream.zfree = (free_func) 0;
317           zstream.opaque = (voidpf) 0;
318           if (inflateInit (&zstream) != Z_OK)
319             {
320               mp_msg (MSGT_DEMUX, MSGL_WARN,
321                       MSGTR_MPDEMUX_MKV_ZlibInitializationFailed);
322               return modified;
323             }
324           zstream.next_in = (Bytef *) src;
325           zstream.avail_in = *size;
326
327           modified = 1;
328           *dest = NULL;
329           zstream.avail_out = *size;
330           do {
331             *size += 4000;
332             *dest = realloc (*dest, *size);
333             zstream.next_out = (Bytef *) (*dest + zstream.total_out);
334             result = inflate (&zstream, Z_NO_FLUSH);
335             if (result != Z_OK && result != Z_STREAM_END)
336               {
337                 mp_msg (MSGT_DEMUX, MSGL_WARN,
338                         MSGTR_MPDEMUX_MKV_ZlibDecompressionFailed);
339                 free(*dest);
340                 *dest = NULL;
341                 inflateEnd (&zstream);
342                 return modified;
343               }
344             zstream.avail_out += 4000;
345           } while (zstream.avail_out == 4000 &&
346                    zstream.avail_in != 0 && result != Z_STREAM_END);
347
348           *size = zstream.total_out;
349           inflateEnd (&zstream);
350         }
351 #endif
352       if (track->encodings[i].comp_algo == 2)
353         {
354           /* lzo encoded track */
355           int dstlen = *size * 3;
356
357           *dest = NULL;
358           while (1)
359             {
360               int srclen = *size;
361               if (dstlen > SIZE_MAX - AV_LZO_OUTPUT_PADDING) goto lzo_fail;
362               *dest = realloc (*dest, dstlen + AV_LZO_OUTPUT_PADDING);
363               result = av_lzo1x_decode (*dest, &dstlen, src, &srclen);
364               if (result == 0)
365                 break;
366               if (!(result & AV_LZO_OUTPUT_FULL))
367                 {
368 lzo_fail:
369                   mp_msg (MSGT_DEMUX, MSGL_WARN,
370                           MSGTR_MPDEMUX_MKV_LzoDecompressionFailed);
371                   free(*dest);
372                   *dest = NULL;
373                   return modified;
374                 }
375               mp_msg (MSGT_DEMUX, MSGL_DBG2,
376                       "[mkv] lzo decompression buffer too small.\n");
377               dstlen *= 2;
378             }
379           *size = dstlen;
380         }
381     }
382
383   return modified;
384 }
385
386
387 static int
388 demux_mkv_read_info (demuxer_t *demuxer)
389 {
390   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
391   stream_t *s = demuxer->stream;
392   uint64_t length, l;
393   int il;
394   uint64_t tc_scale = 1000000;
395   long double duration = 0.;
396
397   length = ebml_read_length (s, NULL);
398   while (length > 0)
399     {
400       switch (ebml_read_id (s, &il))
401         {
402         case MATROSKA_ID_TIMECODESCALE:
403           {
404             uint64_t num = ebml_read_uint (s, &l);
405             if (num == EBML_UINT_INVALID)
406               return 1;
407             tc_scale = num;
408             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + timecode scale: %"PRIu64"\n",
409                     tc_scale);
410             break;
411           }
412
413         case MATROSKA_ID_DURATION:
414           {
415             long double num = ebml_read_float (s, &l);
416             if (num == EBML_FLOAT_INVALID)
417               return 1;
418             duration = num;
419             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + duration: %.3Lfs\n",
420                     duration * tc_scale / 1000000000.0);
421             break;
422           }
423
424         default:
425           ebml_read_skip (s, &l);
426           break;
427         }
428       length -= l + il;
429     }
430   mkv_d->tc_scale = tc_scale;
431   mkv_d->duration = duration * tc_scale / 1000000000.0;
432   return 0;
433 }
434
435 /**
436  * \brief free array of kv_content_encoding_t
437  * \param encodings pointer to array
438  * \param numencodings number of encodings in array
439  */
440 static void
441 demux_mkv_free_encodings(mkv_content_encoding_t *encodings, int numencodings)
442 {
443   while (numencodings-- > 0)
444     free(encodings[numencodings].comp_settings);
445   free(encodings);
446 }
447
448 static int
449 demux_mkv_read_trackencodings (demuxer_t *demuxer, mkv_track_t *track)
450 {
451   stream_t *s = demuxer->stream;
452   mkv_content_encoding_t *ce, e;
453   uint64_t len, length, l;
454   int il, n;
455
456   ce = malloc (sizeof (*ce));
457   n = 0;
458
459   len = length = ebml_read_length (s, &il);
460   len += il;
461   while (length > 0)
462     {
463       switch (ebml_read_id (s, &il))
464         {
465         case MATROSKA_ID_CONTENTENCODING:
466           {
467             uint64_t len;
468             int i;
469
470             memset (&e, 0, sizeof (e));
471             e.scope = 1;
472
473             len = ebml_read_length (s, &i);
474             l = len + i;
475
476             while (len > 0)
477               {
478                 uint64_t num, l;
479                 int il;
480
481                 switch (ebml_read_id (s, &il))
482                   {
483                   case MATROSKA_ID_CONTENTENCODINGORDER:
484                     num = ebml_read_uint (s, &l);
485                     if (num == EBML_UINT_INVALID)
486                       goto err_out;
487                     e.order = num;
488                     break;
489
490                   case MATROSKA_ID_CONTENTENCODINGSCOPE:
491                     num = ebml_read_uint (s, &l);
492                     if (num == EBML_UINT_INVALID)
493                       goto err_out;
494                     e.scope = num;
495                     break;
496
497                   case MATROSKA_ID_CONTENTENCODINGTYPE:
498                     num = ebml_read_uint (s, &l);
499                     if (num == EBML_UINT_INVALID)
500                       goto err_out;
501                     e.type = num;
502                     break;
503
504                   case MATROSKA_ID_CONTENTCOMPRESSION:
505                     {
506                       uint64_t le;
507
508                       le = ebml_read_length (s, &i);
509                       l = le + i;
510
511                       while (le > 0)
512                         {
513                           uint64_t l;
514                           int il;
515
516                           switch (ebml_read_id (s, &il))
517                             {
518                             case MATROSKA_ID_CONTENTCOMPALGO:
519                               num = ebml_read_uint (s, &l);
520                               if (num == EBML_UINT_INVALID)
521                                 goto err_out;
522                               e.comp_algo = num;
523                               break;
524
525                             case MATROSKA_ID_CONTENTCOMPSETTINGS:
526                               l = ebml_read_length (s, &i);
527                               e.comp_settings = malloc (l);
528                               stream_read (s, e.comp_settings, l);
529                               e.comp_settings_len = l;
530                               l += i;
531                               break;
532
533                             default:
534                               ebml_read_skip (s, &l);
535                               break;
536                             }
537                           le -= l + il;
538                         }
539
540                       if (e.type == 1)
541                         {
542                           mp_msg(MSGT_DEMUX, MSGL_WARN,
543                                  MSGTR_MPDEMUX_MKV_TrackEncrypted, track->tnum);
544                         }
545                       else if (e.type != 0)
546                         {
547                           mp_msg(MSGT_DEMUX, MSGL_WARN,
548                                  MSGTR_MPDEMUX_MKV_UnknownContentEncoding, track->tnum);
549                         }
550
551                       if (e.comp_algo != 0 && e.comp_algo != 2)
552                         {
553                           mp_msg (MSGT_DEMUX, MSGL_WARN,
554                                   MSGTR_MPDEMUX_MKV_UnknownCompression,
555                                   track->tnum, e.comp_algo);
556                         }
557 #if !CONFIG_ZLIB
558                       else if (e.comp_algo == 0)
559                         {
560                           mp_msg (MSGT_DEMUX, MSGL_WARN,
561                                   MSGTR_MPDEMUX_MKV_ZlibCompressionUnsupported,
562                                   track->tnum);
563                         }
564 #endif
565
566                       break;
567                     }
568
569                   default:
570                     ebml_read_skip (s, &l);
571                     break;
572                   }
573                 len -= l + il;
574               }
575             for (i=0; i<n; i++)
576               if (e.order <= ce[i].order)
577                 break;
578             ce = realloc (ce, (n+1) *sizeof (*ce));
579             memmove (ce+i+1, ce+i, (n-i) * sizeof (*ce));
580             memcpy (ce+i, &e, sizeof (e));
581             n++;
582             break;
583           }
584
585         default:
586           ebml_read_skip (s, &l);
587           break;
588         }
589
590       length -= l + il;
591     }
592
593   track->encodings = ce;
594   track->num_encodings = n;
595   return len;
596
597 err_out:
598   demux_mkv_free_encodings(ce, n);
599   return 0;
600 }
601
602 static int
603 demux_mkv_read_trackaudio (demuxer_t *demuxer, mkv_track_t *track)
604 {
605   stream_t *s = demuxer->stream;
606   uint64_t len, length, l;
607   int il;
608
609   track->a_sfreq = 8000.0;
610   track->a_channels = 1;
611
612   len = length = ebml_read_length (s, &il);
613   len += il;
614   while (length > 0)
615     {
616       switch (ebml_read_id (s, &il))
617         {
618         case MATROSKA_ID_AUDIOSAMPLINGFREQ:
619           {
620             long double num = ebml_read_float (s, &l);
621             if (num == EBML_FLOAT_INVALID)
622               return 0;
623             track->a_sfreq = num;
624             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Sampling frequency: %f\n",
625                     track->a_sfreq);
626             break;
627           }
628
629         case MATROSKA_ID_AUDIOBITDEPTH:
630           {
631             uint64_t num = ebml_read_uint (s, &l);
632             if (num == EBML_UINT_INVALID)
633               return 0;
634             track->a_bps = num;
635             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Bit depth: %u\n",
636                     track->a_bps);
637             break;
638           }
639
640         case MATROSKA_ID_AUDIOCHANNELS:
641           {
642             uint64_t num = ebml_read_uint (s, &l);
643             if (num == EBML_UINT_INVALID)
644               return 0;
645             track->a_channels = num;
646             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Channels: %u\n",
647                     track->a_channels);
648             break;
649           }
650
651         default:
652             ebml_read_skip (s, &l);
653             break;
654         }
655       length -= l + il;
656     }
657   return len;
658 }
659
660 static int
661 demux_mkv_read_trackvideo (demuxer_t *demuxer, mkv_track_t *track)
662 {
663   stream_t *s = demuxer->stream;
664   uint64_t len, length, l;
665   int il;
666
667   len = length = ebml_read_length (s, &il);
668   len += il;
669   while (length > 0)
670     {
671       switch (ebml_read_id (s, &il))
672         {
673         case MATROSKA_ID_VIDEOFRAMERATE:
674           {
675             long double num = ebml_read_float (s, &l);
676             if (num == EBML_FLOAT_INVALID)
677               return 0;
678             track->v_frate = num;
679             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Frame rate: %f\n",
680                     track->v_frate);
681             if (track->v_frate > 0)
682               track->default_duration = 1 / track->v_frate;
683             break;
684           }
685
686         case MATROSKA_ID_VIDEODISPLAYWIDTH:
687           {
688             uint64_t num = ebml_read_uint (s, &l);
689             if (num == EBML_UINT_INVALID)
690               return 0;
691             track->v_dwidth = num;
692             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Display width: %u\n",
693                     track->v_dwidth);
694             break;
695           }
696
697         case MATROSKA_ID_VIDEODISPLAYHEIGHT:
698           {
699             uint64_t num = ebml_read_uint (s, &l);
700             if (num == EBML_UINT_INVALID)
701               return 0;
702             track->v_dheight = num;
703             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Display height: %u\n",
704                     track->v_dheight);
705             break;
706           }
707
708         case MATROSKA_ID_VIDEOPIXELWIDTH:
709           {
710             uint64_t num = ebml_read_uint (s, &l);
711             if (num == EBML_UINT_INVALID)
712               return 0;
713             track->v_width = num;
714             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Pixel width: %u\n",
715                     track->v_width);
716             break;
717           }
718
719         case MATROSKA_ID_VIDEOPIXELHEIGHT:
720           {
721             uint64_t num = ebml_read_uint (s, &l);
722             if (num == EBML_UINT_INVALID)
723               return 0;
724             track->v_height = num;
725             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |   + Pixel height: %u\n",
726                     track->v_height);
727             break;
728           }
729
730         default:
731             ebml_read_skip (s, &l);
732             break;
733         }
734       length -= l + il;
735     }
736   return len;
737 }
738
739 /**
740  * \brief free any data associated with given track
741  * \param track track of which to free data
742  */
743 static void
744 demux_mkv_free_trackentry(mkv_track_t *track) {
745   free (track->name);
746   free (track->codec_id);
747   free (track->language);
748   free (track->private_data);
749   free (track->audio_buf);
750   free (track->audio_timestamp);
751   demux_mkv_free_encodings(track->encodings, track->num_encodings);
752   free(track);
753 }
754
755 static int
756 demux_mkv_read_trackentry (demuxer_t *demuxer)
757 {
758   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
759   stream_t *s = demuxer->stream;
760   mkv_track_t *track;
761   uint64_t len, length, l;
762   int il;
763
764   track = calloc (1, sizeof (*track));
765   /* set default values */
766   track->default_track = 1;
767   track->name = 0;
768   track->language = strdup("eng");
769
770   len = length = ebml_read_length (s, &il);
771   len += il;
772   while (length > 0)
773     {
774       switch (ebml_read_id (s, &il))
775         {
776         case MATROSKA_ID_TRACKNUMBER:
777           {
778             uint64_t num = ebml_read_uint (s, &l);
779             if (num == EBML_UINT_INVALID)
780               goto err_out;
781             track->tnum = num;
782             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Track number: %u\n",
783                     track->tnum);
784             break;
785           }
786
787         case MATROSKA_ID_TRACKNAME:
788           {
789             track->name = ebml_read_utf8 (s, &l);
790             if (track->name == NULL)
791               goto err_out;
792             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Name: %s\n",
793                     track->name);
794             break;
795           }
796
797         case MATROSKA_ID_TRACKTYPE:
798           {
799             uint64_t num = ebml_read_uint (s, &l);
800             if (num == EBML_UINT_INVALID)
801               return 0;
802             track->type = num;
803             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Track type: ");
804             switch (track->type)
805               {
806               case MATROSKA_TRACK_AUDIO:
807                 mp_msg (MSGT_DEMUX, MSGL_V, "Audio\n");
808                 break;
809               case MATROSKA_TRACK_VIDEO:
810                 mp_msg (MSGT_DEMUX, MSGL_V, "Video\n");
811                 break;
812               case MATROSKA_TRACK_SUBTITLE:
813                 mp_msg (MSGT_DEMUX, MSGL_V, "Subtitle\n");
814                 break;
815               default:
816                 mp_msg (MSGT_DEMUX, MSGL_V, "unknown\n");
817                 break;
818             }
819             break;
820           }
821
822         case MATROSKA_ID_TRACKAUDIO:
823           mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Audio track\n");
824           l = demux_mkv_read_trackaudio (demuxer, track);
825           if (l == 0)
826             goto err_out;
827           break;
828
829         case MATROSKA_ID_TRACKVIDEO:
830           mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Video track\n");
831           l = demux_mkv_read_trackvideo (demuxer, track);
832           if (l == 0)
833             goto err_out;
834           break;
835
836         case MATROSKA_ID_CODECID:
837           track->codec_id = ebml_read_ascii (s, &l);
838           if (track->codec_id == NULL)
839             goto err_out;
840           if (!strcmp (track->codec_id, MKV_V_MSCOMP) ||
841               !strcmp (track->codec_id, MKV_A_ACM))
842             track->ms_compat = 1;
843           else if (!strcmp (track->codec_id, MKV_S_VOBSUB))
844             track->subtitle_type = MATROSKA_SUBTYPE_VOBSUB;
845           else if (!strcmp (track->codec_id, MKV_S_TEXTSSA)
846                    || !strcmp (track->codec_id, MKV_S_TEXTASS)
847                    || !strcmp (track->codec_id, MKV_S_SSA)
848                    || !strcmp (track->codec_id, MKV_S_ASS))
849             {
850               track->subtitle_type = MATROSKA_SUBTYPE_SSA;
851             }
852           else if (!strcmp (track->codec_id, MKV_S_TEXTASCII))
853             track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
854           if (!strcmp (track->codec_id, MKV_S_TEXTUTF8))
855             {
856               track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
857             }
858           mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Codec ID: %s\n",
859                   track->codec_id);
860           break;
861
862         case MATROSKA_ID_CODECPRIVATE:
863           {
864             int x;
865             uint64_t num = ebml_read_length (s, &x);
866             // audit: cheap guard against overflows later..
867             if (num > SIZE_MAX - 1000) return 0;
868             l = x + num;
869             track->private_data = malloc (num + AV_LZO_INPUT_PADDING);
870             if (stream_read(s, track->private_data, num) != (int) num)
871               goto err_out;
872             track->private_size = num;
873             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + CodecPrivate, length "
874                     "%u\n", track->private_size);
875             break;
876           }
877
878         case MATROSKA_ID_TRACKLANGUAGE:
879           free(track->language);
880           track->language = ebml_read_utf8 (s, &l);
881           if (track->language == NULL)
882             goto err_out;
883           mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Language: %s\n",
884                   track->language);
885           break;
886
887         case MATROSKA_ID_TRACKFLAGDEFAULT:
888           {
889             uint64_t num = ebml_read_uint (s, &l);
890             if (num == EBML_UINT_INVALID)
891               goto err_out;
892             track->default_track = num;
893             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Default flag: %u\n",
894                     track->default_track);
895             break;
896           }
897
898         case MATROSKA_ID_TRACKDEFAULTDURATION:
899           {
900             uint64_t num = ebml_read_uint (s, &l);
901             if (num == EBML_UINT_INVALID)
902               goto err_out;
903             if (num == 0)
904               mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Default duration: 0");
905             else
906               {
907                 track->v_frate = 1000000000.0 / num;
908                 track->default_duration = num / 1000000000.0;
909                 mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + Default duration: "
910                         "%.3fms ( = %.3f fps)\n",num/1000000.0,track->v_frate);
911               }
912             break;
913           }
914
915         case MATROSKA_ID_TRACKENCODINGS:
916           l = demux_mkv_read_trackencodings (demuxer, track);
917           if (l == 0)
918             goto err_out;
919           break;
920
921         default:
922           ebml_read_skip (s, &l);
923           break;
924         }
925       length -= l + il;
926     }
927
928   mkv_d->tracks[mkv_d->num_tracks++] = track;
929   return len;
930
931 err_out:
932   demux_mkv_free_trackentry(track);
933   return 0;
934 }
935
936 static int
937 demux_mkv_read_tracks (demuxer_t *demuxer)
938 {
939   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
940   stream_t *s = demuxer->stream;
941   uint64_t length, l;
942   int il;
943
944   mkv_d->tracks = malloc (sizeof (*mkv_d->tracks));
945   mkv_d->num_tracks = 0;
946
947   length = ebml_read_length (s, NULL);
948   while (length > 0)
949     {
950       switch (ebml_read_id (s, &il))
951         {
952         case MATROSKA_ID_TRACKENTRY:
953           mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + a track...\n");
954           mkv_d->tracks = realloc (mkv_d->tracks,
955                                    (mkv_d->num_tracks+1)
956                                    *sizeof (*mkv_d->tracks));
957           l = demux_mkv_read_trackentry (demuxer);
958           if (l == 0)
959             return 1;
960           break;
961
962         default:
963             ebml_read_skip (s, &l);
964             break;
965         }
966       length -= l + il;
967     }
968   return 0;
969 }
970
971 static int
972 demux_mkv_read_cues (demuxer_t *demuxer)
973 {
974   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
975   stream_t *s = demuxer->stream;
976   uint64_t length, l, time, track, pos;
977   off_t off;
978   int i, il;
979
980   if (index_mode == 0) {
981     ebml_read_skip (s, NULL);
982     return 0;
983   }
984   off = stream_tell (s);
985   for (i=0; i<mkv_d->parsed_cues_num; i++)
986     if (mkv_d->parsed_cues[i] == off)
987       {
988         ebml_read_skip (s, NULL);
989         return 0;
990       }
991   mkv_d->parsed_cues = realloc (mkv_d->parsed_cues,
992                                 (mkv_d->parsed_cues_num+1)
993                                 * sizeof (off_t));
994   mkv_d->parsed_cues[mkv_d->parsed_cues_num++] = off;
995
996   mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing cues ] -----------\n");
997   length = ebml_read_length (s, NULL);
998
999   while (length > 0)
1000     {
1001       time = track = pos = EBML_UINT_INVALID;
1002
1003       switch (ebml_read_id (s, &il))
1004         {
1005         case MATROSKA_ID_POINTENTRY:
1006           {
1007             uint64_t len;
1008
1009             len = ebml_read_length (s, &i);
1010             l = len + i;
1011
1012             while (len > 0)
1013               {
1014                 uint64_t l;
1015                 int il;
1016
1017                 switch (ebml_read_id (s, &il))
1018                   {
1019                   case MATROSKA_ID_CUETIME:
1020                     time = ebml_read_uint (s, &l);
1021                     break;
1022
1023                   case MATROSKA_ID_CUETRACKPOSITION:
1024                     {
1025                       uint64_t le;
1026
1027                       le = ebml_read_length (s, &i);
1028                       l = le + i;
1029
1030                       while (le > 0)
1031                         {
1032                           uint64_t l;
1033                           int il;
1034
1035                           switch (ebml_read_id (s, &il))
1036                             {
1037                             case MATROSKA_ID_CUETRACK:
1038                               track = ebml_read_uint (s, &l);
1039                               break;
1040
1041                             case MATROSKA_ID_CUECLUSTERPOSITION:
1042                               pos = ebml_read_uint (s, &l);
1043                               break;
1044
1045                             default:
1046                               ebml_read_skip (s, &l);
1047                               break;
1048                             }
1049                           le -= l + il;
1050                         }
1051                       break;
1052                     }
1053
1054                   default:
1055                     ebml_read_skip (s, &l);
1056                     break;
1057                   }
1058                 len -= l + il;
1059               }
1060             break;
1061           }
1062
1063         default:
1064           ebml_read_skip (s, &l);
1065           break;
1066         }
1067
1068       length -= l + il;
1069
1070       if (time != EBML_UINT_INVALID && track != EBML_UINT_INVALID
1071           && pos != EBML_UINT_INVALID)
1072         {
1073           grow_array(&mkv_d->indexes, mkv_d->num_indexes, sizeof(mkv_index_t));
1074           if (!mkv_d->indexes) {
1075             mkv_d->num_indexes = 0;
1076             break;
1077           }
1078           mkv_d->indexes[mkv_d->num_indexes].tnum = track;
1079           mkv_d->indexes[mkv_d->num_indexes].timecode = time;
1080           mkv_d->indexes[mkv_d->num_indexes].filepos =mkv_d->segment_start+pos;
1081           mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] |+ found cue point "
1082                   "for track %"PRIu64": timecode %"PRIu64", filepos: %"PRIu64"\n",
1083                   track, time, mkv_d->segment_start + pos);
1084           mkv_d->num_indexes++;
1085         }
1086     }
1087
1088   mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing cues ] -----------\n");
1089   return 0;
1090 }
1091
1092 static int
1093 demux_mkv_read_chapters (demuxer_t *demuxer)
1094 {
1095   stream_t *s = demuxer->stream;
1096   uint64_t length, l;
1097   int il;
1098
1099   if (demuxer->chapters)
1100     {
1101       ebml_read_skip (s, NULL);
1102       return 0;
1103     }
1104
1105   mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing chapters ] ---------\n");
1106   length = ebml_read_length (s, NULL);
1107
1108   while (length > 0)
1109     {
1110       switch (ebml_read_id (s, &il))
1111         {
1112         case MATROSKA_ID_EDITIONENTRY:
1113           {
1114             uint64_t len;
1115             int i;
1116
1117             len = ebml_read_length (s, &i);
1118             l = len + i;
1119
1120             while (len > 0)
1121               {
1122                 uint64_t l;
1123                 int il;
1124
1125                 switch (ebml_read_id (s, &il))
1126                   {
1127                   case MATROSKA_ID_CHAPTERATOM:
1128                     {
1129                       uint64_t len, start=0, end=0;
1130                       char* name = 0;
1131                       int i;
1132                       int cid;
1133
1134                       len = ebml_read_length (s, &i);
1135                       l = len + i;
1136
1137                       while (len > 0)
1138                         {
1139                           uint64_t l;
1140                           int il;
1141
1142                           switch (ebml_read_id (s, &il))
1143                             {
1144                             case MATROSKA_ID_CHAPTERTIMESTART:
1145                               start = ebml_read_uint (s, &l) / 1000000;
1146                               break;
1147
1148                             case MATROSKA_ID_CHAPTERTIMEEND:
1149                               end = ebml_read_uint (s, &l) / 1000000;
1150                               break;
1151
1152                             case MATROSKA_ID_CHAPTERDISPLAY:
1153                               {
1154                                 uint64_t len;
1155                                 int i;
1156
1157                                 len = ebml_read_length (s, &i);
1158                                 l = len + i;
1159                                 while (len > 0)
1160                                   {
1161                                     uint64_t l;
1162                                     int il;
1163
1164                                     switch (ebml_read_id (s, &il))
1165                                       {
1166                                         case MATROSKA_ID_CHAPSTRING:
1167                                           name = ebml_read_utf8 (s, &l);
1168                                           break;
1169                                         default:
1170                                           ebml_read_skip (s, &l);
1171                                           break;
1172                                       }
1173                                     len -= l + il;
1174                                   }
1175                               }
1176                               break;
1177
1178                             default:
1179                               ebml_read_skip (s, &l);
1180                               break;
1181                             }
1182                           len -= l + il;
1183                         }
1184
1185                       if (!name)
1186                         name = strdup("(unnamed)");
1187
1188                       cid = demuxer_add_chapter(demuxer, name, start, end);
1189
1190                       mp_msg(MSGT_DEMUX, MSGL_V,
1191                              "[mkv] Chapter %u from %02d:%02d:%02d."
1192                              "%03d to %02d:%02d:%02d.%03d, %s\n",
1193                              cid,
1194                              (int) (start / 60 / 60 / 1000),
1195                              (int) ((start / 60 / 1000) % 60),
1196                              (int) ((start / 1000) % 60),
1197                              (int) (start % 1000),
1198                              (int) (end / 60 / 60 / 1000),
1199                              (int) ((end / 60 / 1000) % 60),
1200                              (int) ((end / 1000) % 60),
1201                              (int) (end % 1000), name);
1202
1203                       free(name);
1204                       break;
1205                     }
1206
1207                   default:
1208                     ebml_read_skip (s, &l);
1209                     break;
1210                   }
1211                 len -= l + il;
1212               }
1213             break;
1214           }
1215
1216         default:
1217           ebml_read_skip (s, &l);
1218           break;
1219         }
1220
1221       length -= l + il;
1222     }
1223
1224   mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing chapters ] ---------\n");
1225   return 0;
1226 }
1227
1228 static int
1229 demux_mkv_read_tags (demuxer_t *demuxer)
1230 {
1231   ebml_read_skip (demuxer->stream, NULL);
1232   return 0;
1233 }
1234
1235 static int
1236 demux_mkv_read_attachments (demuxer_t *demuxer)
1237 {
1238   stream_t *s = demuxer->stream;
1239   uint64_t length, l;
1240   int il;
1241
1242   mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing attachments ] ---------\n");
1243   length = ebml_read_length (s, NULL);
1244
1245   while (length > 0)
1246     {
1247       switch (ebml_read_id (s, &il))
1248         {
1249           case MATROSKA_ID_ATTACHEDFILE:
1250             {
1251               uint64_t len;
1252               int i;
1253               char* name = NULL;
1254               char* mime = NULL;
1255               char* data = NULL;
1256               int data_size = 0;
1257
1258               len = ebml_read_length (s, &i);
1259               l = len + i;
1260
1261               mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + an attachment...\n");
1262
1263               while (len > 0)
1264                 {
1265                   uint64_t l;
1266                   int il;
1267
1268                   switch (ebml_read_id (s, &il))
1269                     {
1270                     case MATROSKA_ID_FILENAME:
1271                       name = ebml_read_utf8 (s, &l);
1272                       if (name == NULL)
1273                         return 0;
1274                       mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + FileName: %s\n",
1275                         name);
1276                       break;
1277
1278                     case MATROSKA_ID_FILEMIMETYPE:
1279                       mime = ebml_read_ascii (s, &l);
1280                       if (mime == NULL)
1281                         return 0;
1282                       mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + FileMimeType: %s\n",
1283                         mime);
1284                       break;
1285
1286                     case MATROSKA_ID_FILEDATA:
1287                       {
1288                         int x;
1289                         uint64_t num = ebml_read_length (s, &x);
1290                         l = x + num;
1291                         free(data);
1292                         data = malloc (num);
1293                         if (stream_read(s, data, num) != (int) num)
1294                         {
1295                           free(data);
1296                           return 0;
1297                         }
1298                         data_size = num;
1299                         mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |  + FileData, length "
1300                                 "%u\n", data_size);
1301                         break;
1302                       }
1303
1304                     default:
1305                       ebml_read_skip (s, &l);
1306                       break;
1307                     }
1308                   len -= l + il;
1309                 }
1310
1311               demuxer_add_attachment(demuxer, name, mime, data, data_size);
1312               mp_msg(MSGT_DEMUX, MSGL_V,
1313                      "[mkv] Attachment: %s, %s, %u bytes\n",
1314                      name, mime, data_size);
1315               break;
1316             }
1317
1318           default:
1319             ebml_read_skip (s, &l);
1320             break;
1321         }
1322       length -= l + il;
1323     }
1324
1325   mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing attachments ] ---------\n");
1326   return 0;
1327 }
1328
1329 static int
1330 demux_mkv_read_seekhead (demuxer_t *demuxer)
1331 {
1332   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1333   stream_t *s = demuxer->stream;
1334   uint64_t length, l, seek_pos, saved_pos, num;
1335   uint32_t seek_id;
1336   int i, il, res = 0;
1337   off_t off;
1338
1339   off = stream_tell (s);
1340   for (i=0; i<mkv_d->parsed_seekhead_num; i++)
1341     if (mkv_d->parsed_seekhead[i] == off)
1342       {
1343         ebml_read_skip (s, NULL);
1344         return 0;
1345       }
1346   mkv_d->parsed_seekhead = realloc (mkv_d->parsed_seekhead,
1347                                     (mkv_d->parsed_seekhead_num+1)
1348                                     * sizeof (off_t));
1349   mkv_d->parsed_seekhead[mkv_d->parsed_seekhead_num++] = off;
1350
1351   mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing seek head ] ---------\n");
1352   length = ebml_read_length (s, NULL);
1353   /* off now holds the position of the next element after the seek head. */
1354   off = stream_tell (s) + length;
1355   while (length > 0 && !res)
1356     {
1357
1358       seek_id = 0;
1359       seek_pos = EBML_UINT_INVALID;
1360
1361       switch (ebml_read_id (s, &il))
1362         {
1363         case MATROSKA_ID_SEEKENTRY:
1364           {
1365             uint64_t len;
1366
1367             len = ebml_read_length (s, &i);
1368             l = len + i;
1369
1370             while (len > 0)
1371               {
1372                 uint64_t l;
1373                 int il;
1374
1375                 switch (ebml_read_id (s, &il))
1376                   {
1377                   case MATROSKA_ID_SEEKID:
1378                     num = ebml_read_uint (s, &l);
1379                     if (num != EBML_UINT_INVALID)
1380                       seek_id = num;
1381                     break;
1382
1383                   case MATROSKA_ID_SEEKPOSITION:
1384                     seek_pos = ebml_read_uint (s, &l);
1385                     break;
1386
1387                   default:
1388                     ebml_read_skip (s, &l);
1389                     break;
1390                   }
1391                 len -= l + il;
1392               }
1393
1394             break;
1395           }
1396
1397         default:
1398             ebml_read_skip (s, &l);
1399             break;
1400         }
1401       length -= l + il;
1402
1403       if (seek_id == 0 || seek_id == MATROSKA_ID_CLUSTER
1404           || seek_pos == EBML_UINT_INVALID ||
1405           ((mkv_d->segment_start + seek_pos) >= (uint64_t)demuxer->movi_end))
1406         continue;
1407
1408       saved_pos = stream_tell (s);
1409       if (!stream_seek (s, mkv_d->segment_start + seek_pos))
1410         res = 1;
1411       else
1412         {
1413           if (ebml_read_id (s, &il) != seek_id)
1414             res = 1;
1415           else
1416             switch (seek_id)
1417               {
1418               case MATROSKA_ID_CUES:
1419                 if (demux_mkv_read_cues (demuxer))
1420                   res = 1;
1421                 break;
1422
1423               case MATROSKA_ID_TAGS:
1424                 if (demux_mkv_read_tags (demuxer))
1425                   res = 1;
1426                 break;
1427
1428               case MATROSKA_ID_SEEKHEAD:
1429                 if (demux_mkv_read_seekhead (demuxer))
1430                   res = 1;
1431                 break;
1432
1433               case MATROSKA_ID_CHAPTERS:
1434                 if (demux_mkv_read_chapters (demuxer))
1435                   res = 1;
1436                 break;
1437               }
1438         }
1439
1440       stream_seek (s, saved_pos);
1441     }
1442   if (res)
1443     {
1444       /* If there was an error then try to skip this seek head. */
1445       if (stream_seek (s, off))
1446         res = 0;
1447     }
1448   else
1449   if (length > 0)
1450      stream_seek (s, stream_tell (s) + length);
1451   mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing seek head ] ---------\n");
1452   return res;
1453 }
1454
1455 static int
1456 demux_mkv_open_video (demuxer_t *demuxer, mkv_track_t *track, int vid);
1457 static int
1458 demux_mkv_open_audio (demuxer_t *demuxer, mkv_track_t *track, int aid);
1459 static int
1460 demux_mkv_open_sub (demuxer_t *demuxer, mkv_track_t *track, int sid);
1461
1462 static void
1463 display_create_tracks (demuxer_t *demuxer)
1464 {
1465   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *)demuxer->priv;
1466   int i, vid=0, aid=0, sid=0;
1467
1468   for (i=0; i<mkv_d->num_tracks; i++)
1469     {
1470       char *type = "unknown", str[32];
1471       *str = '\0';
1472       switch (mkv_d->tracks[i]->type)
1473         {
1474         case MATROSKA_TRACK_VIDEO:
1475           type = "video";
1476           demux_mkv_open_video(demuxer, mkv_d->tracks[i], vid);
1477           if (mkv_d->tracks[i]->name)
1478             mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", vid, mkv_d->tracks[i]->name);
1479           sprintf (str, "-vid %u", vid++);
1480           break;
1481         case MATROSKA_TRACK_AUDIO:
1482           type = "audio";
1483           demux_mkv_open_audio(demuxer, mkv_d->tracks[i], aid);
1484           if (mkv_d->tracks[i]->name)
1485             mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", aid, mkv_d->tracks[i]->name);
1486           mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", aid, mkv_d->tracks[i]->language);
1487           sprintf (str, "-aid %u, -alang %.5s",aid++,mkv_d->tracks[i]->language);
1488           break;
1489         case MATROSKA_TRACK_SUBTITLE:
1490           type = "subtitles";
1491           demux_mkv_open_sub(demuxer, mkv_d->tracks[i], sid);
1492           if (mkv_d->tracks[i]->name)
1493             mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", sid, mkv_d->tracks[i]->name);
1494           mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid, mkv_d->tracks[i]->language);
1495           sprintf (str, "-sid %u, -slang %.5s",sid++,mkv_d->tracks[i]->language);
1496           break;
1497         }
1498       if (mkv_d->tracks[i]->name)
1499         mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackIDName,
1500              mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, mkv_d->tracks[i]->name, str);
1501       else
1502         mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackID,
1503              mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, str);
1504     }
1505 }
1506
1507 typedef struct {
1508   char *id;
1509   int fourcc;
1510   int extradata;
1511 } videocodec_info_t;
1512
1513 static const videocodec_info_t vinfo[] = {
1514   { MKV_V_MPEG1,     mmioFOURCC('m', 'p', 'g', '1'), 0 },
1515   { MKV_V_MPEG2,     mmioFOURCC('m', 'p', 'g', '2'), 0 },
1516   { MKV_V_MPEG4_SP,  mmioFOURCC('m', 'p', '4', 'v'), 1 },
1517   { MKV_V_MPEG4_ASP, mmioFOURCC('m', 'p', '4', 'v'), 1 },
1518   { MKV_V_MPEG4_AP,  mmioFOURCC('m', 'p', '4', 'v'), 1 },
1519   { MKV_V_MPEG4_AVC, mmioFOURCC('a', 'v', 'c', '1'), 1 },
1520   { MKV_V_THEORA,    mmioFOURCC('t', 'h', 'e', 'o'), 1 },
1521   { NULL, 0, 0 }
1522 };
1523
1524 static int
1525 demux_mkv_open_video (demuxer_t *demuxer, mkv_track_t *track, int vid)
1526 {
1527   BITMAPINFOHEADER *bih;
1528   void *ImageDesc = NULL;
1529   sh_video_t *sh_v;
1530
1531   if (track->ms_compat)  /* MS compatibility mode */
1532     {
1533       BITMAPINFOHEADER *src;
1534
1535       if (track->private_data == NULL
1536           || track->private_size < sizeof (BITMAPINFOHEADER))
1537         return 1;
1538
1539       src = (BITMAPINFOHEADER *) track->private_data;
1540       bih = calloc (1, track->private_size);
1541       bih->biSize = le2me_32 (src->biSize);
1542       bih->biWidth = le2me_32 (src->biWidth);
1543       bih->biHeight = le2me_32 (src->biHeight);
1544       bih->biPlanes = le2me_16 (src->biPlanes);
1545       bih->biBitCount = le2me_16 (src->biBitCount);
1546       bih->biCompression = le2me_32 (src->biCompression);
1547       bih->biSizeImage = le2me_32 (src->biSizeImage);
1548       bih->biXPelsPerMeter = le2me_32 (src->biXPelsPerMeter);
1549       bih->biYPelsPerMeter = le2me_32 (src->biYPelsPerMeter);
1550       bih->biClrUsed = le2me_32 (src->biClrUsed);
1551       bih->biClrImportant = le2me_32 (src->biClrImportant);
1552       memcpy((char *) bih + sizeof (BITMAPINFOHEADER),
1553              (char *) src + sizeof (BITMAPINFOHEADER),
1554              track->private_size - sizeof (BITMAPINFOHEADER));
1555
1556       if (track->v_width == 0)
1557         track->v_width = bih->biWidth;
1558       if (track->v_height == 0)
1559         track->v_height = bih->biHeight;
1560     }
1561   else
1562     {
1563       bih = calloc (1, sizeof (BITMAPINFOHEADER));
1564       bih->biSize = sizeof (BITMAPINFOHEADER);
1565       bih->biWidth = track->v_width;
1566       bih->biHeight = track->v_height;
1567       bih->biBitCount = 24;
1568       bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
1569
1570       if (track->private_size >= RVPROPERTIES_SIZE
1571           && (!strcmp (track->codec_id, MKV_V_REALV10)
1572               || !strcmp (track->codec_id, MKV_V_REALV20)
1573               || !strcmp (track->codec_id, MKV_V_REALV30)
1574               || !strcmp (track->codec_id, MKV_V_REALV40)))
1575         {
1576           unsigned char *dst, *src;
1577           uint32_t type2;
1578           unsigned int cnt;
1579
1580           src = (uint8_t *)track->private_data + RVPROPERTIES_SIZE;
1581
1582           cnt = track->private_size - RVPROPERTIES_SIZE;
1583           bih = realloc(bih, sizeof (BITMAPINFOHEADER)+8+cnt);
1584           bih->biSize = 48+cnt;
1585           bih->biPlanes = 1;
1586           type2 = AV_RB32(src - 4);
1587           if (type2 == 0x10003000 || type2 == 0x10003001)
1588             bih->biCompression=mmioFOURCC('R','V','1','3');
1589           else
1590             bih->biCompression=mmioFOURCC('R','V',track->codec_id[9],'0');
1591           dst = (unsigned char *) (bih + 1);
1592           // copy type1 and type2 info from rv properties
1593           memcpy(dst, src - 8, 8);
1594           stream_read(demuxer->stream, dst+8, cnt);
1595           track->realmedia = 1;
1596
1597 #ifdef CONFIG_QTX_CODECS
1598         }
1599       else if (track->private_size >= sizeof (ImageDescription)
1600                && !strcmp(track->codec_id, MKV_V_QUICKTIME))
1601         {
1602           ImageDescriptionPtr idesc;
1603
1604           idesc = (ImageDescriptionPtr) track->private_data;
1605           idesc->idSize = be2me_32 (idesc->idSize);
1606           idesc->cType = be2me_32 (idesc->cType);
1607           idesc->version = be2me_16 (idesc->version);
1608           idesc->revisionLevel = be2me_16 (idesc->revisionLevel);
1609           idesc->vendor = be2me_32 (idesc->vendor);
1610           idesc->temporalQuality = be2me_32 (idesc->temporalQuality);
1611           idesc->spatialQuality = be2me_32 (idesc->spatialQuality);
1612           idesc->width = be2me_16 (idesc->width);
1613           idesc->height = be2me_16 (idesc->height);
1614           idesc->hRes = be2me_32 (idesc->hRes);
1615           idesc->vRes = be2me_32 (idesc->vRes);
1616           idesc->dataSize = be2me_32 (idesc->dataSize);
1617           idesc->frameCount = be2me_16 (idesc->frameCount);
1618           idesc->depth = be2me_16 (idesc->depth);
1619           idesc->clutID = be2me_16 (idesc->clutID);
1620           bih->biPlanes = 1;
1621           bih->biCompression = idesc->cType;
1622           ImageDesc = idesc;
1623 #endif /* CONFIG_QTX_CODECS */
1624
1625         }
1626       else
1627         {
1628           const videocodec_info_t *vi = vinfo;
1629           while (vi->id && strcmp(vi->id, track->codec_id)) vi++;
1630           bih->biCompression = vi->fourcc;
1631           if (vi->extradata && track->private_data && (track->private_size > 0))
1632             {
1633               bih->biSize += track->private_size;
1634               bih = realloc (bih, bih->biSize);
1635               memcpy (bih + 1, track->private_data, track->private_size);
1636             }
1637           track->reorder_timecodes = user_correct_pts == 0;
1638           if (!vi->id) {
1639               mp_msg (MSGT_DEMUX,MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownCodecID,
1640                       track->codec_id, track->tnum);
1641               free(bih);
1642               return 1;
1643           }
1644         }
1645     }
1646
1647   sh_v = new_sh_video_vid (demuxer, track->tnum, vid);
1648   sh_v->bih = bih;
1649   sh_v->format = sh_v->bih->biCompression;
1650   if (track->v_frate == 0.0)
1651     track->v_frate = 25.0;
1652   sh_v->fps = track->v_frate;
1653   sh_v->frametime = 1 / track->v_frate;
1654   sh_v->aspect = 0;
1655   if (!track->realmedia)
1656     {
1657       sh_v->disp_w = track->v_width;
1658       sh_v->disp_h = track->v_height;
1659       if (track->v_dheight)
1660       sh_v->aspect = (float)track->v_dwidth / (float)track->v_dheight;
1661     }
1662   else
1663     {
1664       // vd_realvid.c will set aspect to disp_w/disp_h and rederive
1665       // disp_w and disp_h from the RealVideo stream contents returned
1666       // by the Real DLLs. If DisplayWidth/DisplayHeight was not set in
1667       // the Matroska file then it has already been set to PixelWidth/Height
1668       // by check_track_information.
1669       sh_v->disp_w = track->v_dwidth;
1670       sh_v->disp_h = track->v_dheight;
1671     }
1672   sh_v->ImageDesc = ImageDesc;
1673   mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] Aspect: %f\n", sh_v->aspect);
1674
1675   sh_v->ds = demuxer->video;
1676   return 0;
1677 }
1678
1679 static int
1680 demux_mkv_open_audio (demuxer_t *demuxer, mkv_track_t *track, int aid)
1681 {
1682   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1683   sh_audio_t *sh_a = new_sh_audio_aid(demuxer, track->tnum, aid);
1684   demux_packet_t *dp;
1685   if(!sh_a) return 1;
1686   mkv_d->audio_tracks[mkv_d->last_aid] = track->tnum;
1687
1688   if (track->language && (strcmp(track->language, "und") != 0))
1689     sh_a->lang = strdup(track->language);
1690   sh_a->default_track = track->default_track;
1691   sh_a->ds = demuxer->audio;
1692   sh_a->wf = malloc (sizeof (WAVEFORMATEX));
1693   if (track->ms_compat && (track->private_size >= sizeof(WAVEFORMATEX)))
1694     {
1695       WAVEFORMATEX *wf = (WAVEFORMATEX *)track->private_data;
1696       sh_a->wf = realloc(sh_a->wf, track->private_size);
1697       sh_a->wf->wFormatTag = le2me_16 (wf->wFormatTag);
1698       sh_a->wf->nChannels = le2me_16 (wf->nChannels);
1699       sh_a->wf->nSamplesPerSec = le2me_32 (wf->nSamplesPerSec);
1700       sh_a->wf->nAvgBytesPerSec = le2me_32 (wf->nAvgBytesPerSec);
1701       sh_a->wf->nBlockAlign = le2me_16 (wf->nBlockAlign);
1702       sh_a->wf->wBitsPerSample = le2me_16 (wf->wBitsPerSample);
1703       sh_a->wf->cbSize = track->private_size - sizeof(WAVEFORMATEX);
1704       memcpy(sh_a->wf + 1, wf + 1, track->private_size - sizeof(WAVEFORMATEX));
1705       if (track->a_sfreq == 0.0)
1706         track->a_sfreq = sh_a->wf->nSamplesPerSec;
1707       if (track->a_channels == 0)
1708         track->a_channels = sh_a->wf->nChannels;
1709       if (track->a_bps == 0)
1710         track->a_bps = sh_a->wf->wBitsPerSample;
1711       track->a_formattag = sh_a->wf->wFormatTag;
1712     }
1713   else
1714     {
1715       memset(sh_a->wf, 0, sizeof (WAVEFORMATEX));
1716       if (!strcmp(track->codec_id, MKV_A_MP3) ||
1717           !strcmp(track->codec_id, MKV_A_MP2))
1718         track->a_formattag = 0x0055;
1719       else if (!strncmp(track->codec_id, MKV_A_AC3, strlen(MKV_A_AC3)))
1720         track->a_formattag = 0x2000;
1721       else if (!strcmp(track->codec_id, MKV_A_DTS))
1722         track->a_formattag = 0x2001;
1723       else if (!strcmp(track->codec_id, MKV_A_PCM) ||
1724                !strcmp(track->codec_id, MKV_A_PCM_BE))
1725         track->a_formattag = 0x0001;
1726       else if (!strcmp(track->codec_id, MKV_A_AAC_2MAIN) ||
1727                !strncmp(track->codec_id, MKV_A_AAC_2LC,
1728                         strlen(MKV_A_AAC_2LC)) ||
1729                !strcmp(track->codec_id, MKV_A_AAC_2SSR) ||
1730                !strcmp(track->codec_id, MKV_A_AAC_4MAIN) ||
1731                !strncmp(track->codec_id, MKV_A_AAC_4LC,
1732                         strlen(MKV_A_AAC_4LC)) ||
1733                !strcmp(track->codec_id, MKV_A_AAC_4SSR) ||
1734                !strcmp(track->codec_id, MKV_A_AAC_4LTP) ||
1735                !strcmp(track->codec_id, MKV_A_AAC))
1736         track->a_formattag = mmioFOURCC('M', 'P', '4', 'A');
1737       else if (!strcmp(track->codec_id, MKV_A_VORBIS))
1738         {
1739           if (track->private_data == NULL)
1740             return 1;
1741           track->a_formattag = mmioFOURCC('v', 'r', 'b', 's');
1742         }
1743       else if (!strcmp(track->codec_id, MKV_A_QDMC))
1744         track->a_formattag = mmioFOURCC('Q', 'D', 'M', 'C');
1745       else if (!strcmp(track->codec_id, MKV_A_QDMC2))
1746         track->a_formattag = mmioFOURCC('Q', 'D', 'M', '2');
1747       else if (!strcmp(track->codec_id, MKV_A_WAVPACK))
1748         track->a_formattag = mmioFOURCC('W', 'V', 'P', 'K');
1749       else if (!strcmp(track->codec_id, MKV_A_TRUEHD))
1750         track->a_formattag = mmioFOURCC('T', 'R', 'H', 'D');
1751       else if (!strcmp(track->codec_id, MKV_A_FLAC))
1752         {
1753           if (track->private_data == NULL || track->private_size == 0)
1754             {
1755               mp_msg (MSGT_DEMUX, MSGL_WARN,
1756                       MSGTR_MPDEMUX_MKV_FlacTrackDoesNotContainValidHeaders);
1757               return 1;
1758             }
1759           track->a_formattag = mmioFOURCC ('f', 'L', 'a', 'C');
1760         }
1761       else if (track->private_size >= RAPROPERTIES4_SIZE)
1762         {
1763           if (!strcmp(track->codec_id, MKV_A_REAL28))
1764             track->a_formattag = mmioFOURCC('2', '8', '_', '8');
1765           else if (!strcmp(track->codec_id, MKV_A_REALATRC))
1766             track->a_formattag = mmioFOURCC('a', 't', 'r', 'c');
1767           else if (!strcmp(track->codec_id, MKV_A_REALCOOK))
1768             track->a_formattag = mmioFOURCC('c', 'o', 'o', 'k');
1769           else if (!strcmp(track->codec_id, MKV_A_REALDNET))
1770             track->a_formattag = mmioFOURCC('d', 'n', 'e', 't');
1771           else if (!strcmp(track->codec_id, MKV_A_REALSIPR))
1772             track->a_formattag = mmioFOURCC('s', 'i', 'p', 'r');
1773         }
1774       else
1775         {
1776           mp_msg (MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownAudioCodec,
1777                   track->codec_id, track->tnum);
1778           free_sh_audio(demuxer, track->tnum);
1779           return 1;
1780         }
1781     }
1782
1783   sh_a->format = track->a_formattag;
1784   sh_a->wf->wFormatTag = track->a_formattag;
1785   sh_a->channels = track->a_channels;
1786   sh_a->wf->nChannels = track->a_channels;
1787   sh_a->samplerate = (uint32_t) track->a_sfreq;
1788   sh_a->wf->nSamplesPerSec = (uint32_t) track->a_sfreq;
1789   if (track->a_bps == 0)
1790     {
1791       sh_a->samplesize = 2;
1792       sh_a->wf->wBitsPerSample = 16;
1793     }
1794   else
1795     {
1796       sh_a->samplesize = track->a_bps / 8;
1797       sh_a->wf->wBitsPerSample = track->a_bps;
1798     }
1799   if (track->a_formattag == 0x0055)  /* MP3 || MP2 */
1800     {
1801       sh_a->wf->nAvgBytesPerSec = 16000;
1802       sh_a->wf->nBlockAlign = 1152;
1803     }
1804   else if ((track->a_formattag == 0x2000) || /* AC3 */
1805            (track->a_formattag == 0x2001)) /* DTS */
1806     {
1807       free(sh_a->wf);
1808       sh_a->wf = NULL;
1809     }
1810   else if (track->a_formattag == 0x0001)  /* PCM || PCM_BE */
1811     {
1812       sh_a->wf->nAvgBytesPerSec = sh_a->channels * sh_a->samplerate*2;
1813       sh_a->wf->nBlockAlign = sh_a->wf->nAvgBytesPerSec;
1814       if (!strcmp(track->codec_id, MKV_A_PCM_BE))
1815         sh_a->format = mmioFOURCC('t', 'w', 'o', 's');
1816     }
1817   else if (!strcmp(track->codec_id, MKV_A_QDMC) ||
1818            !strcmp(track->codec_id, MKV_A_QDMC2))
1819     {
1820       sh_a->wf->nAvgBytesPerSec = 16000;
1821       sh_a->wf->nBlockAlign = 1486;
1822       track->fix_i_bps = 1;
1823       track->qt_last_a_pts = 0.0;
1824       if (track->private_data != NULL)
1825         {
1826           sh_a->codecdata=malloc(track->private_size);
1827           memcpy (sh_a->codecdata, track->private_data,
1828                   track->private_size);
1829           sh_a->codecdata_len = track->private_size;
1830         }
1831     }
1832   else if (track->a_formattag == mmioFOURCC('M', 'P', '4', 'A'))
1833     {
1834       int profile, srate_idx;
1835
1836       sh_a->wf->nAvgBytesPerSec = 16000;
1837       sh_a->wf->nBlockAlign = 1024;
1838
1839       if (!strcmp (track->codec_id, MKV_A_AAC) &&
1840           (NULL != track->private_data))
1841         {
1842           sh_a->codecdata=malloc(track->private_size);
1843           memcpy (sh_a->codecdata, track->private_data,
1844                   track->private_size);
1845           sh_a->codecdata_len = track->private_size;
1846           return 0;
1847         }
1848
1849       /* Recreate the 'private data' */
1850       /* which faad2 uses in its initialization */
1851       srate_idx = aac_get_sample_rate_index (sh_a->samplerate);
1852       if (!strncmp (&track->codec_id[12], "MAIN", 4))
1853         profile = 0;
1854       else if (!strncmp (&track->codec_id[12], "LC", 2))
1855         profile = 1;
1856       else if (!strncmp (&track->codec_id[12], "SSR", 3))
1857         profile = 2;
1858       else
1859         profile = 3;
1860       sh_a->codecdata = malloc (5);
1861       sh_a->codecdata[0] = ((profile+1) << 3) | ((srate_idx&0xE) >> 1);
1862       sh_a->codecdata[1] = ((srate_idx&0x1)<<7)|(track->a_channels<<3);
1863
1864       if (strstr(track->codec_id, "SBR") != NULL)
1865         {
1866           /* HE-AAC (aka SBR AAC) */
1867           sh_a->codecdata_len = 5;
1868
1869           sh_a->samplerate *= 2;
1870           sh_a->wf->nSamplesPerSec *= 2;
1871           srate_idx = aac_get_sample_rate_index(sh_a->samplerate);
1872           sh_a->codecdata[2] = AAC_SYNC_EXTENSION_TYPE >> 3;
1873           sh_a->codecdata[3] = ((AAC_SYNC_EXTENSION_TYPE&0x07)<<5) | 5;
1874           sh_a->codecdata[4] = (1 << 7) | (srate_idx << 3);
1875           track->default_duration = 1024.0 / (sh_a->samplerate / 2);
1876         }
1877       else
1878         {
1879           sh_a->codecdata_len = 2;
1880           track->default_duration = 1024.0 / (float)sh_a->samplerate;
1881         }
1882     }
1883   else if (track->a_formattag == mmioFOURCC('v', 'r', 'b', 's'))  /* VORBIS */
1884     {
1885       sh_a->wf->cbSize = track->private_size;
1886       sh_a->wf = realloc(sh_a->wf, sizeof(WAVEFORMATEX) + sh_a->wf->cbSize);
1887       memcpy((unsigned char *) (sh_a->wf+1), track->private_data, sh_a->wf->cbSize);
1888     }
1889   else if (track->private_size >= RAPROPERTIES4_SIZE
1890            && !strncmp (track->codec_id, MKV_A_REALATRC, 7))
1891     {
1892       /* Common initialization for all RealAudio codecs */
1893       unsigned char *src = track->private_data;
1894       int codecdata_length, version;
1895       int flavor;
1896
1897       sh_a->wf->nAvgBytesPerSec = 0;  /* FIXME !? */
1898
1899       version = AV_RB16(src + 4);
1900       flavor = AV_RB16(src + 22);
1901       track->coded_framesize = AV_RB32(src + 24);
1902       track->sub_packet_h = AV_RB16(src + 40);
1903       sh_a->wf->nBlockAlign =
1904       track->audiopk_size = AV_RB16(src + 42);
1905       track->sub_packet_size = AV_RB16(src + 44);
1906       if (version == 4)
1907         {
1908           src += RAPROPERTIES4_SIZE;
1909           src += src[0] + 1;
1910           src += src[0] + 1;
1911         }
1912       else
1913         src += RAPROPERTIES5_SIZE;
1914
1915       src += 3;
1916       if (version == 5)
1917         src++;
1918       codecdata_length = AV_RB32(src);
1919       src += 4;
1920       sh_a->wf->cbSize = codecdata_length;
1921       sh_a->wf = realloc (sh_a->wf,
1922                           sizeof (WAVEFORMATEX) +
1923                           sh_a->wf->cbSize);
1924       memcpy(((char *)(sh_a->wf + 1)), src, codecdata_length);
1925
1926       switch (track->a_formattag) {
1927         case mmioFOURCC('a', 't', 'r', 'c'):
1928           sh_a->wf->nAvgBytesPerSec = atrc_fl2bps[flavor];
1929           sh_a->wf->nBlockAlign = track->sub_packet_size;
1930           track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
1931           track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
1932           break;
1933         case mmioFOURCC('c', 'o', 'o', 'k'):
1934           sh_a->wf->nAvgBytesPerSec = cook_fl2bps[flavor];
1935           sh_a->wf->nBlockAlign = track->sub_packet_size;
1936           track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
1937           track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
1938           break;
1939         case mmioFOURCC('s', 'i', 'p', 'r'):
1940           sh_a->wf->nAvgBytesPerSec = sipr_fl2bps[flavor];
1941           sh_a->wf->nBlockAlign = track->coded_framesize;
1942           track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
1943           track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
1944           break;
1945         case mmioFOURCC('2', '8', '_', '8'):
1946           sh_a->wf->nAvgBytesPerSec = 3600;
1947           sh_a->wf->nBlockAlign = track->coded_framesize;
1948           track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
1949           track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
1950           break;
1951       }
1952
1953       track->realmedia = 1;
1954     }
1955   else if (!strcmp(track->codec_id, MKV_A_FLAC) ||
1956            (track->a_formattag == 0xf1ac))
1957     {
1958       unsigned char *ptr;
1959       int size;
1960       free(sh_a->wf);
1961       sh_a->wf = NULL;
1962
1963       if (track->a_formattag == mmioFOURCC('f', 'L', 'a', 'C'))
1964         {
1965           ptr = (unsigned char *)track->private_data;
1966           size = track->private_size;
1967         }
1968       else
1969         {
1970           sh_a->format = mmioFOURCC('f', 'L', 'a', 'C');
1971           ptr = (unsigned char *) track->private_data
1972             + sizeof (WAVEFORMATEX);
1973           size = track->private_size - sizeof (WAVEFORMATEX);
1974         }
1975       if (size < 4 || ptr[0] != 'f' || ptr[1] != 'L' ||
1976           ptr[2] != 'a' || ptr[3] != 'C')
1977         {
1978           dp = new_demux_packet (4);
1979           memcpy (dp->buffer, "fLaC", 4);
1980         }
1981       else
1982         {
1983           dp = new_demux_packet (size);
1984           memcpy (dp->buffer, ptr, size);
1985         }
1986       dp->pts = 0;
1987       dp->flags = 0;
1988       ds_add_packet (demuxer->audio, dp);
1989     }
1990   else if (track->a_formattag == mmioFOURCC('W', 'V', 'P', 'K') ||
1991            track->a_formattag == mmioFOURCC('T', 'R', 'H', 'D'))
1992     {  /* do nothing, still works */  }
1993   else if (!track->ms_compat || (track->private_size < sizeof(WAVEFORMATEX)))
1994     {
1995       free_sh_audio(demuxer, track->tnum);
1996       return 1;
1997     }
1998
1999   return 0;
2000 }
2001
2002 static int
2003 demux_mkv_open_sub (demuxer_t *demuxer, mkv_track_t *track, int sid)
2004 {
2005   if (track->subtitle_type != MATROSKA_SUBTYPE_UNKNOWN)
2006     {
2007       int size, m;
2008       uint8_t *buffer;
2009       sh_sub_t *sh = new_sh_sub_sid(demuxer, track->tnum, sid);
2010       track->sh_sub = sh;
2011       sh->type = 't';
2012       if (track->subtitle_type == MATROSKA_SUBTYPE_VOBSUB)
2013         sh->type = 'v';
2014       if (track->subtitle_type == MATROSKA_SUBTYPE_SSA)
2015         sh->type = 'a';
2016       size = track->private_size;
2017       m = demux_mkv_decode (track,track->private_data,&buffer,&size,2);
2018       if (buffer && m)
2019         {
2020           free (track->private_data);
2021           track->private_data = buffer;
2022           track->private_size = size;
2023         }
2024       sh->extradata=malloc(track->private_size);
2025       memcpy (sh->extradata, track->private_data,
2026               track->private_size);
2027       sh->extradata_len = track->private_size;
2028       if (track->language && (strcmp(track->language, "und") != 0))
2029         sh->lang = strdup(track->language);
2030       sh->default_track = track->default_track;
2031     }
2032   else
2033     {
2034       mp_msg (MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_MKV_SubtitleTypeNotSupported,
2035               track->codec_id);
2036       return 1;
2037     }
2038
2039   return 0;
2040 }
2041
2042 static void demux_mkv_seek (demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags);
2043
2044 static int
2045 demux_mkv_open (demuxer_t *demuxer)
2046 {
2047   stream_t *s = demuxer->stream;
2048   mkv_demuxer_t *mkv_d;
2049   mkv_track_t *track;
2050   int i, version, cont = 0;
2051   char *str;
2052
2053   stream_seek(s, s->start_pos);
2054   str = ebml_read_header (s, &version);
2055   if (str == NULL || strcmp (str, "matroska") || version > 2)
2056     {
2057       mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] no head found\n");
2058       return 0;
2059     }
2060   free (str);
2061
2062   mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] Found the head...\n");
2063
2064   if (ebml_read_id (s, NULL) != MATROSKA_ID_SEGMENT)
2065     {
2066       mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n");
2067       return 0;
2068     }
2069   ebml_read_length (s, NULL);  /* return bytes number until EOF */
2070
2071   mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] + a segment...\n");
2072
2073   mkv_d = calloc (1, sizeof (mkv_demuxer_t));
2074   demuxer->priv = mkv_d;
2075   mkv_d->tc_scale = 1000000;
2076   mkv_d->segment_start = stream_tell (s);
2077   mkv_d->parsed_cues = malloc (sizeof (off_t));
2078   mkv_d->parsed_seekhead = malloc (sizeof (off_t));
2079
2080   while (!cont)
2081     {
2082       switch (ebml_read_id (s, NULL))
2083         {
2084         case MATROSKA_ID_INFO:
2085           mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ segment information...\n");
2086           cont = demux_mkv_read_info (demuxer);
2087           break;
2088
2089         case MATROSKA_ID_TRACKS:
2090           mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ segment tracks...\n");
2091           cont = demux_mkv_read_tracks (demuxer);
2092           break;
2093
2094         case MATROSKA_ID_CUES:
2095           cont = demux_mkv_read_cues (demuxer);
2096           break;
2097
2098         case MATROSKA_ID_TAGS:
2099           cont = demux_mkv_read_tags (demuxer);
2100           break;
2101
2102         case MATROSKA_ID_SEEKHEAD:
2103           cont = demux_mkv_read_seekhead (demuxer);
2104           break;
2105
2106         case MATROSKA_ID_CHAPTERS:
2107           cont = demux_mkv_read_chapters (demuxer);
2108           break;
2109
2110         case MATROSKA_ID_ATTACHMENTS:
2111           cont = demux_mkv_read_attachments (demuxer);
2112           break;
2113
2114         case MATROSKA_ID_CLUSTER:
2115           {
2116             int p, l;
2117             mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ found cluster, headers are "
2118                     "parsed completely :)\n");
2119             /* get the first cluster timecode */
2120             p = stream_tell(s);
2121             l = ebml_read_length (s, NULL);
2122             while (ebml_read_id (s, NULL) != MATROSKA_ID_CLUSTERTIMECODE)
2123               {
2124                 ebml_read_skip (s, NULL);
2125                 if (stream_tell (s) >= p + l)
2126                   break;
2127               }
2128             if (stream_tell (s) < p + l)
2129               {
2130                 uint64_t num = ebml_read_uint (s, NULL);
2131                 if (num == EBML_UINT_INVALID)
2132                   return 0;
2133                 mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0;
2134                 mkv_d->has_first_tc = 1;
2135               }
2136             stream_seek (s, p - 4);
2137             cont = 1;
2138             break;
2139           }
2140
2141         default:
2142           cont = 1;
2143         case EBML_ID_VOID:
2144           ebml_read_skip (s, NULL);
2145           break;
2146         }
2147     }
2148
2149   display_create_tracks (demuxer);
2150
2151   /* select video track */
2152   track = NULL;
2153   if (demuxer->video->id == -1)  /* automatically select a video track */
2154     {
2155       /* search for a video track that has the 'default' flag set */
2156       for (i=0; i<mkv_d->num_tracks; i++)
2157         if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO
2158             && mkv_d->tracks[i]->default_track)
2159           {
2160             track = mkv_d->tracks[i];
2161             break;
2162           }
2163
2164       if (track == NULL)
2165         /* no track has the 'default' flag set */
2166         /* let's take the first video track */
2167         for (i=0; i<mkv_d->num_tracks; i++)
2168           if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO)
2169             {
2170               track = mkv_d->tracks[i];
2171               break;
2172             }
2173     }
2174   else if (demuxer->video->id != -2)  /* -2 = no video at all */
2175     track = demux_mkv_find_track_by_num (mkv_d, demuxer->video->id,
2176                                          MATROSKA_TRACK_VIDEO);
2177
2178   if (track && demuxer->v_streams[track->tnum])
2179               {
2180                 mp_msg (MSGT_DEMUX, MSGL_INFO,
2181                         MSGTR_MPDEMUX_MKV_WillPlayVideoTrack, track->tnum);
2182                 demuxer->video->id = track->tnum;
2183                 demuxer->video->sh = demuxer->v_streams[track->tnum];
2184               }
2185   else
2186     {
2187       mp_msg (MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoVideoTrackFound);
2188       demuxer->video->id = -2;
2189     }
2190
2191   /* select audio track */
2192   track = NULL;
2193   if (track == NULL)
2194     /* search for an audio track that has the 'default' flag set */
2195     for (i=0; i < mkv_d->num_tracks; i++)
2196       if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO
2197           && mkv_d->tracks[i]->default_track)
2198         {
2199           track = mkv_d->tracks[i];
2200           break;
2201         }
2202
2203   if (track == NULL)
2204     /* no track has the 'default' flag set */
2205     /* let's take the first audio track */
2206     for (i=0; i < mkv_d->num_tracks; i++)
2207       if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO)
2208         {
2209           track = mkv_d->tracks[i];
2210           break;
2211         }
2212
2213   if (track && demuxer->a_streams[track->tnum])
2214     {
2215       demuxer->audio->id = track->tnum;
2216       demuxer->audio->sh = demuxer->a_streams[track->tnum];
2217     }
2218   else
2219     {
2220       mp_msg (MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoAudioTrackFound);
2221       demuxer->audio->id = -2;
2222     }
2223
2224
2225   if(demuxer->audio->id != -2)
2226   for (i=0; i < mkv_d->num_tracks; i++)
2227     {
2228       if(mkv_d->tracks[i]->type != MATROSKA_TRACK_AUDIO)
2229           continue;
2230       if(demuxer->a_streams[track->tnum])
2231         {
2232           mkv_d->last_aid++;
2233           if(mkv_d->last_aid == MAX_A_STREAMS)
2234             break;
2235         }
2236     }
2237
2238   if (demuxer->chapters)
2239     {
2240       for (i=0; i < (int)demuxer->num_chapters; i++)
2241         {
2242           demuxer->chapters[i].start -= mkv_d->first_tc;
2243           demuxer->chapters[i].end -= mkv_d->first_tc;
2244         }
2245       if (dvd_last_chapter > 0 && dvd_last_chapter <= demuxer->num_chapters)
2246         {
2247           if (demuxer->chapters[dvd_last_chapter-1].end != 0)
2248             mkv_d->stop_timecode = demuxer->chapters[dvd_last_chapter-1].end;
2249           else if (dvd_last_chapter + 1 <= demuxer->num_chapters)
2250             mkv_d->stop_timecode = demuxer->chapters[dvd_last_chapter].start;
2251         }
2252     }
2253
2254   if (s->end_pos == 0 || (mkv_d->indexes == NULL && index_mode < 0))
2255     demuxer->seekable = 0;
2256   else
2257     {
2258       demuxer->movi_start = s->start_pos;
2259       demuxer->movi_end = s->end_pos;
2260       demuxer->seekable = 1;
2261     }
2262
2263   return DEMUXER_TYPE_MATROSKA;
2264 }
2265
2266 static void
2267 demux_close_mkv (demuxer_t *demuxer)
2268 {
2269   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2270
2271   if (mkv_d)
2272     {
2273       int i;
2274       free_cached_dps (demuxer);
2275       if (mkv_d->tracks)
2276         {
2277           for (i=0; i<mkv_d->num_tracks; i++)
2278             demux_mkv_free_trackentry(mkv_d->tracks[i]);
2279           free (mkv_d->tracks);
2280         }
2281       free (mkv_d->indexes);
2282       free (mkv_d->cluster_positions);
2283       free (mkv_d->parsed_cues);
2284       free (mkv_d->parsed_seekhead);
2285       free (mkv_d);
2286     }
2287 }
2288
2289 static int
2290 demux_mkv_read_block_lacing (uint8_t *buffer, uint64_t *size,
2291                              uint8_t *laces, uint32_t **all_lace_sizes)
2292 {
2293   uint32_t total = 0, *lace_size;
2294   uint8_t flags;
2295   int i;
2296
2297   *all_lace_sizes = NULL;
2298   lace_size = NULL;
2299   /* lacing flags */
2300   flags = *buffer++;
2301   (*size)--;
2302
2303   switch ((flags & 0x06) >> 1)
2304     {
2305     case 0:  /* no lacing */
2306       *laces = 1;
2307       lace_size = calloc(*laces, sizeof(uint32_t));
2308       lace_size[0] = *size;
2309       break;
2310
2311     case 1:  /* xiph lacing */
2312     case 2:  /* fixed-size lacing */
2313     case 3:  /* EBML lacing */
2314       *laces = *buffer++;
2315       (*size)--;
2316       (*laces)++;
2317       lace_size = calloc(*laces, sizeof(uint32_t));
2318
2319       switch ((flags & 0x06) >> 1)
2320         {
2321         case 1:  /* xiph lacing */
2322           for (i=0; i < *laces-1; i++)
2323             {
2324               lace_size[i] = 0;
2325               do
2326                 {
2327                   lace_size[i] += *buffer;
2328                   (*size)--;
2329                 } while (*buffer++ == 0xFF);
2330               total += lace_size[i];
2331             }
2332           lace_size[i] = *size - total;
2333           break;
2334
2335         case 2:  /* fixed-size lacing */
2336           for (i=0; i < *laces; i++)
2337             lace_size[i] = *size / *laces;
2338           break;
2339
2340         case 3:  /* EBML lacing */
2341           {
2342             int l;
2343             uint64_t num = ebml_read_vlen_uint (buffer, &l);
2344             if (num == EBML_UINT_INVALID) {
2345               free(lace_size);
2346               return 1;
2347             }
2348             buffer += l;
2349             *size -= l;
2350
2351             total = lace_size[0] = num;
2352             for (i=1; i < *laces-1; i++)
2353               {
2354                 int64_t snum;
2355                 snum = ebml_read_vlen_int (buffer, &l);
2356                 if (snum == EBML_INT_INVALID) {
2357                   free(lace_size);
2358                   return 1;
2359                 }
2360                 buffer += l;
2361                 *size -= l;
2362                 lace_size[i] = lace_size[i-1] + snum;
2363                 total += lace_size[i];
2364               }
2365             lace_size[i] = *size - total;
2366             break;
2367           }
2368         }
2369       break;
2370     }
2371   *all_lace_sizes = lace_size;
2372   return 0;
2373 }
2374
2375 static void
2376 handle_subtitles(demuxer_t *demuxer, mkv_track_t *track, char *block,
2377                  int64_t size, uint64_t block_duration, uint64_t timecode)
2378 {
2379   demux_packet_t *dp;
2380
2381   if (block_duration == 0)
2382     {
2383       mp_msg (MSGT_DEMUX, MSGL_WARN,
2384               MSGTR_MPDEMUX_MKV_NoBlockDurationForSubtitleTrackFound);
2385       return;
2386     }
2387
2388   sub_utf8 = 1;
2389   dp = new_demux_packet(size);
2390   memcpy(dp->buffer, block, size);
2391   dp->pts = timecode / 1000.0f;
2392   dp->endpts = (timecode + block_duration) / 1000.0f;
2393   ds_add_packet(demuxer->sub, dp);
2394 }
2395
2396 double real_fix_timestamp(unsigned char *buf, unsigned int timestamp, unsigned int format, int64_t *kf_base, int *kf_pts, double *pts);
2397
2398 static void
2399 handle_realvideo (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
2400                   uint32_t size, int block_bref)
2401 {
2402   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2403   demux_packet_t *dp;
2404   uint32_t timestamp = mkv_d->last_pts * 1000;
2405
2406   dp = new_demux_packet (size);
2407   memcpy (dp->buffer, buffer, size);
2408
2409   if (mkv_d->v_skip_to_keyframe)
2410     {
2411       dp->pts = mkv_d->last_pts;
2412       track->rv_kf_base = 0;
2413       track->rv_kf_pts = timestamp;
2414     }
2415   else
2416     dp->pts = real_fix_timestamp (dp->buffer, timestamp,
2417                                   ((sh_video_t*)demuxer->video->sh)->bih->biCompression,
2418                                   &track->rv_kf_base, &track->rv_kf_pts, NULL);
2419   dp->pos = demuxer->filepos;
2420   dp->flags = block_bref ? 0 : 0x10;
2421
2422   ds_add_packet(demuxer->video, dp);
2423 }
2424
2425 static void
2426 handle_realaudio (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
2427                   uint32_t size, int block_bref)
2428 {
2429   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2430   int sps = track->sub_packet_size;
2431   int sph = track->sub_packet_h;
2432   int cfs = track->coded_framesize;
2433   int w = track->audiopk_size;
2434   int spc = track->sub_packet_cnt;
2435   demux_packet_t *dp;
2436   int x;
2437
2438   if ((track->a_formattag == mmioFOURCC('2', '8', '_', '8')) ||
2439       (track->a_formattag == mmioFOURCC('c', 'o', 'o', 'k')) ||
2440       (track->a_formattag == mmioFOURCC('a', 't', 'r', 'c')) ||
2441       (track->a_formattag == mmioFOURCC('s', 'i', 'p', 'r')))
2442     {
2443 //      if(!block_bref)
2444 //        spc = track->sub_packet_cnt = 0;
2445       switch (track->a_formattag) {
2446         case mmioFOURCC('2', '8', '_', '8'):
2447           for (x = 0; x < sph / 2; x++)
2448             memcpy(track->audio_buf + x * 2 * w + spc * cfs, buffer + cfs * x, cfs);
2449           break;
2450         case mmioFOURCC('c', 'o', 'o', 'k'):
2451         case mmioFOURCC('a', 't', 'r', 'c'):
2452           for (x = 0; x < w / sps; x++)
2453             memcpy(track->audio_buf + sps * (sph * x + ((sph + 1) / 2) * (spc & 1) + (spc >> 1)), buffer + sps * x, sps);
2454           break;
2455         case mmioFOURCC('s', 'i', 'p', 'r'):
2456           memcpy(track->audio_buf + spc * w, buffer, w);
2457           if (spc == sph - 1)
2458             {
2459               int n;
2460               int bs = sph * w * 2 / 96;  // nibbles per subpacket
2461               // Perform reordering
2462               for(n=0; n < 38; n++)
2463                 {
2464                   int j;
2465                   int i = bs * sipr_swaps[n][0];
2466                   int o = bs * sipr_swaps[n][1];
2467                   // swap nibbles of block 'i' with 'o'      TODO: optimize
2468                   for(j = 0;j < bs; j++)
2469                     {
2470                       int x = (i & 1) ? (track->audio_buf[i >> 1] >> 4) : (track->audio_buf[i >> 1] & 0x0F);
2471                       int y = (o & 1) ? (track->audio_buf[o >> 1] >> 4) : (track->audio_buf[o >> 1] & 0x0F);
2472                       if(o & 1)
2473                         track->audio_buf[o >> 1] = (track->audio_buf[o >> 1] & 0x0F) | (x << 4);
2474                       else
2475                         track->audio_buf[o >> 1] = (track->audio_buf[o >> 1] & 0xF0) | x;
2476                       if(i & 1)
2477                         track->audio_buf[i >> 1] = (track->audio_buf[i >> 1] & 0x0F) | (y << 4);
2478                       else
2479                         track->audio_buf[i >> 1] = (track->audio_buf[i >> 1] & 0xF0) | y;
2480                       ++i; ++o;
2481                     }
2482                 }
2483             }
2484           break;
2485       }
2486       track->audio_timestamp[track->sub_packet_cnt] = (track->ra_pts == mkv_d->last_pts) ? 0 : (mkv_d->last_pts);
2487       track->ra_pts = mkv_d->last_pts;
2488       if (track->sub_packet_cnt == 0)
2489         track->audio_filepos = demuxer->filepos;
2490       if (++(track->sub_packet_cnt) == sph)
2491         {
2492            int apk_usize = ((WAVEFORMATEX*)((sh_audio_t*)demuxer->audio->sh)->wf)->nBlockAlign;
2493            track->sub_packet_cnt = 0;
2494            // Release all the audio packets
2495            for (x = 0; x < sph*w/apk_usize; x++)
2496              {
2497                dp = new_demux_packet(apk_usize);
2498                memcpy(dp->buffer, track->audio_buf + x * apk_usize, apk_usize);
2499                /* Put timestamp only on packets that correspond to original audio packets in file */
2500                dp->pts = (x * apk_usize % w) ? 0 : track->audio_timestamp[x * apk_usize / w];
2501                dp->pos = track->audio_filepos; // all equal
2502                dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
2503                ds_add_packet(demuxer->audio, dp);
2504              }
2505         }
2506    } else { // Not a codec that require reordering
2507   dp = new_demux_packet (size);
2508   memcpy(dp->buffer, buffer, size);
2509   if (track->ra_pts == mkv_d->last_pts && !mkv_d->a_skip_to_keyframe)
2510     dp->pts = 0;
2511   else
2512     dp->pts = mkv_d->last_pts;
2513   track->ra_pts = mkv_d->last_pts;
2514
2515   dp->pos = demuxer->filepos;
2516   dp->flags = block_bref ? 0 : 0x10;
2517   ds_add_packet (demuxer->audio, dp);
2518   }
2519 }
2520
2521 /** Reorder timecodes and add cached demux packets to the queues.
2522  *
2523  * Timecode reordering is needed if a video track contains B frames that
2524  * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2525  * MPlayer doesn't like timestamps in display order. This function adjusts
2526  * the timestamp of cached frames (which are exactly one I/P frame followed
2527  * by one or more B frames) so that they are in coding order again.
2528  *
2529  * Example: The track with 25 FPS contains four frames with the timecodes
2530  * I at 0ms, P at 120ms, B at 40ms and B at 80ms. As soon as the next I
2531  * or P frame arrives these timecodes can be changed to I at 0ms, P at 40ms,
2532  * B at 80ms and B at 120ms.
2533  *
2534  * This works for simple H.264 B-frame pyramids, but not for arbitrary orders.
2535  *
2536  * \param demuxer The Matroska demuxer struct for this instance.
2537  * \param track The track structure whose cache should be handled.
2538  */
2539 static void
2540 flush_cached_dps (demuxer_t *demuxer, mkv_track_t *track)
2541 {
2542   int i, ok;
2543
2544   if (track->num_cached_dps == 0)
2545     return;
2546
2547   do {
2548     ok = 1;
2549     for (i = 1; i < track->num_cached_dps; i++)
2550       if (track->cached_dps[i - 1]->pts > track->cached_dps[i]->pts) {
2551         float tmp_pts = track->cached_dps[i - 1]->pts;
2552         track->cached_dps[i - 1]->pts = track->cached_dps[i]->pts;
2553         track->cached_dps[i]->pts = tmp_pts;
2554         ok = 0;
2555       }
2556   } while (!ok);
2557
2558   for (i = 0; i < track->num_cached_dps; i++)
2559     ds_add_packet (demuxer->video, track->cached_dps[i]);
2560   track->num_cached_dps = 0;
2561 }
2562
2563 /** Cache video frames if timecodes have to be reordered.
2564  *
2565  * Timecode reordering is needed if a video track contains B frames that
2566  * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2567  * This function takes in a Matroska block read from the file, allocates a
2568  * demux packet for it, fills in its values, allocates space for storing
2569  * pointers to the cached demux packets and adds the packet to it. If
2570  * the packet contains an I or a P frame then ::flush_cached_dps is called
2571  * in order to send the old cached frames downstream.
2572  *
2573  * \param demuxer The Matroska demuxer struct for this instance.
2574  * \param track The packet is meant for this track.
2575  * \param buffer The actual frame contents.
2576  * \param size The frame size in bytes.
2577  * \param block_bref A relative timecode (backward reference). If it is \c 0
2578  *   then the frame is an I frame.
2579  * \param block_fref A relative timecode (forward reference). If it is \c 0
2580  *   then the frame is either an I frame or a P frame depending on the value
2581  *   of \a block_bref. Otherwise it's a B frame.
2582  */
2583 static void
2584 handle_video_bframes (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
2585                       uint32_t size, int block_bref, int block_fref)
2586 {
2587   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2588   demux_packet_t *dp;
2589
2590   dp = new_demux_packet (size);
2591   memcpy(dp->buffer, buffer, size);
2592   dp->pos = demuxer->filepos;
2593   dp->pts = mkv_d->last_pts;
2594   if ((track->num_cached_dps > 0) && (dp->pts < track->max_pts))
2595     block_fref = 1;
2596   if (block_fref == 0)          /* I or P frame */
2597     flush_cached_dps (demuxer, track);
2598   if (block_bref != 0)          /* I frame, don't cache it */
2599     dp->flags = 0x10;
2600   if ((track->num_cached_dps + 1) > track->num_allocated_dps)
2601     {
2602       track->cached_dps = (demux_packet_t **)
2603         realloc(track->cached_dps, (track->num_cached_dps + 10) *
2604                 sizeof(demux_packet_t *));
2605       track->num_allocated_dps += 10;
2606     }
2607   track->cached_dps[track->num_cached_dps] = dp;
2608   track->num_cached_dps++;
2609   if (dp->pts > track->max_pts)
2610     track->max_pts = dp->pts;
2611 }
2612
2613 static int
2614 handle_block (demuxer_t *demuxer, uint8_t *block, uint64_t length,
2615               uint64_t block_duration, int64_t block_bref, int64_t block_fref, uint8_t simpleblock)
2616 {
2617   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2618   mkv_track_t *track = NULL;
2619   demux_stream_t *ds = NULL;
2620   uint64_t old_length;
2621   int64_t tc;
2622   uint32_t *lace_size;
2623   uint8_t laces, flags;
2624   int i, num, tmp, use_this_block = 1;
2625   float current_pts;
2626   int16_t time;
2627
2628   /* first byte(s): track num */
2629   num = ebml_read_vlen_uint (block, &tmp);
2630   block += tmp;
2631   /* time (relative to cluster time) */
2632   time = block[0] << 8 | block[1];
2633   block += 2;
2634   length -= tmp + 2;
2635   old_length = length;
2636   flags = block[0];
2637   if (demux_mkv_read_block_lacing (block, &length, &laces, &lace_size))
2638     return 0;
2639   block += old_length - length;
2640
2641   tc = ((time*mkv_d->tc_scale+mkv_d->cluster_tc) /1000000.0 - mkv_d->first_tc);
2642   if (tc < 0)
2643     tc = 0;
2644   if (mkv_d->stop_timecode > 0 && tc > mkv_d->stop_timecode) {
2645     free(lace_size);
2646     return -1;
2647   }
2648   current_pts = tc / 1000.0;
2649
2650   for (i=0; i<mkv_d->num_tracks; i++)
2651     if (mkv_d->tracks[i]->tnum == num) {
2652       track = mkv_d->tracks[i];
2653       break;
2654     }
2655   if (track == NULL)
2656     {
2657       free(lace_size);
2658       return 1;
2659     }
2660   if (num == demuxer->audio->id)
2661     {
2662       ds = demuxer->audio;
2663
2664       if (mkv_d->a_skip_to_keyframe)
2665         {
2666           if (simpleblock)
2667             {
2668                if (!(flags&0x80))   /*current frame isn't a keyframe*/
2669                  use_this_block = 0;
2670             }
2671           else if (block_bref != 0)
2672             use_this_block = 0;
2673         }
2674       else if (mkv_d->v_skip_to_keyframe)
2675         use_this_block = 0;
2676
2677       if (track->fix_i_bps && use_this_block)
2678         {
2679           sh_audio_t *sh = (sh_audio_t *) ds->sh;
2680
2681           if (block_duration != 0)
2682             {
2683               sh->i_bps = length * 1000 / block_duration;
2684               track->fix_i_bps = 0;
2685             }
2686           else if (track->qt_last_a_pts == 0.0)
2687             track->qt_last_a_pts = current_pts;
2688           else if(track->qt_last_a_pts != current_pts)
2689             {
2690               sh->i_bps = length / (current_pts - track->qt_last_a_pts);
2691               track->fix_i_bps = 0;
2692             }
2693         }
2694     }
2695   else if (tc < mkv_d->skip_to_timecode)
2696     use_this_block = 0;
2697   else if (num == demuxer->video->id)
2698     {
2699       ds = demuxer->video;
2700       if (mkv_d->v_skip_to_keyframe)
2701         {
2702           if (simpleblock)
2703             {
2704               if (!(flags&0x80))   /*current frame isn't a keyframe*/
2705                 use_this_block = 0;
2706             }
2707           else if (block_bref != 0 || block_fref != 0)
2708             use_this_block = 0;
2709         }
2710     }
2711   else if (num == demuxer->sub->id)
2712     {
2713       ds = demuxer->sub;
2714       if (track->subtitle_type != MATROSKA_SUBTYPE_VOBSUB)
2715         {
2716           if (!mkv_d->v_skip_to_keyframe)
2717             handle_subtitles (demuxer, track, block, length,
2718                               block_duration, tc);
2719           use_this_block = 0;
2720         }
2721     }
2722   else
2723     use_this_block = 0;
2724
2725   if (use_this_block)
2726     {
2727       mkv_d->last_pts = current_pts;
2728       mkv_d->last_filepos = demuxer->filepos;
2729
2730       for (i=0; i < laces; i++)
2731         {
2732           if (ds == demuxer->video && track->realmedia)
2733             handle_realvideo (demuxer, track, block, lace_size[i], block_bref);
2734           else if (ds == demuxer->audio && track->realmedia)
2735             handle_realaudio (demuxer, track, block, lace_size[i], block_bref);
2736           else if (ds == demuxer->video && track->reorder_timecodes)
2737             handle_video_bframes (demuxer, track, block, lace_size[i],
2738                                   block_bref, block_fref);
2739           else
2740             {
2741               int modified, size = lace_size[i];
2742               demux_packet_t *dp;
2743               uint8_t *buffer;
2744               modified = demux_mkv_decode (track, block, &buffer, &size, 1);
2745               if (buffer)
2746                 {
2747                   dp = new_demux_packet (size);
2748                   memcpy (dp->buffer, buffer, size);
2749                   if (modified)
2750                     free (buffer);
2751                   dp->flags = (block_bref == 0 && block_fref == 0) ? 0x10 : 0;
2752                   /* If default_duration is 0, assume no pts value is known
2753                    * for packets after the first one (rather than all pts
2754                    * values being the same) */
2755                   if (i == 0 || track->default_duration)
2756                   dp->pts = mkv_d->last_pts + i * track->default_duration;
2757                   ds_add_packet (ds, dp);
2758                 }
2759             }
2760           block += lace_size[i];
2761         }
2762
2763       if (ds == demuxer->video)
2764         {
2765           mkv_d->v_skip_to_keyframe = 0;
2766           mkv_d->skip_to_timecode = 0;
2767         }
2768       else if (ds == demuxer->audio)
2769         mkv_d->a_skip_to_keyframe = 0;
2770
2771       free(lace_size);
2772       return 1;
2773     }
2774
2775   free(lace_size);
2776   return 0;
2777 }
2778
2779 static int
2780 demux_mkv_fill_buffer (demuxer_t *demuxer, demux_stream_t *ds)
2781 {
2782   mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2783   stream_t *s = demuxer->stream;
2784   uint64_t l;
2785   int il, tmp;
2786
2787   while (1)
2788     {
2789       while (mkv_d->cluster_size > 0)
2790         {
2791           uint64_t block_duration = 0,  block_length = 0;
2792           int64_t block_bref = 0, block_fref = 0;
2793           uint8_t *block = NULL;
2794
2795           while (mkv_d->blockgroup_size > 0)
2796             {
2797               switch (ebml_read_id (s, &il))
2798                 {
2799                 case MATROSKA_ID_BLOCKDURATION:
2800                   {
2801                     block_duration = ebml_read_uint (s, &l);
2802                     if (block_duration == EBML_UINT_INVALID) {
2803                       free(block);
2804                       return 0;
2805                     }
2806                     block_duration *= mkv_d->tc_scale / 1000000.0;
2807                     break;
2808                   }
2809
2810                 case MATROSKA_ID_BLOCK:
2811                   block_length = ebml_read_length (s, &tmp);
2812                   free(block);
2813                   if (block_length > SIZE_MAX - AV_LZO_INPUT_PADDING) return 0;
2814                   block = malloc (block_length + AV_LZO_INPUT_PADDING);
2815                   demuxer->filepos = stream_tell (s);
2816                   if (stream_read (s,block,block_length) != (int) block_length)
2817                   {
2818                     free(block);
2819                     return 0;
2820                   }
2821                   l = tmp + block_length;
2822                   break;
2823
2824                 case MATROSKA_ID_REFERENCEBLOCK:
2825                   {
2826                     int64_t num = ebml_read_int (s, &l);
2827                     if (num == EBML_INT_INVALID) {
2828                       free(block);
2829                       return 0;
2830                     }
2831                     if (num <= 0)
2832                       block_bref = num;
2833                     else
2834                       block_fref = num;
2835                     break;
2836                   }
2837
2838                 case EBML_ID_INVALID:
2839                   free(block);
2840                   return 0;
2841
2842                 default:
2843                   ebml_read_skip (s, &l);
2844                   break;
2845                 }
2846               mkv_d->blockgroup_size -= l + il;
2847               mkv_d->cluster_size -= l + il;
2848             }
2849
2850           if (block)
2851             {
2852               int res = handle_block (demuxer, block, block_length,
2853                                       block_duration, block_bref, block_fref, 0);
2854               free (block);
2855               if (res < 0)
2856                 return 0;
2857               if (res)
2858                 return 1;
2859             }
2860
2861           if (mkv_d->cluster_size > 0)
2862             {
2863               switch (ebml_read_id (s, &il))
2864                 {
2865                 case MATROSKA_ID_CLUSTERTIMECODE:
2866                   {
2867                     uint64_t num = ebml_read_uint (s, &l);
2868                     if (num == EBML_UINT_INVALID)
2869                       return 0;
2870                     if (!mkv_d->has_first_tc)
2871                       {
2872                         mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0;
2873                         mkv_d->has_first_tc = 1;
2874                       }
2875                     mkv_d->cluster_tc = num * mkv_d->tc_scale;
2876                     break;
2877                   }
2878
2879                 case MATROSKA_ID_BLOCKGROUP:
2880                   mkv_d->blockgroup_size = ebml_read_length (s, &tmp);
2881                   l = tmp;
2882                   break;
2883
2884                 case MATROSKA_ID_SIMPLEBLOCK:
2885                   {
2886                     int res;
2887                     block_length = ebml_read_length (s, &tmp);
2888                     block = malloc (block_length);
2889                     demuxer->filepos = stream_tell (s);
2890                     if (stream_read (s,block,block_length) != (int) block_length)
2891                     {
2892                       free(block);
2893                       return 0;
2894                     }
2895                     l = tmp + block_length;
2896                     res = handle_block (demuxer, block, block_length,
2897                                         block_duration, block_bref, block_fref, 1);
2898                     free (block);
2899                     mkv_d->cluster_size -= l + il;
2900                     if (res < 0)
2901                       return 0;
2902                     else if (res)
2903                       return 1;
2904                     else mkv_d->cluster_size += l + il;
2905                     break;
2906                   }
2907                 case EBML_ID_INVALID:
2908                   return 0;
2909
2910                 default:
2911                   ebml_read_skip (s, &l);
2912                   break;
2913                 }
2914               mkv_d->cluster_size -= l + il;
2915             }
2916         }
2917
2918       if (ebml_read_id (s, &il) != MATROSKA_ID_CLUSTER)
2919         return 0;
2920       add_cluster_position(mkv_d, stream_tell(s)-il);
2921       mkv_d->cluster_size = ebml_read_length (s, NULL);
2922     }
2923
2924   return 0;
2925 }
2926
2927 static void
2928 demux_mkv_seek (demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
2929 {
2930   free_cached_dps (demuxer);
2931   if (!(flags & SEEK_FACTOR))  /* time in secs */
2932     {
2933       mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2934       stream_t *s = demuxer->stream;
2935       int64_t target_timecode = 0, diff, min_diff=0xFFFFFFFFFFFFFFFLL;
2936       int i;
2937
2938       if (!(flags & SEEK_ABSOLUTE))  /* relative seek */
2939         target_timecode = (int64_t) (mkv_d->last_pts * 1000.0);
2940       target_timecode += (int64_t)(rel_seek_secs * 1000.0);
2941       if (target_timecode < 0)
2942         target_timecode = 0;
2943
2944       if (mkv_d->indexes == NULL)  /* no index was found */
2945         {
2946           uint64_t target_filepos, cluster_pos, max_pos;
2947
2948           target_filepos = (uint64_t) (target_timecode * mkv_d->last_filepos
2949                                        / (mkv_d->last_pts * 1000.0));
2950
2951           max_pos = mkv_d->num_cluster_pos ? mkv_d->cluster_positions[mkv_d->num_cluster_pos-1] : 0;
2952           if (target_filepos > max_pos)
2953             {
2954               if ((off_t) max_pos > stream_tell (s))
2955                 stream_seek (s, max_pos);
2956               else
2957                 stream_seek (s, stream_tell (s) + mkv_d->cluster_size);
2958               /* parse all the clusters upto target_filepos */
2959               while (!s->eof && stream_tell(s) < (off_t) target_filepos)
2960                 {
2961                   switch (ebml_read_id (s, &i))
2962                     {
2963                     case MATROSKA_ID_CLUSTER:
2964                       add_cluster_position(mkv_d, (uint64_t) stream_tell(s)-i);
2965                       break;
2966
2967                     case MATROSKA_ID_CUES:
2968                       demux_mkv_read_cues (demuxer);
2969                       break;
2970                     }
2971                   ebml_read_skip (s, NULL);
2972                 }
2973               if (s->eof)
2974                 stream_reset(s);
2975             }
2976
2977           if (mkv_d->indexes == NULL)
2978             {
2979               cluster_pos = mkv_d->cluster_positions[0];
2980               /* Let's find the nearest cluster */
2981               for (i=0; i < mkv_d->num_cluster_pos; i++)
2982                 {
2983                   diff = mkv_d->cluster_positions[i] - target_filepos;
2984                   if (rel_seek_secs < 0 && diff < 0 && -diff < min_diff)
2985                     {
2986                       cluster_pos = mkv_d->cluster_positions[i];
2987                       min_diff = -diff;
2988                     }
2989                   else if (rel_seek_secs > 0
2990                            && (diff < 0 ? -1 * diff : diff) < min_diff)
2991                     {
2992                       cluster_pos = mkv_d->cluster_positions[i];
2993                       min_diff = diff < 0 ? -1 * diff : diff;
2994                     }
2995                 }
2996               mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
2997               stream_seek (s, cluster_pos);
2998             }
2999         }
3000       else
3001         {
3002           mkv_index_t *index = NULL;
3003           int seek_id = (demuxer->video->id < 0) ? demuxer->audio->id : demuxer->video->id;
3004
3005           /* let's find the entry in the indexes with the smallest */
3006           /* difference to the wanted timecode. */
3007           for (i=0; i < mkv_d->num_indexes; i++)
3008             if (mkv_d->indexes[i].tnum == seek_id)
3009               {
3010                 diff = target_timecode + mkv_d->first_tc -
3011                        (int64_t) mkv_d->indexes[i].timecode * mkv_d->tc_scale / 1000000.0;
3012
3013                 if ((flags & SEEK_ABSOLUTE || target_timecode <= mkv_d->last_pts*1000)) {
3014                     // Absolute seek or seek backward: find the last index
3015                     // position before target time
3016                     if (diff < 0 || diff >= min_diff)
3017                         continue;
3018                 }
3019                 else {
3020                     // Relative seek forward: find the first index position
3021                     // after target time. If no such index exists, find last
3022                     // position between current position and target time.
3023                     if (diff <= 0) {
3024                         if (min_diff <= 0 && diff <= min_diff)
3025                             continue;