From 02e44beef72f32161ebc794dfe4617dbfe6fe07a Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Sun, 7 Jun 2026 21:38:20 +0200 Subject: [PATCH] avfilter: add ff_slice_pos() helper for slice boundaries Slice based filter workers compute their per-thread row/sample/channel boundaries as total * jobnr / nb_jobs. The total * jobnr product is evaluated in int and overflows signed int for large dimensions and many slice threads, before the division by nb_jobs brings it back in range. (cherry picked from commit 218b4771a32153c434f1e87e5c3063c01c51c432) Signed-off-by: Michael Niedermayer --- libavfilter/filters.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libavfilter/filters.h b/libavfilter/filters.h index 86bc49d459..69411d3aa7 100644 --- a/libavfilter/filters.h +++ b/libavfilter/filters.h @@ -266,4 +266,19 @@ static inline void ff_outlink_set_status(AVFilterLink *link, int status, int64_t */ int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink); +/** + * Compute the boundary index for a slice when work of size total is split + * into nb_jobs slices. Returns the first index of slice jobnr, so the slice + * jobnr covers [ff_slice_pos(total, jobnr, nb_jobs), + * ff_slice_pos(total, jobnr + 1, nb_jobs)). + * + * The multiplication is performed in 64bit to avoid signed overflow of the + * total * jobnr intermediate that would occur for large dimensions and many + * slice threads. + */ +static inline int ff_slice_pos(int total, int jobnr, int nb_jobs) +{ + return (int)((int64_t)total * jobnr / nb_jobs); +} + #endif /* AVFILTER_FILTERS_H */