demux_mkv: support E-AC-3 audio
[mplayer/greg.git] / libmpdemux / demux_mkv.c
blob58570855ae63ed30d53e1b04dfab92849eb0341e
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 = MATROSKA_SUBTYPE_VOBSUB;
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 = MATROSKA_SUBTYPE_SSA;
613 } else if (!strcmp(track->codec_id, MKV_S_TEXTASCII))
614 track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
615 if (!strcmp(track->codec_id, MKV_S_TEXTUTF8)) {
616 track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
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 != MATROSKA_SUBTYPE_UNKNOWN) {
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 = 't';
1576 if (track->subtitle_type == MATROSKA_SUBTYPE_VOBSUB)
1577 sh->type = 'v';
1578 if (track->subtitle_type == MATROSKA_SUBTYPE_SSA)
1579 sh->type = 'a';
1580 size = track->private_size;
1581 demux_mkv_decode(track, track->private_data, &buffer, &size, 2);
1582 if (buffer && buffer != track->private_data) {
1583 talloc_free(track->private_data);
1584 talloc_steal(track, buffer);
1585 track->private_data = buffer;
1586 track->private_size = size;
1588 sh->extradata = malloc(track->private_size);
1589 memcpy(sh->extradata, track->private_data, track->private_size);
1590 sh->extradata_len = track->private_size;
1591 if (track->language && (strcmp(track->language, "und") != 0))
1592 sh->lang = strdup(track->language);
1593 sh->default_track = track->default_track;
1594 } else {
1595 mp_tmsg(MSGT_DEMUX, MSGL_ERR,
1596 "[mkv] Subtitle type '%s' is not supported.\n",
1597 track->codec_id);
1598 return 1;
1601 return 0;
1604 static int demux_mkv_open(demuxer_t *demuxer)
1606 stream_t *s = demuxer->stream;
1607 mkv_demuxer_t *mkv_d;
1608 mkv_track_t *track;
1609 int i, cont = 0;
1611 stream_seek(s, s->start_pos);
1612 if (ebml_read_id(s, NULL) != EBML_ID_EBML)
1613 return 0;
1614 struct ebml_ebml ebml_master = {};
1615 struct ebml_parse_ctx parse_ctx = { .no_error_messages = true };
1616 if (ebml_read_element(s, &parse_ctx, &ebml_master, &ebml_ebml_desc) < 0)
1617 return 0;
1618 if (ebml_master.doc_type.start == NULL) {
1619 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] File has EBML header but no doctype."
1620 " Assuming \"matroska\".\n");
1621 } else if (bstrcmp(ebml_master.doc_type, BSTR("matroska")) != 0
1622 && bstrcmp(ebml_master.doc_type, BSTR("webm")) != 0) {
1623 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] no head found\n");
1624 talloc_free(parse_ctx.talloc_ctx);
1625 return 0;
1627 if (ebml_master.doc_type_read_version > 2) {
1628 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] This looks like a Matroska file, "
1629 "but we don't support format version %"PRIu64"\n",
1630 ebml_master.doc_type_read_version);
1631 talloc_free(parse_ctx.talloc_ctx);
1632 return 0;
1634 if ((ebml_master.n_ebml_read_version
1635 && ebml_master.ebml_read_version != EBML_VERSION)
1636 || (ebml_master.n_ebml_max_size_length
1637 && ebml_master.ebml_max_size_length > 8)
1638 || (ebml_master.n_ebml_max_id_length
1639 && ebml_master.ebml_max_id_length != 4)) {
1640 mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] This looks like a Matroska file, "
1641 "but the header has bad parameters\n");
1642 talloc_free(parse_ctx.talloc_ctx);
1643 return 0;
1645 talloc_free(parse_ctx.talloc_ctx);
1647 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Found the head...\n");
1649 if (ebml_read_id(s, NULL) != MATROSKA_ID_SEGMENT) {
1650 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n");
1651 return 0;
1653 ebml_read_length(s, NULL); /* return bytes number until EOF */
1655 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] + a segment...\n");
1657 mkv_d = talloc_zero(demuxer, struct mkv_demuxer);
1658 demuxer->priv = mkv_d;
1659 mkv_d->tc_scale = 1000000;
1660 mkv_d->segment_start = stream_tell(s);
1662 while (!cont) {
1663 uint32_t id = ebml_read_id(s, NULL);
1664 switch (id) {
1665 case MATROSKA_ID_CLUSTER:
1666 mp_msg(MSGT_DEMUX, MSGL_V,
1667 "[mkv] |+ found cluster, headers are "
1668 "parsed completely :)\n");
1669 stream_seek(s, stream_tell(s) - 4);
1670 cont = 1;
1671 break;
1673 default:
1674 cont = read_header_element(demuxer, id, 0) < 1;
1675 break;
1676 case EBML_ID_VOID:
1677 ebml_read_skip(s, NULL);
1678 break;
1682 display_create_tracks(demuxer);
1684 /* select video track */
1685 track = NULL;
1686 if (demuxer->video->id == -1) { /* automatically select a video track */
1687 /* search for a video track that has the 'default' flag set */
1688 for (i = 0; i < mkv_d->num_tracks; i++)
1689 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO
1690 && mkv_d->tracks[i]->default_track) {
1691 track = mkv_d->tracks[i];
1692 break;
1695 if (track == NULL)
1696 /* no track has the 'default' flag set */
1697 /* let's take the first video track */
1698 for (i = 0; i < mkv_d->num_tracks; i++)
1699 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO
1700 && mkv_d->tracks[i]->id >= 0) {
1701 track = mkv_d->tracks[i];
1702 break;
1704 } else if (demuxer->video->id != -2) /* -2 = no video at all */
1705 track = find_track_by_num(mkv_d, demuxer->video->id,
1706 MATROSKA_TRACK_VIDEO);
1708 if (track && demuxer->v_streams[track->id]) {
1709 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[mkv] Will play video track %u.\n",
1710 track->tnum);
1711 demuxer->video->id = track->id;
1712 demuxer->video->sh = demuxer->v_streams[track->id];
1713 } else {
1714 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[mkv] No video track found/wanted.\n");
1715 demuxer->video->id = -2;
1718 /* select audio track */
1719 track = NULL;
1720 if (track == NULL)
1721 /* search for an audio track that has the 'default' flag set */
1722 for (i = 0; i < mkv_d->num_tracks; i++)
1723 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO
1724 && mkv_d->tracks[i]->default_track) {
1725 track = mkv_d->tracks[i];
1726 break;
1729 if (track == NULL)
1730 /* no track has the 'default' flag set */
1731 /* let's take the first audio track */
1732 for (i = 0; i < mkv_d->num_tracks; i++)
1733 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO
1734 && mkv_d->tracks[i]->id >= 0) {
1735 track = mkv_d->tracks[i];
1736 break;
1739 if (track && demuxer->a_streams[track->id]) {
1740 demuxer->audio->id = track->id;
1741 demuxer->audio->sh = demuxer->a_streams[track->id];
1742 } else {
1743 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[mkv] No audio track found/wanted.\n");
1744 demuxer->audio->id = -2;
1747 if (s->end_pos == 0)
1748 demuxer->seekable = 0;
1749 else {
1750 demuxer->movi_start = s->start_pos;
1751 demuxer->movi_end = s->end_pos;
1752 demuxer->seekable = 1;
1755 demuxer->accurate_seek = true;
1757 return DEMUXER_TYPE_MATROSKA;
1760 static void demux_close_mkv(demuxer_t *demuxer)
1762 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1764 if (mkv_d) {
1765 int i;
1766 if (mkv_d->tracks) {
1767 for (i = 0; i < mkv_d->num_tracks; i++)
1768 demux_mkv_free_trackentry(mkv_d->tracks[i]);
1770 free(mkv_d->indexes);
1771 free(mkv_d->cluster_positions);
1775 static int demux_mkv_read_block_lacing(uint8_t *buffer, uint64_t *size,
1776 uint8_t *laces,
1777 uint32_t **all_lace_sizes)
1779 uint32_t total = 0;
1780 uint32_t *lace_size = NULL;
1781 uint8_t flags;
1782 int i;
1784 *all_lace_sizes = NULL;
1785 /* lacing flags */
1786 if (*size < 1)
1787 goto error;
1788 flags = *buffer++;
1789 (*size)--;
1791 switch ((flags & 0x06) >> 1) {
1792 case 0: /* no lacing */
1793 *laces = 1;
1794 lace_size = calloc(*laces, sizeof(uint32_t));
1795 lace_size[0] = *size;
1796 break;
1798 case 1: /* xiph lacing */
1799 case 2: /* fixed-size lacing */
1800 case 3: /* EBML lacing */
1801 if (*size < 1)
1802 goto error;
1803 *laces = *buffer++;
1804 (*size)--;
1805 (*laces)++;
1806 lace_size = calloc(*laces, sizeof(uint32_t));
1808 switch ((flags & 0x06) >> 1) {
1809 case 1: /* xiph lacing */
1810 for (i = 0; i < *laces - 1; i++) {
1811 lace_size[i] = 0;
1812 do {
1813 if (!*size)
1814 goto error;
1815 lace_size[i] += *buffer;
1816 (*size)--;
1817 } while (*buffer++ == 0xFF);
1818 if (lace_size[i] > *size - total || total > *size)
1819 goto error;
1820 total += lace_size[i];
1822 lace_size[i] = *size - total;
1823 break;
1825 case 2: /* fixed-size lacing */
1826 for (i = 0; i < *laces; i++)
1827 lace_size[i] = *size / *laces;
1828 break;
1830 case 3:; /* EBML lacing */
1831 int l;
1832 uint64_t num = ebml_read_vlen_uint(buffer, &l);
1833 if (num == EBML_UINT_INVALID)
1834 goto error;
1835 buffer += l;
1836 if (*size < l)
1837 goto error;
1838 *size -= l;
1839 if (num > *size)
1840 goto error;
1842 total = lace_size[0] = num;
1843 for (i = 1; i < *laces - 1; i++) {
1844 int64_t snum = ebml_read_vlen_int(buffer, &l);
1845 if (snum == EBML_INT_INVALID)
1846 goto error;
1847 buffer += l;
1848 if (*size < l)
1849 goto error;
1850 *size -= l;
1851 lace_size[i] = lace_size[i - 1] + snum;
1852 if (lace_size[i] > *size - total || total > *size)
1853 goto error;
1854 total += lace_size[i];
1856 lace_size[i] = *size - total;
1857 break;
1859 break;
1861 *all_lace_sizes = lace_size;
1862 return 0;
1864 error:
1865 free(lace_size);
1866 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] Bad input [lacing]\n");
1867 return 1;
1870 static void handle_subtitles(demuxer_t *demuxer, mkv_track_t *track,
1871 char *block, int64_t size,
1872 uint64_t block_duration, uint64_t timecode)
1874 demux_packet_t *dp;
1876 if (block_duration == 0) {
1877 mp_msg(MSGT_DEMUX, MSGL_WARN,
1878 "[mkv] Warning: No BlockDuration for subtitle track found.\n");
1879 return;
1882 sub_utf8 = 1;
1883 dp = new_demux_packet(size);
1884 memcpy(dp->buffer, block, size);
1885 dp->pts = timecode / 1e9;
1886 dp->duration = block_duration / 1e9;
1887 ds_add_packet(demuxer->sub, dp);
1890 static void handle_realvideo(demuxer_t *demuxer, mkv_track_t *track,
1891 uint8_t *buffer, uint32_t size, int64_t block_bref)
1893 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1894 demux_packet_t *dp;
1895 uint32_t timestamp = mkv_d->last_pts * 1000;
1897 dp = new_demux_packet(size);
1898 memcpy(dp->buffer, buffer, size);
1900 if (mkv_d->v_skip_to_keyframe) {
1901 dp->pts = mkv_d->last_pts;
1902 track->rv_kf_base = 0;
1903 track->rv_kf_pts = timestamp;
1904 } else
1905 dp->pts =
1906 real_fix_timestamp(dp->buffer, timestamp,
1907 ((sh_video_t *) demuxer->video->sh)->bih->
1908 biCompression, &track->rv_kf_base,
1909 &track->rv_kf_pts, NULL);
1910 dp->pos = demuxer->filepos;
1911 dp->flags = block_bref ? 0 : 0x10;
1913 ds_add_packet(demuxer->video, dp);
1916 static void handle_realaudio(demuxer_t *demuxer, mkv_track_t *track,
1917 uint8_t *buffer, uint32_t size, int64_t block_bref)
1919 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
1920 int sps = track->sub_packet_size;
1921 int sph = track->sub_packet_h;
1922 int cfs = track->coded_framesize;
1923 int w = track->audiopk_size;
1924 int spc = track->sub_packet_cnt;
1925 demux_packet_t *dp;
1926 int x;
1928 if ((track->a_formattag == mmioFOURCC('2', '8', '_', '8'))
1929 || (track->a_formattag == mmioFOURCC('c', 'o', 'o', 'k'))
1930 || (track->a_formattag == mmioFOURCC('a', 't', 'r', 'c'))
1931 || (track->a_formattag == mmioFOURCC('s', 'i', 'p', 'r'))) {
1932 // if(!block_bref)
1933 // spc = track->sub_packet_cnt = 0;
1934 switch (track->a_formattag) {
1935 case mmioFOURCC('2', '8', '_', '8'):
1936 for (x = 0; x < sph / 2; x++)
1937 memcpy(track->audio_buf + x * 2 * w + spc * cfs,
1938 buffer + cfs * x, cfs);
1939 break;
1940 case mmioFOURCC('c', 'o', 'o', 'k'):
1941 case mmioFOURCC('a', 't', 'r', 'c'):
1942 for (x = 0; x < w / sps; x++)
1943 memcpy(track->audio_buf +
1944 sps * (sph * x + ((sph + 1) / 2) * (spc & 1) +
1945 (spc >> 1)), buffer + sps * x, sps);
1946 break;
1947 case mmioFOURCC('s', 'i', 'p', 'r'):
1948 memcpy(track->audio_buf + spc * w, buffer, w);
1949 if (spc == sph - 1) {
1950 int n;
1951 int bs = sph * w * 2 / 96; // nibbles per subpacket
1952 // Perform reordering
1953 for (n = 0; n < 38; n++) {
1954 int j;
1955 int i = bs * sipr_swaps[n][0];
1956 int o = bs * sipr_swaps[n][1];
1957 // swap nibbles of block 'i' with 'o' TODO: optimize
1958 for (j = 0; j < bs; j++) {
1959 int x = (i & 1) ?
1960 (track->audio_buf[i >> 1] >> 4) :
1961 (track->audio_buf[i >> 1] & 0x0F);
1962 int y = (o & 1) ?
1963 (track->audio_buf[o >> 1] >> 4) :
1964 (track->audio_buf[o >> 1] & 0x0F);
1965 if (o & 1)
1966 track->audio_buf[o >> 1] =
1967 (track->audio_buf[o >> 1] & 0x0F) | (x << 4);
1968 else
1969 track->audio_buf[o >> 1] =
1970 (track->audio_buf[o >> 1] & 0xF0) | x;
1971 if (i & 1)
1972 track->audio_buf[i >> 1] =
1973 (track->audio_buf[i >> 1] & 0x0F) | (y << 4);
1974 else
1975 track->audio_buf[i >> 1] =
1976 (track->audio_buf[i >> 1] & 0xF0) | y;
1977 ++i;
1978 ++o;
1982 break;
1984 track->audio_timestamp[track->sub_packet_cnt] =
1985 (track->ra_pts == mkv_d->last_pts) ? 0 : (mkv_d->last_pts);
1986 track->ra_pts = mkv_d->last_pts;
1987 if (track->sub_packet_cnt == 0)
1988 track->audio_filepos = demuxer->filepos;
1989 if (++(track->sub_packet_cnt) == sph) {
1990 int apk_usize =
1991 ((sh_audio_t *) demuxer->audio->sh)->wf->nBlockAlign;
1992 track->sub_packet_cnt = 0;
1993 // Release all the audio packets
1994 for (x = 0; x < sph * w / apk_usize; x++) {
1995 dp = new_demux_packet(apk_usize);
1996 memcpy(dp->buffer, track->audio_buf + x * apk_usize,
1997 apk_usize);
1998 /* Put timestamp only on packets that correspond to original
1999 * audio packets in file */
2000 dp->pts = (x * apk_usize % w) ? 0 :
2001 track->audio_timestamp[x * apk_usize / w];
2002 dp->pos = track->audio_filepos; // all equal
2003 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
2004 ds_add_packet(demuxer->audio, dp);
2007 } else { // Not a codec that require reordering
2008 dp = new_demux_packet(size);
2009 memcpy(dp->buffer, buffer, size);
2010 if (track->ra_pts == mkv_d->last_pts && !mkv_d->a_skip_to_keyframe)
2011 dp->pts = 0;
2012 else
2013 dp->pts = mkv_d->last_pts;
2014 track->ra_pts = mkv_d->last_pts;
2016 dp->pos = demuxer->filepos;
2017 dp->flags = block_bref ? 0 : 0x10;
2018 ds_add_packet(demuxer->audio, dp);
2022 static int handle_block(demuxer_t *demuxer, uint8_t *block, uint64_t length,
2023 uint64_t block_duration, int64_t block_bref,
2024 int64_t block_fref, uint8_t simpleblock)
2026 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2027 mkv_track_t *track = NULL;
2028 demux_stream_t *ds = NULL;
2029 uint64_t old_length;
2030 uint64_t tc;
2031 uint32_t *lace_size;
2032 uint8_t laces, flags;
2033 int i, num, tmp, use_this_block = 1;
2034 double current_pts;
2035 int16_t time;
2037 /* first byte(s): track num */
2038 num = ebml_read_vlen_uint(block, &tmp);
2039 block += tmp;
2040 /* time (relative to cluster time) */
2041 time = block[0] << 8 | block[1];
2042 block += 2;
2043 length -= tmp + 2;
2044 old_length = length;
2045 flags = block[0];
2046 if (demux_mkv_read_block_lacing(block, &length, &laces, &lace_size))
2047 return 0;
2048 block += old_length - length;
2050 tc = time * mkv_d->tc_scale + mkv_d->cluster_tc;
2051 current_pts = tc / 1e9;
2053 for (i = 0; i < mkv_d->num_tracks; i++)
2054 if (mkv_d->tracks[i]->tnum == num) {
2055 track = mkv_d->tracks[i];
2056 break;
2058 if (track == NULL) {
2059 free(lace_size);
2060 return 1;
2062 if (track->type == MATROSKA_TRACK_AUDIO
2063 && track->id == demuxer->audio->id) {
2064 ds = demuxer->audio;
2066 if (mkv_d->a_skip_to_keyframe) {
2067 if (simpleblock) {
2068 if (!(flags & 0x80)) /*current frame isn't a keyframe */
2069 use_this_block = 0;
2070 } else if (block_bref != 0)
2071 use_this_block = 0;
2073 if (mkv_d->v_skip_to_keyframe)
2074 use_this_block = 0;
2076 if (track->fix_i_bps && use_this_block) {
2077 sh_audio_t *sh = (sh_audio_t *) ds->sh;
2079 if (block_duration != 0) {
2080 sh->i_bps = length * 1e9 / block_duration;
2081 track->fix_i_bps = 0;
2082 } else if (track->qt_last_a_pts == 0.0)
2083 track->qt_last_a_pts = current_pts;
2084 else if (track->qt_last_a_pts != current_pts) {
2085 sh->i_bps = length / (current_pts - track->qt_last_a_pts);
2086 track->fix_i_bps = 0;
2089 } else if (tc < mkv_d->skip_to_timecode)
2090 use_this_block = 0;
2091 else if (track->type == MATROSKA_TRACK_VIDEO
2092 && track->id == demuxer->video->id) {
2093 ds = demuxer->video;
2094 if (mkv_d->v_skip_to_keyframe) {
2095 if (simpleblock) {
2096 if (!(flags & 0x80)) /*current frame isn't a keyframe */
2097 use_this_block = 0;
2098 } else if (block_bref != 0 || block_fref != 0)
2099 use_this_block = 0;
2101 } else if (track->type == MATROSKA_TRACK_SUBTITLE
2102 && track->id == demuxer->sub->id) {
2103 ds = demuxer->sub;
2104 if (track->subtitle_type != MATROSKA_SUBTYPE_VOBSUB) {
2105 uint8_t *buffer;
2106 int size = length;
2107 demux_mkv_decode(track, block, &buffer, &size, 1);
2108 handle_subtitles(demuxer, track, buffer, size, block_duration, tc);
2109 if (buffer != block)
2110 talloc_free(buffer);
2111 use_this_block = 0;
2113 } else
2114 use_this_block = 0;
2116 if (use_this_block) {
2117 mkv_d->last_pts = current_pts;
2118 mkv_d->last_filepos = demuxer->filepos;
2120 for (i = 0; i < laces; i++) {
2121 if (ds == demuxer->video && track->realmedia)
2122 handle_realvideo(demuxer, track, block, lace_size[i],
2123 block_bref);
2124 else if (ds == demuxer->audio && track->realmedia)
2125 handle_realaudio(demuxer, track, block, lace_size[i],
2126 block_bref);
2127 else {
2128 int size = lace_size[i];
2129 demux_packet_t *dp;
2130 uint8_t *buffer;
2131 demux_mkv_decode(track, block, &buffer, &size, 1);
2132 if (buffer) {
2133 dp = new_demux_packet(size);
2134 memcpy(dp->buffer, buffer, size);
2135 if (buffer != block)
2136 talloc_free(buffer);
2137 dp->flags = (block_bref == 0
2138 && block_fref == 0) ? 0x10 : 0;
2139 /* If default_duration is 0, assume no pts value is known
2140 * for packets after the first one (rather than all pts
2141 * values being the same) */
2142 if (i == 0 || track->default_duration)
2143 dp->pts =
2144 mkv_d->last_pts + i * track->default_duration;
2145 ds_add_packet(ds, dp);
2148 block += lace_size[i];
2151 if (ds == demuxer->video) {
2152 mkv_d->v_skip_to_keyframe = 0;
2153 mkv_d->skip_to_timecode = 0;
2154 } else if (ds == demuxer->audio)
2155 mkv_d->a_skip_to_keyframe = 0;
2157 free(lace_size);
2158 return 1;
2161 free(lace_size);
2162 return 0;
2165 static int demux_mkv_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
2167 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2168 stream_t *s = demuxer->stream;
2169 uint64_t l;
2170 int il, tmp;
2172 while (1) {
2173 while (mkv_d->cluster_size > 0) {
2174 uint64_t block_duration = 0, block_length = 0;
2175 int64_t block_bref = 0, block_fref = 0;
2176 uint8_t *block = NULL;
2178 while (mkv_d->blockgroup_size > 0) {
2179 switch (ebml_read_id(s, &il)) {
2180 case MATROSKA_ID_BLOCKDURATION:
2181 block_duration = ebml_read_uint(s, &l);
2182 if (block_duration == EBML_UINT_INVALID) {
2183 free(block);
2184 return 0;
2186 block_duration *= mkv_d->tc_scale;
2187 break;
2189 case MATROSKA_ID_BLOCK:
2190 block_length = ebml_read_length(s, &tmp);
2191 free(block);
2192 if (block_length > 500000000)
2193 return 0;
2194 block = malloc(block_length + AV_LZO_INPUT_PADDING);
2195 demuxer->filepos = stream_tell(s);
2196 if (stream_read(s, block, block_length) !=
2197 (int) block_length) {
2198 free(block);
2199 return 0;
2201 l = tmp + block_length;
2202 break;
2204 case MATROSKA_ID_REFERENCEBLOCK:;
2205 int64_t num = ebml_read_int(s, &l);
2206 if (num == EBML_INT_INVALID) {
2207 free(block);
2208 return 0;
2210 if (num <= 0)
2211 block_bref = num;
2212 else
2213 block_fref = num;
2214 break;
2216 case EBML_ID_INVALID:
2217 free(block);
2218 return 0;
2220 default:
2221 ebml_read_skip(s, &l);
2222 break;
2224 mkv_d->blockgroup_size -= l + il;
2225 mkv_d->cluster_size -= l + il;
2228 if (block) {
2229 int res = handle_block(demuxer, block, block_length,
2230 block_duration, block_bref, block_fref,
2232 free(block);
2233 if (res < 0)
2234 return 0;
2235 if (res)
2236 return 1;
2239 if (mkv_d->cluster_size > 0) {
2240 switch (ebml_read_id(s, &il)) {
2241 case MATROSKA_ID_TIMECODE:;
2242 uint64_t num = ebml_read_uint(s, &l);
2243 if (num == EBML_UINT_INVALID)
2244 return 0;
2245 mkv_d->cluster_tc = num * mkv_d->tc_scale;
2246 add_cluster_position(mkv_d, mkv_d->cluster_start,
2247 mkv_d->cluster_tc);
2248 break;
2250 case MATROSKA_ID_BLOCKGROUP:
2251 mkv_d->blockgroup_size = ebml_read_length(s, &tmp);
2252 l = tmp;
2253 break;
2255 case MATROSKA_ID_SIMPLEBLOCK:;
2256 int res;
2257 block_length = ebml_read_length(s, &tmp);
2258 if (block_length > 500000000)
2259 return 0;
2260 block = malloc(block_length);
2261 demuxer->filepos = stream_tell(s);
2262 if (stream_read(s, block, block_length) !=
2263 (int) block_length) {
2264 free(block);
2265 return 0;
2267 l = tmp + block_length;
2268 res = handle_block(demuxer, block, block_length,
2269 block_duration, block_bref,
2270 block_fref, 1);
2271 free(block);
2272 mkv_d->cluster_size -= l + il;
2273 if (res < 0)
2274 return 0;
2275 else if (res)
2276 return 1;
2277 else
2278 mkv_d->cluster_size += l + il;
2279 break;
2281 case EBML_ID_INVALID:
2282 return 0;
2284 default:
2285 ebml_read_skip(s, &l);
2286 break;
2288 mkv_d->cluster_size -= l + il;
2292 while (ebml_read_id(s, &il) != MATROSKA_ID_CLUSTER) {
2293 ebml_read_skip(s, NULL);
2294 if (s->eof)
2295 return 0;
2297 mkv_d->cluster_start = stream_tell(s) - il;
2298 mkv_d->cluster_size = ebml_read_length(s, NULL);
2301 return 0;
2304 static int seek_creating_index(struct demuxer *demuxer, float rel_seek_secs,
2305 int flags)
2307 struct mkv_demuxer *mkv_d = demuxer->priv;
2308 struct stream *s = demuxer->stream;
2309 int64_t target_tc_ns = (int64_t) (rel_seek_secs * 1e9);
2310 if (target_tc_ns < 0)
2311 target_tc_ns = 0;
2312 uint64_t max_filepos = 0;
2313 int64_t max_tc = -1;
2314 int n = mkv_d->num_cluster_pos;
2315 if (n > 0) {
2316 max_filepos = mkv_d->cluster_positions[n - 1].filepos;
2317 max_tc = mkv_d->cluster_positions[n - 1].timecode;
2320 if (target_tc_ns > max_tc) {
2321 if ((off_t) max_filepos > stream_tell(s))
2322 stream_seek(s, max_filepos);
2323 else
2324 stream_seek(s, stream_tell(s) + mkv_d->cluster_size);
2325 /* parse all the clusters upto target_filepos */
2326 while (!s->eof) {
2327 uint64_t start = stream_tell(s);
2328 uint32_t type = ebml_read_id(s, NULL);
2329 uint64_t len = ebml_read_length(s, NULL);
2330 uint64_t end = stream_tell(s) + len;
2331 if (type == MATROSKA_ID_CLUSTER) {
2332 while (!s->eof && stream_tell(s) < end) {
2333 if (ebml_read_id(s, NULL) == MATROSKA_ID_TIMECODE) {
2334 uint64_t tc = ebml_read_uint(s, NULL);
2335 tc *= mkv_d->tc_scale;
2336 add_cluster_position(mkv_d, start, tc);
2337 if (tc >= target_tc_ns)
2338 goto enough_index;
2339 break;
2343 if (s->eof)
2344 break;
2345 stream_seek(s, end);
2347 enough_index:
2348 if (s->eof)
2349 stream_reset(s);
2351 if (!mkv_d->num_cluster_pos) {
2352 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] no target for seek found\n");
2353 return -1;
2355 uint64_t cluster_pos = mkv_d->cluster_positions[0].filepos;
2356 /* Let's find the nearest cluster */
2357 int64_t min_diff = 0xFFFFFFFFFFFFFFF;
2358 for (int i = 0; i < mkv_d->num_cluster_pos; i++) {
2359 int64_t diff = mkv_d->cluster_positions[i].timecode - target_tc_ns;
2360 if (flags & SEEK_BACKWARD && diff < 0 && -diff < min_diff) {
2361 cluster_pos = mkv_d->cluster_positions[i].filepos;
2362 min_diff = -diff;
2363 } else if (flags & SEEK_FORWARD
2364 && (diff < 0 ? -1 * diff : diff) < min_diff) {
2365 cluster_pos = mkv_d->cluster_positions[i].filepos;
2366 min_diff = diff < 0 ? -1 * diff : diff;
2369 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
2370 stream_seek(s, cluster_pos);
2371 return 0;
2374 static struct mkv_index *seek_with_cues(struct demuxer *demuxer, int seek_id,
2375 int64_t target_timecode, int flags)
2377 struct mkv_demuxer *mkv_d = demuxer->priv;
2378 struct mkv_index *index = NULL;
2380 /* Find the entry in the index closest to the target timecode in the
2381 * give direction. If there are no such entries - we're trying to seek
2382 * backward from a target time before the first entry or forward from a
2383 * target time after the last entry - then still seek to the first/last
2384 * entry if that's further in the direction wanted than mkv_d->last_pts.
2386 int64_t min_diff = target_timecode - (int64_t)(mkv_d->last_pts * 1e9 + 0.5);
2387 if (flags & SEEK_BACKWARD)
2388 min_diff = -min_diff;
2389 min_diff = FFMAX(min_diff, 1);
2390 for (int i = 0; i < mkv_d->num_indexes; i++)
2391 if (seek_id < 0 || mkv_d->indexes[i].tnum == seek_id) {
2392 int64_t diff =
2393 target_timecode -
2394 (int64_t) (mkv_d->indexes[i].timecode * mkv_d->tc_scale);
2395 if (flags & SEEK_BACKWARD)
2396 diff = -diff;
2397 if (diff <= 0) {
2398 if (min_diff <= 0 && diff <= min_diff)
2399 continue;
2400 } else if (diff >= min_diff)
2401 continue;
2402 min_diff = diff;
2403 index = mkv_d->indexes + i;
2406 if (index) { /* We've found an entry. */
2407 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
2408 stream_seek(demuxer->stream, index->filepos);
2410 return index;
2413 static void demux_mkv_seek(demuxer_t *demuxer, float rel_seek_secs,
2414 float audio_delay, int flags)
2416 mkv_demuxer_t *mkv_d = demuxer->priv;
2417 uint64_t v_tnum = -1;
2418 if (demuxer->video->id >= 0)
2419 v_tnum = find_track_by_num(mkv_d, demuxer->video->id,
2420 MATROSKA_TRACK_VIDEO)->tnum;
2421 uint64_t a_tnum = -1;
2422 if (demuxer->audio->id >= 0)
2423 a_tnum = find_track_by_num(mkv_d, demuxer->audio->id,
2424 MATROSKA_TRACK_AUDIO)->tnum;
2425 if (!(flags & (SEEK_BACKWARD | SEEK_FORWARD))) {
2426 if (flags & SEEK_ABSOLUTE || rel_seek_secs < 0)
2427 flags |= SEEK_BACKWARD;
2428 else
2429 flags |= SEEK_FORWARD;
2431 // Adjust the target a little bit to catch cases where the target position
2432 // specifies a keyframe with high, but not perfect, precision.
2433 rel_seek_secs += flags & SEEK_FORWARD ? -0.005 : 0.005;
2435 if (!(flags & SEEK_FACTOR)) { /* time in secs */
2436 mkv_index_t *index = NULL;
2438 if (!(flags & SEEK_ABSOLUTE)) /* relative seek */
2439 rel_seek_secs += mkv_d->last_pts;
2440 rel_seek_secs = FFMAX(rel_seek_secs, 0);
2441 int64_t target_timecode = rel_seek_secs * 1e9 + 0.5;
2443 if (mkv_d->indexes == NULL) { /* no index was found */
2444 if (seek_creating_index(demuxer, rel_seek_secs, flags) < 0)
2445 return;
2446 } else {
2447 int seek_id = (demuxer->video->id < 0) ?
2448 a_tnum : v_tnum;
2449 index = seek_with_cues(demuxer, seek_id, target_timecode, flags);
2450 if (!index)
2451 index = seek_with_cues(demuxer, -1, target_timecode, flags);
2454 if (demuxer->video->id >= 0)
2455 mkv_d->v_skip_to_keyframe = 1;
2456 if (flags & SEEK_FORWARD)
2457 mkv_d->skip_to_timecode = target_timecode;
2458 else
2459 mkv_d->skip_to_timecode = index ? index->timecode * mkv_d->tc_scale
2460 : 0;
2461 mkv_d->a_skip_to_keyframe = 1;
2463 demux_mkv_fill_buffer(demuxer, NULL);
2464 } else if ((demuxer->movi_end <= 0) || !(flags & SEEK_ABSOLUTE))
2465 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
2466 else {
2467 stream_t *s = demuxer->stream;
2468 uint64_t target_filepos;
2469 mkv_index_t *index = NULL;
2470 int i;
2472 if (mkv_d->indexes == NULL) { /* not implemented without index */
2473 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
2474 return;
2477 target_filepos = (uint64_t) (demuxer->movi_end * rel_seek_secs);
2478 for (i = 0; i < mkv_d->num_indexes; i++)
2479 if (mkv_d->indexes[i].tnum == v_tnum)
2480 if ((index == NULL)
2481 || ((mkv_d->indexes[i].filepos >= target_filepos)
2482 && ((index->filepos < target_filepos)
2483 || (mkv_d->indexes[i].filepos < index->filepos))))
2484 index = &mkv_d->indexes[i];
2486 if (!index)
2487 return;
2489 mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
2490 stream_seek(s, index->filepos);
2492 if (demuxer->video->id >= 0)
2493 mkv_d->v_skip_to_keyframe = 1;
2494 mkv_d->skip_to_timecode = index->timecode * mkv_d->tc_scale;
2495 mkv_d->a_skip_to_keyframe = 1;
2497 demux_mkv_fill_buffer(demuxer, NULL);
2501 static int demux_mkv_control(demuxer_t *demuxer, int cmd, void *arg)
2503 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
2505 switch (cmd) {
2506 case DEMUXER_CTRL_CORRECT_PTS:
2507 return DEMUXER_CTRL_OK;
2508 case DEMUXER_CTRL_GET_TIME_LENGTH:
2509 if (mkv_d->duration == 0)
2510 return DEMUXER_CTRL_DONTKNOW;
2512 *((double *) arg) = (double) mkv_d->duration;
2513 return DEMUXER_CTRL_OK;
2515 case DEMUXER_CTRL_GET_PERCENT_POS:
2516 if (mkv_d->duration == 0) {
2517 return DEMUXER_CTRL_DONTKNOW;
2520 *((int *) arg) = (int) (100 * mkv_d->last_pts / mkv_d->duration);
2521 return DEMUXER_CTRL_OK;
2523 case DEMUXER_CTRL_SWITCH_AUDIO:;
2524 int new_aid = *(int *) arg;
2525 int current_aid = demuxer->audio->id;
2526 if (current_aid < 0)
2527 current_aid = -1;
2528 if (new_aid == -1) // cycle to next
2529 new_aid = (current_aid + 2) % (mkv_d->num_audio_tracks + 1) - 1;
2530 if (new_aid < 0 || new_aid >= mkv_d->num_audio_tracks)
2531 new_aid = -2;
2532 *(int *) arg = new_aid;
2533 if (current_aid != new_aid)
2534 ds_free_packs(demuxer->audio);
2535 demuxer->audio->id = new_aid;
2536 return DEMUXER_CTRL_OK;
2538 default:
2539 return DEMUXER_CTRL_NOTIMPL;
2543 const demuxer_desc_t demuxer_desc_matroska = {
2544 "Matroska demuxer",
2545 "mkv",
2546 "Matroska",
2547 "Aurelien Jacobs",
2549 DEMUXER_TYPE_MATROSKA,
2550 1, // safe autodetect
2551 demux_mkv_open,
2552 demux_mkv_fill_buffer,
2553 NULL,
2554 demux_close_mkv,
2555 demux_mkv_seek,
2556 demux_mkv_control