From aa275b2f0ce452dee7f968d637566cf3ff4b674d Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 2 Jun 2019 21:45:21 +0200 Subject: 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. --- demux/demux.c | 25 ++++++++++--------------- 1 file 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; } -- cgit v1.2.3