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.
35 #include "stream/stream.h"
40 #include "demux_real.h"
46 #include "libavutil/common.h"
48 #ifdef CONFIG_QTX_CODECS
49 #include "loader/qtx/qtxsdk/components.h"
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},
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
;
85 uint8_t *comp_settings
;
86 int comp_settings_len
;
87 } mkv_content_encoding_t
;
89 typedef struct mkv_track
{
92 int id
; // -aid / -sid / -vid option value
100 uint32_t v_width
, v_height
, v_dwidth
, v_dheight
;
103 uint32_t a_formattag
;
104 uint32_t a_channels
, a_bps
;
108 double default_duration
;
112 unsigned char *private_data
;
113 unsigned int private_size
;
115 /* stuff for realmedia */
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 */
134 double qt_last_a_pts
;
138 /* generic content encoding support */
139 mkv_content_encoding_t
*encodings
;
142 /* For VobSubs and SSA/ASS */
146 typedef struct mkv_index
{
148 uint64_t timecode
, filepos
;
151 typedef struct mkv_demuxer
{
154 double duration
, last_pts
;
155 uint64_t last_filepos
;
157 mkv_track_t
**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
;
174 bool parsed_chapters
;
175 bool parsed_attachments
;
180 } *cluster_positions
;
183 uint64_t skip_to_timecode
;
184 int v_skip_to_keyframe
, a_skip_to_keyframe
;
186 int num_audio_tracks
;
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
)
203 array
= realloc(array
, (nelem
+ 32) * elsize
);
207 static bool is_parsed_header(struct mkv_demuxer
*mkv_d
, off_t pos
)
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
)
218 if (mkv_d
->num_parsed_pos
&& mkv_d
->parsed_pos
[low
] == pos
)
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
;
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
)
240 static void add_cluster_position(mkv_demuxer_t
*mkv_d
, uint64_t filepos
,
246 int n
= mkv_d
->num_cluster_pos
;
247 if (n
> 0 && mkv_d
->cluster_positions
[n
-1].filepos
>= filepos
)
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
){
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
268 while (sample_rate
< srates
[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
;
280 for (int i
= 0; i
< track
->num_encodings
; i
++) {
281 struct mkv_content_encoding
*enc
= track
->encodings
+ i
;
282 if (!(enc
->scope
& type
))
285 if (src
!= *dest
&& src
!= orig_src
)
287 src
= *dest
; // output from last iteration is new source
289 if (enc
->comp_algo
== 0) {
291 /* zlib encoded track */
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");
306 zstream
.next_in
= (Bytef
*) src
;
307 zstream
.avail_in
= *size
;
310 zstream
.avail_out
= *size
;
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");
322 inflateEnd(&zstream
);
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
);
332 } else if (enc
->comp_algo
== 2) {
333 /* lzo encoded track */
335 int dstlen
= *size
* 3;
340 *dest
= talloc_realloc_size(NULL
, *dest
,
341 dstlen
+ AV_LZO_OUTPUT_PADDING
);
343 int result
= av_lzo1x_decode(*dest
, &out_avail
, src
, &srclen
);
346 if (!(result
& AV_LZO_OUTPUT_FULL
)) {
347 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
348 "[mkv] lzo decompression failed.\n");
353 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,
354 "[mkv] lzo decompression buffer too small.\n");
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
;
367 if (src
!= *dest
&& src
!= orig_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;
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)
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",
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
);
400 memcpy(demuxer
->matroska_data
.segment_uid
, info
.segment_uid
.start
,
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
);
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
;
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
;
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",
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",
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
);
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. "
470 for (i
= 0; i
< n_enc
; i
++)
471 if (e
.order
>= ce
[i
].order
)
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
);
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
);
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",
502 if (audio
->n_channels
) {
503 track
->a_channels
= audio
->channels
;
504 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Channels: %u\n",
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",
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",
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",
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",
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",
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
);
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
;
561 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Track number: %u\n",
564 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Missing track number!\n");
567 track
->name
= talloc_strndup(track
, entry
->name
.start
,
569 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Name: %s\n",
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");
579 case MATROSKA_TRACK_VIDEO
:
580 mp_msg(MSGT_DEMUX
, MSGL_V
, "Video\n");
582 case MATROSKA_TRACK_SUBTITLE
:
583 mp_msg(MSGT_DEMUX
, MSGL_V
, "Subtitle\n");
586 mp_msg(MSGT_DEMUX
, MSGL_V
, "unknown\n");
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",
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",
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
);
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");
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)
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
);
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
);
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)
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");
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
;
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
++;
729 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] \\---- [ parsing cues ] -----------\n");
730 talloc_free(parse_ctx
.talloc_ctx
);
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)
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",
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
);
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 "
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");
810 memcpy(chapter
.segment_uid
, ca
->chapter_segment_uid
.start
,
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),
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
,
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
);
849 talloc_free(parse_ctx
.talloc_ctx
);
850 mp_msg(MSGT_DEMUX
, MSGL_V
,
851 "[mkv] \\---- [ parsing chapters ] ---------\n");
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)
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
)
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
);
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)
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");
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
);
905 talloc_free(parse_ctx
.talloc_ctx
);
906 mp_msg(MSGT_DEMUX
, MSGL_V
,
907 "[mkv] \\---- [ parsing attachments ] ---------\n");
911 static int read_header_element(struct demuxer
*demuxer
, uint32_t id
,
914 static int demux_mkv_read_seekhead(demuxer_t
*demuxer
)
916 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
917 struct stream
*s
= demuxer
->stream
;
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) {
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");
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");
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 "
950 mp_msg(MSGT_DEMUX
, MSGL_V
,
951 "[mkv] \\---- [ parsing seek head ] ---------\n");
952 talloc_free(parse_ctx
.talloc_ctx
);
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");
962 if (ebml_read_id(s
, NULL
) != id
) {
963 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Expected element not found\n");
969 static int read_header_element(struct demuxer
*demuxer
, uint32_t id
,
972 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
973 stream_t
*s
= demuxer
->stream
;
974 off_t pos
= stream_tell(s
) - 4;
977 case MATROSKA_ID_INFO
:
978 if (mkv_d
->parsed_info
)
980 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
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
)
989 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
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
))
998 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1000 return demux_mkv_read_cues(demuxer
) ? -1 : 1;
1002 case MATROSKA_ID_TAGS
:
1003 if (mkv_d
->parsed_tags
)
1005 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
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
))
1013 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1015 return demux_mkv_read_seekhead(demuxer
) ? -1 : 1;
1017 case MATROSKA_ID_CHAPTERS
:
1018 if (mkv_d
->parsed_chapters
)
1020 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
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
)
1028 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1030 mkv_d
->parsed_attachments
= true;
1031 return demux_mkv_read_attachments(demuxer
) ? -1 : 1;
1035 ebml_read_skip(s
, NULL
);
1039 ebml_read_skip(s
, NULL
);
1045 static int demux_mkv_open_video(demuxer_t
*demuxer
, mkv_track_t
*track
,
1047 static int demux_mkv_open_audio(demuxer_t
*demuxer
, mkv_track_t
*track
,
1049 static int demux_mkv_open_sub(demuxer_t
*demuxer
, mkv_track_t
*track
,
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];
1060 switch (mkv_d
->tracks
[i
]->type
) {
1061 case MATROSKA_TRACK_VIDEO
:
1063 mkv_d
->tracks
[i
]->id
= -1;
1064 if (vid
== MAX_V_STREAMS
)
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
++);
1073 case MATROSKA_TRACK_AUDIO
:
1075 mkv_d
->tracks
[i
]->id
= -1;
1076 if (aid
== MAX_A_STREAMS
)
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
);
1088 case MATROSKA_TRACK_SUBTITLE
:
1090 mkv_d
->tracks
[i
]->id
= -1;
1091 if (sid
== MAX_S_STREAMS
)
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
);
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
);
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
,
1114 mkv_d
->num_audio_tracks
= aid
;
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},
1136 static int demux_mkv_open_video(demuxer_t
*demuxer
, mkv_track_t
*track
,
1139 BITMAPINFOHEADER
*bih
;
1140 void *ImageDesc
= NULL
;
1143 if (track
->ms_compat
) { /* MS compatibility mode */
1144 BITMAPINFOHEADER
*src
;
1146 if (track
->private_data
== NULL
1147 || track
->private_size
< sizeof(*bih
))
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
);
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
;
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
;
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
;
1194 type2
= AV_RB32(src
- 4);
1195 if (type2
== 0x10003000 || type2
== 0x10003001)
1196 bih
->biCompression
= mmioFOURCC('R', 'V', '1', '3');
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
);
1227 bih
->biCompression
= idesc
->cType
;
1229 #endif /* CONFIG_QTX_CODECS */
1232 const videocodec_info_t
*vi
= vinfo
;
1233 while (vi
->id
&& strcmp(vi
->id
, track
->codec_id
))
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
);
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
);
1253 sh_v
= new_sh_video(demuxer
, vid
);
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
;
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
;
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
;
1282 static int demux_mkv_open_audio(demuxer_t
*demuxer
, mkv_track_t
*track
,
1285 sh_audio_t
*sh_a
= new_sh_audio(demuxer
, aid
);
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
;
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
)
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");
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');
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
);
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;
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 */
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
;
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))
1434 else if (!strncmp(&track
->codec_id
[12], "LC", 2))
1436 else if (!strncmp(&track
->codec_id
[12], "SSR", 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);
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
,
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
;
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);
1481 src
+= RAPROPERTIES4_SIZE
;
1485 src
+= RAPROPERTIES5_SIZE
;
1490 codecdata_length
= AV_RB32(src
);
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
;
1501 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1502 track
->audio_timestamp
=
1503 malloc(track
->sub_packet_h
* sizeof(double));
1505 case mmioFOURCC('c', 'o', 'o', 'k'):
1506 sh_a
->wf
->nAvgBytesPerSec
= cook_fl2bps
[flavor
];
1507 sh_a
->wf
->nBlockAlign
= track
->sub_packet_size
;
1509 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1510 track
->audio_timestamp
=
1511 malloc(track
->sub_packet_h
* sizeof(double));
1513 case mmioFOURCC('s', 'i', 'p', 'r'):
1514 sh_a
->wf
->nAvgBytesPerSec
= sipr_fl2bps
[flavor
];
1515 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
1517 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1518 track
->audio_timestamp
=
1519 malloc(track
->sub_packet_h
* sizeof(double));
1521 case mmioFOURCC('2', '8', '_', '8'):
1522 sh_a
->wf
->nAvgBytesPerSec
= 3600;
1523 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
1525 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1526 track
->audio_timestamp
=
1527 malloc(track
->sub_packet_h
* sizeof(double));
1531 track
->realmedia
= 1;
1532 } else if (!strcmp(track
->codec_id
, MKV_A_FLAC
)
1533 || (track
->a_formattag
== 0xf1ac)) {
1539 if (track
->a_formattag
== mmioFOURCC('f', 'L', 'a', 'C')) {
1540 ptr
= track
->private_data
;
1541 size
= track
->private_size
;
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'
1549 sh_a
->codecdata
= malloc(4);
1550 sh_a
->codecdata_len
= 4;
1551 memcpy(sh_a
->codecdata
, "fLaC", 4);
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
);
1567 static int demux_mkv_open_sub(demuxer_t
*demuxer
, mkv_track_t
*track
,
1570 if (track
->subtitle_type
) {
1573 sh_sub_t
*sh
= new_sh_sub(demuxer
, sid
);
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
;
1591 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
1592 "[mkv] Subtitle type '%s' is not supported.\n",
1600 static int demux_mkv_open(demuxer_t
*demuxer
)
1602 stream_t
*s
= demuxer
->stream
;
1603 mkv_demuxer_t
*mkv_d
;
1607 stream_seek(s
, s
->start_pos
);
1608 if (ebml_read_id(s
, NULL
) != EBML_ID_EBML
)
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)
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
);
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
);
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
);
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");
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
);
1659 uint32_t id
= ebml_read_id(s
, NULL
);
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);
1670 cont
= read_header_element(demuxer
, id
, 0) < 1;
1673 ebml_read_skip(s
, NULL
);
1678 display_create_tracks(demuxer
);
1680 /* select video track */
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
];
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
];
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",
1707 demuxer
->video
->id
= track
->id
;
1708 demuxer
->video
->sh
= demuxer
->v_streams
[track
->id
];
1710 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] No video track found/wanted.\n");
1711 demuxer
->video
->id
= -2;
1714 /* select audio track */
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
];
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
];
1735 if (track
&& demuxer
->a_streams
[track
->id
]) {
1736 demuxer
->audio
->id
= track
->id
;
1737 demuxer
->audio
->sh
= demuxer
->a_streams
[track
->id
];
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;
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
;
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
,
1773 uint32_t **all_lace_sizes
)
1776 uint32_t *lace_size
= NULL
;
1780 *all_lace_sizes
= NULL
;
1787 switch ((flags
& 0x06) >> 1) {
1788 case 0: /* no lacing */
1790 lace_size
= calloc(*laces
, sizeof(uint32_t));
1791 lace_size
[0] = *size
;
1794 case 1: /* xiph lacing */
1795 case 2: /* fixed-size lacing */
1796 case 3: /* EBML lacing */
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
++) {
1811 lace_size
[i
] += *buffer
;
1813 } while (*buffer
++ == 0xFF);
1814 if (lace_size
[i
] > *size
- total
|| total
> *size
)
1816 total
+= lace_size
[i
];
1818 lace_size
[i
] = *size
- total
;
1821 case 2: /* fixed-size lacing */
1822 for (i
= 0; i
< *laces
; i
++)
1823 lace_size
[i
] = *size
/ *laces
;
1826 case 3:; /* EBML lacing */
1828 uint64_t num
= ebml_read_vlen_uint(buffer
, &l
);
1829 if (num
== EBML_UINT_INVALID
)
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
)
1847 lace_size
[i
] = lace_size
[i
- 1] + snum
;
1848 if (lace_size
[i
] > *size
- total
|| total
> *size
)
1850 total
+= lace_size
[i
];
1852 lace_size
[i
] = *size
- total
;
1857 *all_lace_sizes
= lace_size
;
1862 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Bad input [lacing]\n");
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
;
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
;
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
;
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'))) {
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
);
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
);
1923 case mmioFOURCC('s', 'i', 'p', 'r'):
1924 memcpy(track
->audio_buf
+ spc
* w
, buffer
, w
);
1925 if (spc
== sph
- 1) {
1927 int bs
= sph
* w
* 2 / 96; // nibbles per subpacket
1928 // Perform reordering
1929 for (n
= 0; n
< 38; n
++) {
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
++) {
1936 (track
->audio_buf
[i
>> 1] >> 4) :
1937 (track
->audio_buf
[i
>> 1] & 0x0F);
1939 (track
->audio_buf
[o
>> 1] >> 4) :
1940 (track
->audio_buf
[o
>> 1] & 0x0F);
1942 track
->audio_buf
[o
>> 1] =
1943 (track
->audio_buf
[o
>> 1] & 0x0F) | (x
<< 4);
1945 track
->audio_buf
[o
>> 1] =
1946 (track
->audio_buf
[o
>> 1] & 0xF0) | x
;
1948 track
->audio_buf
[i
>> 1] =
1949 (track
->audio_buf
[i
>> 1] & 0x0F) | (y
<< 4);
1951 track
->audio_buf
[i
>> 1] =
1952 (track
->audio_buf
[i
>> 1] & 0xF0) | y
;
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
) {
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
,
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
)
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
;
2007 uint32_t *lace_size
;
2008 uint8_t laces
, flags
;
2009 int i
, num
, tmp
, use_this_block
= 1;
2013 /* first byte(s): track num */
2014 num
= ebml_read_vlen_uint(block
, &tmp
);
2016 /* time (relative to cluster time) */
2017 time
= block
[0] << 8 | block
[1];
2020 old_length
= length
;
2022 if (demux_mkv_read_block_lacing(block
, &length
, &laces
, &lace_size
))
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
];
2034 if (track
== NULL
) {
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
) {
2044 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2046 } else if (block_bref
!= 0)
2049 if (mkv_d
->v_skip_to_keyframe
)
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
)
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
) {
2072 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2074 } else if (block_bref
!= 0 || block_fref
!= 0)
2077 } else if (track
->type
== MATROSKA_TRACK_SUBTITLE
2078 && track
->id
== demuxer
->sub
->id
) {
2081 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Subtitles use Matroska "
2082 "lacing. This is abnormal and not supported.\n");
2085 sub_utf8
= 1; // XXX this variable should be eventually removed
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
],
2097 else if (ds
== demuxer
->audio
&& track
->realmedia
)
2098 handle_realaudio(demuxer
, track
, block
, lace_size
[i
],
2101 int size
= lace_size
[i
];
2104 demux_mkv_decode(track
, block
, &buffer
, &size
, 1);
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
)
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;
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
;
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
) {
2160 block_duration
*= mkv_d
->tc_scale
;
2163 case MATROSKA_ID_BLOCK
:
2164 block_length
= ebml_read_length(s
, &tmp
);
2166 if (block_length
> 500000000)
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
) {
2175 l
= tmp
+ block_length
;
2178 case MATROSKA_ID_REFERENCEBLOCK
:;
2179 int64_t num
= ebml_read_int(s
, &l
);
2180 if (num
== EBML_INT_INVALID
) {
2190 case EBML_ID_INVALID
:
2195 ebml_read_skip(s
, &l
);
2198 mkv_d
->blockgroup_size
-= l
+ il
;
2199 mkv_d
->cluster_size
-= l
+ il
;
2203 int res
= handle_block(demuxer
, block
, block_length
,
2204 block_duration
, block_bref
, block_fref
,
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
)
2219 mkv_d
->cluster_tc
= num
* mkv_d
->tc_scale
;
2220 add_cluster_position(mkv_d
, mkv_d
->cluster_start
,
2224 case MATROSKA_ID_BLOCKGROUP
:
2225 mkv_d
->blockgroup_size
= ebml_read_length(s
, &tmp
);
2229 case MATROSKA_ID_SIMPLEBLOCK
:;
2231 block_length
= ebml_read_length(s
, &tmp
);
2232 if (block_length
> 500000000)
2234 block
= malloc(block_length
);
2235 demuxer
->filepos
= stream_tell(s
);
2236 if (stream_read(s
, block
, block_length
) !=
2237 (int) block_length
) {
2241 l
= tmp
+ block_length
;
2242 res
= handle_block(demuxer
, block
, block_length
,
2243 block_duration
, block_bref
,
2246 mkv_d
->cluster_size
-= l
+ il
;
2252 mkv_d
->cluster_size
+= l
+ il
;
2255 case EBML_ID_INVALID
:
2259 ebml_read_skip(s
, &l
);
2262 mkv_d
->cluster_size
-= l
+ il
;
2266 while (ebml_read_id(s
, &il
) != MATROSKA_ID_CLUSTER
) {
2267 ebml_read_skip(s
, NULL
);
2271 mkv_d
->cluster_start
= stream_tell(s
) - il
;
2272 mkv_d
->cluster_size
= ebml_read_length(s
, NULL
);
2278 static int seek_creating_index(struct demuxer
*demuxer
, float rel_seek_secs
,
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)
2286 uint64_t max_filepos
= 0;
2287 int64_t max_tc
= -1;
2288 int n
= mkv_d
->num_cluster_pos
;
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
);
2298 stream_seek(s
, stream_tell(s
) + mkv_d
->cluster_size
);
2299 /* parse all the clusters upto target_filepos */
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
)
2319 stream_seek(s
, end
);
2325 if (!mkv_d
->num_cluster_pos
) {
2326 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] no target for seek found\n");
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
;
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
);
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
) {
2368 (int64_t) (mkv_d
->indexes
[i
].timecode
* mkv_d
->tc_scale
);
2369 if (flags
& SEEK_BACKWARD
)
2372 if (min_diff
<= 0 && diff
<= min_diff
)
2374 } else if (diff
>= min_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
);
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
;
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)
2421 int seek_id
= (demuxer
->video
->id
< 0) ?
2423 index
= seek_with_cues(demuxer
, seek_id
, target_timecode
, flags
);
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
;
2433 mkv_d
->skip_to_timecode
= index
? index
->timecode
* mkv_d
->tc_scale
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");
2441 stream_t
*s
= demuxer
->stream
;
2442 uint64_t target_filepos
;
2443 mkv_index_t
*index
= NULL
;
2446 if (mkv_d
->indexes
== NULL
) { /* not implemented without index */
2447 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
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
)
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
];
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
;
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)
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
)
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
;
2513 return DEMUXER_CTRL_NOTIMPL
;
2517 const demuxer_desc_t demuxer_desc_matroska
= {
2523 DEMUXER_TYPE_MATROSKA
,
2524 1, // safe autodetect
2526 demux_mkv_fill_buffer
,