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.
34 #include "stream/stream.h"
44 #include "subreader.h"
45 #include "libvo/sub.h"
49 #include "libavutil/common.h"
51 #ifdef CONFIG_QTX_CODECS
52 #include "loader/qtx/qtxsdk/components.h"
59 #include "libavutil/lzo.h"
60 #include "ffmpeg_files/intreadwrite.h"
61 #include "libavutil/avstring.h"
63 static const unsigned char sipr_swaps
[38][2] = {
64 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
65 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
66 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
67 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
71 // Map flavour to bytes per second
72 #define SIPR_FLAVORS 4
73 #define ATRC_FLAVORS 8
74 #define COOK_FLAVORS 34
75 static const int sipr_fl2bps
[SIPR_FLAVORS
] = { 813, 1062, 625, 2000 };
76 static const int atrc_fl2bps
[ATRC_FLAVORS
] =
77 { 8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100 };
78 static const int cook_fl2bps
[COOK_FLAVORS
] = {
79 1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498,
80 4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584,
81 4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513,
82 12016, 16408, 22911, 33506
86 uint32_t order
, type
, scope
;
88 uint8_t *comp_settings
;
89 int comp_settings_len
;
90 } mkv_content_encoding_t
;
92 typedef struct mkv_track
{
102 uint32_t v_width
, v_height
, v_dwidth
, v_dheight
;
105 uint32_t a_formattag
;
106 uint32_t a_channels
, a_bps
;
109 double default_duration
;
113 unsigned char *private_data
;
114 unsigned int private_size
;
116 /* stuff for realmedia */
120 double rv_pts
; /* previous video timestamp */
121 double ra_pts
; /* previous audio timestamp */
123 /** realaudio descrambling */
124 int sub_packet_size
; ///< sub packet size, per stream
125 int sub_packet_h
; ///< number of coded frames per block
126 int coded_framesize
; ///< coded frame size, per stream
127 int audiopk_size
; ///< audio packet size
128 unsigned char *audio_buf
; ///< place to store reordered audio data
129 double *audio_timestamp
; ///< timestamp for each audio packet
130 int sub_packet_cnt
; ///< number of subpacket already received
131 int audio_filepos
; ///< file position of first audio packet in block
133 /* stuff for quicktime */
135 double qt_last_a_pts
;
139 /* The timecodes of video frames might have to be reordered if they're
140 in display order (the timecodes, not the frames themselves!). In this
141 case demux packets have to be cached with the help of these variables. */
142 int reorder_timecodes
;
143 demux_packet_t
**cached_dps
;
144 int num_cached_dps
, num_allocated_dps
;
147 /* generic content encoding support */
148 mkv_content_encoding_t
*encodings
;
151 /* For VobSubs and SSA/ASS */
155 typedef struct mkv_index
{
157 uint64_t timecode
, filepos
;
160 typedef struct mkv_demuxer
{
163 double duration
, last_pts
;
164 uint64_t last_filepos
;
166 mkv_track_t
**tracks
;
169 uint64_t tc_scale
, cluster_tc
;
171 uint64_t cluster_start
;
172 uint64_t cluster_size
;
173 uint64_t blockgroup_size
;
175 mkv_index_t
*indexes
;
183 bool parsed_chapters
;
184 bool parsed_attachments
;
189 } *cluster_positions
;
192 int64_t skip_to_timecode
;
193 int v_skip_to_keyframe
, a_skip_to_keyframe
;
196 int audio_tracks
[MAX_A_STREAMS
];
199 #define REALHEADER_SIZE 16
200 #define RVPROPERTIES_SIZE 34
201 #define RAPROPERTIES4_SIZE 56
202 #define RAPROPERTIES5_SIZE 70
205 * \brief ensures there is space for at least one additional element
206 * \param array array to grow
207 * \param nelem current number of elements in array
208 * \param elsize size of one array element
210 static void *grow_array(void *array
, int nelem
, size_t elsize
)
213 array
= realloc(array
, (nelem
+ 32) * elsize
);
217 static bool is_parsed_header(struct mkv_demuxer
*mkv_d
, off_t pos
)
220 int high
= mkv_d
->num_parsed_pos
;
221 while (high
> low
+ 1) {
222 int mid
= high
+ low
>> 1;
223 if (mkv_d
->parsed_pos
[mid
] > pos
)
228 if (mkv_d
->num_parsed_pos
&& mkv_d
->parsed_pos
[low
] == pos
)
230 if (!(mkv_d
->num_parsed_pos
& 31))
231 mkv_d
->parsed_pos
= talloc_realloc(mkv_d
, mkv_d
->parsed_pos
, off_t
,
232 mkv_d
->num_parsed_pos
+ 32);
233 mkv_d
->num_parsed_pos
++;
234 for (int i
= mkv_d
->num_parsed_pos
- 1; i
> low
; i
--)
235 mkv_d
->parsed_pos
[i
] = mkv_d
->parsed_pos
[i
- 1];
236 mkv_d
->parsed_pos
[low
] = pos
;
240 static mkv_track_t
*demux_mkv_find_track_by_num(mkv_demuxer_t
*d
, int n
,
245 for (i
= 0, id
= 0; i
< d
->num_tracks
; i
++)
246 if (d
->tracks
[i
] != NULL
&& d
->tracks
[i
]->type
== type
)
253 static void add_cluster_position(mkv_demuxer_t
*mkv_d
, uint64_t filepos
,
259 int n
= mkv_d
->num_cluster_pos
;
260 if (n
> 0 && mkv_d
->cluster_positions
[n
-1].filepos
>= filepos
)
263 mkv_d
->cluster_positions
=
264 grow_array(mkv_d
->cluster_positions
, mkv_d
->num_cluster_pos
,
265 sizeof(*mkv_d
->cluster_positions
));
266 mkv_d
->cluster_positions
[mkv_d
->num_cluster_pos
++] = (struct cluster_pos
){
268 .timecode
= timecode
,
273 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
274 static int aac_get_sample_rate_index(uint32_t sample_rate
)
276 static const int srates
[] = {
277 92017, 75132, 55426, 46009, 37566, 27713,
278 23004, 18783, 13856, 11502, 9391, 0
281 while (sample_rate
< srates
[i
])
286 /** \brief Free cached demux packets
288 * Reordering the timecodes requires caching of demux packets. This function
289 * frees all these cached packets and the memory for the cached pointers
292 * \param demuxer The demuxer for which the cache is to be freed.
294 static void free_cached_dps(demuxer_t
*demuxer
)
296 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
300 for (k
= 0; k
< mkv_d
->num_tracks
; k
++) {
301 track
= mkv_d
->tracks
[k
];
302 for (i
= 0; i
< track
->num_cached_dps
; i
++)
303 free_demux_packet(track
->cached_dps
[i
]);
304 free(track
->cached_dps
);
305 track
->cached_dps
= NULL
;
306 track
->num_cached_dps
= 0;
307 track
->num_allocated_dps
= 0;
312 static int demux_mkv_decode(mkv_track_t
*track
, uint8_t *src
,
313 uint8_t **dest
, uint32_t *size
, uint32_t type
)
319 if (track
->num_encodings
<= 0)
322 for (i
= 0; i
< track
->num_encodings
; i
++) {
323 if (!(track
->encodings
[i
].scope
& type
))
327 if (track
->encodings
[i
].comp_algo
== 0) {
328 /* zlib encoded track */
331 zstream
.zalloc
= (alloc_func
) 0;
332 zstream
.zfree
= (free_func
) 0;
333 zstream
.opaque
= (voidpf
) 0;
334 if (inflateInit(&zstream
) != Z_OK
) {
335 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
336 "[mkv] zlib initialization failed.\n");
339 zstream
.next_in
= (Bytef
*) src
;
340 zstream
.avail_in
= *size
;
344 zstream
.avail_out
= *size
;
347 *dest
= realloc(*dest
, *size
);
348 zstream
.next_out
= (Bytef
*) (*dest
+ zstream
.total_out
);
349 result
= inflate(&zstream
, Z_NO_FLUSH
);
350 if (result
!= Z_OK
&& result
!= Z_STREAM_END
) {
351 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
352 "[mkv] zlib decompression failed.\n");
355 inflateEnd(&zstream
);
358 zstream
.avail_out
+= 4000;
359 } while (zstream
.avail_out
== 4000 && zstream
.avail_in
!= 0
360 && result
!= Z_STREAM_END
);
362 *size
= zstream
.total_out
;
363 inflateEnd(&zstream
);
366 if (track
->encodings
[i
].comp_algo
== 2) {
367 /* lzo encoded track */
368 int dstlen
= *size
* 3;
373 if (dstlen
> SIZE_MAX
- AV_LZO_OUTPUT_PADDING
)
375 *dest
= realloc(*dest
, dstlen
+ AV_LZO_OUTPUT_PADDING
);
376 result
= av_lzo1x_decode(*dest
, &dstlen
, src
, &srclen
);
379 if (!(result
& AV_LZO_OUTPUT_FULL
)) {
381 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
382 "[mkv] lzo decompression failed.\n");
387 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,
388 "[mkv] lzo decompression buffer too small.\n");
399 static int demux_mkv_read_info(demuxer_t
*demuxer
)
401 mkv_demuxer_t
*mkv_d
= demuxer
->priv
;
402 stream_t
*s
= demuxer
->stream
;
404 mkv_d
->tc_scale
= 1000000;
407 struct ebml_info info
= {};
408 struct ebml_parse_ctx parse_ctx
= {};
409 if (ebml_read_element(s
, &parse_ctx
, &info
, &ebml_info_desc
) < 0)
411 if (info
.n_timecode_scale
) {
412 mkv_d
->tc_scale
= info
.timecode_scale
;
413 mp_msg(MSGT_DEMUX
, MSGL_V
,
414 "[mkv] | + timecode scale: %" PRIu64
"\n", mkv_d
->tc_scale
);
416 if (info
.n_duration
) {
417 mkv_d
->duration
= info
.duration
* mkv_d
->tc_scale
/ 1e9
;
418 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + duration: %.3fs\n",
421 if (info
.n_segment_uid
) {
422 int len
= info
.segment_uid
.len
;
423 if (len
!= sizeof(demuxer
->matroska_data
.segment_uid
)) {
424 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
425 "[mkv] segment uid invalid length %d\n", len
);
427 memcpy(demuxer
->matroska_data
.segment_uid
, info
.segment_uid
.start
,
429 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + segment uid");
430 for (int i
= 0; i
< len
; i
++)
431 mp_msg(MSGT_DEMUX
, MSGL_V
, " %02x",
432 demuxer
->matroska_data
.segment_uid
[i
]);
433 mp_msg(MSGT_DEMUX
, MSGL_V
, "\n");
436 talloc_free(parse_ctx
.talloc_ctx
);
441 * \brief free array of kv_content_encoding_t
442 * \param encodings pointer to array
443 * \param numencodings number of encodings in array
445 static void demux_mkv_free_encodings(mkv_content_encoding_t
*encodings
,
448 while (numencodings
-- > 0)
449 free(encodings
[numencodings
].comp_settings
);
453 static int demux_mkv_read_trackencodings(demuxer_t
*demuxer
,
456 stream_t
*s
= demuxer
->stream
;
457 mkv_content_encoding_t
*ce
, e
;
458 uint64_t len1
, len2
, length
, l
;
461 ce
= malloc(sizeof(*ce
));
464 len1
= length
= ebml_read_length(s
, &il
);
467 switch (ebml_read_id(s
, &il
)) {
468 case MATROSKA_ID_CONTENTENCODING
:
469 memset(&e
, 0, sizeof(e
));
472 len2
= ebml_read_length(s
, &i
);
479 switch (ebml_read_id(s
, &il
)) {
480 case MATROSKA_ID_CONTENTENCODINGORDER
:
481 num
= ebml_read_uint(s
, &l
);
482 if (num
== EBML_UINT_INVALID
)
487 case MATROSKA_ID_CONTENTENCODINGSCOPE
:
488 num
= ebml_read_uint(s
, &l
);
489 if (num
== EBML_UINT_INVALID
)
494 case MATROSKA_ID_CONTENTENCODINGTYPE
:
495 num
= ebml_read_uint(s
, &l
);
496 if (num
== EBML_UINT_INVALID
)
501 case MATROSKA_ID_CONTENTCOMPRESSION
:;
504 le
= ebml_read_length(s
, &i
);
511 switch (ebml_read_id(s
, &il
)) {
512 case MATROSKA_ID_CONTENTCOMPALGO
:
513 num
= ebml_read_uint(s
, &l
);
514 if (num
== EBML_UINT_INVALID
)
519 case MATROSKA_ID_CONTENTCOMPSETTINGS
:
520 l
= ebml_read_length(s
, &i
);
521 e
.comp_settings
= malloc(l
);
522 stream_read(s
, e
.comp_settings
, l
);
523 e
.comp_settings_len
= l
;
528 ebml_read_skip(s
, &l
);
535 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Track "
536 "number %u has been encrypted and "
537 "decryption has not yet been\n"
538 "[mkv] implemented. Skipping track.\n",
540 } else if (e
.type
!= 0) {
541 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
542 "[mkv] Unknown content encoding type for "
543 "track %u. Skipping track.\n",
547 if (e
.comp_algo
!= 0 && e
.comp_algo
!= 2) {
548 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
549 "[mkv] Track %u has been compressed with "
550 "an unknown/unsupported compression\n"
551 "[mkv] algorithm (%u). Skipping track.\n",
552 track
->tnum
, e
.comp_algo
);
555 else if (e
.comp_algo
== 0) {
556 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
557 "[mkv] Track %u was compressed with zlib "
558 "but mplayer has not been compiled\n"
559 "[mkv] with support for zlib compression. "
568 ebml_read_skip(s
, &l
);
573 for (i
= 0; i
< n
; i
++)
574 if (e
.order
<= ce
[i
].order
)
576 ce
= realloc(ce
, (n
+ 1) * sizeof(*ce
));
577 memmove(ce
+ i
+ 1, ce
+ i
, (n
- i
) * sizeof(*ce
));
578 memcpy(ce
+ i
, &e
, sizeof(e
));
583 ebml_read_skip(s
, &l
);
590 track
->encodings
= ce
;
591 track
->num_encodings
= n
;
595 demux_mkv_free_encodings(ce
, n
);
599 static int demux_mkv_read_trackaudio(demuxer_t
*demuxer
, mkv_track_t
*track
)
601 stream_t
*s
= demuxer
->stream
;
602 uint64_t len
, length
, l
;
607 track
->a_sfreq
= 8000.0;
608 track
->a_channels
= 1;
610 len
= length
= ebml_read_length(s
, &il
);
613 switch (ebml_read_id(s
, &il
)) {
614 case MATROSKA_ID_SAMPLINGFREQUENCY
:
615 fnum
= ebml_read_float(s
, &l
);
616 if (fnum
== EBML_FLOAT_INVALID
)
618 track
->a_sfreq
= fnum
;
619 mp_msg(MSGT_DEMUX
, MSGL_V
,
620 "[mkv] | + Sampling frequency: %f\n", track
->a_sfreq
);
623 case MATROSKA_ID_BITDEPTH
:
624 num
= ebml_read_uint(s
, &l
);
625 if (num
== EBML_UINT_INVALID
)
628 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Bit depth: %u\n",
632 case MATROSKA_ID_CHANNELS
:
633 num
= ebml_read_uint(s
, &l
);
634 if (num
== EBML_UINT_INVALID
)
636 track
->a_channels
= num
;
637 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Channels: %u\n",
642 ebml_read_skip(s
, &l
);
650 static int demux_mkv_read_trackvideo(demuxer_t
*demuxer
, mkv_track_t
*track
)
652 stream_t
*s
= demuxer
->stream
;
653 uint64_t len
, length
, l
;
658 len
= length
= ebml_read_length(s
, &il
);
661 switch (ebml_read_id(s
, &il
)) {
662 case MATROSKA_ID_FRAMERATE
:
663 fnum
= ebml_read_float(s
, &l
);
664 if (fnum
== EBML_FLOAT_INVALID
)
666 track
->v_frate
= fnum
;
667 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Frame rate: %f\n",
669 if (track
->v_frate
> 0)
670 track
->default_duration
= 1 / track
->v_frate
;
673 case MATROSKA_ID_DISPLAYWIDTH
:
674 num
= ebml_read_uint(s
, &l
);
675 if (num
== EBML_UINT_INVALID
)
677 track
->v_dwidth
= num
;
678 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Display width: %u\n",
682 case MATROSKA_ID_DISPLAYHEIGHT
:
683 num
= ebml_read_uint(s
, &l
);
684 if (num
== EBML_UINT_INVALID
)
686 track
->v_dheight
= num
;
687 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Display height: %u\n",
691 case MATROSKA_ID_PIXELWIDTH
:
692 num
= ebml_read_uint(s
, &l
);
693 if (num
== EBML_UINT_INVALID
)
695 track
->v_width
= num
;
696 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Pixel width: %u\n",
700 case MATROSKA_ID_PIXELHEIGHT
:
701 num
= ebml_read_uint(s
, &l
);
702 if (num
== EBML_UINT_INVALID
)
704 track
->v_height
= num
;
705 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Pixel height: %u\n",
710 ebml_read_skip(s
, &l
);
719 * \brief free any data associated with given track
720 * \param track track of which to free data
722 static void demux_mkv_free_trackentry(mkv_track_t
*track
)
725 free(track
->codec_id
);
726 free(track
->language
);
727 free(track
->private_data
);
728 free(track
->audio_buf
);
729 free(track
->audio_timestamp
);
730 demux_mkv_free_encodings(track
->encodings
, track
->num_encodings
);
734 static int demux_mkv_read_trackentry(demuxer_t
*demuxer
)
736 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
737 stream_t
*s
= demuxer
->stream
;
739 uint64_t len
, length
, l
;
743 track
= calloc(1, sizeof(*track
));
744 /* set default values */
745 track
->default_track
= 1;
747 track
->language
= strdup("eng");
749 len
= length
= ebml_read_length(s
, &il
);
752 switch (ebml_read_id(s
, &il
)) {
753 case MATROSKA_ID_TRACKNUMBER
:
754 num
= ebml_read_uint(s
, &l
);
755 if (num
== EBML_UINT_INVALID
)
758 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Track number: %u\n",
762 case MATROSKA_ID_NAME
:
763 track
->name
= ebml_read_utf8(s
, &l
);
764 if (track
->name
== NULL
)
766 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Name: %s\n",
770 case MATROSKA_ID_TRACKTYPE
:
771 num
= ebml_read_uint(s
, &l
);
772 if (num
== EBML_UINT_INVALID
)
775 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Track type: ");
776 switch (track
->type
) {
777 case MATROSKA_TRACK_AUDIO
:
778 mp_msg(MSGT_DEMUX
, MSGL_V
, "Audio\n");
780 case MATROSKA_TRACK_VIDEO
:
781 mp_msg(MSGT_DEMUX
, MSGL_V
, "Video\n");
783 case MATROSKA_TRACK_SUBTITLE
:
784 mp_msg(MSGT_DEMUX
, MSGL_V
, "Subtitle\n");
787 mp_msg(MSGT_DEMUX
, MSGL_V
, "unknown\n");
792 case MATROSKA_ID_AUDIO
:
793 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Audio track\n");
794 l
= demux_mkv_read_trackaudio(demuxer
, track
);
799 case MATROSKA_ID_VIDEO
:
800 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Video track\n");
801 l
= demux_mkv_read_trackvideo(demuxer
, track
);
806 case MATROSKA_ID_CODECID
:
807 track
->codec_id
= ebml_read_ascii(s
, &l
);
808 if (track
->codec_id
== NULL
)
810 if (!strcmp(track
->codec_id
, MKV_V_MSCOMP
)
811 || !strcmp(track
->codec_id
, MKV_A_ACM
))
812 track
->ms_compat
= 1;
813 else if (!strcmp(track
->codec_id
, MKV_S_VOBSUB
))
814 track
->subtitle_type
= MATROSKA_SUBTYPE_VOBSUB
;
815 else if (!strcmp(track
->codec_id
, MKV_S_TEXTSSA
)
816 || !strcmp(track
->codec_id
, MKV_S_TEXTASS
)
817 || !strcmp(track
->codec_id
, MKV_S_SSA
)
818 || !strcmp(track
->codec_id
, MKV_S_ASS
)) {
819 track
->subtitle_type
= MATROSKA_SUBTYPE_SSA
;
820 } else if (!strcmp(track
->codec_id
, MKV_S_TEXTASCII
))
821 track
->subtitle_type
= MATROSKA_SUBTYPE_TEXT
;
822 if (!strcmp(track
->codec_id
, MKV_S_TEXTUTF8
)) {
823 track
->subtitle_type
= MATROSKA_SUBTYPE_TEXT
;
825 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Codec ID: %s\n",
829 case MATROSKA_ID_CODECPRIVATE
:;
831 num
= ebml_read_length(s
, &x
);
832 // audit: cheap guard against overflows later..
833 if (num
> SIZE_MAX
- 1000)
836 track
->private_data
= malloc(num
+ AV_LZO_INPUT_PADDING
);
837 if (stream_read(s
, track
->private_data
, num
) != (int) num
)
839 track
->private_size
= num
;
840 mp_msg(MSGT_DEMUX
, MSGL_V
,
841 "[mkv] | + CodecPrivate, length " "%u\n",
842 track
->private_size
);
845 case MATROSKA_ID_LANGUAGE
:
846 free(track
->language
);
847 track
->language
= ebml_read_utf8(s
, &l
);
848 if (track
->language
== NULL
)
850 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Language: %s\n",
854 case MATROSKA_ID_FLAGDEFAULT
:
855 num
= ebml_read_uint(s
, &l
);
856 if (num
== EBML_UINT_INVALID
)
858 track
->default_track
= num
;
859 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + Default flag: %u\n",
860 track
->default_track
);
863 case MATROSKA_ID_DEFAULTDURATION
:
864 num
= ebml_read_uint(s
, &l
);
865 if (num
== EBML_UINT_INVALID
)
868 mp_msg(MSGT_DEMUX
, MSGL_V
,
869 "[mkv] | + Default duration: 0");
871 track
->v_frate
= 1000000000.0 / num
;
872 track
->default_duration
= num
/ 1000000000.0;
873 mp_msg(MSGT_DEMUX
, MSGL_V
,
874 "[mkv] | + Default duration: "
875 "%.3fms ( = %.3f fps)\n", num
/ 1000000.0,
880 case MATROSKA_ID_CONTENTENCODINGS
:
881 l
= demux_mkv_read_trackencodings(demuxer
, track
);
887 ebml_read_skip(s
, &l
);
893 mkv_d
->tracks
[mkv_d
->num_tracks
++] = track
;
897 demux_mkv_free_trackentry(track
);
901 static int demux_mkv_read_tracks(demuxer_t
*demuxer
)
903 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
904 stream_t
*s
= demuxer
->stream
;
908 mkv_d
->tracks
= malloc(sizeof(*mkv_d
->tracks
));
909 mkv_d
->num_tracks
= 0;
911 length
= ebml_read_length(s
, NULL
);
913 switch (ebml_read_id(s
, &il
)) {
914 case MATROSKA_ID_TRACKENTRY
:
915 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] | + a track...\n");
916 mkv_d
->tracks
= realloc(mkv_d
->tracks
, (mkv_d
->num_tracks
+ 1)
917 * sizeof(*mkv_d
->tracks
));
918 l
= demux_mkv_read_trackentry(demuxer
);
924 ebml_read_skip(s
, &l
);
932 static int demux_mkv_read_cues(demuxer_t
*demuxer
)
934 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
935 stream_t
*s
= demuxer
->stream
;
937 if (index_mode
== 0 || index_mode
== 2) {
938 ebml_read_skip(s
, NULL
);
942 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing cues ] -----------\n");
943 struct ebml_cues cues
= {};
944 struct ebml_parse_ctx parse_ctx
= {};
945 if (ebml_read_element(s
, &parse_ctx
, &cues
, &ebml_cues_desc
) < 0)
947 for (int i
= 0; i
< cues
.n_cue_point
; i
++) {
948 struct ebml_cue_point
*cuepoint
= &cues
.cue_point
[i
];
949 if (cuepoint
->n_cue_time
!= 1 || !cuepoint
->n_cue_track_positions
) {
950 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Malformed CuePoint element\n");
953 uint64_t time
= cuepoint
->cue_time
;
954 for (int i
= 0; i
< cuepoint
->n_cue_track_positions
; i
++) {
955 struct ebml_cue_track_positions
*trackpos
=
956 &cuepoint
->cue_track_positions
[i
];
957 uint64_t track
= trackpos
->cue_track
;
958 uint64_t pos
= trackpos
->cue_cluster_position
;
960 grow_array(mkv_d
->indexes
, mkv_d
->num_indexes
,
961 sizeof(mkv_index_t
));
962 mkv_d
->indexes
[mkv_d
->num_indexes
].tnum
= track
;
963 mkv_d
->indexes
[mkv_d
->num_indexes
].timecode
= time
;
964 mkv_d
->indexes
[mkv_d
->num_indexes
].filepos
=
965 mkv_d
->segment_start
+ pos
;
966 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,
967 "[mkv] |+ found cue point for track %" PRIu64
968 ": timecode %" PRIu64
", filepos: %" PRIu64
"\n", track
,
969 time
, mkv_d
->segment_start
+ pos
);
970 mkv_d
->num_indexes
++;
975 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] \\---- [ parsing cues ] -----------\n");
976 talloc_free(parse_ctx
.talloc_ctx
);
980 static int demux_mkv_read_chapters(struct demuxer
*demuxer
)
982 struct MPOpts
*opts
= demuxer
->opts
;
983 stream_t
*s
= demuxer
->stream
;
985 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing chapters ] ---------\n");
986 struct ebml_chapters file_chapters
= {};
987 struct ebml_parse_ctx parse_ctx
= {};
988 if (ebml_read_element(s
, &parse_ctx
, &file_chapters
,
989 &ebml_chapters_desc
) < 0)
992 int selected_edition
= 0;
993 int num_editions
= file_chapters
.n_edition_entry
;
994 struct ebml_edition_entry
*editions
= file_chapters
.edition_entry
;
995 if (opts
->edition_id
>= 0 && opts
->edition_id
< num_editions
) {
996 selected_edition
= opts
->edition_id
;
997 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] User-specified edition: %d\n",
1000 for (int i
= 0; i
< num_editions
; i
++)
1001 if (editions
[i
].edition_flag_default
) {
1002 selected_edition
= i
;
1003 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Default edition: %d\n", i
);
1006 struct matroska_chapter
*m_chapters
= NULL
;
1007 if (editions
[selected_edition
].edition_flag_ordered
) {
1008 int count
= editions
[selected_edition
].n_chapter_atom
;
1009 m_chapters
= talloc_array_ptrtype(demuxer
, m_chapters
, count
);
1010 demuxer
->matroska_data
.ordered_chapters
= m_chapters
;
1011 demuxer
->matroska_data
.num_ordered_chapters
= count
;
1014 for (int idx
= 0; idx
< num_editions
; idx
++) {
1015 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] New edition %d\n", idx
);
1016 int warn_level
= idx
== selected_edition
? MSGL_WARN
: MSGL_V
;
1017 if (editions
[idx
].n_edition_flag_default
)
1018 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Default edition flag: %"PRIu64
1019 "\n", editions
[idx
].edition_flag_default
);
1020 if (editions
[idx
].n_edition_flag_ordered
)
1021 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Ordered chapter flag: %"PRIu64
1022 "\n", editions
[idx
].edition_flag_ordered
);
1023 for (int i
= 0; i
< editions
[idx
].n_chapter_atom
; i
++) {
1024 struct ebml_chapter_atom
*ca
= editions
[idx
].chapter_atom
+ i
;
1025 struct matroska_chapter chapter
= { };
1026 struct bstr name
= { "(unnamed)", 9 };
1028 if (!ca
->n_chapter_time_start
)
1029 mp_msg(MSGT_DEMUX
, warn_level
,
1030 "[mkv] Chapter lacks start time\n");
1031 chapter
.start
= ca
->chapter_time_start
/ 1000000;
1032 chapter
.end
= ca
->chapter_time_end
/ 1000000;
1034 if (ca
->n_chapter_display
) {
1035 if (ca
->n_chapter_display
> 1)
1036 mp_msg(MSGT_DEMUX
, warn_level
, "[mkv] Multiple chapter "
1037 "names not supported, picking first\n");
1038 if (!ca
->chapter_display
[0].n_chap_string
)
1039 mp_msg(MSGT_DEMUX
, warn_level
, "[mkv] Malformed chapter "
1042 name
= ca
->chapter_display
[0].chap_string
;
1045 if (ca
->n_chapter_segment_uid
) {
1046 chapter
.has_segment_uid
= true;
1047 int len
= ca
->chapter_segment_uid
.len
;
1048 if (len
!= sizeof(chapter
.segment_uid
))
1049 mp_msg(MSGT_DEMUX
, warn_level
,
1050 "[mkv] Chapter segment uid bad length %d\n", len
);
1051 else if (ca
->n_chapter_segment_edition_uid
) {
1052 mp_tmsg(MSGT_DEMUX
, warn_level
, "[mkv] Warning: "
1053 "unsupported edition recursion in chapter; "
1054 "will skip on playback!\n");
1056 memcpy(chapter
.segment_uid
, ca
->chapter_segment_uid
.start
,
1058 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Chapter segment uid ");
1059 for (int i
= 0; i
< len
; i
++)
1060 mp_msg(MSGT_DEMUX
, MSGL_V
, "%02x ",
1061 chapter
.segment_uid
[i
]);
1062 mp_msg(MSGT_DEMUX
, MSGL_V
, "\n");
1066 mp_msg(MSGT_DEMUX
, MSGL_V
,
1067 "[mkv] Chapter %u from %02d:%02d:%02d.%03d "
1068 "to %02d:%02d:%02d.%03d, %.*s\n", idx
,
1069 (int) (chapter
.start
/ 60 / 60 / 1000),
1070 (int) ((chapter
.start
/ 60 / 1000) % 60),
1071 (int) ((chapter
.start
/ 1000) % 60),
1072 (int) (chapter
.start
% 1000),
1073 (int) (chapter
.end
/ 60 / 60 / 1000),
1074 (int) ((chapter
.end
/ 60 / 1000) % 60),
1075 (int) ((chapter
.end
/ 1000) % 60),
1076 (int) (chapter
.end
% 1000),
1077 name
.len
, name
.start
);
1079 if (idx
== selected_edition
){
1080 demuxer_add_chapter(demuxer
, name
.start
, name
.len
,
1081 chapter
.start
, chapter
.end
);
1082 if (editions
[idx
].edition_flag_ordered
) {
1083 chapter
.name
= talloc_strndup(m_chapters
, name
.start
,
1085 m_chapters
[i
] = chapter
;
1090 if (num_editions
> 1)
1091 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
1092 "[mkv] Found %d editions, will play #%d (first is 0).\n",
1093 num_editions
, selected_edition
);
1096 talloc_free(parse_ctx
.talloc_ctx
);
1097 mp_msg(MSGT_DEMUX
, MSGL_V
,
1098 "[mkv] \\---- [ parsing chapters ] ---------\n");
1102 static int demux_mkv_read_tags(demuxer_t
*demuxer
)
1104 ebml_read_skip(demuxer
->stream
, NULL
);
1108 static int demux_mkv_read_attachments(demuxer_t
*demuxer
)
1110 stream_t
*s
= demuxer
->stream
;
1112 mp_msg(MSGT_DEMUX
, MSGL_V
,
1113 "[mkv] /---- [ parsing attachments ] ---------\n");
1115 struct ebml_attachments attachments
= {};
1116 struct ebml_parse_ctx parse_ctx
= {};
1117 if (ebml_read_element(s
, &parse_ctx
, &attachments
,
1118 &ebml_attachments_desc
) < 0)
1121 for (int i
= 0; i
< attachments
.n_attached_file
; i
++) {
1122 struct ebml_attached_file
*attachment
= &attachments
.attached_file
[i
];
1123 if (!attachment
->n_file_name
|| !attachment
->n_file_mime_type
1124 || !attachment
->n_file_data
) {
1125 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Malformed attachment\n");
1128 struct bstr name
= attachment
->file_name
;
1129 struct bstr mime
= attachment
->file_mime_type
;
1130 char *data
= attachment
->file_data
.start
;
1131 int data_size
= attachment
->file_data
.len
;
1132 demuxer_add_attachment(demuxer
, name
.start
, name
.len
, mime
.start
,
1133 mime
.len
, data
, data_size
);
1134 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Attachment: %.*s, %.*s, %u bytes\n",
1135 name
.len
, name
.start
, mime
.len
, mime
.start
, data_size
);
1139 talloc_free(parse_ctx
.talloc_ctx
);
1140 mp_msg(MSGT_DEMUX
, MSGL_V
,
1141 "[mkv] \\---- [ parsing attachments ] ---------\n");
1145 static int read_header_element(struct demuxer
*demuxer
, uint32_t id
,
1148 static int demux_mkv_read_seekhead(demuxer_t
*demuxer
)
1150 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
1151 struct stream
*s
= demuxer
->stream
;
1153 struct ebml_seek_head seekhead
= {};
1154 struct ebml_parse_ctx parse_ctx
= {};
1156 mp_msg(MSGT_DEMUX
, MSGL_V
,
1157 "[mkv] /---- [ parsing seek head ] ---------\n");
1158 if (ebml_read_element(s
, &parse_ctx
, &seekhead
, &ebml_seek_head_desc
) < 0) {
1162 /* off now holds the position of the next element after the seek head. */
1163 off_t off
= stream_tell(s
);
1164 for (int i
= 0; i
< seekhead
.n_seek
; i
++) {
1165 struct ebml_seek
*seek
= &seekhead
.seek
[i
];
1166 if (seek
->n_seek_id
!= 1 || seek
->n_seek_position
!= 1) {
1167 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Invalid SeekHead entry\n");
1170 uint64_t pos
= seek
->seek_position
+ mkv_d
->segment_start
;
1171 if (pos
>= demuxer
->movi_end
) {
1172 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] SeekHead position beyond "
1173 "end of file - incomplete file?\n");
1176 read_header_element(demuxer
, seek
->seek_id
, pos
);
1178 if (!stream_seek(s
, off
)) {
1179 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "[mkv] Couldn't seek back after "
1184 mp_msg(MSGT_DEMUX
, MSGL_V
,
1185 "[mkv] \\---- [ parsing seek head ] ---------\n");
1186 talloc_free(parse_ctx
.talloc_ctx
);
1190 static bool seek_pos_id(struct stream
*s
, off_t pos
, uint32_t id
)
1192 if (!stream_seek(s
, pos
)) {
1193 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Failed to seek in file\n");
1196 if (ebml_read_id(s
, NULL
) != id
) {
1197 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Expected element not found\n");
1203 static int read_header_element(struct demuxer
*demuxer
, uint32_t id
,
1206 struct mkv_demuxer
*mkv_d
= demuxer
->priv
;
1207 stream_t
*s
= demuxer
->stream
;
1208 off_t pos
= stream_tell(s
) - 4;
1211 case MATROSKA_ID_INFO
:
1212 if (mkv_d
->parsed_info
)
1214 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1216 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] |+ segment information...\n");
1217 mkv_d
->parsed_info
= true;
1218 return demux_mkv_read_info(demuxer
) ? -1 : 1;
1220 case MATROSKA_ID_TRACKS
:
1221 if (mkv_d
->parsed_tracks
)
1223 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1225 mkv_d
->parsed_tracks
= true;
1226 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] |+ segment tracks...\n");
1227 return demux_mkv_read_tracks(demuxer
) ? -1 : 1;
1229 case MATROSKA_ID_CUES
:
1230 if (is_parsed_header(mkv_d
, pos
))
1232 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1234 return demux_mkv_read_cues(demuxer
) ? -1 : 1;
1236 case MATROSKA_ID_TAGS
:
1237 if (mkv_d
->parsed_tags
)
1239 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1241 mkv_d
->parsed_tags
= true;
1242 return demux_mkv_read_tags(demuxer
) ? -1 : 1;
1244 case MATROSKA_ID_SEEKHEAD
:
1245 if (is_parsed_header(mkv_d
, pos
))
1247 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1249 return demux_mkv_read_seekhead(demuxer
) ? -1 : 1;
1251 case MATROSKA_ID_CHAPTERS
:
1252 if (mkv_d
->parsed_chapters
)
1254 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1256 mkv_d
->parsed_chapters
= true;
1257 return demux_mkv_read_chapters(demuxer
) ? -1 : 1;
1259 case MATROSKA_ID_ATTACHMENTS
:
1260 if (mkv_d
->parsed_attachments
)
1262 if (at_filepos
&& !seek_pos_id(s
, at_filepos
, id
))
1264 mkv_d
->parsed_attachments
= true;
1265 return demux_mkv_read_attachments(demuxer
) ? -1 : 1;
1269 ebml_read_skip(s
, NULL
);
1273 ebml_read_skip(s
, NULL
);
1279 static int demux_mkv_open_video(demuxer_t
*demuxer
, mkv_track_t
*track
,
1281 static int demux_mkv_open_audio(demuxer_t
*demuxer
, mkv_track_t
*track
,
1283 static int demux_mkv_open_sub(demuxer_t
*demuxer
, mkv_track_t
*track
,
1286 static void display_create_tracks(demuxer_t
*demuxer
)
1288 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1289 int i
, vid
= 0, aid
= 0, sid
= 0;
1291 for (i
= 0; i
< mkv_d
->num_tracks
; i
++) {
1292 char *type
= "unknown", str
[32];
1294 switch (mkv_d
->tracks
[i
]->type
) {
1295 case MATROSKA_TRACK_VIDEO
:
1297 demux_mkv_open_video(demuxer
, mkv_d
->tracks
[i
], vid
);
1298 if (mkv_d
->tracks
[i
]->name
)
1299 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n", vid
,
1300 mkv_d
->tracks
[i
]->name
);
1301 sprintf(str
, "-vid %u", vid
++);
1303 case MATROSKA_TRACK_AUDIO
:
1305 demux_mkv_open_audio(demuxer
, mkv_d
->tracks
[i
], aid
);
1306 if (mkv_d
->tracks
[i
]->name
)
1307 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n", aid
,
1308 mkv_d
->tracks
[i
]->name
);
1309 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", aid
,
1310 mkv_d
->tracks
[i
]->language
);
1311 sprintf(str
, "-aid %u, -alang %.5s", aid
++,
1312 mkv_d
->tracks
[i
]->language
);
1314 case MATROSKA_TRACK_SUBTITLE
:
1316 demux_mkv_open_sub(demuxer
, mkv_d
->tracks
[i
], sid
);
1317 if (mkv_d
->tracks
[i
]->name
)
1318 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n", sid
,
1319 mkv_d
->tracks
[i
]->name
);
1320 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sid
,
1321 mkv_d
->tracks
[i
]->language
);
1322 sprintf(str
, "-sid %u, -slang %.5s", sid
++,
1323 mkv_d
->tracks
[i
]->language
);
1326 if (mkv_d
->tracks
[i
]->name
)
1327 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
,
1328 "[mkv] Track ID %u: %s (%s) \"%s\", %s\n",
1329 mkv_d
->tracks
[i
]->tnum
, type
, mkv_d
->tracks
[i
]->codec_id
,
1330 mkv_d
->tracks
[i
]->name
, str
);
1332 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] Track ID %u: %s (%s), %s\n",
1333 mkv_d
->tracks
[i
]->tnum
, type
, mkv_d
->tracks
[i
]->codec_id
,
1342 } videocodec_info_t
;
1344 static const videocodec_info_t vinfo
[] = {
1345 {MKV_V_MPEG1
, mmioFOURCC('m', 'p', 'g', '1'), 0},
1346 {MKV_V_MPEG2
, mmioFOURCC('m', 'p', 'g', '2'), 0},
1347 {MKV_V_MPEG4_SP
, mmioFOURCC('m', 'p', '4', 'v'), 1},
1348 {MKV_V_MPEG4_ASP
, mmioFOURCC('m', 'p', '4', 'v'), 1},
1349 {MKV_V_MPEG4_AP
, mmioFOURCC('m', 'p', '4', 'v'), 1},
1350 {MKV_V_MPEG4_AVC
, mmioFOURCC('a', 'v', 'c', '1'), 1},
1351 {MKV_V_THEORA
, mmioFOURCC('t', 'h', 'e', 'o'), 1},
1355 static int demux_mkv_open_video(demuxer_t
*demuxer
, mkv_track_t
*track
,
1358 struct MPOpts
*opts
= demuxer
->opts
;
1359 BITMAPINFOHEADER
*bih
;
1360 void *ImageDesc
= NULL
;
1363 if (track
->ms_compat
) { /* MS compatibility mode */
1364 BITMAPINFOHEADER
*src
;
1366 if (track
->private_data
== NULL
1367 || track
->private_size
< sizeof(BITMAPINFOHEADER
))
1370 src
= (BITMAPINFOHEADER
*) track
->private_data
;
1371 bih
= calloc(1, track
->private_size
);
1372 bih
->biSize
= le2me_32(src
->biSize
);
1373 bih
->biWidth
= le2me_32(src
->biWidth
);
1374 bih
->biHeight
= le2me_32(src
->biHeight
);
1375 bih
->biPlanes
= le2me_16(src
->biPlanes
);
1376 bih
->biBitCount
= le2me_16(src
->biBitCount
);
1377 bih
->biCompression
= le2me_32(src
->biCompression
);
1378 bih
->biSizeImage
= le2me_32(src
->biSizeImage
);
1379 bih
->biXPelsPerMeter
= le2me_32(src
->biXPelsPerMeter
);
1380 bih
->biYPelsPerMeter
= le2me_32(src
->biYPelsPerMeter
);
1381 bih
->biClrUsed
= le2me_32(src
->biClrUsed
);
1382 bih
->biClrImportant
= le2me_32(src
->biClrImportant
);
1383 memcpy((char *) bih
+ sizeof(BITMAPINFOHEADER
),
1384 (char *) src
+ sizeof(BITMAPINFOHEADER
),
1385 track
->private_size
- sizeof(BITMAPINFOHEADER
));
1387 if (track
->v_width
== 0)
1388 track
->v_width
= bih
->biWidth
;
1389 if (track
->v_height
== 0)
1390 track
->v_height
= bih
->biHeight
;
1392 bih
= calloc(1, sizeof(BITMAPINFOHEADER
));
1393 bih
->biSize
= sizeof(BITMAPINFOHEADER
);
1394 bih
->biWidth
= track
->v_width
;
1395 bih
->biHeight
= track
->v_height
;
1396 bih
->biBitCount
= 24;
1397 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/ 8;
1399 if (track
->private_size
>= RVPROPERTIES_SIZE
1400 && (!strcmp(track
->codec_id
, MKV_V_REALV10
)
1401 || !strcmp(track
->codec_id
, MKV_V_REALV20
)
1402 || !strcmp(track
->codec_id
, MKV_V_REALV30
)
1403 || !strcmp(track
->codec_id
, MKV_V_REALV40
))) {
1404 unsigned char *dst
, *src
;
1408 src
= (uint8_t *) track
->private_data
+ RVPROPERTIES_SIZE
;
1410 cnt
= track
->private_size
- RVPROPERTIES_SIZE
;
1411 bih
= realloc(bih
, sizeof(BITMAPINFOHEADER
) + 8 + cnt
);
1412 bih
->biSize
= 48 + cnt
;
1414 type2
= AV_RB32(src
- 4);
1415 if (type2
== 0x10003000 || type2
== 0x10003001)
1416 bih
->biCompression
= mmioFOURCC('R', 'V', '1', '3');
1418 bih
->biCompression
=
1419 mmioFOURCC('R', 'V', track
->codec_id
[9], '0');
1420 dst
= (unsigned char *) (bih
+ 1);
1421 // copy type1 and type2 info from rv properties
1422 memcpy(dst
, src
- 8, 8);
1423 stream_read(demuxer
->stream
, dst
+ 8, cnt
);
1424 track
->realmedia
= 1;
1426 #ifdef CONFIG_QTX_CODECS
1427 } else if (track
->private_size
>= sizeof(ImageDescription
)
1428 && !strcmp(track
->codec_id
, MKV_V_QUICKTIME
)) {
1429 ImageDescriptionPtr idesc
;
1431 idesc
= (ImageDescriptionPtr
) track
->private_data
;
1432 idesc
->idSize
= be2me_32(idesc
->idSize
);
1433 idesc
->cType
= be2me_32(idesc
->cType
);
1434 idesc
->version
= be2me_16(idesc
->version
);
1435 idesc
->revisionLevel
= be2me_16(idesc
->revisionLevel
);
1436 idesc
->vendor
= be2me_32(idesc
->vendor
);
1437 idesc
->temporalQuality
= be2me_32(idesc
->temporalQuality
);
1438 idesc
->spatialQuality
= be2me_32(idesc
->spatialQuality
);
1439 idesc
->width
= be2me_16(idesc
->width
);
1440 idesc
->height
= be2me_16(idesc
->height
);
1441 idesc
->hRes
= be2me_32(idesc
->hRes
);
1442 idesc
->vRes
= be2me_32(idesc
->vRes
);
1443 idesc
->dataSize
= be2me_32(idesc
->dataSize
);
1444 idesc
->frameCount
= be2me_16(idesc
->frameCount
);
1445 idesc
->depth
= be2me_16(idesc
->depth
);
1446 idesc
->clutID
= be2me_16(idesc
->clutID
);
1448 bih
->biCompression
= idesc
->cType
;
1450 #endif /* CONFIG_QTX_CODECS */
1453 const videocodec_info_t
*vi
= vinfo
;
1454 while (vi
->id
&& strcmp(vi
->id
, track
->codec_id
))
1456 bih
->biCompression
= vi
->fourcc
;
1457 if (vi
->extradata
&& track
->private_data
1458 && (track
->private_size
> 0)) {
1459 bih
->biSize
+= track
->private_size
;
1460 bih
= realloc(bih
, bih
->biSize
);
1461 memcpy(bih
+ 1, track
->private_data
, track
->private_size
);
1463 track
->reorder_timecodes
= opts
->user_correct_pts
== 0;
1465 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Unknown/unsupported "
1466 "CodecID (%s) or missing/bad CodecPrivate\n"
1467 "[mkv] data (track %u).\n",
1468 track
->codec_id
, track
->tnum
);
1475 sh_v
= new_sh_video_vid(demuxer
, track
->tnum
, vid
);
1477 sh_v
->format
= sh_v
->bih
->biCompression
;
1478 if (track
->v_frate
== 0.0)
1479 track
->v_frate
= 25.0;
1480 sh_v
->fps
= track
->v_frate
;
1481 sh_v
->frametime
= 1 / track
->v_frate
;
1483 if (!track
->realmedia
) {
1484 sh_v
->disp_w
= track
->v_width
;
1485 sh_v
->disp_h
= track
->v_height
;
1486 if (track
->v_dheight
)
1487 sh_v
->aspect
= (double) track
->v_dwidth
/ track
->v_dheight
;
1489 // vd_realvid.c will set aspect to disp_w/disp_h and rederive
1490 // disp_w and disp_h from the RealVideo stream contents returned
1491 // by the Real DLLs. If DisplayWidth/DisplayHeight was not set in
1492 // the Matroska file then it has already been set to PixelWidth/Height
1493 // by check_track_information.
1494 sh_v
->disp_w
= track
->v_dwidth
;
1495 sh_v
->disp_h
= track
->v_dheight
;
1497 sh_v
->ImageDesc
= ImageDesc
;
1498 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Aspect: %f\n", sh_v
->aspect
);
1500 sh_v
->ds
= demuxer
->video
;
1504 static int demux_mkv_open_audio(demuxer_t
*demuxer
, mkv_track_t
*track
,
1507 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1508 sh_audio_t
*sh_a
= new_sh_audio_aid(demuxer
, track
->tnum
, aid
);
1511 mkv_d
->audio_tracks
[mkv_d
->last_aid
] = track
->tnum
;
1513 if (track
->language
&& (strcmp(track
->language
, "und") != 0))
1514 sh_a
->lang
= strdup(track
->language
);
1515 sh_a
->default_track
= track
->default_track
;
1516 sh_a
->ds
= demuxer
->audio
;
1517 sh_a
->wf
= malloc(sizeof(WAVEFORMATEX
));
1518 if (track
->ms_compat
&& (track
->private_size
>= sizeof(WAVEFORMATEX
))) {
1519 WAVEFORMATEX
*wf
= (WAVEFORMATEX
*) track
->private_data
;
1520 sh_a
->wf
= realloc(sh_a
->wf
, track
->private_size
);
1521 sh_a
->wf
->wFormatTag
= le2me_16(wf
->wFormatTag
);
1522 sh_a
->wf
->nChannels
= le2me_16(wf
->nChannels
);
1523 sh_a
->wf
->nSamplesPerSec
= le2me_32(wf
->nSamplesPerSec
);
1524 sh_a
->wf
->nAvgBytesPerSec
= le2me_32(wf
->nAvgBytesPerSec
);
1525 sh_a
->wf
->nBlockAlign
= le2me_16(wf
->nBlockAlign
);
1526 sh_a
->wf
->wBitsPerSample
= le2me_16(wf
->wBitsPerSample
);
1527 sh_a
->wf
->cbSize
= track
->private_size
- sizeof(WAVEFORMATEX
);
1528 memcpy(sh_a
->wf
+ 1, wf
+ 1,
1529 track
->private_size
- sizeof(WAVEFORMATEX
));
1530 if (track
->a_sfreq
== 0.0)
1531 track
->a_sfreq
= sh_a
->wf
->nSamplesPerSec
;
1532 if (track
->a_channels
== 0)
1533 track
->a_channels
= sh_a
->wf
->nChannels
;
1534 if (track
->a_bps
== 0)
1535 track
->a_bps
= sh_a
->wf
->wBitsPerSample
;
1536 track
->a_formattag
= sh_a
->wf
->wFormatTag
;
1538 memset(sh_a
->wf
, 0, sizeof(WAVEFORMATEX
));
1539 if (!strcmp(track
->codec_id
, MKV_A_MP3
)
1540 || !strcmp(track
->codec_id
, MKV_A_MP2
))
1541 track
->a_formattag
= 0x0055;
1542 else if (!strncmp(track
->codec_id
, MKV_A_AC3
, strlen(MKV_A_AC3
)))
1543 track
->a_formattag
= 0x2000;
1544 else if (!strcmp(track
->codec_id
, MKV_A_DTS
))
1545 track
->a_formattag
= 0x2001;
1546 else if (!strcmp(track
->codec_id
, MKV_A_PCM
)
1547 || !strcmp(track
->codec_id
, MKV_A_PCM_BE
))
1548 track
->a_formattag
= 0x0001;
1549 else if (!strcmp(track
->codec_id
, MKV_A_AAC_2MAIN
)
1550 || !strncmp(track
->codec_id
, MKV_A_AAC_2LC
,
1551 strlen(MKV_A_AAC_2LC
))
1552 || !strcmp(track
->codec_id
, MKV_A_AAC_2SSR
)
1553 || !strcmp(track
->codec_id
, MKV_A_AAC_4MAIN
)
1554 || !strncmp(track
->codec_id
, MKV_A_AAC_4LC
,
1555 strlen(MKV_A_AAC_4LC
))
1556 || !strcmp(track
->codec_id
, MKV_A_AAC_4SSR
)
1557 || !strcmp(track
->codec_id
, MKV_A_AAC_4LTP
)
1558 || !strcmp(track
->codec_id
, MKV_A_AAC
))
1559 track
->a_formattag
= mmioFOURCC('M', 'P', '4', 'A');
1560 else if (!strcmp(track
->codec_id
, MKV_A_VORBIS
)) {
1561 if (track
->private_data
== NULL
)
1563 track
->a_formattag
= mmioFOURCC('v', 'r', 'b', 's');
1564 } else if (!strcmp(track
->codec_id
, MKV_A_QDMC
))
1565 track
->a_formattag
= mmioFOURCC('Q', 'D', 'M', 'C');
1566 else if (!strcmp(track
->codec_id
, MKV_A_QDMC2
))
1567 track
->a_formattag
= mmioFOURCC('Q', 'D', 'M', '2');
1568 else if (!strcmp(track
->codec_id
, MKV_A_WAVPACK
))
1569 track
->a_formattag
= mmioFOURCC('W', 'V', 'P', 'K');
1570 else if (!strcmp(track
->codec_id
, MKV_A_TRUEHD
))
1571 track
->a_formattag
= mmioFOURCC('T', 'R', 'H', 'D');
1572 else if (!strcmp(track
->codec_id
, MKV_A_FLAC
)) {
1573 if (track
->private_data
== NULL
|| track
->private_size
== 0) {
1574 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
,
1575 "[mkv] FLAC track does not contain valid headers.\n");
1578 track
->a_formattag
= mmioFOURCC('f', 'L', 'a', 'C');
1579 } else if (track
->private_size
>= RAPROPERTIES4_SIZE
) {
1580 if (!strcmp(track
->codec_id
, MKV_A_REAL28
))
1581 track
->a_formattag
= mmioFOURCC('2', '8', '_', '8');
1582 else if (!strcmp(track
->codec_id
, MKV_A_REALATRC
))
1583 track
->a_formattag
= mmioFOURCC('a', 't', 'r', 'c');
1584 else if (!strcmp(track
->codec_id
, MKV_A_REALCOOK
))
1585 track
->a_formattag
= mmioFOURCC('c', 'o', 'o', 'k');
1586 else if (!strcmp(track
->codec_id
, MKV_A_REALDNET
))
1587 track
->a_formattag
= mmioFOURCC('d', 'n', 'e', 't');
1588 else if (!strcmp(track
->codec_id
, MKV_A_REALSIPR
))
1589 track
->a_formattag
= mmioFOURCC('s', 'i', 'p', 'r');
1591 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] Unknown/unsupported audio "
1592 "codec ID '%s' for track %u or missing/faulty\n[mkv] "
1593 "private codec data.\n", track
->codec_id
, track
->tnum
);
1594 free_sh_audio(demuxer
, track
->tnum
);
1599 sh_a
->format
= track
->a_formattag
;
1600 sh_a
->wf
->wFormatTag
= track
->a_formattag
;
1601 sh_a
->channels
= track
->a_channels
;
1602 sh_a
->wf
->nChannels
= track
->a_channels
;
1603 sh_a
->samplerate
= (uint32_t) track
->a_sfreq
;
1604 sh_a
->wf
->nSamplesPerSec
= (uint32_t) track
->a_sfreq
;
1605 if (track
->a_bps
== 0) {
1606 sh_a
->samplesize
= 2;
1607 sh_a
->wf
->wBitsPerSample
= 16;
1609 sh_a
->samplesize
= track
->a_bps
/ 8;
1610 sh_a
->wf
->wBitsPerSample
= track
->a_bps
;
1612 if (track
->a_formattag
== 0x0055) { /* MP3 || MP2 */
1613 sh_a
->wf
->nAvgBytesPerSec
= 16000;
1614 sh_a
->wf
->nBlockAlign
= 1152;
1615 } else if ((track
->a_formattag
== 0x2000) /* AC3 */
1616 || (track
->a_formattag
== 0x2001)) { /* DTS */
1619 } else if (track
->a_formattag
== 0x0001) { /* PCM || PCM_BE */
1620 sh_a
->wf
->nAvgBytesPerSec
= sh_a
->channels
* sh_a
->samplerate
* 2;
1621 sh_a
->wf
->nBlockAlign
= sh_a
->wf
->nAvgBytesPerSec
;
1622 if (!strcmp(track
->codec_id
, MKV_A_PCM_BE
))
1623 sh_a
->format
= mmioFOURCC('t', 'w', 'o', 's');
1624 } else if (!strcmp(track
->codec_id
, MKV_A_QDMC
)
1625 || !strcmp(track
->codec_id
, MKV_A_QDMC2
)) {
1626 sh_a
->wf
->nAvgBytesPerSec
= 16000;
1627 sh_a
->wf
->nBlockAlign
= 1486;
1628 track
->fix_i_bps
= 1;
1629 track
->qt_last_a_pts
= 0.0;
1630 if (track
->private_data
!= NULL
) {
1631 sh_a
->codecdata
= malloc(track
->private_size
);
1632 memcpy(sh_a
->codecdata
, track
->private_data
, track
->private_size
);
1633 sh_a
->codecdata_len
= track
->private_size
;
1635 } else if (track
->a_formattag
== mmioFOURCC('M', 'P', '4', 'A')) {
1636 int profile
, srate_idx
;
1638 sh_a
->wf
->nAvgBytesPerSec
= 16000;
1639 sh_a
->wf
->nBlockAlign
= 1024;
1641 if (!strcmp(track
->codec_id
, MKV_A_AAC
)
1642 && (NULL
!= track
->private_data
)) {
1643 sh_a
->codecdata
= malloc(track
->private_size
);
1644 memcpy(sh_a
->codecdata
, track
->private_data
, track
->private_size
);
1645 sh_a
->codecdata_len
= track
->private_size
;
1649 /* Recreate the 'private data' */
1650 /* which faad2 uses in its initialization */
1651 srate_idx
= aac_get_sample_rate_index(sh_a
->samplerate
);
1652 if (!strncmp(&track
->codec_id
[12], "MAIN", 4))
1654 else if (!strncmp(&track
->codec_id
[12], "LC", 2))
1656 else if (!strncmp(&track
->codec_id
[12], "SSR", 3))
1660 sh_a
->codecdata
= malloc(5);
1661 sh_a
->codecdata
[0] = ((profile
+ 1) << 3) | ((srate_idx
& 0xE) >> 1);
1662 sh_a
->codecdata
[1] =
1663 ((srate_idx
& 0x1) << 7) | (track
->a_channels
<< 3);
1665 if (strstr(track
->codec_id
, "SBR") != NULL
) {
1666 /* HE-AAC (aka SBR AAC) */
1667 sh_a
->codecdata_len
= 5;
1669 sh_a
->samplerate
*= 2;
1670 sh_a
->wf
->nSamplesPerSec
*= 2;
1671 srate_idx
= aac_get_sample_rate_index(sh_a
->samplerate
);
1672 sh_a
->codecdata
[2] = AAC_SYNC_EXTENSION_TYPE
>> 3;
1673 sh_a
->codecdata
[3] = ((AAC_SYNC_EXTENSION_TYPE
& 0x07) << 5) | 5;
1674 sh_a
->codecdata
[4] = (1 << 7) | (srate_idx
<< 3);
1675 track
->default_duration
= 1024.0 / (sh_a
->samplerate
/ 2);
1677 sh_a
->codecdata_len
= 2;
1678 track
->default_duration
= 1024.0 / sh_a
->samplerate
;
1680 } else if (track
->a_formattag
== mmioFOURCC('v', 'r', 'b', 's')) { /* VORBIS */
1681 sh_a
->wf
->cbSize
= track
->private_size
;
1682 sh_a
->wf
= realloc(sh_a
->wf
, sizeof(WAVEFORMATEX
) + sh_a
->wf
->cbSize
);
1683 memcpy((unsigned char *) (sh_a
->wf
+ 1), track
->private_data
,
1685 } else if (track
->private_size
>= RAPROPERTIES4_SIZE
1686 && !strncmp(track
->codec_id
, MKV_A_REALATRC
, 7)) {
1687 /* Common initialization for all RealAudio codecs */
1688 unsigned char *src
= track
->private_data
;
1689 int codecdata_length
, version
;
1692 sh_a
->wf
->nAvgBytesPerSec
= 0; /* FIXME !? */
1694 version
= AV_RB16(src
+ 4);
1695 flavor
= AV_RB16(src
+ 22);
1696 track
->coded_framesize
= AV_RB32(src
+ 24);
1697 track
->sub_packet_h
= AV_RB16(src
+ 40);
1698 sh_a
->wf
->nBlockAlign
= track
->audiopk_size
= AV_RB16(src
+ 42);
1699 track
->sub_packet_size
= AV_RB16(src
+ 44);
1701 src
+= RAPROPERTIES4_SIZE
;
1705 src
+= RAPROPERTIES5_SIZE
;
1710 codecdata_length
= AV_RB32(src
);
1712 sh_a
->wf
->cbSize
= codecdata_length
;
1713 sh_a
->wf
= realloc(sh_a
->wf
, sizeof(WAVEFORMATEX
) + sh_a
->wf
->cbSize
);
1714 memcpy(((char *) (sh_a
->wf
+ 1)), src
, codecdata_length
);
1716 switch (track
->a_formattag
) {
1717 case mmioFOURCC('a', 't', 'r', 'c'):
1718 sh_a
->wf
->nAvgBytesPerSec
= atrc_fl2bps
[flavor
];
1719 sh_a
->wf
->nBlockAlign
= track
->sub_packet_size
;
1721 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1722 track
->audio_timestamp
=
1723 malloc(track
->sub_packet_h
* sizeof(double));
1725 case mmioFOURCC('c', 'o', 'o', 'k'):
1726 sh_a
->wf
->nAvgBytesPerSec
= cook_fl2bps
[flavor
];
1727 sh_a
->wf
->nBlockAlign
= track
->sub_packet_size
;
1729 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1730 track
->audio_timestamp
=
1731 malloc(track
->sub_packet_h
* sizeof(double));
1733 case mmioFOURCC('s', 'i', 'p', 'r'):
1734 sh_a
->wf
->nAvgBytesPerSec
= sipr_fl2bps
[flavor
];
1735 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
1737 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1738 track
->audio_timestamp
=
1739 malloc(track
->sub_packet_h
* sizeof(double));
1741 case mmioFOURCC('2', '8', '_', '8'):
1742 sh_a
->wf
->nAvgBytesPerSec
= 3600;
1743 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
1745 malloc(track
->sub_packet_h
* track
->audiopk_size
);
1746 track
->audio_timestamp
=
1747 malloc(track
->sub_packet_h
* sizeof(double));
1751 track
->realmedia
= 1;
1752 } else if (!strcmp(track
->codec_id
, MKV_A_FLAC
)
1753 || (track
->a_formattag
== 0xf1ac)) {
1759 if (track
->a_formattag
== mmioFOURCC('f', 'L', 'a', 'C')) {
1760 ptr
= track
->private_data
;
1761 size
= track
->private_size
;
1763 sh_a
->format
= mmioFOURCC('f', 'L', 'a', 'C');
1764 ptr
= track
->private_data
+ sizeof(WAVEFORMATEX
);
1765 size
= track
->private_size
- sizeof(WAVEFORMATEX
);
1767 if (size
< 4 || ptr
[0] != 'f' || ptr
[1] != 'L' || ptr
[2] != 'a'
1769 sh_a
->codecdata
= malloc(4);
1770 sh_a
->codecdata_len
= 4;
1771 memcpy(sh_a
->codecdata
, "fLaC", 4);
1773 sh_a
->codecdata
= malloc(size
);
1774 sh_a
->codecdata_len
= size
;
1775 memcpy(sh_a
->codecdata
, ptr
, size
);
1777 } else if (track
->a_formattag
== mmioFOURCC('W', 'V', 'P', 'K') || track
->a_formattag
== mmioFOURCC('T', 'R', 'H', 'D')) { /* do nothing, still works */
1778 } else if (!track
->ms_compat
1779 || (track
->private_size
< sizeof(WAVEFORMATEX
))) {
1780 free_sh_audio(demuxer
, track
->tnum
);
1787 static int demux_mkv_open_sub(demuxer_t
*demuxer
, mkv_track_t
*track
,
1790 if (track
->subtitle_type
!= MATROSKA_SUBTYPE_UNKNOWN
) {
1793 sh_sub_t
*sh
= new_sh_sub_sid(demuxer
, track
->tnum
, sid
);
1796 if (track
->subtitle_type
== MATROSKA_SUBTYPE_VOBSUB
)
1798 if (track
->subtitle_type
== MATROSKA_SUBTYPE_SSA
)
1800 size
= track
->private_size
;
1801 m
= demux_mkv_decode(track
, track
->private_data
, &buffer
, &size
, 2);
1803 free(track
->private_data
);
1804 track
->private_data
= buffer
;
1805 track
->private_size
= size
;
1807 sh
->extradata
= malloc(track
->private_size
);
1808 memcpy(sh
->extradata
, track
->private_data
, track
->private_size
);
1809 sh
->extradata_len
= track
->private_size
;
1810 if (track
->language
&& (strcmp(track
->language
, "und") != 0))
1811 sh
->lang
= strdup(track
->language
);
1812 sh
->default_track
= track
->default_track
;
1814 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
1815 "[mkv] Subtitle type '%s' is not supported.\n",
1823 static int demux_mkv_open(demuxer_t
*demuxer
)
1825 stream_t
*s
= demuxer
->stream
;
1826 mkv_demuxer_t
*mkv_d
;
1830 stream_seek(s
, s
->start_pos
);
1831 if (ebml_read_id(s
, NULL
) != EBML_ID_EBML
)
1833 struct ebml_ebml ebml_master
= {};
1834 struct ebml_parse_ctx parse_ctx
= { .no_error_messages
= true };
1835 if (ebml_read_element(s
, &parse_ctx
, &ebml_master
, &ebml_ebml_desc
) < 0)
1837 if (ebml_master
.doc_type
.len
!= 8 || strncmp(ebml_master
.doc_type
.start
,
1839 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "[mkv] no head found\n");
1840 talloc_free(parse_ctx
.talloc_ctx
);
1843 if (ebml_master
.doc_type_read_version
> 2) {
1844 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] This looks like a Matroska file, "
1845 "but we don't support format version %"PRIu64
"\n",
1846 ebml_master
.doc_type_read_version
);
1847 talloc_free(parse_ctx
.talloc_ctx
);
1850 if ((ebml_master
.n_ebml_read_version
1851 && ebml_master
.ebml_read_version
!= EBML_VERSION
)
1852 || (ebml_master
.n_ebml_max_size_length
1853 && ebml_master
.ebml_max_size_length
> 8)
1854 || (ebml_master
.n_ebml_max_id_length
1855 && ebml_master
.ebml_max_id_length
!= 4)) {
1856 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "[mkv] This looks like a Matroska file, "
1857 "but the header has bad parameters\n");
1858 talloc_free(parse_ctx
.talloc_ctx
);
1861 talloc_free(parse_ctx
.talloc_ctx
);
1863 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] Found the head...\n");
1865 if (ebml_read_id(s
, NULL
) != MATROSKA_ID_SEGMENT
) {
1866 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] but no segment :(\n");
1869 ebml_read_length(s
, NULL
); /* return bytes number until EOF */
1871 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] + a segment...\n");
1873 mkv_d
= talloc_zero(demuxer
, struct mkv_demuxer
);
1874 demuxer
->priv
= mkv_d
;
1875 mkv_d
->tc_scale
= 1000000;
1876 mkv_d
->segment_start
= stream_tell(s
);
1879 uint32_t id
= ebml_read_id(s
, NULL
);
1881 case MATROSKA_ID_CLUSTER
:
1882 mp_msg(MSGT_DEMUX
, MSGL_V
,
1883 "[mkv] |+ found cluster, headers are "
1884 "parsed completely :)\n");
1885 stream_seek(s
, stream_tell(s
) - 4);
1890 cont
= read_header_element(demuxer
, id
, 0) < 1;
1893 ebml_read_skip(s
, NULL
);
1898 display_create_tracks(demuxer
);
1900 /* select video track */
1902 if (demuxer
->video
->id
== -1) { /* automatically select a video track */
1903 /* search for a video track that has the 'default' flag set */
1904 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1905 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_VIDEO
1906 && mkv_d
->tracks
[i
]->default_track
) {
1907 track
= mkv_d
->tracks
[i
];
1912 /* no track has the 'default' flag set */
1913 /* let's take the first video track */
1914 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1915 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_VIDEO
) {
1916 track
= mkv_d
->tracks
[i
];
1919 } else if (demuxer
->video
->id
!= -2) /* -2 = no video at all */
1921 demux_mkv_find_track_by_num(mkv_d
, demuxer
->video
->id
,
1922 MATROSKA_TRACK_VIDEO
);
1924 if (track
&& demuxer
->v_streams
[track
->tnum
]) {
1925 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] Will play video track %u.\n",
1927 demuxer
->video
->id
= track
->tnum
;
1928 demuxer
->video
->sh
= demuxer
->v_streams
[track
->tnum
];
1930 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] No video track found/wanted.\n");
1931 demuxer
->video
->id
= -2;
1934 /* select audio track */
1937 /* search for an audio track that has the 'default' flag set */
1938 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1939 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_AUDIO
1940 && mkv_d
->tracks
[i
]->default_track
) {
1941 track
= mkv_d
->tracks
[i
];
1946 /* no track has the 'default' flag set */
1947 /* let's take the first audio track */
1948 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1949 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_AUDIO
) {
1950 track
= mkv_d
->tracks
[i
];
1954 if (track
&& demuxer
->a_streams
[track
->tnum
]) {
1955 demuxer
->audio
->id
= track
->tnum
;
1956 demuxer
->audio
->sh
= demuxer
->a_streams
[track
->tnum
];
1958 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[mkv] No audio track found/wanted.\n");
1959 demuxer
->audio
->id
= -2;
1963 if (demuxer
->audio
->id
!= -2)
1964 for (i
= 0; i
< mkv_d
->num_tracks
; i
++) {
1965 if (mkv_d
->tracks
[i
]->type
!= MATROSKA_TRACK_AUDIO
)
1967 if (demuxer
->a_streams
[track
->tnum
]) {
1969 if (mkv_d
->last_aid
== MAX_A_STREAMS
)
1974 if (s
->end_pos
== 0 || (mkv_d
->indexes
== NULL
&& index_mode
< 0))
1975 demuxer
->seekable
= 0;
1977 demuxer
->movi_start
= s
->start_pos
;
1978 demuxer
->movi_end
= s
->end_pos
;
1979 demuxer
->seekable
= 1;
1982 demuxer
->accurate_seek
= true;
1984 return DEMUXER_TYPE_MATROSKA
;
1987 static void demux_close_mkv(demuxer_t
*demuxer
)
1989 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1993 free_cached_dps(demuxer
);
1994 if (mkv_d
->tracks
) {
1995 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
1996 demux_mkv_free_trackentry(mkv_d
->tracks
[i
]);
1997 free(mkv_d
->tracks
);
1999 free(mkv_d
->indexes
);
2000 free(mkv_d
->cluster_positions
);
2004 static int demux_mkv_read_block_lacing(uint8_t *buffer
, uint64_t *size
,
2006 uint32_t **all_lace_sizes
)
2008 uint32_t total
= 0, *lace_size
;
2012 *all_lace_sizes
= NULL
;
2018 switch ((flags
& 0x06) >> 1) {
2019 case 0: /* no lacing */
2021 lace_size
= calloc(*laces
, sizeof(uint32_t));
2022 lace_size
[0] = *size
;
2025 case 1: /* xiph lacing */
2026 case 2: /* fixed-size lacing */
2027 case 3: /* EBML lacing */
2031 lace_size
= calloc(*laces
, sizeof(uint32_t));
2033 switch ((flags
& 0x06) >> 1) {
2034 case 1: /* xiph lacing */
2035 for (i
= 0; i
< *laces
- 1; i
++) {
2038 lace_size
[i
] += *buffer
;
2040 } while (*buffer
++ == 0xFF);
2041 total
+= lace_size
[i
];
2043 lace_size
[i
] = *size
- total
;
2046 case 2: /* fixed-size lacing */
2047 for (i
= 0; i
< *laces
; i
++)
2048 lace_size
[i
] = *size
/ *laces
;
2051 case 3:; /* EBML lacing */
2053 uint64_t num
= ebml_read_vlen_uint(buffer
, &l
);
2054 if (num
== EBML_UINT_INVALID
) {
2061 total
= lace_size
[0] = num
;
2062 for (i
= 1; i
< *laces
- 1; i
++) {
2064 snum
= ebml_read_vlen_int(buffer
, &l
);
2065 if (snum
== EBML_INT_INVALID
) {
2071 lace_size
[i
] = lace_size
[i
- 1] + snum
;
2072 total
+= lace_size
[i
];
2074 lace_size
[i
] = *size
- total
;
2079 *all_lace_sizes
= lace_size
;
2083 static void handle_subtitles(demuxer_t
*demuxer
, mkv_track_t
*track
,
2084 char *block
, int64_t size
,
2085 uint64_t block_duration
, uint64_t timecode
)
2089 if (block_duration
== 0) {
2090 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
2091 "[mkv] Warning: No BlockDuration for subtitle track found.\n");
2096 dp
= new_demux_packet(size
);
2097 memcpy(dp
->buffer
, block
, size
);
2098 dp
->pts
= timecode
/ 1000.0;
2099 dp
->endpts
= (timecode
+ block_duration
) / 1000.0;
2100 ds_add_packet(demuxer
->sub
, dp
);
2103 double real_fix_timestamp(unsigned char *buf
, unsigned int timestamp
,
2104 unsigned int format
, int64_t *kf_base
, int *kf_pts
,
2107 static void handle_realvideo(demuxer_t
*demuxer
, mkv_track_t
*track
,
2108 uint8_t *buffer
, uint32_t size
, int block_bref
)
2110 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2112 uint32_t timestamp
= mkv_d
->last_pts
* 1000;
2114 dp
= new_demux_packet(size
);
2115 memcpy(dp
->buffer
, buffer
, size
);
2117 if (mkv_d
->v_skip_to_keyframe
) {
2118 dp
->pts
= mkv_d
->last_pts
;
2119 track
->rv_kf_base
= 0;
2120 track
->rv_kf_pts
= timestamp
;
2123 real_fix_timestamp(dp
->buffer
, timestamp
,
2124 ((sh_video_t
*) demuxer
->video
->sh
)->bih
->
2125 biCompression
, &track
->rv_kf_base
,
2126 &track
->rv_kf_pts
, NULL
);
2127 dp
->pos
= demuxer
->filepos
;
2128 dp
->flags
= block_bref
? 0 : 0x10;
2130 ds_add_packet(demuxer
->video
, dp
);
2133 static void handle_realaudio(demuxer_t
*demuxer
, mkv_track_t
*track
,
2134 uint8_t *buffer
, uint32_t size
, int block_bref
)
2136 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2137 int sps
= track
->sub_packet_size
;
2138 int sph
= track
->sub_packet_h
;
2139 int cfs
= track
->coded_framesize
;
2140 int w
= track
->audiopk_size
;
2141 int spc
= track
->sub_packet_cnt
;
2145 if ((track
->a_formattag
== mmioFOURCC('2', '8', '_', '8'))
2146 || (track
->a_formattag
== mmioFOURCC('c', 'o', 'o', 'k'))
2147 || (track
->a_formattag
== mmioFOURCC('a', 't', 'r', 'c'))
2148 || (track
->a_formattag
== mmioFOURCC('s', 'i', 'p', 'r'))) {
2150 // spc = track->sub_packet_cnt = 0;
2151 switch (track
->a_formattag
) {
2152 case mmioFOURCC('2', '8', '_', '8'):
2153 for (x
= 0; x
< sph
/ 2; x
++)
2154 memcpy(track
->audio_buf
+ x
* 2 * w
+ spc
* cfs
,
2155 buffer
+ cfs
* x
, cfs
);
2157 case mmioFOURCC('c', 'o', 'o', 'k'):
2158 case mmioFOURCC('a', 't', 'r', 'c'):
2159 for (x
= 0; x
< w
/ sps
; x
++)
2160 memcpy(track
->audio_buf
+
2161 sps
* (sph
* x
+ ((sph
+ 1) / 2) * (spc
& 1) +
2162 (spc
>> 1)), buffer
+ sps
* x
, sps
);
2164 case mmioFOURCC('s', 'i', 'p', 'r'):
2165 memcpy(track
->audio_buf
+ spc
* w
, buffer
, w
);
2166 if (spc
== sph
- 1) {
2168 int bs
= sph
* w
* 2 / 96; // nibbles per subpacket
2169 // Perform reordering
2170 for (n
= 0; n
< 38; n
++) {
2172 int i
= bs
* sipr_swaps
[n
][0];
2173 int o
= bs
* sipr_swaps
[n
][1];
2174 // swap nibbles of block 'i' with 'o' TODO: optimize
2175 for (j
= 0; j
< bs
; j
++) {
2177 (track
->audio_buf
[i
>> 1] >> 4) :
2178 (track
->audio_buf
[i
>> 1] & 0x0F);
2180 (track
->audio_buf
[o
>> 1] >> 4) :
2181 (track
->audio_buf
[o
>> 1] & 0x0F);
2183 track
->audio_buf
[o
>> 1] =
2184 (track
->audio_buf
[o
>> 1] & 0x0F) | (x
<< 4);
2186 track
->audio_buf
[o
>> 1] =
2187 (track
->audio_buf
[o
>> 1] & 0xF0) | x
;
2189 track
->audio_buf
[i
>> 1] =
2190 (track
->audio_buf
[i
>> 1] & 0x0F) | (y
<< 4);
2192 track
->audio_buf
[i
>> 1] =
2193 (track
->audio_buf
[i
>> 1] & 0xF0) | y
;
2201 track
->audio_timestamp
[track
->sub_packet_cnt
] =
2202 (track
->ra_pts
== mkv_d
->last_pts
) ? 0 : (mkv_d
->last_pts
);
2203 track
->ra_pts
= mkv_d
->last_pts
;
2204 if (track
->sub_packet_cnt
== 0)
2205 track
->audio_filepos
= demuxer
->filepos
;
2206 if (++(track
->sub_packet_cnt
) == sph
) {
2208 ((WAVEFORMATEX
*) ((sh_audio_t
*) demuxer
->audio
->sh
)->wf
)->
2210 track
->sub_packet_cnt
= 0;
2211 // Release all the audio packets
2212 for (x
= 0; x
< sph
* w
/ apk_usize
; x
++) {
2213 dp
= new_demux_packet(apk_usize
);
2214 memcpy(dp
->buffer
, track
->audio_buf
+ x
* apk_usize
,
2216 /* Put timestamp only on packets that correspond to original
2217 * audio packets in file */
2218 dp
->pts
= (x
* apk_usize
% w
) ? 0 :
2219 track
->audio_timestamp
[x
* apk_usize
/ w
];
2220 dp
->pos
= track
->audio_filepos
; // all equal
2221 dp
->flags
= x
? 0 : 0x10; // Mark first packet as keyframe
2222 ds_add_packet(demuxer
->audio
, dp
);
2225 } else { // Not a codec that require reordering
2226 dp
= new_demux_packet(size
);
2227 memcpy(dp
->buffer
, buffer
, size
);
2228 if (track
->ra_pts
== mkv_d
->last_pts
&& !mkv_d
->a_skip_to_keyframe
)
2231 dp
->pts
= mkv_d
->last_pts
;
2232 track
->ra_pts
= mkv_d
->last_pts
;
2234 dp
->pos
= demuxer
->filepos
;
2235 dp
->flags
= block_bref
? 0 : 0x10;
2236 ds_add_packet(demuxer
->audio
, dp
);
2240 /** Reorder timecodes and add cached demux packets to the queues.
2242 * Timecode reordering is needed if a video track contains B frames that
2243 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2244 * MPlayer doesn't like timestamps in display order. This function adjusts
2245 * the timestamp of cached frames (which are exactly one I/P frame followed
2246 * by one or more B frames) so that they are in coding order again.
2248 * Example: The track with 25 FPS contains four frames with the timecodes
2249 * I at 0ms, P at 120ms, B at 40ms and B at 80ms. As soon as the next I
2250 * or P frame arrives these timecodes can be changed to I at 0ms, P at 40ms,
2251 * B at 80ms and B at 120ms.
2253 * This works for simple H.264 B-frame pyramids, but not for arbitrary orders.
2255 * \param demuxer The Matroska demuxer struct for this instance.
2256 * \param track The track structure whose cache should be handled.
2258 static void flush_cached_dps(demuxer_t
*demuxer
, mkv_track_t
*track
)
2262 if (track
->num_cached_dps
== 0)
2267 for (i
= 1; i
< track
->num_cached_dps
; i
++)
2268 if (track
->cached_dps
[i
- 1]->pts
> track
->cached_dps
[i
]->pts
) {
2269 double tmp_pts
= track
->cached_dps
[i
- 1]->pts
;
2270 track
->cached_dps
[i
- 1]->pts
= track
->cached_dps
[i
]->pts
;
2271 track
->cached_dps
[i
]->pts
= tmp_pts
;
2276 for (i
= 0; i
< track
->num_cached_dps
; i
++)
2277 ds_add_packet(demuxer
->video
, track
->cached_dps
[i
]);
2278 track
->num_cached_dps
= 0;
2281 /** Cache video frames if timecodes have to be reordered.
2283 * Timecode reordering is needed if a video track contains B frames that
2284 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2285 * This function takes in a Matroska block read from the file, allocates a
2286 * demux packet for it, fills in its values, allocates space for storing
2287 * pointers to the cached demux packets and adds the packet to it. If
2288 * the packet contains an I or a P frame then ::flush_cached_dps is called
2289 * in order to send the old cached frames downstream.
2291 * \param demuxer The Matroska demuxer struct for this instance.
2292 * \param track The packet is meant for this track.
2293 * \param buffer The actual frame contents.
2294 * \param size The frame size in bytes.
2295 * \param block_bref A relative timecode (backward reference). If it is \c 0
2296 * then the frame is an I frame.
2297 * \param block_fref A relative timecode (forward reference). If it is \c 0
2298 * then the frame is either an I frame or a P frame depending on the value
2299 * of \a block_bref. Otherwise it's a B frame.
2301 static void handle_video_bframes(demuxer_t
*demuxer
, mkv_track_t
*track
,
2302 uint8_t *buffer
, uint32_t size
,
2303 int block_bref
, int block_fref
)
2305 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2308 dp
= new_demux_packet(size
);
2309 memcpy(dp
->buffer
, buffer
, size
);
2310 dp
->pos
= demuxer
->filepos
;
2311 dp
->pts
= mkv_d
->last_pts
;
2312 if ((track
->num_cached_dps
> 0) && (dp
->pts
< track
->max_pts
))
2314 if (block_fref
== 0) /* I or P frame */
2315 flush_cached_dps(demuxer
, track
);
2316 if (block_bref
!= 0) /* I frame, don't cache it */
2318 if ((track
->num_cached_dps
+ 1) > track
->num_allocated_dps
) {
2319 track
->cached_dps
= (demux_packet_t
**)
2320 realloc(track
->cached_dps
,
2321 (track
->num_cached_dps
+ 10) * sizeof(demux_packet_t
*));
2322 track
->num_allocated_dps
+= 10;
2324 track
->cached_dps
[track
->num_cached_dps
] = dp
;
2325 track
->num_cached_dps
++;
2326 if (dp
->pts
> track
->max_pts
)
2327 track
->max_pts
= dp
->pts
;
2330 static int handle_block(demuxer_t
*demuxer
, uint8_t *block
, uint64_t length
,
2331 uint64_t block_duration
, int64_t block_bref
,
2332 int64_t block_fref
, uint8_t simpleblock
)
2334 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2335 mkv_track_t
*track
= NULL
;
2336 demux_stream_t
*ds
= NULL
;
2337 uint64_t old_length
;
2339 uint32_t *lace_size
;
2340 uint8_t laces
, flags
;
2341 int i
, num
, tmp
, use_this_block
= 1;
2345 /* first byte(s): track num */
2346 num
= ebml_read_vlen_uint(block
, &tmp
);
2348 /* time (relative to cluster time) */
2349 time
= block
[0] << 8 | block
[1];
2352 old_length
= length
;
2354 if (demux_mkv_read_block_lacing(block
, &length
, &laces
, &lace_size
))
2356 block
+= old_length
- length
;
2358 tc
= (time
* mkv_d
->tc_scale
+ mkv_d
->cluster_tc
) / 1000000.0 + 0.5;
2361 current_pts
= tc
/ 1000.0;
2363 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
2364 if (mkv_d
->tracks
[i
]->tnum
== num
) {
2365 track
= mkv_d
->tracks
[i
];
2368 if (track
== NULL
) {
2372 if (num
== demuxer
->audio
->id
) {
2373 ds
= demuxer
->audio
;
2375 if (mkv_d
->a_skip_to_keyframe
) {
2377 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2379 } else if (block_bref
!= 0)
2381 } else if (mkv_d
->v_skip_to_keyframe
)
2384 if (track
->fix_i_bps
&& use_this_block
) {
2385 sh_audio_t
*sh
= (sh_audio_t
*) ds
->sh
;
2387 if (block_duration
!= 0) {
2388 sh
->i_bps
= length
* 1000 / block_duration
;
2389 track
->fix_i_bps
= 0;
2390 } else if (track
->qt_last_a_pts
== 0.0)
2391 track
->qt_last_a_pts
= current_pts
;
2392 else if (track
->qt_last_a_pts
!= current_pts
) {
2393 sh
->i_bps
= length
/ (current_pts
- track
->qt_last_a_pts
);
2394 track
->fix_i_bps
= 0;
2397 } else if (tc
< mkv_d
->skip_to_timecode
)
2399 else if (num
== demuxer
->video
->id
) {
2400 ds
= demuxer
->video
;
2401 if (mkv_d
->v_skip_to_keyframe
) {
2403 if (!(flags
& 0x80)) /*current frame isn't a keyframe */
2405 } else if (block_bref
!= 0 || block_fref
!= 0)
2408 } else if (num
== demuxer
->sub
->id
) {
2410 if (track
->subtitle_type
!= MATROSKA_SUBTYPE_VOBSUB
) {
2411 if (!mkv_d
->v_skip_to_keyframe
)
2412 handle_subtitles(demuxer
, track
, block
, length
, block_duration
,
2419 if (use_this_block
) {
2420 mkv_d
->last_pts
= current_pts
;
2421 mkv_d
->last_filepos
= demuxer
->filepos
;
2423 for (i
= 0; i
< laces
; i
++) {
2424 if (ds
== demuxer
->video
&& track
->realmedia
)
2425 handle_realvideo(demuxer
, track
, block
, lace_size
[i
],
2427 else if (ds
== demuxer
->audio
&& track
->realmedia
)
2428 handle_realaudio(demuxer
, track
, block
, lace_size
[i
],
2430 else if (ds
== demuxer
->video
&& track
->reorder_timecodes
)
2431 handle_video_bframes(demuxer
, track
, block
, lace_size
[i
],
2432 block_bref
, block_fref
);
2434 int modified
, size
= lace_size
[i
];
2437 modified
= demux_mkv_decode(track
, block
, &buffer
, &size
, 1);
2439 dp
= new_demux_packet(size
);
2440 memcpy(dp
->buffer
, buffer
, size
);
2443 dp
->flags
= (block_bref
== 0
2444 && block_fref
== 0) ? 0x10 : 0;
2445 /* If default_duration is 0, assume no pts value is known
2446 * for packets after the first one (rather than all pts
2447 * values being the same) */
2448 if (i
== 0 || track
->default_duration
)
2450 mkv_d
->last_pts
+ i
* track
->default_duration
;
2451 ds_add_packet(ds
, dp
);
2454 block
+= lace_size
[i
];
2457 if (ds
== demuxer
->video
) {
2458 mkv_d
->v_skip_to_keyframe
= 0;
2459 mkv_d
->skip_to_timecode
= 0;
2460 } else if (ds
== demuxer
->audio
)
2461 mkv_d
->a_skip_to_keyframe
= 0;
2471 static int demux_mkv_fill_buffer(demuxer_t
*demuxer
, demux_stream_t
*ds
)
2473 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2474 stream_t
*s
= demuxer
->stream
;
2479 while (mkv_d
->cluster_size
> 0) {
2480 uint64_t block_duration
= 0, block_length
= 0;
2481 int64_t block_bref
= 0, block_fref
= 0;
2482 uint8_t *block
= NULL
;
2484 while (mkv_d
->blockgroup_size
> 0) {
2485 switch (ebml_read_id(s
, &il
)) {
2486 case MATROSKA_ID_BLOCKDURATION
:
2487 block_duration
= ebml_read_uint(s
, &l
);
2488 if (block_duration
== EBML_UINT_INVALID
) {
2493 block_duration
* mkv_d
->tc_scale
/ 1000000.0 + 0.5;
2496 case MATROSKA_ID_BLOCK
:
2497 block_length
= ebml_read_length(s
, &tmp
);
2499 if (block_length
> SIZE_MAX
- AV_LZO_INPUT_PADDING
)
2501 block
= malloc(block_length
+ AV_LZO_INPUT_PADDING
);
2502 demuxer
->filepos
= stream_tell(s
);
2503 if (stream_read(s
, block
, block_length
) !=
2504 (int) block_length
) {
2508 l
= tmp
+ block_length
;
2511 case MATROSKA_ID_REFERENCEBLOCK
:;
2512 int64_t num
= ebml_read_int(s
, &l
);
2513 if (num
== EBML_INT_INVALID
) {
2523 case EBML_ID_INVALID
:
2528 ebml_read_skip(s
, &l
);
2531 mkv_d
->blockgroup_size
-= l
+ il
;
2532 mkv_d
->cluster_size
-= l
+ il
;
2536 int res
= handle_block(demuxer
, block
, block_length
,
2537 block_duration
, block_bref
, block_fref
,
2546 if (mkv_d
->cluster_size
> 0) {
2547 switch (ebml_read_id(s
, &il
)) {
2548 case MATROSKA_ID_TIMECODE
:;
2549 uint64_t num
= ebml_read_uint(s
, &l
);
2550 if (num
== EBML_UINT_INVALID
)
2552 mkv_d
->cluster_tc
= num
* mkv_d
->tc_scale
;
2553 add_cluster_position(mkv_d
, mkv_d
->cluster_start
,
2557 case MATROSKA_ID_BLOCKGROUP
:
2558 mkv_d
->blockgroup_size
= ebml_read_length(s
, &tmp
);
2562 case MATROSKA_ID_SIMPLEBLOCK
:;
2564 block_length
= ebml_read_length(s
, &tmp
);
2565 block
= malloc(block_length
);
2566 demuxer
->filepos
= stream_tell(s
);
2567 if (stream_read(s
, block
, block_length
) !=
2568 (int) block_length
) {
2572 l
= tmp
+ block_length
;
2573 res
= handle_block(demuxer
, block
, block_length
,
2574 block_duration
, block_bref
,
2577 mkv_d
->cluster_size
-= l
+ il
;
2583 mkv_d
->cluster_size
+= l
+ il
;
2586 case EBML_ID_INVALID
:
2590 ebml_read_skip(s
, &l
);
2593 mkv_d
->cluster_size
-= l
+ il
;
2597 while (ebml_read_id(s
, &il
) != MATROSKA_ID_CLUSTER
) {
2598 ebml_read_skip(s
, NULL
);
2602 mkv_d
->cluster_start
= stream_tell(s
) - il
;
2603 mkv_d
->cluster_size
= ebml_read_length(s
, NULL
);
2609 static void demux_mkv_seek(demuxer_t
*demuxer
, float rel_seek_secs
,
2610 float audio_delay
, int flags
)
2612 if (!(flags
& (SEEK_BACKWARD
| SEEK_FORWARD
))) {
2613 if (flags
& SEEK_ABSOLUTE
|| rel_seek_secs
< 0)
2614 flags
|= SEEK_BACKWARD
;
2616 flags
|= SEEK_FORWARD
;
2618 // Adjust the target a little bit to catch cases where the target position
2619 // specifies a keyframe with high, but not perfect, precision.
2620 rel_seek_secs
+= flags
& SEEK_FORWARD
? -0.005 : 0.005;
2622 free_cached_dps(demuxer
);
2623 if (!(flags
& SEEK_FACTOR
)) { /* time in secs */
2624 mkv_index_t
*index
= NULL
;
2625 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2626 stream_t
*s
= demuxer
->stream
;
2627 int64_t target_timecode
= 0, diff
, min_diff
= 0xFFFFFFFFFFFFFFFLL
;
2630 if (!(flags
& SEEK_ABSOLUTE
)) /* relative seek */
2631 target_timecode
= (int64_t) (mkv_d
->last_pts
* 1000.0);
2632 target_timecode
+= (int64_t) (rel_seek_secs
* 1000.0);
2633 if (target_timecode
< 0)
2634 target_timecode
= 0;
2636 if (mkv_d
->indexes
== NULL
) { /* no index was found */
2637 int64_t target_tc_ns
= (int64_t) (rel_seek_secs
* 1e9
);
2638 if (target_tc_ns
< 0)
2640 uint64_t max_filepos
= 0;
2641 int64_t max_tc
= -1;
2642 int n
= mkv_d
->num_cluster_pos
;
2644 max_filepos
= mkv_d
->cluster_positions
[n
- 1].filepos
;
2645 max_tc
= mkv_d
->cluster_positions
[n
- 1].timecode
;
2648 if (target_tc_ns
> max_tc
) {
2649 if ((off_t
) max_filepos
> stream_tell(s
))
2650 stream_seek(s
, max_filepos
);
2652 stream_seek(s
, stream_tell(s
) + mkv_d
->cluster_size
);
2653 /* parse all the clusters upto target_filepos */
2655 uint64_t start
= stream_tell(s
);
2656 uint32_t type
= ebml_read_id(s
, NULL
);
2657 uint64_t len
= ebml_read_length(s
, NULL
);
2658 uint64_t end
= stream_tell(s
) + len
;
2659 if (type
== MATROSKA_ID_CLUSTER
) {
2660 while (!s
->eof
&& stream_tell(s
) < end
) {
2661 if (ebml_read_id(s
, NULL
)
2662 == MATROSKA_ID_TIMECODE
) {
2663 uint64_t tc
= ebml_read_uint(s
, NULL
);
2664 tc
*= mkv_d
->tc_scale
;
2665 add_cluster_position(mkv_d
, start
, tc
);
2666 if (tc
>= target_tc_ns
)
2672 stream_seek(s
, end
);
2678 if (!mkv_d
->num_cluster_pos
) {
2679 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] no target for seek found\n");
2682 uint64_t cluster_pos
= mkv_d
->cluster_positions
[0].filepos
;
2683 /* Let's find the nearest cluster */
2684 for (i
= 0; i
< mkv_d
->num_cluster_pos
; i
++) {
2685 diff
= mkv_d
->cluster_positions
[i
].timecode
- target_tc_ns
;
2686 if (flags
& SEEK_BACKWARD
&& diff
< 0 && -diff
< min_diff
) {
2687 cluster_pos
= mkv_d
->cluster_positions
[i
].filepos
;
2689 } else if (flags
& SEEK_FORWARD
2690 && (diff
< 0 ? -1 * diff
: diff
) < min_diff
) {
2691 cluster_pos
= mkv_d
->cluster_positions
[i
].filepos
;
2692 min_diff
= diff
< 0 ? -1 * diff
: diff
;
2695 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
2696 stream_seek(s
, cluster_pos
);
2698 int seek_id
= (demuxer
->video
->id
< 0) ?
2699 demuxer
->audio
->id
: demuxer
->video
->id
;
2701 /* let's find the entry in the indexes with the smallest */
2702 /* difference to the wanted timecode. */
2703 for (i
= 0; i
< mkv_d
->num_indexes
; i
++)
2704 if (mkv_d
->indexes
[i
].tnum
== seek_id
) {
2707 (int64_t) (mkv_d
->indexes
[i
].timecode
*
2708 mkv_d
->tc_scale
/ 1000000.0 + 0.5);
2710 if (flags
& SEEK_BACKWARD
) {
2711 // Seek backward: find the last index position
2712 // before target time
2713 if (diff
< 0 || diff
>= min_diff
)
2716 // Seek forward: find the first index position
2717 // after target time. If no such index exists, find last
2718 // position between current position and target time.
2720 if (min_diff
<= 0 && diff
<= min_diff
)
2723 FFMIN(target_timecode
- mkv_d
->last_pts
,
2728 index
= mkv_d
->indexes
+ i
;
2731 if (index
) { /* We've found an entry. */
2732 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
2733 stream_seek(s
, index
->filepos
);
2737 if (demuxer
->video
->id
>= 0)
2738 mkv_d
->v_skip_to_keyframe
= 1;
2739 if (flags
& SEEK_FORWARD
)
2740 mkv_d
->skip_to_timecode
= target_timecode
;
2742 mkv_d
->skip_to_timecode
= index
? index
->timecode
: 0;
2743 mkv_d
->a_skip_to_keyframe
= 1;
2745 demux_mkv_fill_buffer(demuxer
, NULL
);
2746 } else if ((demuxer
->movi_end
<= 0) || !(flags
& SEEK_ABSOLUTE
))
2747 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
2749 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2750 stream_t
*s
= demuxer
->stream
;
2751 uint64_t target_filepos
;
2752 mkv_index_t
*index
= NULL
;
2755 if (mkv_d
->indexes
== NULL
) { /* not implemented without index */
2756 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
2760 target_filepos
= (uint64_t) (demuxer
->movi_end
* rel_seek_secs
);
2761 for (i
= 0; i
< mkv_d
->num_indexes
; i
++)
2762 if (mkv_d
->indexes
[i
].tnum
== demuxer
->video
->id
)
2764 || ((mkv_d
->indexes
[i
].filepos
>= target_filepos
)
2765 && ((index
->filepos
< target_filepos
)
2766 || (mkv_d
->indexes
[i
].filepos
< index
->filepos
))))
2767 index
= &mkv_d
->indexes
[i
];
2772 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
2773 stream_seek(s
, index
->filepos
);
2775 if (demuxer
->video
->id
>= 0)
2776 mkv_d
->v_skip_to_keyframe
= 1;
2777 mkv_d
->skip_to_timecode
= index
->timecode
;
2778 mkv_d
->a_skip_to_keyframe
= 1;
2780 demux_mkv_fill_buffer(demuxer
, NULL
);
2784 static int demux_mkv_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
2786 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2789 case DEMUXER_CTRL_CORRECT_PTS
:
2790 return DEMUXER_CTRL_OK
;
2791 case DEMUXER_CTRL_GET_TIME_LENGTH
:
2792 if (mkv_d
->duration
== 0)
2793 return DEMUXER_CTRL_DONTKNOW
;
2795 *((double *) arg
) = (double) mkv_d
->duration
;
2796 return DEMUXER_CTRL_OK
;
2798 case DEMUXER_CTRL_GET_PERCENT_POS
:
2799 if (mkv_d
->duration
== 0) {
2800 return DEMUXER_CTRL_DONTKNOW
;
2803 *((int *) arg
) = (int) (100 * mkv_d
->last_pts
/ mkv_d
->duration
);
2804 return DEMUXER_CTRL_OK
;
2806 case DEMUXER_CTRL_SWITCH_AUDIO
:
2807 if (demuxer
->audio
&& demuxer
->audio
->sh
) {
2808 sh_audio_t
*sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
2809 int aid
= *(int *) arg
;
2811 aid
= (sh
->aid
+ 1) % mkv_d
->last_aid
;
2812 if (aid
!= sh
->aid
) {
2813 mkv_track_t
*track
=
2814 demux_mkv_find_track_by_num(mkv_d
, aid
,
2815 MATROSKA_TRACK_AUDIO
);
2817 demuxer
->audio
->id
= track
->tnum
;
2818 sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
2819 ds_free_packs(demuxer
->audio
);
2822 *(int *) arg
= sh
->aid
;
2825 return DEMUXER_CTRL_OK
;
2828 return DEMUXER_CTRL_NOTIMPL
;
2832 const demuxer_desc_t demuxer_desc_matroska
= {
2838 DEMUXER_TYPE_MATROSKA
,
2839 1, // safe autodetect
2841 demux_mkv_fill_buffer
,