From d44512011350ac3453bce65731bb82fbbe2fe012 Mon Sep 17 00:00:00 2001
From: Wu Jianhua <jianhua.wu@intel.com>
Date: Wed, 23 Feb 2022 00:58:57 +0800
Subject: [PATCH 02/10] avfilter/vf_blend_vulkan: add average blend mode

Use the commands below to test: (href: https://trac.ffmpeg.org/wiki/Blend)

I. make an image for test
ffmpeg -f lavfi -i color=s=256x256,geq=r='H-1-Y':g='H-1-Y':b='H-1-Y' -frames 1 \
-y -pix_fmt yuv420p test.jpg

II. blend in sw
ffmpeg -i test.jpg -vf "split[a][b];[b]transpose[b];[a][b]blend=all_mode=average,\
pseudocolor=preset=turbo" -y average_sw.jpg

III. blend in vulkan
ffmpeg -init_hw_device vulkan -i test.jpg -vf "split[a][b];[b]transpose[b];\
[a]hwupload[a];[b]hwupload[b];[a][b]blend_vulkan=all_mode=average,hwdownload,\
format=yuv420p,pseudocolor=preset=turbo" -y average_vulkan.jpg

Signed-off-by: Wu Jianhua <jianhua.wu@intel.com>
---
 libavfilter/vf_blend_vulkan.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/libavfilter/vf_blend_vulkan.c b/libavfilter/vf_blend_vulkan.c
index b0349512a8..2fc6e9f0c5 100644
--- a/libavfilter/vf_blend_vulkan.c
+++ b/libavfilter/vf_blend_vulkan.c
@@ -68,8 +68,8 @@ static const char blend_##MODE##_func[] = { \
 #define A top
 #define B bottom
 
-#define MAX vec4(1.0)
-
+#define MAX  vec4(1.0)
+#define HALF vec4(0.5)
 #ifdef min
 #undef min
 #endif
@@ -78,6 +78,7 @@ static const char blend_##MODE##_func[] = { \
 
 DEFINE_BLEND_MODE(NORMAL, A * opacity + B * (1.0f - opacity))
 DEFINE_BLEND_MODE(ADDITION, FN(min(MAX, A + B)))
+DEFINE_BLEND_MODE(AVERAGE,  FN((A + B) * HALF))
 DEFINE_BLEND_MODE(MULTIPLY, FN(1.0f * A * B / 1.0f))
 
 static inline void init_blend_func(FilterParamsVulkan *param)
@@ -89,6 +90,7 @@ static inline void init_blend_func(FilterParamsVulkan *param)
 
     switch (param->mode) {
     CASE(ADDITION)
+    CASE(AVERAGE)
     CASE(NORMAL)
     CASE(MULTIPLY)
     default: param->blend = NULL; break;
@@ -461,6 +463,7 @@ static const AVOption blend_vulkan_options[] = {
     { "c3_mode", "set component #3 blend mode", OFFSET(params[3].mode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, BLEND_NB - 1, FLAGS, "mode" },
     { "all_mode", "set blend mode for all components", OFFSET(all_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, BLEND_NB - 1, FLAGS, "mode" },
         { "addition", "", 0, AV_OPT_TYPE_CONST, { .i64 = BLEND_ADDITION }, 0, 0, FLAGS, "mode" },
+        { "average",  "", 0, AV_OPT_TYPE_CONST, { .i64 = BLEND_AVERAGE  }, 0, 0, FLAGS, "mode" },
         { "normal",   "", 0, AV_OPT_TYPE_CONST, { .i64 = BLEND_NORMAL   }, 0, 0, FLAGS, "mode" },
         { "multiply", "", 0, AV_OPT_TYPE_CONST, { .i64 = BLEND_MULTIPLY }, 0, 0, FLAGS, "mode" },
 
-- 
2.25.1

