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.
This commit is contained in:
wm4 2019-06-02 21:45:21 +02:00
parent c0014e2f93
commit aa275b2f0c
1 changed files with 10 additions and 15 deletions

View File

@ -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)
// 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;
}
if (target) {
if (diff <= 0) {
if (target_diff <= 0 && diff <= target_diff)
continue;
} else if (diff >= target_diff)
continue;
}
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;
}