2004-01-19 19:16:10 +00:00
|
|
|
/*
|
2009-05-08 21:51:13 +00:00
|
|
|
* Matroska demuxer
|
|
|
|
* Copyright (C) 2004 Aurelien Jacobs <aurel@gnuage.org>
|
2004-01-19 19:16:10 +00:00
|
|
|
* Based on the one written by Ronald Bultje for gstreamer
|
2009-05-08 21:51:13 +00:00
|
|
|
* and on demux_mkv.cpp from Moritz Bunkus.
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-01-19 19:16:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2006-06-04 22:41:27 +00:00
|
|
|
#include <inttypes.h>
|
2009-03-16 21:30:48 +00:00
|
|
|
#include <stdbool.h>
|
demux_mkv: improve robustness against broken files
Fixes test7.mkv from the Matroska test file collection, as well as some
real broken files I've found in the wild. (Unfortunately, true recovery
requires resetting the decoders and playback state with a manual seek,
but it's still better than just exiting.)
If there are broken EBML elements, try harder to skip them correctly.
Do this by searching for the next cluster element. The cluster element
intentionally has a long ID, so it's a suitable element for
resynchronizing (mkvmerge does something similar).
We know that data is corrupt if the ID or length fields of an element
are malformed. Additionally, if skipping an unknown element goes past
the end of the file, we assume it's corrupt and undo the seek. Do this
because it often happens that corrupt data is interpreted as correct
EBML elements. Since these elements will have a ridiculous values in
their length fields due to the large value range that is possible
(0-2^56-2), they will go past the end of the file. So instead of
skipping them (which would result in playback termination), try to
find the next cluster instead. (We still skip unknown elements that
are within the file, as this is needed for correct operation. Also, we
first execute the seek, because we don't really know where the file
ends. Doing it this way is better for unseekable streams too, because
it will still work in the non-error case.)
This is done as special case in the packet reading function only. On
the other hand, that's the only part of the file that's read after
initialization is done.
2013-03-27 23:01:17 +00:00
|
|
|
#include <assert.h>
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2012-02-01 18:01:16 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
#include <libavutil/lzo.h>
|
|
|
|
#include <libavutil/intreadwrite.h>
|
|
|
|
#include <libavutil/avstring.h>
|
|
|
|
|
2013-12-27 19:00:24 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
2013-05-30 17:55:15 +00:00
|
|
|
#include <libavcodec/version.h>
|
|
|
|
|
2012-02-01 18:01:16 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_ZLIB
|
2012-02-01 18:01:16 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-16 21:30:48 +00:00
|
|
|
#include "talloc.h"
|
2013-12-27 19:00:24 +00:00
|
|
|
#include "common/av_common.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2014-08-29 10:09:04 +00:00
|
|
|
#include "misc/bstr.h"
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2014-08-30 21:24:46 +00:00
|
|
|
#include "video/csputils.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "demux.h"
|
2004-01-19 19:16:10 +00:00
|
|
|
#include "stheader.h"
|
|
|
|
#include "ebml.h"
|
|
|
|
#include "matroska.h"
|
2013-07-11 17:35:09 +00:00
|
|
|
#include "codec_tags.h"
|
2014-09-25 00:22:44 +00:00
|
|
|
#include "video/img_fourcc.h"
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2006-11-05 13:53:27 +00:00
|
|
|
|
2009-12-29 19:06:21 +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},
|
2009-12-29 19:06:21 +00:00
|
|
|
{77,80}
|
|
|
|
};
|
2006-04-03 19:41:17 +00:00
|
|
|
|
|
|
|
// Map flavour to bytes per second
|
|
|
|
#define SIPR_FLAVORS 4
|
|
|
|
#define ATRC_FLAVORS 8
|
|
|
|
#define COOK_FLAVORS 34
|
2009-12-29 19:06:21 +00:00
|
|
|
static const int sipr_fl2bps[SIPR_FLAVORS] = { 813, 1062, 625, 2000 };
|
2010-05-29 14:15:55 +00:00
|
|
|
static const int atrc_fl2bps[ATRC_FLAVORS] = {
|
|
|
|
8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100 };
|
2008-04-12 15:51:11 +00:00
|
|
|
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,
|
2009-12-29 19:06:21 +00:00
|
|
|
12016, 16408, 22911, 33506
|
|
|
|
};
|
2006-04-03 19:41:17 +00:00
|
|
|
|
2013-04-12 14:07:45 +00:00
|
|
|
enum {
|
|
|
|
MAX_NUM_LACES = 256,
|
|
|
|
};
|
|
|
|
|
2010-01-26 14:05:49 +00:00
|
|
|
typedef struct mkv_content_encoding {
|
|
|
|
uint64_t order, type, scope;
|
|
|
|
uint64_t comp_algo;
|
2009-12-29 19:06:21 +00:00
|
|
|
uint8_t *comp_settings;
|
|
|
|
int comp_settings_len;
|
2004-01-19 19:16:10 +00:00
|
|
|
} mkv_content_encoding_t;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
typedef struct mkv_track {
|
|
|
|
int tnum;
|
|
|
|
char *name;
|
2013-04-14 17:23:18 +00:00
|
|
|
struct sh_stream *stream;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
char *codec_id;
|
|
|
|
int ms_compat;
|
|
|
|
char *language;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
int type;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
uint32_t v_width, v_height, v_dwidth, v_dheight;
|
2013-04-03 23:20:06 +00:00
|
|
|
bool v_dwidth_set, v_dheight_set;
|
2009-12-29 19:06:21 +00:00
|
|
|
double v_frate;
|
2013-01-24 15:57:21 +00:00
|
|
|
uint32_t colorspace;
|
2014-08-30 21:24:46 +00:00
|
|
|
int stereo_mode;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
uint32_t a_formattag;
|
|
|
|
uint32_t a_channels, a_bps;
|
|
|
|
float a_sfreq;
|
2010-11-21 12:52:08 +00:00
|
|
|
float a_osfreq;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
double default_duration;
|
2014-11-03 19:00:34 +00:00
|
|
|
double codec_delay;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
int default_track;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
unsigned char *private_data;
|
|
|
|
unsigned int private_size;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-12-27 19:00:24 +00:00
|
|
|
bool parse;
|
|
|
|
void *parser_tmp;
|
|
|
|
AVCodecParserContext *av_parser;
|
|
|
|
AVCodecContext *av_parser_codec;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
/* stuff for realmedia */
|
|
|
|
int realmedia;
|
|
|
|
int64_t rv_kf_base;
|
|
|
|
int rv_kf_pts;
|
|
|
|
double ra_pts; /* previous audio timestamp */
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2006-04-03 19:41:17 +00:00
|
|
|
/** realaudio descrambling */
|
2014-11-21 04:10:28 +00:00
|
|
|
uint32_t sub_packet_size; ///< sub packet size, per stream
|
|
|
|
uint32_t sub_packet_h; ///< number of coded frames per block
|
2014-06-29 21:22:07 +00:00
|
|
|
uint32_t coded_framesize; ///< coded frame size, per stream
|
2014-11-21 04:10:28 +00:00
|
|
|
uint32_t audiopk_size; ///< audio packet size
|
2009-12-29 19:06:21 +00:00
|
|
|
unsigned char *audio_buf; ///< place to store reordered audio data
|
|
|
|
double *audio_timestamp; ///< timestamp for each audio packet
|
2014-11-21 04:10:28 +00:00
|
|
|
uint32_t sub_packet_cnt; ///< number of subpacket already received
|
2009-12-29 19:06:21 +00:00
|
|
|
int audio_filepos; ///< file position of first audio packet in block
|
|
|
|
|
|
|
|
/* generic content encoding support */
|
|
|
|
mkv_content_encoding_t *encodings;
|
|
|
|
int num_encodings;
|
|
|
|
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
/* latest added index entry for this track */
|
2014-06-29 21:22:07 +00:00
|
|
|
size_t last_index_entry;
|
2004-01-19 19:16:10 +00:00
|
|
|
} mkv_track_t;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
typedef struct mkv_index {
|
|
|
|
int tnum;
|
2014-11-05 19:44:21 +00:00
|
|
|
uint64_t timecode, duration;
|
|
|
|
uint64_t filepos; // position of the cluster which contains the packet
|
2004-01-19 19:16:10 +00:00
|
|
|
} mkv_index_t;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
typedef struct mkv_demuxer {
|
2014-01-22 22:37:03 +00:00
|
|
|
int64_t segment_start, segment_end;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
double duration, last_pts;
|
|
|
|
uint64_t last_filepos;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_track_t **tracks;
|
|
|
|
int num_tracks;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-01-22 23:54:08 +00:00
|
|
|
struct ebml_tags *tags;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
uint64_t tc_scale, cluster_tc;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-01 16:42:52 +00:00
|
|
|
uint64_t cluster_start;
|
2013-04-11 18:31:58 +00:00
|
|
|
uint64_t cluster_end;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_index_t *indexes;
|
2014-06-29 21:22:07 +00:00
|
|
|
size_t num_indexes;
|
2013-04-11 15:40:23 +00:00
|
|
|
bool index_complete;
|
2013-05-22 22:26:42 +00:00
|
|
|
uint64_t deferred_cues;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-01-22 21:12:14 +00:00
|
|
|
struct header_elem {
|
|
|
|
int32_t id;
|
|
|
|
int64_t pos;
|
|
|
|
bool parsed;
|
|
|
|
} *headers;
|
|
|
|
int num_headers;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2011-01-23 22:29:01 +00:00
|
|
|
uint64_t skip_to_timecode;
|
2014-02-09 20:13:03 +00:00
|
|
|
int v_skip_to_keyframe, a_skip_to_keyframe;
|
2014-11-03 19:00:34 +00:00
|
|
|
int a_skip_preroll;
|
2013-09-08 03:09:16 +00:00
|
|
|
int subtitle_preroll;
|
2014-11-05 19:44:21 +00:00
|
|
|
|
|
|
|
bool index_has_durations;
|
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
|
|
|
|
2013-09-08 03:09:16 +00:00
|
|
|
// Maximum number of subtitle packets that are accepted for pre-roll.
|
2013-09-15 22:20:19 +00:00
|
|
|
// (Subtitle packets added before first A/V keyframe packet is found with seek.)
|
|
|
|
#define NUM_SUB_PREROLL_PACKETS 500
|
2013-09-08 03:09:16 +00:00
|
|
|
|
2014-11-18 22:07:20 +00:00
|
|
|
static void probe_last_timestamp(struct demuxer *demuxer);
|
|
|
|
|
2004-01-19 19:16:10 +00:00
|
|
|
#define AAC_SYNC_EXTENSION_TYPE 0x02b7
|
2009-12-29 19:06:21 +00:00
|
|
|
static int aac_get_sample_rate_index(uint32_t sample_rate)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
static const int srates[] = {
|
|
|
|
92017, 75132, 55426, 46009, 37566, 27713,
|
|
|
|
23004, 18783, 13856, 11502, 9391, 0
|
|
|
|
};
|
|
|
|
int i = 0;
|
|
|
|
while (sample_rate < srates[i])
|
|
|
|
i++;
|
|
|
|
return i;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
static bstr demux_mkv_decode(struct mp_log *log, mkv_track_t *track,
|
|
|
|
bstr data, uint32_t type)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2013-04-12 15:25:51 +00:00
|
|
|
uint8_t *src = data.start;
|
2010-01-26 17:40:07 +00:00
|
|
|
uint8_t *orig_src = src;
|
2013-04-12 15:25:51 +00:00
|
|
|
uint8_t *dest = src;
|
|
|
|
uint32_t size = data.len;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-26 17:40:07 +00:00
|
|
|
for (int i = 0; i < track->num_encodings; i++) {
|
|
|
|
struct mkv_content_encoding *enc = track->encodings + i;
|
|
|
|
if (!(enc->scope & type))
|
2009-12-29 19:06:21 +00:00
|
|
|
continue;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-12 15:25:51 +00:00
|
|
|
if (src != dest && src != orig_src)
|
2010-03-21 16:36:47 +00:00
|
|
|
talloc_free(src);
|
2013-04-12 15:25:51 +00:00
|
|
|
src = dest; // output from last iteration is new source
|
2010-01-26 17:40:07 +00:00
|
|
|
|
|
|
|
if (enc->comp_algo == 0) {
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_ZLIB
|
2009-12-29 19:06:21 +00:00
|
|
|
/* zlib encoded track */
|
2010-01-26 14:38:39 +00:00
|
|
|
|
2013-04-12 15:25:51 +00:00
|
|
|
if (size == 0)
|
2010-01-26 14:38:39 +00:00
|
|
|
continue;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
z_stream zstream;
|
|
|
|
|
|
|
|
zstream.zalloc = (alloc_func) 0;
|
|
|
|
zstream.zfree = (free_func) 0;
|
|
|
|
zstream.opaque = (voidpf) 0;
|
|
|
|
if (inflateInit(&zstream) != Z_OK) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_warn(log, "zlib initialization failed.\n");
|
2010-01-26 17:40:07 +00:00
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
|
|
|
zstream.next_in = (Bytef *) src;
|
2013-04-12 15:25:51 +00:00
|
|
|
zstream.avail_in = size;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-12 15:25:51 +00:00
|
|
|
dest = NULL;
|
|
|
|
zstream.avail_out = size;
|
2010-01-26 17:40:07 +00:00
|
|
|
int result;
|
2009-12-29 19:06:21 +00:00
|
|
|
do {
|
2014-06-29 21:22:07 +00:00
|
|
|
if (size >= INT_MAX - 4000) {
|
|
|
|
talloc_free(dest);
|
|
|
|
dest = NULL;
|
|
|
|
inflateEnd(&zstream);
|
|
|
|
goto error;
|
|
|
|
}
|
2013-04-12 15:25:51 +00:00
|
|
|
size += 4000;
|
2013-12-27 19:00:24 +00:00
|
|
|
dest = talloc_realloc_size(track->parser_tmp, dest, size);
|
2013-04-12 15:25:51 +00:00
|
|
|
zstream.next_out = (Bytef *) (dest + zstream.total_out);
|
2009-12-29 19:06:21 +00:00
|
|
|
result = inflate(&zstream, Z_NO_FLUSH);
|
|
|
|
if (result != Z_OK && result != Z_STREAM_END) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_warn(log, "zlib decompression failed.\n");
|
2013-04-12 15:25:51 +00:00
|
|
|
talloc_free(dest);
|
|
|
|
dest = NULL;
|
2009-12-29 19:06:21 +00:00
|
|
|
inflateEnd(&zstream);
|
2010-01-26 17:40:07 +00:00
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
|
|
|
zstream.avail_out += 4000;
|
|
|
|
} while (zstream.avail_out == 4000 && zstream.avail_in != 0
|
|
|
|
&& result != Z_STREAM_END);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-12 15:25:51 +00:00
|
|
|
size = zstream.total_out;
|
2009-12-29 19:06:21 +00:00
|
|
|
inflateEnd(&zstream);
|
2004-01-19 19:16:10 +00:00
|
|
|
#endif
|
2010-01-26 17:40:07 +00:00
|
|
|
} else if (enc->comp_algo == 2) {
|
2009-12-29 19:06:21 +00:00
|
|
|
/* lzo encoded track */
|
2010-09-18 17:24:39 +00:00
|
|
|
int out_avail;
|
2014-07-01 22:14:18 +00:00
|
|
|
int maxlen = INT_MAX - AV_LZO_OUTPUT_PADDING;
|
|
|
|
if (size >= maxlen / 3)
|
2014-06-29 21:22:07 +00:00
|
|
|
goto error;
|
2013-04-12 15:25:51 +00:00
|
|
|
int dstlen = size * 3;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-12 15:25:51 +00:00
|
|
|
dest = NULL;
|
2009-12-29 19:06:21 +00:00
|
|
|
while (1) {
|
2013-04-12 15:25:51 +00:00
|
|
|
int srclen = size;
|
2013-12-27 19:00:24 +00:00
|
|
|
dest = talloc_realloc_size(track->parser_tmp, dest,
|
2013-04-12 15:25:51 +00:00
|
|
|
dstlen + AV_LZO_OUTPUT_PADDING);
|
2010-09-18 17:24:39 +00:00
|
|
|
out_avail = dstlen;
|
2013-04-12 15:25:51 +00:00
|
|
|
int result = av_lzo1x_decode(dest, &out_avail, src, &srclen);
|
2009-12-29 19:06:21 +00:00
|
|
|
if (result == 0)
|
|
|
|
break;
|
|
|
|
if (!(result & AV_LZO_OUTPUT_FULL)) {
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_warn(log, "lzo decompression failed.\n");
|
2013-04-12 15:25:51 +00:00
|
|
|
talloc_free(dest);
|
|
|
|
dest = NULL;
|
2010-01-26 17:40:07 +00:00
|
|
|
goto error;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-12-21 19:24:20 +00:00
|
|
|
mp_dbg(log, "lzo decompression buffer too small.\n");
|
2014-07-01 22:14:18 +00:00
|
|
|
if (dstlen >= maxlen / 2) {
|
2014-06-29 21:22:07 +00:00
|
|
|
talloc_free(dest);
|
|
|
|
dest = NULL;
|
|
|
|
goto error;
|
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
dstlen *= 2;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-04-12 15:25:51 +00:00
|
|
|
size = dstlen - out_avail;
|
2010-01-26 17:40:07 +00:00
|
|
|
} else if (enc->comp_algo == 3) {
|
2013-12-27 19:00:24 +00:00
|
|
|
dest = talloc_size(track->parser_tmp, size + enc->comp_settings_len);
|
2013-04-12 15:25:51 +00:00
|
|
|
memcpy(dest, enc->comp_settings, enc->comp_settings_len);
|
|
|
|
memcpy(dest + enc->comp_settings_len, src, size);
|
|
|
|
size += enc->comp_settings_len;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-26 17:40:07 +00:00
|
|
|
error:
|
2013-04-12 15:25:51 +00:00
|
|
|
if (src != dest && src != orig_src)
|
2010-03-21 16:36:47 +00:00
|
|
|
talloc_free(src);
|
2014-06-29 21:22:07 +00:00
|
|
|
if (!size)
|
|
|
|
dest = NULL;
|
2013-04-12 15:25:51 +00:00
|
|
|
return (bstr){dest, size};
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2011-08-04 05:38:39 +00:00
|
|
|
int res = 0;
|
2009-03-16 21:30:48 +00:00
|
|
|
|
2014-01-22 21:12:14 +00:00
|
|
|
MP_VERBOSE(demuxer, "|+ segment information...\n");
|
|
|
|
|
2010-01-26 12:06:49 +00:00
|
|
|
mkv_d->tc_scale = 1000000;
|
|
|
|
mkv_d->duration = 0;
|
|
|
|
|
2013-11-04 22:49:22 +00:00
|
|
|
struct ebml_info info = {0};
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = {demuxer->log};
|
2010-01-26 12:06:49 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &info, &ebml_info_desc) < 0)
|
2011-08-04 05:38:39 +00:00
|
|
|
return -1;
|
2010-01-26 12:06:49 +00:00
|
|
|
if (info.n_timecode_scale) {
|
|
|
|
mkv_d->tc_scale = info.timecode_scale;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + timecode scale: %" PRIu64 "\n", mkv_d->tc_scale);
|
2014-12-06 12:47:03 +00:00
|
|
|
if (mkv_d->tc_scale < 1 || mkv_d->tc_scale > INT_MAX) {
|
|
|
|
res = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2010-01-26 12:06:49 +00:00
|
|
|
if (info.n_duration) {
|
|
|
|
mkv_d->duration = info.duration * mkv_d->tc_scale / 1e9;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + duration: %.3fs\n",
|
2009-03-13 01:26:32 +00:00
|
|
|
mkv_d->duration);
|
2010-01-26 12:06:49 +00:00
|
|
|
}
|
2013-05-15 13:00:52 +00:00
|
|
|
if (info.n_title) {
|
2014-02-06 12:43:01 +00:00
|
|
|
mp_tags_set_bstr(demuxer->metadata, bstr0("TITLE"), info.title);
|
2013-05-15 13:00:52 +00:00
|
|
|
}
|
2010-01-26 12:06:49 +00:00
|
|
|
if (info.n_segment_uid) {
|
|
|
|
int len = info.segment_uid.len;
|
2013-10-07 00:49:12 +00:00
|
|
|
if (len != sizeof(demuxer->matroska_data.uid.segment)) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_INFO(demuxer, "segment uid invalid length %d\n", len);
|
2010-01-26 12:06:49 +00:00
|
|
|
} else {
|
2013-10-07 00:49:12 +00:00
|
|
|
memcpy(demuxer->matroska_data.uid.segment, info.segment_uid.start,
|
2010-01-26 12:06:49 +00:00
|
|
|
len);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + segment uid");
|
2010-01-26 12:06:49 +00:00
|
|
|
for (int i = 0; i < len; i++)
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, " %02x",
|
2013-10-07 00:49:12 +00:00
|
|
|
demuxer->matroska_data.uid.segment[i]);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "\n");
|
2010-01-26 12:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-04 05:38:39 +00:00
|
|
|
if (demuxer->params && demuxer->params->matroska_wanted_uids) {
|
2013-09-27 00:42:09 +00:00
|
|
|
if (info.n_segment_uid) {
|
|
|
|
for (int i = 0; i < demuxer->params->matroska_num_wanted_uids; i++) {
|
2013-10-07 00:49:12 +00:00
|
|
|
struct matroska_segment_uid *uid = demuxer->params->matroska_wanted_uids + i;
|
2013-09-26 06:54:25 +00:00
|
|
|
if (!memcmp(info.segment_uid.start, uid->segment, 16)) {
|
|
|
|
demuxer->matroska_data.uid.edition = uid->edition;
|
2013-09-27 00:42:09 +00:00
|
|
|
goto out;
|
2013-09-26 06:54:25 +00:00
|
|
|
}
|
2013-09-27 00:42:09 +00:00
|
|
|
}
|
2011-08-04 05:38:39 +00:00
|
|
|
}
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_INFO(demuxer, "This is not one of the wanted files. "
|
2011-08-04 05:38:39 +00:00
|
|
|
"Stopping attempt to open.\n");
|
|
|
|
res = -2;
|
|
|
|
}
|
|
|
|
out:
|
2010-01-26 12:06:49 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
2011-08-04 05:38:39 +00:00
|
|
|
return res;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
static void parse_trackencodings(struct demuxer *demuxer,
|
|
|
|
struct mkv_track *track,
|
|
|
|
struct ebml_content_encodings *encodings)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2010-01-26 14:05:49 +00:00
|
|
|
// initial allocation to be a non-NULL context before realloc
|
|
|
|
mkv_content_encoding_t *ce = talloc_size(track, 1);
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
for (int n_enc = 0; n_enc < encodings->n_content_encoding; n_enc++) {
|
|
|
|
struct ebml_content_encoding *enc = encodings->content_encoding + n_enc;
|
2013-10-19 21:15:06 +00:00
|
|
|
struct mkv_content_encoding e = {0};
|
2010-01-26 14:05:49 +00:00
|
|
|
e.order = enc->content_encoding_order;
|
|
|
|
if (enc->n_content_encoding_scope)
|
|
|
|
e.scope = enc->content_encoding_scope;
|
|
|
|
else
|
2004-01-19 19:16:10 +00:00
|
|
|
e.scope = 1;
|
2010-01-26 14:05:49 +00:00
|
|
|
e.type = enc->content_encoding_type;
|
|
|
|
|
|
|
|
if (enc->n_content_compression) {
|
|
|
|
struct ebml_content_compression *z = &enc->content_compression;
|
|
|
|
e.comp_algo = z->content_comp_algo;
|
|
|
|
if (z->n_content_comp_settings) {
|
|
|
|
int sz = z->content_comp_settings.len;
|
|
|
|
e.comp_settings = talloc_size(ce, sz);
|
|
|
|
memcpy(e.comp_settings, z->content_comp_settings.start, sz);
|
|
|
|
e.comp_settings_len = sz;
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 14:05:49 +00:00
|
|
|
if (e.type == 1) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Track "
|
2010-01-26 14:05:49 +00:00
|
|
|
"number %u has been encrypted and "
|
|
|
|
"decryption has not yet been\n"
|
2013-12-21 19:24:20 +00:00
|
|
|
"implemented. Skipping track.\n",
|
2010-01-26 14:05:49 +00:00
|
|
|
track->tnum);
|
|
|
|
} else if (e.type != 0) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Unknown content encoding type for "
|
2010-01-26 14:05:49 +00:00
|
|
|
"track %u. Skipping track.\n",
|
|
|
|
track->tnum);
|
2010-01-28 15:17:54 +00:00
|
|
|
} else if (e.comp_algo != 0 && e.comp_algo != 2 && e.comp_algo != 3) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Track %u has been compressed with "
|
2010-01-26 14:05:49 +00:00
|
|
|
"an unknown/unsupported compression\n"
|
2013-12-21 19:24:20 +00:00
|
|
|
"algorithm (%" PRIu64 "). Skipping track.\n",
|
2010-01-26 14:05:49 +00:00
|
|
|
track->tnum, e.comp_algo);
|
|
|
|
}
|
2013-07-16 11:28:28 +00:00
|
|
|
#if !HAVE_ZLIB
|
2010-01-26 14:05:49 +00:00
|
|
|
else if (e.comp_algo == 0) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Track %u was compressed with zlib "
|
2012-10-11 00:04:08 +00:00
|
|
|
"but mpv has not been compiled\n"
|
2013-12-21 19:24:20 +00:00
|
|
|
"with support for zlib compression. "
|
2010-01-26 14:05:49 +00:00
|
|
|
"Skipping track.\n",
|
|
|
|
track->tnum);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
int i;
|
2013-10-19 21:10:03 +00:00
|
|
|
for (i = 0; i < n_enc; i++) {
|
2010-01-26 17:40:07 +00:00
|
|
|
if (e.order >= ce[i].order)
|
2010-01-26 14:05:49 +00:00
|
|
|
break;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2014-06-29 21:22:07 +00:00
|
|
|
ce = talloc_realloc(track, ce, mkv_content_encoding_t, n_enc + 1);
|
2010-01-26 14:05:49 +00:00
|
|
|
memmove(ce + i + 1, ce + i, (n_enc - i) * sizeof(*ce));
|
|
|
|
memcpy(ce + i, &e, sizeof(e));
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
track->encodings = ce;
|
2010-01-26 16:15:02 +00:00
|
|
|
track->num_encodings = encodings->n_content_encoding;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
static void parse_trackaudio(struct demuxer *demuxer, struct mkv_track *track,
|
|
|
|
struct ebml_audio *audio)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2010-01-26 16:15:02 +00:00
|
|
|
if (audio->n_sampling_frequency) {
|
|
|
|
track->a_sfreq = audio->sampling_frequency;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Sampling frequency: %f\n", track->a_sfreq);
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2010-01-26 12:41:07 +00:00
|
|
|
track->a_sfreq = 8000;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2010-11-21 12:52:08 +00:00
|
|
|
if (audio->n_output_sampling_frequency) {
|
|
|
|
track->a_osfreq = audio->output_sampling_frequency;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Output sampling frequency: %f\n", track->a_osfreq);
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2010-11-21 12:52:08 +00:00
|
|
|
track->a_osfreq = track->a_sfreq;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
if (audio->n_bit_depth) {
|
|
|
|
track->a_bps = audio->bit_depth;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Bit depth: %u\n", track->a_bps);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
if (audio->n_channels) {
|
|
|
|
track->a_channels = audio->channels;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Channels: %u\n", track->a_channels);
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2010-01-26 12:41:07 +00:00
|
|
|
track->a_channels = 1;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
static void parse_trackvideo(struct demuxer *demuxer, struct mkv_track *track,
|
|
|
|
struct ebml_video *video)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2010-01-26 16:15:02 +00:00
|
|
|
if (video->n_frame_rate) {
|
|
|
|
track->v_frate = video->frame_rate;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Frame rate: %f\n", track->v_frate);
|
2010-01-26 12:55:06 +00:00
|
|
|
if (track->v_frate > 0)
|
|
|
|
track->default_duration = 1 / track->v_frate;
|
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
if (video->n_display_width) {
|
|
|
|
track->v_dwidth = video->display_width;
|
2013-04-03 23:20:06 +00:00
|
|
|
track->v_dwidth_set = true;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Display width: %u\n", track->v_dwidth);
|
2010-01-26 12:55:06 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
if (video->n_display_height) {
|
|
|
|
track->v_dheight = video->display_height;
|
2013-04-03 23:20:06 +00:00
|
|
|
track->v_dheight_set = true;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Display height: %u\n", track->v_dheight);
|
2010-01-26 12:55:06 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
if (video->n_pixel_width) {
|
|
|
|
track->v_width = video->pixel_width;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Pixel width: %u\n", track->v_width);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
if (video->n_pixel_height) {
|
|
|
|
track->v_height = video->pixel_height;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Pixel height: %u\n", track->v_height);
|
2010-01-26 12:55:06 +00:00
|
|
|
}
|
2013-01-24 15:57:21 +00:00
|
|
|
if (video->n_colour_space && video->colour_space.len == 4) {
|
|
|
|
uint8_t *d = (uint8_t *)&video->colour_space.start[0];
|
|
|
|
track->colorspace = d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Colorspace: %#x\n",
|
2014-07-05 14:42:03 +00:00
|
|
|
(unsigned int)track->colorspace);
|
2013-01-24 15:57:21 +00:00
|
|
|
}
|
2014-08-30 21:24:46 +00:00
|
|
|
if (video->n_stereo_mode) {
|
|
|
|
const char *name = MP_STEREO3D_NAME(video->stereo_mode);
|
|
|
|
if (name) {
|
|
|
|
track->stereo_mode = video->stereo_mode;
|
|
|
|
MP_VERBOSE(demuxer, "| + StereoMode: %s\n", name);
|
|
|
|
} else {
|
|
|
|
MP_WARN(demuxer, "Unknown StereoMode: %d\n", (int)video->stereo_mode);
|
|
|
|
}
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2006-10-09 18:48:55 +00:00
|
|
|
/**
|
|
|
|
* \brief free any data associated with given track
|
|
|
|
* \param track track of which to free data
|
|
|
|
*/
|
2009-12-29 19:06:21 +00:00
|
|
|
static void demux_mkv_free_trackentry(mkv_track_t *track)
|
|
|
|
{
|
2013-12-27 19:00:24 +00:00
|
|
|
talloc_free(track->parser_tmp);
|
2010-01-26 14:05:49 +00:00
|
|
|
talloc_free(track);
|
2006-10-09 18:48:55 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
static void parse_trackentry(struct demuxer *demuxer,
|
|
|
|
struct ebml_track_entry *entry)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
2014-06-29 21:22:07 +00:00
|
|
|
struct mkv_track *track = talloc_zero(NULL, struct mkv_track);
|
|
|
|
track->last_index_entry = (size_t)-1;
|
2013-12-27 19:00:24 +00:00
|
|
|
track->parser_tmp = talloc_new(track);
|
2010-01-26 16:15:02 +00:00
|
|
|
|
|
|
|
track->tnum = entry->track_number;
|
2013-10-19 21:10:03 +00:00
|
|
|
if (track->tnum) {
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Track number: %u\n", track->tnum);
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_ERR(demuxer, "Missing track number!\n");
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
|
|
|
|
if (entry->n_name) {
|
|
|
|
track->name = talloc_strndup(track, entry->name.start,
|
|
|
|
entry->name.len);
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Name: %s\n", track->name);
|
2010-01-26 16:15:02 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
track->type = entry->track_type;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Track type: ");
|
2010-01-26 16:15:02 +00:00
|
|
|
switch (track->type) {
|
|
|
|
case MATROSKA_TRACK_AUDIO:
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Audio\n");
|
2010-01-26 16:15:02 +00:00
|
|
|
break;
|
|
|
|
case MATROSKA_TRACK_VIDEO:
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Video\n");
|
2010-01-26 16:15:02 +00:00
|
|
|
break;
|
|
|
|
case MATROSKA_TRACK_SUBTITLE:
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Subtitle\n");
|
2010-01-26 16:15:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "unknown\n");
|
2010-01-26 16:15:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
if (entry->n_audio) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Audio track\n");
|
2010-01-26 16:15:02 +00:00
|
|
|
parse_trackaudio(demuxer, track, &entry->audio);
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
if (entry->n_video) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Video track\n");
|
2010-01-26 16:15:02 +00:00
|
|
|
parse_trackvideo(demuxer, track, &entry->video);
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
if (entry->n_codec_id) {
|
|
|
|
track->codec_id = talloc_strndup(track, entry->codec_id.start,
|
|
|
|
entry->codec_id.len);
|
|
|
|
if (!strcmp(track->codec_id, MKV_V_MSCOMP)
|
|
|
|
|| !strcmp(track->codec_id, MKV_A_ACM))
|
|
|
|
track->ms_compat = 1;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Codec ID: %s\n", track->codec_id);
|
2013-04-20 21:40:41 +00:00
|
|
|
} else {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_ERR(demuxer, "Missing codec ID!\n");
|
2013-04-20 21:40:41 +00:00
|
|
|
track->codec_id = "";
|
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
|
2014-09-04 17:19:28 +00:00
|
|
|
if (entry->n_codec_private && entry->codec_private.len <= 0x10000000) {
|
2010-01-26 16:15:02 +00:00
|
|
|
int len = entry->codec_private.len;
|
|
|
|
track->private_data = talloc_size(track, len + AV_LZO_INPUT_PADDING);
|
|
|
|
memcpy(track->private_data, entry->codec_private.start, len);
|
|
|
|
track->private_size = len;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + CodecPrivate, length %u\n", track->private_size);
|
2010-01-26 16:15:02 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
if (entry->n_language) {
|
|
|
|
track->language = talloc_strndup(track, entry->language.start,
|
|
|
|
entry->language.len);
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Language: %s\n", track->language);
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2010-01-26 16:15:02 +00:00
|
|
|
track->language = talloc_strdup(track, "eng");
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
if (entry->n_flag_default) {
|
|
|
|
track->default_track = entry->flag_default;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Default flag: %u\n", track->default_track);
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2010-01-26 16:15:02 +00:00
|
|
|
track->default_track = 1;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
|
|
|
|
if (entry->n_default_duration) {
|
|
|
|
track->default_duration = entry->default_duration / 1e9;
|
2013-10-19 21:10:03 +00:00
|
|
|
if (entry->default_duration == 0) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Default duration: 0");
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2010-01-26 16:15:02 +00:00
|
|
|
if (!track->v_frate)
|
|
|
|
track->v_frate = 1e9 / entry->default_duration;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + Default duration: %.3fms ( = %.3f fps)\n",
|
2014-07-05 14:42:03 +00:00
|
|
|
entry->default_duration / 1000000.0, track->v_frate);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
if (entry->n_content_encodings)
|
|
|
|
parse_trackencodings(demuxer, track, &entry->content_encodings);
|
2006-10-09 18:48:55 +00:00
|
|
|
|
2014-11-03 19:00:34 +00:00
|
|
|
if (entry->n_codec_delay)
|
|
|
|
track->codec_delay = entry->codec_delay / 1e9;
|
|
|
|
|
2010-01-26 16:15:02 +00:00
|
|
|
mkv_d->tracks[mkv_d->num_tracks++] = track;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static int demux_mkv_read_tracks(demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-01-22 21:12:14 +00:00
|
|
|
MP_VERBOSE(demuxer, "|+ segment tracks...\n");
|
|
|
|
|
2013-10-19 21:15:06 +00:00
|
|
|
struct ebml_tracks tracks = {0};
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = {demuxer->log};
|
2010-01-26 16:15:02 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &tracks, &ebml_tracks_desc) < 0)
|
2011-08-20 18:45:42 +00:00
|
|
|
return -1;
|
2010-01-26 16:15:02 +00:00
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
mkv_d->tracks = talloc_zero_array(mkv_d, struct mkv_track*,
|
|
|
|
tracks.n_track_entry);
|
2010-01-26 16:15:02 +00:00
|
|
|
for (int i = 0; i < tracks.n_track_entry; i++) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "| + a track...\n");
|
2010-01-26 16:15:02 +00:00
|
|
|
parse_trackentry(demuxer, &tracks.track_entry[i]);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2010-01-26 16:15:02 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 15:40:23 +00:00
|
|
|
static void cue_index_add(demuxer_t *demuxer, int track_id, uint64_t filepos,
|
2014-11-05 19:44:21 +00:00
|
|
|
uint64_t timecode, uint64_t duration)
|
2013-04-11 15:40:23 +00:00
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
MP_TARRAY_GROW(mkv_d, mkv_d->indexes, mkv_d->num_indexes);
|
|
|
|
|
2014-11-05 19:44:21 +00:00
|
|
|
mkv_d->indexes[mkv_d->num_indexes] = (mkv_index_t) {
|
|
|
|
.tnum = track_id,
|
|
|
|
.filepos = filepos,
|
|
|
|
.timecode = timecode,
|
|
|
|
.duration = duration,
|
|
|
|
};
|
|
|
|
|
2013-04-11 15:40:23 +00:00
|
|
|
mkv_d->num_indexes++;
|
|
|
|
}
|
|
|
|
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
static void add_block_position(demuxer_t *demuxer, struct mkv_track *track,
|
2014-11-05 19:44:21 +00:00
|
|
|
uint64_t filepos,
|
|
|
|
uint64_t timecode, uint64_t duration)
|
2013-04-11 15:40:23 +00:00
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
if (mkv_d->index_complete || !track)
|
2013-04-11 15:40:23 +00:00
|
|
|
return;
|
2014-11-05 19:44:21 +00:00
|
|
|
|
|
|
|
mkv_d->index_has_durations = true;
|
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
if (track->last_index_entry != (size_t)-1) {
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
mkv_index_t *index = &mkv_d->indexes[track->last_index_entry];
|
2014-11-05 19:42:20 +00:00
|
|
|
// Never add blocks which are already covered by the index.
|
|
|
|
if (index->timecode >= timecode)
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-11-05 19:44:21 +00:00
|
|
|
cue_index_add(demuxer, track->tnum, filepos, timecode, duration);
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
track->last_index_entry = mkv_d->num_indexes - 1;
|
2013-04-11 15:40:23 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static int demux_mkv_read_cues(demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2013-07-07 21:54:11 +00:00
|
|
|
struct MPOpts *opts = demuxer->opts;
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
|
2014-01-22 21:12:14 +00:00
|
|
|
mkv_d->deferred_cues = 0;
|
|
|
|
|
2014-06-13 00:05:37 +00:00
|
|
|
if (opts->index_mode != 1) {
|
2014-01-14 16:38:21 +00:00
|
|
|
ebml_read_skip(demuxer->log, -1, s);
|
2004-01-19 19:16:10 +00:00
|
|
|
return 0;
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "/---- [ parsing cues ] -----------\n");
|
2013-10-19 21:15:06 +00:00
|
|
|
struct ebml_cues cues = {0};
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = {demuxer->log};
|
2010-01-25 11:19:09 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &cues, &ebml_cues_desc) < 0)
|
2011-08-20 18:45:42 +00:00
|
|
|
return -1;
|
2013-05-22 22:26:42 +00:00
|
|
|
|
|
|
|
mkv_d->num_indexes = 0;
|
2014-11-14 22:52:47 +00:00
|
|
|
mkv_d->index_has_durations = false;
|
2013-05-22 22:26:42 +00:00
|
|
|
|
2010-01-25 11:19:09 +00:00
|
|
|
for (int i = 0; i < cues.n_cue_point; i++) {
|
|
|
|
struct ebml_cue_point *cuepoint = &cues.cue_point[i];
|
|
|
|
if (cuepoint->n_cue_time != 1 || !cuepoint->n_cue_track_positions) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Malformed CuePoint element\n");
|
2010-01-25 11:19:09 +00:00
|
|
|
continue;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2010-01-25 11:19:09 +00:00
|
|
|
uint64_t time = cuepoint->cue_time;
|
2013-07-22 22:45:23 +00:00
|
|
|
for (int c = 0; c < cuepoint->n_cue_track_positions; c++) {
|
2010-01-25 11:19:09 +00:00
|
|
|
struct ebml_cue_track_positions *trackpos =
|
2013-07-22 22:45:23 +00:00
|
|
|
&cuepoint->cue_track_positions[c];
|
2013-04-11 15:40:23 +00:00
|
|
|
uint64_t pos = mkv_d->segment_start + trackpos->cue_cluster_position;
|
2014-11-05 19:44:21 +00:00
|
|
|
cue_index_add(demuxer, trackpos->cue_track, pos,
|
|
|
|
time, trackpos->cue_duration);
|
|
|
|
mkv_d->index_has_durations |= trackpos->n_cue_duration > 0;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_DBG(demuxer, "|+ found cue point for track %" PRIu64
|
2014-11-05 19:44:21 +00:00
|
|
|
": timecode %" PRIu64 ", filepos: %" PRIu64
|
|
|
|
" offset %" PRIu64 ", duration %" PRIu64 "\n",
|
|
|
|
trackpos->cue_track, time, pos,
|
|
|
|
trackpos->cue_relative_position, trackpos->cue_duration);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 15:40:23 +00:00
|
|
|
// Do not attempt to create index on the fly.
|
|
|
|
mkv_d->index_complete = true;
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "\\---- [ parsing cues ] -----------\n");
|
2010-01-25 11:19:09 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-22 22:26:42 +00:00
|
|
|
static void read_deferred_cues(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
|
|
|
|
if (mkv_d->deferred_cues) {
|
|
|
|
int64_t pos = mkv_d->deferred_cues;
|
|
|
|
mkv_d->deferred_cues = 0;
|
|
|
|
if (!stream_seek(s, pos)) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Failed to seek to cues\n");
|
2013-05-22 22:26:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-14 16:38:51 +00:00
|
|
|
if (ebml_read_id(s) != MATROSKA_ID_CUES) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Expected element not found\n");
|
2013-05-22 22:26:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
demux_mkv_read_cues(demuxer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 01:26:32 +00:00
|
|
|
static int demux_mkv_read_chapters(struct demuxer *demuxer)
|
|
|
|
{
|
2009-12-01 12:28:34 +00:00
|
|
|
struct MPOpts *opts = demuxer->opts;
|
2009-03-13 01:26:32 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
2013-08-21 16:41:59 +00:00
|
|
|
int wanted_edition = opts->edition_id;
|
2013-09-26 06:53:54 +00:00
|
|
|
uint64_t wanted_edition_uid = demuxer->matroska_data.uid.edition;
|
|
|
|
|
|
|
|
/* A specific edition UID was requested; ignore the user option which is
|
|
|
|
* only applicable to the top-level file. */
|
|
|
|
if (wanted_edition_uid)
|
|
|
|
wanted_edition = -1;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "/---- [ parsing chapters ] ---------\n");
|
2013-10-19 21:15:06 +00:00
|
|
|
struct ebml_chapters file_chapters = {0};
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = {demuxer->log};
|
2010-01-26 11:39:33 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &file_chapters,
|
|
|
|
&ebml_chapters_desc) < 0)
|
2011-08-20 18:45:42 +00:00
|
|
|
return -1;
|
2009-03-13 01:26:32 +00:00
|
|
|
|
2013-09-26 06:53:54 +00:00
|
|
|
int selected_edition = -1;
|
2010-01-26 11:39:33 +00:00
|
|
|
int num_editions = file_chapters.n_edition_entry;
|
|
|
|
struct ebml_edition_entry *editions = file_chapters.edition_entry;
|
2014-01-22 23:54:08 +00:00
|
|
|
for (int i = 0; i < num_editions; i++) {
|
|
|
|
struct demux_edition new = {
|
|
|
|
.demuxer_id = editions[i].edition_uid,
|
|
|
|
.default_edition = editions[i].edition_flag_default,
|
|
|
|
.metadata = talloc_zero(demuxer, struct mp_tags),
|
|
|
|
};
|
|
|
|
MP_TARRAY_APPEND(demuxer, demuxer->editions, demuxer->num_editions, new);
|
|
|
|
}
|
2013-08-21 16:41:59 +00:00
|
|
|
if (wanted_edition >= 0 && wanted_edition < num_editions) {
|
|
|
|
selected_edition = wanted_edition;
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_VERBOSE(demuxer, "User-specified edition: %d\n", selected_edition);
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < num_editions; i++) {
|
2013-09-26 06:53:54 +00:00
|
|
|
if (wanted_edition_uid &&
|
|
|
|
editions[i].edition_uid == wanted_edition_uid) {
|
|
|
|
selected_edition = i;
|
|
|
|
break;
|
|
|
|
} else if (editions[i].edition_flag_default) {
|
2010-01-26 11:39:33 +00:00
|
|
|
selected_edition = i;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Default edition: %d\n", i);
|
2010-01-26 11:39:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 06:53:54 +00:00
|
|
|
if (selected_edition < 0) {
|
|
|
|
if (wanted_edition_uid) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_ERR(demuxer, "Unable to find expected edition uid: %"PRIu64"\n",
|
2013-09-26 06:53:54 +00:00
|
|
|
wanted_edition_uid);
|
|
|
|
return -1;
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2013-09-26 06:53:54 +00:00
|
|
|
selected_edition = 0;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2013-09-26 06:53:54 +00:00
|
|
|
}
|
2009-03-16 21:30:48 +00:00
|
|
|
|
2010-01-26 11:39:33 +00:00
|
|
|
for (int idx = 0; idx < num_editions; idx++) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "New edition %d\n", idx);
|
2010-01-26 11:39:33 +00:00
|
|
|
int warn_level = idx == selected_edition ? MSGL_WARN : MSGL_V;
|
|
|
|
if (editions[idx].n_edition_flag_default)
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Default edition flag: %"PRIu64
|
2014-07-05 14:42:03 +00:00
|
|
|
"\n", editions[idx].edition_flag_default);
|
2010-01-26 11:39:33 +00:00
|
|
|
if (editions[idx].n_edition_flag_ordered)
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Ordered chapter flag: %"PRIu64
|
2014-07-05 14:42:03 +00:00
|
|
|
"\n", editions[idx].edition_flag_ordered);
|
2013-10-19 21:02:53 +00:00
|
|
|
|
|
|
|
int chapter_count = editions[idx].n_chapter_atom;
|
|
|
|
|
|
|
|
struct matroska_chapter *m_chapters = NULL;
|
|
|
|
if (idx == selected_edition && editions[idx].edition_flag_ordered) {
|
|
|
|
m_chapters = talloc_array_ptrtype(demuxer, m_chapters, chapter_count);
|
|
|
|
demuxer->matroska_data.ordered_chapters = m_chapters;
|
|
|
|
demuxer->matroska_data.num_ordered_chapters = chapter_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < chapter_count; i++) {
|
2010-01-26 11:39:33 +00:00
|
|
|
struct ebml_chapter_atom *ca = editions[idx].chapter_atom + i;
|
2013-10-19 21:15:06 +00:00
|
|
|
struct matroska_chapter chapter = {0};
|
2010-01-26 11:39:33 +00:00
|
|
|
struct bstr name = { "(unnamed)", 9 };
|
|
|
|
|
|
|
|
if (!ca->n_chapter_time_start)
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_MSG(demuxer, warn_level, "Chapter lacks start time\n");
|
2011-01-23 22:29:01 +00:00
|
|
|
chapter.start = ca->chapter_time_start;
|
|
|
|
chapter.end = ca->chapter_time_end;
|
2010-01-26 11:39:33 +00:00
|
|
|
|
|
|
|
if (ca->n_chapter_display) {
|
|
|
|
if (ca->n_chapter_display > 1)
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_MSG(demuxer, warn_level, "Multiple chapter "
|
2010-01-26 11:39:33 +00:00
|
|
|
"names not supported, picking first\n");
|
|
|
|
if (!ca->chapter_display[0].n_chap_string)
|
2014-07-05 14:42:03 +00:00
|
|
|
MP_MSG(demuxer, warn_level, "Malformed chapter name entry\n");
|
2010-01-26 11:39:33 +00:00
|
|
|
else
|
|
|
|
name = ca->chapter_display[0].chap_string;
|
2009-03-13 01:26:32 +00:00
|
|
|
}
|
2010-01-26 11:39:33 +00:00
|
|
|
|
|
|
|
if (ca->n_chapter_segment_uid) {
|
|
|
|
chapter.has_segment_uid = true;
|
|
|
|
int len = ca->chapter_segment_uid.len;
|
2013-10-07 00:49:12 +00:00
|
|
|
if (len != sizeof(chapter.uid.segment))
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_MSG(demuxer, warn_level,
|
|
|
|
"Chapter segment uid bad length %d\n", len);
|
2013-09-26 06:21:19 +00:00
|
|
|
else {
|
2013-10-07 00:49:12 +00:00
|
|
|
memcpy(chapter.uid.segment, ca->chapter_segment_uid.start,
|
2010-01-26 11:39:33 +00:00
|
|
|
len);
|
2013-09-26 06:21:19 +00:00
|
|
|
if (ca->n_chapter_segment_edition_uid)
|
|
|
|
chapter.uid.edition = ca->chapter_segment_edition_uid;
|
|
|
|
else
|
|
|
|
chapter.uid.edition = 0;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Chapter segment uid ");
|
2013-07-22 22:45:23 +00:00
|
|
|
for (int n = 0; n < len; n++)
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "%02x ",
|
2013-10-07 00:49:12 +00:00
|
|
|
chapter.uid.segment[n]);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "\n");
|
2010-01-26 11:39:33 +00:00
|
|
|
}
|
2009-03-16 21:30:48 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Chapter %u from %02d:%02d:%02d.%03d "
|
2010-04-24 17:28:09 +00:00
|
|
|
"to %02d:%02d:%02d.%03d, %.*s\n", i,
|
2011-01-23 22:29:01 +00:00
|
|
|
(int) (chapter.start / 60 / 60 / 1000000000),
|
|
|
|
(int) ((chapter.start / 60 / 1000000000) % 60),
|
|
|
|
(int) ((chapter.start / 1000000000) % 60),
|
|
|
|
(int) (chapter.start % 1000000000),
|
|
|
|
(int) (chapter.end / 60 / 60 / 1000000000),
|
|
|
|
(int) ((chapter.end / 60 / 1000000000) % 60),
|
|
|
|
(int) ((chapter.end / 1000000000) % 60),
|
|
|
|
(int) (chapter.end % 1000000000),
|
2010-05-20 20:50:18 +00:00
|
|
|
BSTR_P(name));
|
2010-01-26 11:39:33 +00:00
|
|
|
|
2013-10-19 21:02:53 +00:00
|
|
|
if (idx == selected_edition) {
|
2014-11-02 16:20:04 +00:00
|
|
|
demuxer_add_chapter(demuxer, name, chapter.start / 1e9,
|
2013-09-08 05:42:05 +00:00
|
|
|
ca->chapter_uid);
|
2013-10-19 21:02:53 +00:00
|
|
|
}
|
|
|
|
if (m_chapters) {
|
|
|
|
chapter.name = talloc_strndup(m_chapters, name.start, name.len);
|
|
|
|
m_chapters[i] = chapter;
|
2010-01-26 11:39:33 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 23:19:42 +00:00
|
|
|
demuxer->num_editions = num_editions;
|
|
|
|
demuxer->edition = selected_edition;
|
|
|
|
|
2010-01-26 11:39:33 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "\\---- [ parsing chapters ] ---------\n");
|
2009-03-13 01:26:32 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static int demux_mkv_read_tags(demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2014-01-22 23:54:08 +00:00
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
2010-03-28 12:21:42 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = {demuxer->log};
|
2013-10-19 21:15:06 +00:00
|
|
|
struct ebml_tags tags = {0};
|
2010-03-28 12:21:42 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &tags, &ebml_tags_desc) < 0)
|
2011-08-20 18:45:42 +00:00
|
|
|
return -1;
|
2010-03-28 12:21:42 +00:00
|
|
|
|
2014-01-22 23:54:08 +00:00
|
|
|
mkv_d->tags = talloc_memdup(mkv_d, &tags, sizeof(tags));
|
|
|
|
talloc_steal(mkv_d->tags, parse_ctx.talloc_ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_tags(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
|
|
|
struct ebml_tags *tags = mkv_d->tags;
|
|
|
|
|
|
|
|
if (!tags)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = 0; i < tags->n_tag; i++) {
|
|
|
|
struct ebml_tag tag = tags->tag[i];
|
|
|
|
if (tag.targets.target_track_uid || tag.targets.target_attachment_uid)
|
2010-03-28 12:21:42 +00:00
|
|
|
continue;
|
|
|
|
|
2014-02-06 12:43:01 +00:00
|
|
|
struct mp_tags *dst = NULL;
|
|
|
|
|
2013-09-08 05:42:05 +00:00
|
|
|
if (tag.targets.target_chapter_uid) {
|
2014-02-06 12:43:01 +00:00
|
|
|
for (int n = 0; n < demuxer->num_chapters; n++) {
|
|
|
|
if (demuxer->chapters[n].demuxer_id ==
|
|
|
|
tag.targets.target_chapter_uid)
|
|
|
|
{
|
|
|
|
dst = demuxer->chapters[n].metadata;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-08 05:42:05 +00:00
|
|
|
}
|
2014-01-22 23:54:08 +00:00
|
|
|
} else if (tag.targets.target_edition_uid) {
|
|
|
|
for (int n = 0; n < demuxer->num_editions; n++) {
|
|
|
|
if (demuxer->editions[n].demuxer_id ==
|
|
|
|
tag.targets.target_edition_uid)
|
|
|
|
{
|
2014-02-06 12:43:01 +00:00
|
|
|
dst = demuxer->editions[n].metadata;
|
2014-01-22 23:54:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 05:42:05 +00:00
|
|
|
} else {
|
2014-02-06 12:43:01 +00:00
|
|
|
dst = demuxer->metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst) {
|
2013-09-08 05:42:05 +00:00
|
|
|
for (int j = 0; j < tag.n_simple_tag; j++) {
|
2014-02-06 12:43:01 +00:00
|
|
|
mp_tags_set_bstr(dst, tag.simple_tag[j].tag_name,
|
|
|
|
tag.simple_tag[j].tag_string);
|
2013-09-08 05:42:05 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-28 12:21:42 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static int demux_mkv_read_attachments(demuxer_t *demuxer)
|
2006-07-07 18:26:51 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "/---- [ parsing attachments ] ---------\n");
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2013-10-19 21:15:06 +00:00
|
|
|
struct ebml_attachments attachments = {0};
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = {demuxer->log};
|
2010-01-25 17:42:37 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &attachments,
|
|
|
|
&ebml_attachments_desc) < 0)
|
2011-08-20 18:45:42 +00:00
|
|
|
return -1;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2010-01-25 17:42:37 +00:00
|
|
|
for (int i = 0; i < attachments.n_attached_file; i++) {
|
|
|
|
struct ebml_attached_file *attachment = &attachments.attached_file[i];
|
|
|
|
if (!attachment->n_file_name || !attachment->n_file_mime_type
|
|
|
|
|| !attachment->n_file_data) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Malformed attachment\n");
|
2010-01-25 17:42:37 +00:00
|
|
|
continue;
|
2006-07-07 18:26:51 +00:00
|
|
|
}
|
2010-01-25 17:42:37 +00:00
|
|
|
struct bstr name = attachment->file_name;
|
|
|
|
struct bstr mime = attachment->file_mime_type;
|
2010-05-19 10:44:37 +00:00
|
|
|
demuxer_add_attachment(demuxer, name, mime, attachment->file_data);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Attachment: %.*s, %.*s, %zu bytes\n",
|
2014-07-05 14:42:03 +00:00
|
|
|
BSTR_P(name), BSTR_P(mime), attachment->file_data.len);
|
2006-07-07 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 17:42:37 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "\\---- [ parsing attachments ] ---------\n");
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2006-07-07 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 10:37:42 +00:00
|
|
|
static int read_header_element(struct demuxer *demuxer, uint32_t id,
|
2012-11-18 19:46:12 +00:00
|
|
|
int64_t at_filepos);
|
2010-01-25 10:37:42 +00:00
|
|
|
|
2014-01-22 21:12:14 +00:00
|
|
|
static struct header_elem *get_header_element(struct demuxer *demuxer,
|
|
|
|
uint32_t id,
|
|
|
|
int64_t element_filepos)
|
|
|
|
{
|
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
|
|
|
|
|
|
|
// Note that some files in fact contain a SEEKHEAD with a list of all
|
|
|
|
// clusters - we have no use for that.
|
|
|
|
if (!ebml_is_mkv_level1_id(id) || id == MATROSKA_ID_CLUSTER)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (int n = 0; n < mkv_d->num_headers; n++) {
|
|
|
|
struct header_elem *elem = &mkv_d->headers[n];
|
|
|
|
// SEEKHEAD is the only element that can happen multiple times.
|
|
|
|
// Other elements might be duplicated (or attempted to be read twice,
|
|
|
|
// even if it's only once in the file), but only the first is used.
|
|
|
|
if (elem->id == id && (id != MATROSKA_ID_SEEKHEAD ||
|
|
|
|
elem->pos == element_filepos))
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
struct header_elem elem = { .id = id, .pos = element_filepos };
|
|
|
|
MP_TARRAY_APPEND(mkv_d, mkv_d->headers, mkv_d->num_headers, elem);
|
|
|
|
return &mkv_d->headers[mkv_d->num_headers - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the level 1 element with the given id as read. Return whether it
|
|
|
|
// was marked read before (e.g. for checking whether it was already read).
|
|
|
|
// element_filepos refers to the file position of the element ID.
|
|
|
|
static bool test_header_element(struct demuxer *demuxer, uint32_t id,
|
|
|
|
int64_t element_filepos)
|
|
|
|
{
|
|
|
|
struct header_elem *elem = get_header_element(demuxer, id, element_filepos);
|
|
|
|
if (!elem)
|
|
|
|
return false;
|
|
|
|
if (elem->parsed)
|
|
|
|
return true;
|
|
|
|
elem->parsed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static int demux_mkv_read_seekhead(demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2010-01-25 10:37:42 +00:00
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
|
|
|
struct stream *s = demuxer->stream;
|
|
|
|
int res = 0;
|
2013-10-19 21:15:06 +00:00
|
|
|
struct ebml_seek_head seekhead = {0};
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = {demuxer->log};
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "/---- [ parsing seek head ] ---------\n");
|
2010-01-25 10:37:42 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &seekhead, &ebml_seek_head_desc) < 0) {
|
2011-08-04 05:38:39 +00:00
|
|
|
res = -1;
|
2010-01-25 10:37:42 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < seekhead.n_seek; i++) {
|
|
|
|
struct ebml_seek *seek = &seekhead.seek[i];
|
|
|
|
if (seek->n_seek_id != 1 || seek->n_seek_position != 1) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Invalid SeekHead entry\n");
|
2010-01-25 10:37:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint64_t pos = seek->seek_position + mkv_d->segment_start;
|
2014-12-29 22:14:19 +00:00
|
|
|
MP_DBG(demuxer, "Element 0x%x at %"PRIu64".\n",
|
|
|
|
(unsigned)seek->seek_id, pos);
|
2014-01-22 21:12:14 +00:00
|
|
|
get_header_element(demuxer, seek->seek_id, pos);
|
|
|
|
// This is nice to warn against incomplete files.
|
2014-05-24 12:04:09 +00:00
|
|
|
int64_t end = 0;
|
|
|
|
stream_control(s, STREAM_CTRL_GET_SIZE, &end);
|
|
|
|
if (pos >= end)
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "SeekHead position beyond "
|
2014-01-22 21:12:14 +00:00
|
|
|
"end of file - incomplete file?\n");
|
2010-01-25 10:37:42 +00:00
|
|
|
}
|
|
|
|
out:
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "\\---- [ parsing seek head ] ---------\n");
|
2010-01-25 10:37:42 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
|
|
|
return res;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-25 10:37:42 +00:00
|
|
|
static int read_header_element(struct demuxer *demuxer, uint32_t id,
|
2014-01-22 21:12:14 +00:00
|
|
|
int64_t start_filepos)
|
2010-01-25 10:37:42 +00:00
|
|
|
{
|
2014-01-22 21:12:14 +00:00
|
|
|
if (id == EBML_ID_INVALID)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (test_header_element(demuxer, id, start_filepos))
|
|
|
|
goto skip;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-25 10:37:42 +00:00
|
|
|
switch(id) {
|
|
|
|
case MATROSKA_ID_INFO:
|
2011-08-04 05:38:39 +00:00
|
|
|
return demux_mkv_read_info(demuxer);
|
2010-01-25 10:37:42 +00:00
|
|
|
case MATROSKA_ID_TRACKS:
|
2011-08-20 18:45:42 +00:00
|
|
|
return demux_mkv_read_tracks(demuxer);
|
2010-01-25 10:37:42 +00:00
|
|
|
case MATROSKA_ID_CUES:
|
2011-08-20 18:45:42 +00:00
|
|
|
return demux_mkv_read_cues(demuxer);
|
2010-01-25 10:37:42 +00:00
|
|
|
case MATROSKA_ID_TAGS:
|
2011-08-20 18:45:42 +00:00
|
|
|
return demux_mkv_read_tags(demuxer);
|
2010-01-25 10:37:42 +00:00
|
|
|
case MATROSKA_ID_SEEKHEAD:
|
2011-08-04 05:38:39 +00:00
|
|
|
return demux_mkv_read_seekhead(demuxer);
|
2010-01-25 10:37:42 +00:00
|
|
|
case MATROSKA_ID_CHAPTERS:
|
2011-08-20 18:45:42 +00:00
|
|
|
return demux_mkv_read_chapters(demuxer);
|
2010-01-25 10:37:42 +00:00
|
|
|
case MATROSKA_ID_ATTACHMENTS:
|
2011-08-20 18:45:42 +00:00
|
|
|
return demux_mkv_read_attachments(demuxer);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2014-01-22 21:12:14 +00:00
|
|
|
skip:
|
|
|
|
ebml_read_skip(demuxer->log, -1, demuxer->stream);
|
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:53:51 +00:00
|
|
|
static void add_coverart(struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < demuxer->num_attachments; n++) {
|
|
|
|
struct demux_attachment *att = &demuxer->attachments[n];
|
|
|
|
const char *codec = mp_map_mimetype_to_video_codec(att->type);
|
|
|
|
if (!codec)
|
|
|
|
continue;
|
|
|
|
struct sh_stream *sh = new_sh_stream(demuxer, STREAM_VIDEO);
|
|
|
|
if (!sh)
|
|
|
|
break;
|
|
|
|
sh->codec = codec;
|
|
|
|
sh->attached_picture = new_demux_packet_from(att->data, att->data_size);
|
2014-12-23 01:50:53 +00:00
|
|
|
sh->title = att->name;
|
2014-12-22 11:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-25 10:37:42 +00:00
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track);
|
|
|
|
static int demux_mkv_open_audio(demuxer_t *demuxer, mkv_track_t *track);
|
|
|
|
static int demux_mkv_open_sub(demuxer_t *demuxer, mkv_track_t *track);
|
2006-08-26 19:17:04 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static void display_create_tracks(demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
for (int i = 0; i < mkv_d->num_tracks; i++) {
|
2009-12-29 19:06:21 +00:00
|
|
|
switch (mkv_d->tracks[i]->type) {
|
2004-01-19 19:16:10 +00:00
|
|
|
case MATROSKA_TRACK_VIDEO:
|
2013-04-14 17:23:18 +00:00
|
|
|
demux_mkv_open_video(demuxer, mkv_d->tracks[i]);
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
case MATROSKA_TRACK_AUDIO:
|
2013-04-14 17:23:18 +00:00
|
|
|
demux_mkv_open_audio(demuxer, mkv_d->tracks[i]);
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
case MATROSKA_TRACK_SUBTITLE:
|
2013-04-14 17:23:18 +00:00
|
|
|
demux_mkv_open_sub(demuxer, mkv_d->tracks[i]);
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-10 13:58:57 +00:00
|
|
|
typedef struct {
|
2009-12-29 19:06:21 +00:00
|
|
|
char *id;
|
|
|
|
int fourcc;
|
|
|
|
int extradata;
|
2007-05-10 13:58:57 +00:00
|
|
|
} videocodec_info_t;
|
|
|
|
|
|
|
|
static const videocodec_info_t vinfo[] = {
|
2013-11-02 14:10:28 +00:00
|
|
|
{MKV_V_MJPEG, MP_FOURCC('m', 'j', 'p', 'g'), 1},
|
|
|
|
{MKV_V_MPEG1, MP_FOURCC('m', 'p', 'g', '1'), 0},
|
|
|
|
{MKV_V_MPEG2, MP_FOURCC('m', 'p', 'g', '2'), 0},
|
|
|
|
{MKV_V_MPEG4_SP, MP_FOURCC('m', 'p', '4', 'v'), 1},
|
|
|
|
{MKV_V_MPEG4_ASP, MP_FOURCC('m', 'p', '4', 'v'), 1},
|
|
|
|
{MKV_V_MPEG4_AP, MP_FOURCC('m', 'p', '4', 'v'), 1},
|
|
|
|
{MKV_V_MPEG4_AVC, MP_FOURCC('a', 'v', 'c', '1'), 1},
|
|
|
|
{MKV_V_THEORA, MP_FOURCC('t', 'h', 'e', 'o'), 1},
|
|
|
|
{MKV_V_VP8, MP_FOURCC('V', 'P', '8', '0'), 0},
|
2014-12-31 14:00:18 +00:00
|
|
|
{MKV_V_VP9, MP_FOURCC('V', 'P', '9', '0'), 0},
|
2013-11-02 14:10:28 +00:00
|
|
|
{MKV_V_DIRAC, MP_FOURCC('d', 'r', 'a', 'c'), 0},
|
|
|
|
{MKV_V_PRORES, MP_FOURCC('p', 'r', '0', '0'), 0},
|
|
|
|
{MKV_V_HEVC, MP_FOURCC('H', 'E', 'V', 'C'), 1},
|
2014-04-26 20:24:15 +00:00
|
|
|
{0}
|
2007-05-10 13:58:57 +00:00
|
|
|
};
|
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2014-10-13 14:42:00 +00:00
|
|
|
unsigned char *extradata = NULL;
|
2013-04-14 04:04:58 +00:00
|
|
|
unsigned int extradata_size = 0;
|
2014-09-25 00:22:44 +00:00
|
|
|
struct sh_stream *sh = new_sh_stream(demuxer, STREAM_VIDEO);
|
|
|
|
if (!sh)
|
|
|
|
return 1;
|
|
|
|
track->stream = sh;
|
|
|
|
sh_video_t *sh_v = sh->video;
|
|
|
|
sh->demuxer_id = track->tnum;
|
|
|
|
sh->title = talloc_strdup(sh_v, track->name);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->ms_compat) { /* MS compatibility mode */
|
2014-09-25 00:22:44 +00:00
|
|
|
// The private_data contains a BITMAPINFOHEADER struct
|
|
|
|
if (track->private_data == NULL || track->private_size < 40)
|
2009-12-29 19:06:21 +00:00
|
|
|
return 1;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-09-25 00:22:44 +00:00
|
|
|
unsigned char *h = track->private_data;
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->v_width == 0)
|
2014-09-25 00:22:44 +00:00
|
|
|
track->v_width = AV_RL32(h + 4); // biWidth
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->v_height == 0)
|
2014-09-25 00:22:44 +00:00
|
|
|
track->v_height = AV_RL32(h + 8); // biHeight
|
|
|
|
sh_v->bits_per_coded_sample = AV_RL16(h + 14); // biBitCount
|
|
|
|
sh->format = AV_RL32(h + 16); // biCompression
|
|
|
|
|
|
|
|
extradata = track->private_data + 40;
|
|
|
|
extradata_size = track->private_size - 40;
|
2014-12-08 17:49:39 +00:00
|
|
|
mp_set_codec_from_tag(sh);
|
2009-12-29 19:06:21 +00:00
|
|
|
} else {
|
2014-09-25 00:22:44 +00:00
|
|
|
sh_v->bits_per_coded_sample = 24;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
|
|
|
if (track->private_size >= RVPROPERTIES_SIZE
|
|
|
|
&& (!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))) {
|
2013-04-14 04:04:58 +00:00
|
|
|
unsigned char *src;
|
2009-12-29 19:06:21 +00:00
|
|
|
uint32_t type2;
|
|
|
|
unsigned int cnt;
|
|
|
|
|
|
|
|
src = (uint8_t *) track->private_data + RVPROPERTIES_SIZE;
|
|
|
|
|
|
|
|
cnt = track->private_size - RVPROPERTIES_SIZE;
|
|
|
|
type2 = AV_RB32(src - 4);
|
|
|
|
if (type2 == 0x10003000 || type2 == 0x10003001)
|
2014-09-25 00:22:44 +00:00
|
|
|
sh->format = MP_FOURCC('R', 'V', '1', '3');
|
2009-12-29 19:06:21 +00:00
|
|
|
else
|
2014-09-25 00:22:44 +00:00
|
|
|
sh->format = MP_FOURCC('R', 'V', track->codec_id[9], '0');
|
2009-12-29 19:06:21 +00:00
|
|
|
// copy type1 and type2 info from rv properties
|
2013-04-14 04:04:58 +00:00
|
|
|
extradata_size = cnt + 8;
|
|
|
|
extradata = src - 8;
|
2009-12-29 19:06:21 +00:00
|
|
|
track->realmedia = 1;
|
2014-12-08 17:49:39 +00:00
|
|
|
mp_set_codec_from_tag(sh);
|
2013-01-24 15:57:21 +00:00
|
|
|
} else if (strcmp(track->codec_id, MKV_V_UNCOMPRESSED) == 0) {
|
|
|
|
// raw video, "like AVI" - this is a FourCC
|
2014-09-25 00:22:44 +00:00
|
|
|
sh->format = track->colorspace;
|
2014-12-08 17:49:39 +00:00
|
|
|
sh->codec = "rawvideo";
|
|
|
|
} else if (strcmp(track->codec_id, MKV_V_QUICKTIME) == 0) {
|
|
|
|
uint32_t fourcc1 = 0, fourcc2 = 0;
|
|
|
|
if (track->private_size >= 8) {
|
|
|
|
fourcc1 = AV_RL32(track->private_data + 0);
|
|
|
|
fourcc2 = AV_RL32(track->private_data + 4);
|
|
|
|
}
|
|
|
|
if (fourcc1 == MP_FOURCC('S', 'V', 'Q', '3') ||
|
|
|
|
fourcc2 == MP_FOURCC('S', 'V', 'Q', '3'))
|
|
|
|
{
|
|
|
|
sh->codec = "svq3";
|
|
|
|
extradata = track->private_data;
|
|
|
|
extradata_size = track->private_size;
|
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
} else {
|
|
|
|
const videocodec_info_t *vi = vinfo;
|
|
|
|
while (vi->id && strcmp(vi->id, track->codec_id))
|
|
|
|
vi++;
|
2014-12-08 17:49:39 +00:00
|
|
|
if (vi->id) {
|
|
|
|
sh->format = vi->fourcc;
|
|
|
|
mp_set_codec_from_tag(sh);
|
|
|
|
}
|
2014-11-27 21:24:32 +00:00
|
|
|
if (vi->extradata && track->private_data && track->private_size > 0)
|
|
|
|
{
|
2013-04-14 04:04:58 +00:00
|
|
|
extradata = track->private_data;
|
|
|
|
extradata_size = track->private_size;
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 14:00:18 +00:00
|
|
|
if (sh->format == MP_FOURCC('V', 'P', '9', '0'))
|
|
|
|
track->parse = true;
|
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
if (extradata_size > 0x1000000) {
|
|
|
|
MP_WARN(demuxer, "Invalid CodecPrivate\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-25 00:22:44 +00:00
|
|
|
sh_v->coded_width = track->v_width;
|
|
|
|
sh_v->coded_height = track->v_height;
|
2014-09-24 22:59:15 +00:00
|
|
|
sh_v->extradata = talloc_memdup(sh_v, extradata, extradata_size);
|
|
|
|
sh_v->extradata_len = extradata_size;
|
2014-12-08 17:49:39 +00:00
|
|
|
if (!sh->codec) {
|
|
|
|
MP_WARN(demuxer, "Unknown/unsupported CodecID (%s) or missing/bad "
|
|
|
|
"CodecPrivate data (track %u).\n",
|
|
|
|
track->codec_id, track->tnum);
|
2013-01-29 23:57:07 +00:00
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->v_frate == 0.0)
|
|
|
|
track->v_frate = 25.0;
|
|
|
|
sh_v->fps = track->v_frate;
|
|
|
|
sh_v->aspect = 0;
|
|
|
|
if (!track->realmedia) {
|
|
|
|
sh_v->disp_w = track->v_width;
|
|
|
|
sh_v->disp_h = track->v_height;
|
2013-04-03 23:20:06 +00:00
|
|
|
uint32_t dw = track->v_dwidth_set ? track->v_dwidth : track->v_width;
|
|
|
|
uint32_t dh = track->v_dheight_set ? track->v_dheight : track->v_height;
|
2013-03-27 23:00:04 +00:00
|
|
|
if (dw && dh)
|
|
|
|
sh_v->aspect = (double) dw / dh;
|
2009-12-29 19:06:21 +00:00
|
|
|
} 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;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Aspect: %f\n", sh_v->aspect);
|
video: add insane hack to work around FFmpeg/Libav insanity
So, FFmpeg/Libav requires us to figure out video timestamps ourselves
(see last 10 commits or so), but the methods it provides for this aren't
even sufficient. In particular, everything that uses AVI-style DTS (avi,
vfw-muxed mkv, possibly mpeg4-in-ogm) with a codec that has an internal
frame delay is broken. In this case, libavcodec will shift the packet-
to-image correspondence by the codec delay, meaning that with a delay=1,
the first AVFrame.pkt_dts is not 0, but that of the second packet. All
timestamps will appear shifted. The start time (e.g. the time displayed
when doing "mpv file.avi --pause") will not be exactly 0.
(According to Libav developers, this is how it's supposed to work; just
that the first DTS values are normally negative with formats that use
DTS "properly". Who cares if it doesn't work at all with very common
video formats? There's no indication that they'll fix this soon,
either. An elegant workaround is missing too.)
Add a hack to re-enable the old PTS code for AVI and vfw-muxed MKV.
Since these timestamps are not reorderd, we wouldn't need to sort them,
but it's less code this way (and possibly more robust, should a demuxer
unexpectedly output PTS).
The original intention of all the timestamp changes recently was
actually to get rid of demuxer-specific hacks and the old timestamp
sorting code, but it looks like this didn't work out. Yet another case
where trying to replace native MPlayer functionality with FFmpeg/Libav
led to disadvantages and bugs. (Note that the old PTS sorting code
doesn't and can't handle frame dropping correctly, though.)
Bug reports:
https://trac.ffmpeg.org/ticket/3178
https://bugzilla.libav.org/show_bug.cgi?id=600
2013-11-28 14:10:45 +00:00
|
|
|
sh_v->avi_dts = track->ms_compat;
|
2014-08-30 21:24:46 +00:00
|
|
|
sh_v->stereo_mode = track->stereo_mode;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
static const struct mkv_audio_tag {
|
2011-08-20 02:54:27 +00:00
|
|
|
char *id; bool prefix; uint32_t formattag;
|
|
|
|
} mkv_audio_tags[] = {
|
|
|
|
{ MKV_A_MP2, 0, 0x0055 },
|
2014-12-31 14:00:18 +00:00
|
|
|
{ MKV_A_MP3, 0, 0x0055 },
|
2011-08-20 02:54:27 +00:00
|
|
|
{ MKV_A_AC3, 1, 0x2000 },
|
2013-11-02 14:10:28 +00:00
|
|
|
{ MKV_A_EAC3, 1, MP_FOURCC('E', 'A', 'C', '3') },
|
2011-08-20 02:54:27 +00:00
|
|
|
{ MKV_A_DTS, 0, 0x2001 },
|
|
|
|
{ MKV_A_PCM, 0, 0x0001 },
|
|
|
|
{ MKV_A_PCM_BE, 0, 0x0001 },
|
2013-11-11 17:40:59 +00:00
|
|
|
{ MKV_A_PCM_FLT, 0, 0x0003 },
|
2013-11-02 14:10:28 +00:00
|
|
|
{ MKV_A_AAC_2MAIN, 0, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_AAC_2LC, 1, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_AAC_2SSR, 0, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_AAC_4MAIN, 0, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_AAC_4LC, 1, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_AAC_4SSR, 0, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_AAC_4LTP, 0, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_AAC, 0, MP_FOURCC('M', 'P', '4', 'A') },
|
|
|
|
{ MKV_A_VORBIS, 0, MP_FOURCC('v', 'r', 'b', 's') },
|
|
|
|
{ MKV_A_OPUS, 0, MP_FOURCC('O', 'p', 'u', 's') },
|
|
|
|
{ MKV_A_OPUS_EXP, 0, MP_FOURCC('O', 'p', 'u', 's') },
|
|
|
|
{ MKV_A_QDMC, 0, MP_FOURCC('Q', 'D', 'M', 'C') },
|
|
|
|
{ MKV_A_QDMC2, 0, MP_FOURCC('Q', 'D', 'M', '2') },
|
|
|
|
{ MKV_A_WAVPACK, 0, MP_FOURCC('W', 'V', 'P', 'K') },
|
2014-12-31 14:00:18 +00:00
|
|
|
{ MKV_A_TRUEHD, 0, MP_FOURCC('T', 'R', 'H', 'D') },
|
2013-11-02 14:10:28 +00:00
|
|
|
{ MKV_A_FLAC, 0, MP_FOURCC('f', 'L', 'a', 'C') },
|
|
|
|
{ MKV_A_ALAC, 0, MP_FOURCC('a', 'L', 'a', 'C') },
|
|
|
|
{ MKV_A_REAL28, 0, MP_FOURCC('2', '8', '_', '8') },
|
|
|
|
{ MKV_A_REALATRC, 0, MP_FOURCC('a', 't', 'r', 'c') },
|
|
|
|
{ MKV_A_REALCOOK, 0, MP_FOURCC('c', 'o', 'o', 'k') },
|
|
|
|
{ MKV_A_REALDNET, 0, MP_FOURCC('d', 'n', 'e', 't') },
|
|
|
|
{ MKV_A_REALSIPR, 0, MP_FOURCC('s', 'i', 'p', 'r') },
|
|
|
|
{ MKV_A_TTA1, 0, MP_FOURCC('T', 'T', 'A', '1') },
|
2011-08-20 02:54:27 +00:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
static int demux_mkv_open_audio(demuxer_t *demuxer, mkv_track_t *track)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2013-04-14 17:23:18 +00:00
|
|
|
struct sh_stream *sh = new_sh_stream(demuxer, STREAM_AUDIO);
|
|
|
|
if (!sh)
|
2009-12-29 19:06:21 +00:00
|
|
|
return 1;
|
2013-04-14 17:23:18 +00:00
|
|
|
track->stream = sh;
|
|
|
|
sh_audio_t *sh_a = sh->audio;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-11-27 21:43:38 +00:00
|
|
|
if (track->private_size > 0x1000000)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
unsigned char *extradata = track->private_data;
|
|
|
|
unsigned int extradata_len = track->private_size;
|
2014-09-24 23:56:51 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->language && (strcmp(track->language, "und") != 0))
|
2013-11-23 20:37:56 +00:00
|
|
|
sh->lang = talloc_strdup(sh_a, track->language);
|
|
|
|
sh->demuxer_id = track->tnum;
|
|
|
|
sh->title = talloc_strdup(sh_a, track->name);
|
|
|
|
sh->default_track = track->default_track;
|
2013-07-16 20:44:15 +00:00
|
|
|
if (!track->a_osfreq)
|
|
|
|
track->a_osfreq = track->a_sfreq;
|
2014-09-25 00:22:44 +00:00
|
|
|
|
2011-08-20 02:54:27 +00:00
|
|
|
if (track->ms_compat) {
|
2014-09-25 00:22:44 +00:00
|
|
|
// The private_data contains a WAVEFORMATEX struct
|
|
|
|
if (track->private_size < 18)
|
2011-08-20 02:54:27 +00:00
|
|
|
goto error;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "track with MS compat audio.\n");
|
2014-09-25 00:22:44 +00:00
|
|
|
unsigned char *h = track->private_data;
|
|
|
|
track->a_formattag = AV_RL16(h + 0); // wFormatTag
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->a_channels == 0)
|
2014-09-25 00:22:44 +00:00
|
|
|
track->a_channels = AV_RL16(h + 2); // nChannels
|
|
|
|
if (track->a_osfreq == 0.0)
|
|
|
|
track->a_osfreq = AV_RL32(h + 4); // nSamplesPerSec
|
|
|
|
sh_a->bitrate = AV_RL32(h + 8) * 8; // nAvgBytesPerSec
|
|
|
|
sh_a->block_align = AV_RL16(h + 12); // nBlockAlign
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->a_bps == 0)
|
2014-09-25 00:22:44 +00:00
|
|
|
track->a_bps = AV_RL16(h + 14); // wBitsPerSample
|
|
|
|
extradata = track->private_data + 18;
|
|
|
|
extradata_len = track->private_size - 18;
|
2009-12-29 19:06:21 +00:00
|
|
|
} else {
|
2011-08-20 02:54:27 +00:00
|
|
|
for (int i = 0; ; i++) {
|
2014-06-10 21:56:05 +00:00
|
|
|
const struct mkv_audio_tag *t = mkv_audio_tags + i;
|
2011-08-20 02:54:27 +00:00
|
|
|
if (t->id == NULL)
|
|
|
|
goto error;
|
|
|
|
if (t->prefix) {
|
2012-07-28 21:47:42 +00:00
|
|
|
if (!bstr_startswith0(bstr0(track->codec_id), t->id))
|
2011-08-20 02:54:27 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (strcmp(track->codec_id, t->id))
|
|
|
|
continue;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2011-08-20 02:54:27 +00:00
|
|
|
track->a_formattag = t->formattag;
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-23 20:37:56 +00:00
|
|
|
sh->format = track->a_formattag;
|
2014-09-24 23:56:51 +00:00
|
|
|
mp_chmap_set_unknown(&sh_a->channels, track->a_channels);
|
2013-07-16 20:44:15 +00:00
|
|
|
sh_a->samplerate = (uint32_t) track->a_osfreq;
|
2014-11-27 21:24:32 +00:00
|
|
|
sh_a->bits_per_coded_sample = track->a_bps ? track->a_bps : 16;
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->a_formattag == 0x0055) { /* MP3 || MP2 */
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = 16000 * 8;
|
|
|
|
sh_a->block_align = 1152;
|
2014-12-31 14:00:18 +00:00
|
|
|
track->parse = true;
|
2009-12-29 19:06:21 +00:00
|
|
|
} else if ((track->a_formattag == 0x2000) /* AC3 */
|
2013-11-02 14:10:28 +00:00
|
|
|
|| track->a_formattag == MP_FOURCC('E', 'A', 'C', '3')
|
2009-12-29 19:06:21 +00:00
|
|
|
|| (track->a_formattag == 0x2001)) { /* DTS */
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bits_per_coded_sample = 0;
|
|
|
|
sh_a->bitrate = 0;
|
|
|
|
sh_a->block_align = 0;
|
2009-12-29 19:06:21 +00:00
|
|
|
} else if (track->a_formattag == 0x0001) { /* PCM || PCM_BE */
|
|
|
|
if (!strcmp(track->codec_id, MKV_A_PCM_BE))
|
2013-11-23 20:37:56 +00:00
|
|
|
sh->format = MP_FOURCC('t', 'w', 'o', 's');
|
2013-11-11 17:40:59 +00:00
|
|
|
} else if (track->a_formattag == 0x0003) { /* PCM_FLT */
|
|
|
|
/* ok */
|
2009-12-29 19:06:21 +00:00
|
|
|
} else if (!strcmp(track->codec_id, MKV_A_QDMC)
|
|
|
|
|| !strcmp(track->codec_id, MKV_A_QDMC2)) {
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = 16000 * 8;
|
|
|
|
sh_a->block_align = 1486;
|
2013-11-02 14:10:28 +00:00
|
|
|
} else if (track->a_formattag == MP_FOURCC('M', 'P', '4', 'A')) {
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = 16000 * 8;
|
|
|
|
sh_a->block_align = 1024;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-11-27 21:43:38 +00:00
|
|
|
if (strcmp(track->codec_id, MKV_A_AAC)) {
|
2013-05-30 17:58:18 +00:00
|
|
|
/* Recreate the 'private data' */
|
2014-11-27 21:24:32 +00:00
|
|
|
int srate_idx = aac_get_sample_rate_index(track->a_sfreq);
|
2014-11-27 20:54:37 +00:00
|
|
|
const char *tail = "";
|
|
|
|
if (strlen(track->codec_id) >= 12)
|
|
|
|
tail = &track->codec_id[12];
|
2014-11-27 21:24:32 +00:00
|
|
|
int profile = 3;
|
2014-11-27 20:54:37 +00:00
|
|
|
if (!strncmp(tail, "MAIN", 4))
|
2013-05-30 17:58:18 +00:00
|
|
|
profile = 0;
|
2014-11-27 20:54:37 +00:00
|
|
|
else if (!strncmp(tail, "LC", 2))
|
2013-05-30 17:58:18 +00:00
|
|
|
profile = 1;
|
2014-11-27 20:54:37 +00:00
|
|
|
else if (!strncmp(tail, "SSR", 3))
|
2013-05-30 17:58:18 +00:00
|
|
|
profile = 2;
|
2013-11-14 18:51:09 +00:00
|
|
|
sh_a->codecdata = talloc_size(sh_a, 5);
|
2013-05-30 17:58:18 +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;
|
|
|
|
|
|
|
|
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 / sh_a->samplerate;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-11-02 14:10:28 +00:00
|
|
|
} else if (track->a_formattag == MP_FOURCC('v', 'r', 'b', 's')) {
|
2011-08-20 02:54:27 +00:00
|
|
|
/* VORBIS */
|
2013-02-14 12:23:26 +00:00
|
|
|
} else if (!strcmp(track->codec_id, MKV_A_OPUS)
|
|
|
|
|| !strcmp(track->codec_id, MKV_A_OPUS_EXP)) {
|
2013-11-23 20:37:56 +00:00
|
|
|
sh->format = MP_FOURCC('O', 'p', 'u', 's');
|
2011-08-20 02:54:27 +00:00
|
|
|
} else if (!strncmp(track->codec_id, MKV_A_REALATRC, 7)) {
|
|
|
|
if (track->private_size < RAPROPERTIES4_SIZE)
|
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
/* Common initialization for all RealAudio codecs */
|
|
|
|
unsigned char *src = track->private_data;
|
|
|
|
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = 0; /* FIXME !? */
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-11-27 21:24:32 +00:00
|
|
|
int version = AV_RB16(src + 4);
|
|
|
|
unsigned int flavor = AV_RB16(src + 22);
|
2009-12-29 19:06:21 +00:00
|
|
|
track->coded_framesize = AV_RB32(src + 24);
|
|
|
|
track->sub_packet_h = AV_RB16(src + 40);
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->block_align = track->audiopk_size = AV_RB16(src + 42);
|
2009-12-29 19:06:21 +00:00
|
|
|
track->sub_packet_size = AV_RB16(src + 44);
|
|
|
|
if (version == 4) {
|
|
|
|
src += RAPROPERTIES4_SIZE;
|
|
|
|
src += src[0] + 1;
|
|
|
|
src += src[0] + 1;
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2009-12-29 19:06:21 +00:00
|
|
|
src += RAPROPERTIES5_SIZE;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
if (track->audiopk_size == 0 || track->sub_packet_size == 0 ||
|
|
|
|
track->sub_packet_h == 0 || track->coded_framesize == 0)
|
|
|
|
goto error;
|
|
|
|
if (track->coded_framesize > 0x40000000)
|
|
|
|
goto error;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
src += 3;
|
|
|
|
if (version == 5)
|
|
|
|
src++;
|
2014-11-27 21:24:32 +00:00
|
|
|
uint32_t codecdata_length = AV_RB32(src);
|
|
|
|
if (codecdata_length > 0x1000000)
|
2014-06-29 21:22:07 +00:00
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
src += 4;
|
2014-09-24 23:56:51 +00:00
|
|
|
extradata_len = codecdata_length;
|
|
|
|
extradata = src;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
|
|
|
switch (track->a_formattag) {
|
2013-11-02 14:10:28 +00:00
|
|
|
case MP_FOURCC('a', 't', 'r', 'c'):
|
2014-11-21 04:07:13 +00:00
|
|
|
if (flavor >= MP_ARRAY_SIZE(atrc_fl2bps))
|
|
|
|
goto error;
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = atrc_fl2bps[flavor] * 8;
|
|
|
|
sh_a->block_align = track->sub_packet_size;
|
2011-08-20 02:54:27 +00:00
|
|
|
goto audiobuf;
|
2013-11-02 14:10:28 +00:00
|
|
|
case MP_FOURCC('c', 'o', 'o', 'k'):
|
2014-11-21 04:07:13 +00:00
|
|
|
if (flavor >= MP_ARRAY_SIZE(cook_fl2bps))
|
|
|
|
goto error;
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = cook_fl2bps[flavor] * 8;
|
|
|
|
sh_a->block_align = track->sub_packet_size;
|
2011-08-20 02:54:27 +00:00
|
|
|
goto audiobuf;
|
2013-11-02 14:10:28 +00:00
|
|
|
case MP_FOURCC('s', 'i', 'p', 'r'):
|
2014-11-21 04:07:13 +00:00
|
|
|
if (flavor >= MP_ARRAY_SIZE(sipr_fl2bps))
|
|
|
|
goto error;
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = sipr_fl2bps[flavor] * 8;
|
|
|
|
sh_a->block_align = track->coded_framesize;
|
2011-08-20 02:54:27 +00:00
|
|
|
goto audiobuf;
|
2013-11-02 14:10:28 +00:00
|
|
|
case MP_FOURCC('2', '8', '_', '8'):
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bitrate = 3600 * 8;
|
|
|
|
sh_a->block_align = track->coded_framesize;
|
2011-08-20 02:54:27 +00:00
|
|
|
audiobuf:
|
2009-12-29 19:06:21 +00:00
|
|
|
track->audio_buf =
|
2014-06-29 21:22:07 +00:00
|
|
|
talloc_array_size(track, track->sub_packet_h, track->audiopk_size);
|
2009-12-29 19:06:21 +00:00
|
|
|
track->audio_timestamp =
|
2014-06-29 21:22:07 +00:00
|
|
|
talloc_array(track, double, track->sub_packet_h);
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
|
|
|
|
track->realmedia = 1;
|
|
|
|
} else if (!strcmp(track->codec_id, MKV_A_FLAC)
|
|
|
|
|| (track->a_formattag == 0xf1ac)) {
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->bits_per_coded_sample = 0;
|
|
|
|
sh_a->bitrate = 0;
|
|
|
|
sh_a->block_align = 0;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-11-27 21:43:38 +00:00
|
|
|
if (track->ms_compat)
|
2013-11-23 20:37:56 +00:00
|
|
|
sh->format = MP_FOURCC('f', 'L', 'a', 'C');
|
2014-11-27 21:43:38 +00:00
|
|
|
unsigned char *ptr = extradata;
|
|
|
|
unsigned int size = extradata_len;
|
2009-12-29 19:06:21 +00:00
|
|
|
if (size < 4 || ptr[0] != 'f' || ptr[1] != 'L' || ptr[2] != 'a'
|
|
|
|
|| ptr[3] != 'C') {
|
2013-11-14 18:51:09 +00:00
|
|
|
sh_a->codecdata = talloc_size(sh_a, 4);
|
2009-10-17 17:30:49 +00:00
|
|
|
sh_a->codecdata_len = 4;
|
|
|
|
memcpy(sh_a->codecdata, "fLaC", 4);
|
2009-12-29 19:06:21 +00:00
|
|
|
} else {
|
2013-11-14 18:51:09 +00:00
|
|
|
sh_a->codecdata = talloc_size(sh_a, size);
|
2009-10-17 17:30:49 +00:00
|
|
|
sh_a->codecdata_len = size;
|
|
|
|
memcpy(sh_a->codecdata, ptr, size);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-03-15 06:49:47 +00:00
|
|
|
} else if (!strcmp(track->codec_id, MKV_A_ALAC)) {
|
2014-11-27 21:43:38 +00:00
|
|
|
if (track->private_size) {
|
2013-03-15 06:49:47 +00:00
|
|
|
sh_a->codecdata_len = track->private_size + 12;
|
2013-11-14 18:51:09 +00:00
|
|
|
sh_a->codecdata = talloc_size(sh_a, sh_a->codecdata_len);
|
2013-03-15 06:49:47 +00:00
|
|
|
char *data = sh_a->codecdata;
|
|
|
|
AV_WB32(data + 0, sh_a->codecdata_len);
|
|
|
|
memcpy(data + 4, "alac", 4);
|
|
|
|
AV_WB32(data + 8, 0);
|
|
|
|
memcpy(data + 12, track->private_data, track->private_size);
|
|
|
|
}
|
2014-12-31 14:00:18 +00:00
|
|
|
} else if (track->a_formattag == MP_FOURCC('W', 'V', 'P', 'K')) {
|
2014-11-27 21:43:38 +00:00
|
|
|
/* ok */
|
2014-12-31 14:00:18 +00:00
|
|
|
} else if (track->a_formattag == MP_FOURCC('T', 'R', 'H', 'D')) {
|
|
|
|
track->parse = true;
|
2013-11-02 14:10:28 +00:00
|
|
|
} else if (track->a_formattag == MP_FOURCC('T', 'T', 'A', '1')) {
|
2010-04-02 13:00:42 +00:00
|
|
|
sh_a->codecdata_len = 30;
|
2013-11-14 18:51:09 +00:00
|
|
|
sh_a->codecdata = talloc_zero_size(sh_a, sh_a->codecdata_len);
|
2010-04-02 13:00:42 +00:00
|
|
|
if (!sh_a->codecdata)
|
|
|
|
goto error;
|
|
|
|
char *data = sh_a->codecdata;
|
|
|
|
memcpy(data + 0, "TTA1", 4);
|
|
|
|
AV_WL16(data + 4, 1);
|
2013-04-06 20:43:12 +00:00
|
|
|
AV_WL16(data + 6, sh_a->channels.num);
|
2014-09-24 23:56:51 +00:00
|
|
|
AV_WL16(data + 8, sh_a->bits_per_coded_sample);
|
2013-07-16 20:44:15 +00:00
|
|
|
AV_WL32(data + 10, track->a_osfreq);
|
2010-04-02 13:00:42 +00:00
|
|
|
// Bogus: last frame won't be played.
|
|
|
|
AV_WL32(data + 14, 0);
|
2011-08-20 02:54:27 +00:00
|
|
|
} else if (!track->ms_compat) {
|
|
|
|
goto error;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
demux_mkv: ignore DefaultDuration in some cases
This fixes playback of the sample linked by FFmpeg ticket 2508. The fix
follows ffmpeg commit 6158a3b (although it's not exactly the same).
The problem here is that the file contains an apparently non-sense
DefaultDuration value. DefaultDuration for audio tracks is used to
derive PTS values for packets with no timestamps, like they can happen
with frames inside a laced block. So the first packet of a SimpleBlock
will have a correct PTS, while the PTS values of the following packets
are calculated using DefaultDuration, and thus are broken.
This leads to seemingly ok playback, but broken A/V sync. Not using the
DefaultDuration value will leave the PTS values of these packets unset,
and the audio decoder can derive them from the output instead.
The fix more or less uses a heuristic to detect the broken case: if the
sample rate is 8 KHz (Matroska default, can assume unset), and the codec
is AC3 (as the broken file did), don't use it. I'm not sure why this
should be done only for AC3, maybe the muxing application (mkvmerge
v4.9.1) has known issues with AC3. AC3 also doesn't support 8 KHz as
sample rate natively.
(By the way, I'm not sure why we should honor the DefaultDuration at all
for audio. It doesn't seem to be needed. You can't seek to these frames,
and decoders should always be able to produce perfect PTS values by
adding the duration of the decoded audio to the first PTS.)
2013-07-16 20:59:55 +00:00
|
|
|
// Some files have broken default DefaultDuration set, which will lead to
|
|
|
|
// audio packets with incorrect timestamps. This follows FFmpeg commit
|
|
|
|
// 6158a3b, sample see FFmpeg ticket 2508.
|
|
|
|
if (sh_a->samplerate == 8000 && strcmp(track->codec_id, MKV_A_AC3) == 0)
|
|
|
|
track->default_duration = 0;
|
|
|
|
|
2013-11-23 20:37:56 +00:00
|
|
|
mp_set_codec_from_tag(sh);
|
core: redo how codecs are mapped, remove codecs.conf
Use codec names instead of FourCCs to identify codecs. Rewrite how
codecs are selected and initialized. Now each decoder exports a list
of decoders (and the codec it supports) via add_decoders(). The order
matters, and the first decoder for a given decoder is preferred over
the other decoders. E.g. all ad_mpg123 decoders are preferred over
ad_lavc, because it comes first in the mpcodecs_ad_drivers array.
Likewise, decoders within ad_lavc that are enumerated first by
libavcodec (using av_codec_next()) are preferred. (This is actually
critical to select h264 software decoding by default instead of vdpau.
libavcodec and ffmpeg/avconv use the same method to select decoders by
default, so we hope this is sane.)
The codec names follow libavcodec's codec names as defined by
AVCodecDescriptor.name (see libavcodec/codec_desc.c). Some decoders
have names different from the canonical codec name. The AVCodecDescriptor
API is relatively new, so we need a compatibility layer for older
libavcodec versions for codec names that are referenced internally,
and which are different from the decoder name. (Add a configure check
for that, because checking versions is getting way too messy.)
demux/codec_tags.c is generated from the former codecs.conf (minus
"special" decoders like vdpau, and excluding the mappings that are the
same as the mappings libavformat's exported RIFF tables). It contains
all the mappings from FourCCs to codec name. This is needed for
demux_mkv, demux_mpg, demux_avi and demux_asf. demux_lavf will set the
codec as determined by libavformat, while the other demuxers have to do
this on their own, using the mp_set_audio/video_codec_from_tag()
functions. Note that the sh_audio/video->format members don't uniquely
identify the codec anymore, and sh->codec takes over this role.
Replace the --ac/--vc/--afm/--vfm with new --vd/--ad options, which
provide cover the functionality of the removed switched.
Note: there's no CODECS_FLAG_FLIP flag anymore. This means some obscure
container/video combinations (e.g. the sample Film_200_zygo_pro.mov)
are played flipped. ffplay/avplay doesn't handle this properly either,
so we don't care and blame ffmeg/libav instead.
2013-02-09 14:15:19 +00:00
|
|
|
|
2014-11-27 21:43:38 +00:00
|
|
|
if (!sh_a->codecdata && extradata_len) {
|
2014-09-24 23:56:51 +00:00
|
|
|
sh_a->codecdata = talloc_memdup(sh_a, extradata, extradata_len);
|
|
|
|
sh_a->codecdata_len = extradata_len;
|
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2011-08-20 02:54:27 +00:00
|
|
|
|
|
|
|
error:
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Unknown/unsupported audio "
|
|
|
|
"codec ID '%s' for track %u or missing/faulty\n"
|
2011-08-20 02:54:27 +00:00
|
|
|
"private codec data.\n", track->codec_id, track->tnum);
|
|
|
|
return 1;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
static const char *const mkv_sub_tag[][2] = {
|
2013-04-15 19:25:21 +00:00
|
|
|
{ MKV_S_VOBSUB, "dvd_subtitle" },
|
|
|
|
{ MKV_S_TEXTSSA, "ass"},
|
|
|
|
{ MKV_S_TEXTASS, "ass"},
|
|
|
|
{ MKV_S_SSA, "ass"},
|
|
|
|
{ MKV_S_ASS, "ass"},
|
|
|
|
{ MKV_S_TEXTASCII, "subrip"},
|
|
|
|
{ MKV_S_TEXTUTF8, "subrip"},
|
|
|
|
{ MKV_S_PGS, "hdmv_pgs_subtitle"},
|
2013-08-24 13:17:37 +00:00
|
|
|
{ MKV_S_WEBVTT_S, "webvtt-webm"},
|
|
|
|
{ MKV_S_WEBVTT_C, "webvtt-webm"},
|
2014-06-17 20:44:27 +00:00
|
|
|
{ MKV_S_DVB, "dvb_subtitle"},
|
2013-04-15 19:25:21 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
static int demux_mkv_open_sub(demuxer_t *demuxer, mkv_track_t *track)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2013-04-15 19:25:21 +00:00
|
|
|
const char *subtitle_type = NULL;
|
|
|
|
for (int n = 0; mkv_sub_tag[n][0]; n++) {
|
|
|
|
if (strcmp(track->codec_id, mkv_sub_tag[n][0]) == 0) {
|
|
|
|
subtitle_type = mkv_sub_tag[n][1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 08:22:54 +00:00
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
if (track->private_size > 0x10000000)
|
|
|
|
return 1;
|
|
|
|
|
2013-11-23 20:37:56 +00:00
|
|
|
struct sh_stream *sh = new_sh_stream(demuxer, STREAM_SUB);
|
|
|
|
if (!sh)
|
2013-04-16 08:22:54 +00:00
|
|
|
return 1;
|
2013-11-23 20:37:56 +00:00
|
|
|
track->stream = sh;
|
|
|
|
sh_sub_t *sh_s = sh->sub;
|
|
|
|
sh->demuxer_id = track->tnum;
|
|
|
|
sh->codec = subtitle_type;
|
2014-11-27 21:24:32 +00:00
|
|
|
bstr in = (bstr){track->private_data, track->private_size};
|
2013-12-21 19:24:20 +00:00
|
|
|
bstr buffer = demux_mkv_decode(demuxer->log, track, in, 2);
|
2013-04-16 08:22:54 +00:00
|
|
|
if (buffer.start && buffer.start != track->private_data) {
|
|
|
|
talloc_free(track->private_data);
|
|
|
|
talloc_steal(track, buffer.start);
|
|
|
|
track->private_data = buffer.start;
|
|
|
|
track->private_size = buffer.len;
|
|
|
|
}
|
2013-11-23 20:37:56 +00:00
|
|
|
sh_s->extradata = talloc_size(sh, track->private_size);
|
|
|
|
memcpy(sh_s->extradata, track->private_data, track->private_size);
|
|
|
|
sh_s->extradata_len = track->private_size;
|
2013-04-16 08:22:54 +00:00
|
|
|
if (track->language && (strcmp(track->language, "und") != 0))
|
2013-11-23 20:37:56 +00:00
|
|
|
sh->lang = talloc_strdup(sh, track->language);
|
|
|
|
sh->title = talloc_strdup(sh, track->name);
|
|
|
|
sh->default_track = track->default_track;
|
2013-04-16 08:22:54 +00:00
|
|
|
|
2014-07-05 14:42:03 +00:00
|
|
|
if (!subtitle_type)
|
|
|
|
MP_ERR(demuxer, "Subtitle type '%s' is not supported.\n", track->codec_id);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 00:49:07 +00:00
|
|
|
static int read_ebml_header(demuxer_t *demuxer)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
|
2014-01-14 16:38:51 +00:00
|
|
|
if (ebml_read_id(s) != EBML_ID_EBML)
|
2010-01-25 00:43:27 +00:00
|
|
|
return 0;
|
2013-11-04 22:49:22 +00:00
|
|
|
struct ebml_ebml ebml_master = {{0}};
|
2013-12-21 19:24:20 +00:00
|
|
|
struct ebml_parse_ctx parse_ctx = { demuxer->log, .no_error_messages = true };
|
2010-01-25 00:43:27 +00:00
|
|
|
if (ebml_read_element(s, &parse_ctx, &ebml_master, &ebml_ebml_desc) < 0)
|
|
|
|
return 0;
|
2010-10-21 19:58:51 +00:00
|
|
|
if (ebml_master.doc_type.start == NULL) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "File has EBML header but no doctype."
|
2014-07-05 14:42:03 +00:00
|
|
|
" Assuming \"matroska\".\n");
|
2012-07-28 21:47:42 +00:00
|
|
|
} else if (bstrcmp(ebml_master.doc_type, bstr0("matroska")) != 0
|
|
|
|
&& bstrcmp(ebml_master.doc_type, bstr0("webm")) != 0) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_DBG(demuxer, "no head found\n");
|
2010-01-25 00:43:27 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ebml_master.doc_type_read_version > 2) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "This looks like a Matroska file, "
|
2014-07-05 14:42:03 +00:00
|
|
|
"but we don't support format version %"PRIu64"\n",
|
|
|
|
ebml_master.doc_type_read_version);
|
2010-01-25 00:43:27 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((ebml_master.n_ebml_read_version
|
|
|
|
&& ebml_master.ebml_read_version != EBML_VERSION)
|
|
|
|
|| (ebml_master.n_ebml_max_size_length
|
|
|
|
&& ebml_master.ebml_max_size_length > 8)
|
|
|
|
|| (ebml_master.n_ebml_max_id_length
|
2013-10-19 21:10:03 +00:00
|
|
|
&& ebml_master.ebml_max_id_length != 4))
|
|
|
|
{
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "This looks like a Matroska file, "
|
2014-07-05 14:42:03 +00:00
|
|
|
"but the header has bad parameters\n");
|
2010-01-25 00:43:27 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2010-01-25 00:43:27 +00:00
|
|
|
talloc_free(parse_ctx.talloc_ctx);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-14 00:49:07 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-01-22 22:37:03 +00:00
|
|
|
static int read_mkv_segment_header(demuxer_t *demuxer, int64_t *segment_end)
|
2013-04-14 00:49:07 +00:00
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
int num_skip = 0;
|
|
|
|
if (demuxer->params)
|
|
|
|
num_skip = demuxer->params->matroska_wanted_segment;
|
|
|
|
|
|
|
|
while (!s->eof) {
|
2014-01-14 16:38:51 +00:00
|
|
|
if (ebml_read_id(s) != MATROSKA_ID_SEGMENT) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "segment not found\n");
|
2013-04-14 00:49:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "+ a segment...\n");
|
2014-01-14 16:38:51 +00:00
|
|
|
uint64_t len = ebml_read_length(s);
|
2014-01-22 22:37:03 +00:00
|
|
|
*segment_end = (len == EBML_UINT_INVALID) ? 0 : stream_tell(s) + len;
|
2013-04-14 00:49:07 +00:00
|
|
|
if (num_skip <= 0)
|
|
|
|
return 1;
|
|
|
|
num_skip--;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, " (skipping)\n");
|
2014-01-22 22:37:03 +00:00
|
|
|
if (*segment_end <= 0)
|
2013-04-14 00:49:07 +00:00
|
|
|
break;
|
2014-05-24 12:04:09 +00:00
|
|
|
int64_t end = 0;
|
|
|
|
stream_control(s, STREAM_CTRL_GET_SIZE, &end);
|
|
|
|
if (*segment_end >= end)
|
2013-12-14 20:55:03 +00:00
|
|
|
return 0;
|
2014-01-22 22:37:03 +00:00
|
|
|
if (!stream_seek(s, *segment_end)) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Failed to seek in file\n");
|
2013-04-14 00:49:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Segments are like concatenated Matroska files
|
|
|
|
if (!read_ebml_header(demuxer))
|
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "End of file, no further segments.\n");
|
2013-04-14 00:49:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
static int demux_mkv_open(demuxer_t *demuxer, enum demux_check check)
|
2013-04-14 00:49:07 +00:00
|
|
|
{
|
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
mkv_demuxer_t *mkv_d;
|
2014-01-22 21:12:14 +00:00
|
|
|
int64_t start_pos;
|
2014-01-22 22:37:03 +00:00
|
|
|
int64_t end_pos;
|
2013-04-14 00:49:07 +00:00
|
|
|
|
2014-11-16 17:51:11 +00:00
|
|
|
bstr start = stream_peek(s, 4);
|
2014-11-21 02:50:40 +00:00
|
|
|
uint32_t start_id = 0;
|
2014-11-16 17:51:11 +00:00
|
|
|
for (int n = 0; n < start.len; n++)
|
|
|
|
start_id = (start_id << 8) | start.start[n];
|
|
|
|
if (start_id != EBML_ID_EBML)
|
|
|
|
return -1;
|
|
|
|
|
2013-04-14 00:49:07 +00:00
|
|
|
if (!read_ebml_header(demuxer))
|
2013-07-11 18:08:12 +00:00
|
|
|
return -1;
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "Found the head...\n");
|
2013-04-14 00:49:07 +00:00
|
|
|
|
2014-01-22 22:37:03 +00:00
|
|
|
if (!read_mkv_segment_header(demuxer, &end_pos))
|
2013-07-11 18:08:12 +00:00
|
|
|
return -1;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2010-01-25 10:37:42 +00:00
|
|
|
mkv_d = talloc_zero(demuxer, struct mkv_demuxer);
|
2009-12-29 19:06:21 +00:00
|
|
|
demuxer->priv = mkv_d;
|
|
|
|
mkv_d->tc_scale = 1000000;
|
|
|
|
mkv_d->segment_start = stream_tell(s);
|
2014-01-22 22:37:03 +00:00
|
|
|
mkv_d->segment_end = end_pos;
|
2014-11-03 19:00:34 +00:00
|
|
|
mkv_d->a_skip_preroll = 1;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-14 00:49:07 +00:00
|
|
|
if (demuxer->params && demuxer->params->matroska_was_valid)
|
|
|
|
*demuxer->params->matroska_was_valid = true;
|
|
|
|
|
2011-08-04 05:38:39 +00:00
|
|
|
while (1) {
|
2014-01-22 21:12:14 +00:00
|
|
|
start_pos = stream_tell(s);
|
|
|
|
stream_peek(s, 4); // make sure we can always seek back
|
2014-01-14 16:38:51 +00:00
|
|
|
uint32_t id = ebml_read_id(s);
|
2011-09-25 15:35:09 +00:00
|
|
|
if (s->eof) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Unexpected end of file (no clusters found)\n");
|
2013-04-14 00:39:11 +00:00
|
|
|
break;
|
2011-09-25 15:35:09 +00:00
|
|
|
}
|
2011-08-20 18:45:42 +00:00
|
|
|
if (id == MATROSKA_ID_CLUSTER) {
|
2014-01-22 21:12:14 +00:00
|
|
|
MP_VERBOSE(demuxer, "|+ found cluster\n");
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2014-01-22 21:12:14 +00:00
|
|
|
int res = read_header_element(demuxer, id, start_pos);
|
|
|
|
if (res < 0)
|
2014-01-22 21:33:55 +00:00
|
|
|
return -1;
|
2014-01-22 21:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read headers that come after the first cluster (i.e. require seeking).
|
|
|
|
// Note: reading might increase ->num_headers.
|
|
|
|
// Likewise, ->headers might be reallocated.
|
|
|
|
for (int n = 0; n < mkv_d->num_headers; n++) {
|
|
|
|
struct header_elem *elem = &mkv_d->headers[n];
|
|
|
|
if (elem->parsed)
|
|
|
|
continue;
|
|
|
|
elem->parsed = true;
|
|
|
|
if (elem->id == MATROSKA_ID_CUES) {
|
|
|
|
// Read cues when they are needed, to avoid seeking on opening.
|
|
|
|
MP_VERBOSE(demuxer, "Deferring reading cues.\n");
|
|
|
|
mkv_d->deferred_cues = elem->pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
MP_VERBOSE(demuxer, "Seeking to %"PRIu64" to read header element 0x%x.\n",
|
|
|
|
elem->pos, (unsigned)elem->id);
|
2014-05-24 12:04:09 +00:00
|
|
|
int64_t end = 0;
|
|
|
|
stream_control(s, STREAM_CTRL_GET_SIZE, &end);
|
|
|
|
if (elem->pos >= end) {
|
2014-01-22 21:12:14 +00:00
|
|
|
MP_WARN(demuxer, "SeekHead position beyond "
|
|
|
|
"end of file - incomplete file?\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!stream_seek(s, elem->pos)) {
|
|
|
|
MP_WARN(demuxer, "Failed to seek when reading header element.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ebml_read_id(s) != elem->id) {
|
|
|
|
MP_ERR(demuxer, "Expected element 0x%x not found\n",
|
|
|
|
(unsigned int)elem->id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
elem->parsed = false; // don't make read_header_element skip it
|
|
|
|
int res = read_header_element(demuxer, elem->id, elem->pos);
|
2011-08-20 18:45:42 +00:00
|
|
|
if (res < 0)
|
2014-01-22 21:33:55 +00:00
|
|
|
return -1;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2014-01-22 21:12:14 +00:00
|
|
|
if (!stream_seek(s, start_pos)) {
|
|
|
|
MP_ERR(demuxer, "Couldn't seek back after reading headers?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_VERBOSE(demuxer, "All headers are parsed!\n");
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-01-22 23:54:08 +00:00
|
|
|
process_tags(demuxer);
|
2009-12-29 19:06:21 +00:00
|
|
|
display_create_tracks(demuxer);
|
2014-12-22 11:53:51 +00:00
|
|
|
add_coverart(demuxer);
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-11-18 22:07:20 +00:00
|
|
|
if (demuxer->opts->mkv_probe_duration)
|
|
|
|
probe_last_timestamp(demuxer);
|
|
|
|
|
2013-07-11 18:08:12 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 15:25:51 +00:00
|
|
|
static bool bstr_read_u8(bstr *buffer, uint8_t *out_u8)
|
|
|
|
{
|
|
|
|
if (buffer->len > 0) {
|
|
|
|
*out_u8 = buffer->start[0];
|
|
|
|
buffer->len -= 1;
|
|
|
|
buffer->start += 1;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 16:16:50 +00:00
|
|
|
static int demux_mkv_read_block_lacing(bstr *buffer, int *laces,
|
2013-04-12 14:07:45 +00:00
|
|
|
uint32_t lace_size[MAX_NUM_LACES])
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2010-10-31 03:14:50 +00:00
|
|
|
uint32_t total = 0;
|
2013-04-13 16:16:50 +00:00
|
|
|
uint8_t flags, t;
|
2009-12-29 19:06:21 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* lacing flags */
|
2013-04-12 15:25:51 +00:00
|
|
|
if (!bstr_read_u8(buffer, &flags))
|
2010-10-31 03:14:50 +00:00
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-13 16:44:11 +00:00
|
|
|
int type = (flags >> 1) & 0x03;
|
|
|
|
if (type == 0) { /* no lacing */
|
2009-12-29 19:06:21 +00:00
|
|
|
*laces = 1;
|
2013-04-12 15:25:51 +00:00
|
|
|
lace_size[0] = buffer->len;
|
2013-04-13 16:44:11 +00:00
|
|
|
} else {
|
2013-04-13 16:16:50 +00:00
|
|
|
if (!bstr_read_u8(buffer, &t))
|
2010-10-31 03:14:50 +00:00
|
|
|
goto error;
|
2013-04-13 16:16:50 +00:00
|
|
|
*laces = t + 1;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-13 16:44:11 +00:00
|
|
|
switch (type) {
|
2009-12-29 19:06:21 +00:00
|
|
|
case 1: /* xiph lacing */
|
|
|
|
for (i = 0; i < *laces - 1; i++) {
|
|
|
|
lace_size[i] = 0;
|
|
|
|
do {
|
2013-04-12 15:25:51 +00:00
|
|
|
if (!bstr_read_u8(buffer, &t))
|
2010-10-31 03:14:50 +00:00
|
|
|
goto error;
|
2013-04-12 15:25:51 +00:00
|
|
|
lace_size[i] += t;
|
|
|
|
} while (t == 0xFF);
|
2009-12-29 19:06:21 +00:00
|
|
|
total += lace_size[i];
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-04-12 15:25:51 +00:00
|
|
|
lace_size[i] = buffer->len - total;
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
case 2: /* fixed-size lacing */
|
|
|
|
for (i = 0; i < *laces; i++)
|
2013-04-12 15:25:51 +00:00
|
|
|
lace_size[i] = buffer->len / *laces;
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
case 3:; /* EBML lacing */
|
2013-04-12 15:25:51 +00:00
|
|
|
uint64_t num = ebml_read_vlen_uint(buffer);
|
2010-10-31 03:14:50 +00:00
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
goto error;
|
2013-04-12 15:25:51 +00:00
|
|
|
if (num > buffer->len)
|
2010-10-31 03:14:50 +00:00
|
|
|
goto error;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
|
|
|
total = lace_size[0] = num;
|
2009-12-29 19:06:21 +00:00
|
|
|
for (i = 1; i < *laces - 1; i++) {
|
2013-04-12 15:25:51 +00:00
|
|
|
int64_t snum = ebml_read_vlen_int(buffer);
|
2010-10-31 03:14:50 +00:00
|
|
|
if (snum == EBML_INT_INVALID)
|
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
lace_size[i] = lace_size[i - 1] + snum;
|
2004-01-19 19:16:10 +00:00
|
|
|
total += lace_size[i];
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
2013-04-12 15:25:51 +00:00
|
|
|
lace_size[i] = buffer->len - total;
|
2004-01-19 19:16:10 +00:00
|
|
|
break;
|
2013-05-06 18:58:54 +00:00
|
|
|
|
|
|
|
default: abort();
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-13 17:17:16 +00:00
|
|
|
|
|
|
|
total = buffer->len;
|
|
|
|
for (i = 0; i < *laces; i++) {
|
|
|
|
if (lace_size[i] > total)
|
|
|
|
goto error;
|
|
|
|
total -= lace_size[i];
|
|
|
|
}
|
|
|
|
if (total != 0)
|
|
|
|
goto error;
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2010-10-31 03:14:50 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
return 1;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
demux_mkv: fix realvideo timestamp handling
This was broken by the recent commits. Apparently realvideo timestamps
are severely mangled, and Matroska _of course_ doesn't have the sane,
umangled timestamps, but something unusable. The existing unmangling
code in demux_mkv.c didn't output proper timestamps either. Instead,
it was something weird that triggered sorting. Without sorting (it was
disabled by default recently), you'd get decreasing PTS warnings
In order to fix this, steal some code from libavcodec. Basically copy
the contents of rv34_parser.c (with some changes), which makes
everything magically work. (Maybe it would be better to use the
libavcodec parser API, but I don't want to do that just for this. An
alternative idea would be refusing to read files that have realvideo
tracks, and delegate this to demux_lavf.c, but maybe that's too redical
too.)
I wish I hadn't notice this...
2013-11-26 22:41:01 +00:00
|
|
|
static int64_t real_fix_timestamp(unsigned char *buf, int len, int64_t timestamp,
|
|
|
|
unsigned int format, int64_t *kf_base,
|
|
|
|
int *kf_pts)
|
|
|
|
{
|
|
|
|
if (format != MP_FOURCC('R', 'V', '3', '0') &&
|
|
|
|
format != MP_FOURCC('R', 'V', '4', '0'))
|
|
|
|
return timestamp;
|
|
|
|
|
|
|
|
if (len < 1) // invalid packet
|
|
|
|
return timestamp;
|
|
|
|
|
|
|
|
int offset = 1 + (buf[0] + 1) * 8;
|
|
|
|
if (offset + 4 > len) // invalid packet
|
|
|
|
return timestamp;
|
|
|
|
|
|
|
|
int hdr = AV_RB32(buf + offset);
|
|
|
|
int pict_type, pts;
|
|
|
|
if (format == MP_FOURCC('R', 'V', '3', '0')) {
|
|
|
|
pict_type = (hdr >> 27) & 3;
|
|
|
|
pts = (hdr >> 7) & 0x1FFF;
|
|
|
|
} else {
|
|
|
|
pict_type = (hdr >> 29) & 3;
|
|
|
|
pts = (hdr >> 6) & 0x1FFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pict_type != 3) {
|
|
|
|
*kf_base = timestamp;
|
|
|
|
*kf_pts = pts;
|
2012-08-18 09:13:58 +00:00
|
|
|
} else {
|
2014-08-30 10:03:57 +00:00
|
|
|
timestamp = *kf_base - ((*kf_pts - pts) & 0x1FFF);
|
demux_mkv: fix realvideo timestamp handling
This was broken by the recent commits. Apparently realvideo timestamps
are severely mangled, and Matroska _of course_ doesn't have the sane,
umangled timestamps, but something unusable. The existing unmangling
code in demux_mkv.c didn't output proper timestamps either. Instead,
it was something weird that triggered sorting. Without sorting (it was
disabled by default recently), you'd get decreasing PTS warnings
In order to fix this, steal some code from libavcodec. Basically copy
the contents of rv34_parser.c (with some changes), which makes
everything magically work. (Maybe it would be better to use the
libavcodec parser API, but I don't want to do that just for this. An
alternative idea would be refusing to read files that have realvideo
tracks, and delegate this to demux_lavf.c, but maybe that's too redical
too.)
I wish I hadn't notice this...
2013-11-26 22:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return timestamp;
|
2012-08-18 09:13:58 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static void handle_realvideo(demuxer_t *demuxer, mkv_track_t *track,
|
2013-04-12 15:25:51 +00:00
|
|
|
bstr data, bool keyframe)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
demux_packet_t *dp;
|
demux_mkv: fix realvideo timestamp handling
This was broken by the recent commits. Apparently realvideo timestamps
are severely mangled, and Matroska _of course_ doesn't have the sane,
umangled timestamps, but something unusable. The existing unmangling
code in demux_mkv.c didn't output proper timestamps either. Instead,
it was something weird that triggered sorting. Without sorting (it was
disabled by default recently), you'd get decreasing PTS warnings
In order to fix this, steal some code from libavcodec. Basically copy
the contents of rv34_parser.c (with some changes), which makes
everything magically work. (Maybe it would be better to use the
libavcodec parser API, but I don't want to do that just for this. An
alternative idea would be refusing to read files that have realvideo
tracks, and delegate this to demux_lavf.c, but maybe that's too redical
too.)
I wish I hadn't notice this...
2013-11-26 22:41:01 +00:00
|
|
|
int64_t timestamp = mkv_d->last_pts * 1000;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-14 21:19:29 +00:00
|
|
|
dp = new_demux_packet_from(data.start, data.len);
|
2014-09-16 16:11:00 +00:00
|
|
|
if (!dp)
|
|
|
|
return;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
|
|
|
if (mkv_d->v_skip_to_keyframe) {
|
|
|
|
dp->pts = mkv_d->last_pts;
|
|
|
|
track->rv_kf_base = 0;
|
demux_mkv: fix realvideo timestamp handling
This was broken by the recent commits. Apparently realvideo timestamps
are severely mangled, and Matroska _of course_ doesn't have the sane,
umangled timestamps, but something unusable. The existing unmangling
code in demux_mkv.c didn't output proper timestamps either. Instead,
it was something weird that triggered sorting. Without sorting (it was
disabled by default recently), you'd get decreasing PTS warnings
In order to fix this, steal some code from libavcodec. Basically copy
the contents of rv34_parser.c (with some changes), which makes
everything magically work. (Maybe it would be better to use the
libavcodec parser API, but I don't want to do that just for this. An
alternative idea would be refusing to read files that have realvideo
tracks, and delegate this to demux_lavf.c, but maybe that's too redical
too.)
I wish I hadn't notice this...
2013-11-26 22:41:01 +00:00
|
|
|
track->rv_kf_pts = 0;
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2009-12-29 19:06:21 +00:00
|
|
|
dp->pts =
|
demux_mkv: fix realvideo timestamp handling
This was broken by the recent commits. Apparently realvideo timestamps
are severely mangled, and Matroska _of course_ doesn't have the sane,
umangled timestamps, but something unusable. The existing unmangling
code in demux_mkv.c didn't output proper timestamps either. Instead,
it was something weird that triggered sorting. Without sorting (it was
disabled by default recently), you'd get decreasing PTS warnings
In order to fix this, steal some code from libavcodec. Basically copy
the contents of rv34_parser.c (with some changes), which makes
everything magically work. (Maybe it would be better to use the
libavcodec parser API, but I don't want to do that just for this. An
alternative idea would be refusing to read files that have realvideo
tracks, and delegate this to demux_lavf.c, but maybe that's too redical
too.)
I wish I hadn't notice this...
2013-11-26 22:41:01 +00:00
|
|
|
real_fix_timestamp(dp->buffer, dp->len, timestamp,
|
2014-09-24 22:59:15 +00:00
|
|
|
track->stream->format,
|
demux_mkv: fix realvideo timestamp handling
This was broken by the recent commits. Apparently realvideo timestamps
are severely mangled, and Matroska _of course_ doesn't have the sane,
umangled timestamps, but something unusable. The existing unmangling
code in demux_mkv.c didn't output proper timestamps either. Instead,
it was something weird that triggered sorting. Without sorting (it was
disabled by default recently), you'd get decreasing PTS warnings
In order to fix this, steal some code from libavcodec. Basically copy
the contents of rv34_parser.c (with some changes), which makes
everything magically work. (Maybe it would be better to use the
libavcodec parser API, but I don't want to do that just for this. An
alternative idea would be refusing to read files that have realvideo
tracks, and delegate this to demux_lavf.c, but maybe that's too redical
too.)
I wish I hadn't notice this...
2013-11-26 22:41:01 +00:00
|
|
|
&track->rv_kf_base, &track->rv_kf_pts) * 0.001;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2013-11-16 20:04:28 +00:00
|
|
|
dp->pos = mkv_d->last_filepos;
|
2012-07-24 21:23:27 +00:00
|
|
|
dp->keyframe = keyframe;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-07-05 14:59:44 +00:00
|
|
|
demux_add_packet(track->stream, dp);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static void handle_realaudio(demuxer_t *demuxer, mkv_track_t *track,
|
2013-04-12 15:25:51 +00:00
|
|
|
bstr data, bool keyframe)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
2014-11-21 04:10:28 +00:00
|
|
|
uint32_t sps = track->sub_packet_size;
|
|
|
|
uint32_t sph = track->sub_packet_h;
|
2014-06-29 21:22:07 +00:00
|
|
|
uint32_t cfs = track->coded_framesize; // restricted to [1,0x40000000]
|
2014-11-21 04:10:28 +00:00
|
|
|
uint32_t w = track->audiopk_size;
|
|
|
|
uint32_t spc = track->sub_packet_cnt;
|
2013-04-12 15:25:51 +00:00
|
|
|
uint8_t *buffer = data.start;
|
|
|
|
uint32_t size = data.len;
|
2009-12-29 19:06:21 +00:00
|
|
|
demux_packet_t *dp;
|
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
// track->audio_buf allocation size
|
|
|
|
size_t audiobuf_size = sph * w;
|
|
|
|
|
2013-11-02 14:10:28 +00:00
|
|
|
if ((track->a_formattag == MP_FOURCC('2', '8', '_', '8'))
|
|
|
|
|| (track->a_formattag == MP_FOURCC('c', 'o', 'o', 'k'))
|
|
|
|
|| (track->a_formattag == MP_FOURCC('a', 't', 'r', 'c'))
|
2014-06-29 21:22:07 +00:00
|
|
|
|| (track->a_formattag == MP_FOURCC('s', 'i', 'p', 'r')))
|
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
switch (track->a_formattag) {
|
2013-11-02 14:10:28 +00:00
|
|
|
case MP_FOURCC('2', '8', '_', '8'):
|
2014-06-29 21:22:07 +00:00
|
|
|
for (int x = 0; x < sph / 2; x++) {
|
|
|
|
uint64_t dst_offset = x * 2 * w + spc * (uint64_t)cfs;
|
|
|
|
if (dst_offset + cfs > audiobuf_size)
|
|
|
|
goto error;
|
|
|
|
uint64_t src_offset = x * (uint64_t)cfs;
|
|
|
|
if (src_offset + cfs > size)
|
|
|
|
goto error;
|
|
|
|
memcpy(track->audio_buf + dst_offset, buffer + src_offset, cfs);
|
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2013-11-02 14:10:28 +00:00
|
|
|
case MP_FOURCC('c', 'o', 'o', 'k'):
|
|
|
|
case MP_FOURCC('a', 't', 'r', 'c'):
|
2014-06-29 21:22:07 +00:00
|
|
|
for (int x = 0; x < w / sps; x++) {
|
|
|
|
uint32_t dst_offset = sps * (sph * x + ((sph + 1) / 2) * (spc & 1)
|
|
|
|
+ (spc >> 1));
|
|
|
|
if (dst_offset + sps > audiobuf_size)
|
|
|
|
goto error;
|
|
|
|
uint32_t src_offset = sps * x;
|
|
|
|
if (src_offset + sps > size)
|
|
|
|
goto error;
|
|
|
|
memcpy(track->audio_buf + dst_offset, buffer + src_offset, sps);
|
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
break;
|
2013-11-02 14:10:28 +00:00
|
|
|
case MP_FOURCC('s', 'i', 'p', 'r'):
|
2014-06-29 21:22:07 +00:00
|
|
|
if (spc * w + w > audiobuf_size || w > size)
|
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
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++) {
|
2014-06-29 21:28:47 +00:00
|
|
|
unsigned int i = bs * sipr_swaps[n][0]; // 77 max
|
|
|
|
unsigned int o = bs * sipr_swaps[n][1]; // 95 max
|
2014-06-29 21:22:07 +00:00
|
|
|
// swap nibbles of block 'i' with 'o'
|
|
|
|
for (int j = 0; j < bs; j++) {
|
|
|
|
if (i / 2 >= audiobuf_size || o / 2 >= audiobuf_size)
|
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
int x = (i & 1) ?
|
2014-06-29 21:28:47 +00:00
|
|
|
(track->audio_buf[i / 2] >> 4) :
|
|
|
|
(track->audio_buf[i / 2] & 0x0F);
|
2009-12-29 19:06:21 +00:00
|
|
|
int y = (o & 1) ?
|
2014-06-29 21:28:47 +00:00
|
|
|
(track->audio_buf[o / 2] >> 4) :
|
|
|
|
(track->audio_buf[o / 2] & 0x0F);
|
2009-12-29 19:06:21 +00:00
|
|
|
if (o & 1)
|
2014-06-29 21:28:47 +00:00
|
|
|
track->audio_buf[o / 2] =
|
|
|
|
(track->audio_buf[o / 2] & 0x0F) | (x << 4);
|
2009-12-29 19:06:21 +00:00
|
|
|
else
|
2014-06-29 21:28:47 +00:00
|
|
|
track->audio_buf[o / 2] =
|
|
|
|
(track->audio_buf[o / 2] & 0xF0) | x;
|
2009-12-29 19:06:21 +00:00
|
|
|
if (i & 1)
|
2014-06-29 21:28:47 +00:00
|
|
|
track->audio_buf[i / 2] =
|
|
|
|
(track->audio_buf[i / 2] & 0x0F) | (y << 4);
|
2009-12-29 19:06:21 +00:00
|
|
|
else
|
2014-06-29 21:28:47 +00:00
|
|
|
track->audio_buf[i / 2] =
|
|
|
|
(track->audio_buf[i / 2] & 0xF0) | y;
|
2009-12-29 19:06:21 +00:00
|
|
|
++i;
|
|
|
|
++o;
|
2006-04-03 19:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
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)
|
2013-11-16 20:04:28 +00:00
|
|
|
track->audio_filepos = mkv_d->last_filepos;
|
2009-12-29 19:06:21 +00:00
|
|
|
if (++(track->sub_packet_cnt) == sph) {
|
|
|
|
track->sub_packet_cnt = 0;
|
2014-06-29 21:22:07 +00:00
|
|
|
// apk_usize has same range as coded_framesize in worst case
|
2014-09-24 23:56:51 +00:00
|
|
|
uint32_t apk_usize = track->stream->audio->block_align;
|
2014-06-29 21:22:07 +00:00
|
|
|
if (apk_usize > audiobuf_size)
|
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
// Release all the audio packets
|
2013-07-22 22:45:23 +00:00
|
|
|
for (int x = 0; x < sph * w / apk_usize; x++) {
|
2013-04-14 21:19:29 +00:00
|
|
|
dp = new_demux_packet_from(track->audio_buf + x * apk_usize,
|
|
|
|
apk_usize);
|
2014-09-16 16:11:00 +00:00
|
|
|
if (!dp)
|
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
/* Put timestamp only on packets that correspond to original
|
|
|
|
* audio packets in file */
|
2013-07-20 00:16:46 +00:00
|
|
|
dp->pts = (x * apk_usize % w) ? MP_NOPTS_VALUE :
|
2009-12-29 19:06:21 +00:00
|
|
|
track->audio_timestamp[x * apk_usize / w];
|
|
|
|
dp->pos = track->audio_filepos; // all equal
|
2012-07-24 21:23:27 +00:00
|
|
|
dp->keyframe = !x; // Mark first packet as keyframe
|
2014-07-05 14:59:44 +00:00
|
|
|
demux_add_packet(track->stream, dp);
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
2006-04-03 19:41:17 +00:00
|
|
|
}
|
2013-04-24 19:33:33 +00:00
|
|
|
} else { // Not a codec that requires reordering
|
2013-04-14 21:19:29 +00:00
|
|
|
dp = new_demux_packet_from(buffer, size);
|
2014-09-16 16:11:00 +00:00
|
|
|
if (!dp)
|
|
|
|
goto error;
|
2009-12-29 19:06:21 +00:00
|
|
|
if (track->ra_pts == mkv_d->last_pts && !mkv_d->a_skip_to_keyframe)
|
2013-07-20 00:16:46 +00:00
|
|
|
dp->pts = MP_NOPTS_VALUE;
|
2009-12-29 19:06:21 +00:00
|
|
|
else
|
|
|
|
dp->pts = mkv_d->last_pts;
|
|
|
|
track->ra_pts = mkv_d->last_pts;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-11-16 20:04:28 +00:00
|
|
|
dp->pos = mkv_d->last_filepos;
|
2012-07-24 21:23:27 +00:00
|
|
|
dp->keyframe = keyframe;
|
2014-07-05 14:59:44 +00:00
|
|
|
demux_add_packet(track->stream, dp);
|
2009-12-29 19:06:21 +00:00
|
|
|
}
|
2014-06-29 21:22:07 +00:00
|
|
|
return;
|
|
|
|
error:
|
2014-09-16 16:11:00 +00:00
|
|
|
MP_ERR(demuxer, "RealAudio packet extraction or decryption error.\n");
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 19:00:24 +00:00
|
|
|
static void mkv_seek_reset(demuxer_t *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = demuxer->priv;
|
|
|
|
|
|
|
|
for (int i = 0; i < mkv_d->num_tracks; i++) {
|
|
|
|
mkv_track_t *track = mkv_d->tracks[i];
|
|
|
|
if (track->av_parser)
|
|
|
|
av_parser_close(track->av_parser);
|
|
|
|
track->av_parser = NULL;
|
|
|
|
if (track->av_parser_codec) {
|
|
|
|
avcodec_close(track->av_parser_codec);
|
|
|
|
av_free(track->av_parser_codec);
|
|
|
|
}
|
|
|
|
track->av_parser_codec = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 15:40:55 +00:00
|
|
|
// Copied from libavformat/matroskadec.c (FFmpeg 310f9dd / 2013-05-30)
|
|
|
|
// Originally added with Libav commit 9b6f47c
|
|
|
|
// License: LGPL v2.1 or later
|
|
|
|
// Author header: The FFmpeg Project (this function still came from Libav)
|
|
|
|
// Modified to use talloc, removed ffmpeg/libav specific error codes.
|
|
|
|
static int libav_parse_wavpack(mkv_track_t *track, uint8_t *src,
|
|
|
|
uint8_t **pdst, int *size)
|
|
|
|
{
|
|
|
|
uint8_t *dst = NULL;
|
|
|
|
int dstlen = 0;
|
|
|
|
int srclen = *size;
|
|
|
|
uint32_t samples;
|
|
|
|
uint16_t ver;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
if (srclen < 12 || track->private_size < 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ver = AV_RL16(track->private_data);
|
|
|
|
|
|
|
|
samples = AV_RL32(src);
|
|
|
|
src += 4;
|
|
|
|
srclen -= 4;
|
|
|
|
|
|
|
|
while (srclen >= 8) {
|
|
|
|
int multiblock;
|
|
|
|
uint32_t blocksize;
|
|
|
|
uint8_t *tmp;
|
|
|
|
|
|
|
|
uint32_t flags = AV_RL32(src);
|
|
|
|
uint32_t crc = AV_RL32(src + 4);
|
|
|
|
src += 8;
|
|
|
|
srclen -= 8;
|
|
|
|
|
|
|
|
multiblock = (flags & 0x1800) != 0x1800;
|
|
|
|
if (multiblock) {
|
|
|
|
if (srclen < 4)
|
|
|
|
goto fail;
|
|
|
|
blocksize = AV_RL32(src);
|
|
|
|
src += 4;
|
|
|
|
srclen -= 4;
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2013-05-30 15:40:55 +00:00
|
|
|
blocksize = srclen;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2013-05-30 15:40:55 +00:00
|
|
|
|
|
|
|
if (blocksize > srclen)
|
|
|
|
goto fail;
|
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
if (dstlen > 0x10000000 || blocksize > 0x10000000)
|
|
|
|
goto fail;
|
|
|
|
|
2013-12-27 19:00:24 +00:00
|
|
|
tmp = talloc_realloc(track->parser_tmp, dst, uint8_t,
|
|
|
|
dstlen + blocksize + 32);
|
2013-05-30 15:40:55 +00:00
|
|
|
if (!tmp)
|
|
|
|
goto fail;
|
|
|
|
dst = tmp;
|
|
|
|
dstlen += blocksize + 32;
|
|
|
|
|
|
|
|
AV_WL32(dst + offset, MKTAG('w', 'v', 'p', 'k')); // tag
|
|
|
|
AV_WL32(dst + offset + 4, blocksize + 24); // blocksize - 8
|
|
|
|
AV_WL16(dst + offset + 8, ver); // version
|
|
|
|
AV_WL16(dst + offset + 10, 0); // track/index_no
|
|
|
|
AV_WL32(dst + offset + 12, 0); // total samples
|
|
|
|
AV_WL32(dst + offset + 16, 0); // block index
|
|
|
|
AV_WL32(dst + offset + 20, samples); // number of samples
|
|
|
|
AV_WL32(dst + offset + 24, flags); // flags
|
|
|
|
AV_WL32(dst + offset + 28, crc); // crc
|
|
|
|
memcpy (dst + offset + 32, src, blocksize); // block data
|
|
|
|
|
|
|
|
src += blocksize;
|
|
|
|
srclen -= blocksize;
|
|
|
|
offset += blocksize + 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pdst = dst;
|
|
|
|
*size = dstlen;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
talloc_free(dst);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-27 19:00:24 +00:00
|
|
|
static bool mkv_parse_packet(mkv_track_t *track, bstr *raw, bstr *out)
|
2013-05-30 15:40:55 +00:00
|
|
|
{
|
2013-11-02 14:10:28 +00:00
|
|
|
if (track->a_formattag == MP_FOURCC('W', 'V', 'P', 'K')) {
|
2013-12-27 19:00:24 +00:00
|
|
|
int size = raw->len;
|
2013-05-30 15:40:55 +00:00
|
|
|
uint8_t *parsed;
|
2013-12-27 19:00:24 +00:00
|
|
|
if (libav_parse_wavpack(track, raw->start, &parsed, &size) >= 0) {
|
|
|
|
out->start = parsed;
|
|
|
|
out->len = size;
|
|
|
|
*raw = (bstr){0};
|
|
|
|
return true;
|
2013-05-30 15:40:55 +00:00
|
|
|
}
|
2013-09-08 21:05:18 +00:00
|
|
|
} else if (track->codec_id && strcmp(track->codec_id, MKV_V_PRORES) == 0) {
|
2013-12-27 19:00:24 +00:00
|
|
|
size_t newlen = raw->len + 8;
|
|
|
|
char *data = talloc_size(track->parser_tmp, newlen);
|
2013-09-08 21:05:18 +00:00
|
|
|
AV_WB32(data + 0, newlen);
|
|
|
|
AV_WB32(data + 4, MKBETAG('i', 'c', 'p', 'f'));
|
2013-12-27 19:00:24 +00:00
|
|
|
memcpy(data + 8, raw->start, raw->len);
|
|
|
|
out->start = data;
|
|
|
|
out->len = newlen;
|
|
|
|
*raw = (bstr){0};
|
|
|
|
return true;
|
|
|
|
} else if (track->parse) {
|
|
|
|
if (!track->av_parser) {
|
|
|
|
int id = mp_codec_to_av_codec_id(track->stream->codec);
|
|
|
|
const AVCodec *codec = avcodec_find_decoder(id);
|
|
|
|
track->av_parser = av_parser_init(id);
|
|
|
|
if (codec)
|
|
|
|
track->av_parser_codec = avcodec_alloc_context3(codec);
|
|
|
|
}
|
|
|
|
if (track->av_parser && track->av_parser_codec) {
|
|
|
|
while (raw->len) {
|
|
|
|
uint8_t *data;
|
|
|
|
int size;
|
|
|
|
int len = av_parser_parse2(track->av_parser, track->av_parser_codec,
|
|
|
|
&data, &size, raw->start, raw->len,
|
|
|
|
AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
|
2014-06-29 21:22:07 +00:00
|
|
|
if (len < 0 || len > 0x10000000)
|
2013-12-27 19:00:24 +00:00
|
|
|
return false;
|
|
|
|
*raw = bstr_cut(*raw, len);
|
|
|
|
if (size) {
|
|
|
|
out->start = data;
|
|
|
|
out->len = size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-30 15:40:55 +00:00
|
|
|
}
|
2013-12-27 19:00:24 +00:00
|
|
|
// No parsing
|
|
|
|
*out = *raw;
|
|
|
|
*raw = (bstr){0};
|
|
|
|
return true;
|
2013-05-30 15:40:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 19:42:46 +00:00
|
|
|
struct block_info {
|
2014-11-03 19:00:34 +00:00
|
|
|
uint64_t duration, discardpadding;
|
2013-04-11 19:42:46 +00:00
|
|
|
bool simple, keyframe;
|
2013-04-11 23:26:02 +00:00
|
|
|
uint64_t timecode;
|
|
|
|
mkv_track_t *track;
|
2013-04-12 15:25:51 +00:00
|
|
|
bstr data;
|
2013-04-11 23:26:02 +00:00
|
|
|
void *alloc;
|
2013-11-16 20:04:28 +00:00
|
|
|
int64_t filepos;
|
2013-04-11 19:42:46 +00:00
|
|
|
};
|
|
|
|
|
2013-04-11 22:43:34 +00:00
|
|
|
static void free_block(struct block_info *block)
|
2013-04-11 19:42:46 +00:00
|
|
|
{
|
2013-04-11 23:26:02 +00:00
|
|
|
free(block->alloc);
|
2013-04-12 15:25:51 +00:00
|
|
|
block->alloc = NULL;
|
|
|
|
block->data = (bstr){0};
|
2013-04-11 19:42:46 +00:00
|
|
|
}
|
|
|
|
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
static void index_block(demuxer_t *demuxer, struct block_info *block)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
if (block->keyframe) {
|
|
|
|
add_block_position(demuxer, block->track, mkv_d->cluster_start,
|
2014-11-05 19:44:21 +00:00
|
|
|
block->timecode / mkv_d->tc_scale,
|
|
|
|
block->duration / mkv_d->tc_scale);
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 16:38:08 +00:00
|
|
|
static int read_block(demuxer_t *demuxer, int64_t end, struct block_info *block)
|
2013-04-11 19:42:46 +00:00
|
|
|
{
|
2013-04-11 23:26:02 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
2013-04-11 19:42:46 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
2013-04-24 14:19:18 +00:00
|
|
|
uint64_t num;
|
2013-04-11 23:26:02 +00:00
|
|
|
int16_t time;
|
2013-04-12 15:25:51 +00:00
|
|
|
uint64_t length;
|
2013-04-11 23:26:02 +00:00
|
|
|
int res = -1;
|
2013-04-11 19:42:46 +00:00
|
|
|
|
2013-04-11 22:43:34 +00:00
|
|
|
free_block(block);
|
2014-01-14 16:38:51 +00:00
|
|
|
length = ebml_read_length(s);
|
2014-01-14 16:38:08 +00:00
|
|
|
if (length > 500000000 || stream_tell(s) + length > (uint64_t)end)
|
2013-04-11 23:26:02 +00:00
|
|
|
goto exit;
|
2013-04-12 15:25:51 +00:00
|
|
|
block->alloc = malloc(length + AV_LZO_INPUT_PADDING);
|
2013-04-12 15:40:45 +00:00
|
|
|
if (!block->alloc)
|
|
|
|
goto exit;
|
2013-04-12 15:25:51 +00:00
|
|
|
block->data = (bstr){block->alloc, length};
|
2013-11-16 20:04:28 +00:00
|
|
|
block->filepos = stream_tell(s);
|
2013-04-12 15:25:51 +00:00
|
|
|
if (stream_read(s, block->data.start, block->data.len) != block->data.len)
|
2013-04-11 23:26:02 +00:00
|
|
|
goto exit;
|
2013-04-11 22:43:34 +00:00
|
|
|
|
2013-04-11 23:26:02 +00:00
|
|
|
// Parse header of the Block element
|
|
|
|
/* first byte(s): track num */
|
2013-04-12 15:25:51 +00:00
|
|
|
num = ebml_read_vlen_uint(&block->data);
|
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
goto exit;
|
2013-04-11 23:26:02 +00:00
|
|
|
/* time (relative to cluster time) */
|
2013-04-12 15:25:51 +00:00
|
|
|
if (block->data.len < 3)
|
|
|
|
goto exit;
|
|
|
|
time = block->data.start[0] << 8 | block->data.start[1];
|
|
|
|
block->data.start += 2;
|
|
|
|
block->data.len -= 2;
|
2013-04-11 23:26:02 +00:00
|
|
|
if (block->simple)
|
2013-04-12 15:25:51 +00:00
|
|
|
block->keyframe = block->data.start[0] & 0x80;
|
2013-04-11 23:26:02 +00:00
|
|
|
block->timecode = time * mkv_d->tc_scale + mkv_d->cluster_tc;
|
|
|
|
for (int i = 0; i < mkv_d->num_tracks; i++) {
|
|
|
|
if (mkv_d->tracks[i]->tnum == num) {
|
|
|
|
block->track = mkv_d->tracks[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!block->track) {
|
|
|
|
res = 0;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = 1;
|
|
|
|
exit:
|
|
|
|
if (res <= 0)
|
|
|
|
free_block(block);
|
|
|
|
return res;
|
2013-04-11 19:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_block(demuxer_t *demuxer, struct block_info *block_info)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
2013-04-14 04:04:58 +00:00
|
|
|
int laces;
|
2009-12-29 19:06:21 +00:00
|
|
|
double current_pts;
|
2013-04-12 15:25:51 +00:00
|
|
|
bstr data = block_info->data;
|
2013-04-11 19:42:46 +00:00
|
|
|
bool keyframe = block_info->keyframe;
|
|
|
|
uint64_t block_duration = block_info->duration;
|
2013-04-11 23:26:02 +00:00
|
|
|
uint64_t tc = block_info->timecode;
|
|
|
|
mkv_track_t *track = block_info->track;
|
2013-04-14 17:23:18 +00:00
|
|
|
struct sh_stream *stream = track->stream;
|
2013-04-12 14:07:45 +00:00
|
|
|
uint32_t lace_size[MAX_NUM_LACES];
|
2013-04-13 19:19:25 +00:00
|
|
|
bool use_this_block = tc >= mkv_d->skip_to_timecode;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-07-06 17:02:21 +00:00
|
|
|
if (!demux_stream_is_selected(stream))
|
2013-04-14 17:23:18 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-12-21 19:24:20 +00:00
|
|
|
if (demux_mkv_read_block_lacing(&data, &laces, lace_size)) {
|
|
|
|
MP_ERR(demuxer, "Bad input [lacing]\n");
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2013-12-21 19:24:20 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-11-03 19:00:34 +00:00
|
|
|
current_pts = tc / 1e9 - track->codec_delay;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
if (track->type == MATROSKA_TRACK_AUDIO) {
|
2012-07-24 21:23:27 +00:00
|
|
|
if (mkv_d->a_skip_to_keyframe)
|
2014-02-09 18:00:06 +00:00
|
|
|
use_this_block &= keyframe;
|
2013-04-14 17:23:18 +00:00
|
|
|
} else if (track->type == MATROSKA_TRACK_SUBTITLE) {
|
2013-09-15 22:20:19 +00:00
|
|
|
if (!use_this_block && mkv_d->subtitle_preroll)
|
2013-09-08 03:09:16 +00:00
|
|
|
use_this_block = 1;
|
2013-09-15 22:20:19 +00:00
|
|
|
if (use_this_block) {
|
|
|
|
if (mkv_d->subtitle_preroll) {
|
|
|
|
mkv_d->subtitle_preroll--;
|
|
|
|
} else {
|
|
|
|
// This could overflow the demuxer queue.
|
|
|
|
if (mkv_d->a_skip_to_keyframe || mkv_d->v_skip_to_keyframe)
|
|
|
|
use_this_block = 0;
|
|
|
|
}
|
2013-09-08 03:09:16 +00:00
|
|
|
}
|
2013-04-03 23:43:14 +00:00
|
|
|
if (use_this_block) {
|
|
|
|
if (laces > 1) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "Subtitles use Matroska "
|
2013-04-24 19:33:33 +00:00
|
|
|
"lacing. This is abnormal and not supported.\n");
|
2013-04-03 23:43:14 +00:00
|
|
|
use_this_block = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-04-14 17:23:18 +00:00
|
|
|
} else if (track->type == MATROSKA_TRACK_VIDEO) {
|
2012-07-24 21:23:27 +00:00
|
|
|
if (mkv_d->v_skip_to_keyframe)
|
2013-04-13 19:19:25 +00:00
|
|
|
use_this_block &= keyframe;
|
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
if (use_this_block) {
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_d->last_pts = current_pts;
|
2013-11-16 20:04:28 +00:00
|
|
|
mkv_d->last_filepos = block_info->filepos;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-12-27 19:00:24 +00:00
|
|
|
int p = 0;
|
2013-04-14 04:04:58 +00:00
|
|
|
for (int i = 0; i < laces; i++) {
|
2013-04-12 15:25:51 +00:00
|
|
|
bstr block = bstr_splice(data, 0, lace_size[i]);
|
2013-04-14 17:23:18 +00:00
|
|
|
if (stream->type == STREAM_VIDEO && track->realmedia)
|
2013-04-12 15:25:51 +00:00
|
|
|
handle_realvideo(demuxer, track, block, keyframe);
|
2013-04-14 17:23:18 +00:00
|
|
|
else if (stream->type == STREAM_AUDIO && track->realmedia)
|
2013-04-12 15:25:51 +00:00
|
|
|
handle_realaudio(demuxer, track, block, keyframe);
|
2009-12-29 19:06:21 +00:00
|
|
|
else {
|
2013-12-27 19:00:24 +00:00
|
|
|
bstr raw = demux_mkv_decode(demuxer->log, track, block, 1);
|
|
|
|
bstr buffer;
|
|
|
|
while (raw.start && mkv_parse_packet(track, &raw, &buffer)) {
|
2013-04-14 21:19:29 +00:00
|
|
|
demux_packet_t *dp =
|
|
|
|
new_demux_packet_from(buffer.start, buffer.len);
|
2014-09-16 16:11:00 +00:00
|
|
|
if (!dp)
|
|
|
|
break;
|
2012-07-24 21:23:27 +00:00
|
|
|
dp->keyframe = keyframe;
|
2014-10-29 21:08:50 +00:00
|
|
|
dp->pos = mkv_d->last_filepos;
|
2009-12-29 19:06:21 +00:00
|
|
|
/* If default_duration is 0, assume no pts value is known
|
|
|
|
* for packets after the first one (rather than all pts
|
2014-09-25 23:25:48 +00:00
|
|
|
* values being the same). Also, don't use it for extra
|
|
|
|
* packets resulting from parsing. */
|
|
|
|
if (p == 0 || (p == i && track->default_duration))
|
2013-12-27 19:00:24 +00:00
|
|
|
dp->pts = mkv_d->last_pts + p * track->default_duration;
|
2013-11-25 22:14:54 +00:00
|
|
|
if (track->ms_compat)
|
|
|
|
MPSWAP(double, dp->pts, dp->dts);
|
2014-09-25 23:25:48 +00:00
|
|
|
if (p == 0)
|
|
|
|
dp->duration = block_duration / 1e9;
|
2014-11-03 19:00:34 +00:00
|
|
|
if (stream->type == STREAM_AUDIO) {
|
|
|
|
unsigned int srate = track->a_sfreq;
|
|
|
|
demux_packet_set_padding(dp,
|
|
|
|
mkv_d->a_skip_preroll ? track->codec_delay * srate : 0,
|
|
|
|
block_info->discardpadding / 1e9 * srate);
|
|
|
|
mkv_d->a_skip_preroll = 0;
|
|
|
|
}
|
2014-07-05 14:59:44 +00:00
|
|
|
demux_add_packet(stream, dp);
|
2014-09-25 23:25:48 +00:00
|
|
|
p++;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-12-27 19:00:24 +00:00
|
|
|
talloc_free_children(track->parser_tmp);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-04-12 15:25:51 +00:00
|
|
|
data = bstr_cut(data, lace_size[i]);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 17:23:18 +00:00
|
|
|
if (stream->type == STREAM_VIDEO) {
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_d->v_skip_to_keyframe = 0;
|
|
|
|
mkv_d->skip_to_timecode = 0;
|
2013-09-08 03:09:16 +00:00
|
|
|
mkv_d->subtitle_preroll = 0;
|
2013-10-19 21:10:03 +00:00
|
|
|
} else if (stream->type == STREAM_AUDIO) {
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_d->a_skip_to_keyframe = 0;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
return 1;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
return 0;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 21:33:42 +00:00
|
|
|
static int read_block_group(demuxer_t *demuxer, int64_t end,
|
|
|
|
struct block_info *block)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
2013-04-11 22:43:34 +00:00
|
|
|
*block = (struct block_info){ .keyframe = true };
|
2013-04-11 21:33:42 +00:00
|
|
|
|
|
|
|
while (stream_tell(s) < end) {
|
2014-01-14 16:38:51 +00:00
|
|
|
switch (ebml_read_id(s)) {
|
2013-04-11 21:33:42 +00:00
|
|
|
case MATROSKA_ID_BLOCKDURATION:
|
2014-01-14 16:38:51 +00:00
|
|
|
block->duration = ebml_read_uint(s);
|
2013-04-11 21:33:42 +00:00
|
|
|
if (block->duration == EBML_UINT_INVALID)
|
2013-04-11 22:43:34 +00:00
|
|
|
goto error;
|
2013-04-11 21:33:42 +00:00
|
|
|
block->duration *= mkv_d->tc_scale;
|
|
|
|
break;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2014-11-03 19:00:34 +00:00
|
|
|
case MATROSKA_ID_DISCARDPADDING:
|
|
|
|
block->discardpadding = ebml_read_uint(s);
|
|
|
|
if (block->discardpadding == EBML_UINT_INVALID)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
2013-04-11 21:33:42 +00:00
|
|
|
case MATROSKA_ID_BLOCK:
|
2014-01-14 16:38:08 +00:00
|
|
|
if (read_block(demuxer, end, block) < 0)
|
2013-04-11 22:43:34 +00:00
|
|
|
goto error;
|
2013-04-11 21:33:42 +00:00
|
|
|
break;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-11 21:33:42 +00:00
|
|
|
case MATROSKA_ID_REFERENCEBLOCK:;
|
2014-01-14 16:38:51 +00:00
|
|
|
int64_t num = ebml_read_int(s);
|
2013-04-11 21:33:42 +00:00
|
|
|
if (num == EBML_INT_INVALID)
|
2013-04-11 22:43:34 +00:00
|
|
|
goto error;
|
2013-04-11 21:33:42 +00:00
|
|
|
if (num)
|
|
|
|
block->keyframe = false;
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2014-01-14 16:38:21 +00:00
|
|
|
case MATROSKA_ID_CLUSTER:
|
2013-04-11 21:33:42 +00:00
|
|
|
case EBML_ID_INVALID:
|
2013-04-11 22:43:34 +00:00
|
|
|
goto error;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-11 21:33:42 +00:00
|
|
|
default:
|
2014-01-14 16:38:21 +00:00
|
|
|
if (ebml_read_skip(demuxer->log, end, s) != 0)
|
2013-04-11 22:43:34 +00:00
|
|
|
goto error;
|
2013-04-11 21:33:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-12 15:25:51 +00:00
|
|
|
return block->data.start ? 1 : 0;
|
2013-04-11 22:43:34 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
free_block(block);
|
|
|
|
return -1;
|
2013-04-11 21:33:42 +00:00
|
|
|
}
|
2004-02-09 19:21:08 +00:00
|
|
|
|
2013-04-11 22:43:34 +00:00
|
|
|
static int read_next_block(demuxer_t *demuxer, struct block_info *block)
|
2013-04-11 21:33:42 +00:00
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
|
|
|
stream_t *s = demuxer->stream;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-11 21:33:42 +00:00
|
|
|
while (1) {
|
|
|
|
while (stream_tell(s) < mkv_d->cluster_end) {
|
2013-04-12 12:57:06 +00:00
|
|
|
int64_t start_filepos = stream_tell(s);
|
2014-01-14 16:38:51 +00:00
|
|
|
switch (ebml_read_id(s)) {
|
2013-04-12 12:57:06 +00:00
|
|
|
case MATROSKA_ID_TIMECODE: {
|
2014-01-14 16:38:51 +00:00
|
|
|
uint64_t num = ebml_read_uint(s);
|
2013-04-12 12:57:06 +00:00
|
|
|
if (num == EBML_UINT_INVALID)
|
|
|
|
goto find_next_cluster;
|
|
|
|
mkv_d->cluster_tc = num * mkv_d->tc_scale;
|
|
|
|
break;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-12 12:57:06 +00:00
|
|
|
case MATROSKA_ID_BLOCKGROUP: {
|
2014-01-14 16:38:51 +00:00
|
|
|
int64_t end = ebml_read_length(s);
|
2013-04-12 12:57:06 +00:00
|
|
|
end += stream_tell(s);
|
2014-01-14 16:38:08 +00:00
|
|
|
if (end > mkv_d->cluster_end)
|
|
|
|
goto find_next_cluster;
|
2013-04-12 12:57:06 +00:00
|
|
|
int res = read_block_group(demuxer, end, block);
|
|
|
|
if (res < 0)
|
|
|
|
goto find_next_cluster;
|
|
|
|
if (res > 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-04-12 12:57:06 +00:00
|
|
|
case MATROSKA_ID_SIMPLEBLOCK: {
|
|
|
|
*block = (struct block_info){ .simple = true };
|
2014-01-14 16:38:08 +00:00
|
|
|
int res = read_block(demuxer, mkv_d->cluster_end, block);
|
2013-04-12 12:57:06 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto find_next_cluster;
|
|
|
|
if (res > 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-12 12:57:06 +00:00
|
|
|
case MATROSKA_ID_CLUSTER:
|
|
|
|
mkv_d->cluster_start = start_filepos;
|
|
|
|
goto next_cluster;
|
2013-04-11 21:28:27 +00:00
|
|
|
|
2013-04-12 12:57:06 +00:00
|
|
|
case EBML_ID_INVALID:
|
|
|
|
goto find_next_cluster;
|
2004-02-09 19:21:08 +00:00
|
|
|
|
2013-04-12 12:57:06 +00:00
|
|
|
default: ;
|
2014-01-14 16:38:21 +00:00
|
|
|
if (ebml_read_skip(demuxer->log, mkv_d->cluster_end, s) != 0)
|
2013-04-12 12:57:06 +00:00
|
|
|
goto find_next_cluster;
|
|
|
|
break;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
demux_mkv: improve robustness against broken files
Fixes test7.mkv from the Matroska test file collection, as well as some
real broken files I've found in the wild. (Unfortunately, true recovery
requires resetting the decoders and playback state with a manual seek,
but it's still better than just exiting.)
If there are broken EBML elements, try harder to skip them correctly.
Do this by searching for the next cluster element. The cluster element
intentionally has a long ID, so it's a suitable element for
resynchronizing (mkvmerge does something similar).
We know that data is corrupt if the ID or length fields of an element
are malformed. Additionally, if skipping an unknown element goes past
the end of the file, we assume it's corrupt and undo the seek. Do this
because it often happens that corrupt data is interpreted as correct
EBML elements. Since these elements will have a ridiculous values in
their length fields due to the large value range that is possible
(0-2^56-2), they will go past the end of the file. So instead of
skipping them (which would result in playback termination), try to
find the next cluster instead. (We still skip unknown elements that
are within the file, as this is needed for correct operation. Also, we
first execute the seek, because we don't really know where the file
ends. Doing it this way is better for unseekable streams too, because
it will still work in the non-error case.)
This is done as special case in the packet reading function only. On
the other hand, that's the only part of the file that's read after
initialization is done.
2013-03-27 23:01:17 +00:00
|
|
|
find_next_cluster:
|
2013-04-11 21:33:42 +00:00
|
|
|
mkv_d->cluster_end = 0;
|
demux_mkv: improve robustness against broken files
Fixes test7.mkv from the Matroska test file collection, as well as some
real broken files I've found in the wild. (Unfortunately, true recovery
requires resetting the decoders and playback state with a manual seek,
but it's still better than just exiting.)
If there are broken EBML elements, try harder to skip them correctly.
Do this by searching for the next cluster element. The cluster element
intentionally has a long ID, so it's a suitable element for
resynchronizing (mkvmerge does something similar).
We know that data is corrupt if the ID or length fields of an element
are malformed. Additionally, if skipping an unknown element goes past
the end of the file, we assume it's corrupt and undo the seek. Do this
because it often happens that corrupt data is interpreted as correct
EBML elements. Since these elements will have a ridiculous values in
their length fields due to the large value range that is possible
(0-2^56-2), they will go past the end of the file. So instead of
skipping them (which would result in playback termination), try to
find the next cluster instead. (We still skip unknown elements that
are within the file, as this is needed for correct operation. Also, we
first execute the seek, because we don't really know where the file
ends. Doing it this way is better for unseekable streams too, because
it will still work in the non-error case.)
This is done as special case in the packet reading function only. On
the other hand, that's the only part of the file that's read after
initialization is done.
2013-03-27 23:01:17 +00:00
|
|
|
for (;;) {
|
2013-04-11 18:31:58 +00:00
|
|
|
mkv_d->cluster_start = stream_tell(s);
|
2014-01-14 16:38:51 +00:00
|
|
|
uint32_t id = ebml_read_id(s);
|
demux_mkv: improve robustness against broken files
Fixes test7.mkv from the Matroska test file collection, as well as some
real broken files I've found in the wild. (Unfortunately, true recovery
requires resetting the decoders and playback state with a manual seek,
but it's still better than just exiting.)
If there are broken EBML elements, try harder to skip them correctly.
Do this by searching for the next cluster element. The cluster element
intentionally has a long ID, so it's a suitable element for
resynchronizing (mkvmerge does something similar).
We know that data is corrupt if the ID or length fields of an element
are malformed. Additionally, if skipping an unknown element goes past
the end of the file, we assume it's corrupt and undo the seek. Do this
because it often happens that corrupt data is interpreted as correct
EBML elements. Since these elements will have a ridiculous values in
their length fields due to the large value range that is possible
(0-2^56-2), they will go past the end of the file. So instead of
skipping them (which would result in playback termination), try to
find the next cluster instead. (We still skip unknown elements that
are within the file, as this is needed for correct operation. Also, we
first execute the seek, because we don't really know where the file
ends. Doing it this way is better for unseekable streams too, because
it will still work in the non-error case.)
This is done as special case in the packet reading function only. On
the other hand, that's the only part of the file that's read after
initialization is done.
2013-03-27 23:01:17 +00:00
|
|
|
if (id == MATROSKA_ID_CLUSTER)
|
|
|
|
break;
|
2010-01-25 11:27:35 +00:00
|
|
|
if (s->eof)
|
2013-04-11 22:43:34 +00:00
|
|
|
return -1;
|
2014-01-22 22:37:03 +00:00
|
|
|
if (id == EBML_ID_EBML && stream_tell(s) >= mkv_d->segment_end) {
|
|
|
|
// Appended segment - don't use its clusters, consider this EOF.
|
|
|
|
stream_seek(s, stream_tell(s) - 4);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-14 16:38:21 +00:00
|
|
|
// For the sake of robustness, consider even unknown level 1
|
|
|
|
// elements the same as unknown/broken IDs.
|
|
|
|
if (!ebml_is_mkv_level1_id(id) ||
|
|
|
|
ebml_read_skip(demuxer->log, -1, s) != 0)
|
|
|
|
{
|
|
|
|
ebml_resync_cluster(demuxer->log, s);
|
|
|
|
}
|
2010-01-25 11:27:35 +00:00
|
|
|
}
|
2013-04-11 21:28:27 +00:00
|
|
|
next_cluster:
|
2014-01-14 16:38:51 +00:00
|
|
|
mkv_d->cluster_end = ebml_read_length(s);
|
2013-04-11 21:28:27 +00:00
|
|
|
// mkv files for "streaming" can have this legally
|
|
|
|
if (mkv_d->cluster_end != EBML_UINT_INVALID)
|
|
|
|
mkv_d->cluster_end += stream_tell(s);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
2013-04-11 22:43:34 +00:00
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2013-07-11 17:17:51 +00:00
|
|
|
static int demux_mkv_fill_buffer(demuxer_t *demuxer)
|
2013-04-11 22:43:34 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
int res;
|
|
|
|
struct block_info block;
|
|
|
|
res = read_next_block(demuxer, &block);
|
|
|
|
if (res < 0)
|
|
|
|
return 0;
|
|
|
|
if (res > 0) {
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
index_block(demuxer, &block);
|
2013-04-11 22:43:34 +00:00
|
|
|
res = handle_block(demuxer, &block);
|
|
|
|
free_block(&block);
|
|
|
|
if (res > 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
static mkv_index_t *get_highest_index_entry(struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
|
|
|
assert(!mkv_d->index_complete); // would require separate code
|
|
|
|
|
|
|
|
mkv_index_t *index = NULL;
|
|
|
|
for (int n = 0; n < mkv_d->num_tracks; n++) {
|
|
|
|
int n_index = mkv_d->tracks[n]->last_index_entry;
|
|
|
|
if (n_index >= 0) {
|
|
|
|
mkv_index_t *index2 = &mkv_d->indexes[n_index];
|
|
|
|
if (!index || index2->filepos > index->filepos)
|
|
|
|
index = index2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2013-04-11 15:40:23 +00:00
|
|
|
static int create_index_until(struct demuxer *demuxer, uint64_t timecode)
|
2010-11-08 02:27:09 +00:00
|
|
|
{
|
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
|
|
|
struct stream *s = demuxer->stream;
|
|
|
|
|
2013-05-22 22:26:42 +00:00
|
|
|
read_deferred_cues(demuxer);
|
|
|
|
|
2013-04-11 15:40:23 +00:00
|
|
|
if (mkv_d->index_complete)
|
|
|
|
return 0;
|
|
|
|
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
mkv_index_t *index = get_highest_index_entry(demuxer);
|
|
|
|
|
|
|
|
if (!index || index->timecode * mkv_d->tc_scale < timecode) {
|
|
|
|
if (index)
|
|
|
|
stream_seek(s, index->filepos);
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_VERBOSE(demuxer, "creating index until TC %" PRIu64 "\n", timecode);
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
for (;;) {
|
|
|
|
int res;
|
|
|
|
struct block_info block;
|
|
|
|
res = read_next_block(demuxer, &block);
|
|
|
|
if (res < 0)
|
|
|
|
break;
|
|
|
|
if (res > 0) {
|
|
|
|
index_block(demuxer, &block);
|
|
|
|
free_block(&block);
|
2010-11-08 02:27:09 +00:00
|
|
|
}
|
demux_mkv: fix seeking with index generation
Relative seeks backwards didn't work too well with incomplete files, or
other files that are missing the seek index. The problem was that the
on-the-fly seek index generation simply added cluster positions as seek
entries. While this is perfectly fine, the seek code had no information
about the location of video key frames. For example, a 5 second long
cluster can have only 1 video key frame, which is located 4 seconds into
the cluster. Seeking backwards by one second while still located in the
same cluster would select this cluster as seek target again. Decoding
would resume with the key frame, giving the impression that seeking is
"stuck" at this frame.
Make the generated index aware of key frame and track information, so
that video can always be seeked in an idea way. This also uses the
normal block parsing code for indexing the clusters, instead of the
suspicious looking special code. (This code didn't parse the Matroska
elements correctly, but was fine for files with normal structure. Files
with corrupted clusters or clusters formatted for streaming were not
handled properly.)
Skipping is now quite a bit slower (takes about twice as long as
before), but it removes the special cased skipping code, and it's still
much faster (at least twice as fast) than libavformat. It needs to do
more I/O (no more skipping entire clusters, all data is read), and has
more CPU usage (more data needs to be parsed).
2013-04-12 00:22:23 +00:00
|
|
|
index = get_highest_index_entry(demuxer);
|
|
|
|
if (index && index->timecode * mkv_d->tc_scale >= timecode)
|
2010-11-08 02:43:48 +00:00
|
|
|
break;
|
2010-11-08 02:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-11 15:40:23 +00:00
|
|
|
if (!mkv_d->indexes) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_WARN(demuxer, "no target for seek found\n");
|
2010-11-08 02:27:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-08 03:43:35 +00:00
|
|
|
static struct mkv_index *seek_with_cues(struct demuxer *demuxer, int seek_id,
|
|
|
|
int64_t target_timecode, int flags)
|
|
|
|
{
|
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
|
|
|
struct mkv_index *index = NULL;
|
|
|
|
|
2010-12-20 00:26:10 +00:00
|
|
|
/* Find the entry in the index closest to the target timecode in the
|
|
|
|
* give direction. If there are no such entries - we're trying to seek
|
|
|
|
* backward from a target time before the first entry or forward from a
|
|
|
|
* target time after the last entry - then still seek to the first/last
|
|
|
|
* entry if that's further in the direction wanted than mkv_d->last_pts.
|
|
|
|
*/
|
2011-01-23 22:29:01 +00:00
|
|
|
int64_t min_diff = target_timecode - (int64_t)(mkv_d->last_pts * 1e9 + 0.5);
|
2010-12-20 00:26:10 +00:00
|
|
|
if (flags & SEEK_BACKWARD)
|
|
|
|
min_diff = -min_diff;
|
|
|
|
min_diff = FFMAX(min_diff, 1);
|
2013-04-03 23:43:14 +00:00
|
|
|
|
2014-06-29 21:22:07 +00:00
|
|
|
for (size_t i = 0; i < mkv_d->num_indexes; i++) {
|
2010-11-08 03:43:35 +00:00
|
|
|
if (seek_id < 0 || mkv_d->indexes[i].tnum == seek_id) {
|
2010-11-15 15:48:22 +00:00
|
|
|
int64_t diff =
|
2010-11-08 03:43:35 +00:00
|
|
|
target_timecode -
|
2011-01-23 22:29:01 +00:00
|
|
|
(int64_t) (mkv_d->indexes[i].timecode * mkv_d->tc_scale);
|
2010-12-20 00:26:10 +00:00
|
|
|
if (flags & SEEK_BACKWARD)
|
|
|
|
diff = -diff;
|
|
|
|
if (diff <= 0) {
|
|
|
|
if (min_diff <= 0 && diff <= min_diff)
|
2010-11-08 03:43:35 +00:00
|
|
|
continue;
|
2010-12-20 00:26:10 +00:00
|
|
|
} else if (diff >= min_diff)
|
|
|
|
continue;
|
2010-11-08 03:43:35 +00:00
|
|
|
min_diff = diff;
|
|
|
|
index = mkv_d->indexes + i;
|
|
|
|
}
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2010-11-08 03:43:35 +00:00
|
|
|
|
|
|
|
if (index) { /* We've found an entry. */
|
2013-04-03 23:43:14 +00:00
|
|
|
uint64_t seek_pos = index->filepos;
|
2013-09-15 22:20:19 +00:00
|
|
|
if (flags & SEEK_SUBPREROLL) {
|
2014-11-14 22:52:47 +00:00
|
|
|
// Find the cluster with the highest filepos, that has a timestamp
|
|
|
|
// still lower than min_tc.
|
|
|
|
double secs = demuxer->opts->mkv_subtitle_preroll_secs;
|
|
|
|
uint64_t pre = MPMIN(INT64_MAX, secs * 1e9 / mkv_d->tc_scale);
|
|
|
|
uint64_t min_tc = pre < index->timecode ? index->timecode - pre : 0;
|
2013-04-03 23:43:14 +00:00
|
|
|
uint64_t prev_target = 0;
|
2014-11-14 22:52:47 +00:00
|
|
|
uint64_t prev_tc = 0;
|
2014-06-29 21:22:07 +00:00
|
|
|
for (size_t i = 0; i < mkv_d->num_indexes; i++) {
|
2014-11-11 18:36:03 +00:00
|
|
|
if (seek_id < 0 || mkv_d->indexes[i].tnum == seek_id) {
|
2014-11-14 22:52:47 +00:00
|
|
|
struct mkv_index *cur = &mkv_d->indexes[i];
|
|
|
|
if (cur->timecode <= min_tc && cur->timecode >= prev_tc) {
|
|
|
|
prev_tc = cur->timecode;
|
|
|
|
prev_target = cur->filepos;
|
|
|
|
}
|
2014-11-11 18:36:03 +00:00
|
|
|
}
|
2013-04-03 23:43:14 +00:00
|
|
|
}
|
2014-11-05 19:47:42 +00:00
|
|
|
if (mkv_d->index_has_durations) {
|
2014-11-11 18:36:03 +00:00
|
|
|
// Find the earliest cluster that is not before prev_target,
|
|
|
|
// but contains subtitle packets overlapping with the cluster
|
|
|
|
// at seek_pos.
|
|
|
|
uint64_t target = seek_pos;
|
2014-11-05 19:47:42 +00:00
|
|
|
for (size_t i = 0; i < mkv_d->num_indexes; i++) {
|
|
|
|
struct mkv_index *cur = &mkv_d->indexes[i];
|
2014-11-11 18:36:03 +00:00
|
|
|
if (cur->timecode <= index->timecode &&
|
|
|
|
cur->timecode + cur->duration > index->timecode &&
|
|
|
|
cur->filepos >= prev_target &&
|
|
|
|
cur->filepos < target)
|
|
|
|
{
|
|
|
|
target = cur->filepos;
|
|
|
|
}
|
2014-11-05 19:47:42 +00:00
|
|
|
}
|
2014-11-11 18:36:03 +00:00
|
|
|
prev_target = target;
|
2014-11-05 19:47:42 +00:00
|
|
|
}
|
2013-04-03 23:43:14 +00:00
|
|
|
if (prev_target)
|
|
|
|
seek_pos = prev_target;
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:33:42 +00:00
|
|
|
mkv_d->cluster_end = 0;
|
2013-04-03 23:43:14 +00:00
|
|
|
stream_seek(demuxer->stream, seek_pos);
|
2010-11-08 03:43:35 +00:00
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2014-07-21 17:28:18 +00:00
|
|
|
static void demux_mkv_seek(demuxer_t *demuxer, double rel_seek_secs, int flags)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2010-05-22 07:14:41 +00:00
|
|
|
mkv_demuxer_t *mkv_d = demuxer->priv;
|
2013-05-22 22:26:42 +00:00
|
|
|
int64_t old_pos = stream_tell(demuxer->stream);
|
2010-05-22 07:14:41 +00:00
|
|
|
uint64_t v_tnum = -1;
|
|
|
|
uint64_t a_tnum = -1;
|
2013-04-14 17:23:18 +00:00
|
|
|
bool st_active[STREAM_TYPE_COUNT] = {0};
|
2013-12-27 19:00:24 +00:00
|
|
|
mkv_seek_reset(demuxer);
|
2013-04-14 17:23:18 +00:00
|
|
|
for (int i = 0; i < mkv_d->num_tracks; i++) {
|
|
|
|
mkv_track_t *track = mkv_d->tracks[i];
|
2014-07-06 17:02:21 +00:00
|
|
|
if (demux_stream_is_selected(track->stream)) {
|
2013-04-14 17:23:18 +00:00
|
|
|
st_active[track->stream->type] = true;
|
|
|
|
if (track->type == MATROSKA_TRACK_VIDEO)
|
|
|
|
v_tnum = track->tnum;
|
|
|
|
if (track->type == MATROSKA_TRACK_AUDIO)
|
|
|
|
a_tnum = track->tnum;
|
|
|
|
}
|
|
|
|
}
|
2013-09-15 22:20:19 +00:00
|
|
|
mkv_d->subtitle_preroll = NUM_SUB_PREROLL_PACKETS;
|
|
|
|
if (!st_active[STREAM_SUB] || !st_active[STREAM_VIDEO])
|
|
|
|
flags &= ~SEEK_SUBPREROLL;
|
2014-10-29 21:45:21 +00:00
|
|
|
|
2009-03-31 16:30:14 +00:00
|
|
|
// Adjust the target a little bit to catch cases where the target position
|
|
|
|
// specifies a keyframe with high, but not perfect, precision.
|
2009-07-09 15:23:35 +00:00
|
|
|
rel_seek_secs += flags & SEEK_FORWARD ? -0.005 : 0.005;
|
2009-03-31 16:30:14 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
if (!(flags & SEEK_FACTOR)) { /* time in secs */
|
|
|
|
mkv_index_t *index = NULL;
|
|
|
|
|
|
|
|
if (!(flags & SEEK_ABSOLUTE)) /* relative seek */
|
2010-11-08 03:18:48 +00:00
|
|
|
rel_seek_secs += mkv_d->last_pts;
|
2011-01-23 22:29:01 +00:00
|
|
|
rel_seek_secs = FFMAX(rel_seek_secs, 0);
|
|
|
|
int64_t target_timecode = rel_seek_secs * 1e9 + 0.5;
|
2009-12-29 19:06:21 +00:00
|
|
|
|
2013-04-11 15:40:23 +00:00
|
|
|
if (create_index_until(demuxer, target_timecode) >= 0) {
|
2013-04-14 17:23:18 +00:00
|
|
|
int seek_id = st_active[STREAM_VIDEO] ? v_tnum : a_tnum;
|
2010-11-08 03:43:35 +00:00
|
|
|
index = seek_with_cues(demuxer, seek_id, target_timecode, flags);
|
|
|
|
if (!index)
|
|
|
|
index = seek_with_cues(demuxer, -1, target_timecode, flags);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-22 22:26:42 +00:00
|
|
|
if (!index)
|
|
|
|
stream_seek(demuxer->stream, old_pos);
|
|
|
|
|
2013-10-19 21:10:03 +00:00
|
|
|
if (flags & SEEK_FORWARD) {
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_d->skip_to_timecode = target_timecode;
|
2013-10-19 21:10:03 +00:00
|
|
|
} else {
|
2011-01-23 22:29:01 +00:00
|
|
|
mkv_d->skip_to_timecode = index ? index->timecode * mkv_d->tc_scale
|
|
|
|
: 0;
|
2013-10-19 21:10:03 +00:00
|
|
|
}
|
2014-10-29 21:45:21 +00:00
|
|
|
} else {
|
2009-12-29 19:06:21 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
|
2013-05-22 22:26:42 +00:00
|
|
|
read_deferred_cues(demuxer);
|
|
|
|
|
2014-05-24 12:04:09 +00:00
|
|
|
int64_t size = 0;
|
|
|
|
stream_control(s, STREAM_CTRL_GET_SIZE, &size);
|
|
|
|
|
2014-10-29 21:34:40 +00:00
|
|
|
int64_t target_filepos = size * MPCLAMP(rel_seek_secs, 0, 1);
|
|
|
|
|
|
|
|
mkv_index_t *index = NULL;
|
|
|
|
if (mkv_d->index_complete) {
|
|
|
|
for (size_t i = 0; i < mkv_d->num_indexes; i++) {
|
|
|
|
if (mkv_d->indexes[i].tnum == v_tnum) {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 22:26:42 +00:00
|
|
|
}
|
2004-03-26 19:45:06 +00:00
|
|
|
|
2013-04-11 21:33:42 +00:00
|
|
|
mkv_d->cluster_end = 0;
|
2004-03-26 19:45:06 +00:00
|
|
|
|
2014-10-29 21:34:40 +00:00
|
|
|
if (index) {
|
|
|
|
stream_seek(s, index->filepos);
|
|
|
|
mkv_d->skip_to_timecode = index->timecode * mkv_d->tc_scale;
|
|
|
|
} else {
|
|
|
|
stream_seek(s, target_filepos);
|
|
|
|
if (ebml_resync_cluster(mp_null_log, s) < 0) {
|
|
|
|
// Assume EOF
|
|
|
|
mkv_d->cluster_end = size;
|
|
|
|
}
|
|
|
|
}
|
2004-03-26 19:45:06 +00:00
|
|
|
}
|
2014-11-03 19:25:21 +00:00
|
|
|
|
|
|
|
mkv_d->v_skip_to_keyframe = st_active[STREAM_VIDEO];
|
|
|
|
mkv_d->a_skip_to_keyframe = st_active[STREAM_AUDIO];
|
|
|
|
mkv_d->a_skip_preroll = mkv_d->a_skip_to_keyframe;
|
|
|
|
|
|
|
|
demux_mkv_fill_buffer(demuxer);
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:07:20 +00:00
|
|
|
static void probe_last_timestamp(struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
mkv_demuxer_t *mkv_d = demuxer->priv;
|
|
|
|
int64_t old_pos = stream_tell(demuxer->stream);
|
|
|
|
|
|
|
|
if (!demuxer->seekable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Pick some arbitrary video track
|
|
|
|
int v_tnum = -1;
|
|
|
|
for (int n = 0; n < mkv_d->num_tracks; n++) {
|
|
|
|
if (mkv_d->tracks[n]->type == MATROSKA_TRACK_VIDEO) {
|
|
|
|
v_tnum = mkv_d->tracks[n]->tnum;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (v_tnum < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
read_deferred_cues(demuxer);
|
|
|
|
|
|
|
|
if (!mkv_d->index_complete)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find last cluster that still has video packets
|
|
|
|
int64_t target = 0;
|
|
|
|
for (size_t i = 0; i < mkv_d->num_indexes; i++) {
|
|
|
|
struct mkv_index *cur = &mkv_d->indexes[i];
|
|
|
|
if (cur->tnum == v_tnum)
|
|
|
|
target = MPMAX(target, cur->filepos);
|
|
|
|
}
|
|
|
|
if (!target)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!stream_seek(demuxer->stream, target))
|
|
|
|
return;
|
|
|
|
|
|
|
|
int64_t last_ts[STREAM_TYPE_COUNT] = {0};
|
|
|
|
while (1) {
|
|
|
|
struct block_info block;
|
|
|
|
int res = read_next_block(demuxer, &block);
|
|
|
|
if (res < 0)
|
|
|
|
break;
|
|
|
|
if (res > 0) {
|
|
|
|
if (block.track && block.track->stream) {
|
|
|
|
enum stream_type type = block.track->stream->type;
|
2014-11-20 21:27:27 +00:00
|
|
|
uint64_t endtime = block.timecode + block.duration;
|
|
|
|
if (last_ts[type] < endtime)
|
|
|
|
last_ts[type] = endtime;
|
2014-11-18 22:07:20 +00:00
|
|
|
}
|
|
|
|
free_block(&block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_ts[STREAM_VIDEO])
|
|
|
|
mkv_d->duration = last_ts[STREAM_VIDEO] / 1e9;
|
|
|
|
|
|
|
|
stream_seek(demuxer->stream, old_pos);
|
|
|
|
mkv_d->cluster_start = mkv_d->cluster_end = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
static int demux_mkv_control(demuxer_t *demuxer, int cmd, void *arg)
|
2004-01-19 19:16:10 +00:00
|
|
|
{
|
2009-12-29 19:06:21 +00:00
|
|
|
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
switch (cmd) {
|
2004-01-19 19:16:10 +00:00
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH:
|
2009-12-29 19:06:21 +00:00
|
|
|
if (mkv_d->duration == 0)
|
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
2004-01-19 19:16:10 +00:00
|
|
|
|
2009-12-29 19:06:21 +00:00
|
|
|
*((double *) arg) = (double) mkv_d->duration;
|
|
|
|
return DEMUXER_CTRL_OK;
|
2004-01-19 19:16:10 +00:00
|
|
|
default:
|
2009-12-29 19:06:21 +00:00
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
2004-01-19 19:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 19:00:24 +00:00
|
|
|
static void mkv_free(struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
struct mkv_demuxer *mkv_d = demuxer->priv;
|
|
|
|
if (!mkv_d)
|
|
|
|
return;
|
|
|
|
mkv_seek_reset(demuxer);
|
|
|
|
for (int i = 0; i < mkv_d->num_tracks; i++)
|
|
|
|
demux_mkv_free_trackentry(mkv_d->tracks[i]);
|
|
|
|
}
|
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_matroska = {
|
2013-07-11 18:08:12 +00:00
|
|
|
.name = "mkv",
|
2013-07-12 20:12:02 +00:00
|
|
|
.desc = "Matroska",
|
2013-07-11 18:08:12 +00:00
|
|
|
.type = DEMUXER_TYPE_MATROSKA,
|
2013-07-12 19:58:11 +00:00
|
|
|
.open = demux_mkv_open,
|
2013-07-11 18:08:12 +00:00
|
|
|
.fill_buffer = demux_mkv_fill_buffer,
|
|
|
|
.close = mkv_free,
|
|
|
|
.seek = demux_mkv_seek,
|
|
|
|
.control = demux_mkv_control
|
2005-08-05 19:57:47 +00:00
|
|
|
};
|
2013-10-07 00:49:12 +00:00
|
|
|
|
|
|
|
bool demux_matroska_uid_cmp(struct matroska_segment_uid *a,
|
|
|
|
struct matroska_segment_uid *b)
|
|
|
|
{
|
|
|
|
return (!memcmp(a->segment, b->segment, 16) &&
|
|
|
|
a->edition == b->edition);
|
|
|
|
}
|