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