diff --git a/demux/demux.c b/demux/demux.c index 3032e69784..3f65b2f158 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -3098,18 +3098,39 @@ static void switch_current_range(struct demux_internal *in, free_empty_cached_ranges(in); } +// Search for the entry with the highest index with entry.pts <= pts true. +static struct demux_packet *search_index(struct demux_queue *queue, double pts) +{ + size_t a = 0; + size_t b = queue->num_index; + + while (a < b) { + size_t m = a + (b - a) / 2; + struct index_entry *e = &QUEUE_INDEX_ENTRY(queue, m); + + bool m_ok = e->pts <= pts; + + if (a + 1 == b) + return m_ok ? e->pkt : NULL; + + if (m_ok) { + a = m; + } else { + b = m; + } + } + + return NULL; +} + static struct demux_packet *find_seek_target(struct demux_queue *queue, double pts, int flags) { pts -= queue->ds->sh->seek_preroll; - struct demux_packet *start = queue->head; - for (size_t n = 0; n < queue->num_index; n++) { - struct index_entry *e = &QUEUE_INDEX_ENTRY(queue, n); - if (e->pts > pts) - break; - start = e->pkt; - } + struct demux_packet *start = search_index(queue, pts); + if (!start) + start = queue->head; struct demux_packet *target = NULL; for (struct demux_packet *dp = start; dp; dp = dp->next) {