2 * native Matroska demuxer
3 * Written by 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.
16 #include "stream/stream.h"
26 #include "subreader.h"
27 #include "libvo/sub.h"
29 #include "libass/ass.h"
30 #include "libass/ass_mp.h"
32 #include "libavutil/common.h"
35 #include "loader/qtx/qtxsdk/components.h"
42 #ifdef USE_LIBAVUTIL_SO
43 #include <ffmpeg/lzo.h>
44 #include <ffmpeg/intreadwrite.h>
46 #include "libavutil/lzo.h"
47 #include "libavutil/intreadwrite.h"
49 #include "libavutil/avstring.h"
51 static unsigned char sipr_swaps
[38][2]={
52 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
53 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
54 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
55 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
58 // Map flavour to bytes per second
59 #define SIPR_FLAVORS 4
60 #define ATRC_FLAVORS 8
61 #define COOK_FLAVORS 34
62 static int sipr_fl2bps
[SIPR_FLAVORS
] = {813, 1062, 625, 2000};
63 static int atrc_fl2bps
[ATRC_FLAVORS
] = {8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100};
64 static int cook_fl2bps
[COOK_FLAVORS
] = {1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498,
65 4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584,
66 4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513,
67 12016, 16408, 22911, 33506};
71 uint32_t order
, type
, scope
;
73 uint8_t *comp_settings
;
74 int comp_settings_len
;
75 } mkv_content_encoding_t
;
77 typedef struct mkv_track
88 uint32_t v_width
, v_height
, v_dwidth
, v_dheight
;
92 uint32_t a_channels
, a_bps
;
95 float default_duration
;
100 unsigned int private_size
;
102 /* stuff for realmedia */
104 int rv_kf_base
, rv_kf_pts
;
105 float rv_pts
; /* previous video timestamp */
106 float ra_pts
; /* previous audio timestamp */
108 /** realaudio descrambling */
109 int sub_packet_size
; ///< sub packet size, per stream
110 int sub_packet_h
; ///< number of coded frames per block
111 int coded_framesize
; ///< coded frame size, per stream
112 int audiopk_size
; ///< audio packet size
113 unsigned char *audio_buf
; ///< place to store reordered audio data
114 float *audio_timestamp
; ///< timestamp for each audio packet
115 int sub_packet_cnt
; ///< number of subpacket already received
116 int audio_filepos
; ///< file position of first audio packet in block
118 /* stuff for quicktime */
124 /* The timecodes of video frames might have to be reordered if they're
125 in display order (the timecodes, not the frames themselves!). In this
126 case demux packets have to be cached with the help of these variables. */
127 int reorder_timecodes
;
128 demux_packet_t
**cached_dps
;
129 int num_cached_dps
, num_allocated_dps
;
132 /* generic content encoding support */
133 mkv_content_encoding_t
*encodings
;
136 /* For VobSubs and SSA/ASS */
140 typedef struct mkv_index
143 uint64_t timecode
, filepos
;
146 typedef struct mkv_demuxer
150 float duration
, last_pts
;
151 uint64_t last_filepos
;
153 mkv_track_t
**tracks
;
156 uint64_t tc_scale
, cluster_tc
, first_tc
;
159 uint64_t cluster_size
;
160 uint64_t blockgroup_size
;
162 mkv_index_t
*indexes
;
167 off_t
*parsed_seekhead
;
168 int parsed_seekhead_num
;
170 uint64_t *cluster_positions
;
173 int64_t skip_to_timecode
;
174 int v_skip_to_keyframe
, a_skip_to_keyframe
;
176 int64_t stop_timecode
;
179 int audio_tracks
[MAX_A_STREAMS
];
182 #define REALHEADER_SIZE 16
183 #define RVPROPERTIES_SIZE 34
184 #define RAPROPERTIES4_SIZE 56
185 #define RAPROPERTIES5_SIZE 70
187 /* for e.g. "-slang ger" */
188 extern char *dvdsub_lang
;
189 extern char *audio_lang
;
190 extern int dvdsub_id
;
193 * \brief ensures there is space for at least one additional element
194 * \param array array to grow
195 * \param nelem current number of elements in array
196 * \param elsize size of one array element
198 static void grow_array(void **array
, int nelem
, size_t elsize
) {
200 *array
= realloc(*array
, (nelem
+ 32) * elsize
);
204 demux_mkv_find_track_by_num (mkv_demuxer_t
*d
, int n
, int type
)
208 for (i
=0, id
=0; i
< d
->num_tracks
; i
++)
209 if (d
->tracks
[i
] != NULL
&& d
->tracks
[i
]->type
== type
)
217 demux_mkv_find_track_by_language (mkv_demuxer_t
*d
, char *language
, int type
)
221 language
+= strspn(language
,",");
222 while((len
= strcspn(language
,",")) > 0)
224 for (i
=0; i
< d
->num_tracks
; i
++)
225 if (d
->tracks
[i
] != NULL
&& d
->tracks
[i
]->language
!= NULL
&&
226 d
->tracks
[i
]->type
== type
&&
227 !strncmp(d
->tracks
[i
]->language
, language
, len
))
230 language
+= strspn(language
,",");
237 add_cluster_position (mkv_demuxer_t
*mkv_d
, uint64_t position
)
239 int i
= mkv_d
->num_cluster_pos
;
242 if (mkv_d
->cluster_positions
[i
] == position
)
245 grow_array(&mkv_d
->cluster_positions
, mkv_d
->num_cluster_pos
,
247 mkv_d
->cluster_positions
[mkv_d
->num_cluster_pos
++] = position
;
251 #define AAC_SYNC_EXTENSION_TYPE 0x02b7
253 aac_get_sample_rate_index (uint32_t sample_rate
)
255 if (92017 <= sample_rate
)
257 else if (75132 <= sample_rate
)
259 else if (55426 <= sample_rate
)
261 else if (46009 <= sample_rate
)
263 else if (37566 <= sample_rate
)
265 else if (27713 <= sample_rate
)
267 else if (23004 <= sample_rate
)
269 else if (18783 <= sample_rate
)
271 else if (13856 <= sample_rate
)
273 else if (11502 <= sample_rate
)
275 else if (9391 <= sample_rate
)
283 vobsub_parse_size (sh_sub_t
*sh
, const char *start
)
285 if (sscanf(&start
[6], "%dx%d", &sh
->width
, &sh
->height
) == 2)
287 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] VobSub size: %ux%u\n",
288 sh
->width
, sh
->height
);
295 vobsub_parse_palette (sh_sub_t
*sh
, const char *start
)
300 while (isspace(*start
))
302 for (i
= 0; i
< 16; i
++)
305 if (sscanf(start
, "%06x", &tmp
) != 1)
307 sh
->palette
[i
] = vobsub_palette_to_yuv(tmp
);
309 while ((*start
== ',') || isspace(*start
))
314 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] VobSub palette: %06x,%06x,"
315 "%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,%06x,"
316 "%06x,%06x,%06x\n", sh
->palette
[0],
317 sh
->palette
[1], sh
->palette
[2],
318 sh
->palette
[3], sh
->palette
[4],
319 sh
->palette
[5], sh
->palette
[6],
320 sh
->palette
[7], sh
->palette
[8],
321 sh
->palette
[9], sh
->palette
[10],
322 sh
->palette
[11], sh
->palette
[12],
323 sh
->palette
[13], sh
->palette
[14],
332 vobsub_parse_custom_colors (sh_sub_t
*sh
, const char *start
)
334 int use_custom_colors
, i
;
336 unsigned int tridx
= 0;
338 use_custom_colors
= 0;
340 while (isspace(*start
))
342 if (!strncasecmp(start
, "ON", 2) || (*start
== '1'))
343 use_custom_colors
= 1;
344 else if (!strncasecmp(start
, "OFF", 3) || (*start
== '0'))
345 use_custom_colors
= 0;
346 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] VobSub custom colors: %s\n",
347 use_custom_colors
? "ON" : "OFF");
348 if ((p
= strstr(start
, "tridx:")) != NULL
)
349 tridx
= strtoul(p
+ 6, NULL
, 2);
350 if ((start
= strstr(start
, "colors:")) != NULL
)
353 while (isspace(*start
))
355 for (i
= 0; i
< 4; i
++)
358 if (sscanf(start
, "%06x", &tmp
) != 1)
360 sh
->colors
[i
] = vobsub_rgb_to_yuv(tmp
);
361 if ((tridx
<< i
) & 0x08)
362 sh
->colors
[i
] |= 1 << 31;
364 while ((*start
== ',') || isspace(*start
))
369 sh
->custom_colors
= 4;
370 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] VobSub colors: %08x,"
371 "%08x,%08x,%08x\n", sh
->colors
[0],
372 sh
->colors
[1], sh
->colors
[2],
376 if (!use_custom_colors
)
377 sh
->custom_colors
= 0;
382 vobsub_parse_forced_subs (sh_sub_t
*sh
, const char *start
)
385 while (isspace(*start
))
387 if (!strncasecmp(start
, "on", 2) || (*start
== '1'))
388 sh
->forced_subs_only
= 1;
389 else if (!strncasecmp(start
, "off", 3) || (*start
== '0'))
390 sh
->forced_subs_only
= 0;
393 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] VobSub forced subs: %d\n",
394 sh
->forced_subs_only
);
398 /** \brief Free cached demux packets
400 * Reordering the timecodes requires caching of demux packets. This function
401 * frees all these cached packets and the memory for the cached pointers
404 * \param demuxer The demuxer for which the cache is to be freed.
407 free_cached_dps (demuxer_t
*demuxer
)
409 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
413 for (k
= 0; k
< mkv_d
->num_tracks
; k
++)
415 track
= mkv_d
->tracks
[k
];
416 for (i
= 0; i
< track
->num_cached_dps
; i
++)
417 free_demux_packet (track
->cached_dps
[i
]);
418 free(track
->cached_dps
);
419 track
->cached_dps
= NULL
;
420 track
->num_cached_dps
= 0;
421 track
->num_allocated_dps
= 0;
427 demux_mkv_parse_idx (mkv_track_t
*t
)
429 int things_found
, last
;
430 char *buf
, *pos
, *start
;
432 if ((t
->private_data
== NULL
) || (t
->private_size
== 0))
436 buf
= malloc(t
->private_size
+ 1);
439 memcpy(buf
, t
->private_data
, t
->private_size
);
440 buf
[t
->private_size
] = 0;
441 t
->sh_sub
->has_palette
= 0;
448 if ((*pos
== 0) || (*pos
== '\r') || (*pos
== '\n'))
454 if (!strncasecmp(start
, "size: ", 6))
455 things_found
|= vobsub_parse_size(t
->sh_sub
, start
);
456 else if (!strncasecmp(start
, "palette:", 8))
457 things_found
|= vobsub_parse_palette(t
->sh_sub
, start
);
458 else if (!strncasecmp(start
, "custom colors:", 14))
459 things_found
|= vobsub_parse_custom_colors(t
->sh_sub
, start
);
460 else if (!strncasecmp(start
, "forced subs:", 12))
461 things_found
|= vobsub_parse_forced_subs(t
->sh_sub
, start
);
469 while ((*pos
== '\r') || (*pos
== '\n'));
475 while (!last
&& (*start
!= 0));
479 return (things_found
& 3) == 3;
484 demux_mkv_decode (mkv_track_t
*track
, uint8_t *src
, uint8_t **dest
,
485 uint32_t *size
, uint32_t type
)
491 if (track
->num_encodings
<= 0)
494 for (i
=0; i
<track
->num_encodings
; i
++)
496 if (!(track
->encodings
[i
].scope
& type
))
500 if (track
->encodings
[i
].comp_algo
== 0)
502 /* zlib encoded track */
505 zstream
.zalloc
= (alloc_func
) 0;
506 zstream
.zfree
= (free_func
) 0;
507 zstream
.opaque
= (voidpf
) 0;
508 if (inflateInit (&zstream
) != Z_OK
)
510 mp_msg (MSGT_DEMUX
, MSGL_WARN
,
511 MSGTR_MPDEMUX_MKV_ZlibInitializationFailed
);
514 zstream
.next_in
= (Bytef
*) src
;
515 zstream
.avail_in
= *size
;
519 zstream
.avail_out
= *size
;
522 *dest
= realloc (*dest
, *size
);
523 zstream
.next_out
= (Bytef
*) (*dest
+ zstream
.total_out
);
524 result
= inflate (&zstream
, Z_NO_FLUSH
);
525 if (result
!= Z_OK
&& result
!= Z_STREAM_END
)
527 mp_msg (MSGT_DEMUX
, MSGL_WARN
,
528 MSGTR_MPDEMUX_MKV_ZlibDecompressionFailed
);
531 inflateEnd (&zstream
);
534 zstream
.avail_out
+= 4000;
535 } while (zstream
.avail_out
== 4000 &&
536 zstream
.avail_in
!= 0 && result
!= Z_STREAM_END
);
538 *size
= zstream
.total_out
;
539 inflateEnd (&zstream
);
542 if (track
->encodings
[i
].comp_algo
== 2)
544 /* lzo encoded track */
545 int dstlen
= *size
* 3;
551 if (dstlen
> SIZE_MAX
- LZO_OUTPUT_PADDING
) goto lzo_fail
;
552 *dest
= realloc (*dest
, dstlen
+ LZO_OUTPUT_PADDING
);
553 result
= lzo1x_decode (*dest
, &dstlen
, src
, &srclen
);
556 if (!(result
& LZO_OUTPUT_FULL
))
559 mp_msg (MSGT_DEMUX
, MSGL_WARN
,
560 MSGTR_MPDEMUX_MKV_LzoDecompressionFailed
);
565 mp_msg (MSGT_DEMUX
, MSGL_DBG2
,
566 "[mkv] lzo decompression buffer too small.\n");
578 demux_mkv_read_info (demuxer_t
*demuxer
)
580 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
581 stream_t
*s
= demuxer
->stream
;
584 uint64_t tc_scale
= 1000000;
585 long double duration
= 0.;
587 length
= ebml_read_length (s
, NULL
);
590 switch (ebml_read_id (s
, &il
))
592 case MATROSKA_ID_TIMECODESCALE
:
594 uint64_t num
= ebml_read_uint (s
, &l
);
595 if (num
== EBML_UINT_INVALID
)
598 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + timecode scale: %"PRIu64
"\n",
603 case MATROSKA_ID_DURATION
:
605 long double num
= ebml_read_float (s
, &l
);
606 if (num
== EBML_FLOAT_INVALID
)
609 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + duration: %.3Lfs\n",
610 duration
* tc_scale
/ 1000000000.0);
615 ebml_read_skip (s
, &l
);
620 mkv_d
->tc_scale
= tc_scale
;
621 mkv_d
->duration
= duration
* tc_scale
/ 1000000000.0;
626 * \brief free array of kv_content_encoding_t
627 * \param encodings pointer to array
628 * \param numencodings number of encodings in array
631 demux_mkv_free_encodings(mkv_content_encoding_t
*encodings
, int numencodings
)
633 while (numencodings
-- > 0)
634 free(encodings
[numencodings
].comp_settings
);
639 demux_mkv_read_trackencodings (demuxer_t
*demuxer
, mkv_track_t
*track
)
641 stream_t
*s
= demuxer
->stream
;
642 mkv_content_encoding_t
*ce
, e
;
643 uint64_t len
, length
, l
;
646 ce
= malloc (sizeof (*ce
));
649 len
= length
= ebml_read_length (s
, &il
);
653 switch (ebml_read_id (s
, &il
))
655 case MATROSKA_ID_CONTENTENCODING
:
660 memset (&e
, 0, sizeof (e
));
663 len
= ebml_read_length (s
, &i
);
671 switch (ebml_read_id (s
, &il
))
673 case MATROSKA_ID_CONTENTENCODINGORDER
:
674 num
= ebml_read_uint (s
, &l
);
675 if (num
== EBML_UINT_INVALID
)
680 case MATROSKA_ID_CONTENTENCODINGSCOPE
:
681 num
= ebml_read_uint (s
, &l
);
682 if (num
== EBML_UINT_INVALID
)
687 case MATROSKA_ID_CONTENTENCODINGTYPE
:
688 num
= ebml_read_uint (s
, &l
);
689 if (num
== EBML_UINT_INVALID
)
694 case MATROSKA_ID_CONTENTCOMPRESSION
:
698 le
= ebml_read_length (s
, &i
);
706 switch (ebml_read_id (s
, &il
))
708 case MATROSKA_ID_CONTENTCOMPALGO
:
709 num
= ebml_read_uint (s
, &l
);
710 if (num
== EBML_UINT_INVALID
)
715 case MATROSKA_ID_CONTENTCOMPSETTINGS
:
716 l
= ebml_read_length (s
, &i
);
717 e
.comp_settings
= malloc (l
);
718 stream_read (s
, e
.comp_settings
, l
);
719 e
.comp_settings_len
= l
;
724 ebml_read_skip (s
, &l
);
732 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
733 MSGTR_MPDEMUX_MKV_TrackEncrypted
, track
->tnum
);
735 else if (e
.type
!= 0)
737 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
738 MSGTR_MPDEMUX_MKV_UnknownContentEncoding
, track
->tnum
);
741 if (e
.comp_algo
!= 0 && e
.comp_algo
!= 2)
743 mp_msg (MSGT_DEMUX
, MSGL_WARN
,
744 MSGTR_MPDEMUX_MKV_UnknownCompression
,
745 track
->tnum
, e
.comp_algo
);
748 else if (e
.comp_algo
== 0)
750 mp_msg (MSGT_DEMUX
, MSGL_WARN
,
751 MSGTR_MPDEMUX_MKV_ZlibCompressionUnsupported
,
760 ebml_read_skip (s
, &l
);
766 if (e
.order
<= ce
[i
].order
)
768 ce
= realloc (ce
, (n
+1) *sizeof (*ce
));
769 memmove (ce
+i
+1, ce
+i
, (n
-i
) * sizeof (*ce
));
770 memcpy (ce
+i
, &e
, sizeof (e
));
776 ebml_read_skip (s
, &l
);
783 track
->encodings
= ce
;
784 track
->num_encodings
= n
;
788 demux_mkv_free_encodings(ce
, n
);
793 demux_mkv_read_trackaudio (demuxer_t
*demuxer
, mkv_track_t
*track
)
795 stream_t
*s
= demuxer
->stream
;
796 uint64_t len
, length
, l
;
799 track
->a_sfreq
= 8000.0;
800 track
->a_channels
= 1;
802 len
= length
= ebml_read_length (s
, &il
);
806 switch (ebml_read_id (s
, &il
))
808 case MATROSKA_ID_AUDIOSAMPLINGFREQ
:
810 long double num
= ebml_read_float (s
, &l
);
811 if (num
== EBML_FLOAT_INVALID
)
813 track
->a_sfreq
= num
;
814 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Sampling frequency: %f\n",
819 case MATROSKA_ID_AUDIOBITDEPTH
:
821 uint64_t num
= ebml_read_uint (s
, &l
);
822 if (num
== EBML_UINT_INVALID
)
825 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Bit depth: %u\n",
830 case MATROSKA_ID_AUDIOCHANNELS
:
832 uint64_t num
= ebml_read_uint (s
, &l
);
833 if (num
== EBML_UINT_INVALID
)
835 track
->a_channels
= num
;
836 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Channels: %u\n",
842 ebml_read_skip (s
, &l
);
851 demux_mkv_read_trackvideo (demuxer_t
*demuxer
, mkv_track_t
*track
)
853 stream_t
*s
= demuxer
->stream
;
854 uint64_t len
, length
, l
;
857 len
= length
= ebml_read_length (s
, &il
);
861 switch (ebml_read_id (s
, &il
))
863 case MATROSKA_ID_VIDEOFRAMERATE
:
865 long double num
= ebml_read_float (s
, &l
);
866 if (num
== EBML_FLOAT_INVALID
)
868 track
->v_frate
= num
;
869 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Frame rate: %f\n",
871 if (track
->v_frate
> 0)
872 track
->default_duration
= 1 / track
->v_frate
;
876 case MATROSKA_ID_VIDEODISPLAYWIDTH
:
878 uint64_t num
= ebml_read_uint (s
, &l
);
879 if (num
== EBML_UINT_INVALID
)
881 track
->v_dwidth
= num
;
882 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Display width: %u\n",
887 case MATROSKA_ID_VIDEODISPLAYHEIGHT
:
889 uint64_t num
= ebml_read_uint (s
, &l
);
890 if (num
== EBML_UINT_INVALID
)
892 track
->v_dheight
= num
;
893 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Display height: %u\n",
898 case MATROSKA_ID_VIDEOPIXELWIDTH
:
900 uint64_t num
= ebml_read_uint (s
, &l
);
901 if (num
== EBML_UINT_INVALID
)
903 track
->v_width
= num
;
904 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Pixel width: %u\n",
909 case MATROSKA_ID_VIDEOPIXELHEIGHT
:
911 uint64_t num
= ebml_read_uint (s
, &l
);
912 if (num
== EBML_UINT_INVALID
)
914 track
->v_height
= num
;
915 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Pixel height: %u\n",
921 ebml_read_skip (s
, &l
);
930 * \brief free any data associated with given track
931 * \param track track of which to free data
934 demux_mkv_free_trackentry(mkv_track_t
*track
) {
938 free (track
->codec_id
);
940 free (track
->language
);
941 if (track
->private_data
)
942 free (track
->private_data
);
943 if (track
->audio_buf
)
944 free (track
->audio_buf
);
945 if (track
->audio_timestamp
)
946 free (track
->audio_timestamp
);
947 demux_mkv_free_encodings(track
->encodings
, track
->num_encodings
);
952 demux_mkv_read_trackentry (demuxer_t
*demuxer
)
954 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
955 stream_t
*s
= demuxer
->stream
;
957 uint64_t len
, length
, l
;
960 track
= calloc (1, sizeof (*track
));
961 /* set default values */
962 track
->default_track
= 1;
964 track
->language
= strdup("eng");
966 len
= length
= ebml_read_length (s
, &il
);
970 switch (ebml_read_id (s
, &il
))
972 case MATROSKA_ID_TRACKNUMBER
:
974 uint64_t num
= ebml_read_uint (s
, &l
);
975 if (num
== EBML_UINT_INVALID
)
978 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Track number: %u\n",
983 case MATROSKA_ID_TRACKNAME
:
985 track
->name
= ebml_read_utf8 (s
, &l
);
986 if (track
->name
== NULL
)
988 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Name: %s\n",
993 case MATROSKA_ID_TRACKTYPE
:
995 uint64_t num
= ebml_read_uint (s
, &l
);
996 if (num
== EBML_UINT_INVALID
)
999 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Track type: ");
1000 switch (track
->type
)
1002 case MATROSKA_TRACK_AUDIO
:
1003 mp_msg (MSGT_DEMUX
, MSGL_V
, "Audio\n");
1005 case MATROSKA_TRACK_VIDEO
:
1006 mp_msg (MSGT_DEMUX
, MSGL_V
, "Video\n");
1008 case MATROSKA_TRACK_SUBTITLE
:
1009 mp_msg (MSGT_DEMUX
, MSGL_V
, "Subtitle\n");
1012 mp_msg (MSGT_DEMUX
, MSGL_V
, "unknown\n");
1018 case MATROSKA_ID_TRACKAUDIO
:
1019 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Audio track\n");
1020 l
= demux_mkv_read_trackaudio (demuxer
, track
);
1025 case MATROSKA_ID_TRACKVIDEO
:
1026 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Video track\n");
1027 l
= demux_mkv_read_trackvideo (demuxer
, track
);
1032 case MATROSKA_ID_CODECID
:
1033 track
->codec_id
= ebml_read_ascii (s
, &l
);
1034 if (track
->codec_id
== NULL
)
1036 if (!strcmp (track
->codec_id
, MKV_V_MSCOMP
) ||
1037 !strcmp (track
->codec_id
, MKV_A_ACM
))
1038 track
->ms_compat
= 1;
1039 else if (!strcmp (track
->codec_id
, MKV_S_VOBSUB
))
1040 track
->subtitle_type
= MATROSKA_SUBTYPE_VOBSUB
;
1041 else if (!strcmp (track
->codec_id
, MKV_S_TEXTSSA
)
1042 || !strcmp (track
->codec_id
, MKV_S_TEXTASS
)
1043 || !strcmp (track
->codec_id
, MKV_S_SSA
)
1044 || !strcmp (track
->codec_id
, MKV_S_ASS
))
1046 track
->subtitle_type
= MATROSKA_SUBTYPE_SSA
;
1048 else if (!strcmp (track
->codec_id
, MKV_S_TEXTASCII
))
1049 track
->subtitle_type
= MATROSKA_SUBTYPE_TEXT
;
1050 if (!strcmp (track
->codec_id
, MKV_S_TEXTUTF8
))
1052 track
->subtitle_type
= MATROSKA_SUBTYPE_TEXT
;
1054 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Codec ID: %s\n",
1058 case MATROSKA_ID_CODECPRIVATE
:
1061 uint64_t num
= ebml_read_length (s
, &x
);
1062 // audit: cheap guard against overflows later..
1063 if (num
> SIZE_MAX
- 1000) return 0;
1065 track
->private_data
= malloc (num
+ LZO_INPUT_PADDING
);
1066 if (stream_read(s
, track
->private_data
, num
) != (int) num
)
1068 track
->private_size
= num
;
1069 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + CodecPrivate, length "
1070 "%u\n", track
->private_size
);
1074 case MATROSKA_ID_TRACKLANGUAGE
:
1075 free(track
->language
);
1076 track
->language
= ebml_read_utf8 (s
, &l
);
1077 if (track
->language
== NULL
)
1079 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Language: %s\n",
1083 case MATROSKA_ID_TRACKFLAGDEFAULT
:
1085 uint64_t num
= ebml_read_uint (s
, &l
);
1086 if (num
== EBML_UINT_INVALID
)
1088 track
->default_track
= num
;
1089 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Default flag: %u\n",
1090 track
->default_track
);
1094 case MATROSKA_ID_TRACKDEFAULTDURATION
:
1096 uint64_t num
= ebml_read_uint (s
, &l
);
1097 if (num
== EBML_UINT_INVALID
)
1100 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Default duration: 0");
1103 track
->v_frate
= 1000000000.0 / num
;
1104 track
->default_duration
= num
/ 1000000000.0;
1105 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + Default duration: "
1106 "%.3fms ( = %.3f fps)\n",num
/1000000.0,track
->v_frate
);
1111 case MATROSKA_ID_TRACKENCODINGS
:
1112 l
= demux_mkv_read_trackencodings (demuxer
, track
);
1118 ebml_read_skip (s
, &l
);
1124 mkv_d
->tracks
[mkv_d
->num_tracks
++] = track
;
1128 demux_mkv_free_trackentry(track
);
1133 demux_mkv_read_tracks (demuxer_t
*demuxer
)
1135 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1136 stream_t
*s
= demuxer
->stream
;
1140 mkv_d
->tracks
= malloc (sizeof (*mkv_d
->tracks
));
1141 mkv_d
->num_tracks
= 0;
1143 length
= ebml_read_length (s
, NULL
);
1146 switch (ebml_read_id (s
, &il
))
1148 case MATROSKA_ID_TRACKENTRY
:
1149 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + a track...\n");
1150 mkv_d
->tracks
= realloc (mkv_d
->tracks
,
1151 (mkv_d
->num_tracks
+1)
1152 *sizeof (*mkv_d
->tracks
));
1153 l
= demux_mkv_read_trackentry (demuxer
);
1159 ebml_read_skip (s
, &l
);
1168 demux_mkv_read_cues (demuxer_t
*demuxer
)
1170 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1171 stream_t
*s
= demuxer
->stream
;
1172 uint64_t length
, l
, time
, track
, pos
;
1176 if (index_mode
== 0) {
1177 ebml_read_skip (s
, NULL
);
1180 off
= stream_tell (s
);
1181 for (i
=0; i
<mkv_d
->parsed_cues_num
; i
++)
1182 if (mkv_d
->parsed_cues
[i
] == off
)
1184 ebml_read_skip (s
, NULL
);
1187 mkv_d
->parsed_cues
= realloc (mkv_d
->parsed_cues
,
1188 (mkv_d
->parsed_cues_num
+1)
1190 mkv_d
->parsed_cues
[mkv_d
->parsed_cues_num
++] = off
;
1192 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing cues ] -----------\n");
1193 length
= ebml_read_length (s
, NULL
);
1197 time
= track
= pos
= EBML_UINT_INVALID
;
1199 switch (ebml_read_id (s
, &il
))
1201 case MATROSKA_ID_POINTENTRY
:
1205 len
= ebml_read_length (s
, &i
);
1213 switch (ebml_read_id (s
, &il
))
1215 case MATROSKA_ID_CUETIME
:
1216 time
= ebml_read_uint (s
, &l
);
1219 case MATROSKA_ID_CUETRACKPOSITION
:
1223 le
= ebml_read_length (s
, &i
);
1231 switch (ebml_read_id (s
, &il
))
1233 case MATROSKA_ID_CUETRACK
:
1234 track
= ebml_read_uint (s
, &l
);
1237 case MATROSKA_ID_CUECLUSTERPOSITION
:
1238 pos
= ebml_read_uint (s
, &l
);
1242 ebml_read_skip (s
, &l
);
1251 ebml_read_skip (s
, &l
);
1260 ebml_read_skip (s
, &l
);
1266 if (time
!= EBML_UINT_INVALID
&& track
!= EBML_UINT_INVALID
1267 && pos
!= EBML_UINT_INVALID
)
1269 grow_array(&mkv_d
->indexes
, mkv_d
->num_indexes
, sizeof(mkv_index_t
));
1270 mkv_d
->indexes
[mkv_d
->num_indexes
].tnum
= track
;
1271 mkv_d
->indexes
[mkv_d
->num_indexes
].timecode
= time
;
1272 mkv_d
->indexes
[mkv_d
->num_indexes
].filepos
=mkv_d
->segment_start
+pos
;
1273 mp_msg (MSGT_DEMUX
, MSGL_DBG2
, "[mkv] |+ found cue point "
1274 "for track %"PRIu64
": timecode %"PRIu64
", filepos: %"PRIu64
"\n",
1275 track
, time
, mkv_d
->segment_start
+ pos
);
1276 mkv_d
->num_indexes
++;
1280 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] \\---- [ parsing cues ] -----------\n");
1285 demux_mkv_read_chapters (demuxer_t
*demuxer
)
1287 stream_t
*s
= demuxer
->stream
;
1291 if (demuxer
->chapters
)
1293 ebml_read_skip (s
, NULL
);
1297 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing chapters ] ---------\n");
1298 length
= ebml_read_length (s
, NULL
);
1302 switch (ebml_read_id (s
, &il
))
1304 case MATROSKA_ID_EDITIONENTRY
:
1309 len
= ebml_read_length (s
, &i
);
1317 switch (ebml_read_id (s
, &il
))
1319 case MATROSKA_ID_CHAPTERATOM
:
1321 uint64_t len
, start
=0, end
=0;
1326 len
= ebml_read_length (s
, &i
);
1334 switch (ebml_read_id (s
, &il
))
1336 case MATROSKA_ID_CHAPTERTIMESTART
:
1337 start
= ebml_read_uint (s
, &l
) / 1000000;
1340 case MATROSKA_ID_CHAPTERTIMEEND
:
1341 end
= ebml_read_uint (s
, &l
) / 1000000;
1344 case MATROSKA_ID_CHAPTERDISPLAY
:
1349 len
= ebml_read_length (s
, &i
);
1356 switch (ebml_read_id (s
, &il
))
1358 case MATROSKA_ID_CHAPSTRING
:
1359 name
= ebml_read_utf8 (s
, &l
);
1362 ebml_read_skip (s
, &l
);
1371 ebml_read_skip (s
, &l
);
1378 name
= strdup("(unnamed)");
1380 cid
= demuxer_add_chapter(demuxer
, name
, start
, end
);
1382 mp_msg(MSGT_DEMUX
, MSGL_V
,
1383 "[mkv] Chapter %u from %02d:%02d:%02d."
1384 "%03d to %02d:%02d:%02d.%03d, %s\n",
1386 (int) (start
/ 60 / 60 / 1000),
1387 (int) ((start
/ 60 / 1000) % 60),
1388 (int) ((start
/ 1000) % 60),
1389 (int) (start
% 1000),
1390 (int) (end
/ 60 / 60 / 1000),
1391 (int) ((end
/ 60 / 1000) % 60),
1392 (int) ((end
/ 1000) % 60),
1393 (int) (end
% 1000), name
);
1400 ebml_read_skip (s
, &l
);
1409 ebml_read_skip (s
, &l
);
1416 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] \\---- [ parsing chapters ] ---------\n");
1421 demux_mkv_read_tags (demuxer_t
*demuxer
)
1423 ebml_read_skip (demuxer
->stream
, NULL
);
1428 demux_mkv_read_attachments (demuxer_t
*demuxer
)
1430 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1431 stream_t
*s
= demuxer
->stream
;
1435 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing attachments ] ---------\n");
1436 length
= ebml_read_length (s
, NULL
);
1440 switch (ebml_read_id (s
, &il
))
1442 case MATROSKA_ID_ATTACHEDFILE
:
1451 len
= ebml_read_length (s
, &i
);
1454 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + an attachment...\n");
1461 switch (ebml_read_id (s
, &il
))
1463 case MATROSKA_ID_FILENAME
:
1464 name
= ebml_read_utf8 (s
, &l
);
1467 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + FileName: %s\n",
1471 case MATROSKA_ID_FILEMIMETYPE
:
1472 mime
= ebml_read_ascii (s
, &l
);
1475 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + FileMimeType: %s\n",
1479 case MATROSKA_ID_FILEDATA
:
1482 uint64_t num
= ebml_read_length (s
, &x
);
1485 data
= malloc (num
);
1486 if (stream_read(s
, data
, num
) != (int) num
)
1492 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] | + FileData, length "
1498 ebml_read_skip (s
, &l
);
1504 demuxer_add_attachment(demuxer
, name
, mime
, data
, data_size
);
1505 mp_msg(MSGT_DEMUX
, MSGL_V
,
1506 "[mkv] Attachment: %s, %s, %u bytes\n",
1507 name
, mime
, data_size
);
1512 ebml_read_skip (s
, &l
);
1518 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] \\---- [ parsing attachments ] ---------\n");
1523 demux_mkv_read_seekhead (demuxer_t
*demuxer
)
1525 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1526 stream_t
*s
= demuxer
->stream
;
1527 uint64_t length
, l
, seek_pos
, saved_pos
, num
;
1532 off
= stream_tell (s
);
1533 for (i
=0; i
<mkv_d
->parsed_seekhead_num
; i
++)
1534 if (mkv_d
->parsed_seekhead
[i
] == off
)
1536 ebml_read_skip (s
, NULL
);
1539 mkv_d
->parsed_seekhead
= realloc (mkv_d
->parsed_seekhead
,
1540 (mkv_d
->parsed_seekhead_num
+1)
1542 mkv_d
->parsed_seekhead
[mkv_d
->parsed_seekhead_num
++] = off
;
1544 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] /---- [ parsing seek head ] ---------\n");
1545 length
= ebml_read_length (s
, NULL
);
1546 /* off now holds the position of the next element after the seek head. */
1547 off
= stream_tell (s
) + length
;
1548 while (length
> 0 && !res
)
1552 seek_pos
= EBML_UINT_INVALID
;
1554 switch (ebml_read_id (s
, &il
))
1556 case MATROSKA_ID_SEEKENTRY
:
1560 len
= ebml_read_length (s
, &i
);
1568 switch (ebml_read_id (s
, &il
))
1570 case MATROSKA_ID_SEEKID
:
1571 num
= ebml_read_uint (s
, &l
);
1572 if (num
!= EBML_UINT_INVALID
)
1576 case MATROSKA_ID_SEEKPOSITION
:
1577 seek_pos
= ebml_read_uint (s
, &l
);
1581 ebml_read_skip (s
, &l
);
1591 ebml_read_skip (s
, &l
);
1596 if (seek_id
== 0 || seek_id
== MATROSKA_ID_CLUSTER
1597 || seek_pos
== EBML_UINT_INVALID
||
1598 ((mkv_d
->segment_start
+ seek_pos
) >= (uint64_t)demuxer
->movi_end
))
1601 saved_pos
= stream_tell (s
);
1602 if (!stream_seek (s
, mkv_d
->segment_start
+ seek_pos
))
1606 if (ebml_read_id (s
, &il
) != seek_id
)
1611 case MATROSKA_ID_CUES
:
1612 if (demux_mkv_read_cues (demuxer
))
1616 case MATROSKA_ID_TAGS
:
1617 if (demux_mkv_read_tags (demuxer
))
1621 case MATROSKA_ID_SEEKHEAD
:
1622 if (demux_mkv_read_seekhead (demuxer
))
1626 case MATROSKA_ID_CHAPTERS
:
1627 if (demux_mkv_read_chapters (demuxer
))
1633 stream_seek (s
, saved_pos
);
1637 /* If there was an error then try to skip this seek head. */
1638 if (stream_seek (s
, off
))
1643 stream_seek (s
, stream_tell (s
) + length
);
1644 mp_msg(MSGT_DEMUX
, MSGL_V
, "[mkv] \\---- [ parsing seek head ] ---------\n");
1649 demux_mkv_open_video (demuxer_t
*demuxer
, mkv_track_t
*track
, int vid
);
1651 demux_mkv_open_audio (demuxer_t
*demuxer
, mkv_track_t
*track
, int aid
);
1653 demux_mkv_open_sub (demuxer_t
*demuxer
, mkv_track_t
*track
, int sid
);
1656 display_create_tracks (demuxer_t
*demuxer
)
1658 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*)demuxer
->priv
;
1659 int i
, vid
=0, aid
=0, sid
=0;
1661 for (i
=0; i
<mkv_d
->num_tracks
; i
++)
1663 char *type
= "unknown", str
[32];
1665 switch (mkv_d
->tracks
[i
]->type
)
1667 case MATROSKA_TRACK_VIDEO
:
1669 demux_mkv_open_video(demuxer
, mkv_d
->tracks
[i
], vid
);
1670 if (mkv_d
->tracks
[i
]->name
)
1671 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_VID_%d_NAME=%s\n", vid
, mkv_d
->tracks
[i
]->name
);
1672 sprintf (str
, "-vid %u", vid
++);
1674 case MATROSKA_TRACK_AUDIO
:
1676 demux_mkv_open_audio(demuxer
, mkv_d
->tracks
[i
], aid
);
1677 if (mkv_d
->tracks
[i
]->name
)
1678 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_NAME=%s\n", aid
, mkv_d
->tracks
[i
]->name
);
1679 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", aid
, mkv_d
->tracks
[i
]->language
);
1680 sprintf (str
, "-aid %u, -alang %.5s",aid
++,mkv_d
->tracks
[i
]->language
);
1682 case MATROSKA_TRACK_SUBTITLE
:
1684 demux_mkv_open_sub(demuxer
, mkv_d
->tracks
[i
], sid
);
1685 if (mkv_d
->tracks
[i
]->name
)
1686 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_NAME=%s\n", sid
, mkv_d
->tracks
[i
]->name
);
1687 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sid
, mkv_d
->tracks
[i
]->language
);
1688 sprintf (str
, "-sid %u, -slang %.5s",sid
++,mkv_d
->tracks
[i
]->language
);
1691 if (mkv_d
->tracks
[i
]->name
)
1692 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_MPDEMUX_MKV_TrackIDName
,
1693 mkv_d
->tracks
[i
]->tnum
, type
, mkv_d
->tracks
[i
]->codec_id
, mkv_d
->tracks
[i
]->name
, str
);
1695 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_MPDEMUX_MKV_TrackID
,
1696 mkv_d
->tracks
[i
]->tnum
, type
, mkv_d
->tracks
[i
]->codec_id
, str
);
1704 } videocodec_info_t
;
1706 static const videocodec_info_t vinfo
[] = {
1707 { MKV_V_MPEG1
, mmioFOURCC('m', 'p', 'g', '1'), 0 },
1708 { MKV_V_MPEG2
, mmioFOURCC('m', 'p', 'g', '2'), 0 },
1709 { MKV_V_MPEG4_SP
, mmioFOURCC('m', 'p', '4', 'v'), 1 },
1710 { MKV_V_MPEG4_ASP
, mmioFOURCC('m', 'p', '4', 'v'), 1 },
1711 { MKV_V_MPEG4_AP
, mmioFOURCC('m', 'p', '4', 'v'), 1 },
1712 { MKV_V_MPEG4_AVC
, mmioFOURCC('a', 'v', 'c', '1'), 1 },
1713 { MKV_V_THEORA
, mmioFOURCC('t', 'h', 'e', 'o'), 1 },
1718 demux_mkv_open_video (demuxer_t
*demuxer
, mkv_track_t
*track
, int vid
)
1720 BITMAPINFOHEADER
*bih
;
1721 void *ImageDesc
= NULL
;
1724 if (track
->ms_compat
) /* MS compatibility mode */
1726 BITMAPINFOHEADER
*src
;
1728 if (track
->private_data
== NULL
1729 || track
->private_size
< sizeof (BITMAPINFOHEADER
))
1732 src
= (BITMAPINFOHEADER
*) track
->private_data
;
1733 bih
= calloc (1, track
->private_size
);
1734 bih
->biSize
= le2me_32 (src
->biSize
);
1735 bih
->biWidth
= le2me_32 (src
->biWidth
);
1736 bih
->biHeight
= le2me_32 (src
->biHeight
);
1737 bih
->biPlanes
= le2me_16 (src
->biPlanes
);
1738 bih
->biBitCount
= le2me_16 (src
->biBitCount
);
1739 bih
->biCompression
= le2me_32 (src
->biCompression
);
1740 bih
->biSizeImage
= le2me_32 (src
->biSizeImage
);
1741 bih
->biXPelsPerMeter
= le2me_32 (src
->biXPelsPerMeter
);
1742 bih
->biYPelsPerMeter
= le2me_32 (src
->biYPelsPerMeter
);
1743 bih
->biClrUsed
= le2me_32 (src
->biClrUsed
);
1744 bih
->biClrImportant
= le2me_32 (src
->biClrImportant
);
1745 memcpy((char *) bih
+ sizeof (BITMAPINFOHEADER
),
1746 (char *) src
+ sizeof (BITMAPINFOHEADER
),
1747 track
->private_size
- sizeof (BITMAPINFOHEADER
));
1749 if (track
->v_width
== 0)
1750 track
->v_width
= bih
->biWidth
;
1751 if (track
->v_height
== 0)
1752 track
->v_height
= bih
->biHeight
;
1756 bih
= calloc (1, sizeof (BITMAPINFOHEADER
));
1757 bih
->biSize
= sizeof (BITMAPINFOHEADER
);
1758 bih
->biWidth
= track
->v_width
;
1759 bih
->biHeight
= track
->v_height
;
1760 bih
->biBitCount
= 24;
1761 bih
->biSizeImage
= bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
/8;
1763 if (track
->private_size
>= RVPROPERTIES_SIZE
1764 && (!strcmp (track
->codec_id
, MKV_V_REALV10
)
1765 || !strcmp (track
->codec_id
, MKV_V_REALV20
)
1766 || !strcmp (track
->codec_id
, MKV_V_REALV30
)
1767 || !strcmp (track
->codec_id
, MKV_V_REALV40
)))
1769 unsigned char *dst
, *src
;
1773 src
= track
->private_data
+ RVPROPERTIES_SIZE
;
1775 cnt
= track
->private_size
- RVPROPERTIES_SIZE
;
1776 bih
= realloc(bih
, sizeof (BITMAPINFOHEADER
)+8+cnt
);
1777 bih
->biSize
= 48+cnt
;
1779 type2
= AV_RB32(src
- 4);
1780 if (type2
== 0x10003000 || type2
== 0x10003001)
1781 bih
->biCompression
=mmioFOURCC('R','V','1','3');
1783 bih
->biCompression
=mmioFOURCC('R','V',track
->codec_id
[9],'0');
1784 dst
= (unsigned char *) (bih
+ 1);
1785 // copy type1 and type2 info from rv properties
1786 memcpy(dst
, src
- 8, 8);
1787 stream_read(demuxer
->stream
, dst
+8, cnt
);
1788 track
->realmedia
= 1;
1790 #ifdef USE_QTX_CODECS
1792 else if (track
->private_size
>= sizeof (ImageDescription
)
1793 && !strcmp(track
->codec_id
, MKV_V_QUICKTIME
))
1795 ImageDescriptionPtr idesc
;
1797 idesc
= (ImageDescriptionPtr
) track
->private_data
;
1798 idesc
->idSize
= be2me_32 (idesc
->idSize
);
1799 idesc
->cType
= be2me_32 (idesc
->cType
);
1800 idesc
->version
= be2me_16 (idesc
->version
);
1801 idesc
->revisionLevel
= be2me_16 (idesc
->revisionLevel
);
1802 idesc
->vendor
= be2me_32 (idesc
->vendor
);
1803 idesc
->temporalQuality
= be2me_32 (idesc
->temporalQuality
);
1804 idesc
->spatialQuality
= be2me_32 (idesc
->spatialQuality
);
1805 idesc
->width
= be2me_16 (idesc
->width
);
1806 idesc
->height
= be2me_16 (idesc
->height
);
1807 idesc
->hRes
= be2me_32 (idesc
->hRes
);
1808 idesc
->vRes
= be2me_32 (idesc
->vRes
);
1809 idesc
->dataSize
= be2me_32 (idesc
->dataSize
);
1810 idesc
->frameCount
= be2me_16 (idesc
->frameCount
);
1811 idesc
->depth
= be2me_16 (idesc
->depth
);
1812 idesc
->clutID
= be2me_16 (idesc
->clutID
);
1814 bih
->biCompression
= idesc
->cType
;
1816 #endif /* USE_QTX_CODECS */
1821 const videocodec_info_t
*vi
= vinfo
;
1822 while (vi
->id
&& strcmp(vi
->id
, track
->codec_id
)) vi
++;
1823 bih
->biCompression
= vi
->fourcc
;
1824 if (vi
->extradata
&& track
->private_data
&& (track
->private_size
> 0))
1826 bih
->biSize
+= track
->private_size
;
1827 bih
= realloc (bih
, bih
->biSize
);
1828 memcpy (bih
+ 1, track
->private_data
, track
->private_size
);
1830 track
->reorder_timecodes
= !correct_pts
;
1832 mp_msg (MSGT_DEMUX
,MSGL_WARN
, MSGTR_MPDEMUX_MKV_UnknownCodecID
,
1833 track
->codec_id
, track
->tnum
);
1840 sh_v
= new_sh_video_vid (demuxer
, track
->tnum
, vid
);
1842 sh_v
->format
= sh_v
->bih
->biCompression
;
1843 if (track
->v_frate
== 0.0)
1844 track
->v_frate
= 25.0;
1845 sh_v
->fps
= track
->v_frate
;
1846 sh_v
->frametime
= 1 / track
->v_frate
;
1848 if (!track
->realmedia
)
1850 sh_v
->disp_w
= track
->v_width
;
1851 sh_v
->disp_h
= track
->v_height
;
1852 if (track
->v_dheight
)
1853 sh_v
->aspect
= (float)track
->v_dwidth
/ (float)track
->v_dheight
;
1857 // vd_realvid.c will set aspect to disp_w/disp_h and rederive
1858 // disp_w and disp_h from the RealVideo stream contents returned
1859 // by the Real DLLs. If DisplayWidth/DisplayHeight was not set in
1860 // the Matroska file then it has already been set to PixelWidth/Height
1861 // by check_track_information.
1862 sh_v
->disp_w
= track
->v_dwidth
;
1863 sh_v
->disp_h
= track
->v_dheight
;
1865 sh_v
->ImageDesc
= ImageDesc
;
1866 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] Aspect: %f\n", sh_v
->aspect
);
1868 sh_v
->ds
= demuxer
->video
;
1873 demux_mkv_open_audio (demuxer_t
*demuxer
, mkv_track_t
*track
, int aid
)
1875 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
1876 sh_audio_t
*sh_a
= new_sh_audio_aid(demuxer
, track
->tnum
, aid
);
1879 mkv_d
->audio_tracks
[mkv_d
->last_aid
] = track
->tnum
;
1881 sh_a
->ds
= demuxer
->audio
;
1882 sh_a
->wf
= malloc (sizeof (WAVEFORMATEX
));
1883 if (track
->ms_compat
&& (track
->private_size
>= sizeof(WAVEFORMATEX
)))
1885 WAVEFORMATEX
*wf
= (WAVEFORMATEX
*)track
->private_data
;
1886 sh_a
->wf
= realloc(sh_a
->wf
, track
->private_size
);
1887 sh_a
->wf
->wFormatTag
= le2me_16 (wf
->wFormatTag
);
1888 sh_a
->wf
->nChannels
= le2me_16 (wf
->nChannels
);
1889 sh_a
->wf
->nSamplesPerSec
= le2me_32 (wf
->nSamplesPerSec
);
1890 sh_a
->wf
->nAvgBytesPerSec
= le2me_32 (wf
->nAvgBytesPerSec
);
1891 sh_a
->wf
->nBlockAlign
= le2me_16 (wf
->nBlockAlign
);
1892 sh_a
->wf
->wBitsPerSample
= le2me_16 (wf
->wBitsPerSample
);
1893 sh_a
->wf
->cbSize
= track
->private_size
- sizeof(WAVEFORMATEX
);
1894 memcpy(sh_a
->wf
+ 1, wf
+ 1, track
->private_size
- sizeof(WAVEFORMATEX
));
1895 if (track
->a_sfreq
== 0.0)
1896 track
->a_sfreq
= sh_a
->wf
->nSamplesPerSec
;
1897 if (track
->a_channels
== 0)
1898 track
->a_channels
= sh_a
->wf
->nChannels
;
1899 if (track
->a_bps
== 0)
1900 track
->a_bps
= sh_a
->wf
->wBitsPerSample
;
1901 track
->a_formattag
= sh_a
->wf
->wFormatTag
;
1905 memset(sh_a
->wf
, 0, sizeof (WAVEFORMATEX
));
1906 if (!strcmp(track
->codec_id
, MKV_A_MP3
) ||
1907 !strcmp(track
->codec_id
, MKV_A_MP2
))
1908 track
->a_formattag
= 0x0055;
1909 else if (!strncmp(track
->codec_id
, MKV_A_AC3
, strlen(MKV_A_AC3
)))
1910 track
->a_formattag
= 0x2000;
1911 else if (!strcmp(track
->codec_id
, MKV_A_DTS
))
1912 track
->a_formattag
= 0x2001;
1913 else if (!strcmp(track
->codec_id
, MKV_A_PCM
) ||
1914 !strcmp(track
->codec_id
, MKV_A_PCM_BE
))
1915 track
->a_formattag
= 0x0001;
1916 else if (!strcmp(track
->codec_id
, MKV_A_AAC_2MAIN
) ||
1917 !strncmp(track
->codec_id
, MKV_A_AAC_2LC
,
1918 strlen(MKV_A_AAC_2LC
)) ||
1919 !strcmp(track
->codec_id
, MKV_A_AAC_2SSR
) ||
1920 !strcmp(track
->codec_id
, MKV_A_AAC_4MAIN
) ||
1921 !strncmp(track
->codec_id
, MKV_A_AAC_4LC
,
1922 strlen(MKV_A_AAC_4LC
)) ||
1923 !strcmp(track
->codec_id
, MKV_A_AAC_4SSR
) ||
1924 !strcmp(track
->codec_id
, MKV_A_AAC_4LTP
) ||
1925 !strcmp(track
->codec_id
, MKV_A_AAC
))
1926 track
->a_formattag
= mmioFOURCC('M', 'P', '4', 'A');
1927 else if (!strcmp(track
->codec_id
, MKV_A_VORBIS
))
1929 if (track
->private_data
== NULL
)
1931 track
->a_formattag
= mmioFOURCC('v', 'r', 'b', 's');
1933 else if (!strcmp(track
->codec_id
, MKV_A_QDMC
))
1934 track
->a_formattag
= mmioFOURCC('Q', 'D', 'M', 'C');
1935 else if (!strcmp(track
->codec_id
, MKV_A_QDMC2
))
1936 track
->a_formattag
= mmioFOURCC('Q', 'D', 'M', '2');
1937 else if (!strcmp(track
->codec_id
, MKV_A_WAVPACK
))
1938 track
->a_formattag
= mmioFOURCC('W', 'V', 'P', 'K');
1939 else if (!strcmp(track
->codec_id
, MKV_A_FLAC
))
1941 if (track
->private_data
== NULL
|| track
->private_size
== 0)
1943 mp_msg (MSGT_DEMUX
, MSGL_WARN
,
1944 MSGTR_MPDEMUX_MKV_FlacTrackDoesNotContainValidHeaders
);
1947 track
->a_formattag
= mmioFOURCC ('f', 'L', 'a', 'C');
1949 else if (track
->private_size
>= RAPROPERTIES4_SIZE
)
1951 if (!strcmp(track
->codec_id
, MKV_A_REAL28
))
1952 track
->a_formattag
= mmioFOURCC('2', '8', '_', '8');
1953 else if (!strcmp(track
->codec_id
, MKV_A_REALATRC
))
1954 track
->a_formattag
= mmioFOURCC('a', 't', 'r', 'c');
1955 else if (!strcmp(track
->codec_id
, MKV_A_REALCOOK
))
1956 track
->a_formattag
= mmioFOURCC('c', 'o', 'o', 'k');
1957 else if (!strcmp(track
->codec_id
, MKV_A_REALDNET
))
1958 track
->a_formattag
= mmioFOURCC('d', 'n', 'e', 't');
1959 else if (!strcmp(track
->codec_id
, MKV_A_REALSIPR
))
1960 track
->a_formattag
= mmioFOURCC('s', 'i', 'p', 'r');
1964 mp_msg (MSGT_DEMUX
, MSGL_WARN
, MSGTR_MPDEMUX_MKV_UnknownAudioCodec
,
1965 track
->codec_id
, track
->tnum
);
1966 free_sh_audio(demuxer
, track
->tnum
);
1971 sh_a
->format
= track
->a_formattag
;
1972 sh_a
->wf
->wFormatTag
= track
->a_formattag
;
1973 sh_a
->channels
= track
->a_channels
;
1974 sh_a
->wf
->nChannels
= track
->a_channels
;
1975 sh_a
->samplerate
= (uint32_t) track
->a_sfreq
;
1976 sh_a
->wf
->nSamplesPerSec
= (uint32_t) track
->a_sfreq
;
1977 if (track
->a_bps
== 0)
1979 sh_a
->samplesize
= 2;
1980 sh_a
->wf
->wBitsPerSample
= 16;
1984 sh_a
->samplesize
= track
->a_bps
/ 8;
1985 sh_a
->wf
->wBitsPerSample
= track
->a_bps
;
1987 if (track
->a_formattag
== 0x0055) /* MP3 || MP2 */
1989 sh_a
->wf
->nAvgBytesPerSec
= 16000;
1990 sh_a
->wf
->nBlockAlign
= 1152;
1992 else if ((track
->a_formattag
== 0x2000) || /* AC3 */
1993 (track
->a_formattag
== 0x2001)) /* DTS */
1998 else if (track
->a_formattag
== 0x0001) /* PCM || PCM_BE */
2000 sh_a
->wf
->nAvgBytesPerSec
= sh_a
->channels
* sh_a
->samplerate
*2;
2001 sh_a
->wf
->nBlockAlign
= sh_a
->wf
->nAvgBytesPerSec
;
2002 if (!strcmp(track
->codec_id
, MKV_A_PCM_BE
))
2003 sh_a
->format
= mmioFOURCC('t', 'w', 'o', 's');
2005 else if (!strcmp(track
->codec_id
, MKV_A_QDMC
) ||
2006 !strcmp(track
->codec_id
, MKV_A_QDMC2
))
2008 sh_a
->wf
->nAvgBytesPerSec
= 16000;
2009 sh_a
->wf
->nBlockAlign
= 1486;
2010 track
->fix_i_bps
= 1;
2011 track
->qt_last_a_pts
= 0.0;
2012 if (track
->private_data
!= NULL
)
2014 sh_a
->codecdata
=malloc(track
->private_size
);
2015 memcpy (sh_a
->codecdata
, track
->private_data
,
2016 track
->private_size
);
2017 sh_a
->codecdata_len
= track
->private_size
;
2020 else if (track
->a_formattag
== mmioFOURCC('M', 'P', '4', 'A'))
2022 int profile
, srate_idx
;
2024 sh_a
->wf
->nAvgBytesPerSec
= 16000;
2025 sh_a
->wf
->nBlockAlign
= 1024;
2027 if (!strcmp (track
->codec_id
, MKV_A_AAC
) &&
2028 (NULL
!= track
->private_data
))
2030 sh_a
->codecdata
=malloc(track
->private_size
);
2031 memcpy (sh_a
->codecdata
, track
->private_data
,
2032 track
->private_size
);
2033 sh_a
->codecdata_len
= track
->private_size
;
2037 /* Recreate the 'private data' */
2038 /* which faad2 uses in its initialization */
2039 srate_idx
= aac_get_sample_rate_index (sh_a
->samplerate
);
2040 if (!strncmp (&track
->codec_id
[12], "MAIN", 4))
2042 else if (!strncmp (&track
->codec_id
[12], "LC", 2))
2044 else if (!strncmp (&track
->codec_id
[12], "SSR", 3))
2048 sh_a
->codecdata
= malloc (5);
2049 sh_a
->codecdata
[0] = ((profile
+1) << 3) | ((srate_idx
&0xE) >> 1);
2050 sh_a
->codecdata
[1] = ((srate_idx
&0x1)<<7)|(track
->a_channels
<<3);
2052 if (strstr(track
->codec_id
, "SBR") != NULL
)
2054 /* HE-AAC (aka SBR AAC) */
2055 sh_a
->codecdata_len
= 5;
2057 sh_a
->samplerate
*= 2;
2058 sh_a
->wf
->nSamplesPerSec
*= 2;
2059 srate_idx
= aac_get_sample_rate_index(sh_a
->samplerate
);
2060 sh_a
->codecdata
[2] = AAC_SYNC_EXTENSION_TYPE
>> 3;
2061 sh_a
->codecdata
[3] = ((AAC_SYNC_EXTENSION_TYPE
&0x07)<<5) | 5;
2062 sh_a
->codecdata
[4] = (1 << 7) | (srate_idx
<< 3);
2063 track
->default_duration
= 1024.0 / (sh_a
->samplerate
/ 2);
2067 sh_a
->codecdata_len
= 2;
2068 track
->default_duration
= 1024.0 / (float)sh_a
->samplerate
;
2071 else if (track
->a_formattag
== mmioFOURCC('v', 'r', 'b', 's')) /* VORBIS */
2073 sh_a
->wf
->cbSize
= track
->private_size
;
2074 sh_a
->wf
= realloc(sh_a
->wf
, sizeof(WAVEFORMATEX
) + sh_a
->wf
->cbSize
);
2075 memcpy((unsigned char *) (sh_a
->wf
+1), track
->private_data
, sh_a
->wf
->cbSize
);
2077 else if (track
->private_size
>= RAPROPERTIES4_SIZE
2078 && !strncmp (track
->codec_id
, MKV_A_REALATRC
, 7))
2080 /* Common initialization for all RealAudio codecs */
2081 unsigned char *src
= track
->private_data
;
2082 int codecdata_length
, version
;
2085 sh_a
->wf
->nAvgBytesPerSec
= 0; /* FIXME !? */
2087 version
= AV_RB16(src
+ 4);
2088 flavor
= AV_RB16(src
+ 22);
2089 track
->coded_framesize
= AV_RB32(src
+ 24);
2090 track
->sub_packet_h
= AV_RB16(src
+ 40);
2091 sh_a
->wf
->nBlockAlign
=
2092 track
->audiopk_size
= AV_RB16(src
+ 42);
2093 track
->sub_packet_size
= AV_RB16(src
+ 44);
2096 src
+= RAPROPERTIES4_SIZE
;
2101 src
+= RAPROPERTIES5_SIZE
;
2106 codecdata_length
= AV_RB32(src
);
2108 sh_a
->wf
->cbSize
= codecdata_length
;
2109 sh_a
->wf
= realloc (sh_a
->wf
,
2110 sizeof (WAVEFORMATEX
) +
2112 memcpy(((char *)(sh_a
->wf
+ 1)), src
, codecdata_length
);
2114 switch (track
->a_formattag
) {
2115 case mmioFOURCC('a', 't', 'r', 'c'):
2116 sh_a
->wf
->nAvgBytesPerSec
= atrc_fl2bps
[flavor
];
2117 sh_a
->wf
->nBlockAlign
= track
->sub_packet_size
;
2118 track
->audio_buf
= malloc(track
->sub_packet_h
* track
->audiopk_size
);
2119 track
->audio_timestamp
= malloc(track
->sub_packet_h
* sizeof(float));
2121 case mmioFOURCC('c', 'o', 'o', 'k'):
2122 sh_a
->wf
->nAvgBytesPerSec
= cook_fl2bps
[flavor
];
2123 sh_a
->wf
->nBlockAlign
= track
->sub_packet_size
;
2124 track
->audio_buf
= malloc(track
->sub_packet_h
* track
->audiopk_size
);
2125 track
->audio_timestamp
= malloc(track
->sub_packet_h
* sizeof(float));
2127 case mmioFOURCC('s', 'i', 'p', 'r'):
2128 sh_a
->wf
->nAvgBytesPerSec
= sipr_fl2bps
[flavor
];
2129 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
2130 track
->audio_buf
= malloc(track
->sub_packet_h
* track
->audiopk_size
);
2131 track
->audio_timestamp
= malloc(track
->sub_packet_h
* sizeof(float));
2133 case mmioFOURCC('2', '8', '_', '8'):
2134 sh_a
->wf
->nAvgBytesPerSec
= 3600;
2135 sh_a
->wf
->nBlockAlign
= track
->coded_framesize
;
2136 track
->audio_buf
= malloc(track
->sub_packet_h
* track
->audiopk_size
);
2137 track
->audio_timestamp
= malloc(track
->sub_packet_h
* sizeof(float));
2141 track
->realmedia
= 1;
2143 else if (!strcmp(track
->codec_id
, MKV_A_FLAC
) ||
2144 (track
->a_formattag
== 0xf1ac))
2151 if (track
->a_formattag
== mmioFOURCC('f', 'L', 'a', 'C'))
2153 ptr
= (unsigned char *)track
->private_data
;
2154 size
= track
->private_size
;
2158 sh_a
->format
= mmioFOURCC('f', 'L', 'a', 'C');
2159 ptr
= (unsigned char *) track
->private_data
2160 + sizeof (WAVEFORMATEX
);
2161 size
= track
->private_size
- sizeof (WAVEFORMATEX
);
2163 if (size
< 4 || ptr
[0] != 'f' || ptr
[1] != 'L' ||
2164 ptr
[2] != 'a' || ptr
[3] != 'C')
2166 dp
= new_demux_packet (4);
2167 memcpy (dp
->buffer
, "fLaC", 4);
2171 dp
= new_demux_packet (size
);
2172 memcpy (dp
->buffer
, ptr
, size
);
2176 ds_add_packet (demuxer
->audio
, dp
);
2178 else if (track
->a_formattag
== mmioFOURCC('W', 'V', 'P', 'K'))
2179 { /* do nothing, still works */ }
2180 else if (!track
->ms_compat
|| (track
->private_size
< sizeof(WAVEFORMATEX
)))
2182 free_sh_audio(demuxer
, track
->tnum
);
2189 /** \brief Parse the private data for VobSub subtitle tracks.
2191 This function tries to parse the private data for all VobSub tracks.
2192 The private data contains the normal text from the original .idx file.
2193 Things like the palette, subtitle dimensions and custom colors are
2196 \param demuxer The generic demuxer.
2199 demux_mkv_parse_vobsub_data (demuxer_t
*demuxer
)
2201 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2205 for (i
= 0; i
< mkv_d
->num_tracks
; i
++)
2207 track
= mkv_d
->tracks
[i
];
2208 if ((track
->type
!= MATROSKA_TRACK_SUBTITLE
) ||
2209 (track
->subtitle_type
!= MATROSKA_SUBTYPE_VOBSUB
))
2212 if (!demux_mkv_parse_idx (track
))
2214 free (track
->private_data
);
2215 track
->private_data
= NULL
;
2216 track
->private_size
= 0;
2222 demux_mkv_open_sub (demuxer_t
*demuxer
, mkv_track_t
*track
, int sid
)
2224 if (track
->subtitle_type
!= MATROSKA_SUBTYPE_UNKNOWN
)
2228 sh_sub_t
*sh
= new_sh_sub_sid(demuxer
, track
->tnum
, sid
);
2231 if (track
->subtitle_type
== MATROSKA_SUBTYPE_VOBSUB
)
2233 if (track
->subtitle_type
== MATROSKA_SUBTYPE_SSA
)
2235 size
= track
->private_size
;
2236 m
= demux_mkv_decode (track
,track
->private_data
,&buffer
,&size
,2);
2239 free (track
->private_data
);
2240 track
->private_data
= buffer
;
2241 track
->private_size
= size
;
2243 sh
->extradata
=malloc(track
->private_size
);
2244 memcpy (sh
->extradata
, track
->private_data
,
2245 track
->private_size
);
2246 sh
->extradata_len
= track
->private_size
;
2250 mp_msg (MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_MKV_SubtitleTypeNotSupported
,
2258 static void demux_mkv_seek (demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
);
2260 /** \brief Given a matroska track number and type, find the id that mplayer would ask for.
2261 * \param d The demuxer for which the subtitle id should be returned.
2262 * \param num The matroska track number we are looking up.
2263 * \param type The track type.
2265 static int demux_mkv_reverse_id(mkv_demuxer_t
*d
, int num
, int type
)
2269 for (i
=0, id
=0; i
< d
->num_tracks
; i
++)
2270 if (d
->tracks
[i
] != NULL
&& d
->tracks
[i
]->type
== type
) {
2271 if (d
->tracks
[i
]->tnum
== num
)
2280 demux_mkv_open (demuxer_t
*demuxer
)
2282 stream_t
*s
= demuxer
->stream
;
2283 mkv_demuxer_t
*mkv_d
;
2285 int i
, version
, cont
= 0;
2288 stream_seek(s
, s
->start_pos
);
2289 str
= ebml_read_header (s
, &version
);
2290 if (str
== NULL
|| strcmp (str
, "matroska") || version
> 2)
2292 mp_msg (MSGT_DEMUX
, MSGL_DBG2
, "[mkv] no head found\n");
2297 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] Found the head...\n");
2299 if (ebml_read_id (s
, NULL
) != MATROSKA_ID_SEGMENT
)
2301 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] but no segment :(\n");
2304 ebml_read_length (s
, NULL
); /* return bytes number until EOF */
2306 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] + a segment...\n");
2308 mkv_d
= calloc (1, sizeof (mkv_demuxer_t
));
2309 demuxer
->priv
= mkv_d
;
2310 mkv_d
->tc_scale
= 1000000;
2311 mkv_d
->segment_start
= stream_tell (s
);
2312 mkv_d
->parsed_cues
= malloc (sizeof (off_t
));
2313 mkv_d
->parsed_seekhead
= malloc (sizeof (off_t
));
2317 switch (ebml_read_id (s
, NULL
))
2319 case MATROSKA_ID_INFO
:
2320 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] |+ segment information...\n");
2321 cont
= demux_mkv_read_info (demuxer
);
2324 case MATROSKA_ID_TRACKS
:
2325 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] |+ segment tracks...\n");
2326 cont
= demux_mkv_read_tracks (demuxer
);
2329 case MATROSKA_ID_CUES
:
2330 cont
= demux_mkv_read_cues (demuxer
);
2333 case MATROSKA_ID_TAGS
:
2334 cont
= demux_mkv_read_tags (demuxer
);
2337 case MATROSKA_ID_SEEKHEAD
:
2338 cont
= demux_mkv_read_seekhead (demuxer
);
2341 case MATROSKA_ID_CHAPTERS
:
2342 cont
= demux_mkv_read_chapters (demuxer
);
2345 case MATROSKA_ID_ATTACHMENTS
:
2346 cont
= demux_mkv_read_attachments (demuxer
);
2349 case MATROSKA_ID_CLUSTER
:
2352 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] |+ found cluster, headers are "
2353 "parsed completely :)\n");
2354 /* get the first cluster timecode */
2356 l
= ebml_read_length (s
, NULL
);
2357 while (ebml_read_id (s
, NULL
) != MATROSKA_ID_CLUSTERTIMECODE
)
2359 ebml_read_skip (s
, NULL
);
2360 if (stream_tell (s
) >= p
+ l
)
2363 if (stream_tell (s
) < p
+ l
)
2365 uint64_t num
= ebml_read_uint (s
, NULL
);
2366 if (num
== EBML_UINT_INVALID
)
2368 mkv_d
->first_tc
= num
* mkv_d
->tc_scale
/ 1000000.0;
2369 mkv_d
->has_first_tc
= 1;
2371 stream_seek (s
, p
- 4);
2379 ebml_read_skip (s
, NULL
);
2384 display_create_tracks (demuxer
);
2386 /* select video track */
2388 if (demuxer
->video
->id
== -1) /* automatically select a video track */
2390 /* search for a video track that has the 'default' flag set */
2391 for (i
=0; i
<mkv_d
->num_tracks
; i
++)
2392 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_VIDEO
2393 && mkv_d
->tracks
[i
]->default_track
)
2395 track
= mkv_d
->tracks
[i
];
2400 /* no track has the 'default' flag set */
2401 /* let's take the first video track */
2402 for (i
=0; i
<mkv_d
->num_tracks
; i
++)
2403 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_VIDEO
)
2405 track
= mkv_d
->tracks
[i
];
2409 else if (demuxer
->video
->id
!= -2) /* -2 = no video at all */
2410 track
= demux_mkv_find_track_by_num (mkv_d
, demuxer
->video
->id
,
2411 MATROSKA_TRACK_VIDEO
);
2413 if (track
&& demuxer
->v_streams
[track
->tnum
])
2415 mp_msg (MSGT_DEMUX
, MSGL_INFO
,
2416 MSGTR_MPDEMUX_MKV_WillPlayVideoTrack
, track
->tnum
);
2417 demuxer
->video
->id
= track
->tnum
;
2418 demuxer
->video
->sh
= demuxer
->v_streams
[track
->tnum
];
2422 mp_msg (MSGT_DEMUX
, MSGL_INFO
, MSGTR_MPDEMUX_MKV_NoVideoTrackFound
);
2423 demuxer
->video
->id
= -2;
2426 /* select audio track */
2428 if (demuxer
->audio
->id
== -1) /* automatically select an audio track */
2430 /* check if the user specified an audio language */
2431 if (audio_lang
!= NULL
)
2432 track
= demux_mkv_find_track_by_language(mkv_d
, audio_lang
,
2433 MATROSKA_TRACK_AUDIO
);
2435 /* no audio language specified, or language not found */
2436 /* search for an audio track that has the 'default' flag set */
2437 for (i
=0; i
< mkv_d
->num_tracks
; i
++)
2438 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_AUDIO
2439 && mkv_d
->tracks
[i
]->default_track
)
2441 track
= mkv_d
->tracks
[i
];
2446 /* no track has the 'default' flag set */
2447 /* let's take the first audio track */
2448 for (i
=0; i
< mkv_d
->num_tracks
; i
++)
2449 if (mkv_d
->tracks
[i
]->type
== MATROSKA_TRACK_AUDIO
)
2451 track
= mkv_d
->tracks
[i
];
2455 else if (demuxer
->audio
->id
!= -2) /* -2 = no audio at all */
2456 track
= demux_mkv_find_track_by_num (mkv_d
, demuxer
->audio
->id
,
2457 MATROSKA_TRACK_AUDIO
);
2458 if (track
&& demuxer
->a_streams
[track
->tnum
])
2460 demuxer
->audio
->id
= track
->tnum
;
2461 demuxer
->audio
->sh
= demuxer
->a_streams
[track
->tnum
];
2465 mp_msg (MSGT_DEMUX
, MSGL_INFO
, MSGTR_MPDEMUX_MKV_NoAudioTrackFound
);
2466 demuxer
->audio
->id
= -2;
2470 if(demuxer
->audio
->id
!= -2)
2471 for (i
=0; i
< mkv_d
->num_tracks
; i
++)
2473 if(mkv_d
->tracks
[i
]->type
!= MATROSKA_TRACK_AUDIO
)
2475 if(demuxer
->a_streams
[track
->tnum
])
2478 if(mkv_d
->last_aid
== MAX_A_STREAMS
)
2483 demux_mkv_parse_vobsub_data (demuxer
);
2485 /* DO NOT automatically select a subtitle track and behave like DVD */
2486 /* playback: only show subtitles if the user explicitely wants them. */
2488 if (demuxer
->sub
->id
>= 0)
2489 track
= demux_mkv_find_track_by_num (mkv_d
, demuxer
->sub
->id
,
2490 MATROSKA_TRACK_SUBTITLE
);
2491 else if (dvdsub_lang
!= NULL
)
2492 track
= demux_mkv_find_track_by_language (mkv_d
, dvdsub_lang
,
2493 MATROSKA_TRACK_SUBTITLE
);
2497 mp_msg (MSGT_DEMUX
, MSGL_INFO
,
2498 MSGTR_MPDEMUX_MKV_WillDisplaySubtitleTrack
, track
->tnum
);
2499 dvdsub_id
= demux_mkv_reverse_id(mkv_d
, track
->tnum
, MATROSKA_TRACK_SUBTITLE
);
2500 demuxer
->sub
->id
= track
->tnum
;
2501 demuxer
->sub
->sh
= demuxer
->s_streams
[track
->tnum
];
2504 demuxer
->sub
->id
= -2;
2506 if (demuxer
->chapters
)
2508 for (i
=0; i
< (int)demuxer
->num_chapters
; i
++)
2510 demuxer
->chapters
[i
].start
-= mkv_d
->first_tc
;
2511 demuxer
->chapters
[i
].end
-= mkv_d
->first_tc
;
2513 if (dvd_last_chapter
> 0 && dvd_last_chapter
<= demuxer
->num_chapters
)
2515 if (demuxer
->chapters
[dvd_last_chapter
-1].end
!= 0)
2516 mkv_d
->stop_timecode
= demuxer
->chapters
[dvd_last_chapter
-1].end
;
2517 else if (dvd_last_chapter
+ 1 <= demuxer
->num_chapters
)
2518 mkv_d
->stop_timecode
= demuxer
->chapters
[dvd_last_chapter
].start
;
2522 if (s
->end_pos
== 0 || (mkv_d
->indexes
== NULL
&& index_mode
< 0))
2523 demuxer
->seekable
= 0;
2526 demuxer
->movi_start
= s
->start_pos
;
2527 demuxer
->movi_end
= s
->end_pos
;
2528 demuxer
->seekable
= 1;
2529 if (demuxer
->chapters
&& dvd_chapter
>1 && dvd_chapter
<=demuxer
->num_chapters
)
2531 if (!mkv_d
->has_first_tc
)
2533 mkv_d
->first_tc
= 0;
2534 mkv_d
->has_first_tc
= 1;
2536 demux_mkv_seek (demuxer
,
2537 demuxer
->chapters
[dvd_chapter
-1].start
/1000.0, 0.0, 1);
2541 return DEMUXER_TYPE_MATROSKA
;
2545 demux_close_mkv (demuxer_t
*demuxer
)
2547 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2552 free_cached_dps (demuxer
);
2555 for (i
=0; i
<mkv_d
->num_tracks
; i
++)
2556 demux_mkv_free_trackentry(mkv_d
->tracks
[i
]);
2557 free (mkv_d
->tracks
);
2560 free (mkv_d
->indexes
);
2561 if (mkv_d
->cluster_positions
)
2562 free (mkv_d
->cluster_positions
);
2563 if (mkv_d
->parsed_cues
)
2564 free (mkv_d
->parsed_cues
);
2565 if (mkv_d
->parsed_seekhead
)
2566 free (mkv_d
->parsed_seekhead
);
2572 demux_mkv_read_block_lacing (uint8_t *buffer
, uint64_t *size
,
2573 uint8_t *laces
, uint32_t **all_lace_sizes
)
2575 uint32_t total
= 0, *lace_size
;
2579 *all_lace_sizes
= NULL
;
2585 switch ((flags
& 0x06) >> 1)
2587 case 0: /* no lacing */
2589 lace_size
= calloc(*laces
, sizeof(uint32_t));
2590 lace_size
[0] = *size
;
2593 case 1: /* xiph lacing */
2594 case 2: /* fixed-size lacing */
2595 case 3: /* EBML lacing */
2599 lace_size
= calloc(*laces
, sizeof(uint32_t));
2601 switch ((flags
& 0x06) >> 1)
2603 case 1: /* xiph lacing */
2604 for (i
=0; i
< *laces
-1; i
++)
2609 lace_size
[i
] += *buffer
;
2611 } while (*buffer
++ == 0xFF);
2612 total
+= lace_size
[i
];
2614 lace_size
[i
] = *size
- total
;
2617 case 2: /* fixed-size lacing */
2618 for (i
=0; i
< *laces
; i
++)
2619 lace_size
[i
] = *size
/ *laces
;
2622 case 3: /* EBML lacing */
2625 uint64_t num
= ebml_read_vlen_uint (buffer
, &l
);
2626 if (num
== EBML_UINT_INVALID
) {
2633 total
= lace_size
[0] = num
;
2634 for (i
=1; i
< *laces
-1; i
++)
2637 snum
= ebml_read_vlen_int (buffer
, &l
);
2638 if (snum
== EBML_INT_INVALID
) {
2644 lace_size
[i
] = lace_size
[i
-1] + snum
;
2645 total
+= lace_size
[i
];
2647 lace_size
[i
] = *size
- total
;
2653 *all_lace_sizes
= lace_size
;
2658 handle_subtitles(demuxer_t
*demuxer
, mkv_track_t
*track
, char *block
,
2659 int64_t size
, uint64_t block_duration
, uint64_t timecode
)
2663 if (block_duration
== 0)
2665 mp_msg (MSGT_DEMUX
, MSGL_WARN
,
2666 MSGTR_MPDEMUX_MKV_NoBlockDurationForSubtitleTrackFound
);
2671 dp
= new_demux_packet(size
);
2672 memcpy(dp
->buffer
, block
, size
);
2673 dp
->pts
= timecode
/ 1000.0f
;
2674 dp
->endpts
= (timecode
+ block_duration
) / 1000.0f
;
2675 ds_add_packet(demuxer
->sub
, dp
);
2678 // Taken from demux_real.c. Thanks to the original developpers :)
2679 #define SKIP_BITS(n) buffer <<= n
2680 #define SHOW_BITS(n) ((buffer) >> (32 - (n)))
2682 static float real_fix_timestamp(mkv_track_t
*track
, unsigned char *s
,
2685 uint32_t buffer
= (s
[0] << 24) + (s
[1] << 16) + (s
[2] << 8) + s
[3];
2690 if (!strcmp(track
->codec_id
, MKV_V_REALV30
) ||
2691 !strcmp(track
->codec_id
, MKV_V_REALV40
)) {
2693 if (!strcmp(track
->codec_id
, MKV_V_REALV30
)) {
2695 pict_type
= SHOW_BITS(2);
2699 pict_type
= SHOW_BITS(2);
2700 SKIP_BITS(2 + 7 + 3);
2702 kf
= SHOW_BITS(13); // kf= 2*SHOW_BITS(12);
2704 if (pict_type
<= 1) {
2705 // I frame, sync timestamps:
2706 track
->rv_kf_base
= timestamp
- kf
;
2707 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "\nTS: base=%08X\n", track
->rv_kf_base
);
2710 // P/B frame, merge timestamps:
2711 int tmp
= timestamp
- track
->rv_kf_base
;
2712 kf
|= tmp
& (~0x1fff); // combine with packet timestamp
2713 if (kf
< (tmp
- 4096)) // workaround wrap-around problems
2715 else if (kf
> (tmp
+ 4096))
2717 kf
+= track
->rv_kf_base
;
2719 if (pict_type
!= 3) { // P || I frame -> swap timestamps
2721 kf
= track
->rv_kf_pts
;
2722 track
->rv_kf_pts
= tmp
;
2724 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "\nTS: %08X -> %08X (%04X) %d %02X %02X %02X "
2725 "%02X %5d\n", timestamp
, kf
, orig_kf
, pict_type
, s
[0], s
[1], s
[2],
2726 s
[3], kf
- (int)(1000.0 * track
->rv_pts
));
2728 v_pts
= kf
* 0.001f
;
2729 track
->rv_pts
= v_pts
;
2735 handle_realvideo (demuxer_t
*demuxer
, mkv_track_t
*track
, uint8_t *buffer
,
2736 uint32_t size
, int block_bref
)
2738 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2740 uint32_t timestamp
= mkv_d
->last_pts
* 1000;
2744 #ifdef WORDS_BIGENDIAN
2750 isize
= --size
- (chunks
+1)*8;
2751 dp
= new_demux_packet (REALHEADER_SIZE
+ size
);
2752 memcpy (dp
->buffer
+ REALHEADER_SIZE
, buffer
+ (chunks
+1)*8, isize
);
2753 #ifdef WORDS_BIGENDIAN
2754 p
= (uint8_t *)(dp
->buffer
+ REALHEADER_SIZE
+ isize
);
2755 for (i
= 0; i
<(chunks
+1)*8; i
+=4) {
2756 p
[i
] = *((uint8_t *)buffer
+i
+3);
2757 p
[i
+1] = *((uint8_t *)buffer
+i
+2);
2758 p
[i
+2] = *((uint8_t *)buffer
+i
+1);
2759 p
[i
+3] = *((uint8_t *)buffer
+i
);
2762 memcpy (dp
->buffer
+ REALHEADER_SIZE
+ isize
, buffer
, (chunks
+1)*8);
2766 *hdr
++ = chunks
; // number of chunks
2767 *hdr
++ = timestamp
; // timestamp from packet header
2768 *hdr
++ = isize
; // length of actual data
2769 *hdr
++ = REALHEADER_SIZE
+ isize
; // offset to chunk offset array
2771 if (mkv_d
->v_skip_to_keyframe
)
2773 dp
->pts
= mkv_d
->last_pts
;
2774 track
->rv_kf_base
= 0;
2775 track
->rv_kf_pts
= timestamp
;
2778 dp
->pts
= real_fix_timestamp (track
, dp
->buffer
+ REALHEADER_SIZE
,
2780 dp
->pos
= demuxer
->filepos
;
2781 dp
->flags
= block_bref
? 0 : 0x10;
2783 ds_add_packet(demuxer
->video
, dp
);
2787 handle_realaudio (demuxer_t
*demuxer
, mkv_track_t
*track
, uint8_t *buffer
,
2788 uint32_t size
, int block_bref
)
2790 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2791 int sps
= track
->sub_packet_size
;
2792 int sph
= track
->sub_packet_h
;
2793 int cfs
= track
->coded_framesize
;
2794 int w
= track
->audiopk_size
;
2795 int spc
= track
->sub_packet_cnt
;
2799 if ((track
->a_formattag
== mmioFOURCC('2', '8', '_', '8')) ||
2800 (track
->a_formattag
== mmioFOURCC('c', 'o', 'o', 'k')) ||
2801 (track
->a_formattag
== mmioFOURCC('a', 't', 'r', 'c')) ||
2802 (track
->a_formattag
== mmioFOURCC('s', 'i', 'p', 'r')))
2805 // spc = track->sub_packet_cnt = 0;
2806 switch (track
->a_formattag
) {
2807 case mmioFOURCC('2', '8', '_', '8'):
2808 for (x
= 0; x
< sph
/ 2; x
++)
2809 memcpy(track
->audio_buf
+ x
* 2 * w
+ spc
* cfs
, buffer
+ cfs
* x
, cfs
);
2811 case mmioFOURCC('c', 'o', 'o', 'k'):
2812 case mmioFOURCC('a', 't', 'r', 'c'):
2813 for (x
= 0; x
< w
/ sps
; x
++)
2814 memcpy(track
->audio_buf
+ sps
* (sph
* x
+ ((sph
+ 1) / 2) * (spc
& 1) + (spc
>> 1)), buffer
+ sps
* x
, sps
);
2816 case mmioFOURCC('s', 'i', 'p', 'r'):
2817 memcpy(track
->audio_buf
+ spc
* w
, buffer
, w
);
2821 int bs
= sph
* w
* 2 / 96; // nibbles per subpacket
2822 // Perform reordering
2823 for(n
=0; n
< 38; n
++)
2826 int i
= bs
* sipr_swaps
[n
][0];
2827 int o
= bs
* sipr_swaps
[n
][1];
2828 // swap nibbles of block 'i' with 'o' TODO: optimize
2829 for(j
= 0;j
< bs
; j
++)
2831 int x
= (i
& 1) ? (track
->audio_buf
[i
>> 1] >> 4) : (track
->audio_buf
[i
>> 1] & 0x0F);
2832 int y
= (o
& 1) ? (track
->audio_buf
[o
>> 1] >> 4) : (track
->audio_buf
[o
>> 1] & 0x0F);
2834 track
->audio_buf
[o
>> 1] = (track
->audio_buf
[o
>> 1] & 0x0F) | (x
<< 4);
2836 track
->audio_buf
[o
>> 1] = (track
->audio_buf
[o
>> 1] & 0xF0) | x
;
2838 track
->audio_buf
[i
>> 1] = (track
->audio_buf
[i
>> 1] & 0x0F) | (y
<< 4);
2840 track
->audio_buf
[i
>> 1] = (track
->audio_buf
[i
>> 1] & 0xF0) | y
;
2847 track
->audio_timestamp
[track
->sub_packet_cnt
] = (track
->ra_pts
== mkv_d
->last_pts
) ? 0 : (mkv_d
->last_pts
);
2848 track
->ra_pts
= mkv_d
->last_pts
;
2849 if (track
->sub_packet_cnt
== 0)
2850 track
->audio_filepos
= demuxer
->filepos
;
2851 if (++(track
->sub_packet_cnt
) == sph
)
2853 int apk_usize
= ((WAVEFORMATEX
*)((sh_audio_t
*)demuxer
->audio
->sh
)->wf
)->nBlockAlign
;
2854 track
->sub_packet_cnt
= 0;
2855 // Release all the audio packets
2856 for (x
= 0; x
< sph
*w
/apk_usize
; x
++)
2858 dp
= new_demux_packet(apk_usize
);
2859 memcpy(dp
->buffer
, track
->audio_buf
+ x
* apk_usize
, apk_usize
);
2860 /* Put timestamp only on packets that correspond to original audio packets in file */
2861 dp
->pts
= (x
* apk_usize
% w
) ? 0 : track
->audio_timestamp
[x
* apk_usize
/ w
];
2862 dp
->pos
= track
->audio_filepos
; // all equal
2863 dp
->flags
= x
? 0 : 0x10; // Mark first packet as keyframe
2864 ds_add_packet(demuxer
->audio
, dp
);
2867 } else { // Not a codec that require reordering
2868 dp
= new_demux_packet (size
);
2869 memcpy(dp
->buffer
, buffer
, size
);
2870 if (track
->ra_pts
== mkv_d
->last_pts
&& !mkv_d
->a_skip_to_keyframe
)
2873 dp
->pts
= mkv_d
->last_pts
;
2874 track
->ra_pts
= mkv_d
->last_pts
;
2876 dp
->pos
= demuxer
->filepos
;
2877 dp
->flags
= block_bref
? 0 : 0x10;
2878 ds_add_packet (demuxer
->audio
, dp
);
2882 /** Reorder timecodes and add cached demux packets to the queues.
2884 * Timecode reordering is needed if a video track contains B frames that
2885 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2886 * MPlayer doesn't like timestamps in display order. This function adjusts
2887 * the timestamp of cached frames (which are exactly one I/P frame followed
2888 * by one or more B frames) so that they are in coding order again.
2890 * Example: The track with 25 FPS contains four frames with the timecodes
2891 * I at 0ms, P at 120ms, B at 40ms and B at 80ms. As soon as the next I
2892 * or P frame arrives these timecodes can be changed to I at 0ms, P at 40ms,
2893 * B at 80ms and B at 120ms.
2895 * This works for simple H.264 B-frame pyramids, but not for arbitrary orders.
2897 * \param demuxer The Matroska demuxer struct for this instance.
2898 * \param track The track structure whose cache should be handled.
2901 flush_cached_dps (demuxer_t
*demuxer
, mkv_track_t
*track
)
2905 if (track
->num_cached_dps
== 0)
2910 for (i
= 1; i
< track
->num_cached_dps
; i
++)
2911 if (track
->cached_dps
[i
- 1]->pts
> track
->cached_dps
[i
]->pts
) {
2912 float tmp_pts
= track
->cached_dps
[i
- 1]->pts
;
2913 track
->cached_dps
[i
- 1]->pts
= track
->cached_dps
[i
]->pts
;
2914 track
->cached_dps
[i
]->pts
= tmp_pts
;
2919 for (i
= 0; i
< track
->num_cached_dps
; i
++)
2920 ds_add_packet (demuxer
->video
, track
->cached_dps
[i
]);
2921 track
->num_cached_dps
= 0;
2924 /** Cache video frames if timecodes have to be reordered.
2926 * Timecode reordering is needed if a video track contains B frames that
2927 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
2928 * This function takes in a Matroska block read from the file, allocates a
2929 * demux packet for it, fills in its values, allocates space for storing
2930 * pointers to the cached demux packets and adds the packet to it. If
2931 * the packet contains an I or a P frame then ::flush_cached_dps is called
2932 * in order to send the old cached frames downstream.
2934 * \param demuxer The Matroska demuxer struct for this instance.
2935 * \param track The packet is meant for this track.
2936 * \param buffer The actual frame contents.
2937 * \param size The frame size in bytes.
2938 * \param block_bref A relative timecode (backward reference). If it is \c 0
2939 * then the frame is an I frame.
2940 * \param block_fref A relative timecode (forward reference). If it is \c 0
2941 * then the frame is either an I frame or a P frame depending on the value
2942 * of \a block_bref. Otherwise it's a B frame.
2945 handle_video_bframes (demuxer_t
*demuxer
, mkv_track_t
*track
, uint8_t *buffer
,
2946 uint32_t size
, int block_bref
, int block_fref
)
2948 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2951 dp
= new_demux_packet (size
);
2952 memcpy(dp
->buffer
, buffer
, size
);
2953 dp
->pos
= demuxer
->filepos
;
2954 dp
->pts
= mkv_d
->last_pts
;
2955 if ((track
->num_cached_dps
> 0) && (dp
->pts
< track
->max_pts
))
2957 if (block_fref
== 0) /* I or P frame */
2958 flush_cached_dps (demuxer
, track
);
2959 if (block_bref
!= 0) /* I frame, don't cache it */
2961 if ((track
->num_cached_dps
+ 1) > track
->num_allocated_dps
)
2963 track
->cached_dps
= (demux_packet_t
**)
2964 realloc(track
->cached_dps
, (track
->num_cached_dps
+ 10) *
2965 sizeof(demux_packet_t
*));
2966 track
->num_allocated_dps
+= 10;
2968 track
->cached_dps
[track
->num_cached_dps
] = dp
;
2969 track
->num_cached_dps
++;
2970 if (dp
->pts
> track
->max_pts
)
2971 track
->max_pts
= dp
->pts
;
2975 handle_block (demuxer_t
*demuxer
, uint8_t *block
, uint64_t length
,
2976 uint64_t block_duration
, int64_t block_bref
, int64_t block_fref
, uint8_t simpleblock
)
2978 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
2979 mkv_track_t
*track
= NULL
;
2980 demux_stream_t
*ds
= NULL
;
2981 uint64_t old_length
;
2983 uint32_t *lace_size
;
2984 uint8_t laces
, flags
;
2985 int i
, num
, tmp
, use_this_block
= 1;
2989 /* first byte(s): track num */
2990 num
= ebml_read_vlen_uint (block
, &tmp
);
2992 /* time (relative to cluster time) */
2993 time
= block
[0] << 8 | block
[1];
2996 old_length
= length
;
2998 if (demux_mkv_read_block_lacing (block
, &length
, &laces
, &lace_size
))
3000 block
+= old_length
- length
;
3002 tc
= ((time
*mkv_d
->tc_scale
+mkv_d
->cluster_tc
) /1000000.0 - mkv_d
->first_tc
);
3005 if (mkv_d
->stop_timecode
> 0 && tc
> mkv_d
->stop_timecode
) {
3009 current_pts
= tc
/ 1000.0;
3011 for (i
=0; i
<mkv_d
->num_tracks
; i
++)
3012 if (mkv_d
->tracks
[i
]->tnum
== num
) {
3013 track
= mkv_d
->tracks
[i
];
3021 if (num
== demuxer
->audio
->id
)
3023 ds
= demuxer
->audio
;
3025 if (mkv_d
->a_skip_to_keyframe
)
3029 if (!(flags
&0x80)) /*current frame isn't a keyframe*/
3032 else if (block_bref
!= 0)
3035 else if (mkv_d
->v_skip_to_keyframe
)
3038 if (track
->fix_i_bps
&& use_this_block
)
3040 sh_audio_t
*sh
= (sh_audio_t
*) ds
->sh
;
3042 if (block_duration
!= 0)
3044 sh
->i_bps
= length
* 1000 / block_duration
;
3045 track
->fix_i_bps
= 0;
3047 else if (track
->qt_last_a_pts
== 0.0)
3048 track
->qt_last_a_pts
= current_pts
;
3049 else if(track
->qt_last_a_pts
!= current_pts
)
3051 sh
->i_bps
= length
/ (current_pts
- track
->qt_last_a_pts
);
3052 track
->fix_i_bps
= 0;
3056 else if (tc
< mkv_d
->skip_to_timecode
)
3058 else if (num
== demuxer
->video
->id
)
3060 ds
= demuxer
->video
;
3061 if (mkv_d
->v_skip_to_keyframe
)
3065 if (!(flags
&0x80)) /*current frame isn't a keyframe*/
3068 else if (block_bref
!= 0 || block_fref
!= 0)
3072 else if (num
== demuxer
->sub
->id
)
3075 if (track
->subtitle_type
!= MATROSKA_SUBTYPE_VOBSUB
)
3077 if (!mkv_d
->v_skip_to_keyframe
)
3078 handle_subtitles (demuxer
, track
, block
, length
,
3079 block_duration
, tc
);
3088 mkv_d
->last_pts
= current_pts
;
3089 mkv_d
->last_filepos
= demuxer
->filepos
;
3091 for (i
=0; i
< laces
; i
++)
3093 if (ds
== demuxer
->video
&& track
->realmedia
)
3094 handle_realvideo (demuxer
, track
, block
, lace_size
[i
], block_bref
);
3095 else if (ds
== demuxer
->audio
&& track
->realmedia
)
3096 handle_realaudio (demuxer
, track
, block
, lace_size
[i
], block_bref
);
3097 else if (ds
== demuxer
->video
&& track
->reorder_timecodes
)
3098 handle_video_bframes (demuxer
, track
, block
, lace_size
[i
],
3099 block_bref
, block_fref
);
3102 int modified
, size
= lace_size
[i
];
3105 modified
= demux_mkv_decode (track
, block
, &buffer
, &size
, 1);
3108 dp
= new_demux_packet (size
);
3109 memcpy (dp
->buffer
, buffer
, size
);
3112 dp
->flags
= (block_bref
== 0 && block_fref
== 0) ? 0x10 : 0;
3113 /* If default_duration is 0, assume no pts value is known
3114 * for packets after the first one (rather than all pts
3115 * values being the same) */
3116 if (i
== 0 || track
->default_duration
)
3117 dp
->pts
= mkv_d
->last_pts
+ i
* track
->default_duration
;
3118 ds_add_packet (ds
, dp
);
3121 block
+= lace_size
[i
];
3124 if (ds
== demuxer
->video
)
3126 mkv_d
->v_skip_to_keyframe
= 0;
3127 mkv_d
->skip_to_timecode
= 0;
3129 else if (ds
== demuxer
->audio
)
3130 mkv_d
->a_skip_to_keyframe
= 0;
3141 demux_mkv_fill_buffer (demuxer_t
*demuxer
, demux_stream_t
*ds
)
3143 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
3144 stream_t
*s
= demuxer
->stream
;
3150 while (mkv_d
->cluster_size
> 0)
3152 uint64_t block_duration
= 0, block_length
= 0;
3153 int64_t block_bref
= 0, block_fref
= 0;
3154 uint8_t *block
= NULL
;
3156 while (mkv_d
->blockgroup_size
> 0)
3158 switch (ebml_read_id (s
, &il
))
3160 case MATROSKA_ID_BLOCKDURATION
:
3162 block_duration
= ebml_read_uint (s
, &l
);
3163 if (block_duration
== EBML_UINT_INVALID
) {
3167 block_duration
*= mkv_d
->tc_scale
/ 1000000.0;
3171 case MATROSKA_ID_BLOCK
:
3172 block_length
= ebml_read_length (s
, &tmp
);
3174 if (block_length
> SIZE_MAX
- LZO_INPUT_PADDING
) return 0;
3175 block
= malloc (block_length
+ LZO_INPUT_PADDING
);
3176 demuxer
->filepos
= stream_tell (s
);
3177 if (stream_read (s
,block
,block_length
) != (int) block_length
)
3182 l
= tmp
+ block_length
;
3185 case MATROSKA_ID_REFERENCEBLOCK
:
3187 int64_t num
= ebml_read_int (s
, &l
);
3188 if (num
== EBML_INT_INVALID
) {
3199 case EBML_ID_INVALID
:
3204 ebml_read_skip (s
, &l
);
3207 mkv_d
->blockgroup_size
-= l
+ il
;
3208 mkv_d
->cluster_size
-= l
+ il
;
3213 int res
= handle_block (demuxer
, block
, block_length
,
3214 block_duration
, block_bref
, block_fref
, 0);
3222 if (mkv_d
->cluster_size
> 0)
3224 switch (ebml_read_id (s
, &il
))
3226 case MATROSKA_ID_CLUSTERTIMECODE
:
3228 uint64_t num
= ebml_read_uint (s
, &l
);
3229 if (num
== EBML_UINT_INVALID
)
3231 if (!mkv_d
->has_first_tc
)
3233 mkv_d
->first_tc
= num
* mkv_d
->tc_scale
/ 1000000.0;
3234 mkv_d
->has_first_tc
= 1;
3236 mkv_d
->cluster_tc
= num
* mkv_d
->tc_scale
;
3240 case MATROSKA_ID_BLOCKGROUP
:
3241 mkv_d
->blockgroup_size
= ebml_read_length (s
, &tmp
);
3245 case MATROSKA_ID_SIMPLEBLOCK
:
3248 block_length
= ebml_read_length (s
, &tmp
);
3249 block
= malloc (block_length
);
3250 demuxer
->filepos
= stream_tell (s
);
3251 if (stream_read (s
,block
,block_length
) != (int) block_length
)
3256 l
= tmp
+ block_length
;
3257 res
= handle_block (demuxer
, block
, block_length
,
3258 block_duration
, block_bref
, block_fref
, 1);
3260 mkv_d
->cluster_size
-= l
+ il
;
3265 else mkv_d
->cluster_size
+= l
+ il
;
3268 case EBML_ID_INVALID
:
3272 ebml_read_skip (s
, &l
);
3275 mkv_d
->cluster_size
-= l
+ il
;
3279 if (ebml_read_id (s
, &il
) != MATROSKA_ID_CLUSTER
)
3281 add_cluster_position(mkv_d
, stream_tell(s
)-il
);
3282 mkv_d
->cluster_size
= ebml_read_length (s
, NULL
);
3289 demux_mkv_seek (demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
)
3291 free_cached_dps (demuxer
);
3292 if (!(flags
& 2)) /* time in secs */
3294 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
3295 stream_t
*s
= demuxer
->stream
;
3296 int64_t target_timecode
= 0, diff
, min_diff
=0xFFFFFFFFFFFFFFFLL
;
3299 if (!(flags
& 1)) /* relative seek */
3300 target_timecode
= (int64_t) (mkv_d
->last_pts
* 1000.0);
3301 target_timecode
+= (int64_t)(rel_seek_secs
* 1000.0);
3302 if (target_timecode
< 0)
3303 target_timecode
= 0;
3305 if (mkv_d
->indexes
== NULL
) /* no index was found */
3307 uint64_t target_filepos
, cluster_pos
, max_pos
;
3309 target_filepos
= (uint64_t) (target_timecode
* mkv_d
->last_filepos
3310 / (mkv_d
->last_pts
* 1000.0));
3312 max_pos
= mkv_d
->cluster_positions
[mkv_d
->num_cluster_pos
-1];
3313 if (target_filepos
> max_pos
)
3315 if ((off_t
) max_pos
> stream_tell (s
))
3316 stream_seek (s
, max_pos
);
3318 stream_seek (s
, stream_tell (s
) + mkv_d
->cluster_size
);
3319 /* parse all the clusters upto target_filepos */
3320 while (!s
->eof
&& stream_tell(s
) < (off_t
) target_filepos
)
3322 switch (ebml_read_id (s
, &i
))
3324 case MATROSKA_ID_CLUSTER
:
3325 add_cluster_position(mkv_d
, (uint64_t) stream_tell(s
)-i
);
3328 case MATROSKA_ID_CUES
:
3329 demux_mkv_read_cues (demuxer
);
3332 ebml_read_skip (s
, NULL
);
3338 if (mkv_d
->indexes
== NULL
)
3340 cluster_pos
= mkv_d
->cluster_positions
[0];
3341 /* Let's find the nearest cluster */
3342 for (i
=0; i
< mkv_d
->num_cluster_pos
; i
++)
3344 diff
= mkv_d
->cluster_positions
[i
] - target_filepos
;
3345 if (rel_seek_secs
< 0 && diff
< 0 && -diff
< min_diff
)
3347 cluster_pos
= mkv_d
->cluster_positions
[i
];
3350 else if (rel_seek_secs
> 0
3351 && (diff
< 0 ? -1 * diff
: diff
) < min_diff
)
3353 cluster_pos
= mkv_d
->cluster_positions
[i
];
3354 min_diff
= diff
< 0 ? -1 * diff
: diff
;
3357 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
3358 stream_seek (s
, cluster_pos
);
3363 mkv_index_t
*index
= NULL
;
3364 int seek_id
= (demuxer
->video
->id
< 0) ? demuxer
->audio
->id
: demuxer
->video
->id
;
3366 /* let's find the entry in the indexes with the smallest */
3367 /* difference to the wanted timecode. */
3368 for (i
=0; i
< mkv_d
->num_indexes
; i
++)
3369 if (mkv_d
->indexes
[i
].tnum
== seek_id
)
3371 diff
= target_timecode
+ mkv_d
->first_tc
-
3372 (int64_t) mkv_d
->indexes
[i
].timecode
* mkv_d
->tc_scale
/ 1000000.0;
3374 if ((flags
& 1 || target_timecode
<= mkv_d
->last_pts
*1000)) {
3375 // Absolute seek or seek backward: find the last index
3376 // position before target time
3377 if (diff
< 0 || diff
>= min_diff
)
3381 // Relative seek forward: find the first index position
3382 // after target time. If no such index exists, find last
3383 // position between current position and target time.
3385 if (min_diff
<= 0 && diff
<= min_diff
)
3388 else if (diff
>= FFMIN(target_timecode
- mkv_d
->last_pts
,
3393 index
= mkv_d
->indexes
+ i
;
3396 if (index
) /* We've found an entry. */
3398 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
3399 stream_seek (s
, index
->filepos
);
3403 if (demuxer
->video
->id
>= 0)
3404 mkv_d
->v_skip_to_keyframe
= 1;
3405 if (rel_seek_secs
> 0.0)
3406 mkv_d
->skip_to_timecode
= target_timecode
;
3407 mkv_d
->a_skip_to_keyframe
= 1;
3409 demux_mkv_fill_buffer(demuxer
, NULL
);
3411 else if ((demuxer
->movi_end
<= 0) || !(flags
& 1))
3412 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
3415 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
3416 stream_t
*s
= demuxer
->stream
;
3417 uint64_t target_filepos
;
3418 mkv_index_t
*index
= NULL
;
3421 if (mkv_d
->indexes
== NULL
) /* no index was found */
3423 mp_msg (MSGT_DEMUX
, MSGL_V
, "[mkv] seek unsupported flags\n");
3427 target_filepos
= (uint64_t)(demuxer
->movi_end
* rel_seek_secs
);
3428 for (i
=0; i
< mkv_d
->num_indexes
; i
++)
3429 if (mkv_d
->indexes
[i
].tnum
== demuxer
->video
->id
)
3430 if ((index
== NULL
) ||
3431 ((mkv_d
->indexes
[i
].filepos
>= target_filepos
) &&
3432 ((index
->filepos
< target_filepos
) ||
3433 (mkv_d
->indexes
[i
].filepos
< index
->filepos
))))
3434 index
= &mkv_d
->indexes
[i
];
3439 mkv_d
->cluster_size
= mkv_d
->blockgroup_size
= 0;
3440 stream_seek (s
, index
->filepos
);
3442 if (demuxer
->video
->id
>= 0)
3443 mkv_d
->v_skip_to_keyframe
= 1;
3444 mkv_d
->skip_to_timecode
= index
->timecode
;
3445 mkv_d
->a_skip_to_keyframe
= 1;
3447 demux_mkv_fill_buffer(demuxer
, NULL
);
3452 demux_mkv_control (demuxer_t
*demuxer
, int cmd
, void *arg
)
3454 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
3458 case DEMUXER_CTRL_GET_TIME_LENGTH
:
3459 if (mkv_d
->duration
== 0)
3460 return DEMUXER_CTRL_DONTKNOW
;
3462 *((double *)arg
) = (double)mkv_d
->duration
;
3463 return DEMUXER_CTRL_OK
;
3465 case DEMUXER_CTRL_GET_PERCENT_POS
:
3466 if (mkv_d
->duration
== 0)
3468 return DEMUXER_CTRL_DONTKNOW
;
3471 *((int *) arg
) = (int) (100 * mkv_d
->last_pts
/ mkv_d
->duration
);
3472 return DEMUXER_CTRL_OK
;
3474 case DEMUXER_CTRL_SWITCH_AUDIO
:
3475 if (demuxer
->audio
&& demuxer
->audio
->sh
) {
3476 sh_audio_t
*sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
3477 int aid
= *(int*)arg
;
3479 aid
= (sh
->aid
+ 1) % mkv_d
->last_aid
;
3480 if (aid
!= sh
->aid
) {
3481 mkv_track_t
*track
= demux_mkv_find_track_by_num (mkv_d
, aid
, MATROSKA_TRACK_AUDIO
);
3483 demuxer
->audio
->id
= track
->tnum
;
3484 sh
= demuxer
->a_streams
[demuxer
->audio
->id
];
3485 ds_free_packs(demuxer
->audio
);
3488 *(int*)arg
= sh
->aid
;
3491 return DEMUXER_CTRL_OK
;
3494 return DEMUXER_CTRL_NOTIMPL
;
3498 /** \brief Get the language code for a subtitle track.
3500 Retrieves the language code for a subtitle track if it is known.
3501 If the language code is "und" then do not copy it ("und" = "undefined").
3503 \param demuxer The demuxer to work on
3504 \param track_num The subtitle track number to get the language from
3505 \param lang Store the language here
3506 \param maxlen The maximum number of characters to copy into lang
3509 demux_mkv_get_sub_lang(demuxer_t
*demuxer
, int track_num
, char *lang
,
3512 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
3513 mkv_track_t
*track
= demux_mkv_find_track_by_num (mkv_d
, track_num
, MATROSKA_TRACK_SUBTITLE
);
3514 if (track
&& track
->language
&& strcmp(track
->language
, "und"))
3515 av_strlcpy(lang
, track
->language
, maxlen
);
3518 /** \brief Get the language code for an audio track.
3520 Retrieves the language code for an audio track if it is known.
3521 If the language code is "und" then do not copy it ("und" = "undefined").
3523 \param demuxer The demuxer to work on
3524 \param track_num The n'th audio track to get the language from
3525 \param lang Store the language here
3526 \param maxlen The maximum number of characters to copy into lang
3529 demux_mkv_get_audio_lang(demuxer_t
*demuxer
, int track_num
, char *lang
,
3532 mkv_demuxer_t
*mkv_d
= (mkv_demuxer_t
*) demuxer
->priv
;
3533 mkv_track_t
*track
= demux_mkv_find_track_by_num (mkv_d
, track_num
, MATROSKA_TRACK_AUDIO
);
3534 if (track
&& track
->language
&& strcmp(track
->language
, "und"))
3535 av_strlcpy(lang
, track
->language
, maxlen
);
3539 const demuxer_desc_t demuxer_desc_matroska
= {
3545 DEMUXER_TYPE_MATROSKA
,
3546 1, // safe autodetect
3548 demux_mkv_fill_buffer
,