[FFmpeg-devel] [PATCH] avfilter/vf_alphamerge: use refcounting for planar formats
Jorge Estrada
jestrada.list at gmail.com
Thu Aug 7 22:58:00 EEST 2025
Use reference plane when handling planar formats. Should address
https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/20153
Example:
ffmpeg -f lavfi -i "color=red:s=640x480:d=5,format=yuva420p" \
-f lavfi -i "color=black:s=640x480:d=5,format=yuv420p,geq=lum='255*gt(150,hypot(X-W/2,Y-H/2))':a=0" \
-f lavfi -i "color=blue:s=640x480:d=5,format=yuv420p" \
-filter_complex "[0:v][1:v]alphamerge[merged_with_alpha];[2:v][merged_with_alpha]overlay" \
-c:v libx264 \
-y out.mp4
---
libavfilter/vf_alphamerge.c | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/libavfilter/vf_alphamerge.c b/libavfilter/vf_alphamerge.c
index f5779484a9..9f53806537 100644
--- a/libavfilter/vf_alphamerge.c
+++ b/libavfilter/vf_alphamerge.c
@@ -28,12 +28,12 @@
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "libavutil/pixfmt.h"
+#include "libavutil/frame.h"
#include "avfilter.h"
#include "drawutils.h"
#include "formats.h"
#include "filters.h"
#include "framesync.h"
-#include "video.h"
enum { Y, U, V, A };
@@ -78,11 +78,23 @@ static int do_alphamerge(FFFrameSync *fs)
}
}
} else {
- const int main_linesize = main_buf->linesize[A];
- const int alpha_linesize = alpha_buf->linesize[Y];
- av_image_copy_plane(main_buf->data[A], main_linesize,
- alpha_buf->data[Y], alpha_linesize,
- FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
+ AVBufferRef *alpha_plane_buf = av_frame_get_plane_buffer(alpha_buf, Y);
+
+ if (!alpha_plane_buf) {
+ av_log(ctx, AV_LOG_ERROR, "Could not get buffer for alpha plane.\n");
+ return AVERROR(EINVAL);
+ }
+
+ av_buffer_unref(&main_buf->buf[A]);
+
+ main_buf->buf[A] = av_buffer_ref(alpha_plane_buf);
+ if (!main_buf->buf[A]) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reference alpha plane buffer.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ main_buf->data[A] = alpha_buf->data[Y];
+ main_buf->linesize[A] = alpha_buf->linesize[Y];
}
return ff_filter_frame(ctx->outputs[0], main_buf);
@@ -212,4 +224,4 @@ const FFFilter ff_vf_alphamerge = {
FILTER_QUERY_FUNC2(query_formats),
.uninit = uninit,
.activate = activate,
-};
+};
\ No newline at end of file
--
2.34.1
More information about the ffmpeg-devel
mailing list