compositor: Use 255 as maximum alpha instead of 256
authorSebastian Dröge <sebastian@centricular.com>
Fri, 20 Jul 2018 13:25:02 +0000 (16:25 +0300)
committerSebastian Dröge <sebastian@centricular.com>
Mon, 23 Jul 2018 15:59:33 +0000 (18:59 +0300)
255 will easily become 0 in the blending function as they expect
the maximum value to be 255.

Can be reproduce with

gst-launch-1.0 videotestsrc pattern=ball ! c.sink_0 \
               videotestsrc pattern=snow ! c.sink_1 \
               compositor name=c \
                 sink_0::zorder=0 sink_1::zorder=1 sink_0::crossfade-ratio=0.5 \
                 background=black ! \
               videoconvert ! xvimagesink

crossfade-ratio +/- 0.001 makes it work correctly and the same happens
at e.g. 0.25, 0.75, N*0.0625

https://bugzilla.gnome.org/show_bug.cgi?id=796846

gst/compositor/blend.c

index b36c164..416f78c 100644 (file)
@@ -33,8 +33,6 @@
 
 #include <gst/video/video.h>
 
-#define BLEND(D,S,alpha) (((D) * (256 - (alpha)) + (S) * (alpha)) >> 8)
-
 GST_DEBUG_CATEGORY_STATIC (gst_compositor_blend_debug);
 #define GST_CAT_DEFAULT gst_compositor_blend_debug
 
@@ -61,7 +59,7 @@ method##_ ##name (GstVideoFrame * srcframe, gint xpos, gint ypos, \
   dest_width = GST_VIDEO_FRAME_COMP_WIDTH (destframe, 0); \
   dest_height = GST_VIDEO_FRAME_COMP_HEIGHT (destframe, 0); \
   \
-  s_alpha = CLAMP ((gint) (src_alpha * 256), 0, 256); \
+  s_alpha = CLAMP ((gint) (src_alpha * 255), 0, 255); \
   \
   /* If it's completely transparent... we just return */ \
   if (G_UNLIKELY (s_alpha == 0)) \
@@ -247,7 +245,7 @@ _blend_##format_name (const guint8 * src, guint8 * dest, \
     return; \
   } \
   \
-  b_alpha = CLAMP ((gint) (src_alpha * 256), 0, 256); \
+  b_alpha = CLAMP ((gint) (src_alpha * 255), 0, 255); \
   \
   BLENDLOOP(dest, dest_stride, src, src_stride, b_alpha, src_width, src_height);\
 } \
@@ -492,7 +490,7 @@ _blend_##format_name (const guint8 * src, guint8 * dest, \
     return; \
   } \
   \
-  b_alpha = CLAMP ((gint) (src_alpha * 256), 0, 256); \
+  b_alpha = CLAMP ((gint) (src_alpha * 255), 0, 255); \
   \
   BLENDLOOP(dest, dest_stride, src, src_stride, b_alpha, src_width, src_height); \
 } \
@@ -691,7 +689,7 @@ blend_##name (GstVideoFrame * srcframe, gint xpos, gint ypos, \
   src_stride = GST_VIDEO_FRAME_COMP_STRIDE (srcframe, 0); \
   dest_stride = GST_VIDEO_FRAME_COMP_STRIDE (destframe, 0); \
   \
-  b_alpha = CLAMP ((gint) (src_alpha * 256), 0, 256); \
+  b_alpha = CLAMP ((gint) (src_alpha * 255), 0, 255); \
   \
   /* adjust src pointers for negative sizes */ \
   if (xpos < 0) { \
@@ -857,7 +855,7 @@ blend_##name (GstVideoFrame * srcframe, gint xpos, gint ypos, \
   src_stride = GST_VIDEO_FRAME_COMP_STRIDE (srcframe, 0); \
   dest_stride = GST_VIDEO_FRAME_COMP_STRIDE (destframe, 0); \
   \
-  b_alpha = CLAMP ((gint) (src_alpha * 256), 0, 256); \
+  b_alpha = CLAMP ((gint) (src_alpha * 255), 0, 255); \
   \
   xpos = GST_ROUND_UP_2 (xpos); \
   \