2004-01-19 19:16:10 +00:00
|
|
|
/*
|
|
|
|
* native Matroska demuxer
|
|
|
|
* Written by Aurelien Jacobs <aurel@gnuage.org>
|
|
|
|
* Based on the one written by Ronald Bultje for gstreamer
|
|
|
|
* and on demux_mkv.cpp from Moritz Bunkus.
|
|
|
|
* Licence: GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
2006-06-04 22:41:27 +00:00
|
|
|
#include <inttypes.h>
|
2009-03-16 21:30:48 +00:00
|
|
|
#include <stdbool.h>
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-03-16 21:30:48 +00:00
|
|
|
#include "talloc.h"
|
2008-04-16 04:11:12 +00:00
|
|
|
#include "options.h"
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2004-01-19 19:16:10 +00:00
|
|
|
#include "demuxer.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
#include "ebml.h"
|
|
|
|
#include "matroska.h"
|
|
|
|
|
2006-11-05 13:53:27 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
|
|
|
|
2007-12-08 12:15:03 +00:00
|
|
|
#include "vobsub.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "subreader.h"
|
|
|
|
#include "libvo/sub.h"
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
#include "libass/ass.h"
|
|
|
|
#include "libass/ass_mp.h"
|
|
|
|
|
2007-03-16 14:55:41 +00:00
|
|
|
#include "libavutil/common.h"
|
|
|
|
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_QTX_CODECS
|
2007-03-15 22:34:45 +00:00
|
|
|
#include "loader/qtx/qtxsdk/components.h"
|
2004-01-19 19:16:10 +00:00
|
|
|
#endif
|
|
|
|
|
2009-02-07 15:23:55 +00:00
|
|
|
#if CONFIG_ZLIB
|
2004-01-19 19:16:10 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2007-01-31 23:28:34 +00:00
|
|
|
#include "libavutil/lzo.h"
|
2007-05-28 18:13:33 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2007-07-05 22:01:07 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2008-04-12 15:51:11 +00:00
|
|
|
static const unsigned char sipr_swaps[38][2]={
|
2006-04-03 19:41:17 +00:00
|
|
|
{0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
|
|
|
|
{13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
|
|
|
|
{25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
|
|
|
|
{42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
|
|
|
|
{77,80} };
|
|
|
|
|
|
|
|
// Map flavour to bytes per second
|
|
|
|
#define SIPR_FLAVORS 4
|
|
|
|
#define ATRC_FLAVORS 8
|
|
|
|
#define COOK_FLAVORS 34
|
2008-04-12 15:51:11 +00:00
|
|
|
static const int sipr_fl2bps[SIPR_FLAVORS] = {813, 1062, 625, 2000};
|
|
|
|
static const int atrc_fl2bps[ATRC_FLAVORS] = {8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100};
|
|
|
|
static const int cook_fl2bps[COOK_FLAVORS] = {
|
|
|
|
1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498,
|
|
|
|
4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584,
|
|
|
|
4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513,
|
|
|
|
12016, 16408, 22911, 33506};
|
2006-04-03 19:41:17 +00:00
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint32_t order, type, scope;
|
|
|
|
uint32_t comp_algo;
|
|
|
|
uint8_t *comp_settings;
|
|
|
|
int comp_settings_len;
|
|
|
|
} mkv_content_encoding_t;
|
|
|
|
|
|
|
|
typedef struct mkv_track
|
|
|
|
{
|
|
|
|
int tnum;
|
2006-09-02 19:39:24 +00:00
|
|
|
char *name;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
char *codec_id;
|
|
|
|
int ms_compat;
|
|
|
|
char *language;
|
|
|
|
|
|
|
|
int type;
|
|
|
|
|
|
|
|
uint32_t v_width, v_height, v_dwidth, v_dheight;
|
|
|
|
float v_frate;
|
|
|
|
|
|
|
|
uint32_t a_formattag;
|
|
|
|
uint32_t a_channels, a_bps;
|
|
|
|
float a_sfreq;
|
|
|
|
|
|
|
|
float default_duration;
|
|
|
|
|
|
|
|
int default_track;
|
|
|
|
|
2008-04-28 01:45:58 +00:00
|
|
|
unsigned char *private_data;
|
2004-01-19 19:16:10 +00:00
|
|
|
unsigned int private_size;
|
|
|
|
|
|
|
|
/* stuff for realmedia */
|
|
|
|
int realmedia;
|
2009-02-09 22:00:57 +00:00
|
|
|
int64_t rv_kf_base;
|
|
|
|
int rv_kf_pts;
|
2004-01-19 19:16:10 +00:00
|
|
|
float rv_pts; /* previous video timestamp */
|
|
|
|
float ra_pts; /* previous audio timestamp */
|
|
|
|
|
2006-04-03 19:41:17 +00:00
|
|
|
/** realaudio descrambling */
|
|
|
|
int sub_packet_size; ///< sub packet size, per stream
|
|
|
|
int sub_packet_h; ///< number of coded frames per block
|
|
|
|
int coded_framesize; ///< coded frame size, per stream
|
|
|
|
int audiopk_size; ///< audio packet size
|
|
|
|
unsigned char *audio_buf; ///< place to store reordered audio data
|
|
|
|
float *audio_timestamp; ///< timestamp for each audio packet
|
|
|
|
int sub_packet_cnt; ///< number of subpacket already received
|
|
|
|
int audio_filepos; ///< file position of first audio packet in block
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
/* stuff for quicktime */
|
|
|
|
int fix_i_bps;
|
|
|
|
float qt_last_a_pts;
|
|
|
|
|
|
|
|
int subtitle_type;
|
|
|
|
|
2004-11-26 16:36:03 +00:00
|
|
|
/* The timecodes of video frames might have to be reordered if they're
|
|
|
|
in display order (the timecodes, not the frames themselves!). In this
|
|
|
|
case demux packets have to be cached with the help of these variables. */
|
|
|
|
int reorder_timecodes;
|
|
|
|
demux_packet_t **cached_dps;
|
|
|
|
int num_cached_dps, num_allocated_dps;
|
2005-01-16 14:59:19 +00:00
|
|
|
float max_pts;
|
2004-11-26 16:36:03 +00:00
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
/* generic content encoding support */
|
|
|
|
mkv_content_encoding_t *encodings;
|
|
|
|
int num_encodings;
|
2004-06-08 21:30:32 +00:00
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
/* For VobSubs and SSA/ASS */
|
2007-06-24 07:29:03 +00:00
|
|
|
sh_sub_t *sh_sub;
|
2004-01-19 19:16:10 +00:00
|
|
|
} mkv_track_t;
|
|
|
|
|
|
|
|
typedef struct mkv_index
|
|
|
|
{
|
|
|
|
int tnum;
|
|
|
|
uint64_t timecode, filepos;
|
|
|
|
} mkv_index_t;
|
|
|
|
|
|
|
|
typedef struct mkv_demuxer
|
|
|
|
{
|
|
|
|
off_t segment_start;
|
|
|
|
|
|
|
|
float duration, last_pts;
|
|
|
|
uint64_t last_filepos;
|
|
|
|
|
|
|
|
mkv_track_t **tracks;
|
|
|
|
int num_tracks;
|
|
|
|
|
|
|
|
uint64_t tc_scale, cluster_tc, first_tc;
|
|
|
|
int has_first_tc;
|
|
|
|
|
|
|
|
uint64_t cluster_size;
|
|
|
|
uint64_t blockgroup_size;
|
|
|
|
|
|
|
|
mkv_index_t *indexes;
|
|
|
|
int num_indexes;
|
|
|
|
|
|
|
|
off_t *parsed_cues;
|
|
|
|
int parsed_cues_num;
|
|
|
|
off_t *parsed_seekhead;
|
|
|
|
int parsed_seekhead_num;
|
|
|
|
|
|
|
|
uint64_t *cluster_positions;
|
|
|
|
int num_cluster_pos;
|
|
|
|
|
|
|
|
int64_t skip_to_timecode;
|
|
|
|
int v_skip_to_keyframe, a_skip_to_keyframe;
|
|
|
|
|
|
|
|
int64_t stop_timecode;
|
2006-06-19 18:34:29 +00:00
|
|
|
|
|
|
|
int last_aid;
|
|
|
|
int audio_tracks[MAX_A_STREAMS];
|
2004-01-19 19:16:10 +00:00
|
|
|
} mkv_demuxer_t;
|
|
|
|
|
2007-05-28 18:13:33 +00:00
|
|
|
#define REALHEADER_SIZE 16
|
|
|
|
#define RVPROPERTIES_SIZE 34
|
|
|
|
#define RAPROPERTIES4_SIZE 56
|
|
|
|
#define RAPROPERTIES5_SIZE 70
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2006-10-10 15:01:11 +00:00
|
|
|
/**
|
|
|
|
* \brief ensures there is space for at least one additional element
|
|
|
|
* \param array array to grow
|
|
|
|
* \param nelem current number of elements in array
|
|
|
|
* \param elsize size of one array element
|
|
|
|
*/
|
2008-04-28 01:45:58 +00:00
|
|
|
static void *grow_array(void *array, int nelem, size_t elsize) {
|
2006-10-10 15:01:11 +00:00
|
|
|
if (!(nelem & 31))
|
2008-04-28 01:45:58 +00:00
|
|
|
array = realloc(array, (nelem + 32) * elsize);
|
|
|
|
return array;
|
2006-10-10 15:01:11 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
static mkv_track_t *
|
|
|
|
demux_mkv_find_track_by_num (mkv_demuxer_t *d, int n, int type)
|
|
|
|
{
|
|
|
|
int i, id;
|
|
|
|
|
|
|
|
for (i=0, id=0; i < d->num_tracks; i++)
|
|
|
|
if (d->tracks[i] != NULL && d->tracks[i]->type == type)
|
|
|
|
if (id++ == n)
|
|
|
|
return d->tracks[i];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_cluster_position (mkv_demuxer_t *mkv_d, uint64_t position)
|
|
|
|
{
|
|
|
|
int i = mkv_d->num_cluster_pos;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
if (mkv_d->cluster_positions[i] == position)
|
|
|
|
return;
|
|
|
|
|
2008-04-28 01:45:58 +00:00
|
|
|
mkv_d->cluster_positions = grow_array(mkv_d->cluster_positions,
|
|
|
|
mkv_d->num_cluster_pos,
|
|
|
|
sizeof(uint64_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define AAC_SYNC_EXTENSION_TYPE 0x02b7
|
|
|
|
static int
|
|
|
|
aac_get_sample_rate_index (uint32_t sample_rate)
|
|
|
|
{
|
|
|
|
if (92017 <= sample_rate)
|
|
|
|
return 0;
|
|
|
|
else if (75132 <= sample_rate)
|
|
|
|
return 1;
|
|
|
|
else if (55426 <= sample_rate)
|
|
|
|
return 2;
|
|
|
|
else if (46009 <= sample_rate)
|
|
|
|
return 3;
|
|
|
|
else if (37566 <= sample_rate)
|
|
|
|
return 4;
|
|
|
|
else if (27713 <= sample_rate)
|
|
|
|
return 5;
|
|
|
|
else if (23004 <= sample_rate)
|
|
|
|
return 6;
|
|
|
|
else if (18783 <= sample_rate)
|
|
|
|
return 7;
|
|
|
|
else if (13856 <= sample_rate)
|
|
|
|
return 8;
|
|
|
|
else if (11502 <= sample_rate)
|
|
|
|
return 9;
|
|
|
|
else if (9391 <= sample_rate)
|
|
|
|
return 10;
|
|
|
|
else
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
|
2004-11-26 16:36:03 +00:00
|
|
|
/** \brief Free cached demux packets
|
|
|
|
*
|
|
|
|
* Reordering the timecodes requires caching of demux packets. This function
|
|
|
|
* frees all these cached packets and the memory for the cached pointers
|
|
|
|
* itself.
|
|
|
|
*
|
|
|
|
* \param demuxer The demuxer for which the cache is to be freed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
free_cached_dps (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
mkv_track_t *track;
|
|
|
|
int i, k;
|
|
|
|
|
|
|
|
for (k = 0; k < mkv_d->num_tracks; k++)
|
|
|
|
{
|
|
|
|
track = mkv_d->tracks[k];
|
|
|
|
for (i = 0; i < track->num_cached_dps; i++)
|
|
|
|
free_demux_packet (track->cached_dps[i]);
|
|
|
|
free(track->cached_dps);
|
|
|
|
track->cached_dps = NULL;
|
|
|
|
track->num_cached_dps = 0;
|
|
|
|
track->num_allocated_dps = 0;
|
2005-05-19 12:09:26 +00:00
|
|
|
track->max_pts = 0;
|
2004-11-26 16:36:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_decode (mkv_track_t *track, uint8_t *src, uint8_t **dest,
|
|
|
|
uint32_t *size, uint32_t type)
|
|
|
|
{
|
|
|
|
int i, result;
|
|
|
|
int modified = 0;
|
|
|
|
|
|
|
|
*dest = src;
|
|
|
|
if (track->num_encodings <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i=0; i<track->num_encodings; i++)
|
|
|
|
{
|
|
|
|
if (!(track->encodings[i].scope & type))
|
|
|
|
continue;
|
|
|
|
|
2009-02-07 15:23:55 +00:00
|
|
|
#if CONFIG_ZLIB
|
2004-01-19 19:16:10 +00:00
|
|
|
if (track->encodings[i].comp_algo == 0)
|
|
|
|
{
|
|
|
|
/* zlib encoded track */
|
|
|
|
z_stream zstream;
|
|
|
|
|
|
|
|
zstream.zalloc = (alloc_func) 0;
|
|
|
|
zstream.zfree = (free_func) 0;
|
|
|
|
zstream.opaque = (voidpf) 0;
|
|
|
|
if (inflateInit (&zstream) != Z_OK)
|
|
|
|
{
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_ZlibInitializationFailed);
|
2004-01-19 19:16:10 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
zstream.next_in = (Bytef *) src;
|
|
|
|
zstream.avail_in = *size;
|
|
|
|
|
|
|
|
modified = 1;
|
2007-01-27 14:45:52 +00:00
|
|
|
*dest = NULL;
|
2004-01-19 19:16:10 +00:00
|
|
|
zstream.avail_out = *size;
|
|
|
|
do {
|
|
|
|
*size += 4000;
|
2006-10-09 17:41:37 +00:00
|
|
|
*dest = realloc (*dest, *size);
|
2004-01-19 19:16:10 +00:00
|
|
|
zstream.next_out = (Bytef *) (*dest + zstream.total_out);
|
|
|
|
result = inflate (&zstream, Z_NO_FLUSH);
|
|
|
|
if (result != Z_OK && result != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_ZlibDecompressionFailed);
|
2004-01-19 19:16:10 +00:00
|
|
|
free(*dest);
|
|
|
|
*dest = NULL;
|
|
|
|
inflateEnd (&zstream);
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
zstream.avail_out += 4000;
|
|
|
|
} while (zstream.avail_out == 4000 &&
|
|
|
|
zstream.avail_in != 0 && result != Z_STREAM_END);
|
|
|
|
|
|
|
|
*size = zstream.total_out;
|
|
|
|
inflateEnd (&zstream);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (track->encodings[i].comp_algo == 2)
|
|
|
|
{
|
|
|
|
/* lzo encoded track */
|
|
|
|
int dstlen = *size * 3;
|
|
|
|
|
2007-01-27 14:54:45 +00:00
|
|
|
*dest = NULL;
|
2004-01-19 19:16:10 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2007-01-31 23:28:34 +00:00
|
|
|
int srclen = *size;
|
2009-02-02 20:22:36 +00:00
|
|
|
if (dstlen > SIZE_MAX - AV_LZO_OUTPUT_PADDING) goto lzo_fail;
|
|
|
|
*dest = realloc (*dest, dstlen + AV_LZO_OUTPUT_PADDING);
|
|
|
|
result = av_lzo1x_decode (*dest, &dstlen, src, &srclen);
|
2007-01-31 23:28:34 +00:00
|
|
|
if (result == 0)
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
2009-02-02 20:22:36 +00:00
|
|
|
if (!(result & AV_LZO_OUTPUT_FULL))
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2007-01-31 23:28:34 +00:00
|
|
|
lzo_fail:
|
2004-01-19 19:16:10 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_LzoDecompressionFailed);
|
2007-01-27 14:52:18 +00:00
|
|
|
free(*dest);
|
|
|
|
*dest = NULL;
|
2004-01-19 19:16:10 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_DBG2,
|
|
|
|
"[mkv] lzo decompression buffer too small.\n");
|
|
|
|
dstlen *= 2;
|
|
|
|
}
|
|
|
|
*size = dstlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
static int demux_mkv_read_info(demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-03-13 01:26:32 +00:00
|
|
|
mkv_demuxer_t *mkv_d = demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t length, l;
|
|
|
|
int i;
|
|
|
|
uint64_t tc_scale = 1000000;
|
|
|
|
long double duration = 0.;
|
|
|
|
|
|
|
|
length = ebml_read_length(s, NULL);
|
|
|
|
while (length > 0) {
|
|
|
|
uint32_t id = ebml_read_id(s, &i);
|
|
|
|
length -= i;
|
|
|
|
switch (id) {
|
2004-01-19 19:16:10 +00:00
|
|
|
case MATROSKA_ID_TIMECODESCALE:
|
2009-03-13 01:26:32 +00:00
|
|
|
tc_scale = ebml_read_uint(s, &l);
|
|
|
|
length -= l;
|
|
|
|
if (tc_scale == EBML_UINT_INVALID)
|
|
|
|
return 1;
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + timecode scale: %"PRIu64"\n",
|
2006-09-12 21:33:10 +00:00
|
|
|
tc_scale);
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_DURATION:
|
2009-03-13 01:26:32 +00:00
|
|
|
duration = ebml_read_float(s, &l);
|
|
|
|
length -= l;
|
|
|
|
if (duration == EBML_FLOAT_INVALID)
|
|
|
|
return 1;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
|
2009-03-16 21:30:48 +00:00
|
|
|
case MATROSKA_ID_SEGMENTUID:;
|
|
|
|
l = ebml_read_length(s, &i);
|
|
|
|
length -= i;
|
|
|
|
if (l != sizeof(demuxer->matroska_data.segment_uid)) {
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_INFO,
|
|
|
|
"[mkv] segment uid invalid length %"PRIu64"\n", l);
|
|
|
|
stream_skip(s, l);
|
|
|
|
} else {
|
|
|
|
stream_read(s, demuxer->matroska_data.segment_uid, l);
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + segment uid");
|
|
|
|
for (int i = 0; i < l; i++)
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, " %02x",
|
|
|
|
demuxer->matroska_data.segment_uid[i]);
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "\n");
|
|
|
|
}
|
|
|
|
length -= l;
|
|
|
|
break;
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
default:
|
2009-03-13 01:26:32 +00:00
|
|
|
ebml_read_skip(s, &l);
|
|
|
|
length -= l;
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-13 01:26:32 +00:00
|
|
|
mkv_d->tc_scale = tc_scale;
|
|
|
|
mkv_d->duration = duration * tc_scale / 1000000000.0;
|
|
|
|
if (duration)
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + duration: %.3fs\n",
|
|
|
|
mkv_d->duration);
|
|
|
|
|
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2006-10-09 18:28:58 +00:00
|
|
|
/**
|
|
|
|
* \brief free array of kv_content_encoding_t
|
|
|
|
* \param encodings pointer to array
|
|
|
|
* \param numencodings number of encodings in array
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
demux_mkv_free_encodings(mkv_content_encoding_t *encodings, int numencodings)
|
|
|
|
{
|
|
|
|
while (numencodings-- > 0)
|
|
|
|
free(encodings[numencodings].comp_settings);
|
|
|
|
free(encodings);
|
|
|
|
}
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_read_trackencodings (demuxer_t *demuxer, mkv_track_t *track)
|
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
mkv_content_encoding_t *ce, e;
|
|
|
|
uint64_t len, length, l;
|
|
|
|
int il, n;
|
|
|
|
|
2006-10-09 17:41:37 +00:00
|
|
|
ce = malloc (sizeof (*ce));
|
2004-01-19 19:16:10 +00:00
|
|
|
n = 0;
|
|
|
|
|
|
|
|
len = length = ebml_read_length (s, &il);
|
|
|
|
len += il;
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CONTENTENCODING:
|
|
|
|
{
|
|
|
|
uint64_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset (&e, 0, sizeof (e));
|
|
|
|
e.scope = 1;
|
|
|
|
|
|
|
|
len = ebml_read_length (s, &i);
|
|
|
|
l = len + i;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
uint64_t num, l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CONTENTENCODINGORDER:
|
|
|
|
num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
2006-10-09 18:28:58 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
e.order = num;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CONTENTENCODINGSCOPE:
|
|
|
|
num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
2006-10-09 18:28:58 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
e.scope = num;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CONTENTENCODINGTYPE:
|
|
|
|
num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
2006-10-09 18:28:58 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
e.type = num;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CONTENTCOMPRESSION:
|
|
|
|
{
|
|
|
|
uint64_t le;
|
|
|
|
|
|
|
|
le = ebml_read_length (s, &i);
|
|
|
|
l = le + i;
|
|
|
|
|
|
|
|
while (le > 0)
|
|
|
|
{
|
|
|
|
uint64_t l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CONTENTCOMPALGO:
|
|
|
|
num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
2006-10-09 18:28:58 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
e.comp_algo = num;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CONTENTCOMPSETTINGS:
|
|
|
|
l = ebml_read_length (s, &i);
|
2006-10-09 17:41:37 +00:00
|
|
|
e.comp_settings = malloc (l);
|
2004-01-19 19:16:10 +00:00
|
|
|
stream_read (s, e.comp_settings, l);
|
|
|
|
e.comp_settings_len = l;
|
|
|
|
l += i;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
le -= l + il;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.type == 1)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_WARN,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_TrackEncrypted, track->tnum);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else if (e.type != 0)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_WARN,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_UnknownContentEncoding, track->tnum);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (e.comp_algo != 0 && e.comp_algo != 2)
|
|
|
|
{
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_UnknownCompression,
|
2004-01-19 19:16:10 +00:00
|
|
|
track->tnum, e.comp_algo);
|
|
|
|
}
|
2009-02-07 15:23:55 +00:00
|
|
|
#if !CONFIG_ZLIB
|
2004-01-19 19:16:10 +00:00
|
|
|
else if (e.comp_algo == 0)
|
|
|
|
{
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_ZlibCompressionUnsupported,
|
2004-01-19 19:16:10 +00:00
|
|
|
track->tnum);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= l + il;
|
|
|
|
}
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
if (e.order <= ce[i].order)
|
|
|
|
break;
|
2006-10-09 17:48:43 +00:00
|
|
|
ce = realloc (ce, (n+1) *sizeof (*ce));
|
2004-01-19 19:16:10 +00:00
|
|
|
memmove (ce+i+1, ce+i, (n-i) * sizeof (*ce));
|
|
|
|
memcpy (ce+i, &e, sizeof (e));
|
|
|
|
n++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= l + il;
|
|
|
|
}
|
|
|
|
|
|
|
|
track->encodings = ce;
|
|
|
|
track->num_encodings = n;
|
|
|
|
return len;
|
2006-10-09 18:28:58 +00:00
|
|
|
|
|
|
|
err_out:
|
|
|
|
demux_mkv_free_encodings(ce, n);
|
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
demux_mkv_read_trackaudio (demuxer_t *demuxer, mkv_track_t *track)
|
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t len, length, l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
track->a_sfreq = 8000.0;
|
|
|
|
track->a_channels = 1;
|
|
|
|
|
|
|
|
len = length = ebml_read_length (s, &il);
|
|
|
|
len += il;
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_AUDIOSAMPLINGFREQ:
|
|
|
|
{
|
|
|
|
long double num = ebml_read_float (s, &l);
|
|
|
|
if (num == EBML_FLOAT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->a_sfreq = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Sampling frequency: %f\n",
|
|
|
|
track->a_sfreq);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_AUDIOBITDEPTH:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->a_bps = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Bit depth: %u\n",
|
|
|
|
track->a_bps);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_AUDIOCHANNELS:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->a_channels = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Channels: %u\n",
|
|
|
|
track->a_channels);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
length -= l + il;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
demux_mkv_read_trackvideo (demuxer_t *demuxer, mkv_track_t *track)
|
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t len, length, l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
len = length = ebml_read_length (s, &il);
|
|
|
|
len += il;
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_VIDEOFRAMERATE:
|
|
|
|
{
|
|
|
|
long double num = ebml_read_float (s, &l);
|
|
|
|
if (num == EBML_FLOAT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->v_frate = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Frame rate: %f\n",
|
|
|
|
track->v_frate);
|
2004-11-28 21:08:46 +00:00
|
|
|
if (track->v_frate > 0)
|
|
|
|
track->default_duration = 1 / track->v_frate;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_VIDEODISPLAYWIDTH:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->v_dwidth = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Display width: %u\n",
|
|
|
|
track->v_dwidth);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_VIDEODISPLAYHEIGHT:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->v_dheight = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Display height: %u\n",
|
|
|
|
track->v_dheight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_VIDEOPIXELWIDTH:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->v_width = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel width: %u\n",
|
|
|
|
track->v_width);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_VIDEOPIXELHEIGHT:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->v_height = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel height: %u\n",
|
|
|
|
track->v_height);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
length -= l + il;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-10-09 18:48:55 +00:00
|
|
|
/**
|
|
|
|
* \brief free any data associated with given track
|
|
|
|
* \param track track of which to free data
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
demux_mkv_free_trackentry(mkv_track_t *track) {
|
2008-02-29 17:25:41 +00:00
|
|
|
free (track->name);
|
|
|
|
free (track->codec_id);
|
|
|
|
free (track->language);
|
|
|
|
free (track->private_data);
|
|
|
|
free (track->audio_buf);
|
|
|
|
free (track->audio_timestamp);
|
2006-10-09 18:48:55 +00:00
|
|
|
demux_mkv_free_encodings(track->encodings, track->num_encodings);
|
2006-10-09 18:56:16 +00:00
|
|
|
free(track);
|
2006-10-09 18:48:55 +00:00
|
|
|
}
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_read_trackentry (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
mkv_track_t *track;
|
|
|
|
uint64_t len, length, l;
|
|
|
|
int il;
|
|
|
|
|
2006-10-09 18:00:54 +00:00
|
|
|
track = calloc (1, sizeof (*track));
|
2004-01-19 19:16:10 +00:00
|
|
|
/* set default values */
|
|
|
|
track->default_track = 1;
|
2006-09-02 19:39:24 +00:00
|
|
|
track->name = 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
track->language = strdup("eng");
|
|
|
|
|
|
|
|
len = length = ebml_read_length (s, &il);
|
|
|
|
len += il;
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_TRACKNUMBER:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
track->tnum = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Track number: %u\n",
|
|
|
|
track->tnum);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-09-02 19:39:24 +00:00
|
|
|
case MATROSKA_ID_TRACKNAME:
|
|
|
|
{
|
|
|
|
track->name = ebml_read_utf8 (s, &l);
|
|
|
|
if (track->name == NULL)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2006-09-02 19:39:24 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Name: %s\n",
|
|
|
|
track->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
case MATROSKA_ID_TRACKTYPE:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
track->type = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Track type: ");
|
|
|
|
switch (track->type)
|
|
|
|
{
|
|
|
|
case MATROSKA_TRACK_AUDIO:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "Audio\n");
|
|
|
|
break;
|
|
|
|
case MATROSKA_TRACK_VIDEO:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "Video\n");
|
|
|
|
break;
|
|
|
|
case MATROSKA_TRACK_SUBTITLE:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "Subtitle\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "unknown\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_TRACKAUDIO:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Audio track\n");
|
|
|
|
l = demux_mkv_read_trackaudio (demuxer, track);
|
|
|
|
if (l == 0)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_TRACKVIDEO:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Video track\n");
|
|
|
|
l = demux_mkv_read_trackvideo (demuxer, track);
|
|
|
|
if (l == 0)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CODECID:
|
|
|
|
track->codec_id = ebml_read_ascii (s, &l);
|
|
|
|
if (track->codec_id == NULL)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-04-02 07:36:42 +00:00
|
|
|
if (!strcmp (track->codec_id, MKV_V_MSCOMP) ||
|
|
|
|
!strcmp (track->codec_id, MKV_A_ACM))
|
2004-01-19 19:16:10 +00:00
|
|
|
track->ms_compat = 1;
|
|
|
|
else if (!strcmp (track->codec_id, MKV_S_VOBSUB))
|
|
|
|
track->subtitle_type = MATROSKA_SUBTYPE_VOBSUB;
|
|
|
|
else if (!strcmp (track->codec_id, MKV_S_TEXTSSA)
|
|
|
|
|| !strcmp (track->codec_id, MKV_S_TEXTASS)
|
|
|
|
|| !strcmp (track->codec_id, MKV_S_SSA)
|
|
|
|
|| !strcmp (track->codec_id, MKV_S_ASS))
|
|
|
|
{
|
|
|
|
track->subtitle_type = MATROSKA_SUBTYPE_SSA;
|
|
|
|
}
|
|
|
|
else if (!strcmp (track->codec_id, MKV_S_TEXTASCII))
|
|
|
|
track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
|
|
|
|
if (!strcmp (track->codec_id, MKV_S_TEXTUTF8))
|
|
|
|
{
|
|
|
|
track->subtitle_type = MATROSKA_SUBTYPE_TEXT;
|
|
|
|
}
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Codec ID: %s\n",
|
|
|
|
track->codec_id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CODECPRIVATE:
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
uint64_t num = ebml_read_length (s, &x);
|
2006-06-04 22:41:27 +00:00
|
|
|
// audit: cheap guard against overflows later..
|
|
|
|
if (num > SIZE_MAX - 1000) return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
l = x + num;
|
2009-02-02 20:22:36 +00:00
|
|
|
track->private_data = malloc (num + AV_LZO_INPUT_PADDING);
|
2004-01-19 19:16:10 +00:00
|
|
|
if (stream_read(s, track->private_data, num) != (int) num)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
track->private_size = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + CodecPrivate, length "
|
|
|
|
"%u\n", track->private_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_TRACKLANGUAGE:
|
2006-10-09 19:10:36 +00:00
|
|
|
free(track->language);
|
2004-01-19 19:16:10 +00:00
|
|
|
track->language = ebml_read_utf8 (s, &l);
|
|
|
|
if (track->language == NULL)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Language: %s\n",
|
|
|
|
track->language);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_TRACKFLAGDEFAULT:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
track->default_track = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Default flag: %u\n",
|
|
|
|
track->default_track);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_TRACKDEFAULTDURATION:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
if (num == 0)
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Default duration: 0");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
track->v_frate = 1000000000.0 / num;
|
2004-11-28 21:08:46 +00:00
|
|
|
track->default_duration = num / 1000000000.0;
|
2004-01-19 19:16:10 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + Default duration: "
|
|
|
|
"%.3fms ( = %.3f fps)\n",num/1000000.0,track->v_frate);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_TRACKENCODINGS:
|
|
|
|
l = demux_mkv_read_trackencodings (demuxer, track);
|
|
|
|
if (l == 0)
|
2006-10-09 18:48:55 +00:00
|
|
|
goto err_out;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
length -= l + il;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkv_d->tracks[mkv_d->num_tracks++] = track;
|
|
|
|
return len;
|
2006-10-09 18:48:55 +00:00
|
|
|
|
|
|
|
err_out:
|
|
|
|
demux_mkv_free_trackentry(track);
|
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
demux_mkv_read_tracks (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t length, l;
|
|
|
|
int il;
|
|
|
|
|
2006-10-09 17:41:37 +00:00
|
|
|
mkv_d->tracks = malloc (sizeof (*mkv_d->tracks));
|
2004-01-19 19:16:10 +00:00
|
|
|
mkv_d->num_tracks = 0;
|
|
|
|
|
|
|
|
length = ebml_read_length (s, NULL);
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_TRACKENTRY:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + a track...\n");
|
2006-10-09 17:41:37 +00:00
|
|
|
mkv_d->tracks = realloc (mkv_d->tracks,
|
|
|
|
(mkv_d->num_tracks+1)
|
|
|
|
*sizeof (*mkv_d->tracks));
|
2004-01-19 19:16:10 +00:00
|
|
|
l = demux_mkv_read_trackentry (demuxer);
|
|
|
|
if (l == 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
length -= l + il;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
demux_mkv_read_cues (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t length, l, time, track, pos;
|
|
|
|
off_t off;
|
|
|
|
int i, il;
|
|
|
|
|
2006-10-15 12:48:18 +00:00
|
|
|
if (index_mode == 0) {
|
|
|
|
ebml_read_skip (s, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
off = stream_tell (s);
|
|
|
|
for (i=0; i<mkv_d->parsed_cues_num; i++)
|
|
|
|
if (mkv_d->parsed_cues[i] == off)
|
|
|
|
{
|
|
|
|
ebml_read_skip (s, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-09 17:41:37 +00:00
|
|
|
mkv_d->parsed_cues = realloc (mkv_d->parsed_cues,
|
|
|
|
(mkv_d->parsed_cues_num+1)
|
|
|
|
* sizeof (off_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
mkv_d->parsed_cues[mkv_d->parsed_cues_num++] = off;
|
|
|
|
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing cues ] -----------\n");
|
|
|
|
length = ebml_read_length (s, NULL);
|
|
|
|
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
time = track = pos = EBML_UINT_INVALID;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_POINTENTRY:
|
|
|
|
{
|
|
|
|
uint64_t len;
|
|
|
|
|
|
|
|
len = ebml_read_length (s, &i);
|
|
|
|
l = len + i;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
uint64_t l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CUETIME:
|
|
|
|
time = ebml_read_uint (s, &l);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CUETRACKPOSITION:
|
|
|
|
{
|
|
|
|
uint64_t le;
|
|
|
|
|
|
|
|
le = ebml_read_length (s, &i);
|
|
|
|
l = le + i;
|
|
|
|
|
|
|
|
while (le > 0)
|
|
|
|
{
|
|
|
|
uint64_t l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CUETRACK:
|
|
|
|
track = ebml_read_uint (s, &l);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CUECLUSTERPOSITION:
|
|
|
|
pos = ebml_read_uint (s, &l);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
le -= l + il;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= l + il;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= l + il;
|
|
|
|
|
|
|
|
if (time != EBML_UINT_INVALID && track != EBML_UINT_INVALID
|
|
|
|
&& pos != EBML_UINT_INVALID)
|
|
|
|
{
|
2008-04-28 01:45:58 +00:00
|
|
|
mkv_d->indexes = grow_array(mkv_d->indexes, mkv_d->num_indexes,
|
|
|
|
sizeof(mkv_index_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
mkv_d->indexes[mkv_d->num_indexes].tnum = track;
|
|
|
|
mkv_d->indexes[mkv_d->num_indexes].timecode = time;
|
|
|
|
mkv_d->indexes[mkv_d->num_indexes].filepos =mkv_d->segment_start+pos;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] |+ found cue point "
|
2005-10-13 18:33:56 +00:00
|
|
|
"for track %"PRIu64": timecode %"PRIu64", filepos: %"PRIu64"\n",
|
2004-01-19 19:16:10 +00:00
|
|
|
track, time, mkv_d->segment_start + pos);
|
|
|
|
mkv_d->num_indexes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing cues ] -----------\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-16 21:30:48 +00:00
|
|
|
static uint64_t read_one_chapter(struct demuxer *demuxer, stream_t *s)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-03-13 01:26:32 +00:00
|
|
|
uint64_t len, l;
|
|
|
|
uint64_t start = 0, end = 0;
|
2009-03-16 21:30:48 +00:00
|
|
|
struct matroska_chapter chapter = {};
|
2009-03-13 01:26:32 +00:00
|
|
|
char *name = 0;
|
|
|
|
int i;
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
len = ebml_read_length(s, &i);
|
|
|
|
uint64_t bytes_read = len + i;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
id = ebml_read_id(s, &i);
|
|
|
|
len -= i;
|
|
|
|
switch (id) {
|
|
|
|
case MATROSKA_ID_CHAPTERTIMESTART:
|
|
|
|
start = ebml_read_uint(s, &l) / 1000000;
|
|
|
|
len -= l;
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
case MATROSKA_ID_CHAPTERTIMEEND:
|
|
|
|
end = ebml_read_uint(s, &l) / 1000000;
|
|
|
|
len -= l;
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
case MATROSKA_ID_CHAPTERDISPLAY:;
|
|
|
|
uint64_t displaylen = ebml_read_length(s, &i);
|
|
|
|
len -= displaylen + i;
|
|
|
|
while (displaylen > 0) {
|
|
|
|
id = ebml_read_id(s, &i);
|
|
|
|
displaylen -= i;
|
|
|
|
switch (id) {
|
|
|
|
case MATROSKA_ID_CHAPSTRING:
|
|
|
|
name = ebml_read_utf8(s, &l);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ebml_read_skip(s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
displaylen -= l;
|
|
|
|
}
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-03-16 21:30:48 +00:00
|
|
|
case MATROSKA_ID_CHAPTERSEGMENTUID:
|
|
|
|
l = ebml_read_length(s, &i);
|
|
|
|
len -= l + i;
|
|
|
|
if (l != sizeof(chapter.segment_uid)) {
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_INFO,
|
|
|
|
"[mkv] chapter segment uid invalid length %"PRIu64"\n",
|
|
|
|
l);
|
|
|
|
stream_skip(s, l);
|
|
|
|
} else {
|
|
|
|
stream_read(s, chapter.segment_uid, l);
|
|
|
|
chapter.has_segment_uid = true;
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Chapter segment uid ");
|
|
|
|
for (int i = 0; i < l; i++)
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "%02x ", chapter.segment_uid[i]);
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
default:
|
|
|
|
ebml_read_skip(s, &l);
|
|
|
|
len -= l;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
if (!name)
|
|
|
|
name = strdup("(unnamed)");
|
|
|
|
|
|
|
|
int cid = demuxer_add_chapter(demuxer, name, start, end);
|
2009-03-16 21:30:48 +00:00
|
|
|
struct matroska_data *m = &demuxer->matroska_data;
|
|
|
|
m->ordered_chapters = talloc_realloc(demuxer, m->ordered_chapters,
|
|
|
|
struct matroska_chapter,
|
|
|
|
m->num_ordered_chapters + 1);
|
|
|
|
chapter.start = start;
|
|
|
|
chapter.end = end;
|
|
|
|
// Will be undone later if this is a normal chapter rather than ordered
|
|
|
|
m->ordered_chapters[m->num_ordered_chapters] = chapter;
|
|
|
|
m->num_ordered_chapters++;
|
2009-03-13 01:26:32 +00:00
|
|
|
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Chapter %u from %02d:%02d:%02d."
|
|
|
|
"%03d to %02d:%02d:%02d.%03d, %s\n",
|
|
|
|
cid,
|
|
|
|
(int) (start / 60 / 60 / 1000),
|
|
|
|
(int) ((start / 60 / 1000) % 60),
|
|
|
|
(int) ((start / 1000) % 60),
|
|
|
|
(int) (start % 1000),
|
|
|
|
(int) (end / 60 / 60 / 1000),
|
|
|
|
(int) ((end / 60 / 1000) % 60),
|
|
|
|
(int) ((end / 1000) % 60),
|
|
|
|
(int) (end % 1000), name);
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
return bytes_read;
|
|
|
|
}
|
2006-08-06 18:55:34 +00:00
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
static int demux_mkv_read_chapters(struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t length, l;
|
|
|
|
int i;
|
|
|
|
uint32_t id;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
if (demuxer->chapters) {
|
|
|
|
ebml_read_skip(s, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing chapters ] ---------\n");
|
|
|
|
length = ebml_read_length(s, NULL);
|
|
|
|
|
2009-03-16 21:30:48 +00:00
|
|
|
bool have_edition = false;
|
2009-03-13 01:26:32 +00:00
|
|
|
while (length > 0) {
|
|
|
|
id = ebml_read_id(s, &i);
|
|
|
|
length -= i;
|
|
|
|
switch (id) {
|
2009-03-16 21:30:48 +00:00
|
|
|
case MATROSKA_ID_EDITIONENTRY:
|
|
|
|
if (have_edition) {
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_WARN, "[mkv] Multiple edition entries"
|
|
|
|
" - ignoring all but first!\n");
|
|
|
|
ebml_read_skip(s, &l);
|
|
|
|
length -= l;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
have_edition = true;
|
2009-03-13 01:26:32 +00:00
|
|
|
uint64_t editionlen = ebml_read_length(s, &i);
|
|
|
|
length -= editionlen + i;
|
2009-03-16 21:30:48 +00:00
|
|
|
bool ordered = false;
|
2009-03-13 01:26:32 +00:00
|
|
|
while (editionlen > 0) {
|
|
|
|
id = ebml_read_id(s, &i);
|
|
|
|
editionlen -= i;
|
|
|
|
switch (id) {
|
|
|
|
case MATROSKA_ID_CHAPTERATOM:
|
|
|
|
l = read_one_chapter(demuxer, s);
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
2009-03-16 21:30:48 +00:00
|
|
|
case MATROSKA_ID_EDITIONFLAGORDERED:
|
|
|
|
ordered = ebml_read_uint(s, &l);
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V,
|
|
|
|
"[mkv] Ordered chapter flag: %d\n", ordered);
|
|
|
|
break;
|
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
default:
|
|
|
|
ebml_read_skip(s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
editionlen -= l;
|
|
|
|
}
|
2009-03-16 21:30:48 +00:00
|
|
|
if (!ordered) {
|
|
|
|
// The chapters should be interpreted as normal ones,
|
|
|
|
// so undo the addition of this information.
|
|
|
|
talloc_free(demuxer->matroska_data.ordered_chapters);
|
|
|
|
demuxer->matroska_data.ordered_chapters = NULL;
|
|
|
|
demuxer->matroska_data.num_ordered_chapters = 0;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-03-13 01:26:32 +00:00
|
|
|
ebml_read_skip(s, &l);
|
|
|
|
length -= l;
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing chapters ] ---------\n");
|
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
demux_mkv_read_tags (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
ebml_read_skip (demuxer->stream, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_read_attachments (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t length, l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing attachments ] ---------\n");
|
|
|
|
length = ebml_read_length (s, NULL);
|
|
|
|
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_ATTACHEDFILE:
|
|
|
|
{
|
|
|
|
uint64_t len;
|
|
|
|
int i;
|
|
|
|
char* name = NULL;
|
|
|
|
char* mime = NULL;
|
|
|
|
char* data = NULL;
|
|
|
|
int data_size = 0;
|
|
|
|
|
|
|
|
len = ebml_read_length (s, &i);
|
|
|
|
l = len + i;
|
|
|
|
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + an attachment...\n");
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
uint64_t l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_FILENAME:
|
|
|
|
name = ebml_read_utf8 (s, &l);
|
|
|
|
if (name == NULL)
|
|
|
|
return 0;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + FileName: %s\n",
|
|
|
|
name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_FILEMIMETYPE:
|
|
|
|
mime = ebml_read_ascii (s, &l);
|
|
|
|
if (mime == NULL)
|
|
|
|
return 0;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + FileMimeType: %s\n",
|
|
|
|
mime);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_FILEDATA:
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
uint64_t num = ebml_read_length (s, &x);
|
|
|
|
l = x + num;
|
2006-10-11 15:47:37 +00:00
|
|
|
free(data);
|
2006-07-07 18:26:51 +00:00
|
|
|
data = malloc (num);
|
|
|
|
if (stream_read(s, data, num) != (int) num)
|
2006-10-11 15:47:37 +00:00
|
|
|
{
|
|
|
|
free(data);
|
2006-07-07 18:26:51 +00:00
|
|
|
return 0;
|
2006-10-11 15:47:37 +00:00
|
|
|
}
|
2006-07-07 18:26:51 +00:00
|
|
|
data_size = num;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] | + FileData, length "
|
|
|
|
"%u\n", data_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= l + il;
|
|
|
|
}
|
|
|
|
|
2008-01-12 01:12:39 +00:00
|
|
|
demuxer_add_attachment(demuxer, name, mime, data, data_size);
|
2006-07-07 18:26:51 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V,
|
|
|
|
"[mkv] Attachment: %s, %s, %u bytes\n",
|
|
|
|
name, mime, data_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
length -= l + il;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing attachments ] ---------\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_read_seekhead (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t length, l, seek_pos, saved_pos, num;
|
|
|
|
uint32_t seek_id;
|
|
|
|
int i, il, res = 0;
|
|
|
|
off_t off;
|
|
|
|
|
|
|
|
off = stream_tell (s);
|
|
|
|
for (i=0; i<mkv_d->parsed_seekhead_num; i++)
|
|
|
|
if (mkv_d->parsed_seekhead[i] == off)
|
|
|
|
{
|
|
|
|
ebml_read_skip (s, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-09 17:41:37 +00:00
|
|
|
mkv_d->parsed_seekhead = realloc (mkv_d->parsed_seekhead,
|
|
|
|
(mkv_d->parsed_seekhead_num+1)
|
|
|
|
* sizeof (off_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
mkv_d->parsed_seekhead[mkv_d->parsed_seekhead_num++] = off;
|
|
|
|
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing seek head ] ---------\n");
|
|
|
|
length = ebml_read_length (s, NULL);
|
2006-11-07 18:14:30 +00:00
|
|
|
/* off now holds the position of the next element after the seek head. */
|
|
|
|
off = stream_tell (s) + length;
|
2004-01-19 19:16:10 +00:00
|
|
|
while (length > 0 && !res)
|
|
|
|
{
|
|
|
|
|
|
|
|
seek_id = 0;
|
|
|
|
seek_pos = EBML_UINT_INVALID;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_SEEKENTRY:
|
|
|
|
{
|
|
|
|
uint64_t len;
|
|
|
|
|
|
|
|
len = ebml_read_length (s, &i);
|
|
|
|
l = len + i;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
uint64_t l;
|
|
|
|
int il;
|
|
|
|
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_SEEKID:
|
|
|
|
num = ebml_read_uint (s, &l);
|
|
|
|
if (num != EBML_UINT_INVALID)
|
|
|
|
seek_id = num;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_SEEKPOSITION:
|
|
|
|
seek_pos = ebml_read_uint (s, &l);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= l + il;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
length -= l + il;
|
|
|
|
|
|
|
|
if (seek_id == 0 || seek_id == MATROSKA_ID_CLUSTER
|
2004-01-31 09:54:09 +00:00
|
|
|
|| seek_pos == EBML_UINT_INVALID ||
|
|
|
|
((mkv_d->segment_start + seek_pos) >= (uint64_t)demuxer->movi_end))
|
2004-01-19 19:16:10 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
saved_pos = stream_tell (s);
|
|
|
|
if (!stream_seek (s, mkv_d->segment_start + seek_pos))
|
|
|
|
res = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ebml_read_id (s, &il) != seek_id)
|
|
|
|
res = 1;
|
|
|
|
else
|
|
|
|
switch (seek_id)
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CUES:
|
|
|
|
if (demux_mkv_read_cues (demuxer))
|
|
|
|
res = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_TAGS:
|
|
|
|
if (demux_mkv_read_tags (demuxer))
|
|
|
|
res = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_SEEKHEAD:
|
|
|
|
if (demux_mkv_read_seekhead (demuxer))
|
|
|
|
res = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CHAPTERS:
|
|
|
|
if (demux_mkv_read_chapters (demuxer))
|
|
|
|
res = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_seek (s, saved_pos);
|
|
|
|
}
|
2006-11-07 18:14:30 +00:00
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
/* If there was an error then try to skip this seek head. */
|
|
|
|
if (stream_seek (s, off))
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
else
|
2004-01-19 19:16:10 +00:00
|
|
|
if (length > 0)
|
|
|
|
stream_seek (s, stream_tell (s) + length);
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing seek head ] ---------\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-08-26 19:17:04 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_open_video (demuxer_t *demuxer, mkv_track_t *track, int vid);
|
|
|
|
static int
|
|
|
|
demux_mkv_open_audio (demuxer_t *demuxer, mkv_track_t *track, int aid);
|
2006-11-13 16:15:23 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_open_sub (demuxer_t *demuxer, mkv_track_t *track, int sid);
|
2006-08-26 19:17:04 +00:00
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
static void
|
2006-08-26 19:17:04 +00:00
|
|
|
display_create_tracks (demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2006-08-26 19:17:04 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *)demuxer->priv;
|
2004-01-19 19:16:10 +00:00
|
|
|
int i, vid=0, aid=0, sid=0;
|
|
|
|
|
|
|
|
for (i=0; i<mkv_d->num_tracks; i++)
|
|
|
|
{
|
|
|
|
char *type = "unknown", str[32];
|
|
|
|
*str = '\0';
|
|
|
|
switch (mkv_d->tracks[i]->type)
|
|
|
|
{
|
|
|
|
case MATROSKA_TRACK_VIDEO:
|
|
|
|
type = "video";
|
2006-08-26 19:17:04 +00:00
|
|
|
demux_mkv_open_video(demuxer, mkv_d->tracks[i], vid);
|
2006-09-02 19:39:24 +00:00
|
|
|
if (mkv_d->tracks[i]->name)
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", vid, mkv_d->tracks[i]->name);
|
2004-01-19 19:16:10 +00:00
|
|
|
sprintf (str, "-vid %u", vid++);
|
|
|
|
break;
|
|
|
|
case MATROSKA_TRACK_AUDIO:
|
|
|
|
type = "audio";
|
2006-08-26 19:17:04 +00:00
|
|
|
demux_mkv_open_audio(demuxer, mkv_d->tracks[i], aid);
|
2006-09-02 19:39:24 +00:00
|
|
|
if (mkv_d->tracks[i]->name)
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", aid, mkv_d->tracks[i]->name);
|
2006-04-24 07:20:34 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", aid, mkv_d->tracks[i]->language);
|
2004-04-26 09:22:17 +00:00
|
|
|
sprintf (str, "-aid %u, -alang %.5s",aid++,mkv_d->tracks[i]->language);
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
case MATROSKA_TRACK_SUBTITLE:
|
2004-09-12 14:01:13 +00:00
|
|
|
type = "subtitles";
|
2006-11-13 16:15:23 +00:00
|
|
|
demux_mkv_open_sub(demuxer, mkv_d->tracks[i], sid);
|
2006-09-02 19:39:24 +00:00
|
|
|
if (mkv_d->tracks[i]->name)
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", sid, mkv_d->tracks[i]->name);
|
2006-04-24 07:20:34 +00:00
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid, mkv_d->tracks[i]->language);
|
2004-04-26 09:22:17 +00:00
|
|
|
sprintf (str, "-sid %u, -slang %.5s",sid++,mkv_d->tracks[i]->language);
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-09-02 19:39:24 +00:00
|
|
|
if (mkv_d->tracks[i]->name)
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackIDName,
|
2006-09-02 19:39:24 +00:00
|
|
|
mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, mkv_d->tracks[i]->name, str);
|
|
|
|
else
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackID,
|
2004-01-19 19:16:10 +00:00
|
|
|
mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-10 13:58:57 +00:00
|
|
|
typedef struct {
|
|
|
|
char *id;
|
|
|
|
int fourcc;
|
|
|
|
int extradata;
|
|
|
|
} videocodec_info_t;
|
|
|
|
|
|
|
|
static const videocodec_info_t vinfo[] = {
|
|
|
|
{ MKV_V_MPEG1, mmioFOURCC('m', 'p', 'g', '1'), 0 },
|
|
|
|
{ MKV_V_MPEG2, mmioFOURCC('m', 'p', 'g', '2'), 0 },
|
|
|
|
{ MKV_V_MPEG4_SP, mmioFOURCC('m', 'p', '4', 'v'), 1 },
|
|
|
|
{ MKV_V_MPEG4_ASP, mmioFOURCC('m', 'p', '4', 'v'), 1 },
|
|
|
|
{ MKV_V_MPEG4_AP, mmioFOURCC('m', 'p', '4', 'v'), 1 },
|
|
|
|
{ MKV_V_MPEG4_AVC, mmioFOURCC('a', 'v', 'c', '1'), 1 },
|
2007-05-10 14:01:53 +00:00
|
|
|
{ MKV_V_THEORA, mmioFOURCC('t', 'h', 'e', 'o'), 1 },
|
2007-05-10 13:58:57 +00:00
|
|
|
{ NULL, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
static int
|
2006-08-26 19:17:04 +00:00
|
|
|
demux_mkv_open_video (demuxer_t *demuxer, mkv_track_t *track, int vid)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2008-04-16 04:11:12 +00:00
|
|
|
struct MPOpts *opts = demuxer->opts;
|
2004-01-19 19:16:10 +00:00
|
|
|
BITMAPINFOHEADER *bih;
|
|
|
|
void *ImageDesc = NULL;
|
|
|
|
sh_video_t *sh_v;
|
|
|
|
|
|
|
|
if (track->ms_compat) /* MS compatibility mode */
|
|
|
|
{
|
|
|
|
BITMAPINFOHEADER *src;
|
|
|
|
|
|
|
|
if (track->private_data == NULL
|
|
|
|
|| track->private_size < sizeof (BITMAPINFOHEADER))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
src = (BITMAPINFOHEADER *) track->private_data;
|
2006-10-09 18:00:54 +00:00
|
|
|
bih = calloc (1, track->private_size);
|
2004-01-19 19:16:10 +00:00
|
|
|
bih->biSize = le2me_32 (src->biSize);
|
|
|
|
bih->biWidth = le2me_32 (src->biWidth);
|
|
|
|
bih->biHeight = le2me_32 (src->biHeight);
|
|
|
|
bih->biPlanes = le2me_16 (src->biPlanes);
|
|
|
|
bih->biBitCount = le2me_16 (src->biBitCount);
|
|
|
|
bih->biCompression = le2me_32 (src->biCompression);
|
|
|
|
bih->biSizeImage = le2me_32 (src->biSizeImage);
|
|
|
|
bih->biXPelsPerMeter = le2me_32 (src->biXPelsPerMeter);
|
|
|
|
bih->biYPelsPerMeter = le2me_32 (src->biYPelsPerMeter);
|
|
|
|
bih->biClrUsed = le2me_32 (src->biClrUsed);
|
|
|
|
bih->biClrImportant = le2me_32 (src->biClrImportant);
|
|
|
|
memcpy((char *) bih + sizeof (BITMAPINFOHEADER),
|
|
|
|
(char *) src + sizeof (BITMAPINFOHEADER),
|
|
|
|
track->private_size - sizeof (BITMAPINFOHEADER));
|
|
|
|
|
|
|
|
if (track->v_width == 0)
|
|
|
|
track->v_width = bih->biWidth;
|
|
|
|
if (track->v_height == 0)
|
|
|
|
track->v_height = bih->biHeight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-09 18:00:54 +00:00
|
|
|
bih = calloc (1, sizeof (BITMAPINFOHEADER));
|
2004-01-19 19:16:10 +00:00
|
|
|
bih->biSize = sizeof (BITMAPINFOHEADER);
|
|
|
|
bih->biWidth = track->v_width;
|
|
|
|
bih->biHeight = track->v_height;
|
|
|
|
bih->biBitCount = 24;
|
|
|
|
bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8;
|
|
|
|
|
2007-05-28 18:13:33 +00:00
|
|
|
if (track->private_size >= RVPROPERTIES_SIZE
|
2004-01-19 19:16:10 +00:00
|
|
|
&& (!strcmp (track->codec_id, MKV_V_REALV10)
|
|
|
|
|| !strcmp (track->codec_id, MKV_V_REALV20)
|
|
|
|
|| !strcmp (track->codec_id, MKV_V_REALV30)
|
|
|
|
|| !strcmp (track->codec_id, MKV_V_REALV40)))
|
|
|
|
{
|
|
|
|
unsigned char *dst, *src;
|
|
|
|
uint32_t type2;
|
2006-11-14 22:40:35 +00:00
|
|
|
unsigned int cnt;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2007-05-28 18:13:33 +00:00
|
|
|
src = track->private_data + RVPROPERTIES_SIZE;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2007-05-28 18:13:33 +00:00
|
|
|
cnt = track->private_size - RVPROPERTIES_SIZE;
|
2006-11-14 22:40:35 +00:00
|
|
|
bih = realloc(bih, sizeof (BITMAPINFOHEADER)+8+cnt);
|
|
|
|
bih->biSize = 48+cnt;
|
2004-01-19 19:16:10 +00:00
|
|
|
bih->biPlanes = 1;
|
2007-05-28 18:13:33 +00:00
|
|
|
type2 = AV_RB32(src - 4);
|
2004-01-19 19:16:10 +00:00
|
|
|
if (type2 == 0x10003000 || type2 == 0x10003001)
|
|
|
|
bih->biCompression=mmioFOURCC('R','V','1','3');
|
|
|
|
else
|
|
|
|
bih->biCompression=mmioFOURCC('R','V',track->codec_id[9],'0');
|
|
|
|
dst = (unsigned char *) (bih + 1);
|
2007-05-28 18:13:33 +00:00
|
|
|
// copy type1 and type2 info from rv properties
|
|
|
|
memcpy(dst, src - 8, 8);
|
2006-11-14 22:40:35 +00:00
|
|
|
stream_read(demuxer->stream, dst+8, cnt);
|
2004-01-19 19:16:10 +00:00
|
|
|
track->realmedia = 1;
|
|
|
|
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_QTX_CODECS
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else if (track->private_size >= sizeof (ImageDescription)
|
|
|
|
&& !strcmp(track->codec_id, MKV_V_QUICKTIME))
|
|
|
|
{
|
|
|
|
ImageDescriptionPtr idesc;
|
|
|
|
|
|
|
|
idesc = (ImageDescriptionPtr) track->private_data;
|
|
|
|
idesc->idSize = be2me_32 (idesc->idSize);
|
|
|
|
idesc->cType = be2me_32 (idesc->cType);
|
|
|
|
idesc->version = be2me_16 (idesc->version);
|
|
|
|
idesc->revisionLevel = be2me_16 (idesc->revisionLevel);
|
|
|
|
idesc->vendor = be2me_32 (idesc->vendor);
|
|
|
|
idesc->temporalQuality = be2me_32 (idesc->temporalQuality);
|
|
|
|
idesc->spatialQuality = be2me_32 (idesc->spatialQuality);
|
|
|
|
idesc->width = be2me_16 (idesc->width);
|
|
|
|
idesc->height = be2me_16 (idesc->height);
|
|
|
|
idesc->hRes = be2me_32 (idesc->hRes);
|
|
|
|
idesc->vRes = be2me_32 (idesc->vRes);
|
|
|
|
idesc->dataSize = be2me_32 (idesc->dataSize);
|
|
|
|
idesc->frameCount = be2me_16 (idesc->frameCount);
|
|
|
|
idesc->depth = be2me_16 (idesc->depth);
|
|
|
|
idesc->clutID = be2me_16 (idesc->clutID);
|
|
|
|
bih->biPlanes = 1;
|
|
|
|
bih->biCompression = idesc->cType;
|
|
|
|
ImageDesc = idesc;
|
2008-07-30 12:01:30 +00:00
|
|
|
#endif /* CONFIG_QTX_CODECS */
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
}
|
2007-05-10 13:58:57 +00:00
|
|
|
else
|
2005-01-11 08:30:56 +00:00
|
|
|
{
|
2007-05-10 13:58:57 +00:00
|
|
|
const videocodec_info_t *vi = vinfo;
|
|
|
|
while (vi->id && strcmp(vi->id, track->codec_id)) vi++;
|
|
|
|
bih->biCompression = vi->fourcc;
|
|
|
|
if (vi->extradata && track->private_data && (track->private_size > 0))
|
2005-01-11 08:30:56 +00:00
|
|
|
{
|
|
|
|
bih->biSize += track->private_size;
|
2006-10-09 17:41:37 +00:00
|
|
|
bih = realloc (bih, bih->biSize);
|
2005-01-11 08:30:56 +00:00
|
|
|
memcpy (bih + 1, track->private_data, track->private_size);
|
|
|
|
}
|
2008-04-16 04:11:12 +00:00
|
|
|
track->reorder_timecodes = opts->user_correct_pts == 0;
|
2007-05-10 13:58:57 +00:00
|
|
|
if (!vi->id) {
|
2007-05-10 14:05:06 +00:00
|
|
|
mp_msg (MSGT_DEMUX,MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownCodecID,
|
|
|
|
track->codec_id, track->tnum);
|
|
|
|
free(bih);
|
|
|
|
return 1;
|
2007-05-10 13:58:57 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-26 19:17:04 +00:00
|
|
|
sh_v = new_sh_video_vid (demuxer, track->tnum, vid);
|
2004-01-19 19:16:10 +00:00
|
|
|
sh_v->bih = bih;
|
|
|
|
sh_v->format = sh_v->bih->biCompression;
|
|
|
|
if (track->v_frate == 0.0)
|
|
|
|
track->v_frate = 25.0;
|
|
|
|
sh_v->fps = track->v_frate;
|
|
|
|
sh_v->frametime = 1 / track->v_frate;
|
2004-09-18 14:35:19 +00:00
|
|
|
sh_v->aspect = 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
if (!track->realmedia)
|
|
|
|
{
|
|
|
|
sh_v->disp_w = track->v_width;
|
|
|
|
sh_v->disp_h = track->v_height;
|
2004-09-18 14:35:19 +00:00
|
|
|
if (track->v_dheight)
|
2004-01-19 19:16:10 +00:00
|
|
|
sh_v->aspect = (float)track->v_dwidth / (float)track->v_dheight;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// vd_realvid.c will set aspect to disp_w/disp_h and rederive
|
|
|
|
// disp_w and disp_h from the RealVideo stream contents returned
|
|
|
|
// by the Real DLLs. If DisplayWidth/DisplayHeight was not set in
|
|
|
|
// the Matroska file then it has already been set to PixelWidth/Height
|
|
|
|
// by check_track_information.
|
|
|
|
sh_v->disp_w = track->v_dwidth;
|
|
|
|
sh_v->disp_h = track->v_dheight;
|
|
|
|
}
|
|
|
|
sh_v->ImageDesc = ImageDesc;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] Aspect: %f\n", sh_v->aspect);
|
|
|
|
|
|
|
|
sh_v->ds = demuxer->video;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-08-26 19:17:04 +00:00
|
|
|
demux_mkv_open_audio (demuxer_t *demuxer, mkv_track_t *track, int aid)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2006-06-19 18:34:29 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
2006-08-26 19:17:04 +00:00
|
|
|
sh_audio_t *sh_a = new_sh_audio_aid(demuxer, track->tnum, aid);
|
2006-07-10 02:51:52 +00:00
|
|
|
demux_packet_t *dp;
|
2006-06-19 18:34:29 +00:00
|
|
|
if(!sh_a) return 1;
|
|
|
|
mkv_d->audio_tracks[mkv_d->last_aid] = track->tnum;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2008-02-29 17:25:37 +00:00
|
|
|
if (track->language && (strcmp(track->language, "und") != 0))
|
|
|
|
sh_a->lang = strdup(track->language);
|
2008-03-30 16:55:46 +00:00
|
|
|
sh_a->default_track = track->default_track;
|
2004-01-19 19:16:10 +00:00
|
|
|
sh_a->ds = demuxer->audio;
|
2006-10-09 17:41:37 +00:00
|
|
|
sh_a->wf = malloc (sizeof (WAVEFORMATEX));
|
2004-04-02 07:36:42 +00:00
|
|
|
if (track->ms_compat && (track->private_size >= sizeof(WAVEFORMATEX)))
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2004-04-02 07:36:42 +00:00
|
|
|
WAVEFORMATEX *wf = (WAVEFORMATEX *)track->private_data;
|
2006-10-09 17:41:37 +00:00
|
|
|
sh_a->wf = realloc(sh_a->wf, track->private_size);
|
2004-04-02 07:36:42 +00:00
|
|
|
sh_a->wf->wFormatTag = le2me_16 (wf->wFormatTag);
|
|
|
|
sh_a->wf->nChannels = le2me_16 (wf->nChannels);
|
|
|
|
sh_a->wf->nSamplesPerSec = le2me_32 (wf->nSamplesPerSec);
|
|
|
|
sh_a->wf->nAvgBytesPerSec = le2me_32 (wf->nAvgBytesPerSec);
|
|
|
|
sh_a->wf->nBlockAlign = le2me_16 (wf->nBlockAlign);
|
|
|
|
sh_a->wf->wBitsPerSample = le2me_16 (wf->wBitsPerSample);
|
|
|
|
sh_a->wf->cbSize = track->private_size - sizeof(WAVEFORMATEX);
|
|
|
|
memcpy(sh_a->wf + 1, wf + 1, track->private_size - sizeof(WAVEFORMATEX));
|
2004-01-19 19:16:10 +00:00
|
|
|
if (track->a_sfreq == 0.0)
|
2004-04-02 07:36:42 +00:00
|
|
|
track->a_sfreq = sh_a->wf->nSamplesPerSec;
|
2004-01-19 19:16:10 +00:00
|
|
|
if (track->a_channels == 0)
|
2004-04-02 07:36:42 +00:00
|
|
|
track->a_channels = sh_a->wf->nChannels;
|
2004-01-19 19:16:10 +00:00
|
|
|
if (track->a_bps == 0)
|
2004-04-02 07:36:42 +00:00
|
|
|
track->a_bps = sh_a->wf->wBitsPerSample;
|
|
|
|
track->a_formattag = sh_a->wf->wFormatTag;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(sh_a->wf, 0, sizeof (WAVEFORMATEX));
|
|
|
|
if (!strcmp(track->codec_id, MKV_A_MP3) ||
|
|
|
|
!strcmp(track->codec_id, MKV_A_MP2))
|
|
|
|
track->a_formattag = 0x0055;
|
2004-10-30 08:47:04 +00:00
|
|
|
else if (!strncmp(track->codec_id, MKV_A_AC3, strlen(MKV_A_AC3)))
|
2004-01-19 19:16:10 +00:00
|
|
|
track->a_formattag = 0x2000;
|
2004-10-30 08:47:04 +00:00
|
|
|
else if (!strcmp(track->codec_id, MKV_A_DTS))
|
|
|
|
track->a_formattag = 0x2001;
|
2004-01-19 19:16:10 +00:00
|
|
|
else if (!strcmp(track->codec_id, MKV_A_PCM) ||
|
|
|
|
!strcmp(track->codec_id, MKV_A_PCM_BE))
|
|
|
|
track->a_formattag = 0x0001;
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_AAC_2MAIN) ||
|
|
|
|
!strncmp(track->codec_id, MKV_A_AAC_2LC,
|
|
|
|
strlen(MKV_A_AAC_2LC)) ||
|
|
|
|
!strcmp(track->codec_id, MKV_A_AAC_2SSR) ||
|
|
|
|
!strcmp(track->codec_id, MKV_A_AAC_4MAIN) ||
|
|
|
|
!strncmp(track->codec_id, MKV_A_AAC_4LC,
|
|
|
|
strlen(MKV_A_AAC_4LC)) ||
|
|
|
|
!strcmp(track->codec_id, MKV_A_AAC_4SSR) ||
|
2005-10-22 10:13:28 +00:00
|
|
|
!strcmp(track->codec_id, MKV_A_AAC_4LTP) ||
|
|
|
|
!strcmp(track->codec_id, MKV_A_AAC))
|
2004-01-19 19:16:10 +00:00
|
|
|
track->a_formattag = mmioFOURCC('M', 'P', '4', 'A');
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_VORBIS))
|
|
|
|
{
|
|
|
|
if (track->private_data == NULL)
|
|
|
|
return 1;
|
2005-02-27 18:11:17 +00:00
|
|
|
track->a_formattag = mmioFOURCC('v', 'r', 'b', 's');
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_QDMC))
|
|
|
|
track->a_formattag = mmioFOURCC('Q', 'D', 'M', 'C');
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_QDMC2))
|
|
|
|
track->a_formattag = mmioFOURCC('Q', 'D', 'M', '2');
|
2007-10-21 22:57:42 +00:00
|
|
|
else if (!strcmp(track->codec_id, MKV_A_WAVPACK))
|
|
|
|
track->a_formattag = mmioFOURCC('W', 'V', 'P', 'K');
|
2004-01-19 19:16:10 +00:00
|
|
|
else if (!strcmp(track->codec_id, MKV_A_FLAC))
|
|
|
|
{
|
|
|
|
if (track->private_data == NULL || track->private_size == 0)
|
|
|
|
{
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN,
|
|
|
|
MSGTR_MPDEMUX_MKV_FlacTrackDoesNotContainValidHeaders);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
track->a_formattag = mmioFOURCC ('f', 'L', 'a', 'C');
|
|
|
|
}
|
2007-05-28 18:13:33 +00:00
|
|
|
else if (track->private_size >= RAPROPERTIES4_SIZE)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
|
|
|
if (!strcmp(track->codec_id, MKV_A_REAL28))
|
|
|
|
track->a_formattag = mmioFOURCC('2', '8', '_', '8');
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_REALATRC))
|
|
|
|
track->a_formattag = mmioFOURCC('a', 't', 'r', 'c');
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_REALCOOK))
|
|
|
|
track->a_formattag = mmioFOURCC('c', 'o', 'o', 'k');
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_REALDNET))
|
|
|
|
track->a_formattag = mmioFOURCC('d', 'n', 'e', 't');
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_REALSIPR))
|
|
|
|
track->a_formattag = mmioFOURCC('s', 'i', 'p', 'r');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownAudioCodec,
|
|
|
|
track->codec_id, track->tnum);
|
2006-06-14 14:05:59 +00:00
|
|
|
free_sh_audio(demuxer, track->tnum);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sh_a->format = track->a_formattag;
|
|
|
|
sh_a->wf->wFormatTag = track->a_formattag;
|
|
|
|
sh_a->channels = track->a_channels;
|
|
|
|
sh_a->wf->nChannels = track->a_channels;
|
|
|
|
sh_a->samplerate = (uint32_t) track->a_sfreq;
|
|
|
|
sh_a->wf->nSamplesPerSec = (uint32_t) track->a_sfreq;
|
2004-09-21 18:02:33 +00:00
|
|
|
if (track->a_bps == 0)
|
|
|
|
{
|
|
|
|
sh_a->samplesize = 2;
|
|
|
|
sh_a->wf->wBitsPerSample = 16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sh_a->samplesize = track->a_bps / 8;
|
|
|
|
sh_a->wf->wBitsPerSample = track->a_bps;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
if (track->a_formattag == 0x0055) /* MP3 || MP2 */
|
|
|
|
{
|
|
|
|
sh_a->wf->nAvgBytesPerSec = 16000;
|
|
|
|
sh_a->wf->nBlockAlign = 1152;
|
|
|
|
}
|
2004-10-30 08:47:04 +00:00
|
|
|
else if ((track->a_formattag == 0x2000) || /* AC3 */
|
|
|
|
(track->a_formattag == 0x2001)) /* DTS */
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2007-06-10 16:45:30 +00:00
|
|
|
free(sh_a->wf);
|
|
|
|
sh_a->wf = NULL;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else if (track->a_formattag == 0x0001) /* PCM || PCM_BE */
|
|
|
|
{
|
|
|
|
sh_a->wf->nAvgBytesPerSec = sh_a->channels * sh_a->samplerate*2;
|
|
|
|
sh_a->wf->nBlockAlign = sh_a->wf->nAvgBytesPerSec;
|
|
|
|
if (!strcmp(track->codec_id, MKV_A_PCM_BE))
|
|
|
|
sh_a->format = mmioFOURCC('t', 'w', 'o', 's');
|
|
|
|
}
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_QDMC) ||
|
|
|
|
!strcmp(track->codec_id, MKV_A_QDMC2))
|
|
|
|
{
|
|
|
|
sh_a->wf->nAvgBytesPerSec = 16000;
|
|
|
|
sh_a->wf->nBlockAlign = 1486;
|
|
|
|
track->fix_i_bps = 1;
|
|
|
|
track->qt_last_a_pts = 0.0;
|
|
|
|
if (track->private_data != NULL)
|
|
|
|
{
|
2006-07-03 13:21:54 +00:00
|
|
|
sh_a->codecdata=malloc(track->private_size);
|
2004-01-19 19:16:10 +00:00
|
|
|
memcpy (sh_a->codecdata, track->private_data,
|
|
|
|
track->private_size);
|
|
|
|
sh_a->codecdata_len = track->private_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (track->a_formattag == mmioFOURCC('M', 'P', '4', 'A'))
|
|
|
|
{
|
|
|
|
int profile, srate_idx;
|
|
|
|
|
|
|
|
sh_a->wf->nAvgBytesPerSec = 16000;
|
|
|
|
sh_a->wf->nBlockAlign = 1024;
|
|
|
|
|
2005-10-22 10:13:28 +00:00
|
|
|
if (!strcmp (track->codec_id, MKV_A_AAC) &&
|
|
|
|
(NULL != track->private_data))
|
|
|
|
{
|
2006-07-03 13:21:54 +00:00
|
|
|
sh_a->codecdata=malloc(track->private_size);
|
2005-10-22 10:13:28 +00:00
|
|
|
memcpy (sh_a->codecdata, track->private_data,
|
|
|
|
track->private_size);
|
|
|
|
sh_a->codecdata_len = track->private_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
/* Recreate the 'private data' */
|
|
|
|
/* which faad2 uses in its initialization */
|
|
|
|
srate_idx = aac_get_sample_rate_index (sh_a->samplerate);
|
|
|
|
if (!strncmp (&track->codec_id[12], "MAIN", 4))
|
|
|
|
profile = 0;
|
|
|
|
else if (!strncmp (&track->codec_id[12], "LC", 2))
|
|
|
|
profile = 1;
|
|
|
|
else if (!strncmp (&track->codec_id[12], "SSR", 3))
|
|
|
|
profile = 2;
|
|
|
|
else
|
|
|
|
profile = 3;
|
2006-10-09 17:41:37 +00:00
|
|
|
sh_a->codecdata = malloc (5);
|
2004-01-19 19:16:10 +00:00
|
|
|
sh_a->codecdata[0] = ((profile+1) << 3) | ((srate_idx&0xE) >> 1);
|
|
|
|
sh_a->codecdata[1] = ((srate_idx&0x1)<<7)|(track->a_channels<<3);
|
|
|
|
|
|
|
|
if (strstr(track->codec_id, "SBR") != NULL)
|
|
|
|
{
|
|
|
|
/* HE-AAC (aka SBR AAC) */
|
|
|
|
sh_a->codecdata_len = 5;
|
|
|
|
|
|
|
|
sh_a->samplerate *= 2;
|
|
|
|
sh_a->wf->nSamplesPerSec *= 2;
|
|
|
|
srate_idx = aac_get_sample_rate_index(sh_a->samplerate);
|
|
|
|
sh_a->codecdata[2] = AAC_SYNC_EXTENSION_TYPE >> 3;
|
|
|
|
sh_a->codecdata[3] = ((AAC_SYNC_EXTENSION_TYPE&0x07)<<5) | 5;
|
|
|
|
sh_a->codecdata[4] = (1 << 7) | (srate_idx << 3);
|
|
|
|
track->default_duration = 1024.0 / (sh_a->samplerate / 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sh_a->codecdata_len = 2;
|
|
|
|
track->default_duration = 1024.0 / (float)sh_a->samplerate;
|
|
|
|
}
|
|
|
|
}
|
2005-02-27 18:11:17 +00:00
|
|
|
else if (track->a_formattag == mmioFOURCC('v', 'r', 'b', 's')) /* VORBIS */
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2005-05-12 17:35:58 +00:00
|
|
|
sh_a->wf->cbSize = track->private_size;
|
2006-10-09 17:41:37 +00:00
|
|
|
sh_a->wf = realloc(sh_a->wf, sizeof(WAVEFORMATEX) + sh_a->wf->cbSize);
|
2005-05-12 17:35:58 +00:00
|
|
|
memcpy((unsigned char *) (sh_a->wf+1), track->private_data, sh_a->wf->cbSize);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2007-05-28 18:13:33 +00:00
|
|
|
else if (track->private_size >= RAPROPERTIES4_SIZE
|
2004-01-19 19:16:10 +00:00
|
|
|
&& !strncmp (track->codec_id, MKV_A_REALATRC, 7))
|
|
|
|
{
|
|
|
|
/* Common initialization for all RealAudio codecs */
|
2007-05-28 18:13:33 +00:00
|
|
|
unsigned char *src = track->private_data;
|
2004-01-19 19:16:10 +00:00
|
|
|
int codecdata_length, version;
|
2007-05-28 18:13:33 +00:00
|
|
|
int flavor;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
sh_a->wf->nAvgBytesPerSec = 0; /* FIXME !? */
|
|
|
|
|
2007-05-28 18:13:33 +00:00
|
|
|
version = AV_RB16(src + 4);
|
|
|
|
flavor = AV_RB16(src + 22);
|
|
|
|
track->coded_framesize = AV_RB32(src + 24);
|
|
|
|
track->sub_packet_h = AV_RB16(src + 40);
|
|
|
|
sh_a->wf->nBlockAlign =
|
|
|
|
track->audiopk_size = AV_RB16(src + 42);
|
|
|
|
track->sub_packet_size = AV_RB16(src + 44);
|
2004-01-19 19:16:10 +00:00
|
|
|
if (version == 4)
|
|
|
|
{
|
2007-05-28 18:13:33 +00:00
|
|
|
src += RAPROPERTIES4_SIZE;
|
2004-01-19 19:16:10 +00:00
|
|
|
src += src[0] + 1;
|
|
|
|
src += src[0] + 1;
|
|
|
|
}
|
|
|
|
else
|
2007-05-28 18:13:33 +00:00
|
|
|
src += RAPROPERTIES5_SIZE;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
src += 3;
|
|
|
|
if (version == 5)
|
|
|
|
src++;
|
2007-05-28 18:13:33 +00:00
|
|
|
codecdata_length = AV_RB32(src);
|
2004-01-19 19:16:10 +00:00
|
|
|
src += 4;
|
2006-04-03 19:41:17 +00:00
|
|
|
sh_a->wf->cbSize = codecdata_length;
|
2006-10-09 17:41:37 +00:00
|
|
|
sh_a->wf = realloc (sh_a->wf,
|
|
|
|
sizeof (WAVEFORMATEX) +
|
|
|
|
sh_a->wf->cbSize);
|
2006-04-03 19:41:17 +00:00
|
|
|
memcpy(((char *)(sh_a->wf + 1)), src, codecdata_length);
|
|
|
|
|
|
|
|
switch (track->a_formattag) {
|
|
|
|
case mmioFOURCC('a', 't', 'r', 'c'):
|
2007-05-28 18:13:33 +00:00
|
|
|
sh_a->wf->nAvgBytesPerSec = atrc_fl2bps[flavor];
|
2006-04-03 19:41:17 +00:00
|
|
|
sh_a->wf->nBlockAlign = track->sub_packet_size;
|
|
|
|
track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
|
|
|
|
track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
|
|
|
|
break;
|
|
|
|
case mmioFOURCC('c', 'o', 'o', 'k'):
|
2007-05-28 18:13:33 +00:00
|
|
|
sh_a->wf->nAvgBytesPerSec = cook_fl2bps[flavor];
|
2006-04-03 19:41:17 +00:00
|
|
|
sh_a->wf->nBlockAlign = track->sub_packet_size;
|
|
|
|
track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
|
|
|
|
track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
|
|
|
|
break;
|
|
|
|
case mmioFOURCC('s', 'i', 'p', 'r'):
|
2007-05-28 18:13:33 +00:00
|
|
|
sh_a->wf->nAvgBytesPerSec = sipr_fl2bps[flavor];
|
2006-04-03 19:41:17 +00:00
|
|
|
sh_a->wf->nBlockAlign = track->coded_framesize;
|
|
|
|
track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
|
|
|
|
track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
|
|
|
|
break;
|
|
|
|
case mmioFOURCC('2', '8', '_', '8'):
|
|
|
|
sh_a->wf->nAvgBytesPerSec = 3600;
|
|
|
|
sh_a->wf->nBlockAlign = track->coded_framesize;
|
|
|
|
track->audio_buf = malloc(track->sub_packet_h * track->audiopk_size);
|
|
|
|
track->audio_timestamp = malloc(track->sub_packet_h * sizeof(float));
|
|
|
|
break;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
track->realmedia = 1;
|
|
|
|
}
|
|
|
|
else if (!strcmp(track->codec_id, MKV_A_FLAC) ||
|
|
|
|
(track->a_formattag == 0xf1ac))
|
|
|
|
{
|
|
|
|
unsigned char *ptr;
|
|
|
|
int size;
|
|
|
|
free(sh_a->wf);
|
|
|
|
sh_a->wf = NULL;
|
|
|
|
|
|
|
|
if (track->a_formattag == mmioFOURCC('f', 'L', 'a', 'C'))
|
|
|
|
{
|
2008-04-28 01:45:58 +00:00
|
|
|
ptr = track->private_data;
|
2004-01-19 19:16:10 +00:00
|
|
|
size = track->private_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sh_a->format = mmioFOURCC('f', 'L', 'a', 'C');
|
2008-04-28 01:45:58 +00:00
|
|
|
ptr = track->private_data
|
2004-01-19 19:16:10 +00:00
|
|
|
+ sizeof (WAVEFORMATEX);
|
|
|
|
size = track->private_size - sizeof (WAVEFORMATEX);
|
|
|
|
}
|
|
|
|
if (size < 4 || ptr[0] != 'f' || ptr[1] != 'L' ||
|
|
|
|
ptr[2] != 'a' || ptr[3] != 'C')
|
|
|
|
{
|
|
|
|
dp = new_demux_packet (4);
|
|
|
|
memcpy (dp->buffer, "fLaC", 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dp = new_demux_packet (size);
|
|
|
|
memcpy (dp->buffer, ptr, size);
|
|
|
|
}
|
|
|
|
dp->pts = 0;
|
|
|
|
dp->flags = 0;
|
|
|
|
ds_add_packet (demuxer->audio, dp);
|
|
|
|
}
|
2007-10-21 22:57:42 +00:00
|
|
|
else if (track->a_formattag == mmioFOURCC('W', 'V', 'P', 'K'))
|
|
|
|
{ /* do nothing, still works */ }
|
2004-04-02 07:36:42 +00:00
|
|
|
else if (!track->ms_compat || (track->private_size < sizeof(WAVEFORMATEX)))
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2006-06-14 14:05:59 +00:00
|
|
|
free_sh_audio(demuxer, track->tnum);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-11-13 16:15:23 +00:00
|
|
|
demux_mkv_open_sub (demuxer_t *demuxer, mkv_track_t *track, int sid)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
|
|
|
if (track->subtitle_type != MATROSKA_SUBTYPE_UNKNOWN)
|
|
|
|
{
|
2008-01-11 21:45:08 +00:00
|
|
|
int size, m;
|
|
|
|
uint8_t *buffer;
|
2006-11-13 16:15:23 +00:00
|
|
|
sh_sub_t *sh = new_sh_sub_sid(demuxer, track->tnum, sid);
|
2007-06-24 07:29:03 +00:00
|
|
|
track->sh_sub = sh;
|
|
|
|
sh->type = 't';
|
2007-02-05 18:25:28 +00:00
|
|
|
if (track->subtitle_type == MATROSKA_SUBTYPE_VOBSUB)
|
2007-06-24 07:29:03 +00:00
|
|
|
sh->type = 'v';
|
2007-02-05 18:25:28 +00:00
|
|
|
if (track->subtitle_type == MATROSKA_SUBTYPE_SSA)
|
2007-06-24 07:29:03 +00:00
|
|
|
sh->type = 'a';
|
2008-01-11 21:45:08 +00:00
|
|
|
size = track->private_size;
|
|
|
|
m = demux_mkv_decode (track,track->private_data,&buffer,&size,2);
|
|
|
|
if (buffer && m)
|
|
|
|
{
|
|
|
|
free (track->private_data);
|
|
|
|
track->private_data = buffer;
|
|
|
|
track->private_size = size;
|
|
|
|
}
|
2008-01-11 21:45:13 +00:00
|
|
|
sh->extradata=malloc(track->private_size);
|
|
|
|
memcpy (sh->extradata, track->private_data,
|
|
|
|
track->private_size);
|
|
|
|
sh->extradata_len = track->private_size;
|
2008-02-29 17:25:39 +00:00
|
|
|
if (track->language && (strcmp(track->language, "und") != 0))
|
|
|
|
sh->lang = strdup(track->language);
|
2008-03-30 16:55:46 +00:00
|
|
|
sh->default_track = track->default_track;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_MKV_SubtitleTypeNotSupported,
|
|
|
|
track->codec_id);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static int
|
2004-01-19 19:16:10 +00:00
|
|
|
demux_mkv_open (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
mkv_demuxer_t *mkv_d;
|
|
|
|
mkv_track_t *track;
|
|
|
|
int i, version, cont = 0;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
stream_seek(s, s->start_pos);
|
|
|
|
str = ebml_read_header (s, &version);
|
2006-09-25 21:17:43 +00:00
|
|
|
if (str == NULL || strcmp (str, "matroska") || version > 2)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2004-01-27 06:02:29 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_DBG2, "[mkv] no head found\n");
|
2004-01-19 19:16:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free (str);
|
|
|
|
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] Found the head...\n");
|
|
|
|
|
|
|
|
if (ebml_read_id (s, NULL) != MATROSKA_ID_SEGMENT)
|
|
|
|
{
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ebml_read_length (s, NULL); /* return bytes number until EOF */
|
|
|
|
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] + a segment...\n");
|
|
|
|
|
2006-10-09 18:00:54 +00:00
|
|
|
mkv_d = calloc (1, sizeof (mkv_demuxer_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
demuxer->priv = mkv_d;
|
|
|
|
mkv_d->tc_scale = 1000000;
|
|
|
|
mkv_d->segment_start = stream_tell (s);
|
2006-10-09 17:41:37 +00:00
|
|
|
mkv_d->parsed_cues = malloc (sizeof (off_t));
|
|
|
|
mkv_d->parsed_seekhead = malloc (sizeof (off_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
while (!cont)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, NULL))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_INFO:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ segment information...\n");
|
|
|
|
cont = demux_mkv_read_info (demuxer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_TRACKS:
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ segment tracks...\n");
|
|
|
|
cont = demux_mkv_read_tracks (demuxer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CUES:
|
|
|
|
cont = demux_mkv_read_cues (demuxer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_TAGS:
|
|
|
|
cont = demux_mkv_read_tags (demuxer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_SEEKHEAD:
|
|
|
|
cont = demux_mkv_read_seekhead (demuxer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CHAPTERS:
|
|
|
|
cont = demux_mkv_read_chapters (demuxer);
|
|
|
|
break;
|
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
case MATROSKA_ID_ATTACHMENTS:
|
|
|
|
cont = demux_mkv_read_attachments (demuxer);
|
|
|
|
break;
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
case MATROSKA_ID_CLUSTER:
|
|
|
|
{
|
|
|
|
int p, l;
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] |+ found cluster, headers are "
|
|
|
|
"parsed completely :)\n");
|
|
|
|
/* get the first cluster timecode */
|
|
|
|
p = stream_tell(s);
|
|
|
|
l = ebml_read_length (s, NULL);
|
|
|
|
while (ebml_read_id (s, NULL) != MATROSKA_ID_CLUSTERTIMECODE)
|
|
|
|
{
|
|
|
|
ebml_read_skip (s, NULL);
|
|
|
|
if (stream_tell (s) >= p + l)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (stream_tell (s) < p + l)
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, NULL);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0;
|
|
|
|
mkv_d->has_first_tc = 1;
|
|
|
|
}
|
|
|
|
stream_seek (s, p - 4);
|
|
|
|
cont = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
cont = 1;
|
|
|
|
case EBML_ID_VOID:
|
|
|
|
ebml_read_skip (s, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-26 19:17:04 +00:00
|
|
|
display_create_tracks (demuxer);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
/* select video track */
|
|
|
|
track = NULL;
|
|
|
|
if (demuxer->video->id == -1) /* automatically select a video track */
|
|
|
|
{
|
|
|
|
/* search for a video track that has the 'default' flag set */
|
|
|
|
for (i=0; i<mkv_d->num_tracks; i++)
|
|
|
|
if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO
|
|
|
|
&& mkv_d->tracks[i]->default_track)
|
|
|
|
{
|
|
|
|
track = mkv_d->tracks[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track == NULL)
|
|
|
|
/* no track has the 'default' flag set */
|
|
|
|
/* let's take the first video track */
|
|
|
|
for (i=0; i<mkv_d->num_tracks; i++)
|
|
|
|
if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO)
|
|
|
|
{
|
|
|
|
track = mkv_d->tracks[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (demuxer->video->id != -2) /* -2 = no video at all */
|
|
|
|
track = demux_mkv_find_track_by_num (mkv_d, demuxer->video->id,
|
|
|
|
MATROSKA_TRACK_VIDEO);
|
|
|
|
|
2006-08-26 19:17:04 +00:00
|
|
|
if (track && demuxer->v_streams[track->tnum])
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_INFO,
|
2006-11-05 13:53:27 +00:00
|
|
|
MSGTR_MPDEMUX_MKV_WillPlayVideoTrack, track->tnum);
|
2004-01-19 19:16:10 +00:00
|
|
|
demuxer->video->id = track->tnum;
|
|
|
|
demuxer->video->sh = demuxer->v_streams[track->tnum];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoVideoTrackFound);
|
2004-01-19 19:16:10 +00:00
|
|
|
demuxer->video->id = -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* select audio track */
|
|
|
|
track = NULL;
|
2008-02-29 17:25:54 +00:00
|
|
|
if (track == NULL)
|
|
|
|
/* search for an audio track that has the 'default' flag set */
|
|
|
|
for (i=0; i < mkv_d->num_tracks; i++)
|
|
|
|
if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO
|
|
|
|
&& mkv_d->tracks[i]->default_track)
|
|
|
|
{
|
|
|
|
track = mkv_d->tracks[i];
|
|
|
|
break;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2008-02-29 17:25:54 +00:00
|
|
|
if (track == NULL)
|
|
|
|
/* no track has the 'default' flag set */
|
|
|
|
/* let's take the first audio track */
|
|
|
|
for (i=0; i < mkv_d->num_tracks; i++)
|
|
|
|
if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO)
|
|
|
|
{
|
|
|
|
track = mkv_d->tracks[i];
|
|
|
|
break;
|
|
|
|
}
|
2008-02-29 17:25:52 +00:00
|
|
|
|
2007-07-16 14:01:53 +00:00
|
|
|
if (track && demuxer->a_streams[track->tnum])
|
|
|
|
{
|
|
|
|
demuxer->audio->id = track->tnum;
|
|
|
|
demuxer->audio->sh = demuxer->a_streams[track->tnum];
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
else
|
|
|
|
{
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoAudioTrackFound);
|
2004-01-22 21:43:36 +00:00
|
|
|
demuxer->audio->id = -2;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2006-06-19 18:34:29 +00:00
|
|
|
|
|
|
|
if(demuxer->audio->id != -2)
|
|
|
|
for (i=0; i < mkv_d->num_tracks; i++)
|
|
|
|
{
|
|
|
|
if(mkv_d->tracks[i]->type != MATROSKA_TRACK_AUDIO)
|
|
|
|
continue;
|
2006-08-26 19:17:04 +00:00
|
|
|
if(demuxer->a_streams[track->tnum])
|
2006-06-19 18:34:29 +00:00
|
|
|
{
|
|
|
|
mkv_d->last_aid++;
|
|
|
|
if(mkv_d->last_aid == MAX_A_STREAMS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-06 18:55:34 +00:00
|
|
|
if (demuxer->chapters)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2006-08-06 18:55:34 +00:00
|
|
|
for (i=0; i < (int)demuxer->num_chapters; i++)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2006-08-06 18:55:34 +00:00
|
|
|
demuxer->chapters[i].start -= mkv_d->first_tc;
|
|
|
|
demuxer->chapters[i].end -= mkv_d->first_tc;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2006-08-06 18:55:34 +00:00
|
|
|
if (dvd_last_chapter > 0 && dvd_last_chapter <= demuxer->num_chapters)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2006-08-06 18:55:34 +00:00
|
|
|
if (demuxer->chapters[dvd_last_chapter-1].end != 0)
|
|
|
|
mkv_d->stop_timecode = demuxer->chapters[dvd_last_chapter-1].end;
|
|
|
|
else if (dvd_last_chapter + 1 <= demuxer->num_chapters)
|
|
|
|
mkv_d->stop_timecode = demuxer->chapters[dvd_last_chapter].start;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->end_pos == 0 || (mkv_d->indexes == NULL && index_mode < 0))
|
|
|
|
demuxer->seekable = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
demuxer->movi_start = s->start_pos;
|
|
|
|
demuxer->movi_end = s->end_pos;
|
|
|
|
demuxer->seekable = 1;
|
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
return DEMUXER_TYPE_MATROSKA;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static void
|
2004-01-19 19:16:10 +00:00
|
|
|
demux_close_mkv (demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
|
|
|
|
if (mkv_d)
|
|
|
|
{
|
|
|
|
int i;
|
2004-11-26 16:36:03 +00:00
|
|
|
free_cached_dps (demuxer);
|
2004-01-19 19:16:10 +00:00
|
|
|
if (mkv_d->tracks)
|
|
|
|
{
|
|
|
|
for (i=0; i<mkv_d->num_tracks; i++)
|
2006-10-09 18:48:55 +00:00
|
|
|
demux_mkv_free_trackentry(mkv_d->tracks[i]);
|
2004-01-19 19:16:10 +00:00
|
|
|
free (mkv_d->tracks);
|
|
|
|
}
|
2008-02-29 17:25:41 +00:00
|
|
|
free (mkv_d->indexes);
|
|
|
|
free (mkv_d->cluster_positions);
|
|
|
|
free (mkv_d->parsed_cues);
|
|
|
|
free (mkv_d->parsed_seekhead);
|
2004-01-19 19:16:10 +00:00
|
|
|
free (mkv_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
demux_mkv_read_block_lacing (uint8_t *buffer, uint64_t *size,
|
2004-03-25 20:47:37 +00:00
|
|
|
uint8_t *laces, uint32_t **all_lace_sizes)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2004-03-25 20:47:37 +00:00
|
|
|
uint32_t total = 0, *lace_size;
|
2004-01-19 19:16:10 +00:00
|
|
|
uint8_t flags;
|
|
|
|
int i;
|
|
|
|
|
2004-03-25 20:47:37 +00:00
|
|
|
*all_lace_sizes = NULL;
|
2004-08-25 07:52:52 +00:00
|
|
|
lace_size = NULL;
|
2004-01-19 19:16:10 +00:00
|
|
|
/* lacing flags */
|
|
|
|
flags = *buffer++;
|
|
|
|
(*size)--;
|
|
|
|
|
|
|
|
switch ((flags & 0x06) >> 1)
|
|
|
|
{
|
|
|
|
case 0: /* no lacing */
|
|
|
|
*laces = 1;
|
2006-07-03 13:21:54 +00:00
|
|
|
lace_size = calloc(*laces, sizeof(uint32_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
lace_size[0] = *size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* xiph lacing */
|
|
|
|
case 2: /* fixed-size lacing */
|
|
|
|
case 3: /* EBML lacing */
|
|
|
|
*laces = *buffer++;
|
|
|
|
(*size)--;
|
2004-03-25 20:47:37 +00:00
|
|
|
(*laces)++;
|
2006-07-03 13:21:54 +00:00
|
|
|
lace_size = calloc(*laces, sizeof(uint32_t));
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
switch ((flags & 0x06) >> 1)
|
|
|
|
{
|
|
|
|
case 1: /* xiph lacing */
|
|
|
|
for (i=0; i < *laces-1; i++)
|
|
|
|
{
|
|
|
|
lace_size[i] = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
lace_size[i] += *buffer;
|
|
|
|
(*size)--;
|
|
|
|
} while (*buffer++ == 0xFF);
|
|
|
|
total += lace_size[i];
|
|
|
|
}
|
|
|
|
lace_size[i] = *size - total;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* fixed-size lacing */
|
|
|
|
for (i=0; i < *laces; i++)
|
|
|
|
lace_size[i] = *size / *laces;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* EBML lacing */
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
uint64_t num = ebml_read_vlen_uint (buffer, &l);
|
2004-03-25 20:47:37 +00:00
|
|
|
if (num == EBML_UINT_INVALID) {
|
|
|
|
free(lace_size);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 1;
|
2004-03-25 20:47:37 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
buffer += l;
|
|
|
|
*size -= l;
|
|
|
|
|
|
|
|
total = lace_size[0] = num;
|
|
|
|
for (i=1; i < *laces-1; i++)
|
|
|
|
{
|
|
|
|
int64_t snum;
|
|
|
|
snum = ebml_read_vlen_int (buffer, &l);
|
2004-03-25 20:47:37 +00:00
|
|
|
if (snum == EBML_INT_INVALID) {
|
|
|
|
free(lace_size);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 1;
|
2004-03-25 20:47:37 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
buffer += l;
|
|
|
|
*size -= l;
|
|
|
|
lace_size[i] = lace_size[i-1] + snum;
|
|
|
|
total += lace_size[i];
|
|
|
|
}
|
|
|
|
lace_size[i] = *size - total;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-03-25 20:47:37 +00:00
|
|
|
*all_lace_sizes = lace_size;
|
2004-01-19 19:16:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_subtitles(demuxer_t *demuxer, mkv_track_t *track, char *block,
|
|
|
|
int64_t size, uint64_t block_duration, uint64_t timecode)
|
|
|
|
{
|
2007-02-05 18:25:28 +00:00
|
|
|
demux_packet_t *dp;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
if (block_duration == 0)
|
|
|
|
{
|
2006-11-05 13:53:27 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_WARN,
|
|
|
|
MSGTR_MPDEMUX_MKV_NoBlockDurationForSubtitleTrackFound);
|
2004-01-19 19:16:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-01-16 12:17:41 +00:00
|
|
|
sub_utf8 = 1;
|
2007-02-05 18:25:28 +00:00
|
|
|
dp = new_demux_packet(size);
|
2008-01-11 21:45:17 +00:00
|
|
|
memcpy(dp->buffer, block, size);
|
2007-02-05 18:25:28 +00:00
|
|
|
dp->pts = timecode / 1000.0f;
|
|
|
|
dp->endpts = (timecode + block_duration) / 1000.0f;
|
|
|
|
ds_add_packet(demuxer->sub, dp);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2009-02-09 22:00:57 +00:00
|
|
|
double real_fix_timestamp(unsigned char *buf, unsigned int timestamp, unsigned int format, int64_t *kf_base, int *kf_pts, double *pts);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
handle_realvideo (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
|
|
|
|
uint32_t size, int block_bref)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
demux_packet_t *dp;
|
2007-05-28 18:13:33 +00:00
|
|
|
uint32_t timestamp = mkv_d->last_pts * 1000;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-02-09 22:00:57 +00:00
|
|
|
dp = new_demux_packet (size);
|
|
|
|
memcpy (dp->buffer, buffer, size);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
if (mkv_d->v_skip_to_keyframe)
|
|
|
|
{
|
|
|
|
dp->pts = mkv_d->last_pts;
|
|
|
|
track->rv_kf_base = 0;
|
2007-05-28 18:13:33 +00:00
|
|
|
track->rv_kf_pts = timestamp;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else
|
2009-02-09 22:00:57 +00:00
|
|
|
dp->pts = real_fix_timestamp (dp->buffer, timestamp,
|
|
|
|
((sh_video_t*)demuxer->video->sh)->bih->biCompression,
|
|
|
|
&track->rv_kf_base, &track->rv_kf_pts, NULL);
|
2004-01-19 19:16:10 +00:00
|
|
|
dp->pos = demuxer->filepos;
|
|
|
|
dp->flags = block_bref ? 0 : 0x10;
|
|
|
|
|
|
|
|
ds_add_packet(demuxer->video, dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_realaudio (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
|
|
|
|
uint32_t size, int block_bref)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
2006-04-03 19:41:17 +00:00
|
|
|
int sps = track->sub_packet_size;
|
|
|
|
int sph = track->sub_packet_h;
|
|
|
|
int cfs = track->coded_framesize;
|
|
|
|
int w = track->audiopk_size;
|
|
|
|
int spc = track->sub_packet_cnt;
|
|
|
|
demux_packet_t *dp;
|
|
|
|
int x;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2006-04-03 19:41:17 +00:00
|
|
|
if ((track->a_formattag == mmioFOURCC('2', '8', '_', '8')) ||
|
|
|
|
(track->a_formattag == mmioFOURCC('c', 'o', 'o', 'k')) ||
|
|
|
|
(track->a_formattag == mmioFOURCC('a', 't', 'r', 'c')) ||
|
|
|
|
(track->a_formattag == mmioFOURCC('s', 'i', 'p', 'r')))
|
|
|
|
{
|
|
|
|
// if(!block_bref)
|
|
|
|
// spc = track->sub_packet_cnt = 0;
|
|
|
|
switch (track->a_formattag) {
|
|
|
|
case mmioFOURCC('2', '8', '_', '8'):
|
|
|
|
for (x = 0; x < sph / 2; x++)
|
|
|
|
memcpy(track->audio_buf + x * 2 * w + spc * cfs, buffer + cfs * x, cfs);
|
|
|
|
break;
|
|
|
|
case mmioFOURCC('c', 'o', 'o', 'k'):
|
|
|
|
case mmioFOURCC('a', 't', 'r', 'c'):
|
|
|
|
for (x = 0; x < w / sps; x++)
|
|
|
|
memcpy(track->audio_buf + sps * (sph * x + ((sph + 1) / 2) * (spc & 1) + (spc >> 1)), buffer + sps * x, sps);
|
|
|
|
break;
|
|
|
|
case mmioFOURCC('s', 'i', 'p', 'r'):
|
|
|
|
memcpy(track->audio_buf + spc * w, buffer, w);
|
|
|
|
if (spc == sph - 1)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int bs = sph * w * 2 / 96; // nibbles per subpacket
|
|
|
|
// Perform reordering
|
|
|
|
for(n=0; n < 38; n++)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
int i = bs * sipr_swaps[n][0];
|
|
|
|
int o = bs * sipr_swaps[n][1];
|
|
|
|
// swap nibbles of block 'i' with 'o' TODO: optimize
|
|
|
|
for(j = 0;j < bs; j++)
|
|
|
|
{
|
|
|
|
int x = (i & 1) ? (track->audio_buf[i >> 1] >> 4) : (track->audio_buf[i >> 1] & 0x0F);
|
|
|
|
int y = (o & 1) ? (track->audio_buf[o >> 1] >> 4) : (track->audio_buf[o >> 1] & 0x0F);
|
|
|
|
if(o & 1)
|
|
|
|
track->audio_buf[o >> 1] = (track->audio_buf[o >> 1] & 0x0F) | (x << 4);
|
|
|
|
else
|
|
|
|
track->audio_buf[o >> 1] = (track->audio_buf[o >> 1] & 0xF0) | x;
|
|
|
|
if(i & 1)
|
|
|
|
track->audio_buf[i >> 1] = (track->audio_buf[i >> 1] & 0x0F) | (y << 4);
|
|
|
|
else
|
|
|
|
track->audio_buf[i >> 1] = (track->audio_buf[i >> 1] & 0xF0) | y;
|
|
|
|
++i; ++o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
track->audio_timestamp[track->sub_packet_cnt] = (track->ra_pts == mkv_d->last_pts) ? 0 : (mkv_d->last_pts);
|
|
|
|
track->ra_pts = mkv_d->last_pts;
|
|
|
|
if (track->sub_packet_cnt == 0)
|
|
|
|
track->audio_filepos = demuxer->filepos;
|
|
|
|
if (++(track->sub_packet_cnt) == sph)
|
|
|
|
{
|
|
|
|
int apk_usize = ((WAVEFORMATEX*)((sh_audio_t*)demuxer->audio->sh)->wf)->nBlockAlign;
|
|
|
|
track->sub_packet_cnt = 0;
|
|
|
|
// Release all the audio packets
|
|
|
|
for (x = 0; x < sph*w/apk_usize; x++)
|
|
|
|
{
|
|
|
|
dp = new_demux_packet(apk_usize);
|
|
|
|
memcpy(dp->buffer, track->audio_buf + x * apk_usize, apk_usize);
|
|
|
|
/* Put timestamp only on packets that correspond to original audio packets in file */
|
|
|
|
dp->pts = (x * apk_usize % w) ? 0 : track->audio_timestamp[x * apk_usize / w];
|
|
|
|
dp->pos = track->audio_filepos; // all equal
|
|
|
|
dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
|
|
|
|
ds_add_packet(demuxer->audio, dp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Not a codec that require reordering
|
|
|
|
dp = new_demux_packet (size);
|
|
|
|
memcpy(dp->buffer, buffer, size);
|
2004-01-19 19:16:10 +00:00
|
|
|
if (track->ra_pts == mkv_d->last_pts && !mkv_d->a_skip_to_keyframe)
|
|
|
|
dp->pts = 0;
|
|
|
|
else
|
|
|
|
dp->pts = mkv_d->last_pts;
|
|
|
|
track->ra_pts = mkv_d->last_pts;
|
|
|
|
|
|
|
|
dp->pos = demuxer->filepos;
|
|
|
|
dp->flags = block_bref ? 0 : 0x10;
|
|
|
|
ds_add_packet (demuxer->audio, dp);
|
2006-04-03 19:41:17 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2004-11-26 16:36:03 +00:00
|
|
|
/** Reorder timecodes and add cached demux packets to the queues.
|
|
|
|
*
|
|
|
|
* Timecode reordering is needed if a video track contains B frames that
|
|
|
|
* are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
|
|
|
|
* MPlayer doesn't like timestamps in display order. This function adjusts
|
|
|
|
* the timestamp of cached frames (which are exactly one I/P frame followed
|
|
|
|
* by one or more B frames) so that they are in coding order again.
|
|
|
|
*
|
|
|
|
* Example: The track with 25 FPS contains four frames with the timecodes
|
|
|
|
* I at 0ms, P at 120ms, B at 40ms and B at 80ms. As soon as the next I
|
|
|
|
* or P frame arrives these timecodes can be changed to I at 0ms, P at 40ms,
|
|
|
|
* B at 80ms and B at 120ms.
|
|
|
|
*
|
2005-11-05 04:56:23 +00:00
|
|
|
* This works for simple H.264 B-frame pyramids, but not for arbitrary orders.
|
|
|
|
*
|
2004-11-26 16:36:03 +00:00
|
|
|
* \param demuxer The Matroska demuxer struct for this instance.
|
|
|
|
* \param track The track structure whose cache should be handled.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flush_cached_dps (demuxer_t *demuxer, mkv_track_t *track)
|
|
|
|
{
|
2005-11-05 04:56:23 +00:00
|
|
|
int i, ok;
|
2004-11-26 16:36:03 +00:00
|
|
|
|
|
|
|
if (track->num_cached_dps == 0)
|
|
|
|
return;
|
2005-11-05 04:56:23 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
ok = 1;
|
|
|
|
for (i = 1; i < track->num_cached_dps; i++)
|
|
|
|
if (track->cached_dps[i - 1]->pts > track->cached_dps[i]->pts) {
|
|
|
|
float tmp_pts = track->cached_dps[i - 1]->pts;
|
|
|
|
track->cached_dps[i - 1]->pts = track->cached_dps[i]->pts;
|
|
|
|
track->cached_dps[i]->pts = tmp_pts;
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
} while (!ok);
|
2004-11-26 16:36:03 +00:00
|
|
|
|
|
|
|
for (i = 0; i < track->num_cached_dps; i++)
|
|
|
|
ds_add_packet (demuxer->video, track->cached_dps[i]);
|
|
|
|
track->num_cached_dps = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Cache video frames if timecodes have to be reordered.
|
|
|
|
*
|
|
|
|
* Timecode reordering is needed if a video track contains B frames that
|
|
|
|
* are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4).
|
|
|
|
* This function takes in a Matroska block read from the file, allocates a
|
|
|
|
* demux packet for it, fills in its values, allocates space for storing
|
|
|
|
* pointers to the cached demux packets and adds the packet to it. If
|
|
|
|
* the packet contains an I or a P frame then ::flush_cached_dps is called
|
|
|
|
* in order to send the old cached frames downstream.
|
|
|
|
*
|
|
|
|
* \param demuxer The Matroska demuxer struct for this instance.
|
|
|
|
* \param track The packet is meant for this track.
|
|
|
|
* \param buffer The actual frame contents.
|
|
|
|
* \param size The frame size in bytes.
|
|
|
|
* \param block_bref A relative timecode (backward reference). If it is \c 0
|
|
|
|
* then the frame is an I frame.
|
|
|
|
* \param block_fref A relative timecode (forward reference). If it is \c 0
|
|
|
|
* then the frame is either an I frame or a P frame depending on the value
|
|
|
|
* of \a block_bref. Otherwise it's a B frame.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
handle_video_bframes (demuxer_t *demuxer, mkv_track_t *track, uint8_t *buffer,
|
|
|
|
uint32_t size, int block_bref, int block_fref)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
demux_packet_t *dp;
|
|
|
|
|
|
|
|
dp = new_demux_packet (size);
|
|
|
|
memcpy(dp->buffer, buffer, size);
|
|
|
|
dp->pos = demuxer->filepos;
|
|
|
|
dp->pts = mkv_d->last_pts;
|
2005-01-16 14:59:19 +00:00
|
|
|
if ((track->num_cached_dps > 0) && (dp->pts < track->max_pts))
|
|
|
|
block_fref = 1;
|
2004-11-26 16:36:03 +00:00
|
|
|
if (block_fref == 0) /* I or P frame */
|
|
|
|
flush_cached_dps (demuxer, track);
|
|
|
|
if (block_bref != 0) /* I frame, don't cache it */
|
|
|
|
dp->flags = 0x10;
|
|
|
|
if ((track->num_cached_dps + 1) > track->num_allocated_dps)
|
|
|
|
{
|
|
|
|
track->cached_dps = (demux_packet_t **)
|
|
|
|
realloc(track->cached_dps, (track->num_cached_dps + 10) *
|
|
|
|
sizeof(demux_packet_t *));
|
|
|
|
track->num_allocated_dps += 10;
|
|
|
|
}
|
|
|
|
track->cached_dps[track->num_cached_dps] = dp;
|
|
|
|
track->num_cached_dps++;
|
2005-01-16 14:59:19 +00:00
|
|
|
if (dp->pts > track->max_pts)
|
|
|
|
track->max_pts = dp->pts;
|
2004-11-26 16:36:03 +00:00
|
|
|
}
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
static int
|
|
|
|
handle_block (demuxer_t *demuxer, uint8_t *block, uint64_t length,
|
2006-09-25 21:17:43 +00:00
|
|
|
uint64_t block_duration, int64_t block_bref, int64_t block_fref, uint8_t simpleblock)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
mkv_track_t *track = NULL;
|
|
|
|
demux_stream_t *ds = NULL;
|
|
|
|
uint64_t old_length;
|
|
|
|
int64_t tc;
|
2004-03-25 20:47:37 +00:00
|
|
|
uint32_t *lace_size;
|
2006-09-25 21:17:43 +00:00
|
|
|
uint8_t laces, flags;
|
2004-01-19 19:16:10 +00:00
|
|
|
int i, num, tmp, use_this_block = 1;
|
|
|
|
float current_pts;
|
|
|
|
int16_t time;
|
|
|
|
|
|
|
|
/* first byte(s): track num */
|
|
|
|
num = ebml_read_vlen_uint (block, &tmp);
|
|
|
|
block += tmp;
|
|
|
|
/* time (relative to cluster time) */
|
2005-01-13 21:04:34 +00:00
|
|
|
time = block[0] << 8 | block[1];
|
2004-01-19 19:16:10 +00:00
|
|
|
block += 2;
|
|
|
|
length -= tmp + 2;
|
|
|
|
old_length = length;
|
2006-09-25 21:17:43 +00:00
|
|
|
flags = block[0];
|
2004-03-25 20:47:37 +00:00
|
|
|
if (demux_mkv_read_block_lacing (block, &length, &laces, &lace_size))
|
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
block += old_length - length;
|
|
|
|
|
|
|
|
tc = ((time*mkv_d->tc_scale+mkv_d->cluster_tc) /1000000.0 - mkv_d->first_tc);
|
|
|
|
if (tc < 0)
|
|
|
|
tc = 0;
|
2004-03-25 20:47:37 +00:00
|
|
|
if (mkv_d->stop_timecode > 0 && tc > mkv_d->stop_timecode) {
|
|
|
|
free(lace_size);
|
2004-01-19 19:16:10 +00:00
|
|
|
return -1;
|
2004-03-25 20:47:37 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
current_pts = tc / 1000.0;
|
|
|
|
|
|
|
|
for (i=0; i<mkv_d->num_tracks; i++)
|
2004-06-28 16:43:01 +00:00
|
|
|
if (mkv_d->tracks[i]->tnum == num) {
|
2004-01-19 19:16:10 +00:00
|
|
|
track = mkv_d->tracks[i];
|
2004-06-28 16:43:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (track == NULL)
|
|
|
|
{
|
|
|
|
free(lace_size);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
if (num == demuxer->audio->id)
|
|
|
|
{
|
|
|
|
ds = demuxer->audio;
|
|
|
|
|
2006-09-25 21:17:43 +00:00
|
|
|
if (mkv_d->a_skip_to_keyframe)
|
|
|
|
{
|
|
|
|
if (simpleblock)
|
|
|
|
{
|
|
|
|
if (!(flags&0x80)) /*current frame isn't a keyframe*/
|
|
|
|
use_this_block = 0;
|
|
|
|
}
|
|
|
|
else if (block_bref != 0)
|
|
|
|
use_this_block = 0;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
else if (mkv_d->v_skip_to_keyframe)
|
|
|
|
use_this_block = 0;
|
|
|
|
|
|
|
|
if (track->fix_i_bps && use_this_block)
|
|
|
|
{
|
|
|
|
sh_audio_t *sh = (sh_audio_t *) ds->sh;
|
|
|
|
|
|
|
|
if (block_duration != 0)
|
|
|
|
{
|
|
|
|
sh->i_bps = length * 1000 / block_duration;
|
|
|
|
track->fix_i_bps = 0;
|
|
|
|
}
|
|
|
|
else if (track->qt_last_a_pts == 0.0)
|
|
|
|
track->qt_last_a_pts = current_pts;
|
|
|
|
else if(track->qt_last_a_pts != current_pts)
|
|
|
|
{
|
|
|
|
sh->i_bps = length / (current_pts - track->qt_last_a_pts);
|
|
|
|
track->fix_i_bps = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tc < mkv_d->skip_to_timecode)
|
|
|
|
use_this_block = 0;
|
|
|
|
else if (num == demuxer->video->id)
|
|
|
|
{
|
|
|
|
ds = demuxer->video;
|
2006-09-25 21:17:43 +00:00
|
|
|
if (mkv_d->v_skip_to_keyframe)
|
|
|
|
{
|
|
|
|
if (simpleblock)
|
|
|
|
{
|
|
|
|
if (!(flags&0x80)) /*current frame isn't a keyframe*/
|
|
|
|
use_this_block = 0;
|
|
|
|
}
|
|
|
|
else if (block_bref != 0 || block_fref != 0)
|
|
|
|
use_this_block = 0;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
else if (num == demuxer->sub->id)
|
|
|
|
{
|
|
|
|
ds = demuxer->sub;
|
|
|
|
if (track->subtitle_type != MATROSKA_SUBTYPE_VOBSUB)
|
|
|
|
{
|
|
|
|
if (!mkv_d->v_skip_to_keyframe)
|
|
|
|
handle_subtitles (demuxer, track, block, length,
|
|
|
|
block_duration, tc);
|
|
|
|
use_this_block = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
use_this_block = 0;
|
|
|
|
|
|
|
|
if (use_this_block)
|
|
|
|
{
|
2005-06-09 07:45:17 +00:00
|
|
|
mkv_d->last_pts = current_pts;
|
2004-01-19 19:16:10 +00:00
|
|
|
mkv_d->last_filepos = demuxer->filepos;
|
|
|
|
|
|
|
|
for (i=0; i < laces; i++)
|
|
|
|
{
|
|
|
|
if (ds == demuxer->video && track->realmedia)
|
|
|
|
handle_realvideo (demuxer, track, block, lace_size[i], block_bref);
|
|
|
|
else if (ds == demuxer->audio && track->realmedia)
|
|
|
|
handle_realaudio (demuxer, track, block, lace_size[i], block_bref);
|
2004-11-26 16:36:03 +00:00
|
|
|
else if (ds == demuxer->video && track->reorder_timecodes)
|
|
|
|
handle_video_bframes (demuxer, track, block, lace_size[i],
|
|
|
|
block_bref, block_fref);
|
2004-01-19 19:16:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int modified, size = lace_size[i];
|
|
|
|
demux_packet_t *dp;
|
|
|
|
uint8_t *buffer;
|
|
|
|
modified = demux_mkv_decode (track, block, &buffer, &size, 1);
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
dp = new_demux_packet (size);
|
|
|
|
memcpy (dp->buffer, buffer, size);
|
|
|
|
if (modified)
|
|
|
|
free (buffer);
|
2006-04-14 21:08:25 +00:00
|
|
|
dp->flags = (block_bref == 0 && block_fref == 0) ? 0x10 : 0;
|
2006-04-24 06:28:45 +00:00
|
|
|
/* If default_duration is 0, assume no pts value is known
|
|
|
|
* for packets after the first one (rather than all pts
|
|
|
|
* values being the same) */
|
|
|
|
if (i == 0 || track->default_duration)
|
2004-01-19 19:16:10 +00:00
|
|
|
dp->pts = mkv_d->last_pts + i * track->default_duration;
|
|
|
|
ds_add_packet (ds, dp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block += lace_size[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ds == demuxer->video)
|
|
|
|
{
|
|
|
|
mkv_d->v_skip_to_keyframe = 0;
|
|
|
|
mkv_d->skip_to_timecode = 0;
|
|
|
|
}
|
|
|
|
else if (ds == demuxer->audio)
|
|
|
|
mkv_d->a_skip_to_keyframe = 0;
|
|
|
|
|
2004-03-25 20:47:37 +00:00
|
|
|
free(lace_size);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-25 20:47:37 +00:00
|
|
|
free(lace_size);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static int
|
|
|
|
demux_mkv_fill_buffer (demuxer_t *demuxer, demux_stream_t *ds)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t l;
|
|
|
|
int il, tmp;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
while (mkv_d->cluster_size > 0)
|
|
|
|
{
|
|
|
|
uint64_t block_duration = 0, block_length = 0;
|
2004-11-26 16:36:03 +00:00
|
|
|
int64_t block_bref = 0, block_fref = 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
uint8_t *block = NULL;
|
|
|
|
|
|
|
|
while (mkv_d->blockgroup_size > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_BLOCKDURATION:
|
|
|
|
{
|
|
|
|
block_duration = ebml_read_uint (s, &l);
|
2007-07-05 22:06:05 +00:00
|
|
|
if (block_duration == EBML_UINT_INVALID) {
|
|
|
|
free(block);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 0;
|
2007-07-05 22:06:05 +00:00
|
|
|
}
|
2006-09-12 21:40:01 +00:00
|
|
|
block_duration *= mkv_d->tc_scale / 1000000.0;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_BLOCK:
|
|
|
|
block_length = ebml_read_length (s, &tmp);
|
2006-10-11 15:57:51 +00:00
|
|
|
free(block);
|
2009-02-02 20:22:36 +00:00
|
|
|
if (block_length > SIZE_MAX - AV_LZO_INPUT_PADDING) return 0;
|
|
|
|
block = malloc (block_length + AV_LZO_INPUT_PADDING);
|
2004-01-19 19:16:10 +00:00
|
|
|
demuxer->filepos = stream_tell (s);
|
|
|
|
if (stream_read (s,block,block_length) != (int) block_length)
|
2006-10-11 15:57:51 +00:00
|
|
|
{
|
|
|
|
free(block);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 0;
|
2006-10-11 15:57:51 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
l = tmp + block_length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_REFERENCEBLOCK:
|
|
|
|
{
|
|
|
|
int64_t num = ebml_read_int (s, &l);
|
2007-07-05 22:06:05 +00:00
|
|
|
if (num == EBML_INT_INVALID) {
|
|
|
|
free(block);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 0;
|
2007-07-05 22:06:05 +00:00
|
|
|
}
|
2004-11-26 16:36:03 +00:00
|
|
|
if (num <= 0)
|
2004-01-19 19:16:10 +00:00
|
|
|
block_bref = num;
|
2004-11-26 16:36:03 +00:00
|
|
|
else
|
|
|
|
block_fref = num;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-02-09 19:21:08 +00:00
|
|
|
case EBML_ID_INVALID:
|
2007-07-05 22:06:05 +00:00
|
|
|
free(block);
|
2004-02-09 19:21:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mkv_d->blockgroup_size -= l + il;
|
|
|
|
mkv_d->cluster_size -= l + il;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block)
|
|
|
|
{
|
|
|
|
int res = handle_block (demuxer, block, block_length,
|
2006-09-25 21:17:43 +00:00
|
|
|
block_duration, block_bref, block_fref, 0);
|
2004-01-19 19:16:10 +00:00
|
|
|
free (block);
|
|
|
|
if (res < 0)
|
|
|
|
return 0;
|
|
|
|
if (res)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkv_d->cluster_size > 0)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &il))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CLUSTERTIMECODE:
|
|
|
|
{
|
|
|
|
uint64_t num = ebml_read_uint (s, &l);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
return 0;
|
|
|
|
if (!mkv_d->has_first_tc)
|
|
|
|
{
|
|
|
|
mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0;
|
|
|
|
mkv_d->has_first_tc = 1;
|
|
|
|
}
|
|
|
|
mkv_d->cluster_tc = num * mkv_d->tc_scale;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MATROSKA_ID_BLOCKGROUP:
|
|
|
|
mkv_d->blockgroup_size = ebml_read_length (s, &tmp);
|
|
|
|
l = tmp;
|
|
|
|
break;
|
|
|
|
|
2006-09-25 21:17:43 +00:00
|
|
|
case MATROSKA_ID_SIMPLEBLOCK:
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
block_length = ebml_read_length (s, &tmp);
|
2006-10-09 17:41:37 +00:00
|
|
|
block = malloc (block_length);
|
2006-09-25 21:17:43 +00:00
|
|
|
demuxer->filepos = stream_tell (s);
|
|
|
|
if (stream_read (s,block,block_length) != (int) block_length)
|
2006-10-11 15:57:51 +00:00
|
|
|
{
|
|
|
|
free(block);
|
2006-09-25 21:17:43 +00:00
|
|
|
return 0;
|
2006-10-11 15:57:51 +00:00
|
|
|
}
|
2006-09-25 21:17:43 +00:00
|
|
|
l = tmp + block_length;
|
|
|
|
res = handle_block (demuxer, block, block_length,
|
|
|
|
block_duration, block_bref, block_fref, 1);
|
|
|
|
free (block);
|
|
|
|
mkv_d->cluster_size -= l + il;
|
|
|
|
if (res < 0)
|
|
|
|
return 0;
|
|
|
|
else if (res)
|
|
|
|
return 1;
|
|
|
|
else mkv_d->cluster_size += l + il;
|
|
|
|
break;
|
|
|
|
}
|
2004-02-09 19:21:08 +00:00
|
|
|
case EBML_ID_INVALID:
|
|
|
|
return 0;
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
default:
|
|
|
|
ebml_read_skip (s, &l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mkv_d->cluster_size -= l + il;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ebml_read_id (s, &il) != MATROSKA_ID_CLUSTER)
|
|
|
|
return 0;
|
|
|
|
add_cluster_position(mkv_d, stream_tell(s)-il);
|
|
|
|
mkv_d->cluster_size = ebml_read_length (s, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static void
|
2006-02-27 19:48:31 +00:00
|
|
|
demux_mkv_seek (demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2004-11-26 16:36:03 +00:00
|
|
|
free_cached_dps (demuxer);
|
2008-01-29 15:11:38 +00:00
|
|
|
if (!(flags & SEEK_FACTOR)) /* time in secs */
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
2007-06-20 18:19:03 +00:00
|
|
|
int64_t target_timecode = 0, diff, min_diff=0xFFFFFFFFFFFFFFFLL;
|
2004-01-19 19:16:10 +00:00
|
|
|
int i;
|
|
|
|
|
2008-01-29 15:11:38 +00:00
|
|
|
if (!(flags & SEEK_ABSOLUTE)) /* relative seek */
|
2004-01-19 19:16:10 +00:00
|
|
|
target_timecode = (int64_t) (mkv_d->last_pts * 1000.0);
|
|
|
|
target_timecode += (int64_t)(rel_seek_secs * 1000.0);
|
|
|
|
if (target_timecode < 0)
|
|
|
|
target_timecode = 0;
|
|
|
|
|
|
|
|
if (mkv_d->indexes == NULL) /* no index was found */
|
|
|
|
{
|
|
|
|
uint64_t target_filepos, cluster_pos, max_pos;
|
|
|
|
|
|
|
|
target_filepos = (uint64_t) (target_timecode * mkv_d->last_filepos
|
|
|
|
/ (mkv_d->last_pts * 1000.0));
|
|
|
|
|
|
|
|
max_pos = mkv_d->cluster_positions[mkv_d->num_cluster_pos-1];
|
|
|
|
if (target_filepos > max_pos)
|
|
|
|
{
|
|
|
|
if ((off_t) max_pos > stream_tell (s))
|
|
|
|
stream_seek (s, max_pos);
|
|
|
|
else
|
|
|
|
stream_seek (s, stream_tell (s) + mkv_d->cluster_size);
|
|
|
|
/* parse all the clusters upto target_filepos */
|
|
|
|
while (!s->eof && stream_tell(s) < (off_t) target_filepos)
|
|
|
|
{
|
|
|
|
switch (ebml_read_id (s, &i))
|
|
|
|
{
|
|
|
|
case MATROSKA_ID_CLUSTER:
|
|
|
|
add_cluster_position(mkv_d, (uint64_t) stream_tell(s)-i);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MATROSKA_ID_CUES:
|
|
|
|
demux_mkv_read_cues (demuxer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ebml_read_skip (s, NULL);
|
|
|
|
}
|
|
|
|
if (s->eof)
|
|
|
|
stream_reset(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkv_d->indexes == NULL)
|
|
|
|
{
|
|
|
|
cluster_pos = mkv_d->cluster_positions[0];
|
|
|
|
/* Let's find the nearest cluster */
|
|
|
|
for (i=0; i < mkv_d->num_cluster_pos; i++)
|
|
|
|
{
|
|
|
|
diff = mkv_d->cluster_positions[i] - target_filepos;
|
|
|
|
if (rel_seek_secs < 0 && diff < 0 && -diff < min_diff)
|
|
|
|
{
|
|
|
|
cluster_pos = mkv_d->cluster_positions[i];
|
|
|
|
min_diff = -diff;
|
|
|
|
}
|
|
|
|
else if (rel_seek_secs > 0
|
|
|
|
&& (diff < 0 ? -1 * diff : diff) < min_diff)
|
|
|
|
{
|
|
|
|
cluster_pos = mkv_d->cluster_positions[i];
|
|
|
|
min_diff = diff < 0 ? -1 * diff : diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
|
|
|
|
stream_seek (s, cluster_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mkv_index_t *index = NULL;
|
2006-11-05 20:29:29 +00:00
|
|
|
int seek_id = (demuxer->video->id < 0) ? demuxer->audio->id : demuxer->video->id;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
/* let's find the entry in the indexes with the smallest */
|
|
|
|
/* difference to the wanted timecode. */
|
|
|
|
for (i=0; i < mkv_d->num_indexes; i++)
|
2006-11-05 20:29:29 +00:00
|
|
|
if (mkv_d->indexes[i].tnum == seek_id)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2006-09-12 21:45:06 +00:00
|
|
|
diff = target_timecode + mkv_d->first_tc -
|
|
|
|
(int64_t) mkv_d->indexes[i].timecode * mkv_d->tc_scale / 1000000.0;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2008-01-29 15:11:38 +00:00
|
|
|
if ((flags & SEEK_ABSOLUTE || target_timecode <= mkv_d->last_pts*1000)) {
|
2007-03-16 14:55:41 +00:00
|
|
|
// Absolute seek or seek backward: find the last index
|
|
|
|
// position before target time
|
|
|
|
if (diff < 0 || diff >= min_diff)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Relative seek forward: find the first index position
|
|
|
|
// after target time. If no such index exists, find last
|
|
|
|
// position between current position and target time.
|
|
|
|
if (diff <= 0) {
|
|
|
|
if (min_diff <= 0 && diff <= min_diff)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (diff >= FFMIN(target_timecode - mkv_d->last_pts,
|
|
|
|
min_diff))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
min_diff = diff;
|
|
|
|
index = mkv_d->indexes + i;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index) /* We've found an entry. */
|
|
|
|
{
|
|
|
|
mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
|
|
|
|
stream_seek (s, index->filepos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (demuxer->video->id >= 0)
|
|
|
|
mkv_d->v_skip_to_keyframe = 1;
|
|
|
|
if (rel_seek_secs > 0.0)
|
|
|
|
mkv_d->skip_to_timecode = target_timecode;
|
|
|
|
mkv_d->a_skip_to_keyframe = 1;
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
demux_mkv_fill_buffer(demuxer, NULL);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2008-01-29 15:11:38 +00:00
|
|
|
else if ((demuxer->movi_end <= 0) || !(flags & SEEK_ABSOLUTE))
|
2004-01-19 19:16:10 +00:00
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
|
2004-03-26 19:45:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint64_t target_filepos;
|
|
|
|
mkv_index_t *index = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (mkv_d->indexes == NULL) /* no index was found */
|
|
|
|
{ /* I'm lazy... */
|
|
|
|
mp_msg (MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
target_filepos = (uint64_t)(demuxer->movi_end * rel_seek_secs);
|
|
|
|
for (i=0; i < mkv_d->num_indexes; i++)
|
|
|
|
if (mkv_d->indexes[i].tnum == demuxer->video->id)
|
|
|
|
if ((index == NULL) ||
|
|
|
|
((mkv_d->indexes[i].filepos >= target_filepos) &&
|
|
|
|
((index->filepos < target_filepos) ||
|
|
|
|
(mkv_d->indexes[i].filepos < index->filepos))))
|
|
|
|
index = &mkv_d->indexes[i];
|
|
|
|
|
|
|
|
if (!index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mkv_d->cluster_size = mkv_d->blockgroup_size = 0;
|
|
|
|
stream_seek (s, index->filepos);
|
|
|
|
|
|
|
|
if (demuxer->video->id >= 0)
|
|
|
|
mkv_d->v_skip_to_keyframe = 1;
|
|
|
|
mkv_d->skip_to_timecode = index->timecode;
|
|
|
|
mkv_d->a_skip_to_keyframe = 1;
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
demux_mkv_fill_buffer(demuxer, NULL);
|
2004-03-26 19:45:06 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static int
|
2004-01-19 19:16:10 +00:00
|
|
|
demux_mkv_control (demuxer_t *demuxer, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2008-02-03 12:14:29 +00:00
|
|
|
case DEMUXER_CTRL_CORRECT_PTS:
|
|
|
|
return DEMUXER_CTRL_OK;
|
2004-01-19 19:16:10 +00:00
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH:
|
|
|
|
if (mkv_d->duration == 0)
|
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
|
2005-09-02 08:32:32 +00:00
|
|
|
*((double *)arg) = (double)mkv_d->duration;
|
2004-01-19 19:16:10 +00:00
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
|
|
|
case DEMUXER_CTRL_GET_PERCENT_POS:
|
|
|
|
if (mkv_d->duration == 0)
|
|
|
|
{
|
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
*((int *) arg) = (int) (100 * mkv_d->last_pts / mkv_d->duration);
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
2005-04-13 22:33:19 +00:00
|
|
|
case DEMUXER_CTRL_SWITCH_AUDIO:
|
|
|
|
if (demuxer->audio && demuxer->audio->sh) {
|
2006-09-03 10:10:57 +00:00
|
|
|
sh_audio_t *sh = demuxer->a_streams[demuxer->audio->id];
|
|
|
|
int aid = *(int*)arg;
|
|
|
|
if (aid < 0)
|
|
|
|
aid = (sh->aid + 1) % mkv_d->last_aid;
|
|
|
|
if (aid != sh->aid) {
|
|
|
|
mkv_track_t *track = demux_mkv_find_track_by_num (mkv_d, aid, MATROSKA_TRACK_AUDIO);
|
|
|
|
if (track) {
|
|
|
|
demuxer->audio->id = track->tnum;
|
|
|
|
sh = demuxer->a_streams[demuxer->audio->id];
|
|
|
|
ds_free_packs(demuxer->audio);
|
|
|
|
}
|
2005-04-13 22:33:19 +00:00
|
|
|
}
|
2006-09-03 10:10:57 +00:00
|
|
|
*(int*)arg = sh->aid;
|
|
|
|
} else
|
|
|
|
*(int*)arg = -2;
|
2005-04-13 22:33:19 +00:00
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
default:
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_matroska = {
|
2005-08-05 19:57:47 +00:00
|
|
|
"Matroska demuxer",
|
|
|
|
"mkv",
|
|
|
|
"Matroska",
|
|
|
|
"Aurelien Jacobs",
|
2007-01-25 17:48:38 +00:00
|
|
|
"",
|
2005-08-05 19:57:47 +00:00
|
|
|
DEMUXER_TYPE_MATROSKA,
|
|
|
|
1, // safe autodetect
|
|
|
|
demux_mkv_open,
|
|
|
|
demux_mkv_fill_buffer,
|
|
|
|
NULL,
|
|
|
|
demux_close_mkv,
|
|
|
|
demux_mkv_seek,
|
|
|
|
demux_mkv_control
|
|
|
|
};
|