Commit 20f4d72bb3 for ffmpeg
commit 20f4d72bb335b4fd8a6559a5686331e19ddf658b
Author: Nathan Moinvaziri <nathan@nathanm.com>
Date: Sun Oct 18 12:58:34 2026 -0700
avfilter/vf_chromakey: read the neighbours of interior pixels directly
Each of the nine neighbours of every pixel went through a bounds check
and its own address computation. Pixels away from the frame edges now
read them from three precomputed rows, which keeps the output identical
and cuts the time of the filter by about a quarter.
Signed-off-by: Nathan Moinvaziri <nathan@nathanm.com>
diff --git a/libavfilter/vf_chromakey.c b/libavfilter/vf_chromakey.c
index 7ef75f2d07..6c9b6c0083 100644
--- a/libavfilter/vf_chromakey.c
+++ b/libavfilter/vf_chromakey.c
@@ -128,10 +128,28 @@ static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int
memset(v, ctx->chromakey_uv[1], sizeof(v));
for (y = slice_start; y < slice_end; ++y) {
+ const int interior = y > 0 && y < frame->height - 1;
+ const uint8_t *urow[3], *vrow[3];
+
+ for (yo = 0; yo < 3 && interior; ++yo) {
+ urow[yo] = frame->data[1] + frame->linesize[1] * ((y + yo - 1) >> ctx->vsub_log2);
+ vrow[yo] = frame->data[2] + frame->linesize[2] * ((y + yo - 1) >> ctx->vsub_log2);
+ }
+
for (x = 0; x < frame->width; ++x) {
- for (yo = 0; yo < 3; ++yo) {
- for (xo = 0; xo < 3; ++xo) {
- get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
+ if (interior && x > 0 && x < frame->width - 1) {
+ /* all nine neighbours lie inside the frame */
+ for (yo = 0; yo < 3; ++yo) {
+ for (xo = 0; xo < 3; ++xo) {
+ u[yo * 3 + xo] = urow[yo][(x + xo - 1) >> ctx->hsub_log2];
+ v[yo * 3 + xo] = vrow[yo][(x + xo - 1) >> ctx->hsub_log2];
+ }
+ }
+ } else {
+ for (yo = 0; yo < 3; ++yo) {
+ for (xo = 0; xo < 3; ++xo) {
+ get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
+ }
}
}