diff --git a/libavfilter/vf_scale_npp.c b/libavfilter/vf_scale_npp.c
index 3fe5bbe500..eadb3b5828 100644
--- a/libavfilter/vf_scale_npp.c
+++ b/libavfilter/vf_scale_npp.c
@@ -319,7 +319,11 @@ static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_heig
         last_stage = i;
     }
 
-    ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage > 0 ? last_stage : 0].frames_ctx);
+    if (last_stage < 0)
+        ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
+    else
+        ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
+
     if (!ctx->outputs[0]->hw_frames_ctx)
         return AVERROR(ENOMEM);