diff options
author | wm4 <wm4@nowhere> | 2019-06-02 21:45:21 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2019-09-19 20:37:05 +0200 |
commit | aa275b2f0ce452dee7f968d637566cf3ff4b674d (patch) | |
tree | 0b98543f71af60fae4df05712683053a7a580337 /demux | |
parent | c0014e2f930a545deb0bb2eb7b98f7c2d3cac2db (diff) | |
download | mpv-aa275b2f0ce452dee7f968d637566cf3ff4b674d.tar.bz2 mpv-aa275b2f0ce452dee7f968d637566cf3ff4b674d.tar.xz |
demux: simplify cache search and exit early
The search was slightly more complicated and slow than it had to be. It
didn't assume that the packet list was sorted, which is responsible for
much of this. (I think the search code was borrowed from demux_mkv.c,
which does not sort index entries.)
There was a half-hearted attempt to make it exit early, but it was
mostly ineffective.
Simplify the code based on the assumption that the list is sorted. This
will exit the search loop once the worst case candidate entry was
checked.
Diffstat (limited to 'demux')
-rw-r--r-- | demux/demux.c | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/demux/demux.c b/demux/demux.c index 39142817ab..82d28cf541 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -3066,29 +3066,25 @@ static struct demux_packet *find_seek_target(struct demux_queue *queue, } struct demux_packet *target = NULL; - double target_diff = MP_NOPTS_VALUE; for (struct demux_packet *dp = start; dp; dp = dp->next) { double range_pts = dp->kf_seek_pts; if (!dp->keyframe || range_pts == MP_NOPTS_VALUE) continue; - double diff = range_pts - pts; if (flags & SEEK_FORWARD) { - diff = -diff; - if (diff > 0) - continue; - } - if (target) { - if (diff <= 0) { - if (target_diff <= 0 && diff <= target_diff) - continue; - } else if (diff >= target_diff) + // Stop on the first packet that is >= pts. + if (target) + break; + if (range_pts < pts) continue; + } else { + // Stop before the first packet that is > pts. + // This still returns a packet with > pts if there's no better one. + if (target && range_pts > pts) + break; } - target_diff = diff; + target = dp; - if (range_pts > pts) - break; } // Usually, the last seen keyframe (keyframe_latest) has kf_seek_pts unset @@ -3106,7 +3102,6 @@ static struct demux_packet *find_seek_target(struct demux_queue *queue, target = queue->keyframe_latest; } - return target; } |