* [FFmpeg-devel] [PATCH] avfilter/vf_alphamerge: use refcounting for planar formats
@ 2025-08-07 19:58 Jorge Estrada
2025-08-07 20:12 ` James Almer
0 siblings, 1 reply; 2+ messages in thread
From: Jorge Estrada @ 2025-08-07 19:58 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Jorge Estrada
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
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avfilter/vf_alphamerge: use refcounting for planar formats
2025-08-07 19:58 [FFmpeg-devel] [PATCH] avfilter/vf_alphamerge: use refcounting for planar formats Jorge Estrada
@ 2025-08-07 20:12 ` James Almer
0 siblings, 0 replies; 2+ messages in thread
From: James Almer @ 2025-08-07 20:12 UTC (permalink / raw)
To: ffmpeg-devel
[-- Attachment #1.1.1: Type: text/plain, Size: 2742 bytes --]
On 8/7/2025 4:58 PM, Jorge Estrada wrote:
> 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);
av_buffer_replace()
> + 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
Please fix this.
[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-08-07 20:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-07 19:58 [FFmpeg-devel] [PATCH] avfilter/vf_alphamerge: use refcounting for planar formats Jorge Estrada
2025-08-07 20:12 ` James Almer
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git