demux_mkv: support Blu-ray subtitles
[mplayer/greg.git] / libmpdemux / demux_mkv.c
blobe22c0b2eb1743beb94550d4168a13b0f309262ea
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.
7 * This file is part of MPlayer.
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.
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.
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.
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <inttypes.h>
30 #include <stdbool.h>
32 #include "talloc.h"
33 #include "options.h"
34 #include "bstr.h"
35 #include "stream/stream.h"
36 #include "demuxer.h"
37 #include "stheader.h"
38 #include "ebml.h"
39 #include "matroska.h"
40 #include "demux_real.h"
42 #include "mp_msg.h"
44 #include "sub/sub.h"
46 #include "libavutil/common.h"
48 #ifdef CONFIG_QTX_CODECS
49 #include "loader/qtx/qtxsdk/components.h"
50 #endif
52 #if CONFIG_ZLIB
53 #include <zlib.h>
54 #endif
56 #include "libavutil/lzo.h"
57 #include "ffmpeg_files/intreadwrite.h"
58 #include "libavutil/avstring.h"
60 static const unsigned char sipr_swaps[38][2] = {
61 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
62 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
63 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
64 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
65 {77,80}
68 // Map flavour to bytes per second
69 #define SIPR_FLAVORS 4
70 #define ATRC_FLAVORS 8
71 #define COOK_FLAVORS 34
72 static const int sipr_fl2bps[SIPR_FLAVORS] = { 813, 1062, 625, 2000 };
73 static const int atrc_fl2bps[ATRC_FLAVORS] = {
74 8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100 };
75 static const int cook_fl2bps[COOK_FLAVORS] = {
76 1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498,
77 4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584,
78 4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513,
79 12016, 16408, 22911, 33506
82 typedef struct mkv_content_encoding {
83 uint64_t order, type, scope;
84 uint64_t comp_algo;
85 uint8_t *comp_settings;
86 int comp_settings_len;
87 } mkv_content_encoding_t;
89 typedef struct mkv_track {
90 int tnum;
91 char *name;
92 int id; // -aid / -sid / -vid option value
94 char *codec_id;
95 int ms_compat;
96 char *language;
98 int type;
100 uint32_t v_width, v_height, v_dwidth, v_dheight;
101 double v_frate;
103 uint32_t a_formattag;
104 uint32_t a_channels, a_bps;
105 float a_sfreq;
106 float a_osfreq;
108 double default_duration;
110 int default_track;
112 unsigned char *private_data;
113 unsigned int private_size;
115 /* stuff for realmedia */
116 int realmedia;
117 int64_t rv_kf_base;
118 int rv_kf_pts;
119 double rv_pts; /* previous video timestamp */
120 double ra_pts; /* previous audio timestamp */
122 /** realaudio descrambling */
123 int sub_packet_size; ///< sub packet size, per stream
124 int sub_packet_h; ///< number of coded frames per block
125 int coded_framesize; ///< coded frame size, per stream
126 int audiopk_size; ///< audio packet size
127 unsigned char *audio_buf; ///< place to store reordered audio data
128 double *audio_timestamp; ///< timestamp for each audio packet
129 int sub_packet_cnt; ///< number of subpacket already received
130 int audio_filepos; ///< file position of first audio packet in block
132 /* stuff for quicktime */
133 int fix_i_bps;
134 double qt_last_a_pts;
136 int subtitle_type;
138 /* generic content encoding support */
139 mkv_content_encoding_t *encodings;
140 int num_encodings;
142 /* For VobSubs and SSA/ASS */
143 sh_sub_t *sh_sub;
144 } mkv_track_t;
146 typedef struct mkv_index {
147 int tnum;
148 uint64_t timecode, filepos;
149 } mkv_index_t;
151 typedef struct mkv_demuxer {
152 off_t segment_start;
154 double duration, last_pts;
155 uint64_t last_filepos;
157 mkv_track_t **tracks;
158 int num_tracks;
160 uint64_t tc_scale, cluster_tc;
162 uint64_t cluster_start;
163 uint64_t cluster_size;
164 uint64_t blockgroup_size;
166 mkv_index_t *indexes;
167 int num_indexes;
169 off_t *parsed_pos;
170 int num_parsed_pos;
171 bool parsed_info;
172 bool parsed_tracks;
173 bool parsed_tags;
174 bool parsed_chapters;
175 bool parsed_attachments;
177 struct cluster_pos {
178 uint64_t filepos;
179 uint64_t timecode;
180 } *cluster_positions;
181 int num_cluster_pos;
183 uint64_t skip_to_timecode;
184 int v_skip_to_keyframe, a_skip_to_keyframe;
186 int num_audio_tracks;
187 } mkv_demuxer_t;
189 #define REALHEADER_SIZE 16
190 #define RVPROPERTIES_SIZE 34
191 #define RAPROPERTIES4_SIZE 56
192 #define RAPROPERTIES5_SIZE 70
195 * \brief ensures there is space for at least one additional element
196 * \param array array to grow
197 * \param nelem current number of elements in array
198 * \param elsize size of one array element
200 static void *grow_array(void *array, int nelem, size_t elsize)
202 if (!(nelem & 31))
203 array = realloc(array, (nelem + 32) * elsize);
204 return array;
207 static bool is_parsed_header(struct mkv_demuxer *mkv_d, off_t pos)
209 int low = 0;
210 int high = mkv_d->num_parsed_pos;
211 while (high > low + 1) {
212 int mid = high + low >> 1;
213 if (mkv_d->parsed_pos[mid] > pos)
214 high = mid;
215 else
216 low = mid;
218 if (mkv_d->num_parsed_pos && mkv_d->parsed_pos[low] == pos)
219 return true;
220 if (!(mkv_d->num_parsed_pos & 31))
221 mkv_d->parsed_pos = talloc_realloc(mkv_d, mkv_d->parsed_pos, off_t,
222 mkv_d->num_parsed_pos + 32);
223 mkv_d->num_parsed_pos++;
224 for (int i = mkv_d->num_parsed_pos - 1; i > low; i--)
225 mkv_d->parsed_pos[i] = mkv_d->parsed_pos[i - 1];
226 mkv_d->parsed_pos[low] = pos;
227 return false;
230 static mkv_track_t *find_track_by_num(struct mkv_demuxer *d, int n, int type)
232 for (int i = 0; i < d->num_tracks; i++)
233 if (d->tracks[i] != NULL && d->tracks[i]->type == type)
234 if (d->tracks[i]->id == n)
235 return d->tracks[i];
237 return NULL;
240 static void add_cluster_position(mkv_demuxer_t *mkv_d, uint64_t filepos,
241 uint64_t timecode)
243 if (mkv_d->indexes)
244 return;
246 int n = mkv_d->num_cluster_pos;
247 if (n > 0 && mkv_d->cluster_positions[n-1].filepos >= filepos)
248 return;
250 mkv_d->cluster_positions =
251 grow_array(mkv_d->cluster_positions, mkv_d->num_cluster_pos,
252 sizeof(*mkv_d->cluster_positions));
253 mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = (struct cluster_pos){
254 .filepos = filepos,
255 .timecode = timecode,
260 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
261 static int aac_get_sample_rate_index(uint32_t sample_rate)
263 static const int srates[] = {
264 92017, 75132, 55426, 46009, 37566, 27713,
265 23004, 18783, 13856, 11502, 9391, 0
267 int i = 0;
268 while (sample_rate < srates[i])
269 i++;
270 return i;
273 static void demux_mkv_decode(mkv_track_t *track, uint8_t *src,
274 uint8_t **dest, uint32_t *size, uint32_t type)
276 uint8_t *orig_src = src;
278 *dest = src;
280 for (int i = 0; i < track->num_encodings; i++) {
281 struct mkv_content_encoding *enc = track->encodings + i;
282 if (!(enc->scope & type))
283 continue;
285 if (src != *dest && src != orig_src)
286 talloc_free(src);
287 src = *dest; // output from last iteration is new source
289 if (enc->comp_algo == 0) {
290 #if CONFIG_ZLIB
291 /* zlib encoded track */
293 if (*size == 0)
294 continue;
296 z_stream zstream;
298 zstream.zalloc = (alloc_func) 0;
299 zstream.zfree = (free_func) 0;
300 zstream.opaque = (voidpf) 0;
301 if (inflateInit(&zstream) != Z_OK) {
302 mp_tmsg(MSGT_DEMUX, MSGL_WARN,
303 "[mkv] zlib initialization failed.\n");
304 goto error;
306 zstream.next_in = (Bytef *) src;
307 zstream.avail_in = *size;
309 *dest = NULL;
310 zstream.avail_out = *size;
311 int result;
312 do {
313 *size += 4000;
314 *dest = talloc_realloc_size(NULL, *dest, *size);
315 zstream.next_out = (Bytef *) (*dest + zstream.total_out);
316 result = inflate(&zstream, Z_NO_FLUSH);
317 if (result != Z_OK && result != Z_STREAM_END) {
318 mp_tmsg(MSGT_DEMUX, MSGL_WARN,
319 "[mkv] zlib decompression failed.\n");
320 talloc_free(*dest);
321 *dest = NULL;
322 inflateEnd(&zstream);
323 goto error;
325 zstream.avail_out += 4000;
326 } while (zstream.avail_out == 4000 && zstream.avail_in != 0
327 && result != Z_STREAM_END);
329 *size = zstream.total_out;
330 inflateEnd(&zstream);
331 #endif
332 } else if (enc->comp_algo == 2) {
333 /* lzo encoded track */
334 int out_avail;
335 int dstlen = *size * 3;
337 *dest = NULL;
338 while (1) {
339 int srclen = *size;
340 *dest = talloc_realloc_size(NULL, *dest,
341 dstlen + AV_LZO_OUTPUT_PADDING);
342 out_avail = dstlen;
343 int result = av_lzo1x_decode(*dest, &out_avail, src, &srclen);
344 if (result == 0)
345 break;
346 if (!(result & AV_LZO_OUTPUT_FULL)) {
347 mp_tmsg(MSGT_DEMUX, MSGL_WARN,
348 "[mkv] lzo decompression failed.\n");
349 talloc_free(*dest);
350 *dest = NULL;
351 goto error;
353 mp_msg(MSGT_DEMUX, MSGL_DBG2,
354 "[mkv] lzo decompression buffer too small.\n");
355 dstlen *= 2;
357 *size = dstlen - out_avail;
358 } else if (enc->comp_algo == 3) {
359 *dest = talloc_size(NULL, *size + enc->comp_settings_len);
360 memcpy(*dest, enc->comp_settings, enc->comp_settings_len);
361 memcpy(*dest + enc->comp_settings_len, src, *size);
362 *size += enc->comp_settings_len;
366 error:
367 if (src != *dest && src != orig_src)
368 talloc_free(src);
372 static int demux_mkv_read_info(demuxer_t *demuxer)
374 mkv_demuxer_t *mkv_d = demuxer->priv;
375 stream_t *s = demuxer->stream;
377 mkv_d->tc_scale = 1000000;
378 mkv_d->duration = 0;
380 struct ebml_info info = {};
381 struct ebml_parse_ctx parse_ctx = {};
382 if (ebml_read_element(s, &parse_ctx, &info, &ebml_info_desc) < 0)
383 return 1;
384 if (info.n_timecode_scale) {
385 mkv_d->tc_scale = info.timecode_scale;
386 mp_msg(MSGT_DEMUX, MSGL_V,
387 "[mkv] | + timecode scale: %" PRIu64 "\n", mkv_d->tc_scale);
389 if (info.n_duration) {
390 mkv_d->duration = info.duration * mkv_d->tc_scale / 1e9;
391 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + duration: %.3fs\n",
392 mkv_d->duration);
394 if (info.n_segment_uid) {
395 int len = info.segment_uid.len;
396 if (len != sizeof(demuxer->matroska_data.segment_uid)) {
397 mp_msg(MSGT_DEMUX, MSGL_INFO,
398 "[mkv] segment uid invalid length %d\n", len);
399 } else {
400 memcpy(demuxer->matroska_data.segment_uid, info.segment_uid.start,
401 len);
402 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + segment uid");
403 for (int i = 0; i < len; i++)
404 mp_msg(MSGT_DEMUX, MSGL_V, " %02x",
405 demuxer->matroska_data.segment_uid[i]);
406 mp_msg(MSGT_DEMUX, MSGL_V, "\n");
409 talloc_free(parse_ctx.talloc_ctx);
410 return 0;
413 static void parse_trackencodings(struct demuxer *demuxer,
414 struct mkv_track *track,
415 struct ebml_content_encodings *encodings)
417 // initial allocation to be a non-NULL context before realloc
418 mkv_content_encoding_t *ce = talloc_size(track, 1);
420 for (int n_enc = 0; n_enc < encodings->n_content_encoding; n_enc++) {
421 struct ebml_content_encoding *enc = encodings->content_encoding + n_enc;
422 struct mkv_content_encoding e = {};
423 e.order = enc->content_encoding_order;
424 if (enc->n_content_encoding_scope)
425 e.scope = enc->content_encoding_scope;
426 else
427 e.scope = 1;
428 e.type = enc->content_encoding_type;
430 if (enc->n_content_compression) {
431 struct ebml_content_compression *z = &enc->content_compression;
432 e.comp_algo = z->content_comp_algo;
433 if (z->n_content_comp_settings) {
434 int sz = z->content_comp_settings.len;
435 e.comp_settings = talloc_size(ce, sz);
436 memcpy(e.comp_settings, z->content_comp_settings.start, sz);
437 e.comp_settings_len = sz;
441 if (e.type == 1) {
442 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[mkv] Track "
443 "number %u has been encrypted and "
444 "decryption has not yet been\n"
445 "[mkv] implemented. Skipping track.\n",
446 track->tnum);
447 } else if (e.type != 0) {
448 mp_tmsg(MSGT_DEMUX, MSGL_WARN,
449 "[mkv] Unknown content encoding type for "
450 "track %u. Skipping track.\n",
451 track->tnum);
452 } else if (e.comp_algo != 0 && e.comp_algo != 2 && e.comp_algo != 3) {
453 mp_tmsg(MSGT_DEMUX, MSGL_WARN,
454 "[mkv] Track %u has been compressed with "
455 "an unknown/unsupported compression\n"
456 "[mkv] algorithm (%" PRIu64 "). Skipping track.\n",
457 track->tnum, e.comp_algo);
459 #if !CONFIG_ZLIB
460 else if (e.comp_algo == 0) {
461 mp_tmsg(MSGT_DEMUX, MSGL_WARN,
462 "[mkv] Track %u was compressed with zlib "
463 "but mplayer has not been compiled\n"
464 "[mkv] with support for zlib compression. "
465 "Skipping track.\n",
466 track->tnum);
468 #endif
469 int i;
470 for (i = 0; i < n_enc; i++)
471 if (e.order >= ce[i].order)
472 break;
473 ce = talloc_realloc_size(track, ce, (n_enc + 1) * sizeof(*ce));
474 memmove(ce + i + 1, ce + i, (n_enc - i) * sizeof(*ce));
475 memcpy(ce + i, &e, sizeof(e));
478 track->encodings = ce;
479 track->num_encodings = encodings->n_content_encoding;
482 static void parse_trackaudio(struct demuxer *demuxer, struct mkv_track *track,
483 struct ebml_audio *audio)
485 if (audio->n_sampling_frequency) {
486 track->a_sfreq = audio->sampling_frequency;
487 mp_msg(MSGT_DEMUX, MSGL_V,
488 "[mkv] | + Sampling frequency: %f\n", track->a_sfreq);
489 } else
490 track->a_sfreq = 8000;
491 if (audio->n_output_sampling_frequency) {
492 track->a_osfreq = audio->output_sampling_frequency;
493 mp_msg(MSGT_DEMUX, MSGL_V,
494 "[mkv] | + Output sampling frequency: %f\n", track->a_osfreq);
495 } else
496 track->a_osfreq = track->a_sfreq;
497 if (audio->n_bit_depth) {
498 track->a_bps = audio->bit_depth;
499 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Bit depth: %u\n",
500 track->a_bps);
502 if (audio->n_channels) {
503 track->a_channels = audio->channels;
504 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Channels: %u\n",
505 track->a_channels);
506 } else
507 track->a_channels = 1;
510 static void parse_trackvideo(struct demuxer *demuxer, struct mkv_track *track,
511 struct ebml_video *video)
513 if (video->n_frame_rate) {
514 track->v_frate = video->frame_rate;
515 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Frame rate: %f\n",
516 track->v_frate);
517 if (track->v_frate > 0)
518 track->default_duration = 1 / track->v_frate;
520 if (video->n_display_width) {
521 track->v_dwidth = video->display_width;
522 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Display width: %u\n",
523 track->v_dwidth);
525 if (video->n_display_height) {
526 track->v_dheight = video->display_height;
527 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Display height: %u\n",
528 track->v_dheight);
530 if (video->n_pixel_width) {
531 track->v_width = video->pixel_width;
532 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel width: %u\n",
533 track->v_width);
535 if (video->n_pixel_height) {
536 track->v_height = video->pixel_height;
537 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel height: %u\n",
538 track->v_height);
543 * \brief free any data associated with given track
544 * \param track track of which to free data
546 static void demux_mkv_free_trackentry(mkv_track_t *track)
548 free(track->audio_buf);
549 free(track->audio_timestamp);
550 talloc_free(track);
553 static void parse_trackentry(struct demuxer *demuxer,
554 struct ebml_track_entry *entry)
556 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
557 struct mkv_track *track = talloc_zero_size(NULL, sizeof(*track));
559 track->tnum = entry->track_number;
560 if (track->tnum)
561 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Track number: %u\n",
562 track->tnum);
563 else
564 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] Missing track number!\n");
566 if (entry->n_name) {
567 track->name = talloc_strndup(track, entry->name.start,
568 entry->name.len);
569 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Name: %s\n",
570 track->name);
573 track->type = entry->track_type;
574 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Track type: ");
575 switch (track->type) {
576 case MATROSKA_TRACK_AUDIO:
577 mp_msg(MSGT_DEMUX, MSGL_V, "Audio\n");
578 break;
579 case MATROSKA_TRACK_VIDEO:
580 mp_msg(MSGT_DEMUX, MSGL_V, "Video\n");
581 break;
582 case MATROSKA_TRACK_SUBTITLE:
583 mp_msg(MSGT_DEMUX, MSGL_V, "Subtitle\n");
584 break;
585 default:
586 mp_msg(MSGT_DEMUX, MSGL_V, "unknown\n");
587 break;
590 if (entry->n_audio) {
591 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Audio track\n");
592 parse_trackaudio(demuxer, track, &entry->audio);
595 if (entry->n_video) {
596 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Video track\n");
597 parse_trackvideo(demuxer, track, &entry->video);
600 if (entry->n_codec_id) {
601 track->codec_id = talloc_strndup(track, entry->codec_id.start,
602 entry->codec_id.len);
603 if (!strcmp(track->codec_id, MKV_V_MSCOMP)
604 || !strcmp(track->codec_id, MKV_A_ACM))
605 track->ms_compat = 1;
606 else if (!strcmp(track->codec_id, MKV_S_VOBSUB))
607 track->subtitle_type = 'v';
608 else if (!strcmp(track->codec_id, MKV_S_TEXTSSA)
609 || !strcmp(track->codec_id, MKV_S_TEXTASS)
610 || !strcmp(track->codec_id, MKV_S_SSA)
611 || !strcmp(track->codec_id, MKV_S_ASS))
612 track->subtitle_type = 'a';
613 else if (!strcmp(track->codec_id, MKV_S_TEXTASCII)
614 || !strcmp(track->codec_id, MKV_S_TEXTUTF8))
615 track->subtitle_type = 't';
616 else if (!strcmp(track->codec_id, MKV_S_PGS))
617 track->subtitle_type = 'p';
618 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Codec ID: %s\n",
619 track->codec_id);
620 } else
621 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] Missing codec ID!\n");
623 if (entry->n_codec_private) {
624 int len = entry->codec_private.len;
625 track->private_data = talloc_size(track, len + AV_LZO_INPUT_PADDING);
626 memcpy(track->private_data, entry->codec_private.start, len);
627 track->private_size = len;
628 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + CodecPrivate, length %u\n",
629 track->private_size);
632 if (entry->n_language) {
633 track->language = talloc_strndup(track, entry->language.start,
634 entry->language.len);
635 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Language: %s\n",
636 track->language);
637 } else
638 track->language = talloc_strdup(track, "eng");
640 if (entry->n_flag_default) {
641 track->default_track = entry->flag_default;
642 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Default flag: %u\n",
643 track->default_track);
644 } else
645 track->default_track = 1;
647 if (entry->n_default_duration) {
648 track->default_duration = entry->default_duration / 1e9;
649 if (entry->default_duration == 0)
650 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Default duration: 0");
651 else {
652 if (!track->v_frate)
653 track->v_frate = 1e9 / entry->default_duration;
654 mp_msg(MSGT_DEMUX, MSGL_V,
655 "[mkv] | + Default duration: %.3fms ( = %.3f fps)\n",
656 entry->default_duration / 1000000.0, track->v_frate);
660 if (entry->n_content_encodings)
661 parse_trackencodings(demuxer, track, &entry->content_encodings);
663 mkv_d->tracks[mkv_d->num_tracks++] = track;
666 static int demux_mkv_read_tracks(demuxer_t *demuxer)
668 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
669 stream_t *s = demuxer->stream;
671 struct ebml_tracks tracks = {};
672 struct ebml_parse_ctx parse_ctx = {};
673 if (ebml_read_element(s, &parse_ctx, &tracks, &ebml_tracks_desc) < 0)
674 return 1;
676 mkv_d->tracks = talloc_size(mkv_d,
677 tracks.n_track_entry * sizeof(*mkv_d->tracks));
678 for (int i = 0; i < tracks.n_track_entry; i++) {
679 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + a track...\n");
680 parse_trackentry(demuxer, &tracks.track_entry[i]);
682 talloc_free(parse_ctx.talloc_ctx);
683 return 0;
686 static int demux_mkv_read_cues(demuxer_t *demuxer)
688 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
689 stream_t *s = demuxer->stream;
691 if (index_mode == 0 || index_mode == 2) {
692 ebml_read_skip(s, NULL);
693 return 0;
696 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing cues ] -----------\n");
697 struct ebml_cues cues = {};
698 struct ebml_parse_ctx parse_ctx = {};
699 if (ebml_read_element(s, &parse_ctx, &cues, &ebml_cues_desc) < 0)
700 goto out;
701 for (int i = 0; i < cues.n_cue_point; i++) {
702 struct ebml_cue_point *cuepoint = &cues.cue_point[i];
703 if (cuepoint->n_cue_time != 1 || !cuepoint->n_cue_track_positions) {
704 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Malformed CuePoint element\n");
705 continue;
707 uint64_t time = cuepoint->cue_time;
708 for (int i = 0; i < cuepoint->n_cue_track_positions; i++) {
709 struct ebml_cue_track_positions *trackpos =
710 &cuepoint->cue_track_positions[i];
711 uint64_t track = trackpos->cue_track;
712 uint64_t pos = trackpos->cue_cluster_position;
713 mkv_d->indexes =
714 grow_array(mkv_d->indexes, mkv_d->num_indexes,
715 sizeof(mkv_index_t));
716 mkv_d->indexes[mkv_d->num_indexes].tnum = track;
717 mkv_d->indexes[mkv_d->num_indexes].timecode = time;
718 mkv_d->indexes[mkv_d->num_indexes].filepos =
719 mkv_d->segment_start + pos;
720 mp_msg(MSGT_DEMUX, MSGL_DBG2,
721 "[mkv] |+ found cue point for track %" PRIu64
722 ": timecode %" PRIu64 ", filepos: %" PRIu64 "\n", track,
723 time, mkv_d->segment_start + pos);
724 mkv_d->num_indexes++;
728 out:
729 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing cues ] -----------\n");
730 talloc_free(parse_ctx.talloc_ctx);
731 return 0;
734 static int demux_mkv_read_chapters(struct demuxer *demuxer)
736 struct MPOpts *opts = demuxer->opts;
737 stream_t *s = demuxer->stream;
739 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing chapters ] ---------\n");
740 struct ebml_chapters file_chapters = {};
741 struct ebml_parse_ctx parse_ctx = {};
742 if (ebml_read_element(s, &parse_ctx, &file_chapters,
743 &ebml_chapters_desc) < 0)
744 goto out;
746 int selected_edition = 0;
747 int num_editions = file_chapters.n_edition_entry;
748 struct ebml_edition_entry *editions = file_chapters.edition_entry;
749 if (opts->edition_id >= 0 && opts->edition_id < num_editions) {
750 selected_edition = opts->edition_id;
751 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] User-specified edition: %d\n",
752 selected_edition);
753 } else
754 for (int i = 0; i < num_editions; i++)
755 if (editions[i].edition_flag_default) {
756 selected_edition = i;
757 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Default edition: %d\n", i);
758 break;
760 struct matroska_chapter *m_chapters = NULL;
761 if (editions[selected_edition].edition_flag_ordered) {
762 int count = editions[selected_edition].n_chapter_atom;
763 m_chapters = talloc_array_ptrtype(demuxer, m_chapters, count);
764 demuxer->matroska_data.ordered_chapters = m_chapters;
765 demuxer->matroska_data.num_ordered_chapters = count;
768 for (int idx = 0; idx < num_editions; idx++) {
769 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] New edition %d\n", idx);
770 int warn_level = idx == selected_edition ? MSGL_WARN : MSGL_V;
771 if (editions[idx].n_edition_flag_default)
772 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Default edition flag: %"PRIu64
773 "\n", editions[idx].edition_flag_default);
774 if (editions[idx].n_edition_flag_ordered)
775 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Ordered chapter flag: %"PRIu64
776 "\n", editions[idx].edition_flag_ordered);
777 for (int i = 0; i < editions[idx].n_chapter_atom; i++) {
778 struct ebml_chapter_atom *ca = editions[idx].chapter_atom + i;
779 struct matroska_chapter chapter = { };
780 struct bstr name = { "(unnamed)", 9 };
782 if (!ca->n_chapter_time_start)
783 mp_msg(MSGT_DEMUX, warn_level,
784 "[mkv] Chapter lacks start time\n");
785 chapter.start = ca->chapter_time_start;
786 chapter.end = ca->chapter_time_end;
788 if (ca->n_chapter_display) {
789 if (ca->n_chapter_display > 1)
790 mp_msg(MSGT_DEMUX, warn_level, "[mkv] Multiple chapter "
791 "names not supported, picking first\n");
792 if (!ca->chapter_display[0].n_chap_string)
793 mp_msg(MSGT_DEMUX, warn_level, "[mkv] Malformed chapter "
794 "name entry\n");
795 else
796 name = ca->chapter_display[0].chap_string;
799 if (ca->n_chapter_segment_uid) {
800 chapter.has_segment_uid = true;
801 int len = ca->chapter_segment_uid.len;
802 if (len != sizeof(chapter.segment_uid))
803 mp_msg(MSGT_DEMUX, warn_level,
804 "[mkv] Chapter segment uid bad length %d\n", len);
805 else if (ca->n_chapter_segment_edition_uid) {
806 mp_tmsg(MSGT_DEMUX, warn_level, "[mkv] Warning: "
807 "unsupported edition recursion in chapter; "
808 "will skip on playback!\n");
809 } else {
810 memcpy(chapter.segment_uid, ca->chapter_segment_uid.start,
811 len);
812 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Chapter segment uid ");
813 for (int i = 0; i < len; i++)
814 mp_msg(MSGT_DEMUX, MSGL_V, "%02x ",
815 chapter.segment_uid[i]);
816 mp_msg(MSGT_DEMUX, MSGL_V, "\n");
820 mp_msg(MSGT_DEMUX, MSGL_V,
821 "[mkv] Chapter %u from %02d:%02d:%02d.%03d "
822 "to %02d:%02d:%02d.%03d, %.*s\n", i,
823 (int) (chapter.start / 60 / 60 / 1000000000),
824 (int) ((chapter.start / 60 / 1000000000) % 60),
825 (int) ((chapter.start / 1000000000) % 60),
826 (int) (chapter.start % 1000000000),
827 (int) (chapter.end / 60 / 60 / 1000000000),
828 (int) ((chapter.end / 60 / 1000000000) % 60),
829 (int) ((chapter.end / 1000000000) % 60),
830 (int) (chapter.end % 1000000000),
831 BSTR_P(name));
833 if (idx == selected_edition){
834 demuxer_add_chapter(demuxer, name, chapter.start, chapter.end);
835 if (editions[idx].edition_flag_ordered) {
836 chapter.name = talloc_strndup(m_chapters, name.start,
837 name.len);
838 m_chapters[i] = chapter;
843 if (num_editions > 1)
844 mp_msg(MSGT_DEMUX, MSGL_INFO,
845 "[mkv] Found %d editions, will play #%d (first is 0).\n",
846 num_editions, selected_edition);
848 out:
849 talloc_free(parse_ctx.talloc_ctx);
850 mp_msg(MSGT_DEMUX, MSGL_V,
851 "[mkv] \\---- [ parsing chapters ] ---------\n");
852 return 0;
855 static int demux_mkv_read_tags(demuxer_t *demuxer)
857 stream_t *s = demuxer->stream;
859 struct ebml_parse_ctx parse_ctx = {};
860 struct ebml_tags tags = {};
861 if (ebml_read_element(s, &parse_ctx, &tags, &ebml_tags_desc) < 0)
862 return 1;
864 for (int i = 0; i < tags.n_tag; i++) {
865 struct ebml_tag tag = tags.tag[i];
866 if (tag.targets.target_track_uid || tag.targets.target_edition_uid ||
867 tag.targets.target_chapter_uid || tag.targets.target_attachment_uid)
868 continue;
870 for (int j; j < tag.n_simple_tag; j++)
871 demux_info_add_bstr(demuxer, tag.simple_tag[j].tag_name, tag.simple_tag[j].tag_string);
874 return 0;
877 static int demux_mkv_read_attachments(demuxer_t *demuxer)
879 stream_t *s = demuxer->stream;
881 mp_msg(MSGT_DEMUX, MSGL_V,
882 "[mkv] /---- [ parsing attachments ] ---------\n");
884 struct ebml_attachments attachments = {};
885 struct ebml_parse_ctx parse_ctx = {};
886 if (ebml_read_element(s, &parse_ctx, &attachments,
887 &ebml_attachments_desc) < 0)
888 goto out;
890 for (int i = 0; i < attachments.n_attached_file; i++) {
891 struct ebml_attached_file *attachment = &attachments.attached_file[i];
892 if (!attachment->n_file_name || !attachment->n_file_mime_type
893 || !attachment->n_file_data) {
894 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Malformed attachment\n");
895 continue;
897 struct bstr name = attachment->file_name;
898 struct bstr mime = attachment->file_mime_type;
899 demuxer_add_attachment(demuxer, name, mime, attachment->file_data);
900 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Attachment: %.*s, %.*s, %zu bytes\n",
901 BSTR_P(name), BSTR_P(mime), attachment->file_data.len);
904 out:
905 talloc_free(parse_ctx.talloc_ctx);
906 mp_msg(MSGT_DEMUX, MSGL_V,
907 "[mkv] \\---- [ parsing attachments ] ---------\n");
908 return 0;
911 static int read_header_element(struct demuxer *demuxer, uint32_t id,
912 off_t at_filepos);
914 static int demux_mkv_read_seekhead(demuxer_t *demuxer)
916 struct mkv_demuxer *mkv_d = demuxer->priv;
917 struct stream *s = demuxer->stream;
918 int res = 0;
919 struct ebml_seek_head seekhead = {};
920 struct ebml_parse_ctx parse_ctx = {};
922 mp_msg(MSGT_DEMUX, MSGL_V,
923 "[mkv] /---- [ parsing seek head ] ---------\n");
924 if (ebml_read_element(s, &parse_ctx, &seekhead, &ebml_seek_head_desc) < 0) {
925 res = 1;
926 goto out;
928 /* off now holds the position of the next element after the seek head. */
929 off_t off = stream_tell(s);
930 for (int i = 0; i < seekhead.n_seek; i++) {
931 struct ebml_seek *seek = &seekhead.seek[i];
932 if (seek->n_seek_id != 1 || seek->n_seek_position != 1) {
933 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Invalid SeekHead entry\n");
934 continue;
936 uint64_t pos = seek->seek_position + mkv_d->segment_start;
937 if (pos >= demuxer->movi_end) {
938 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] SeekHead position beyond "
939 "end of file - incomplete file?\n");
940 continue;
942 read_header_element(demuxer, seek->seek_id, pos);
944 if (!stream_seek(s, off)) {
945 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] Couldn't seek back after "
946 "SeekHead??\n");
947 res = 1;
949 out:
950 mp_msg(MSGT_DEMUX, MSGL_V,
951 "[mkv] \\---- [ parsing seek head ] ---------\n");
952 talloc_free(parse_ctx.talloc_ctx);
953 return res;
956 static bool seek_pos_id(struct stream *s, off_t pos, uint32_t id)
958 if (!stream_seek(s, pos)) {
959 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Failed to seek in file\n");
960 return false;
962 if (ebml_read_id(s, NULL) != id) {
963 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Expected element not found\n");
964 return false;
966 return true;
969 static int read_header_element(struct demuxer *demuxer, uint32_t id,
970 off_t at_filepos)
972 struct mkv_demuxer *mkv_d = demuxer->priv;
973 stream_t *s = demuxer->stream;
974 off_t pos = stream_tell(s) - 4;
976 switch(id) {
977 case MATROSKA_ID_INFO:
978 if (mkv_d->parsed_info)
979 break;
980 if (at_filepos && !seek_pos_id(s, at_filepos, id))
981 return -1;
982 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ segment information...\n");
983 mkv_d->parsed_info = true;
984 return demux_mkv_read_info(demuxer) ? -1 : 1;
986 case MATROSKA_ID_TRACKS:
987 if (mkv_d->parsed_tracks)
988 break;
989 if (at_filepos && !seek_pos_id(s, at_filepos, id))
990 return -1;
991 mkv_d->parsed_tracks = true;
992 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ segment tracks...\n");
993 return demux_mkv_read_tracks(demuxer) ? -1 : 1;
995 case MATROSKA_ID_CUES:
996 if (is_parsed_header(mkv_d, pos))
997 break;
998 if (at_filepos && !seek_pos_id(s, at_filepos, id))
999 return -1;
1000 return demux_mkv_read_cues(demuxer) ? -1 : 1;
1002 case MATROSKA_ID_TAGS:
1003 if (mkv_d->parsed_tags)
1004 break;
1005 if (at_filepos && !seek_pos_id(s, at_filepos, id))
1006 return -1;
1007 mkv_d->parsed_tags = true;
1008 return demux_mkv_read_tags(demuxer) ? -1 : 1;
1010 case MATROSKA_ID_SEEKHEAD:
1011 if (is_parsed_header(mkv_d, pos))
1012 break;
1013 if (at_filepos && !seek_pos_id(s, at_filepos, id))
1014 return -1;
1015 return demux_mkv_read_seekhead(demuxer) ? -1 : 1;
1017 case MATROSKA_ID_CHAPTERS:
1018 if (mkv_d->parsed_chapters)
1019 break;
1020 if (at_filepos && !seek_pos_id(s, at_filepos, id))
1021 return -1;
1022 mkv_d->parsed_chapters = true;
1023 return demux_mkv_read_chapters(demuxer) ? -1 : 1;
1025 case MATROSKA_ID_ATTACHMENTS:
1026 if (mkv_d->parsed_attachments)
1027 break;
1028 if (at_filepos && !seek_pos_id(s, at_filepos, id))
1029 return -1;
1030 mkv_d->parsed_attachments = true;
1031 return demux_mkv_read_attachments(demuxer) ? -1 : 1;
1033 default:
1034 if (!at_filepos)
1035 ebml_read_skip(s, NULL);
1036 return 0;
1038 if (!at_filepos)
1039 ebml_read_skip(s, NULL);
1040 return 1;
1045 static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track,
1046 int vid);
1047 static int demux_mkv_open_audio(demuxer_t *demuxer, mkv_track_t *track,
1048 int aid);
1049 static int demux_mkv_open_sub(demuxer_t *demuxer, mkv_track_t *track,
1050 int sid);
1052 static void display_create_tracks(demuxer_t *demuxer)
1054 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1055 int i, vid = 0, aid = 0, sid = 0;
1057 for (i = 0; i < mkv_d->num_tracks; i++) {
1058 char *type = "unknown", str[32];
1059 *str = '\0';
1060 switch (mkv_d->tracks[i]->type) {
1061 case MATROSKA_TRACK_VIDEO:
1062 type = "video";
1063 mkv_d->tracks[i]->id = -1;
1064 if (vid == MAX_V_STREAMS)
1065 break;
1066 mkv_d->tracks[i]->id = vid;
1067 demux_mkv_open_video(demuxer, mkv_d->tracks[i], vid);
1068 if (mkv_d->tracks[i]->name)
1069 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", vid,
1070 mkv_d->tracks[i]->name);
1071 sprintf(str, "-vid %u", vid++);
1072 break;
1073 case MATROSKA_TRACK_AUDIO:
1074 type = "audio";
1075 mkv_d->tracks[i]->id = -1;
1076 if (aid == MAX_A_STREAMS)
1077 break;
1078 mkv_d->tracks[i]->id = aid;
1079 demux_mkv_open_audio(demuxer, mkv_d->tracks[i], aid);
1080 if (mkv_d->tracks[i]->name)
1081 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", aid,
1082 mkv_d->tracks[i]->name);
1083 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", aid,
1084 mkv_d->tracks[i]->language);
1085 sprintf(str, "-aid %u, -alang %.5s", aid++,
1086 mkv_d->tracks[i]->language);
1087 break;
1088 case MATROSKA_TRACK_SUBTITLE:
1089 type = "subtitles";
1090 mkv_d->tracks[i]->id = -1;
1091 if (sid == MAX_S_STREAMS)
1092 break;
1093 mkv_d->tracks[i]->id = sid;
1094 demux_mkv_open_sub(demuxer, mkv_d->tracks[i], sid);
1095 if (mkv_d->tracks[i]->name)
1096 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", sid,
1097 mkv_d->tracks[i]->name);
1098 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid,
1099 mkv_d->tracks[i]->language);
1100 sprintf(str, "-sid %u, -slang %.5s", sid++,
1101 mkv_d->tracks[i]->language);
1102 break;
1104 if (mkv_d->tracks[i]->name)
1105 mp_tmsg(MSGT_DEMUX, MSGL_INFO,
1106 "[mkv] Track ID %u: %s (%s) \"%s\", %s\n",
1107 mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id,
1108 mkv_d->tracks[i]->name, str);
1109 else
1110 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[mkv] Track ID %u: %s (%s), %s\n",
1111 mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id,
1112 str);
1114 mkv_d->num_audio_tracks = aid;
1117 typedef struct {
1118 char *id;
1119 int fourcc;
1120 int extradata;
1121 } videocodec_info_t;
1123 static const videocodec_info_t vinfo[] = {
1124 {MKV_V_MJPEG, mmioFOURCC('m', 'j', 'p', 'g'), 1},
1125 {MKV_V_MPEG1, mmioFOURCC('m', 'p', 'g', '1'), 0},
1126 {MKV_V_MPEG2, mmioFOURCC('m', 'p', 'g', '2'), 0},
1127 {MKV_V_MPEG4_SP, mmioFOURCC('m', 'p', '4', 'v'), 1},
1128 {MKV_V_MPEG4_ASP, mmioFOURCC('m', 'p', '4', 'v'), 1},
1129 {MKV_V_MPEG4_AP, mmioFOURCC('m', 'p', '4', 'v'), 1},
1130 {MKV_V_MPEG4_AVC, mmioFOURCC('a', 'v', 'c', '1'), 1},
1131 {MKV_V_THEORA, mmioFOURCC('t', 'h', 'e', 'o'), 1},
1132 {MKV_V_VP8, mmioFOURCC('V', 'P', '8', '0'), 0},
1133 {NULL, 0, 0}
1136 static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track,
1137 int vid)
1139 BITMAPINFOHEADER *bih;
1140 void *ImageDesc = NULL;
1141 sh_video_t *sh_v;
1143 if (track->ms_compat) { /* MS compatibility mode */
1144 BITMAPINFOHEADER *src;
1146 if (track->private_data == NULL
1147 || track->private_size < sizeof(*bih))
1148 return 1;
1150 src = (BITMAPINFOHEADER *) track->private_data;
1151 bih = calloc(1, track->private_size);
1152 bih->biSize = le2me_32(src->biSize);
1153 bih->biWidth = le2me_32(src->biWidth);
1154 bih->biHeight = le2me_32(src->biHeight);
1155 bih->biPlanes = le2me_16(src->biPlanes);
1156 bih->biBitCount = le2me_16(src->biBitCount);
1157 bih->biCompression = le2me_32(src->biCompression);
1158 bih->biSizeImage = le2me_32(src->biSizeImage);
1159 bih->biXPelsPerMeter = le2me_32(src->biXPelsPerMeter);
1160 bih->biYPelsPerMeter = le2me_32(src->biYPelsPerMeter);
1161 bih->biClrUsed = le2me_32(src->biClrUsed);
1162 bih->biClrImportant = le2me_32(src->biClrImportant);
1163 memcpy(bih + 1,
1164 src + 1,
1165 track->private_size - sizeof(*bih));
1167 if (track->v_width == 0)
1168 track->v_width = bih->biWidth;
1169 if (track->v_height == 0)
1170 track->v_height = bih->biHeight;
1171 } else {
1172 bih = calloc(1, sizeof(*bih));
1173 bih->biSize = sizeof(*bih);
1174 bih->biWidth = track->v_width;
1175 bih->biHeight = track->v_height;
1176 bih->biBitCount = 24;
1177 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount / 8;
1179 if (track->private_size >= RVPROPERTIES_SIZE
1180 && (!strcmp(track->codec_id, MKV_V_REALV10)
1181 || !strcmp(track->codec_id, MKV_V_REALV20)
1182 || !strcmp(track->codec_id, MKV_V_REALV30)
1183 || !strcmp(track->codec_id, MKV_V_REALV40))) {
1184 unsigned char *dst, *src;
1185 uint32_t type2;
1186 unsigned int cnt;
1188 src = (uint8_t *) track->private_data + RVPROPERTIES_SIZE;
1190 cnt = track->private_size - RVPROPERTIES_SIZE;
1191 bih = realloc(bih, sizeof(*bih) + 8 + cnt);
1192 bih->biSize = 48 + cnt;
1193 bih->biPlanes = 1;
1194 type2 = AV_RB32(src - 4);
1195 if (type2 == 0x10003000 || type2 == 0x10003001)
1196 bih->biCompression = mmioFOURCC('R', 'V', '1', '3');
1197 else
1198 bih->biCompression =
1199 mmioFOURCC('R', 'V', track->codec_id[9], '0');
1200 dst = (unsigned char *) (bih + 1);
1201 // copy type1 and type2 info from rv properties
1202 memcpy(dst, src - 8, 8 + cnt);
1203 track->realmedia = 1;
1205 #ifdef CONFIG_QTX_CODECS
1206 } else if (track->private_size >= sizeof(ImageDescription)
1207 && !strcmp(track->codec_id, MKV_V_QUICKTIME)) {
1208 ImageDescriptionPtr idesc;
1210 idesc = (ImageDescriptionPtr) track->private_data;
1211 idesc->idSize = be2me_32(idesc->idSize);
1212 idesc->cType = be2me_32(idesc->cType);
1213 idesc->version = be2me_16(idesc->version);
1214 idesc->revisionLevel = be2me_16(idesc->revisionLevel);
1215 idesc->vendor = be2me_32(idesc->vendor);
1216 idesc->temporalQuality = be2me_32(idesc->temporalQuality);
1217 idesc->spatialQuality = be2me_32(idesc->spatialQuality);
1218 idesc->width = be2me_16(idesc->width);
1219 idesc->height = be2me_16(idesc->height);
1220 idesc->hRes = be2me_32(idesc->hRes);
1221 idesc->vRes = be2me_32(idesc->vRes);
1222 idesc->dataSize = be2me_32(idesc->dataSize);
1223 idesc->frameCount = be2me_16(idesc->frameCount);
1224 idesc->depth = be2me_16(idesc->depth);
1225 idesc->clutID = be2me_16(idesc->clutID);
1226 bih->biPlanes = 1;
1227 bih->biCompression = idesc->cType;
1228 ImageDesc = idesc;
1229 #endif /* CONFIG_QTX_CODECS */
1231 } else {
1232 const videocodec_info_t *vi = vinfo;
1233 while (vi->id && strcmp(vi->id, track->codec_id))
1234 vi++;
1235 bih->biCompression = vi->fourcc;
1236 if (vi->extradata && track->private_data
1237 && (track->private_size > 0)) {
1238 bih->biSize += track->private_size;
1239 bih = realloc(bih, bih->biSize);
1240 memcpy(bih + 1, track->private_data, track->private_size);
1242 if (!vi->id) {
1243 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[mkv] Unknown/unsupported "
1244 "CodecID (%s) or missing/bad CodecPrivate\n"
1245 "[mkv] data (track %u).\n",
1246 track->codec_id, track->tnum);
1247 free(bih);
1248 return 1;
1253 sh_v = new_sh_video(demuxer, vid);
1254 sh_v->bih = bih;
1255 sh_v->format = sh_v->bih->biCompression;
1256 if (track->v_frate == 0.0)
1257 track->v_frate = 25.0;
1258 sh_v->fps = track->v_frate;
1259 sh_v->frametime = 1 / track->v_frate;
1260 sh_v->aspect = 0;
1261 if (!track->realmedia) {
1262 sh_v->disp_w = track->v_width;
1263 sh_v->disp_h = track->v_height;
1264 if (track->v_dheight)
1265 sh_v->aspect = (double) track->v_dwidth / track->v_dheight;
1266 } else {
1267 // vd_realvid.c will set aspect to disp_w/disp_h and rederive
1268 // disp_w and disp_h from the RealVideo stream contents returned
1269 // by the Real DLLs. If DisplayWidth/DisplayHeight was not set in
1270 // the Matroska file then it has already been set to PixelWidth/Height
1271 // by check_track_information.
1272 sh_v->disp_w = track->v_dwidth;
1273 sh_v->disp_h = track->v_dheight;
1275 sh_v->ImageDesc = ImageDesc;
1276 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Aspect: %f\n", sh_v->aspect);
1278 sh_v->ds = demuxer->video;
1279 return 0;
1282 static int demux_mkv_open_audio(demuxer_t *demuxer, mkv_track_t *track,
1283 int aid)
1285 sh_audio_t *sh_a = new_sh_audio(demuxer, aid);
1286 if (!sh_a)
1287 return 1;
1289 if (track->language && (strcmp(track->language, "und") != 0))
1290 sh_a->lang = strdup(track->language);
1291 sh_a->default_track = track->default_track;
1292 sh_a->ds = demuxer->audio;
1293 sh_a->wf = malloc(sizeof(*sh_a->wf));
1294 if (track->ms_compat && (track->private_size >= sizeof(*sh_a->wf))) {
1295 WAVEFORMATEX *wf = (WAVEFORMATEX *) track->private_data;
1296 sh_a->wf = realloc(sh_a->wf, track->private_size);
1297 sh_a->wf->wFormatTag = le2me_16(wf->wFormatTag);
1298 sh_a->wf->nChannels = le2me_16(wf->nChannels);
1299 sh_a->wf->nSamplesPerSec = le2me_32(wf->nSamplesPerSec);
1300 sh_a->wf->nAvgBytesPerSec = le2me_32(wf->nAvgBytesPerSec);
1301 sh_a->wf->nBlockAlign = le2me_16(wf->nBlockAlign);
1302 sh_a->wf->wBitsPerSample = le2me_16(wf->wBitsPerSample);
1303 sh_a->wf->cbSize = track->private_size - sizeof(*sh_a->wf);
1304 memcpy(sh_a->wf + 1, wf + 1,
1305 track->private_size - sizeof(*sh_a->wf));
1306 if (track->a_sfreq == 0.0)
1307 track->a_sfreq = sh_a->wf->nSamplesPerSec;
1308 if (track->a_channels == 0)
1309 track->a_channels = sh_a->wf->nChannels;
1310 if (track->a_bps == 0)
1311 track->a_bps = sh_a->wf->wBitsPerSample;
1312 track->a_formattag = sh_a->wf->wFormatTag;
1313 } else {
1314 memset(sh_a->wf, 0, sizeof(*sh_a->wf));
1315 if (!strcmp(track->codec_id, MKV_A_MP3)
1316 || !strcmp(track->codec_id, MKV_A_MP2))
1317 track->a_formattag = 0x0055;
1318 else if (!strncmp(track->codec_id, MKV_A_AC3, strlen(MKV_A_AC3)))
1319 track->a_formattag = 0x2000;
1320 else if (!strncmp(track->codec_id, MKV_A_EAC3, strlen(MKV_A_EAC3)))
1321 track->a_formattag = mmioFOURCC('E', 'A', 'C', '3');
1322 else if (!strcmp(track->codec_id, MKV_A_DTS))
1323 track->a_formattag = 0x2001;
1324 else if (!strcmp(track->codec_id, MKV_A_PCM)
1325 || !strcmp(track->codec_id, MKV_A_PCM_BE))
1326 track->a_formattag = 0x0001;
1327 else if (!strcmp(track->codec_id, MKV_A_AAC_2MAIN)
1328 || !strncmp(track->codec_id, MKV_A_AAC_2LC,
1329 strlen(MKV_A_AAC_2LC))
1330 || !strcmp(track->codec_id, MKV_A_AAC_2SSR)
1331 || !strcmp(track->codec_id, MKV_A_AAC_4MAIN)
1332 || !strncmp(track->codec_id, MKV_A_AAC_4LC,
1333 strlen(MKV_A_AAC_4LC))
1334 || !strcmp(track->codec_id, MKV_A_AAC_4SSR)
1335 || !strcmp(track->codec_id, MKV_A_AAC_4LTP)
1336 || !strcmp(track->codec_id, MKV_A_AAC))
1337 track->a_formattag = mmioFOURCC('M', 'P', '4', 'A');
1338 else if (!strcmp(track->codec_id, MKV_A_VORBIS)) {
1339 if (track->private_data == NULL)
1340 return 1;
1341 track->a_formattag = mmioFOURCC('v', 'r', 'b', 's');
1342 } else if (!strcmp(track->codec_id, MKV_A_QDMC))
1343 track->a_formattag = mmioFOURCC('Q', 'D', 'M', 'C');
1344 else if (!strcmp(track->codec_id, MKV_A_QDMC2))
1345 track->a_formattag = mmioFOURCC('Q', 'D', 'M', '2');
1346 else if (!strcmp(track->codec_id, MKV_A_WAVPACK))
1347 track->a_formattag = mmioFOURCC('W', 'V', 'P', 'K');
1348 else if (!strcmp(track->codec_id, MKV_A_TRUEHD))
1349 track->a_formattag = mmioFOURCC('T', 'R', 'H', 'D');
1350 else if (!strcmp(track->codec_id, MKV_A_FLAC)) {
1351 if (track->private_data == NULL || track->private_size == 0) {
1352 mp_tmsg(MSGT_DEMUX, MSGL_WARN,
1353 "[mkv] FLAC track does not contain valid headers.\n");
1354 return 1;
1356 track->a_formattag = mmioFOURCC('f', 'L', 'a', 'C');
1357 } else if (track->private_size >= RAPROPERTIES4_SIZE) {
1358 if (!strcmp(track->codec_id, MKV_A_REAL28))
1359 track->a_formattag = mmioFOURCC('2', '8', '_', '8');
1360 else if (!strcmp(track->codec_id, MKV_A_REALATRC))
1361 track->a_formattag = mmioFOURCC('a', 't', 'r', 'c');
1362 else if (!strcmp(track->codec_id, MKV_A_REALCOOK))
1363 track->a_formattag = mmioFOURCC('c', 'o', 'o', 'k');
1364 else if (!strcmp(track->codec_id, MKV_A_REALDNET))
1365 track->a_formattag = mmioFOURCC('d', 'n', 'e', 't');
1366 else if (!strcmp(track->codec_id, MKV_A_REALSIPR))
1367 track->a_formattag = mmioFOURCC('s', 'i', 'p', 'r');
1368 } else {
1369 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "[mkv] Unknown/unsupported audio "
1370 "codec ID '%s' for track %u or missing/faulty\n[mkv] "
1371 "private codec data.\n", track->codec_id, track->tnum);
1372 free_sh_audio(demuxer, track->id);
1373 return 1;
1377 sh_a->format = track->a_formattag;
1378 sh_a->wf->wFormatTag = track->a_formattag;
1379 sh_a->channels = track->a_channels;
1380 sh_a->wf->nChannels = track->a_channels;
1381 sh_a->samplerate = (uint32_t) track->a_sfreq;
1382 sh_a->container_out_samplerate = track->a_osfreq;
1383 sh_a->wf->nSamplesPerSec = (uint32_t) track->a_sfreq;
1384 if (track->a_bps == 0) {
1385 sh_a->samplesize = 2;
1386 sh_a->wf->wBitsPerSample = 16;
1387 } else {
1388 sh_a->samplesize = track->a_bps / 8;
1389 sh_a->wf->wBitsPerSample = track->a_bps;
1391 if (track->a_formattag == 0x0055) { /* MP3 || MP2 */
1392 sh_a->wf->nAvgBytesPerSec = 16000;
1393 sh_a->wf->nBlockAlign = 1152;
1394 } else if ((track->a_formattag == 0x2000) /* AC3 */
1395 || track->a_formattag == mmioFOURCC('E', 'A', 'C', '3')
1396 || (track->a_formattag == 0x2001)) { /* DTS */
1397 free(sh_a->wf);
1398 sh_a->wf = NULL;
1399 } else if (track->a_formattag == 0x0001) { /* PCM || PCM_BE */
1400 sh_a->wf->nAvgBytesPerSec = sh_a->channels * sh_a->samplerate * 2;
1401 sh_a->wf->nBlockAlign = sh_a->wf->nAvgBytesPerSec;
1402 if (!strcmp(track->codec_id, MKV_A_PCM_BE))
1403 sh_a->format = mmioFOURCC('t', 'w', 'o', 's');
1404 } else if (!strcmp(track->codec_id, MKV_A_QDMC)
1405 || !strcmp(track->codec_id, MKV_A_QDMC2)) {
1406 sh_a->wf->nAvgBytesPerSec = 16000;
1407 sh_a->wf->nBlockAlign = 1486;
1408 track->fix_i_bps = 1;
1409 track->qt_last_a_pts = 0.0;
1410 if (track->private_data != NULL) {
1411 sh_a->codecdata = malloc(track->private_size);
1412 memcpy(sh_a->codecdata, track->private_data, track->private_size);
1413 sh_a->codecdata_len = track->private_size;
1415 } else if (track->a_formattag == mmioFOURCC('M', 'P', '4', 'A')) {
1416 int profile, srate_idx;
1418 sh_a->wf->nAvgBytesPerSec = 16000;
1419 sh_a->wf->nBlockAlign = 1024;
1421 if (!strcmp(track->codec_id, MKV_A_AAC)
1422 && (NULL != track->private_data)) {
1423 sh_a->codecdata = malloc(track->private_size);
1424 memcpy(sh_a->codecdata, track->private_data, track->private_size);
1425 sh_a->codecdata_len = track->private_size;
1426 return 0;
1429 /* Recreate the 'private data' */
1430 /* which faad2 uses in its initialization */
1431 srate_idx = aac_get_sample_rate_index(sh_a->samplerate);
1432 if (!strncmp(&track->codec_id[12], "MAIN", 4))
1433 profile = 0;
1434 else if (!strncmp(&track->codec_id[12], "LC", 2))
1435 profile = 1;
1436 else if (!strncmp(&track->codec_id[12], "SSR", 3))
1437 profile = 2;
1438 else
1439 profile = 3;
1440 sh_a->codecdata = malloc(5);
1441 sh_a->codecdata[0] = ((profile + 1) << 3) | ((srate_idx & 0xE) >> 1);
1442 sh_a->codecdata[1] =
1443 ((srate_idx & 0x1) << 7) | (track->a_channels << 3);
1445 if (strstr(track->codec_id, "SBR") != NULL) {
1446 /* HE-AAC (aka SBR AAC) */
1447 sh_a->codecdata_len = 5;
1449 sh_a->samplerate *= 2;
1450 sh_a->wf->nSamplesPerSec *= 2;
1451 srate_idx = aac_get_sample_rate_index(sh_a->samplerate);
1452 sh_a->codecdata[2] = AAC_SYNC_EXTENSION_TYPE >> 3;
1453 sh_a->codecdata[3] = ((AAC_SYNC_EXTENSION_TYPE & 0x07) << 5) | 5;
1454 sh_a->codecdata[4] = (1 << 7) | (srate_idx << 3);
1455 track->default_duration = 1024.0 / (sh_a->samplerate / 2);
1456 } else {
1457 sh_a->codecdata_len = 2;
1458 track->default_duration = 1024.0 / sh_a->samplerate;
1460 } else if (track->a_formattag == mmioFOURCC('v', 'r', 'b', 's')) { /* VORBIS */
1461 sh_a->wf->cbSize = track->private_size;
1462 sh_a->wf = realloc(sh_a->wf, sizeof(*sh_a->wf) + sh_a->wf->cbSize);
1463 memcpy((unsigned char *) (sh_a->wf + 1), track->private_data,
1464 sh_a->wf->cbSize);
1465 } else if (track->private_size >= RAPROPERTIES4_SIZE
1466 && !strncmp(track->codec_id, MKV_A_REALATRC, 7)) {
1467 /* Common initialization for all RealAudio codecs */
1468 unsigned char *src = track->private_data;
1469 int codecdata_length, version;
1470 int flavor;
1472 sh_a->wf->nAvgBytesPerSec = 0; /* FIXME !? */
1474 version = AV_RB16(src + 4);
1475 flavor = AV_RB16(src + 22);
1476 track->coded_framesize = AV_RB32(src + 24);
1477 track->sub_packet_h = AV_RB16(src + 40);
1478 sh_a->wf->nBlockAlign = track->audiopk_size = AV_RB16(src + 42);
1479 track->sub_packet_size = AV_RB16(src + 44);
1480 if (version == 4) {
1481 src += RAPROPERTIES4_SIZE;
1482 src += src[0] + 1;
1483 src += src[0] + 1;
1484 } else
1485 src += RAPROPERTIES5_SIZE;
1487 src += 3;
1488 if (version == 5)
1489 src++;
1490 codecdata_length = AV_RB32(src);
1491 src += 4;
1492 sh_a->wf->cbSize = codecdata_length;
1493 sh_a->wf = realloc(sh_a->wf, sizeof(*sh_a->wf) + sh_a->wf->cbSize);
1494 memcpy(((char *) (sh_a->wf + 1)), src, codecdata_length);
1496 switch (track->a_formattag) {
1497 case mmioFOURCC('a', 't', 'r', 'c'):
1498 sh_a->wf->nAvgBytesPerSec = atrc_fl2bps[flavor];
1499 sh_a->wf->nBlockAlign = track->sub_packet_size;
1500 track->audio_buf =
1501 malloc(track->sub_packet_h * track->audiopk_size);
1502 track->audio_timestamp =
1503 malloc(track->sub_packet_h * sizeof(double));
1504 break;
1505 case mmioFOURCC('c', 'o', 'o', 'k'):
1506 sh_a->wf->nAvgBytesPerSec = cook_fl2bps[flavor];
1507 sh_a->wf->nBlockAlign = track->sub_packet_size;
1508 track->audio_buf =
1509 malloc(track->sub_packet_h * track->audiopk_size);
1510 track->audio_timestamp =
1511 malloc(track->sub_packet_h * sizeof(double));
1512 break;
1513 case mmioFOURCC('s', 'i', 'p', 'r'):
1514 sh_a->wf->nAvgBytesPerSec = sipr_fl2bps[flavor];
1515 sh_a->wf->nBlockAlign = track->coded_framesize;
1516 track->audio_buf =
1517 malloc(track->sub_packet_h * track->audiopk_size);
1518 track->audio_timestamp =
1519 malloc(track->sub_packet_h * sizeof(double));
1520 break;
1521 case mmioFOURCC('2', '8', '_', '8'):
1522 sh_a->wf->nAvgBytesPerSec = 3600;
1523 sh_a->wf->nBlockAlign = track->coded_framesize;
1524 track->audio_buf =
1525 malloc(track->sub_packet_h * track->audiopk_size);
1526 track->audio_timestamp =
1527 malloc(track->sub_packet_h * sizeof(double));
1528 break;
1531 track->realmedia = 1;
1532 } else if (!strcmp(track->codec_id, MKV_A_FLAC)
1533 || (track->a_formattag == 0xf1ac)) {
1534 unsigned char *ptr;
1535 int size;
1536 free(sh_a->wf);
1537 sh_a->wf = NULL;
1539 if (track->a_formattag == mmioFOURCC('f', 'L', 'a', 'C')) {
1540 ptr = track->private_data;
1541 size = track->private_size;
1542 } else {
1543 sh_a->format = mmioFOURCC('f', 'L', 'a', 'C');
1544 ptr = track->private_data + sizeof(*sh_a->wf);
1545 size = track->private_size - sizeof(*sh_a->wf);
1547 if (size < 4 || ptr[0] != 'f' || ptr[1] != 'L' || ptr[2] != 'a'
1548 || ptr[3] != 'C') {
1549 sh_a->codecdata = malloc(4);
1550 sh_a->codecdata_len = 4;
1551 memcpy(sh_a->codecdata, "fLaC", 4);
1552 } else {
1553 sh_a->codecdata = malloc(size);
1554 sh_a->codecdata_len = size;
1555 memcpy(sh_a->codecdata, ptr, size);
1557 } else if (track->a_formattag == mmioFOURCC('W', 'V', 'P', 'K') || track->a_formattag == mmioFOURCC('T', 'R', 'H', 'D')) { /* do nothing, still works */
1558 } else if (!track->ms_compat
1559 || (track->private_size < sizeof(*sh_a->wf))) {
1560 free_sh_audio(demuxer, track->id);
1561 return 1;
1564 return 0;
1567 static int demux_mkv_open_sub(demuxer_t *demuxer, mkv_track_t *track,
1568 int sid)
1570 if (track->subtitle_type) {
1571 int size;
1572 uint8_t *buffer;
1573 sh_sub_t *sh = new_sh_sub(demuxer, sid);
1574 track->sh_sub = sh;
1575 sh->type = track->subtitle_type;
1576 size = track->private_size;
1577 demux_mkv_decode(track, track->private_data, &buffer, &size, 2);
1578 if (buffer && buffer != track->private_data) {
1579 talloc_free(track->private_data);
1580 talloc_steal(track, buffer);
1581 track->private_data = buffer;
1582 track->private_size = size;
1584 sh->extradata = malloc(track->private_size);
1585 memcpy(sh->extradata, track->private_data, track->private_size);
1586 sh->extradata_len = track->private_size;
1587 if (track->language && (strcmp(track->language, "und") != 0))
1588 sh->lang = strdup(track->language);
1589 sh->default_track = track->default_track;
1590 } else {
1591 mp_tmsg(MSGT_DEMUX, MSGL_ERR,
1592 "[mkv] Subtitle type '%s' is not supported.\n",
1593 track->codec_id);
1594 return 1;
1597 return 0;
1600 static int demux_mkv_open(demuxer_t *demuxer)
1602 stream_t *s = demuxer->stream;
1603 mkv_demuxer_t *mkv_d;
1604 mkv_track_t *track;
1605 int i, cont = 0;
1607 stream_seek(s, s->start_pos);
1608 if (ebml_read_id(s, NULL) != EBML_ID_EBML)
1609 return 0;
1610 struct ebml_ebml ebml_master = {};
1611 struct ebml_parse_ctx parse_ctx = { .no_error_messages = true };
1612 if (ebml_read_element(s, &parse_ctx, &ebml_master, &ebml_ebml_desc) < 0)
1613 return 0;
1614 if (ebml_master.doc_type.start == NULL) {
1615 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] File has EBML header but no doctype."
1616 " Assuming \"matroska\".\n");
1617 } else if (bstrcmp(ebml_master.doc_type, BSTR("matroska")) != 0
1618 && bstrcmp(ebml_master.doc_type, BSTR("webm")) != 0) {
1619 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] no head found\n");
1620 talloc_free(parse_ctx.talloc_ctx);
1621 return 0;
1623 if (ebml_master.doc_type_read_version > 2) {
1624 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] This looks like a Matroska file, "
1625 "but we don't support format version %"PRIu64"\n",
1626 ebml_master.doc_type_read_version);
1627 talloc_free(parse_ctx.talloc_ctx);
1628 return 0;
1630 if ((ebml_master.n_ebml_read_version
1631 && ebml_master.ebml_read_version != EBML_VERSION)
1632 || (ebml_master.n_ebml_max_size_length
1633 && ebml_master.ebml_max_size_length > 8)
1634 || (ebml_master.n_ebml_max_id_length
1635 && ebml_master.ebml_max_id_length != 4)) {
1636 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] This looks like a Matroska file, "
1637 "but the header has bad parameters\n");
1638 talloc_free(parse_ctx.talloc_ctx);
1639 return 0;
1641 talloc_free(parse_ctx.talloc_ctx);
1643 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Found the head...\n");
1645 if (ebml_read_id(s, NULL) != MATROSKA_ID_SEGMENT) {
1646 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n");
1647 return 0;
1649 ebml_read_length(s, NULL); /* return bytes number until EOF */
1651 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] + a segment...\n");
1653 mkv_d = talloc_zero(demuxer, struct mkv_demuxer);
1654 demuxer->priv = mkv_d;
1655 mkv_d->tc_scale = 1000000;
1656 mkv_d->segment_start = stream_tell(s);
1658 while (!cont) {
1659 uint32_t id = ebml_read_id(s, NULL);
1660 switch (id) {
1661 case MATROSKA_ID_CLUSTER:
1662 mp_msg(MSGT_DEMUX, MSGL_V,
1663 "[mkv] |+ found cluster, headers are "
1664 "parsed completely :)\n");
1665 stream_seek(s, stream_tell(s) - 4);
1666 cont = 1;
1667 break;
1669 default:
1670 cont = read_header_element(demuxer, id, 0) < 1;
1671 break;
1672 case EBML_ID_VOID:
1673 ebml_read_skip(s, NULL);
1674 break;
1678 display_create_tracks(demuxer);
1680 /* select video track */
1681 track = NULL;
1682 if (demuxer->video->id == -1) { /* automatically select a video track */
1683 /* search for a video track that has the 'default' flag set */
1684 for (i = 0; i < mkv_d->num_tracks; i++)
1685 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO
1686 && mkv_d->tracks[i]->default_track) {
1687 track = mkv_d->tracks[i];
1688 break;
1691 if (track == NULL)
1692 /* no track has the 'default' flag set */
1693 /* let's take the first video track */
1694 for (i = 0; i < mkv_d->num_tracks; i++)
1695 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO
1696 && mkv_d->tracks[i]->id >= 0) {
1697 track = mkv_d->tracks[i];
1698 break;
1700 } else if (demuxer->video->id != -2) /* -2 = no video at all */
1701 track = find_track_by_num(mkv_d, demuxer->video->id,
1702 MATROSKA_TRACK_VIDEO);
1704 if (track && demuxer->v_streams[track->id]) {
1705 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[mkv] Will play video track %u.\n",
1706 track->tnum);
1707 demuxer->video->id = track->id;
1708 demuxer->video->sh = demuxer->v_streams[track->id];
1709 } else {
1710 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[mkv] No video track found/wanted.\n");
1711 demuxer->video->id = -2;
1714 /* select audio track */
1715 track = NULL;
1716 if (track == NULL)
1717 /* search for an audio track that has the 'default' flag set */
1718 for (i = 0; i < mkv_d->num_tracks; i++)
1719 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO
1720 && mkv_d->tracks[i]->default_track) {
1721 track = mkv_d->tracks[i];
1722 break;
1725 if (track == NULL)
1726 /* no track has the 'default' flag set */
1727 /* let's take the first audio track */
1728 for (i = 0; i < mkv_d->num_tracks; i++)
1729 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO
1730 && mkv_d->tracks[i]->id >= 0) {
1731 track = mkv_d->tracks[i];
1732 break;
1735 if (track && demuxer->a_streams[track->id]) {
1736 demuxer->audio->id = track->id;
1737 demuxer->audio->sh = demuxer->a_streams[track->id];
1738 } else {
1739 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[mkv] No audio track found/wanted.\n");
1740 demuxer->audio->id = -2;
1743 if (s->end_pos == 0)
1744 demuxer->seekable = 0;
1745 else {
1746 demuxer->movi_start = s->start_pos;
1747 demuxer->movi_end = s->end_pos;
1748 demuxer->seekable = 1;
1751 demuxer->accurate_seek = true;
1753 return DEMUXER_TYPE_MATROSKA;
1756 static void demux_close_mkv(demuxer_t *demuxer)
1758 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1760 if (mkv_d) {
1761 int i;
1762 if (mkv_d->tracks) {
1763 for (i = 0; i < mkv_d->num_tracks; i++)
1764 demux_mkv_free_trackentry(mkv_d->tracks[i]);
1766 free(mkv_d->indexes);
1767 free(mkv_d->cluster_positions);
1771 static int demux_mkv_read_block_lacing(uint8_t *buffer, uint64_t *size,
1772 uint8_t *laces,
1773 uint32_t **all_lace_sizes)
1775 uint32_t total = 0;
1776 uint32_t *lace_size = NULL;
1777 uint8_t flags;
1778 int i;
1780 *all_lace_sizes = NULL;
1781 /* lacing flags */
1782 if (*size < 1)
1783 goto error;
1784 flags = *buffer++;
1785 (*size)--;
1787 switch ((flags & 0x06) >> 1) {
1788 case 0: /* no lacing */
1789 *laces = 1;
1790 lace_size = calloc(*laces, sizeof(uint32_t));
1791 lace_size[0] = *size;
1792 break;
1794 case 1: /* xiph lacing */
1795 case 2: /* fixed-size lacing */
1796 case 3: /* EBML lacing */
1797 if (*size < 1)
1798 goto error;
1799 *laces = *buffer++;
1800 (*size)--;
1801 (*laces)++;
1802 lace_size = calloc(*laces, sizeof(uint32_t));
1804 switch ((flags & 0x06) >> 1) {
1805 case 1: /* xiph lacing */
1806 for (i = 0; i < *laces - 1; i++) {
1807 lace_size[i] = 0;
1808 do {
1809 if (!*size)
1810 goto error;
1811 lace_size[i] += *buffer;
1812 (*size)--;
1813 } while (*buffer++ == 0xFF);
1814 if (lace_size[i] > *size - total || total > *size)
1815 goto error;
1816 total += lace_size[i];
1818 lace_size[i] = *size - total;
1819 break;
1821 case 2: /* fixed-size lacing */
1822 for (i = 0; i < *laces; i++)
1823 lace_size[i] = *size / *laces;
1824 break;
1826 case 3:; /* EBML lacing */
1827 int l;
1828 uint64_t num = ebml_read_vlen_uint(buffer, &l);
1829 if (num == EBML_UINT_INVALID)
1830 goto error;
1831 buffer += l;
1832 if (*size < l)
1833 goto error;
1834 *size -= l;
1835 if (num > *size)
1836 goto error;
1838 total = lace_size[0] = num;
1839 for (i = 1; i < *laces - 1; i++) {
1840 int64_t snum = ebml_read_vlen_int(buffer, &l);
1841 if (snum == EBML_INT_INVALID)
1842 goto error;
1843 buffer += l;
1844 if (*size < l)
1845 goto error;
1846 *size -= l;
1847 lace_size[i] = lace_size[i - 1] + snum;
1848 if (lace_size[i] > *size - total || total > *size)
1849 goto error;
1850 total += lace_size[i];
1852 lace_size[i] = *size - total;
1853 break;
1855 break;
1857 *all_lace_sizes = lace_size;
1858 return 0;
1860 error:
1861 free(lace_size);
1862 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] Bad input [lacing]\n");
1863 return 1;
1866 static void handle_realvideo(demuxer_t *demuxer, mkv_track_t *track,
1867 uint8_t *buffer, uint32_t size, int64_t block_bref)
1869 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1870 demux_packet_t *dp;
1871 uint32_t timestamp = mkv_d->last_pts * 1000;
1873 dp = new_demux_packet(size);
1874 memcpy(dp->buffer, buffer, size);
1876 if (mkv_d->v_skip_to_keyframe) {
1877 dp->pts = mkv_d->last_pts;
1878 track->rv_kf_base = 0;
1879 track->rv_kf_pts = timestamp;
1880 } else
1881 dp->pts =
1882 real_fix_timestamp(dp->buffer, timestamp,
1883 ((sh_video_t *) demuxer->video->sh)->bih->
1884 biCompression, &track->rv_kf_base,
1885 &track->rv_kf_pts, NULL);
1886 dp->pos = demuxer->filepos;
1887 dp->flags = block_bref ? 0 : 0x10;
1889 ds_add_packet(demuxer->video, dp);
1892 static void handle_realaudio(demuxer_t *demuxer, mkv_track_t *track,
1893 uint8_t *buffer, uint32_t size, int64_t block_bref)
1895 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1896 int sps = track->sub_packet_size;
1897 int sph = track->sub_packet_h;
1898 int cfs = track->coded_framesize;
1899 int w = track->audiopk_size;
1900 int spc = track->sub_packet_cnt;
1901 demux_packet_t *dp;
1902 int x;
1904 if ((track->a_formattag == mmioFOURCC('2', '8', '_', '8'))
1905 || (track->a_formattag == mmioFOURCC('c', 'o', 'o', 'k'))
1906 || (track->a_formattag == mmioFOURCC('a', 't', 'r', 'c'))
1907 || (track->a_formattag == mmioFOURCC('s', 'i', 'p', 'r'))) {
1908 // if(!block_bref)
1909 // spc = track->sub_packet_cnt = 0;
1910 switch (track->a_formattag) {
1911 case mmioFOURCC('2', '8', '_', '8'):
1912 for (x = 0; x < sph / 2; x++)
1913 memcpy(track->audio_buf + x * 2 * w + spc * cfs,
1914 buffer + cfs * x, cfs);
1915 break;
1916 case mmioFOURCC('c', 'o', 'o', 'k'):
1917 case mmioFOURCC('a', 't', 'r', 'c'):
1918 for (x = 0; x < w / sps; x++)
1919 memcpy(track->audio_buf +
1920 sps * (sph * x + ((sph + 1) / 2) * (spc & 1) +
1921 (spc >> 1)), buffer + sps * x, sps);
1922 break;
1923 case mmioFOURCC('s', 'i', 'p', 'r'):
1924 memcpy(track->audio_buf + spc * w, buffer, w);
1925 if (spc == sph - 1) {
1926 int n;
1927 int bs = sph * w * 2 / 96; // nibbles per subpacket
1928 // Perform reordering
1929 for (n = 0; n < 38; n++) {
1930 int j;
1931 int i = bs * sipr_swaps[n][0];
1932 int o = bs * sipr_swaps[n][1];
1933 // swap nibbles of block 'i' with 'o' TODO: optimize
1934 for (j = 0; j < bs; j++) {
1935 int x = (i & 1) ?
1936 (track->audio_buf[i >> 1] >> 4) :
1937 (track->audio_buf[i >> 1] & 0x0F);
1938 int y = (o & 1) ?
1939 (track->audio_buf[o >> 1] >> 4) :
1940 (track->audio_buf[o >> 1] & 0x0F);
1941 if (o & 1)
1942 track->audio_buf[o >> 1] =
1943 (track->audio_buf[o >> 1] & 0x0F) | (x << 4);
1944 else
1945 track->audio_buf[o >> 1] =
1946 (track->audio_buf[o >> 1] & 0xF0) | x;
1947 if (i & 1)
1948 track->audio_buf[i >> 1] =
1949 (track->audio_buf[i >> 1] & 0x0F) | (y << 4);
1950 else
1951 track->audio_buf[i >> 1] =
1952 (track->audio_buf[i >> 1] & 0xF0) | y;
1953 ++i;
1954 ++o;
1958 break;
1960 track->audio_timestamp[track->sub_packet_cnt] =
1961 (track->ra_pts == mkv_d->last_pts) ? 0 : (mkv_d->last_pts);
1962 track->ra_pts = mkv_d->last_pts;
1963 if (track->sub_packet_cnt == 0)
1964 track->audio_filepos = demuxer->filepos;
1965 if (++(track->sub_packet_cnt) == sph) {
1966 int apk_usize =
1967 ((sh_audio_t *) demuxer->audio->sh)->wf->nBlockAlign;
1968 track->sub_packet_cnt = 0;
1969 // Release all the audio packets
1970 for (x = 0; x < sph * w / apk_usize; x++) {
1971 dp = new_demux_packet(apk_usize);
1972 memcpy(dp->buffer, track->audio_buf + x * apk_usize,
1973 apk_usize);
1974 /* Put timestamp only on packets that correspond to original
1975 * audio packets in file */
1976 dp->pts = (x * apk_usize % w) ? 0 :
1977 track->audio_timestamp[x * apk_usize / w];
1978 dp->pos = track->audio_filepos; // all equal
1979 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
1980 ds_add_packet(demuxer->audio, dp);
1983 } else { // Not a codec that require reordering
1984 dp = new_demux_packet(size);
1985 memcpy(dp->buffer, buffer, size);
1986 if (track->ra_pts == mkv_d->last_pts && !mkv_d->a_skip_to_keyframe)
1987 dp->pts = 0;
1988 else
1989 dp->pts = mkv_d->last_pts;
1990 track->ra_pts = mkv_d->last_pts;
1992 dp->pos = demuxer->filepos;
1993 dp->flags = block_bref ? 0 : 0x10;
1994 ds_add_packet(demuxer->audio, dp);
1998 static int handle_block(demuxer_t *demuxer, uint8_t *block, uint64_t length,
1999 uint64_t block_duration, int64_t block_bref,
2000 int64_t block_fref, uint8_t simpleblock)
2002 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2003 mkv_track_t *track = NULL;
2004 demux_stream_t *ds = NULL;
2005 uint64_t old_length;
2006 uint64_t tc;
2007 uint32_t *lace_size;
2008 uint8_t laces, flags;
2009 int i, num, tmp, use_this_block = 1;
2010 double current_pts;
2011 int16_t time;
2013 /* first byte(s): track num */
2014 num = ebml_read_vlen_uint(block, &tmp);
2015 block += tmp;
2016 /* time (relative to cluster time) */
2017 time = block[0] << 8 | block[1];
2018 block += 2;
2019 length -= tmp + 2;
2020 old_length = length;
2021 flags = block[0];
2022 if (demux_mkv_read_block_lacing(block, &length, &laces, &lace_size))
2023 return 0;
2024 block += old_length - length;
2026 tc = time * mkv_d->tc_scale + mkv_d->cluster_tc;
2027 current_pts = tc / 1e9;
2029 for (i = 0; i < mkv_d->num_tracks; i++)
2030 if (mkv_d->tracks[i]->tnum == num) {
2031 track = mkv_d->tracks[i];
2032 break;
2034 if (track == NULL) {
2035 free(lace_size);
2036 return 1;
2038 if (track->type == MATROSKA_TRACK_AUDIO
2039 && track->id == demuxer->audio->id) {
2040 ds = demuxer->audio;
2042 if (mkv_d->a_skip_to_keyframe) {
2043 if (simpleblock) {
2044 if (!(flags & 0x80)) /*current frame isn't a keyframe */
2045 use_this_block = 0;
2046 } else if (block_bref != 0)
2047 use_this_block = 0;
2049 if (mkv_d->v_skip_to_keyframe)
2050 use_this_block = 0;
2052 if (track->fix_i_bps && use_this_block) {
2053 sh_audio_t *sh = (sh_audio_t *) ds->sh;
2055 if (block_duration != 0) {
2056 sh->i_bps = length * 1e9 / block_duration;
2057 track->fix_i_bps = 0;
2058 } else if (track->qt_last_a_pts == 0.0)
2059 track->qt_last_a_pts = current_pts;
2060 else if (track->qt_last_a_pts != current_pts) {
2061 sh->i_bps = length / (current_pts - track->qt_last_a_pts);
2062 track->fix_i_bps = 0;
2065 } else if (tc < mkv_d->skip_to_timecode)
2066 use_this_block = 0;
2067 else if (track->type == MATROSKA_TRACK_VIDEO
2068 && track->id == demuxer->video->id) {
2069 ds = demuxer->video;
2070 if (mkv_d->v_skip_to_keyframe) {
2071 if (simpleblock) {
2072 if (!(flags & 0x80)) /*current frame isn't a keyframe */
2073 use_this_block = 0;
2074 } else if (block_bref != 0 || block_fref != 0)
2075 use_this_block = 0;
2077 } else if (track->type == MATROSKA_TRACK_SUBTITLE
2078 && track->id == demuxer->sub->id) {
2079 ds = demuxer->sub;
2080 if (laces > 1) {
2081 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Subtitles use Matroska "
2082 "lacing. This is abnormal and not supported.\n");
2083 use_this_block = 0;
2085 sub_utf8 = 1; // XXX this variable should be eventually removed
2086 } else
2087 use_this_block = 0;
2089 if (use_this_block) {
2090 mkv_d->last_pts = current_pts;
2091 mkv_d->last_filepos = demuxer->filepos;
2093 for (i = 0; i < laces; i++) {
2094 if (ds == demuxer->video && track->realmedia)
2095 handle_realvideo(demuxer, track, block, lace_size[i],
2096 block_bref);
2097 else if (ds == demuxer->audio && track->realmedia)
2098 handle_realaudio(demuxer, track, block, lace_size[i],
2099 block_bref);
2100 else {
2101 int size = lace_size[i];
2102 demux_packet_t *dp;
2103 uint8_t *buffer;
2104 demux_mkv_decode(track, block, &buffer, &size, 1);
2105 if (buffer) {
2106 dp = new_demux_packet(size);
2107 memcpy(dp->buffer, buffer, size);
2108 if (buffer != block)
2109 talloc_free(buffer);
2110 dp->flags = (block_bref == 0
2111 && block_fref == 0) ? 0x10 : 0;
2112 /* If default_duration is 0, assume no pts value is known
2113 * for packets after the first one (rather than all pts
2114 * values being the same) */
2115 if (i == 0 || track->default_duration)
2116 dp->pts =
2117 mkv_d->last_pts + i * track->default_duration;
2118 dp->duration = block_duration / 1e9;
2119 ds_add_packet(ds, dp);
2122 block += lace_size[i];
2125 if (ds == demuxer->video) {
2126 mkv_d->v_skip_to_keyframe = 0;
2127 mkv_d->skip_to_timecode = 0;
2128 } else if (ds == demuxer->audio)
2129 mkv_d->a_skip_to_keyframe = 0;
2131 free(lace_size);
2132 return 1;
2135 free(lace_size);
2136 return 0;
2139 static int demux_mkv_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
2141 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2142 stream_t *s = demuxer->stream;
2143 uint64_t l;
2144 int il, tmp;
2146 while (1) {
2147 while (mkv_d->cluster_size > 0) {
2148 uint64_t block_duration = 0, block_length = 0;
2149 int64_t block_bref = 0, block_fref = 0;
2150 uint8_t *block = NULL;
2152 while (mkv_d->blockgroup_size > 0) {
2153 switch (ebml_read_id(s, &il)) {
2154 case MATROSKA_ID_BLOCKDURATION:
2155 block_duration = ebml_read_uint(s, &l);
2156 if (block_duration == EBML_UINT_INVALID) {
2157 free(block);
2158 return 0;
2160 block_duration *= mkv_d->tc_scale;
2161 break;
2163 case MATROSKA_ID_BLOCK:
2164 block_length = ebml_read_length(s, &tmp);
2165 free(block);
2166 if (block_length > 500000000)
2167 return 0;
2168 block = malloc(block_length + AV_LZO_INPUT_PADDING);
2169 demuxer->filepos = stream_tell(s);
2170 if (stream_read(s, block, block_length) !=
2171 (int) block_length) {
2172 free(block);
2173 return 0;
2175 l = tmp + block_length;
2176 break;
2178 case MATROSKA_ID_REFERENCEBLOCK:;
2179 int64_t num = ebml_read_int(s, &l);
2180 if (num == EBML_INT_INVALID) {
2181 free(block);
2182 return 0;
2184 if (num <= 0)
2185 block_bref = num;
2186 else
2187 block_fref = num;
2188 break;
2190 case EBML_ID_INVALID:
2191 free(block);
2192 return 0;
2194 default:
2195 ebml_read_skip(s, &l);
2196 break;
2198 mkv_d->blockgroup_size -= l + il;
2199 mkv_d->cluster_size -= l + il;
2202 if (block) {
2203 int res = handle_block(demuxer, block, block_length,
2204 block_duration, block_bref, block_fref,
2206 free(block);
2207 if (res < 0)
2208 return 0;
2209 if (res)
2210 return 1;
2213 if (mkv_d->cluster_size > 0) {
2214 switch (ebml_read_id(s, &il)) {
2215 case MATROSKA_ID_TIMECODE:;
2216 uint64_t num = ebml_read_uint(s, &l);
2217 if (num == EBML_UINT_INVALID)
2218 return 0;
2219 mkv_d->cluster_tc = num * mkv_d->tc_scale;
2220 add_cluster_position(mkv_d, mkv_d->cluster_start,
2221 mkv_d->cluster_tc);
2222 break;
2224 case MATROSKA_ID_BLOCKGROUP:
2225 mkv_d->blockgroup_size = ebml_read_length(s, &tmp);
2226 l = tmp;
2227 break;
2229 case MATROSKA_ID_SIMPLEBLOCK:;
2230 int res;
2231 block_length = ebml_read_length(s, &tmp);
2232 if (block_length > 500000000)
2233 return 0;
2234 block = malloc(block_length);
2235 demuxer->filepos = stream_tell(s);
2236 if (stream_read(s, block, block_length) !=
2237 (int) block_length) {
2238 free(block);
2239 return 0;
2241 l = tmp + block_length;
2242 res = handle_block(demuxer, block, block_length,
2243 block_duration, block_bref,
2244 block_fref, 1);
2245 free(block);
2246 mkv_d->cluster_size -= l + il;
2247 if (res < 0)
2248 return 0;
2249 else if (res)
2250 return 1;
2251 else
2252 mkv_d->cluster_size += l + il;
2253 break;
2255 case EBML_ID_INVALID:
2256 return 0;
2258 default:
2259 ebml_read_skip(s, &l);
2260 break;
2262 mkv_d->cluster_size -= l + il;
2266 while (ebml_read_id(s, &il) != MATROSKA_ID_CLUSTER) {
2267 ebml_read_skip(s, NULL);
2268 if (s->eof)
2269 return 0;
2271 mkv_d->cluster_start = stream_tell(s) - il;
2272 mkv_d->cluster_size = ebml_read_length(s, NULL);
2275 return 0;
2278 static int seek_creating_index(struct demuxer *demuxer, float rel_seek_secs,
2279 int flags)
2281 struct mkv_demuxer *mkv_d = demuxer->priv;
2282 struct stream *s = demuxer->stream;
2283 int64_t target_tc_ns = (int64_t) (rel_seek_secs * 1e9);
2284 if (target_tc_ns < 0)
2285 target_tc_ns = 0;
2286 uint64_t max_filepos = 0;
2287 int64_t max_tc = -1;
2288 int n = mkv_d->num_cluster_pos;
2289 if (n > 0) {
2290 max_filepos = mkv_d->cluster_positions[n - 1].filepos;
2291 max_tc = mkv_d->cluster_positions[n - 1].timecode;
2294 if (target_tc_ns > max_tc) {
2295 if ((off_t) max_filepos > stream_tell(s))
2296 stream_seek(s, max_filepos);
2297 else
2298 stream_seek(s, stream_tell(s) + mkv_d->cluster_size);
2299 /* parse all the clusters upto target_filepos */
2300 while (!s->eof) {
2301 uint64_t start = stream_tell(s);
2302 uint32_t type = ebml_read_id(s, NULL);
2303 uint64_t len = ebml_read_length(s, NULL);
2304 uint64_t end = stream_tell(s) + len;
2305 if (type == MATROSKA_ID_CLUSTER) {
2306 while (!s->eof && stream_tell(s) < end) {
2307 if (ebml_read_id(s, NULL) == MATROSKA_ID_TIMECODE) {
2308 uint64_t tc = ebml_read_uint(s, NULL);
2309 tc *= mkv_d->tc_scale;
2310 add_cluster_position(mkv_d, start, tc);
2311 if (tc >= target_tc_ns)
2312 goto enough_index;
2313 break;
2317 if (s->eof)
2318 break;
2319 stream_seek(s, end);
2321 enough_index:
2322 if (s->eof)
2323 stream_reset(s);
2325 if (!mkv_d->num_cluster_pos) {
2326 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] no target for seek found\n");
2327 return -1;
2329 uint64_t cluster_pos = mkv_d->cluster_positions[0].filepos;
2330 /* Let's find the nearest cluster */
2331 int64_t min_diff = 0xFFFFFFFFFFFFFFF;
2332 for (int i = 0; i < mkv_d->num_cluster_pos; i++) {
2333 int64_t diff = mkv_d->cluster_positions[i].timecode - target_tc_ns;
2334 if (flags & SEEK_BACKWARD && diff < 0 && -diff < min_diff) {
2335 cluster_pos = mkv_d->cluster_positions[i].filepos;
2336 min_diff = -diff;
2337 } else if (flags & SEEK_FORWARD
2338 && (diff < 0 ? -1 * diff : diff) < min_diff) {
2339 cluster_pos = mkv_d->cluster_positions[i].filepos;
2340 min_diff = diff < 0 ? -1 * diff : diff;
2343 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
2344 stream_seek(s, cluster_pos);
2345 return 0;
2348 static struct mkv_index *seek_with_cues(struct demuxer *demuxer, int seek_id,
2349 int64_t target_timecode, int flags)
2351 struct mkv_demuxer *mkv_d = demuxer->priv;
2352 struct mkv_index *index = NULL;
2354 /* Find the entry in the index closest to the target timecode in the
2355 * give direction. If there are no such entries - we're trying to seek
2356 * backward from a target time before the first entry or forward from a
2357 * target time after the last entry - then still seek to the first/last
2358 * entry if that's further in the direction wanted than mkv_d->last_pts.
2360 int64_t min_diff = target_timecode - (int64_t)(mkv_d->last_pts * 1e9 + 0.5);
2361 if (flags & SEEK_BACKWARD)
2362 min_diff = -min_diff;
2363 min_diff = FFMAX(min_diff, 1);
2364 for (int i = 0; i < mkv_d->num_indexes; i++)
2365 if (seek_id < 0 || mkv_d->indexes[i].tnum == seek_id) {
2366 int64_t diff =
2367 target_timecode -
2368 (int64_t) (mkv_d->indexes[i].timecode * mkv_d->tc_scale);
2369 if (flags & SEEK_BACKWARD)
2370 diff = -diff;
2371 if (diff <= 0) {
2372 if (min_diff <= 0 && diff <= min_diff)
2373 continue;
2374 } else if (diff >= min_diff)
2375 continue;
2376 min_diff = diff;
2377 index = mkv_d->indexes + i;
2380 if (index) { /* We've found an entry. */
2381 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
2382 stream_seek(demuxer->stream, index->filepos);
2384 return index;
2387 static void demux_mkv_seek(demuxer_t *demuxer, float rel_seek_secs,
2388 float audio_delay, int flags)
2390 mkv_demuxer_t *mkv_d = demuxer->priv;
2391 uint64_t v_tnum = -1;
2392 if (demuxer->video->id >= 0)
2393 v_tnum = find_track_by_num(mkv_d, demuxer->video->id,
2394 MATROSKA_TRACK_VIDEO)->tnum;
2395 uint64_t a_tnum = -1;
2396 if (demuxer->audio->id >= 0)
2397 a_tnum = find_track_by_num(mkv_d, demuxer->audio->id,
2398 MATROSKA_TRACK_AUDIO)->tnum;
2399 if (!(flags & (SEEK_BACKWARD | SEEK_FORWARD))) {
2400 if (flags & SEEK_ABSOLUTE || rel_seek_secs < 0)
2401 flags |= SEEK_BACKWARD;
2402 else
2403 flags |= SEEK_FORWARD;
2405 // Adjust the target a little bit to catch cases where the target position
2406 // specifies a keyframe with high, but not perfect, precision.
2407 rel_seek_secs += flags & SEEK_FORWARD ? -0.005 : 0.005;
2409 if (!(flags & SEEK_FACTOR)) { /* time in secs */
2410 mkv_index_t *index = NULL;
2412 if (!(flags & SEEK_ABSOLUTE)) /* relative seek */
2413 rel_seek_secs += mkv_d->last_pts;
2414 rel_seek_secs = FFMAX(rel_seek_secs, 0);
2415 int64_t target_timecode = rel_seek_secs * 1e9 + 0.5;
2417 if (mkv_d->indexes == NULL) { /* no index was found */
2418 if (seek_creating_index(demuxer, rel_seek_secs, flags) < 0)
2419 return;
2420 } else {
2421 int seek_id = (demuxer->video->id < 0) ?
2422 a_tnum : v_tnum;
2423 index = seek_with_cues(demuxer, seek_id, target_timecode, flags);
2424 if (!index)
2425 index = seek_with_cues(demuxer, -1, target_timecode, flags);
2428 if (demuxer->video->id >= 0)
2429 mkv_d->v_skip_to_keyframe = 1;
2430 if (flags & SEEK_FORWARD)
2431 mkv_d->skip_to_timecode = target_timecode;
2432 else
2433 mkv_d->skip_to_timecode = index ? index->timecode * mkv_d->tc_scale
2434 : 0;
2435 mkv_d->a_skip_to_keyframe = 1;
2437 demux_mkv_fill_buffer(demuxer, NULL);
2438 } else if ((demuxer->movi_end <= 0) || !(flags & SEEK_ABSOLUTE))
2439 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
2440 else {
2441 stream_t *s = demuxer->stream;
2442 uint64_t target_filepos;
2443 mkv_index_t *index = NULL;
2444 int i;
2446 if (mkv_d->indexes == NULL) { /* not implemented without index */
2447 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
2448 return;
2451 target_filepos = (uint64_t) (demuxer->movi_end * rel_seek_secs);
2452 for (i = 0; i < mkv_d->num_indexes; i++)
2453 if (mkv_d->indexes[i].tnum == v_tnum)
2454 if ((index == NULL)
2455 || ((mkv_d->indexes[i].filepos >= target_filepos)
2456 && ((index->filepos < target_filepos)
2457 || (mkv_d->indexes[i].filepos < index->filepos))))
2458 index = &mkv_d->indexes[i];
2460 if (!index)
2461 return;
2463 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
2464 stream_seek(s, index->filepos);
2466 if (demuxer->video->id >= 0)
2467 mkv_d->v_skip_to_keyframe = 1;
2468 mkv_d->skip_to_timecode = index->timecode * mkv_d->tc_scale;
2469 mkv_d->a_skip_to_keyframe = 1;
2471 demux_mkv_fill_buffer(demuxer, NULL);
2475 static int demux_mkv_control(demuxer_t *demuxer, int cmd, void *arg)
2477 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2479 switch (cmd) {
2480 case DEMUXER_CTRL_CORRECT_PTS:
2481 return DEMUXER_CTRL_OK;
2482 case DEMUXER_CTRL_GET_TIME_LENGTH:
2483 if (mkv_d->duration == 0)
2484 return DEMUXER_CTRL_DONTKNOW;
2486 *((double *) arg) = (double) mkv_d->duration;
2487 return DEMUXER_CTRL_OK;
2489 case DEMUXER_CTRL_GET_PERCENT_POS:
2490 if (mkv_d->duration == 0) {
2491 return DEMUXER_CTRL_DONTKNOW;
2494 *((int *) arg) = (int) (100 * mkv_d->last_pts / mkv_d->duration);
2495 return DEMUXER_CTRL_OK;
2497 case DEMUXER_CTRL_SWITCH_AUDIO:;
2498 int new_aid = *(int *) arg;
2499 int current_aid = demuxer->audio->id;
2500 if (current_aid < 0)
2501 current_aid = -1;
2502 if (new_aid == -1) // cycle to next
2503 new_aid = (current_aid + 2) % (mkv_d->num_audio_tracks + 1) - 1;
2504 if (new_aid < 0 || new_aid >= mkv_d->num_audio_tracks)
2505 new_aid = -2;
2506 *(int *) arg = new_aid;
2507 if (current_aid != new_aid)
2508 ds_free_packs(demuxer->audio);
2509 demuxer->audio->id = new_aid;
2510 return DEMUXER_CTRL_OK;
2512 default:
2513 return DEMUXER_CTRL_NOTIMPL;
2517 const demuxer_desc_t demuxer_desc_matroska = {
2518 "Matroska demuxer",
2519 "mkv",
2520 "Matroska",
2521 "Aurelien Jacobs",
2523 DEMUXER_TYPE_MATROSKA,
2524 1, // safe autodetect
2525 demux_mkv_open,
2526 demux_mkv_fill_buffer,
2527 NULL,
2528 demux_close_mkv,
2529 demux_mkv_seek,
2530 demux_mkv_control