summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-09-16 00:20:19 +0200
committerwm4 <wm4@nowhere>2013-09-20 21:20:33 +0200
commit7b8d3602880869b16ae1fa036b76102d34aec923 (patch)
tree5f6fbde36f9ab5798ea97c5d0efd9f7a1a677620
parent33940727953f5a301d78c35324802eadcabb4fbd (diff)
downloadmpv-7b8d3602880869b16ae1fa036b76102d34aec923.tar.bz2
mpv-7b8d3602880869b16ae1fa036b76102d34aec923.tar.xz
demux_mkv: don't add too many subtitle packets during seeking
In insane files with a very huge number of subtitle events, and if the --demuxer-mkv-subtitle-preroll option is given, seeking can still overflow the packet queue. Normally, the subtitle_preroll variable specifies the maximum number of packets that can be added. But once this number is reached, the normal seeking behavior is enabled, which will add all subtitle packets with the right timestamps to the packet queue. At this point the next video keyframe can still be quite far away, with enough subtitle packets on the way to overflow the packet queue. Fix this by always setting an upper limit of subtitle packets read during seeking. This should provide additional robustness even if the preroll option is not used. This means that even with normal seeking, at most 500 subtitle packets are demuxed. Packets after that are discarded. One slightly questionable aspect of this commit is that subtitle_preroll is never reset in audio-only mode, but that is probably ok.
-rw-r--r--demux/demux_mkv.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/demux/demux_mkv.c b/demux/demux_mkv.c
index e4f40ba212..56951886d9 100644
--- a/demux/demux_mkv.c
+++ b/demux/demux_mkv.c
@@ -197,7 +197,8 @@ typedef struct mkv_demuxer {
#define RAPROPERTIES5_SIZE 70
// Maximum number of subtitle packets that are accepted for pre-roll.
-#define NUM_SUB_PREROLL_PACKETS 100
+// (Subtitle packets added before first A/V keyframe packet is found with seek.)
+#define NUM_SUB_PREROLL_PACKETS 500
/**
* \brief ensures there is space for at least one additional element
@@ -2269,9 +2270,16 @@ static int handle_block(demuxer_t *demuxer, struct block_info *block_info)
if (mkv_d->v_skip_to_keyframe)
use_this_block = 0;
} else if (track->type == MATROSKA_TRACK_SUBTITLE) {
- if (!use_this_block && mkv_d->subtitle_preroll) {
- mkv_d->subtitle_preroll--;
+ if (!use_this_block && mkv_d->subtitle_preroll)
use_this_block = 1;
+ 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;
+ }
}
if (use_this_block) {
if (laces > 1) {
@@ -2563,7 +2571,7 @@ 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 (mkv_d->subtitle_preroll) {
+ if (flags & SEEK_SUBPREROLL) {
uint64_t prev_target = 0;
for (int i = 0; i < mkv_d->num_indexes; i++) {
if (seek_id < 0 || mkv_d->indexes[i].tnum == seek_id) {
@@ -2600,10 +2608,9 @@ static void demux_mkv_seek(demuxer_t *demuxer, float rel_seek_secs,
a_tnum = track->tnum;
}
}
- mkv_d->subtitle_preroll = 0;
- if ((flags & SEEK_SUBPREROLL) && st_active[STREAM_SUB] &&
- st_active[STREAM_VIDEO])
- mkv_d->subtitle_preroll = NUM_SUB_PREROLL_PACKETS;
+ mkv_d->subtitle_preroll = NUM_SUB_PREROLL_PACKETS;
+ if (!st_active[STREAM_SUB] || !st_active[STREAM_VIDEO])
+ flags &= ~SEEK_SUBPREROLL;
if (!(flags & (SEEK_BACKWARD | SEEK_FORWARD))) {
if (flags & SEEK_ABSOLUTE || rel_seek_secs < 0)
flags |= SEEK_BACKWARD;
@@ -2632,14 +2639,13 @@ static void demux_mkv_seek(demuxer_t *demuxer, float rel_seek_secs,
if (!index)
stream_seek(demuxer->stream, old_pos);
- if (st_active[STREAM_VIDEO])
- mkv_d->v_skip_to_keyframe = 1;
+ mkv_d->v_skip_to_keyframe = st_active[STREAM_VIDEO];
+ mkv_d->a_skip_to_keyframe = st_active[STREAM_AUDIO];
if (flags & SEEK_FORWARD)
mkv_d->skip_to_timecode = target_timecode;
else
mkv_d->skip_to_timecode = index ? index->timecode * mkv_d->tc_scale
: 0;
- mkv_d->a_skip_to_keyframe = 1;
demux_mkv_fill_buffer(demuxer);
} else if ((demuxer->movi_end <= 0) || !(flags & SEEK_ABSOLUTE))
@@ -2675,10 +2681,9 @@ static void demux_mkv_seek(demuxer_t *demuxer, float rel_seek_secs,
mkv_d->cluster_end = 0;
stream_seek(s, index->filepos);
- if (st_active[STREAM_VIDEO])
- mkv_d->v_skip_to_keyframe = 1;
+ mkv_d->v_skip_to_keyframe = st_active[STREAM_VIDEO];
+ mkv_d->a_skip_to_keyframe = st_active[STREAM_AUDIO];
mkv_d->skip_to_timecode = index->timecode * mkv_d->tc_scale;
- mkv_d->a_skip_to_keyframe = 1;
demux_mkv_fill_buffer(demuxer);
}