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"
45 #include "subreader.h"
46 #include "libvo/sub.h"
50 #include "libavutil/common.h"
52 #ifdef CONFIG_QTX_CODECS
53 #include "loader/qtx/qtxsdk/components.h"
60 #include "libavutil/lzo.h"
61 #include "ffmpeg_files/intreadwrite.h"
62 #include "libavutil/avstring.h"
64 static const unsigned char sipr_swaps
[38][2] = {
65 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
66 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
67 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
68 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
72 // Map flavour to bytes per second
73 #define SIPR_FLAVORS 4
74 #define ATRC_FLAVORS 8
75 #define COOK_FLAVORS 34
76 static const int sipr_fl2bps
[SIPR_FLAVORS
] = { 813, 1062, 625, 2000 };
77 static const int atrc_fl2bps
[ATRC_FLAVORS
] = {
78 8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100 };
79 static const int cook_fl2bps
[COOK_FLAVORS
] = {
80 1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498,
81 4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584,
82 4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513,
83 12016, 16408, 22911, 33506
86 typedef struct mkv_content_encoding
{
87 uint64_t order
, type
, scope
;
89 uint8_t *comp_settings
;
90 int comp_settings_len
;
91 } mkv_content_encoding_t
;
93 typedef struct mkv_track
{
96 int id
; // -aid / -sid / -vid option value
104 uint32_t v_width
, v_height
, v_dwidth
, v_dheight
;
107 uint32_t a_formattag
;
108 uint32_t a_channels
, a_bps
;
111 double default_duration
;
115 unsigned char *private_data
;
116 unsigned int private_size
;
118 /* stuff for realmedia */
122 double rv_pts
; /* previous video timestamp */
123 double ra_pts
; /* previous audio timestamp */
125 /** realaudio descrambling */
126 int sub_packet_size
; ///< sub packet size, per stream
127 int sub_packet_h
; ///< number of coded frames per block
128 int coded_framesize
; ///< coded frame size, per stream
129 int audiopk_size
; ///< audio packet size
130 unsigned char *audio_buf
; ///< place to store reordered audio data
131 double *audio_timestamp
; ///< timestamp for each audio packet
132 int sub_packet_cnt
; ///< number of subpacket already received
133 int audio_filepos
; ///< file position of first audio packet in block
135 /* stuff for quicktime */
137 double qt_last_a_pts
;
141 /* The timecodes of video frames might have to be reordered if they're
142 in display order (the timecodes, not the frames themselves!). In this
143 case demux packets have to be cached with the help of these variables. */
144 int reorder_timecodes
;
145 demux_packet_t
**cached_dps
;
146 int num_cached_dps
, num_allocated_dps
;
149 /* generic content encoding support */
150 mkv_content_encoding_t
*encodings
;
153 /* For VobSubs and SSA/ASS */
157 typedef struct mkv_index
{
159 uint64_t timecode
, filepos
;
162 typedef struct mkv_demuxer
{
165 double duration
, last_pts
;
166 uint64_t last_filepos
;
168 mkv_track_t
**tracks
;
171 uint64_t tc_scale
, cluster_tc
;
173 uint64_t cluster_start
;
174 uint64_t cluster_size
;
175 uint64_t blockgroup_size
;
177 mkv_index_t
*indexes
;
185 bool parsed_chapters
;
186 bool parsed_attachments
;
191 } *cluster_positions
;
194 int64_t skip_to_timecode
;
195 int v_skip_to_keyframe
, a_skip_to_keyframe
;
197 int num_audio_tracks
;
200 #define REALHEADER_SIZE 16
201 #define RVPROPERTIES_SIZE 34
202 #define RAPROPERTIES4_SIZE 56
203 #define RAPROPERTIES5_SIZE 70
206 * \brief ensures there is space for at least one additional element
207 * \param array array to grow
208 * \param nelem current number of elements in array
209 * \param elsize size of one array element
211 static void *grow_array(void *array
, int nelem
, size_t elsize
)
214 array
= realloc(array
, (nelem
+ 32) * elsize
);
218 static bool is_parsed_header(struct mkv_demuxer
*mkv_d
, off_t pos
)
221 int high
= mkv_d
->num_parsed_pos
;
222 while (high
> low
+ 1) {
223 int mid
= high
+ low
>> 1;
224 if (mkv_d
->parsed_pos
[mid
] > pos
)
229 if (mkv_d
->num_parsed_pos
&& mkv_d
->parsed_pos
[low
] == pos
)
231 if (!(mkv_d
->num_parsed_pos
& 31))
232 mkv_d
->parsed_pos
= talloc_realloc(mkv_d
, mkv_d
->parsed_pos
, off_t
,
233 mkv_d
->num_parsed_pos
+ 32);
234 mkv_d
->num_parsed_pos
++;
235 for (int i
= mkv_d
->num_parsed_pos
- 1; i
> low
; i
--)
236 mkv_d
->parsed_pos
[i
] = mkv_d
->parsed_pos
[i
- 1];
237 mkv_d
->parsed_pos
[low
] = pos
;
241 static mkv_track_t
*find_track_by_num(struct mkv_demuxer
*d
, int n
, int type
)
243 for (int i
= 0; i
< d
->num_tracks
; i
++)
244 if (d
->tracks
[i
] != NULL
&& d
->tracks
[i
]->type
== type
)
245 if (d
->tracks
[i
]->id
== n
)
251 static void add_cluster_position(mkv_demuxer_t
*mkv_d
, uint64_t filepos
,
257 int n
= mkv_d
->num_cluster_pos
;
258 if (n
> 0 && mkv_d
->cluster_positions
[n
-1].filepos
>= filepos
)
261 mkv_d
->cluster_positions
=
262 grow_array(mkv_d
->cluster_positions
, mkv_d
->num_cluster_pos
,
263 sizeof(*mkv_d
->cluster_positions
));
264 mkv_d
->cluster_positions
[mkv_d
->num_cluster_pos
++] = (struct cluster_pos
){
266 .timecode
= timecode
,
271 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
272 static int aac_get_sample_rate_index(uint32_t sample_rate
)
274 static const int srates
[] = {
275 92017, 75132, 55426, 46009, 37566, 27713,
276 23004, 18783, 13856, 11502, 9391, 0
279 while (sample_rate
< srates
[i
])
284 /** \brief Free cached demux packets
286 * Reordering the timecodes requires caching of demux packets. This function
287 * frees all these cached packets and the memory for the cached pointers
290 * \param demuxer The demuxer for which the cache is to be freed.
292 static void free_cached_dps(demuxer_t
*demuxer
)
294 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
298 for (k
= 0; k
< mkv_d
->num_tracks
; k
++) {
299 track
= mkv_d
->tracks
[k
];
300 for (i
= 0; i
< track
->num_cached_dps
; i
++)
301 free_demux_packet(track
->cached_dps
[i
]);
302 free(track
->cached_dps
);
303 track
->cached_dps
= NULL
;
304 track
->num_cached_dps
= 0;
305 track
->num_allocated_dps
= 0;
310 static void demux_mkv_decode(mkv_track_t
*track
, uint8_t *src
,
311 uint8_t **dest
, uint32_t *size
, uint32_t type
)
313 uint8_t *orig_src
= src
;
317 for (int i
= 0; i
< track
->num_encodings
; i
++) {
318 struct mkv_content_encoding
*enc
= track
->encodings
+ i
;
319 if (!(enc
->scope
& type
))
322 if (src
!= *dest
&& src
!= orig_src
)
324 src
= *dest
; // output from last iteration is new source
326 if (enc
->comp_algo
== 0) {
328 /* zlib encoded track */
335 zstream
.zalloc
= (alloc_func
) 0;
336 zstream
.zfree
= (free_func
) 0;
337 zstream
.opaque
= (voidpf
) 0;
338 if (inflateInit(&zstream
) != Z_OK
) {
339 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
340 "[mkv] zlib initialization failed.\n");
343 zstream
.next_in
= (Bytef
*) src
;
344 zstream
.avail_in
= *size
;
347 zstream
.avail_out
= *size
;
351 *dest
= talloc_realloc_size(NULL
, *dest
, *size
);
352 zstream
.next_out
= (Bytef
*) (*dest
+ zstream
.total_out
);
353 result
= inflate(&zstream
, Z_NO_FLUSH
);
354 if (result
!= Z_OK
&& result
!= Z_STREAM_END
) {
355 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
356 "[mkv] zlib decompression failed.\n");
359 inflateEnd(&zstream
);
362 zstream
.avail_out
+= 4000;
363 } while (zstream
.avail_out
== 4000 && zstream
.avail_in
!= 0
364 && result
!= Z_STREAM_END
);
366 *size
= zstream
.total_out
;
367 inflateEnd(&zstream
);
369 } else if (enc
->comp_algo
== 2) {
370 /* lzo encoded track */
372 int dstlen
= *size
* 3;
377 *dest
= talloc_realloc_size(NULL
, *dest
,
378 dstlen
+ AV_LZO_OUTPUT_PADDING
);
380 int result
= av_lzo1x_decode(*dest
, &out_avail
, src
, &srclen
);
383 if (!(result
& AV_LZO_OUTPUT_FULL
)) {
384 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
385 "[mkv] lzo decompression failed.\n");
390 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,
391 "[mkv] lzo decompression buffer too small.\n");
394 *size
= dstlen
- out_avail
;
395 } else if (enc
->comp_algo
== 3) {
396 *dest
= talloc_size(NULL
, *size
+ enc
->comp_settings_len
);
397 memcpy(*dest
, enc
->comp_settings
, enc
->comp_settings_len
);
398 memcpy(*dest
+ enc
->comp_settings_len
, src
, *size
);
399 *size
+= enc
->comp_settings_len
;
404 if (src
!= *dest
&& src
!= orig_src
)
409 static int demux_mkv_read_info(demuxer_t
*demuxer
)
411 mkv_demuxer_t
*mkv_d
= demuxer
->priv
;
412 stream_t
*s
= demuxer
->stream
;
414 mkv_d
->tc_scale
= 1000000;
417 struct ebml_info info
= {};
418 struct ebml_parse_ctx parse_ctx
= {};
419 if (ebml_read_element(s
, &parse_ctx
, &info
, &ebml_info_desc
) < 0)
421 if (info
.n_timecode_scale
) {
422 mkv_d
->tc_scale
= info
.timecode_scale
;
423 mp_msg(MSGT_DEMUX
, MSGL_V
,
424 "[mkv] | + timecode scale: %" PRIu64
"\n", mkv_d
->tc_scale
);
426 if (info
.n_duration
) {
427 mkv_d
->duration
= info
.duration
* mkv_d
->tc_scale
/ 1e9
;
428 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + duration: %.3fs\n",
431 if (info
.n_segment_uid
) {
432 int len
= info
.segment_uid
.len
;
433 if (len
!= sizeof(demuxer
->matroska_data
.segment_uid
)) {
434 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
435 "[mkv] segment uid invalid length %d\n", len
);
437 memcpy(demuxer
->matroska_data
.segment_uid
, info
.segment_uid
.start
,
439 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + segment uid");
440 for (int i
= 0; i
< len
; i
++)
441 mp_msg(MSGT_DEMUX
, MSGL_V
, " %02x",
442 demuxer
->matroska_data
.segment_uid
[i
]);
443 mp_msg(MSGT_DEMUX
, MSGL_V
, "\n");
446 talloc_free(parse_ctx
.talloc_ctx
);
450 static void parse_trackencodings(struct demuxer
*demuxer
,
451 struct mkv_track
*track
,
452 struct ebml_content_encodings
*encodings
)
454 // initial allocation to be a non-NULL context before realloc
455 mkv_content_encoding_t
*ce
= talloc_size(track
, 1);
457 for (int n_enc
= 0; n_enc
< encodings
->n_content_encoding
; n_enc
++) {
458 struct ebml_content_encoding
*enc
= encodings
->content_encoding
+ n_enc
;
459 struct mkv_content_encoding e
= {};
460 e
.order
= enc
->content_encoding_order
;
461 if (enc
->n_content_encoding_scope
)
462 e
.scope
= enc
->content_encoding_scope
;
465 e
.type
= enc
->content_encoding_type
;
467 if (enc
->n_content_compression
) {
468 struct ebml_content_compression
*z
= &enc
->content_compression
;
469 e
.comp_algo
= z
->content_comp_algo
;
470 if (z
->n_content_comp_settings
) {
471 int sz
= z
->content_comp_settings
.len
;
472 e
.comp_settings
= talloc_size(ce
, sz
);
473 memcpy(e
.comp_settings
, z
->content_comp_settings
.start
, sz
);
474 e
.comp_settings_len
= sz
;
479 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Track "
480 "number %u has been encrypted and "
481 "decryption has not yet been\n"
482 "[mkv] implemented. Skipping track.\n",
484 } else if (e
.type
!= 0) {
485 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
486 "[mkv] Unknown content encoding type for "
487 "track %u. Skipping track.\n",
489 } else if (e
.comp_algo
!= 0 && e
.comp_algo
!= 2 && e
.comp_algo
!= 3) {
490 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
491 "[mkv] Track %u has been compressed with "
492 "an unknown/unsupported compression\n"
493 "[mkv] algorithm (%" PRIu64
"). Skipping track.\n",
494 track
->tnum
, e
.comp_algo
);
497 else if (e
.comp_algo
== 0) {
498 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
499 "[mkv] Track %u was compressed with zlib "
500 "but mplayer has not been compiled\n"
501 "[mkv] with support for zlib compression. "
507 for (i
= 0; i
< n_enc
; i
++)
508 if (e
.order
>= ce
[i
].order
)
510 ce
= talloc_realloc_size(track
, ce
, (n_enc
+ 1) * sizeof(*ce
));
511 memmove(ce
+ i
+ 1, ce
+ i
, (n_enc
- i
) * sizeof(*ce
));
512 memcpy(ce
+ i
, &e
, sizeof(e
));
515 track
->encodings
= ce
;
516 track
->num_encodings
= encodings
->n_content_encoding
;
519 static void parse_trackaudio(struct demuxer
*demuxer
, struct mkv_track
*track
,
520 struct ebml_audio
*audio
)
522 if (audio
->n_sampling_frequency
) {
523 track
->a_sfreq
= audio
->sampling_frequency
;
524 mp_msg(MSGT_DEMUX
, MSGL_V
,
525 "[mkv] | + Sampling frequency: %f\n", track
->a_sfreq
);
527 track
->a_sfreq
= 8000;
528 if (audio
->n_bit_depth
) {
529 track
->a_bps
= audio
->bit_depth
;
530 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Bit depth: %u\n",
533 if (audio
->n_channels
) {
534 track
->a_channels
= audio
->channels
;
535 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Channels: %u\n",
538 track
->a_channels
= 1;
541 static void parse_trackvideo(struct demuxer
*demuxer
, struct mkv_track
*track
,
542 struct ebml_video
*video
)
544 if (video
->n_frame_rate
) {
545 track
->v_frate
= video
->frame_rate
;
546 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Frame rate: %f\n",
548 if (track
->v_frate
> 0)
549 track
->default_duration
= 1 / track
->v_frate
;
551 if (video
->n_display_width
) {
552 track
->v_dwidth
= video
->display_width
;
553 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Display width: %u\n",
556 if (video
->n_display_height
) {
557 track
->v_dheight
= video
->display_height
;
558 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Display height: %u\n",
561 if (video
->n_pixel_width
) {
562 track
->v_width
= video
->pixel_width
;
563 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Pixel width: %u\n",
566 if (video
->n_pixel_height
) {
567 track
->v_height
= video
->pixel_height
;
568 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Pixel height: %u\n",
574 * \brief free any data associated with given track
575 * \param track track of which to free data
577 static void demux_mkv_free_trackentry(mkv_track_t
*track
)
579 free(track
->audio_buf
);
580 free(track
->audio_timestamp
);
584 static void parse_trackentry(struct demuxer
*demuxer
,
585 struct ebml_track_entry
*entry
)
587 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
588 struct mkv_track
*track
= talloc_zero_size(NULL
, sizeof(*track
));
590 track
->tnum
= entry
->track_number
;
592 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Track number: %u\n",
595 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Missing track number!\n");
598 track
->name
= talloc_strndup(track
, entry
->name
.start
,
600 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Name: %s\n",
604 track
->type
= entry
->track_type
;
605 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Track type: ");
606 switch (track
->type
) {
607 case MATROSKA_TRACK_AUDIO
:
608 mp_msg(MSGT_DEMUX
, MSGL_V
, "Audio\n");
610 case MATROSKA_TRACK_VIDEO
:
611 mp_msg(MSGT_DEMUX
, MSGL_V
, "Video\n");
613 case MATROSKA_TRACK_SUBTITLE
:
614 mp_msg(MSGT_DEMUX
, MSGL_V
, "Subtitle\n");
617 mp_msg(MSGT_DEMUX
, MSGL_V
, "unknown\n");
621 if (entry
->n_audio
) {
622 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Audio track\n");
623 parse_trackaudio(demuxer
, track
, &entry
->audio
);
626 if (entry
->n_video
) {
627 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Video track\n");
628 parse_trackvideo(demuxer
, track
, &entry
->video
);
631 if (entry
->n_codec_id
) {
632 track
->codec_id
= talloc_strndup(track
, entry
->codec_id
.start
,
633 entry
->codec_id
.len
);
634 if (!strcmp(track
->codec_id
, MKV_V_MSCOMP
)
635 || !strcmp(track
->codec_id
, MKV_A_ACM
))
636 track
->ms_compat
= 1;
637 else if (!strcmp(track
->codec_id
, MKV_S_VOBSUB
))
638 track
->subtitle_type
= MATROSKA_SUBTYPE_VOBSUB
;
639 else if (!strcmp(track
->codec_id
, MKV_S_TEXTSSA
)
640 || !strcmp(track
->codec_id
, MKV_S_TEXTASS
)
641 || !strcmp(track
->codec_id
, MKV_S_SSA
)
642 || !strcmp(track
->codec_id
, MKV_S_ASS
)) {
643 track
->subtitle_type
= MATROSKA_SUBTYPE_SSA
;
644 } else if (!strcmp(track
->codec_id
, MKV_S_TEXTASCII
))
645 track
->subtitle_type
= MATROSKA_SUBTYPE_TEXT
;
646 if (!strcmp(track
->codec_id
, MKV_S_TEXTUTF8
)) {
647 track
->subtitle_type
= MATROSKA_SUBTYPE_TEXT
;
649 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Codec ID: %s\n",
652 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Missing codec ID!\n");
654 if (entry
->n_codec_private
) {
655 int len
= entry
->codec_private
.len
;
656 track
->private_data
= talloc_size(track
, len
+ AV_LZO_INPUT_PADDING
);
657 memcpy(track
->private_data
, entry
->codec_private
.start
, len
);
658 track
->private_size
= len
;
659 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + CodecPrivate, length %u\n",
660 track
->private_size
);
663 if (entry
->n_language
) {
664 track
->language
= talloc_strndup(track
, entry
->language
.start
,
665 entry
->language
.len
);
666 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Language: %s\n",
669 track
->language
= talloc_strdup(track
, "eng");
671 if (entry
->n_flag_default
) {
672 track
->default_track
= entry
->flag_default
;
673 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Default flag: %u\n",
674 track
->default_track
);
676 track
->default_track
= 1;
678 if (entry
->n_default_duration
) {
679 track
->default_duration
= entry
->default_duration
/ 1e9
;
680 if (entry
->default_duration
== 0)
681 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Default duration: 0");
684 track
->v_frate
= 1e9
/ entry
->default_duration
;
685 mp_msg(MSGT_DEMUX
, MSGL_V
,
686 "[mkv] | + Default duration: %.3fms ( = %.3f fps)\n",
687 entry
->default_duration
/ 1000000.0, track
->v_frate
);
691 if (entry
->n_content_encodings
)
692 parse_trackencodings(demuxer
, track
, &entry
->content_encodings
);
694 mkv_d
->tracks
[mkv_d
->num_tracks
++] = track
;
697 static int demux_mkv_read_tracks(demuxer_t
*demuxer
)
699 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
700 stream_t
*s
= demuxer
->stream
;
702 struct ebml_tracks tracks
= {};
703 struct ebml_parse_ctx parse_ctx
= {};
704 if (ebml_read_element(s
, &parse_ctx
, &tracks
, &ebml_tracks_desc
) < 0)
707 mkv_d
->tracks
= talloc_size(mkv_d
,
708 tracks
.n_track_entry
* sizeof(*mkv_d
->tracks
));
709 for (int i
= 0; i
< tracks
.n_track_entry
; i
++) {
710 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + a track...\n");
711 parse_trackentry(demuxer
, &tracks
.track_entry
[i
]);
713 talloc_free(parse_ctx
.talloc_ctx
);
717 static int demux_mkv_read_cues(demuxer_t
*demuxer
)
719 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
720 stream_t
*s
= demuxer
->stream
;
722 if (index_mode
== 0 || index_mode
== 2) {
723 ebml_read_skip(s
, NULL
);
727 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing cues ] -----------\n");
728 struct ebml_cues cues
= {};
729 struct ebml_parse_ctx parse_ctx
= {};
730 if (ebml_read_element(s
, &parse_ctx
, &cues
, &ebml_cues_desc
) < 0)
732 for (int i
= 0; i
< cues
.n_cue_point
; i
++) {
733 struct ebml_cue_point
*cuepoint
= &cues
.cue_point
[i
];
734 if (cuepoint
->n_cue_time
!= 1 || !cuepoint
->n_cue_track_positions
) {
735 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Malformed CuePoint element\n");
738 uint64_t time
= cuepoint
->cue_time
;
739 for (int i
= 0; i
< cuepoint
->n_cue_track_positions
; i
++) {
740 struct ebml_cue_track_positions
*trackpos
=
741 &cuepoint
->cue_track_positions
[i
];
742 uint64_t track
= trackpos
->cue_track
;
743 uint64_t pos
= trackpos
->cue_cluster_position
;
745 grow_array(mkv_d
->indexes
, mkv_d
->num_indexes
,
746 sizeof(mkv_index_t
));
747 mkv_d
->indexes
[mkv_d
->num_indexes
].tnum
= track
;
748 mkv_d
->indexes
[mkv_d
->num_indexes
].timecode
= time
;
749 mkv_d
->indexes
[mkv_d
->num_indexes
].filepos
=
750 mkv_d
->segment_start
+ pos
;
751 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,
752 "[mkv] |+ found cue point for track %" PRIu64
753 ": timecode %" PRIu64
", filepos: %" PRIu64
"\n", track
,
754 time
, mkv_d
->segment_start
+ pos
);
755 mkv_d
->num_indexes
++;
760 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] \\---- [ parsing cues ] -----------\n");
761 talloc_free(parse_ctx
.talloc_ctx
);
765 static int demux_mkv_read_chapters(struct demuxer
*demuxer
)
767 struct MPOpts
*opts
= demuxer
->opts
;
768 stream_t
*s
= demuxer
->stream
;
770 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing chapters ] ---------\n");
771 struct ebml_chapters file_chapters
= {};
772 struct ebml_parse_ctx parse_ctx
= {};
773 if (ebml_read_element(s
, &parse_ctx
, &file_chapters
,
774 &ebml_chapters_desc
) < 0)
777 int selected_edition
= 0;
778 int num_editions
= file_chapters
.n_edition_entry
;
779 struct ebml_edition_entry
*editions
= file_chapters
.edition_entry
;
780 if (opts
->edition_id
>= 0 && opts
->edition_id
< num_editions
) {
781 selected_edition
= opts
->edition_id
;
782 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] User-specified edition: %d\n",
785 for (int i
= 0; i
< num_editions
; i
++)
786 if (editions
[i
].edition_flag_default
) {
787 selected_edition
= i
;
788 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Default edition: %d\n", i
);
791 struct matroska_chapter
*m_chapters
= NULL
;
792 if (editions
[selected_edition
].edition_flag_ordered
) {
793 int count
= editions
[selected_edition
].n_chapter_atom
;
794 m_chapters
= talloc_array_ptrtype(demuxer
, m_chapters
, count
);
795 demuxer
->matroska_data
.ordered_chapters
= m_chapters
;
796 demuxer
->matroska_data
.num_ordered_chapters
= count
;
799 for (int idx
= 0; idx
< num_editions
; idx
++) {
800 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] New edition %d\n", idx
);
801 int warn_level
= idx
== selected_edition
? MSGL_WARN
: MSGL_V
;
802 if (editions
[idx
].n_edition_flag_default
)
803 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Default edition flag: %"PRIu64
804 "\n", editions
[idx
].edition_flag_default
);
805 if (editions
[idx
].n_edition_flag_ordered
)
806 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Ordered chapter flag: %"PRIu64
807 "\n", editions
[idx
].edition_flag_ordered
);
808 for (int i
= 0; i
< editions
[idx
].n_chapter_atom
; i
++) {
809 struct ebml_chapter_atom
*ca
= editions
[idx
].chapter_atom
+ i
;
810 struct matroska_chapter chapter
= { };
811 struct bstr name
= { "(unnamed)", 9 };
813 if (!ca
->n_chapter_time_start
)
814 mp_msg(MSGT_DEMUX
, warn_level
,
815 "[mkv] Chapter lacks start time\n");
816 chapter
.start
= ca
->chapter_time_start
/ 1000000;
817 chapter
.end
= ca
->chapter_time_end
/ 1000000;
819 if (ca
->n_chapter_display
) {
820 if (ca
->n_chapter_display
> 1)
821 mp_msg(MSGT_DEMUX
, warn_level
, "[mkv] Multiple chapter "
822 "names not supported, picking first\n");
823 if (!ca
->chapter_display
[0].n_chap_string
)
824 mp_msg(MSGT_DEMUX
, warn_level
, "[mkv] Malformed chapter "
827 name
= ca
->chapter_display
[0].chap_string
;
830 if (ca
->n_chapter_segment_uid
) {
831 chapter
.has_segment_uid
= true;
832 int len
= ca
->chapter_segment_uid
.len
;
833 if (len
!= sizeof(chapter
.segment_uid
))
834 mp_msg(MSGT_DEMUX
, warn_level
,
835 "[mkv] Chapter segment uid bad length %d\n", len
);
836 else if (ca
->n_chapter_segment_edition_uid
) {
837 mp_tmsg(MSGT_DEMUX
, warn_level
, "[mkv] Warning: "
838 "unsupported edition recursion in chapter; "
839 "will skip on playback!\n");
841 memcpy(chapter
.segment_uid
, ca
->chapter_segment_uid
.start
,
843 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Chapter segment uid ");
844 for (int i
= 0; i
< len
; i
++)
845 mp_msg(MSGT_DEMUX
, MSGL_V
, "%02x ",
846 chapter
.segment_uid
[i
]);
847 mp_msg(MSGT_DEMUX
, MSGL_V
, "\n");
851 mp_msg(MSGT_DEMUX
, MSGL_V
,
852 "[mkv] Chapter %u from %02d:%02d:%02d.%03d "
853 "to %02d:%02d:%02d.%03d, %.*s\n", i
,
854 (int) (chapter
.start
/ 60 / 60 / 1000),
855 (int) ((chapter
.start
/ 60 / 1000) % 60),
856 (int) ((chapter
.start
/ 1000) % 60),
857 (int) (chapter
.start
% 1000),
858 (int) (chapter
.end
/ 60 / 60 / 1000),
859 (int) ((chapter
.end
/ 60 / 1000) % 60),
860 (int) ((chapter
.end
/ 1000) % 60),
861 (int) (chapter
.end
% 1000),
864 if (idx
== selected_edition
){
865 demuxer_add_chapter(demuxer
, name
, chapter
.start
, chapter
.end
);
866 if (editions
[idx
].edition_flag_ordered
) {
867 chapter
.name
= talloc_strndup(m_chapters
, name
.start
,
869 m_chapters
[i
] = chapter
;
874 if (num_editions
> 1)
875 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
876 "[mkv] Found %d editions, will play #%d (first is 0).\n",
877 num_editions
, selected_edition
);
880 talloc_free(parse_ctx
.talloc_ctx
);
881 mp_msg(MSGT_DEMUX
, MSGL_V
,
882 "[mkv] \\---- [ parsing chapters ] ---------\n");
886 static int demux_mkv_read_tags(demuxer_t
*demuxer
)
888 stream_t
*s
= demuxer
->stream
;
890 struct ebml_parse_ctx parse_ctx
= {};
891 struct ebml_tags tags
= {};
892 if (ebml_read_element(s
, &parse_ctx
, &tags
, &ebml_tags_desc
) < 0)
895 for (int i
= 0; i
< tags
.n_tag
; i
++) {
896 struct ebml_tag tag
= tags
.tag
[i
];
897 if (tag
.targets
.target_track_uid
|| tag
.targets
.target_edition_uid
||
898 tag
.targets
.target_chapter_uid
|| tag
.targets
.target_attachment_uid
)
901 for (int j
; j
< tag
.n_simple_tag
; j
++)
902 demux_info_add_bstr(demuxer
, tag
.simple_tag
[j
].tag_name
, tag
.simple_tag
[j
].tag_string
);
908 static int demux_mkv_read_attachments(demuxer_t
*demuxer
)
910 stream_t
*s
= demuxer
->stream
;
912 mp_msg(MSGT_DEMUX
, MSGL_V
,
913 "[mkv] /---- [ parsing attachments ] ---------\n");
915 struct ebml_attachments attachments
= {};
916 struct ebml_parse_ctx parse_ctx
= {};
917 if (ebml_read_element(s
, &parse_ctx
, &attachments
,
918 &ebml_attachments_desc
) < 0)
921 for (int i
= 0; i
< attachments
.n_attached_file
; i
++) {
922 struct ebml_attached_file
*attachment
= &attachments
.attached_file
[i
];
923 if (!attachment
->n_file_name
|| !attachment
->n_file_mime_type
924 || !attachment
->n_file_data
) {
925 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Malformed attachment\n");
928 struct bstr name
= attachment
->file_name
;
929 struct bstr mime
= attachment
->file_mime_type
;
930 demuxer_add_attachment(demuxer
, name
, mime
, attachment
->file_data
);
931 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Attachment: %.*s, %.*s, %zu bytes\n",
932 BSTR_P(name
), BSTR_P(mime
), attachment
->file_data
.len
);
936 talloc_free(parse_ctx
.talloc_ctx
);
937 mp_msg(MSGT_DEMUX
, MSGL_V
,
938 "[mkv] \\---- [ parsing attachments ] ---------\n");
942 static int read_header_element(struct demuxer
*demuxer
, uint32_t id
,
945 static int demux_mkv_read_seekhead(demuxer_t
*demuxer
)
947 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
948 struct stream
*s
= demuxer
->stream
;
950 struct ebml_seek_head seekhead
= {};
951 struct ebml_parse_ctx parse_ctx
= {};
953 mp_msg(MSGT_DEMUX
, MSGL_V
,
954 "[mkv] /---- [ parsing seek head ] ---------\n");
955 if (ebml_read_element(s
, &parse_ctx
, &seekhead
, &ebml_seek_head_desc
) < 0) {
959 /* off now holds the position of the next element after the seek head. */
960 off_t off
= stream_tell(s
);
961 for (int i
= 0; i
< seekhead
.n_seek
; i
++) {
962 struct ebml_seek
*seek
= &seekhead
.seek
[i
];
963 if (seek
->n_seek_id
!= 1 || seek
->n_seek_position
!= 1) {
964 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Invalid SeekHead entry\n");
967 uint64_t pos
= seek
->seek_position
+ mkv_d
->segment_start
;
968 if (pos
>= demuxer
->movi_end
) {
969 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] SeekHead position beyond "
970 "end of file - incomplete file?\n");
973 read_header_element(demuxer
, seek
->seek_id
, pos
);
975 if (!stream_seek(s
, off
)) {
976 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Couldn't seek back after "
981 mp_msg(MSGT_DEMUX
, MSGL_V
,
982 "[mkv] \\---- [ parsing seek head ] ---------\n");
983 talloc_free(parse_ctx
.talloc_ctx
);
987 static bool seek_pos_id(struct stream
*s
, off_t pos
, uint32_t id
)
989 if (!stream_seek(s
, pos
)) {
990 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Failed to seek in file\n");
993 if (ebml_read_id(s
, NULL
) != id
) {
994 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Expected element not found\n");
1000 static int read_header_element(struct demuxer
*demuxer
, uint32_t id
,
1003 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
1004 stream_t
*s
= demuxer
->stream
;
1005 off_t pos
= stream_tell(s
) - 4;
1008 case MATROSKA_ID_INFO
:
1009 if (mkv_d
->parsed_info
)
1011 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1013 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] |+ segment information...\n");
1014 mkv_d
->parsed_info
= true;
1015 return demux_mkv_read_info(demuxer
) ? -1 : 1;
1017 case MATROSKA_ID_TRACKS
:
1018 if (mkv_d
->parsed_tracks
)
1020 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1022 mkv_d
->parsed_tracks
= true;
1023 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] |+ segment tracks...\n");
1024 return demux_mkv_read_tracks(demuxer
) ? -1 : 1;
1026 case MATROSKA_ID_CUES
:
1027 if (is_parsed_header(mkv_d
, pos
))
1029 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1031 return demux_mkv_read_cues(demuxer
) ? -1 : 1;
1033 case MATROSKA_ID_TAGS
:
1034 if (mkv_d
->parsed_tags
)
1036 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1038 mkv_d
->parsed_tags
= true;
1039 return demux_mkv_read_tags(demuxer
) ? -1 : 1;
1041 case MATROSKA_ID_SEEKHEAD
:
1042 if (is_parsed_header(mkv_d
, pos
))
1044 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1046 return demux_mkv_read_seekhead(demuxer
) ? -1 : 1;
1048 case MATROSKA_ID_CHAPTERS
:
1049 if (mkv_d
->parsed_chapters
)
1051 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1053 mkv_d
->parsed_chapters
= true;
1054 return demux_mkv_read_chapters(demuxer
) ? -1 : 1;
1056 case MATROSKA_ID_ATTACHMENTS
:
1057 if (mkv_d
->parsed_attachments
)
1059 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1061 mkv_d
->parsed_attachments
= true;
1062 return demux_mkv_read_attachments(demuxer
) ? -1 : 1;
1066 ebml_read_skip(s
, NULL
);
1070 ebml_read_skip(s
, NULL
);
1076 static int demux_mkv_open_video(demuxer_t
*demuxer
, mkv_track_t
*track
,
1078 static int demux_mkv_open_audio(demuxer_t
*demuxer
, mkv_track_t
*track
,
1080 static int demux_mkv_open_sub(demuxer_t
*demuxer
, mkv_track_t
*track
,
1083 static void display_create_tracks(demuxer_t
*demuxer
)
1085 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1086 int i
, vid
= 0, aid
= 0, sid
= 0;
1088 for (i
= 0; i
< mkv_d
->num_tracks
; i
++) {
1089 char *type
= "unknown", str
[32];
1091 switch (mkv_d
->tracks
[i
]->type
) {
1092 case MATROSKA_TRACK_VIDEO
:
1094 mkv_d
->tracks
[i
]->id
= -1;
1095 if (vid
== MAX_V_STREAMS
)
1097 mkv_d
->tracks
[i
]->id
= vid
;
1098 demux_mkv_open_video(demuxer
, mkv_d
->tracks
[i
], vid
);
1099 if (mkv_d
->tracks
[i
]->name
)
1100 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n", vid
,
1101 mkv_d
->tracks
[i
]->name
);
1102 sprintf(str
, "-vid %u", vid
++);
1104 case MATROSKA_TRACK_AUDIO
:
1106 mkv_d
->tracks
[i
]->id
= -1;
1107 if (aid
== MAX_A_STREAMS
)
1109 mkv_d
->tracks
[i
]->id
= aid
;
1110 demux_mkv_open_audio(demuxer
, mkv_d
->tracks
[i
], aid
);
1111 if (mkv_d
->tracks
[i
]->name
)
1112 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n", aid
,
1113 mkv_d
->tracks
[i
]->name
);
1114 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", aid
,
1115 mkv_d
->tracks
[i
]->language
);
1116 sprintf(str
, "-aid %u, -alang %.5s", aid
++,
1117 mkv_d
->tracks
[i
]->language
);
1119 case MATROSKA_TRACK_SUBTITLE
:
1121 mkv_d
->tracks
[i
]->id
= -1;
1122 if (sid
== MAX_S_STREAMS
)
1124 mkv_d
->tracks
[i
]->id
= sid
;
1125 demux_mkv_open_sub(demuxer
, mkv_d
->tracks
[i
], sid
);
1126 if (mkv_d
->tracks
[i
]->name
)
1127 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n", sid
,
1128 mkv_d
->tracks
[i
]->name
);
1129 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sid
,
1130 mkv_d
->tracks
[i
]->language
);
1131 sprintf(str
, "-sid %u, -slang %.5s", sid
++,
1132 mkv_d
->tracks
[i
]->language
);
1135 if (mkv_d
->tracks
[i
]->name
)
1136 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
,
1137 "[mkv] Track ID %u: %s (%s) \"%s\", %s\n",
1138 mkv_d
->tracks
[i
]->tnum
, type
, mkv_d
->tracks
[i
]->codec_id
,
1139 mkv_d
->tracks
[i
]->name
, str
);
1141 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] Track ID %u: %s (%s), %s\n",
1142 mkv_d
->tracks
[i
]->tnum
, type
, mkv_d
->tracks
[i
]->codec_id
,
1145 mkv_d
->num_audio_tracks
= aid
;
1152 } videocodec_info_t
;
1154 static const videocodec_info_t vinfo
[] = {
1155 {MKV_V_MJPEG
, mmioFOURCC('m', 'j', 'p', 'g'), 1},
1156 {MKV_V_MPEG1
, mmioFOURCC('m', 'p', 'g', '1'), 0},
1157 {MKV_V_MPEG2
, mmioFOURCC('m', 'p', 'g', '2'), 0},
1158 {MKV_V_MPEG4_SP
, mmioFOURCC('m', 'p', '4', 'v'), 1},
1159 {MKV_V_MPEG4_ASP
, mmioFOURCC('m', 'p', '4', 'v'), 1},
1160 {MKV_V_MPEG4_AP
, mmioFOURCC('m', 'p', '4', 'v'), 1},
1161 {MKV_V_MPEG4_AVC
, mmioFOURCC('a', 'v', 'c', '1'), 1},
1162 {MKV_V_THEORA
, mmioFOURCC('t', 'h', 'e', 'o'), 1},
1163 {MKV_V_VP8
, mmioFOURCC('V', 'P', '8', '0'), 0},
1167 static int demux_mkv_open_video(demuxer_t
*demuxer
, mkv_track_t
*track
,
1170 struct MPOpts
*opts
= demuxer
->opts
;
1171 BITMAPINFOHEADER
*bih
;
1172 void *ImageDesc
= NULL
;
1175 if (track
->ms_compat
) { /* MS compatibility mode */
1176 BITMAPINFOHEADER
*src
;
1178 if (track
->private_data
== NULL
1179 || track
->private_size
< sizeof(*bih
))
1182 src
= (BITMAPINFOHEADER
*) track
->private_data
;
1183 bih
= calloc(1, track
->private_size
);
1184 bih
->biSize
= le2me_32(src
->biSize
);
1185 bih
->biWidth
= le2me_32(src
->biWidth
);
1186 bih
->biHeight
= le2me_32(src
->biHeight
);
1187 bih
->biPlanes
= le2me_16(src
->biPlanes
);
1188 bih
->biBitCount
= le2me_16(src
->biBitCount
);
1189 bih
->biCompression
= le2me_32(src
->biCompression
);
1190 bih
->biSizeImage
= le2me_32(src
->biSizeImage
);
1191 bih
->biXPelsPerMeter
= le2me_32(src
->biXPelsPerMeter
);
1192 bih
->biYPelsPerMeter
= le2me_32(src
->biYPelsPerMeter
);
1193 bih
->biClrUsed
= le2me_32(src
->biClrUsed
);
1194 bih
->biClrImportant
= le2me_32(src
->biClrImportant
);
1197 track
->private_size
- sizeof(*bih
));
1199 if (track
->v_width
== 0)
1200 track
->v_width
= bih
->biWidth
;
1201 if (track
->v_height
== 0)
1202 track
->v_height
= bih
->biHeight
;
1204 bih
= calloc(1, sizeof(*bih
));
1205 bih
->biSize
= sizeof(*bih
);
1206 bih
->biWidth
= track
->v_width
;
1207 bih
->biHeight
= track
->v_height
;
1208 bih
->biBitCount
= 24;
1209 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/ 8;
1211 if (track
->private_size
>= RVPROPERTIES_SIZE
1212 && (!strcmp(track
->codec_id
, MKV_V_REALV10
)
1213 || !strcmp(track
->codec_id
, MKV_V_REALV20
)
1214 || !strcmp(track
->codec_id
, MKV_V_REALV30
)
1215 || !strcmp(track
->codec_id
, MKV_V_REALV40
))) {
1216 unsigned char *dst
, *src
;
1220 src
= (uint8_t *) track
->private_data
+ RVPROPERTIES_SIZE
;
1222 cnt
= track
->private_size
- RVPROPERTIES_SIZE
;
1223 bih
= realloc(bih
, sizeof(*bih
) + 8 + cnt
);
1224 bih
->biSize
= 48 + cnt
;
1226 type2
= AV_RB32(src
- 4);
1227 if (type2
== 0x10003000 || type2
== 0x10003001)
1228 bih
->biCompression
= mmioFOURCC('R', 'V', '1', '3');
1230 bih
->biCompression
=
1231 mmioFOURCC('R', 'V', track
->codec_id
[9], '0');
1232 dst
= (unsigned char *) (bih
+ 1);
1233 // copy type1 and type2 info from rv properties
1234 memcpy(dst
, src
- 8, 8 + cnt
);
1235 track
->realmedia
= 1;
1237 #ifdef CONFIG_QTX_CODECS
1238 } else if (track
->private_size
>= sizeof(ImageDescription
)
1239 && !strcmp(track
->codec_id
, MKV_V_QUICKTIME
)) {
1240 ImageDescriptionPtr idesc
;
1242 idesc
= (ImageDescriptionPtr
) track
->private_data
;
1243 idesc
->idSize
= be2me_32(idesc
->idSize
);
1244 idesc
->cType
= be2me_32(idesc
->cType
);
1245 idesc
->version
= be2me_16(idesc
->version
);
1246 idesc
->revisionLevel
= be2me_16(idesc
->revisionLevel
);
1247 idesc
->vendor
= be2me_32(idesc
->vendor
);
1248 idesc
->temporalQuality
= be2me_32(idesc
->temporalQuality
);
1249 idesc
->spatialQuality
= be2me_32(idesc
->spatialQuality
);
1250 idesc
->width
= be2me_16(idesc
->width
);
1251 idesc
->height
= be2me_16(idesc
->height
);
1252 idesc
->hRes
= be2me_32(idesc
->hRes
);
1253 idesc
->vRes
= be2me_32(idesc
->vRes
);
1254 idesc
->dataSize
= be2me_32(idesc
->dataSize
);
1255 idesc
->frameCount
= be2me_16(idesc
->frameCount
);
1256 idesc
->depth
= be2me_16(idesc
->depth
);
1257 idesc
->clutID
= be2me_16(idesc
->clutID
);
1259 bih
->biCompression
= idesc
->cType
;
1261 #endif /* CONFIG_QTX_CODECS */
1264 const videocodec_info_t
*vi
= vinfo
;
1265 while (vi
->id
&& strcmp(vi
->id
, track
->codec_id
))
1267 bih
->biCompression
= vi
->fourcc
;
1268 if (vi
->extradata
&& track
->private_data
1269 && (track
->private_size
> 0)) {
1270 bih
->biSize
+= track
->private_size
;
1271 bih
= realloc(bih
, bih
->biSize
);
1272 memcpy(bih
+ 1, track
->private_data
, track
->private_size
);
1274 track
->reorder_timecodes
= opts
->user_correct_pts
== 0;
1276 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Unknown/unsupported "
1277 "CodecID (%s) or missing/bad CodecPrivate\n"
1278 "[mkv] data (track %u).\n",
1279 track
->codec_id
, track
->tnum
);
1286 sh_v
= new_sh_video(demuxer
, vid
);
1288 sh_v
->format
= sh_v
->bih
->biCompression
;
1289 if (track
->v_frate
== 0.0)
1290 track
->v_frate
= 25.0;
1291 sh_v
->fps
= track
->v_frate
;
1292 sh_v
->frametime
= 1 / track
->v_frate
;
1294 if (!track
->realmedia
) {
1295 sh_v
->disp_w
= track
->v_width
;
1296 sh_v
->disp_h
= track
->v_height
;
1297 if (track
->v_dheight
)
1298 sh_v
->aspect
= (double) track
->v_dwidth
/ track
->v_dheight
;
1300 // vd_realvid.c will set aspect to disp_w/disp_h and rederive
1301 // disp_w and disp_h from the RealVideo stream contents returned
1302 // by the Real DLLs. If DisplayWidth/DisplayHeight was not set in
1303 // the Matroska file then it has already been set to PixelWidth/Height
1304 // by check_track_information.
1305 sh_v
->disp_w
= track
->v_dwidth
;
1306 sh_v
->disp_h
= track
->v_dheight
;
1308 sh_v
->ImageDesc
= ImageDesc
;
1309 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Aspect: %f\n", sh_v
->aspect
);
1311 sh_v
->ds
= demuxer
->video
;
1315 static int demux_mkv_open_audio(demuxer_t
*demuxer
, mkv_track_t
*track
,
1318 sh_audio_t
*sh_a
= new_sh_audio(demuxer
, aid
);
1322 if (track
->language
&& (strcmp(track
->language
, "und") != 0))
1323 sh_a
->lang
= strdup(track
->language
);
1324 sh_a
->default_track
= track
->default_track
;
1325 sh_a
->ds
= demuxer
->audio
;
1326 sh_a
->wf
= malloc(sizeof(*sh_a
->wf
));
1327 if (track
->ms_compat
&& (track
->private_size
>= sizeof(*sh_a
->wf
))) {
1328 WAVEFORMATEX
*wf
= (WAVEFORMATEX
*) track
->private_data
;
1329 sh_a
->wf
= realloc(sh_a
->wf
, track
->private_size
);
1330 sh_a
->wf
->wFormatTag
= le2me_16(wf
->wFormatTag
);
1331 sh_a
->wf
->nChannels
= le2me_16(wf
->nChannels
);
1332 sh_a
->wf
->nSamplesPerSec
= le2me_32(wf
->nSamplesPerSec
);
1333 sh_a
->wf
->nAvgBytesPerSec
= le2me_32(wf
->nAvgBytesPerSec
);
1334 sh_a
->wf
->nBlockAlign
= le2me_16(wf
->nBlockAlign
);
1335 sh_a
->wf
->wBitsPerSample
= le2me_16(wf
->wBitsPerSample
);
1336 sh_a
->wf
->cbSize
= track
->private_size
- sizeof(*sh_a
->wf
);
1337 memcpy(sh_a
->wf
+ 1, wf
+ 1,
1338 track
->private_size
- sizeof(*sh_a
->wf
));
1339 if (track
->a_sfreq
== 0.0)
1340 track
->a_sfreq
= sh_a
->wf
->nSamplesPerSec
;
1341 if (track
->a_channels
== 0)
1342 track
->a_channels
= sh_a
->wf
->nChannels
;
1343 if (track
->a_bps
== 0)
1344 track
->a_bps
= sh_a
->wf
->wBitsPerSample
;
1345 track
->a_formattag
= sh_a
->wf
->wFormatTag
;
1347 memset(sh_a
->wf
, 0, sizeof(*sh_a
->wf
));
1348 if (!strcmp(track
->codec_id
, MKV_A_MP3
)
1349 || !strcmp(track
->codec_id
, MKV_A_MP2
))
1350 track
->a_formattag
= 0x0055;
1351 else if (!strncmp(track
->codec_id
, MKV_A_AC3
, strlen(MKV_A_AC3
)))
1352 track
->a_formattag
= 0x2000;
1353 else if (!strcmp(track
->codec_id
, MKV_A_DTS
))
1354 track
->a_formattag
= 0x2001;
1355 else if (!strcmp(track
->codec_id
, MKV_A_PCM
)
1356 || !strcmp(track
->codec_id
, MKV_A_PCM_BE
))
1357 track
->a_formattag
= 0x0001;
1358 else if (!strcmp(track
->codec_id
, MKV_A_AAC_2MAIN
)
1359 || !strncmp(track
->codec_id
, MKV_A_AAC_2LC
,
1360 strlen(MKV_A_AAC_2LC
))
1361 || !strcmp(track
->codec_id
, MKV_A_AAC_2SSR
)
1362 || !strcmp(track
->codec_id
, MKV_A_AAC_4MAIN
)
1363 || !strncmp(track
->codec_id
, MKV_A_AAC_4LC
,
1364 strlen(MKV_A_AAC_4LC
))
1365 || !strcmp(track
->codec_id
, MKV_A_AAC_4SSR
)
1366 || !strcmp(track
->codec_id
, MKV_A_AAC_4LTP
)
1367 || !strcmp(track
->codec_id
, MKV_A_AAC
))
1368 track
->a_formattag
= mmioFOURCC('M', 'P', '4', 'A');
1369 else if (!strcmp(track
->codec_id
, MKV_A_VORBIS
)) {
1370 if (track
->private_data
== NULL
)
1372 track
->a_formattag
= mmioFOURCC('v', 'r', 'b', 's');
1373 } else if (!strcmp(track
->codec_id
, MKV_A_QDMC
))
1374 track
->a_formattag
= mmioFOURCC('Q', 'D', 'M', 'C');
1375 else if (!strcmp(track
->codec_id
, MKV_A_QDMC2
))
1376 track
->a_formattag
= mmioFOURCC('Q', 'D', 'M', '2');
1377 else if (!strcmp(track
->codec_id
, MKV_A_WAVPACK
))
1378 track
->a_formattag
= mmioFOURCC('W', 'V', 'P', 'K');
1379 else if (!strcmp(track
->codec_id
, MKV_A_TRUEHD
))
1380 track
->a_formattag
= mmioFOURCC('T', 'R', 'H', 'D');
1381 else if (!strcmp(track
->codec_id
, MKV_A_FLAC
)) {
1382 if (track
->private_data
== NULL
|| track
->private_size
== 0) {
1383 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
1384 "[mkv] FLAC track does not contain valid headers.\n");
1387 track
->a_formattag
= mmioFOURCC('f', 'L', 'a', 'C');
1388 } else if (track
->private_size
>= RAPROPERTIES4_SIZE
) {
1389 if (!strcmp(track
->codec_id
, MKV_A_REAL28
))
1390 track
->a_formattag
= mmioFOURCC('2', '8', '_', '8');
1391 else if (!strcmp(track
->codec_id
, MKV_A_REALATRC
))
1392 track
->a_formattag
= mmioFOURCC('a', 't', 'r', 'c');
1393 else if (!strcmp(track
->codec_id
, MKV_A_REALCOOK
))
1394 track
->a_formattag
= mmioFOURCC('c', 'o', 'o', 'k');
1395 else if (!strcmp(track
->codec_id
, MKV_A_REALDNET
))
1396 track
->a_formattag
= mmioFOURCC('d', 'n', 'e', 't');
1397 else if (!strcmp(track
->codec_id
, MKV_A_REALSIPR
))
1398 track
->a_formattag
= mmioFOURCC('s', 'i', 'p', 'r');
1400 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Unknown/unsupported audio "
1401 "codec ID '%s' for track %u or missing/faulty\n[mkv] "
1402 "private codec data.\n", track
->codec_id
, track
->tnum
);
1403 free_sh_audio(demuxer
, track
->id
);
1408 sh_a
->format
= track
->a_formattag
;
1409 sh_a
->wf
->wFormatTag
= track
->a_formattag
;
1410 sh_a
->channels
= track
->a_channels
;
1411 sh_a
->wf
->nChannels
= track
->a_channels
;
1412 sh_a
->samplerate
= (uint32_t) track
->a_sfreq
;
1413 sh_a
->wf
->nSamplesPerSec
= (uint32_t) track
->a_sfreq
;
1414 if (track
->a_bps
== 0) {
1415 sh_a
->samplesize
= 2;
1416 sh_a
->wf
->wBitsPerSample
= 16;
1418 sh_a
->samplesize
= track
->a_bps
/ 8;
1419 sh_a
->wf
->wBitsPerSample
= track
->a_bps
;
1421 if (track
->a_formattag
== 0x0055) { /* MP3 || MP2 */
1422 sh_a
->wf
->nAvgBytesPerSec
= 16000;
1423 sh_a
->wf
->nBlockAlign
= 1152;
1424 } else if ((track
->a_formattag
== 0x2000) /* AC3 */
1425 || (track
->a_formattag
== 0x2001)) { /* DTS */
1428 } else if (track
->a_formattag
== 0x0001) { /* PCM || PCM_BE */
1429 sh_a
->wf
->nAvgBytesPerSec
= sh_a
->channels
* sh_a
->samplerate
* 2;
1430 sh_a
->wf
->nBlockAlign
= sh_a
->wf
->nAvgBytesPerSec
;
1431 if (!strcmp(track
->codec_id
, MKV_A_PCM_BE
))
1432 sh_a
->format
= mmioFOURCC('t', 'w', 'o', 's');
1433 } else if (!strcmp(track
->codec_id
, MKV_A_QDMC
)
1434 || !strcmp(track
->codec_id
, MKV_A_QDMC2
)) {
1435 sh_a
->wf
->nAvgBytesPerSec
= 16000;
1436 sh_a
->wf
->nBlockAlign
= 1486;
1437 track
->fix_i_bps
= 1;
1438 track
->qt_last_a_pts
= 0.0;
1439 if (track
->private_data
!= NULL
) {
1440 sh_a
->codecdata
= malloc(track
->private_size
);
1441 memcpy(sh_a
->codecdata
, track
->private_data
, track
->private_size
);
1442 sh_a
->codecdata_len
= track
->private_size
;
1444 } else if (track
->a_formattag
== mmioFOURCC('M', 'P', '4', 'A')) {
1445 int profile
, srate_idx
;
1447 sh_a
->wf
->nAvgBytesPerSec
= 16000;
1448 sh_a
->wf
->nBlockAlign
= 1024;
1450 if (!strcmp(track
->codec_id
, MKV_A_AAC
)
1451 && (NULL
!= track
->private_data
)) {
1452 sh_a
->codecdata
= malloc(track
->private_size
);
1453 memcpy(sh_a
->codecdata
, track
->private_data
, track
->private_size
);
1454 sh_a
->codecdata_len
= track
->private_size
;
1458 /* Recreate the 'private data' */
1459 /* which faad2 uses in its initialization */
1460 srate_idx
= aac_get_sample_rate_index(sh_a
->samplerate
);
1461 if (!strncmp(&track
->codec_id
[12], "MAIN", 4))
1463 else if (!strncmp(&track
->codec_id
[12], "LC", 2))
1465 else if (!strncmp(&track
->codec_id
[12], "SSR", 3))
1469 sh_a
->codecdata
= malloc(5);
1470 sh_a
->codecdata
[0] = ((profile
+ 1) << 3) | ((srate_idx
& 0xE) >> 1);
1471 sh_a
->codecdata
[1] =
1472 ((srate_idx
& 0x1) << 7) | (track
->a_channels
<< 3);
1474 if (strstr(track
->codec_id
, "SBR") != NULL
) {
1475 /* HE-AAC (aka SBR AAC) */
1476 sh_a
->codecdata_len
= 5;
1478 sh_a
->samplerate
*= 2;
1479 sh_a
->wf
->nSamplesPerSec
*= 2;
1480 srate_idx
= aac_get_sample_rate_index(sh_a
->samplerate
);
1481 sh_a
->codecdata
[2] = AAC_SYNC_EXTENSION_TYPE
>> 3;
1482 sh_a
->codecdata
[3] = ((AAC_SYNC_EXTENSION_TYPE
& 0x07) << 5) | 5;
1483 sh_a
->codecdata
[4] = (1 << 7) | (srate_idx
<< 3);
1484 track
->default_duration
= 1024.0 / (sh_a
->samplerate
/ 2);
1486 sh_a
->codecdata_len
= 2;
1487 track
->default_duration
= 1024.0 / sh_a
->samplerate
;
1489 } else if (track
->a_formattag
== mmioFOURCC('v', 'r', 'b', 's')) { /* VORBIS */
1490 sh_a
->wf
->cbSize
= track
->private_size
;
1491 sh_a
->wf
= realloc(sh_a
->wf
, sizeof(*sh_a
->wf
) + sh_a
->wf
->cbSize
);
1492 memcpy((unsigned char *) (sh_a
->wf
+ 1), track
->private_data
,
1494 } else if (track
->private_size
>= RAPROPERTIES4_SIZE
1495 && !strncmp(track
->codec_id
, MKV_A_REALATRC
, 7)) {
1496 /* Common initialization for all RealAudio codecs */
1497 unsigned char *src
= track
->private_data
;
1498 int codecdata_length
, version
;
1501 sh_a
->wf
->nAvgBytesPerSec
= 0; /* FIXME !? */
1503 version
= AV_RB16(src
+ 4);
1504 flavor
= AV_RB16(src
+ 22);
1505 track
->coded_framesize
= AV_RB32(src
+ 24);
1506 track
->sub_packet_h
= AV_RB16(src
+ 40);
1507 sh_a
->wf
->nBlockAlign
= track
->audiopk_size
= AV_RB16(src
+ 42);
1508 track
->sub_packet_size
= AV_RB16(src
+ 44);
1510 src
+= RAPROPERTIES4_SIZE
;
1514 src
+= RAPROPERTIES5_SIZE
;
1519 codecdata_length
= AV_RB32(src
);
1521 sh_a
->wf
->cbSize
= codecdata_length
;
1522 sh_a
->wf
= realloc(sh_a
->wf
, sizeof(*sh_a
->wf
) + sh_a
->wf
->cbSize
);
1523 memcpy(((char *) (sh_a
->wf
+ 1)), src
, codecdata_length
);
1525 switch (track
->a_formattag
) {
1526 case mmioFOURCC('a', 't', 'r', 'c'):
1527 sh_a
->wf
->nAvgBytesPerSec
= atrc_fl2bps
[flavor
];
1528 sh_a
->wf
->nBlockAlign
= track
->sub_packet_size
;
1530 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1531 track
->audio_timestamp
=
1532 malloc(track
->sub_packet_h
* sizeof(double));
1534 case mmioFOURCC('c', 'o', 'o', 'k'):
1535 sh_a
->wf
->nAvgBytesPerSec
= cook_fl2bps
[flavor
];
1536 sh_a
->wf
->nBlockAlign
= track
->sub_packet_size
;
1538 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1539 track
->audio_timestamp
=
1540 malloc(track
->sub_packet_h
* sizeof(double));
1542 case mmioFOURCC('s', 'i', 'p', 'r'):
1543 sh_a
->wf
->nAvgBytesPerSec
= sipr_fl2bps
[flavor
];
1544 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
1546 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1547 track
->audio_timestamp
=
1548 malloc(track
->sub_packet_h
* sizeof(double));
1550 case mmioFOURCC('2', '8', '_', '8'):
1551 sh_a
->wf
->nAvgBytesPerSec
= 3600;
1552 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
1554 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1555 track
->audio_timestamp
=
1556 malloc(track
->sub_packet_h
* sizeof(double));
1560 track
->realmedia
= 1;
1561 } else if (!strcmp(track
->codec_id
, MKV_A_FLAC
)
1562 || (track
->a_formattag
== 0xf1ac)) {
1568 if (track
->a_formattag
== mmioFOURCC('f', 'L', 'a', 'C')) {
1569 ptr
= track
->private_data
;
1570 size
= track
->private_size
;
1572 sh_a
->format
= mmioFOURCC('f', 'L', 'a', 'C');
1573 ptr
= track
->private_data
+ sizeof(*sh_a
->wf
);
1574 size
= track
->private_size
- sizeof(*sh_a
->wf
);
1576 if (size
< 4 || ptr
[0] != 'f' || ptr
[1] != 'L' || ptr
[2] != 'a'
1578 sh_a
->codecdata
= malloc(4);
1579 sh_a
->codecdata_len
= 4;
1580 memcpy(sh_a
->codecdata
, "fLaC", 4);
1582 sh_a
->codecdata
= malloc(size
);
1583 sh_a
->codecdata_len
= size
;
1584 memcpy(sh_a
->codecdata
, ptr
, size
);
1586 } else if (track
->a_formattag
== mmioFOURCC('W', 'V', 'P', 'K') || track
->a_formattag
== mmioFOURCC('T', 'R', 'H', 'D')) { /* do nothing, still works */
1587 } else if (!track
->ms_compat
1588 || (track
->private_size
< sizeof(*sh_a
->wf
))) {
1589 free_sh_audio(demuxer
, track
->id
);
1596 static int demux_mkv_open_sub(demuxer_t
*demuxer
, mkv_track_t
*track
,
1599 if (track
->subtitle_type
!= MATROSKA_SUBTYPE_UNKNOWN
) {
1602 sh_sub_t
*sh
= new_sh_sub(demuxer
, sid
);
1605 if (track
->subtitle_type
== MATROSKA_SUBTYPE_VOBSUB
)
1607 if (track
->subtitle_type
== MATROSKA_SUBTYPE_SSA
)
1609 size
= track
->private_size
;
1610 demux_mkv_decode(track
, track
->private_data
, &buffer
, &size
, 2);
1611 if (buffer
&& buffer
!= track
->private_data
) {
1612 talloc_free(track
->private_data
);
1613 talloc_steal(track
, buffer
);
1614 track
->private_data
= buffer
;
1615 track
->private_size
= size
;
1617 sh
->extradata
= malloc(track
->private_size
);
1618 memcpy(sh
->extradata
, track
->private_data
, track
->private_size
);
1619 sh
->extradata_len
= track
->private_size
;
1620 if (track
->language
&& (strcmp(track
->language
, "und") != 0))
1621 sh
->lang
= strdup(track
->language
);
1622 sh
->default_track
= track
->default_track
;
1624 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
1625 "[mkv] Subtitle type '%s' is not supported.\n",
1633 static int demux_mkv_open(demuxer_t
*demuxer
)
1635 stream_t
*s
= demuxer
->stream
;
1636 mkv_demuxer_t
*mkv_d
;
1640 stream_seek(s
, s
->start_pos
);
1641 if (ebml_read_id(s
, NULL
) != EBML_ID_EBML
)
1643 struct ebml_ebml ebml_master
= {};
1644 struct ebml_parse_ctx parse_ctx
= { .no_error_messages
= true };
1645 if (ebml_read_element(s
, &parse_ctx
, &ebml_master
, &ebml_ebml_desc
) < 0)
1647 if (ebml_master
.doc_type
.start
== NULL
) {
1648 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] File has EBML header but no doctype."
1649 " Assuming \"matroska\".\n");
1650 } else if (bstrcmp(ebml_master
.doc_type
, BSTR("matroska")) != 0
1651 && bstrcmp(ebml_master
.doc_type
, BSTR("webm")) != 0) {
1652 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] no head found\n");
1653 talloc_free(parse_ctx
.talloc_ctx
);
1656 if (ebml_master
.doc_type_read_version
> 2) {
1657 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] This looks like a Matroska file, "
1658 "but we don't support format version %"PRIu64
"\n",
1659 ebml_master
.doc_type_read_version
);
1660 talloc_free(parse_ctx
.talloc_ctx
);
1663 if ((ebml_master
.n_ebml_read_version
1664 && ebml_master
.ebml_read_version
!= EBML_VERSION
)
1665 || (ebml_master
.n_ebml_max_size_length
1666 && ebml_master
.ebml_max_size_length
> 8)
1667 || (ebml_master
.n_ebml_max_id_length
1668 && ebml_master
.ebml_max_id_length
!= 4)) {
1669 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] This looks like a Matroska file, "
1670 "but the header has bad parameters\n");
1671 talloc_free(parse_ctx
.talloc_ctx
);
1674 talloc_free(parse_ctx
.talloc_ctx
);
1676 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Found the head...\n");
1678 if (ebml_read_id(s
, NULL
) != MATROSKA_ID_SEGMENT
) {
1679 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] but no segment :(\n");
1682 ebml_read_length(s
, NULL
); /* return bytes number until EOF */
1684 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] + a segment...\n");
1686 mkv_d
= talloc_zero(demuxer
, struct mkv_demuxer
);
1687 demuxer
->priv
= mkv_d
;
1688 mkv_d
->tc_scale
= 1000000;
1689 mkv_d
->segment_start
= stream_tell(s
);
1692 uint32_t id
= ebml_read_id(s
, NULL
);
1694 case MATROSKA_ID_CLUSTER
:
1695 mp_msg(MSGT_DEMUX
, MSGL_V
,
1696 "[mkv] |+ found cluster, headers are "
1697 "parsed completely :)\n");
1698 stream_seek(s
, stream_tell(s
) - 4);
1703 cont
= read_header_element(demuxer
, id
, 0) < 1;
1706 ebml_read_skip(s
, NULL
);
1711 display_create_tracks(demuxer
);
1713 /* select video track */
1715 if (demuxer
->video
->id
== -1) { /* automatically select a video track */
1716 /* search for a video track that has the 'default' flag set */
1717 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1718 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_VIDEO
1719 && mkv_d
->tracks
[i
]->default_track
) {
1720 track
= mkv_d
->tracks
[i
];
1725 /* no track has the 'default' flag set */
1726 /* let's take the first video track */
1727 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1728 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_VIDEO
1729 && mkv_d
->tracks
[i
]->id
>= 0) {
1730 track
= mkv_d
->tracks
[i
];
1733 } else if (demuxer
->video
->id
!= -2) /* -2 = no video at all */
1734 track
= find_track_by_num(mkv_d
, demuxer
->video
->id
,
1735 MATROSKA_TRACK_VIDEO
);
1737 if (track
&& demuxer
->v_streams
[track
->id
]) {
1738 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] Will play video track %u.\n",
1740 demuxer
->video
->id
= track
->id
;
1741 demuxer
->video
->sh
= demuxer
->v_streams
[track
->id
];
1743 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] No video track found/wanted.\n");
1744 demuxer
->video
->id
= -2;
1747 /* select audio track */
1750 /* search for an audio track that has the 'default' flag set */
1751 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1752 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_AUDIO
1753 && mkv_d
->tracks
[i
]->default_track
) {
1754 track
= mkv_d
->tracks
[i
];
1759 /* no track has the 'default' flag set */
1760 /* let's take the first audio track */
1761 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1762 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_AUDIO
1763 && mkv_d
->tracks
[i
]->id
>= 0) {
1764 track
= mkv_d
->tracks
[i
];
1768 if (track
&& demuxer
->a_streams
[track
->id
]) {
1769 demuxer
->audio
->id
= track
->id
;
1770 demuxer
->audio
->sh
= demuxer
->a_streams
[track
->id
];
1772 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] No audio track found/wanted.\n");
1773 demuxer
->audio
->id
= -2;
1776 if (s
->end_pos
== 0)
1777 demuxer
->seekable
= 0;
1779 demuxer
->movi_start
= s
->start_pos
;
1780 demuxer
->movi_end
= s
->end_pos
;
1781 demuxer
->seekable
= 1;
1784 demuxer
->accurate_seek
= true;
1786 return DEMUXER_TYPE_MATROSKA
;
1789 static void demux_close_mkv(demuxer_t
*demuxer
)
1791 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1795 free_cached_dps(demuxer
);
1796 if (mkv_d
->tracks
) {
1797 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1798 demux_mkv_free_trackentry(mkv_d
->tracks
[i
]);
1800 free(mkv_d
->indexes
);
1801 free(mkv_d
->cluster_positions
);
1805 static int demux_mkv_read_block_lacing(uint8_t *buffer
, uint64_t *size
,
1807 uint32_t **all_lace_sizes
)
1810 uint32_t *lace_size
= NULL
;
1814 *all_lace_sizes
= NULL
;
1821 switch ((flags
& 0x06) >> 1) {
1822 case 0: /* no lacing */
1824 lace_size
= calloc(*laces
, sizeof(uint32_t));
1825 lace_size
[0] = *size
;
1828 case 1: /* xiph lacing */
1829 case 2: /* fixed-size lacing */
1830 case 3: /* EBML lacing */
1836 lace_size
= calloc(*laces
, sizeof(uint32_t));
1838 switch ((flags
& 0x06) >> 1) {
1839 case 1: /* xiph lacing */
1840 for (i
= 0; i
< *laces
- 1; i
++) {
1845 lace_size
[i
] += *buffer
;
1847 } while (*buffer
++ == 0xFF);
1848 if (lace_size
[i
] > *size
- total
|| total
> *size
)
1850 total
+= lace_size
[i
];
1852 lace_size
[i
] = *size
- total
;
1855 case 2: /* fixed-size lacing */
1856 for (i
= 0; i
< *laces
; i
++)
1857 lace_size
[i
] = *size
/ *laces
;
1860 case 3:; /* EBML lacing */
1862 uint64_t num
= ebml_read_vlen_uint(buffer
, &l
);
1863 if (num
== EBML_UINT_INVALID
)
1872 total
= lace_size
[0] = num
;
1873 for (i
= 1; i
< *laces
- 1; i
++) {
1874 int64_t snum
= ebml_read_vlen_int(buffer
, &l
);
1875 if (snum
== EBML_INT_INVALID
)
1881 lace_size
[i
] = lace_size
[i
- 1] + snum
;
1882 if (lace_size
[i
] > *size
- total
|| total
> *size
)
1884 total
+= lace_size
[i
];
1886 lace_size
[i
] = *size
- total
;
1891 *all_lace_sizes
= lace_size
;
1896 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Bad input [lacing]\n");
1900 static void handle_subtitles(demuxer_t
*demuxer
, mkv_track_t
*track
,
1901 char *block
, int64_t size
,
1902 uint64_t block_duration
, uint64_t timecode
)
1906 if (block_duration
== 0) {
1907 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
1908 "[mkv] Warning: No BlockDuration for subtitle track found.\n");
1913 dp
= new_demux_packet(size
);
1914 memcpy(dp
->buffer
, block
, size
);
1915 dp
->pts
= timecode
/ 1000.0;
1916 dp
->endpts
= (timecode
+ block_duration
) / 1000.0;
1917 ds_add_packet(demuxer
->sub
, dp
);
1920 static void handle_realvideo(demuxer_t
*demuxer
, mkv_track_t
*track
,
1921 uint8_t *buffer
, uint32_t size
, int block_bref
)
1923 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1925 uint32_t timestamp
= mkv_d
->last_pts
* 1000;
1927 dp
= new_demux_packet(size
);
1928 memcpy(dp
->buffer
, buffer
, size
);
1930 if (mkv_d
->v_skip_to_keyframe
) {
1931 dp
->pts
= mkv_d
->last_pts
;
1932 track
->rv_kf_base
= 0;
1933 track
->rv_kf_pts
= timestamp
;
1936 real_fix_timestamp(dp
->buffer
, timestamp
,
1937 ((sh_video_t
*) demuxer
->video
->sh
)->bih
->
1938 biCompression
, &track
->rv_kf_base
,
1939 &track
->rv_kf_pts
, NULL
);
1940 dp
->pos
= demuxer
->filepos
;
1941 dp
->flags
= block_bref
? 0 : 0x10;
1943 ds_add_packet(demuxer
->video
, dp
);
1946 static void handle_realaudio(demuxer_t
*demuxer
, mkv_track_t
*track
,
1947 uint8_t *buffer
, uint32_t size
, int block_bref
)
1949 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1950 int sps
= track
->sub_packet_size
;
1951 int sph
= track
->sub_packet_h
;
1952 int cfs
= track
->coded_framesize
;
1953 int w
= track
->audiopk_size
;
1954 int spc
= track
->sub_packet_cnt
;
1958 if ((track
->a_formattag
== mmioFOURCC('2', '8', '_', '8'))
1959 || (track
->a_formattag
== mmioFOURCC('c', 'o', 'o', 'k'))
1960 || (track
->a_formattag
== mmioFOURCC('a', 't', 'r', 'c'))
1961 || (track
->a_formattag
== mmioFOURCC('s', 'i', 'p', 'r'))) {
1963 // spc = track->sub_packet_cnt = 0;
1964 switch (track
->a_formattag
) {
1965 case mmioFOURCC('2', '8', '_', '8'):
1966 for (x
= 0; x
< sph
/ 2; x
++)
1967 memcpy(track
->audio_buf
+ x
* 2 * w
+ spc
* cfs
,
1968 buffer
+ cfs
* x
, cfs
);
1970 case mmioFOURCC('c', 'o', 'o', 'k'):
1971 case mmioFOURCC('a', 't', 'r', 'c'):
1972 for (x
= 0; x
< w
/ sps
; x
++)
1973 memcpy(track
->audio_buf
+
1974 sps
* (sph
* x
+ ((sph
+ 1) / 2) * (spc
& 1) +
1975 (spc
>> 1)), buffer
+ sps
* x
, sps
);
1977 case mmioFOURCC('s', 'i', 'p', 'r'):
1978 memcpy(track
->audio_buf
+ spc
* w
, buffer
, w
);
1979 if (spc
== sph
- 1) {
1981 int bs
= sph
* w
* 2 / 96; // nibbles per subpacket
1982 // Perform reordering
1983 for (n
= 0; n
< 38; n
++) {
1985 int i
= bs
* sipr_swaps
[n
][0];
1986 int o
= bs
* sipr_swaps
[n
][1];
1987 // swap nibbles of block 'i' with 'o' TODO: optimize
1988 for (j
= 0; j
< bs
; j
++) {
1990 (track
->audio_buf
[i
>> 1] >> 4) :
1991 (track
->audio_buf
[i
>> 1] & 0x0F);
1993 (track
->audio_buf
[o
>> 1] >> 4) :
1994 (track
->audio_buf
[o
>> 1] & 0x0F);
1996 track
->audio_buf
[o
>> 1] =
1997 (track
->audio_buf
[o
>> 1] & 0x0F) | (x
<< 4);
1999 track
->audio_buf
[o
>> 1] =
2000 (track
->audio_buf
[o
>> 1] & 0xF0) | x
;
2002 track
->audio_buf
[i
>> 1] =
2003 (track
->audio_buf
[i
>> 1] & 0x0F) | (y
<< 4);
2005 track
->audio_buf
[i
>> 1] =
2006 (track
->audio_buf
[i
>> 1] & 0xF0) | y
;
2014 track
->audio_timestamp
[track
->sub_packet_cnt
] =
2015 (track
->ra_pts
== mkv_d
->last_pts
) ? 0 : (mkv_d
->last_pts
);
2016 track
->ra_pts
= mkv_d
->last_pts
;
2017 if (track
->sub_packet_cnt
== 0)
2018 track
->audio_filepos
= demuxer
->filepos
;
2019 if (++(track
->sub_packet_cnt
) == sph
) {
2021 ((sh_audio_t
*) demuxer
->audio
->sh
)->wf
->nBlockAlign
;
2022 track
->sub_packet_cnt
= 0;
2023 // Release all the audio packets
2024 for (x
= 0; x
< sph
* w
/ apk_usize
; x
++) {
2025 dp
= new_demux_packet(apk_usize
);
2026 memcpy(dp
->buffer
, track
->audio_buf
+ x
* apk_usize
,
2028 /* Put timestamp only on packets that correspond to original
2029 * audio packets in file */
2030 dp
->pts
= (x
* apk_usize
% w
) ? 0 :
2031 track
->audio_timestamp
[x
* apk_usize
/ w
];
2032 dp
->pos
= track
->audio_filepos
; // all equal
2033 dp
->flags
= x
? 0 : 0x10; // Mark first packet as keyframe
2034 ds_add_packet(demuxer
->audio
, dp
);
2037 } else { // Not a codec that require reordering
2038 dp
= new_demux_packet(size
);
2039 memcpy(dp
->buffer
, buffer
, size
);
2040 if (track
->ra_pts
== mkv_d
->last_pts
&& !mkv_d
->a_skip_to_keyframe
)
2043 dp
->pts
= mkv_d
->last_pts
;
2044 track
->ra_pts
= mkv_d
->last_pts
;
2046 dp
->pos
= demuxer
->filepos
;
2047 dp
->flags
= block_bref
? 0 : 0x10;
2048 ds_add_packet(demuxer
->audio
, dp
);
2052 /** Reorder timecodes and add cached demux packets to the queues.
2054 * Timecode reordering is needed if a video track contains B frames that
2055 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2056 * MPlayer doesn't like timestamps in display order. This function adjusts
2057 * the timestamp of cached frames (which are exactly one I/P frame followed
2058 * by one or more B frames) so that they are in coding order again.
2060 * Example: The track with 25 FPS contains four frames with the timecodes
2061 * I at 0ms, P at 120ms, B at 40ms and B at 80ms. As soon as the next I
2062 * or P frame arrives these timecodes can be changed to I at 0ms, P at 40ms,
2063 * B at 80ms and B at 120ms.
2065 * This works for simple H.264 B-frame pyramids, but not for arbitrary orders.
2067 * \param demuxer The Matroska demuxer struct for this instance.
2068 * \param track The track structure whose cache should be handled.
2070 static void flush_cached_dps(demuxer_t
*demuxer
, mkv_track_t
*track
)
2074 if (track
->num_cached_dps
== 0)
2079 for (i
= 1; i
< track
->num_cached_dps
; i
++)
2080 if (track
->cached_dps
[i
- 1]->pts
> track
->cached_dps
[i
]->pts
) {
2081 double tmp_pts
= track
->cached_dps
[i
- 1]->pts
;
2082 track
->cached_dps
[i
- 1]->pts
= track
->cached_dps
[i
]->pts
;
2083 track
->cached_dps
[i
]->pts
= tmp_pts
;
2088 for (i
= 0; i
< track
->num_cached_dps
; i
++)
2089 ds_add_packet(demuxer
->video
, track
->cached_dps
[i
]);
2090 track
->num_cached_dps
= 0;
2093 /** Cache video frames if timecodes have to be reordered.
2095 * Timecode reordering is needed if a video track contains B frames that
2096 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2097 * This function takes in a Matroska block read from the file, allocates a
2098 * demux packet for it, fills in its values, allocates space for storing
2099 * pointers to the cached demux packets and adds the packet to it. If
2100 * the packet contains an I or a P frame then ::flush_cached_dps is called
2101 * in order to send the old cached frames downstream.
2103 * \param demuxer The Matroska demuxer struct for this instance.
2104 * \param track The packet is meant for this track.
2105 * \param buffer The actual frame contents.
2106 * \param size The frame size in bytes.
2107 * \param block_bref A relative timecode (backward reference). If it is \c 0
2108 * then the frame is an I frame.
2109 * \param block_fref A relative timecode (forward reference). If it is \c 0
2110 * then the frame is either an I frame or a P frame depending on the value
2111 * of \a block_bref. Otherwise it's a B frame.
2113 static void handle_video_bframes(demuxer_t
*demuxer
, mkv_track_t
*track
,
2114 uint8_t *buffer
, uint32_t size
,
2115 int block_bref
, int block_fref
)
2117 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2120 dp
= new_demux_packet(size
);
2121 memcpy(dp
->buffer
, buffer
, size
);
2122 dp
->pos
= demuxer
->filepos
;
2123 dp
->pts
= mkv_d
->last_pts
;
2124 if ((track
->num_cached_dps
> 0) && (dp
->pts
< track
->max_pts
))
2126 if (block_fref
== 0) /* I or P frame */
2127 flush_cached_dps(demuxer
, track
);
2128 if (block_bref
!= 0) /* I frame, don't cache it */
2130 if ((track
->num_cached_dps
+ 1) > track
->num_allocated_dps
) {
2131 track
->cached_dps
= (demux_packet_t
**)
2132 realloc(track
->cached_dps
,
2133 (track
->num_cached_dps
+ 10) * sizeof(demux_packet_t
*));
2134 track
->num_allocated_dps
+= 10;
2136 track
->cached_dps
[track
->num_cached_dps
] = dp
;
2137 track
->num_cached_dps
++;
2138 if (dp
->pts
> track
->max_pts
)
2139 track
->max_pts
= dp
->pts
;
2142 static int handle_block(demuxer_t
*demuxer
, uint8_t *block
, uint64_t length
,
2143 uint64_t block_duration
, int64_t block_bref
,
2144 int64_t block_fref
, uint8_t simpleblock
)
2146 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2147 mkv_track_t
*track
= NULL
;
2148 demux_stream_t
*ds
= NULL
;
2149 uint64_t old_length
;
2151 uint32_t *lace_size
;
2152 uint8_t laces
, flags
;
2153 int i
, num
, tmp
, use_this_block
= 1;
2157 /* first byte(s): track num */
2158 num
= ebml_read_vlen_uint(block
, &tmp
);
2160 /* time (relative to cluster time) */
2161 time
= block
[0] << 8 | block
[1];
2164 old_length
= length
;
2166 if (demux_mkv_read_block_lacing(block
, &length
, &laces
, &lace_size
))
2168 block
+= old_length
- length
;
2170 tc
= (time
* mkv_d
->tc_scale
+ mkv_d
->cluster_tc
) / 1000000.0 + 0.5;
2173 current_pts
= tc
/ 1000.0;
2175 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
2176 if (mkv_d
->tracks
[i
]->tnum
== num
) {
2177 track
= mkv_d
->tracks
[i
];
2180 if (track
== NULL
) {
2184 if (track
->type
== MATROSKA_TRACK_AUDIO
2185 && track
->id
== demuxer
->audio
->id
) {
2186 ds
= demuxer
->audio
;
2188 if (mkv_d
->a_skip_to_keyframe
) {
2190 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2192 } else if (block_bref
!= 0)
2195 if (mkv_d
->v_skip_to_keyframe
)
2198 if (track
->fix_i_bps
&& use_this_block
) {
2199 sh_audio_t
*sh
= (sh_audio_t
*) ds
->sh
;
2201 if (block_duration
!= 0) {
2202 sh
->i_bps
= length
* 1000 / block_duration
;
2203 track
->fix_i_bps
= 0;
2204 } else if (track
->qt_last_a_pts
== 0.0)
2205 track
->qt_last_a_pts
= current_pts
;
2206 else if (track
->qt_last_a_pts
!= current_pts
) {
2207 sh
->i_bps
= length
/ (current_pts
- track
->qt_last_a_pts
);
2208 track
->fix_i_bps
= 0;
2211 } else if (tc
< mkv_d
->skip_to_timecode
)
2213 else if (track
->type
== MATROSKA_TRACK_VIDEO
2214 && track
->id
== demuxer
->video
->id
) {
2215 ds
= demuxer
->video
;
2216 if (mkv_d
->v_skip_to_keyframe
) {
2218 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2220 } else if (block_bref
!= 0 || block_fref
!= 0)
2223 } else if (track
->type
== MATROSKA_TRACK_SUBTITLE
2224 && track
->id
== demuxer
->sub
->id
) {
2226 if (track
->subtitle_type
!= MATROSKA_SUBTYPE_VOBSUB
) {
2229 demux_mkv_decode(track
, block
, &buffer
, &size
, 1);
2230 handle_subtitles(demuxer
, track
, buffer
, size
, block_duration
, tc
);
2231 if (buffer
!= block
)
2232 talloc_free(buffer
);
2238 if (use_this_block
) {
2239 mkv_d
->last_pts
= current_pts
;
2240 mkv_d
->last_filepos
= demuxer
->filepos
;
2242 for (i
= 0; i
< laces
; i
++) {
2243 if (ds
== demuxer
->video
&& track
->realmedia
)
2244 handle_realvideo(demuxer
, track
, block
, lace_size
[i
],
2246 else if (ds
== demuxer
->audio
&& track
->realmedia
)
2247 handle_realaudio(demuxer
, track
, block
, lace_size
[i
],
2249 else if (ds
== demuxer
->video
&& track
->reorder_timecodes
)
2250 handle_video_bframes(demuxer
, track
, block
, lace_size
[i
],
2251 block_bref
, block_fref
);
2253 int size
= lace_size
[i
];
2256 demux_mkv_decode(track
, block
, &buffer
, &size
, 1);
2258 dp
= new_demux_packet(size
);
2259 memcpy(dp
->buffer
, buffer
, size
);
2260 if (buffer
!= block
)
2261 talloc_free(buffer
);
2262 dp
->flags
= (block_bref
== 0
2263 && block_fref
== 0) ? 0x10 : 0;
2264 /* If default_duration is 0, assume no pts value is known
2265 * for packets after the first one (rather than all pts
2266 * values being the same) */
2267 if (i
== 0 || track
->default_duration
)
2269 mkv_d
->last_pts
+ i
* track
->default_duration
;
2270 ds_add_packet(ds
, dp
);
2273 block
+= lace_size
[i
];
2276 if (ds
== demuxer
->video
) {
2277 mkv_d
->v_skip_to_keyframe
= 0;
2278 mkv_d
->skip_to_timecode
= 0;
2279 } else if (ds
== demuxer
->audio
)
2280 mkv_d
->a_skip_to_keyframe
= 0;
2290 static int demux_mkv_fill_buffer(demuxer_t
*demuxer
, demux_stream_t
*ds
)
2292 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2293 stream_t
*s
= demuxer
->stream
;
2298 while (mkv_d
->cluster_size
> 0) {
2299 uint64_t block_duration
= 0, block_length
= 0;
2300 int64_t block_bref
= 0, block_fref
= 0;
2301 uint8_t *block
= NULL
;
2303 while (mkv_d
->blockgroup_size
> 0) {
2304 switch (ebml_read_id(s
, &il
)) {
2305 case MATROSKA_ID_BLOCKDURATION
:
2306 block_duration
= ebml_read_uint(s
, &l
);
2307 if (block_duration
== EBML_UINT_INVALID
) {
2312 block_duration
* mkv_d
->tc_scale
/ 1000000.0 + 0.5;
2315 case MATROSKA_ID_BLOCK
:
2316 block_length
= ebml_read_length(s
, &tmp
);
2318 if (block_length
> 500000000)
2320 block
= malloc(block_length
+ AV_LZO_INPUT_PADDING
);
2321 demuxer
->filepos
= stream_tell(s
);
2322 if (stream_read(s
, block
, block_length
) !=
2323 (int) block_length
) {
2327 l
= tmp
+ block_length
;
2330 case MATROSKA_ID_REFERENCEBLOCK
:;
2331 int64_t num
= ebml_read_int(s
, &l
);
2332 if (num
== EBML_INT_INVALID
) {
2342 case EBML_ID_INVALID
:
2347 ebml_read_skip(s
, &l
);
2350 mkv_d
->blockgroup_size
-= l
+ il
;
2351 mkv_d
->cluster_size
-= l
+ il
;
2355 int res
= handle_block(demuxer
, block
, block_length
,
2356 block_duration
, block_bref
, block_fref
,
2365 if (mkv_d
->cluster_size
> 0) {
2366 switch (ebml_read_id(s
, &il
)) {
2367 case MATROSKA_ID_TIMECODE
:;
2368 uint64_t num
= ebml_read_uint(s
, &l
);
2369 if (num
== EBML_UINT_INVALID
)
2371 mkv_d
->cluster_tc
= num
* mkv_d
->tc_scale
;
2372 add_cluster_position(mkv_d
, mkv_d
->cluster_start
,
2376 case MATROSKA_ID_BLOCKGROUP
:
2377 mkv_d
->blockgroup_size
= ebml_read_length(s
, &tmp
);
2381 case MATROSKA_ID_SIMPLEBLOCK
:;
2383 block_length
= ebml_read_length(s
, &tmp
);
2384 if (block_length
> 500000000)
2386 block
= malloc(block_length
);
2387 demuxer
->filepos
= stream_tell(s
);
2388 if (stream_read(s
, block
, block_length
) !=
2389 (int) block_length
) {
2393 l
= tmp
+ block_length
;
2394 res
= handle_block(demuxer
, block
, block_length
,
2395 block_duration
, block_bref
,
2398 mkv_d
->cluster_size
-= l
+ il
;
2404 mkv_d
->cluster_size
+= l
+ il
;
2407 case EBML_ID_INVALID
:
2411 ebml_read_skip(s
, &l
);
2414 mkv_d
->cluster_size
-= l
+ il
;
2418 while (ebml_read_id(s
, &il
) != MATROSKA_ID_CLUSTER
) {
2419 ebml_read_skip(s
, NULL
);
2423 mkv_d
->cluster_start
= stream_tell(s
) - il
;
2424 mkv_d
->cluster_size
= ebml_read_length(s
, NULL
);
2430 static int seek_creating_index(struct demuxer
*demuxer
, float rel_seek_secs
,
2433 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
2434 struct stream
*s
= demuxer
->stream
;
2435 int64_t target_tc_ns
= (int64_t) (rel_seek_secs
* 1e9
);
2436 if (target_tc_ns
< 0)
2438 uint64_t max_filepos
= 0;
2439 int64_t max_tc
= -1;
2440 int n
= mkv_d
->num_cluster_pos
;
2442 max_filepos
= mkv_d
->cluster_positions
[n
- 1].filepos
;
2443 max_tc
= mkv_d
->cluster_positions
[n
- 1].timecode
;
2446 if (target_tc_ns
> max_tc
) {
2447 if ((off_t
) max_filepos
> stream_tell(s
))
2448 stream_seek(s
, max_filepos
);
2450 stream_seek(s
, stream_tell(s
) + mkv_d
->cluster_size
);
2451 /* parse all the clusters upto target_filepos */
2453 uint64_t start
= stream_tell(s
);
2454 uint32_t type
= ebml_read_id(s
, NULL
);
2455 uint64_t len
= ebml_read_length(s
, NULL
);
2456 uint64_t end
= stream_tell(s
) + len
;
2457 if (type
== MATROSKA_ID_CLUSTER
) {
2458 while (!s
->eof
&& stream_tell(s
) < end
) {
2459 if (ebml_read_id(s
, NULL
) == MATROSKA_ID_TIMECODE
) {
2460 uint64_t tc
= ebml_read_uint(s
, NULL
);
2461 tc
*= mkv_d
->tc_scale
;
2462 add_cluster_position(mkv_d
, start
, tc
);
2463 if (tc
>= target_tc_ns
)
2471 stream_seek(s
, end
);
2477 if (!mkv_d
->num_cluster_pos
) {
2478 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] no target for seek found\n");
2481 uint64_t cluster_pos
= mkv_d
->cluster_positions
[0].filepos
;
2482 /* Let's find the nearest cluster */
2483 int64_t min_diff
= 0xFFFFFFFFFFFFFFF;
2484 for (int i
= 0; i
< mkv_d
->num_cluster_pos
; i
++) {
2485 int64_t diff
= mkv_d
->cluster_positions
[i
].timecode
- target_tc_ns
;
2486 if (flags
& SEEK_BACKWARD
&& diff
< 0 && -diff
< min_diff
) {
2487 cluster_pos
= mkv_d
->cluster_positions
[i
].filepos
;
2489 } else if (flags
& SEEK_FORWARD
2490 && (diff
< 0 ? -1 * diff
: diff
) < min_diff
) {
2491 cluster_pos
= mkv_d
->cluster_positions
[i
].filepos
;
2492 min_diff
= diff
< 0 ? -1 * diff
: diff
;
2495 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
2496 stream_seek(s
, cluster_pos
);
2500 static struct mkv_index
*seek_with_cues(struct demuxer
*demuxer
, int seek_id
,
2501 int64_t target_timecode
, int flags
)
2503 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
2504 int64_t min_diff
= 0xFFFFFFFFFFFFFFF;
2505 struct mkv_index
*index
= NULL
;
2507 /* let's find the entry in the indexes with the smallest */
2508 /* difference to the wanted timecode. */
2509 for (int i
= 0; i
< mkv_d
->num_indexes
; i
++)
2510 if (seek_id
< 0 || mkv_d
->indexes
[i
].tnum
== seek_id
) {
2513 (int64_t) (mkv_d
->indexes
[i
].timecode
*
2514 mkv_d
->tc_scale
/ 1000000.0 + 0.5);
2516 if (flags
& SEEK_BACKWARD
) {
2517 // Seek backward: find the last index position
2518 // before target time
2519 if (diff
< 0 || diff
>= min_diff
)
2522 // Seek forward: find the first index position
2523 // after target time. If no such index exists, find last
2524 // position between current position and target time.
2526 if (min_diff
<= 0 && diff
<= min_diff
)
2529 FFMIN(target_timecode
- mkv_d
->last_pts
, min_diff
))
2533 index
= mkv_d
->indexes
+ i
;
2536 if (index
) { /* We've found an entry. */
2537 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
2538 stream_seek(demuxer
->stream
, index
->filepos
);
2543 static void demux_mkv_seek(demuxer_t
*demuxer
, float rel_seek_secs
,
2544 float audio_delay
, int flags
)
2546 mkv_demuxer_t
*mkv_d
= demuxer
->priv
;
2547 uint64_t v_tnum
= -1;
2548 if (demuxer
->video
->id
>= 0)
2549 v_tnum
= find_track_by_num(mkv_d
, demuxer
->video
->id
,
2550 MATROSKA_TRACK_VIDEO
)->tnum
;
2551 uint64_t a_tnum
= -1;
2552 if (demuxer
->audio
->id
>= 0)
2553 a_tnum
= find_track_by_num(mkv_d
, demuxer
->audio
->id
,
2554 MATROSKA_TRACK_AUDIO
)->tnum
;
2555 if (!(flags
& (SEEK_BACKWARD
| SEEK_FORWARD
))) {
2556 if (flags
& SEEK_ABSOLUTE
|| rel_seek_secs
< 0)
2557 flags
|= SEEK_BACKWARD
;
2559 flags
|= SEEK_FORWARD
;
2561 // Adjust the target a little bit to catch cases where the target position
2562 // specifies a keyframe with high, but not perfect, precision.
2563 rel_seek_secs
+= flags
& SEEK_FORWARD
? -0.005 : 0.005;
2565 free_cached_dps(demuxer
);
2566 if (!(flags
& SEEK_FACTOR
)) { /* time in secs */
2567 mkv_index_t
*index
= NULL
;
2569 if (!(flags
& SEEK_ABSOLUTE
)) /* relative seek */
2570 rel_seek_secs
+= mkv_d
->last_pts
;
2571 int64_t target_timecode
= rel_seek_secs
* 1000.0;
2572 if (target_timecode
< 0)
2573 target_timecode
= 0;
2575 if (mkv_d
->indexes
== NULL
) { /* no index was found */
2576 if (seek_creating_index(demuxer
, rel_seek_secs
, flags
) < 0)
2579 int seek_id
= (demuxer
->video
->id
< 0) ?
2581 index
= seek_with_cues(demuxer
, seek_id
, target_timecode
, flags
);
2583 index
= seek_with_cues(demuxer
, -1, target_timecode
, flags
);
2586 if (demuxer
->video
->id
>= 0)
2587 mkv_d
->v_skip_to_keyframe
= 1;
2588 if (flags
& SEEK_FORWARD
)
2589 mkv_d
->skip_to_timecode
= target_timecode
;
2591 mkv_d
->skip_to_timecode
= index
? index
->timecode
: 0;
2592 mkv_d
->a_skip_to_keyframe
= 1;
2594 demux_mkv_fill_buffer(demuxer
, NULL
);
2595 } else if ((demuxer
->movi_end
<= 0) || !(flags
& SEEK_ABSOLUTE
))
2596 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
2598 stream_t
*s
= demuxer
->stream
;
2599 uint64_t target_filepos
;
2600 mkv_index_t
*index
= NULL
;
2603 if (mkv_d
->indexes
== NULL
) { /* not implemented without index */
2604 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
2608 target_filepos
= (uint64_t) (demuxer
->movi_end
* rel_seek_secs
);
2609 for (i
= 0; i
< mkv_d
->num_indexes
; i
++)
2610 if (mkv_d
->indexes
[i
].tnum
== v_tnum
)
2612 || ((mkv_d
->indexes
[i
].filepos
>= target_filepos
)
2613 && ((index
->filepos
< target_filepos
)
2614 || (mkv_d
->indexes
[i
].filepos
< index
->filepos
))))
2615 index
= &mkv_d
->indexes
[i
];
2620 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
2621 stream_seek(s
, index
->filepos
);
2623 if (demuxer
->video
->id
>= 0)
2624 mkv_d
->v_skip_to_keyframe
= 1;
2625 mkv_d
->skip_to_timecode
= index
->timecode
;
2626 mkv_d
->a_skip_to_keyframe
= 1;
2628 demux_mkv_fill_buffer(demuxer
, NULL
);
2632 static int demux_mkv_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
2634 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2637 case DEMUXER_CTRL_CORRECT_PTS
:
2638 return DEMUXER_CTRL_OK
;
2639 case DEMUXER_CTRL_GET_TIME_LENGTH
:
2640 if (mkv_d
->duration
== 0)
2641 return DEMUXER_CTRL_DONTKNOW
;
2643 *((double *) arg
) = (double) mkv_d
->duration
;
2644 return DEMUXER_CTRL_OK
;
2646 case DEMUXER_CTRL_GET_PERCENT_POS
:
2647 if (mkv_d
->duration
== 0) {
2648 return DEMUXER_CTRL_DONTKNOW
;
2651 *((int *) arg
) = (int) (100 * mkv_d
->last_pts
/ mkv_d
->duration
);
2652 return DEMUXER_CTRL_OK
;
2654 case DEMUXER_CTRL_SWITCH_AUDIO
:;
2655 int new_aid
= *(int *) arg
;
2656 int current_aid
= demuxer
->audio
->id
;
2657 if (current_aid
< 0)
2659 if (new_aid
== -1) // cycle to next
2660 new_aid
= (current_aid
+ 2) % (mkv_d
->num_audio_tracks
+ 1) - 1;
2661 if (new_aid
< 0 || new_aid
>= mkv_d
->num_audio_tracks
)
2663 *(int *) arg
= new_aid
;
2664 if (current_aid
!= new_aid
)
2665 ds_free_packs(demuxer
->audio
);
2666 demuxer
->audio
->id
= new_aid
;
2667 return DEMUXER_CTRL_OK
;
2670 return DEMUXER_CTRL_NOTIMPL
;
2674 const demuxer_desc_t demuxer_desc_matroska
= {
2680 DEMUXER_TYPE_MATROSKA
,
2681 1, // safe autodetect
2683 demux_mkv_fill_buffer
,