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
= 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",
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
!= MATROSKA_SUBTYPE_UNKNOWN
) {
1573 sh_sub_t
*sh
= new_sh_sub(demuxer
, sid
);
1576 if (track
->subtitle_type
== MATROSKA_SUBTYPE_VOBSUB
)
1578 if (track
->subtitle_type
== MATROSKA_SUBTYPE_SSA
)
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
;
1595 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
1596 "[mkv] Subtitle type '%s' is not supported.\n",
1604 static int demux_mkv_open(demuxer_t
*demuxer
)
1606 stream_t
*s
= demuxer
->stream
;
1607 mkv_demuxer_t
*mkv_d
;
1611 stream_seek(s
, s
->start_pos
);
1612 if (ebml_read_id(s
, NULL
) != EBML_ID_EBML
)
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)
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
);
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
);
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
);
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");
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
);
1663 uint32_t id
= ebml_read_id(s
, NULL
);
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);
1674 cont
= read_header_element(demuxer
, id
, 0) < 1;
1677 ebml_read_skip(s
, NULL
);
1682 display_create_tracks(demuxer
);
1684 /* select video track */
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
];
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
];
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",
1711 demuxer
->video
->id
= track
->id
;
1712 demuxer
->video
->sh
= demuxer
->v_streams
[track
->id
];
1714 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] No video track found/wanted.\n");
1715 demuxer
->video
->id
= -2;
1718 /* select audio track */
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
];
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
];
1739 if (track
&& demuxer
->a_streams
[track
->id
]) {
1740 demuxer
->audio
->id
= track
->id
;
1741 demuxer
->audio
->sh
= demuxer
->a_streams
[track
->id
];
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;
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
;
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
,
1777 uint32_t **all_lace_sizes
)
1780 uint32_t *lace_size
= NULL
;
1784 *all_lace_sizes
= NULL
;
1791 switch ((flags
& 0x06) >> 1) {
1792 case 0: /* no lacing */
1794 lace_size
= calloc(*laces
, sizeof(uint32_t));
1795 lace_size
[0] = *size
;
1798 case 1: /* xiph lacing */
1799 case 2: /* fixed-size lacing */
1800 case 3: /* EBML lacing */
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
++) {
1815 lace_size
[i
] += *buffer
;
1817 } while (*buffer
++ == 0xFF);
1818 if (lace_size
[i
] > *size
- total
|| total
> *size
)
1820 total
+= lace_size
[i
];
1822 lace_size
[i
] = *size
- total
;
1825 case 2: /* fixed-size lacing */
1826 for (i
= 0; i
< *laces
; i
++)
1827 lace_size
[i
] = *size
/ *laces
;
1830 case 3:; /* EBML lacing */
1832 uint64_t num
= ebml_read_vlen_uint(buffer
, &l
);
1833 if (num
== EBML_UINT_INVALID
)
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
)
1851 lace_size
[i
] = lace_size
[i
- 1] + snum
;
1852 if (lace_size
[i
] > *size
- total
|| total
> *size
)
1854 total
+= lace_size
[i
];
1856 lace_size
[i
] = *size
- total
;
1861 *all_lace_sizes
= lace_size
;
1866 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Bad input [lacing]\n");
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
)
1876 if (block_duration
== 0) {
1877 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
1878 "[mkv] Warning: No BlockDuration for subtitle track found.\n");
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
;
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
;
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
;
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'))) {
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
);
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
);
1947 case mmioFOURCC('s', 'i', 'p', 'r'):
1948 memcpy(track
->audio_buf
+ spc
* w
, buffer
, w
);
1949 if (spc
== sph
- 1) {
1951 int bs
= sph
* w
* 2 / 96; // nibbles per subpacket
1952 // Perform reordering
1953 for (n
= 0; n
< 38; n
++) {
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
++) {
1960 (track
->audio_buf
[i
>> 1] >> 4) :
1961 (track
->audio_buf
[i
>> 1] & 0x0F);
1963 (track
->audio_buf
[o
>> 1] >> 4) :
1964 (track
->audio_buf
[o
>> 1] & 0x0F);
1966 track
->audio_buf
[o
>> 1] =
1967 (track
->audio_buf
[o
>> 1] & 0x0F) | (x
<< 4);
1969 track
->audio_buf
[o
>> 1] =
1970 (track
->audio_buf
[o
>> 1] & 0xF0) | x
;
1972 track
->audio_buf
[i
>> 1] =
1973 (track
->audio_buf
[i
>> 1] & 0x0F) | (y
<< 4);
1975 track
->audio_buf
[i
>> 1] =
1976 (track
->audio_buf
[i
>> 1] & 0xF0) | y
;
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
) {
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
,
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
)
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
;
2031 uint32_t *lace_size
;
2032 uint8_t laces
, flags
;
2033 int i
, num
, tmp
, use_this_block
= 1;
2037 /* first byte(s): track num */
2038 num
= ebml_read_vlen_uint(block
, &tmp
);
2040 /* time (relative to cluster time) */
2041 time
= block
[0] << 8 | block
[1];
2044 old_length
= length
;
2046 if (demux_mkv_read_block_lacing(block
, &length
, &laces
, &lace_size
))
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
];
2058 if (track
== NULL
) {
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
) {
2068 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2070 } else if (block_bref
!= 0)
2073 if (mkv_d
->v_skip_to_keyframe
)
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
)
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
) {
2096 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2098 } else if (block_bref
!= 0 || block_fref
!= 0)
2101 } else if (track
->type
== MATROSKA_TRACK_SUBTITLE
2102 && track
->id
== demuxer
->sub
->id
) {
2104 if (track
->subtitle_type
!= MATROSKA_SUBTYPE_VOBSUB
) {
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
);
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
],
2124 else if (ds
== demuxer
->audio
&& track
->realmedia
)
2125 handle_realaudio(demuxer
, track
, block
, lace_size
[i
],
2128 int size
= lace_size
[i
];
2131 demux_mkv_decode(track
, block
, &buffer
, &size
, 1);
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
)
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;
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
;
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
) {
2186 block_duration
*= mkv_d
->tc_scale
;
2189 case MATROSKA_ID_BLOCK
:
2190 block_length
= ebml_read_length(s
, &tmp
);
2192 if (block_length
> 500000000)
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
) {
2201 l
= tmp
+ block_length
;
2204 case MATROSKA_ID_REFERENCEBLOCK
:;
2205 int64_t num
= ebml_read_int(s
, &l
);
2206 if (num
== EBML_INT_INVALID
) {
2216 case EBML_ID_INVALID
:
2221 ebml_read_skip(s
, &l
);
2224 mkv_d
->blockgroup_size
-= l
+ il
;
2225 mkv_d
->cluster_size
-= l
+ il
;
2229 int res
= handle_block(demuxer
, block
, block_length
,
2230 block_duration
, block_bref
, block_fref
,
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
)
2245 mkv_d
->cluster_tc
= num
* mkv_d
->tc_scale
;
2246 add_cluster_position(mkv_d
, mkv_d
->cluster_start
,
2250 case MATROSKA_ID_BLOCKGROUP
:
2251 mkv_d
->blockgroup_size
= ebml_read_length(s
, &tmp
);
2255 case MATROSKA_ID_SIMPLEBLOCK
:;
2257 block_length
= ebml_read_length(s
, &tmp
);
2258 if (block_length
> 500000000)
2260 block
= malloc(block_length
);
2261 demuxer
->filepos
= stream_tell(s
);
2262 if (stream_read(s
, block
, block_length
) !=
2263 (int) block_length
) {
2267 l
= tmp
+ block_length
;
2268 res
= handle_block(demuxer
, block
, block_length
,
2269 block_duration
, block_bref
,
2272 mkv_d
->cluster_size
-= l
+ il
;
2278 mkv_d
->cluster_size
+= l
+ il
;
2281 case EBML_ID_INVALID
:
2285 ebml_read_skip(s
, &l
);
2288 mkv_d
->cluster_size
-= l
+ il
;
2292 while (ebml_read_id(s
, &il
) != MATROSKA_ID_CLUSTER
) {
2293 ebml_read_skip(s
, NULL
);
2297 mkv_d
->cluster_start
= stream_tell(s
) - il
;
2298 mkv_d
->cluster_size
= ebml_read_length(s
, NULL
);
2304 static int seek_creating_index(struct demuxer
*demuxer
, float rel_seek_secs
,
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)
2312 uint64_t max_filepos
= 0;
2313 int64_t max_tc
= -1;
2314 int n
= mkv_d
->num_cluster_pos
;
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
);
2324 stream_seek(s
, stream_tell(s
) + mkv_d
->cluster_size
);
2325 /* parse all the clusters upto target_filepos */
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
)
2345 stream_seek(s
, end
);
2351 if (!mkv_d
->num_cluster_pos
) {
2352 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] no target for seek found\n");
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
;
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
);
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
) {
2394 (int64_t) (mkv_d
->indexes
[i
].timecode
* mkv_d
->tc_scale
);
2395 if (flags
& SEEK_BACKWARD
)
2398 if (min_diff
<= 0 && diff
<= min_diff
)
2400 } else if (diff
>= min_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
);
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
;
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)
2447 int seek_id
= (demuxer
->video
->id
< 0) ?
2449 index
= seek_with_cues(demuxer
, seek_id
, target_timecode
, flags
);
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
;
2459 mkv_d
->skip_to_timecode
= index
? index
->timecode
* mkv_d
->tc_scale
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");
2467 stream_t
*s
= demuxer
->stream
;
2468 uint64_t target_filepos
;
2469 mkv_index_t
*index
= NULL
;
2472 if (mkv_d
->indexes
== NULL
) { /* not implemented without index */
2473 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
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
)
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
];
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
;
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)
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
)
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
;
2539 return DEMUXER_CTRL_NOTIMPL
;
2543 const demuxer_desc_t demuxer_desc_matroska
= {
2549 DEMUXER_TYPE_MATROSKA
,
2550 1, // safe autodetect
2552 demux_mkv_fill_buffer
,