Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Linjie Fu <linjie.justin.fu@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Linjie Fu <linjie.justin.fu@gmail.com>
Subject: [FFmpeg-devel] [PATCH] avfilter/vf_overlay: Fix the calculation of average alpha with alpha composition and threads > 1
Date: Tue,  3 May 2022 01:51:43 +0800
Message-ID: <20220502175143.1643-1-linjie.justin.fu@gmail.com> (raw)

The calculation of average alpha utilizes the value of next line(alpha[x+linesize]):
    alpha[0]            alpha[1]
    alpha[0+linesize]   alpha[1+linesize]

However, alpha[x+linesize] could be either an alpha_composited value or
an original value if it locates at next slice, which may lead to an
incorrect output and run2run changed MD5.

Hence, only utilizes the alpha within one slice on the boundary to fix
the calculation of average alpha.

CMD:
    $ffmpeg -i https://samples.ffmpeg.org/FLV/flash_with_alpha/laraShadow_dl.flv \
            -filter_complex "nullsrc=1920x1080[arena];[arena][0:v]overlay" \
                                                    -vframes 10 -an -f md5 -

Before:
Incorrect output and changed MD5 at each run.

After this patch:
Consistent output and MD5.

Signed-off-by: Linjie Fu <linjie.justin.fu@gmail.com>
---
[RFC]This is a partial fix. A fully-fixed method may be don't overwrite dst_alpha
or preserve the original alpha value in composite_alpha(). 
Then we can use the original alpha value for calculation in blend_plane().

 libavfilter/vf_overlay.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavfilter/vf_overlay.c b/libavfilter/vf_overlay.c
index f560d54dae..9488ccd0d7 100644
--- a/libavfilter/vf_overlay.c
+++ b/libavfilter/vf_overlay.c
@@ -515,13 +515,13 @@ static av_always_inline void blend_plane_##depth##_##nbits##bits(AVFilterContext
             if (main_has_alpha && alpha != 0 && alpha != max) {                                            \
                 /* average alpha for color components, improve quality */                                  \
                 uint8_t alpha_d;                                                                           \
-                if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {                                        \
+                if (hsub && vsub && j+1 < src_hp && k+1 < src_wp && j+1 < slice_end) {                     \
                     alpha_d = (da[0] + da[dst->linesize[3]] +                                              \
                                da[1] + da[dst->linesize[3]+1]) >> 2;                                       \
                 } else if (hsub || vsub) {                                                                 \
                     alpha_h = hsub && k+1 < src_wp ?                                                       \
                         (da[0] + da[1]) >> 1 : da[0];                                                      \
-                    alpha_v = vsub && j+1 < src_hp ?                                                       \
+                    alpha_v = vsub && j+1 < src_hp && j+1 < slice_end ?                                    \
                         (da[0] + da[dst->linesize[3]]) >> 1 : da[0];                                       \
                     alpha_d = (alpha_v + alpha_h) >> 1;                                                    \
                 } else                                                                                     \
-- 
2.31.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".

                 reply	other threads:[~2022-05-02 17:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220502175143.1643-1-linjie.justin.fu@gmail.com \
    --to=linjie.justin.fu@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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