summaryrefslogtreecommitdiffstats
path: root/demux/demux_mkv.c
diff options
context:
space:
mode:
Diffstat (limited to 'demux/demux_mkv.c')
-rw-r--r--demux/demux_mkv.c87
1 files changed, 63 insertions, 24 deletions
diff --git a/demux/demux_mkv.c b/demux/demux_mkv.c
index 4216a6ad71..b4ae042feb 100644
--- a/demux/demux_mkv.c
+++ b/demux/demux_mkv.c
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
+#include <math.h>
#include <assert.h>
#include <libavutil/common.h>
@@ -43,6 +44,7 @@
#include "talloc.h"
#include "common/av_common.h"
#include "options/options.h"
+#include "options/m_option.h"
#include "misc/bstr.h"
#include "stream/stream.h"
#include "video/csputils.h"
@@ -184,6 +186,30 @@ typedef struct mkv_demuxer {
bool eof_warning;
} mkv_demuxer_t;
+#define OPT_BASE_STRUCT struct demux_mkv_opts
+struct demux_mkv_opts {
+ int subtitle_preroll;
+ double subtitle_preroll_secs;
+ int probe_duration;
+ int fix_timestamps;
+};
+
+const struct m_sub_options demux_mkv_conf = {
+ .opts = (const m_option_t[]) {
+ OPT_FLAG("subtitle-preroll", subtitle_preroll, 0),
+ OPT_DOUBLE("subtitle-preroll-secs", subtitle_preroll_secs,
+ M_OPT_MIN, .min = 0),
+ OPT_FLAG("probe-video-duration", probe_duration, 0),
+ OPT_FLAG("fix-timestamps", fix_timestamps, 0),
+ {0}
+ },
+ .size = sizeof(struct demux_mkv_opts),
+ .defaults = &(const struct demux_mkv_opts){
+ .subtitle_preroll_secs = 1.0,
+ .fix_timestamps = 1,
+ },
+};
+
#define REALHEADER_SIZE 16
#define RVPROPERTIES_SIZE 34
#define RAPROPERTIES4_SIZE 56
@@ -1751,6 +1777,7 @@ static int read_mkv_segment_header(demuxer_t *demuxer, int64_t *segment_end)
static int demux_mkv_open(demuxer_t *demuxer, enum demux_check check)
{
stream_t *s = demuxer->stream;
+ struct MPOpts *opts = demuxer->opts;
mkv_demuxer_t *mkv_d;
int64_t start_pos;
int64_t end_pos;
@@ -1836,7 +1863,7 @@ static int demux_mkv_open(demuxer_t *demuxer, enum demux_check check)
add_coverart(demuxer);
demuxer->allow_refresh_seeks = true;
- if (demuxer->opts->mkv_probe_duration)
+ if (opts->demux_mkv->probe_duration)
probe_last_timestamp(demuxer);
return 0;
@@ -2304,6 +2331,19 @@ exit:
return res;
}
+static double fix_timestamp(demuxer_t *demuxer, mkv_track_t *track, double ts)
+{
+ mkv_demuxer_t *mkv_d = demuxer->priv;
+ if (demuxer->opts->demux_mkv->fix_timestamps && track->default_duration > 0) {
+ // Assume that timestamps have been rounded to the timecode scale.
+ double quant = MPMIN(mkv_d->tc_scale / 1e9, 0.001);
+ double rts = rint(ts / track->default_duration) * track->default_duration;
+ if (fabs(rts - ts) < quant)
+ ts = rts;
+ }
+ return ts;
+}
+
static int handle_block(demuxer_t *demuxer, struct block_info *block_info)
{
mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv;
@@ -2326,7 +2366,7 @@ static int handle_block(demuxer_t *demuxer, struct block_info *block_info)
return 0;
}
- current_pts = tc / 1e9 - track->codec_delay;
+ current_pts = fix_timestamp(demuxer, track, tc / 1e9) - track->codec_delay;
if (track->type == MATROSKA_TRACK_AUDIO) {
if (mkv_d->a_skip_to_keyframe)
@@ -2619,35 +2659,30 @@ static int create_index_until(struct demuxer *demuxer, uint64_t timecode)
return 0;
}
+#define FLAG_BACKWARD 1
+#define FLAG_SUBPREROLL 2
static struct mkv_index *seek_with_cues(struct demuxer *demuxer, int seek_id,
int64_t target_timecode, int flags)
{
+ struct MPOpts *opts = demuxer->opts;
struct mkv_demuxer *mkv_d = demuxer->priv;
struct mkv_index *index = NULL;
- /* 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.
- */
- int64_t min_diff = target_timecode - (int64_t)(mkv_d->last_pts * 1e9 + 0.5);
- if (flags & SEEK_BACKWARD)
- min_diff = -min_diff;
- min_diff = FFMAX(min_diff, 1);
-
+ int64_t min_diff = INT64_MIN;
for (size_t i = 0; i < mkv_d->num_indexes; i++) {
if (seek_id < 0 || mkv_d->indexes[i].tnum == seek_id) {
int64_t diff =
target_timecode -
(int64_t) (mkv_d->indexes[i].timecode * mkv_d->tc_scale);
- if (flags & SEEK_BACKWARD)
+ if (flags & FLAG_BACKWARD)
diff = -diff;
- if (diff <= 0) {
- if (min_diff <= 0 && diff <= min_diff)
+ if (min_diff != INT64_MIN) {
+ if (diff <= 0) {
+ if (min_diff <= 0 && diff <= min_diff)
+ continue;
+ } else if (diff >= min_diff)
continue;
- } else if (diff >= min_diff)
- continue;
+ }
min_diff = diff;
index = mkv_d->indexes + i;
}
@@ -2655,10 +2690,10 @@ static struct mkv_index *seek_with_cues(struct demuxer *demuxer, int seek_id,
if (index) { /* We've found an entry. */
uint64_t seek_pos = index->filepos;
- if (flags & SEEK_SUBPREROLL) {
+ if (flags & FLAG_SUBPREROLL) {
// Find the cluster with the highest filepos, that has a timestamp
// still lower than min_tc.
- double secs = demuxer->opts->mkv_subtitle_preroll_secs;
+ double secs = opts->demux_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;
uint64_t prev_target = 0;
@@ -2717,9 +2752,13 @@ static void demux_mkv_seek(demuxer_t *demuxer, double rel_seek_secs, int flags)
a_tnum = track->tnum;
}
}
+
+ int cueflags = (flags & SEEK_BACKWARD) ? FLAG_BACKWARD : 0;
+
mkv_d->subtitle_preroll = NUM_SUB_PREROLL_PACKETS;
- if (!st_active[STREAM_SUB] || !st_active[STREAM_VIDEO])
- flags &= ~SEEK_SUBPREROLL;
+ if (((flags & SEEK_HR) || demuxer->opts->demux_mkv->subtitle_preroll) &&
+ st_active[STREAM_SUB] && st_active[STREAM_VIDEO])
+ cueflags |= FLAG_SUBPREROLL;
// Adjust the target a little bit to catch cases where the target position
// specifies a keyframe with high, but not perfect, precision.
@@ -2735,9 +2774,9 @@ static void demux_mkv_seek(demuxer_t *demuxer, double rel_seek_secs, int flags)
if (create_index_until(demuxer, target_timecode) >= 0) {
int seek_id = st_active[STREAM_VIDEO] ? v_tnum : a_tnum;
- index = seek_with_cues(demuxer, seek_id, target_timecode, flags);
+ index = seek_with_cues(demuxer, seek_id, target_timecode, cueflags);
if (!index)
- index = seek_with_cues(demuxer, -1, target_timecode, flags);
+ index = seek_with_cues(demuxer, -1, target_timecode, cueflags);
}
if (!index)