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