* [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
2025-08-07 23:14 ` [FFmpeg-devel] use av_buffer_replace Jorge Estrada
0 siblings, 2 replies; 4+ 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] 4+ 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
2025-08-07 23:21 ` Jorge Estrada
2025-08-07 23:14 ` [FFmpeg-devel] use av_buffer_replace Jorge Estrada
1 sibling, 1 reply; 4+ 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] 4+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avfilter/vf_alphamerge: use refcounting for planar formats
2025-08-07 20:12 ` James Almer
@ 2025-08-07 23:21 ` Jorge Estrada
0 siblings, 0 replies; 4+ messages in thread
From: Jorge Estrada @ 2025-08-07 23:21 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1: Type: text/plain, Size: 3339 bytes --]
Addressed and attached the updated patch. I sent my previous email
incorrectly. My bad.
On Thu, Aug 7, 2025 at 1:13 PM James Almer <jamrial@gmail.com> wrote:
> 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.
>
> _______________________________________________
> 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".
>
[-- Attachment #2: 0001-avfilter-vf_alphamerge-use-refcounting-for-planar-fo.patch --]
[-- Type: application/octet-stream, Size: 2095 bytes --]
[-- Attachment #3: 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] 4+ messages in thread
* [FFmpeg-devel] use av_buffer_replace
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
@ 2025-08-07 23:14 ` Jorge Estrada
1 sibling, 0 replies; 4+ messages in thread
From: Jorge Estrada @ 2025-08-07 23:14 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Jorge Estrada
Addressed. Uses av_buffer_replace now
---
libavfilter/vf_alphamerge.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/libavfilter/vf_alphamerge.c b/libavfilter/vf_alphamerge.c
index f5779484a9..69c0dd04ea 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,21 @@ 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);
+ }
+
+ ret = av_buffer_replace(&main_buf->buf[A], alpha_plane_buf);
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to replace alpha plane buffer.\n");
+ return ret;
+ }
+
+ 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 +222,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] 4+ messages in thread
end of thread, other threads:[~2025-08-07 23:21 UTC | newest]
Thread overview: 4+ 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
2025-08-07 23:21 ` Jorge Estrada
2025-08-07 23:14 ` [FFmpeg-devel] use av_buffer_replace Jorge Estrada
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