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 */