ffmpeg | branch: master | Niklas Haas <g...@haasn.dev> | Tue Jul 22 12:35:38 
2025 +0200| [603334a04362e7a466cbdf9a25892ded167e3ce0] | committer: Niklas Haas

avfilter/vf_premultiply: use correct premultiplication formula

The previous formula was introduced without justification in 6e713841e8,
and the only thing Paul had to say about it over IRC was that it was copied
from an unspecified source on the internet.

I decided to do some testing and came to the conclusion that this term not
only produces "illegal" files, but also lowers PSNR score, over the naive
implementation without this extra term.

Here are the results of a round-trip test, using allrgb/allyuv (respectively)
as the input, and fade=alpha=yes:n=256 to cycle through every possible alpha
value, comparing the round-trip output against the input:

Before patch:
  PSNR r:26.677431 g:26.677431 b:26.677431 a:inf average:27.926818 min:6.012093 
max:55.400791
  PSNR y:26.677431 u:21.101981 v:21.101981 a:inf average:23.548981 min:9.013835 
max:53.182303 (full)
  PSNR y:27.348055 u:21.101981 v:21.101981 a:inf average:23.625238 min:9.554991 
max:45.652221 (limited)

After patch:
  PSNR r:27.321996 g:27.321996 b:27.321996 a:inf average:28.571384 min:6.012093 
max:52.424553
  PSNR y:27.321996 u:23.187879 v:23.187879 a:inf average:25.431773 min:9.013835 
max:50.199232 (full)
  PSNR y:27.868544 u:23.187879 v:23.187879 a:inf average:25.515660 min:9.554991 
max:45.078298 (limited)

It's worth pointing out that previous version sometimes artificially inflates
PSNR by producing values that are too high (i.e. RGB > A), such as for the
input pair (R = 255, A = 2) which should give R = 2, but actually gives R = 3
under the old logic.

As a second evaluation without this shortcoming, here is a comparison against
the reference value computed with a floating point format:

Before patch:
  PSNR r:53.600599 g:53.957833 b:53.540948 a:inf average:54.945316 
min:50.508901 max:inf (premul only)
  PSNR r:30.734183 g:30.734183 b:30.734183 a:inf average:31.983570 
min:12.058264 max:inf (round-trip)

After patch:
  PSNR r:61.751104 g:65.239091 b:61.339191 a:inf average:63.710714 
min:55.441130 max:inf (premul only)
  PSNR r:32.611851 g:32.611851 b:32.611851 a:inf average:33.861238 
min:12.058264 max:inf (round-trip)

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=603334a04362e7a466cbdf9a25892ded167e3ce0
---

 libavfilter/vf_premultiply.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libavfilter/vf_premultiply.c b/libavfilter/vf_premultiply.c
index 322fc39094..9647563f57 100644
--- a/libavfilter/vf_premultiply.c
+++ b/libavfilter/vf_premultiply.c
@@ -103,7 +103,7 @@ static void premultiply8(const uint8_t *msrc, const uint8_t 
*asrc,
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
+            dst[x] = (msrc[x] * asrc[x] + 128) >> 8;
         }
 
         dst  += dlinesize;
@@ -123,7 +123,7 @@ static void premultiply8yuv(const uint8_t *msrc, const 
uint8_t *asrc,
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) 
>> 8) + 128;
+            dst[x] = (((msrc[x] - 128) * asrc[x]) >> 8) + 128;
         }
 
         dst  += dlinesize;
@@ -143,7 +143,7 @@ static void premultiply8offset(const uint8_t *msrc, const 
uint8_t *asrc,
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) 
+ 128) >> 8) + offset;
+            dst[x] = ((((msrc[x] - offset) * asrc[x]) + 128) >> 8) + offset;
         }
 
         dst  += dlinesize;
@@ -166,7 +166,7 @@ static void premultiply16(const uint8_t *mmsrc, const 
uint8_t *aasrc,
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> 
shift;
+            dst[x] = (msrc[x] * asrc[x] + half) >> shift;
         }
 
         dst  += dlinesize / 2;
@@ -189,7 +189,7 @@ static void premultiply16yuv(const uint8_t *mmsrc, const 
uint8_t *aasrc,
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((((msrc[x] - half) * (int64_t)(((asrc[x] >> 1) & 1) + 
asrc[x]))) >> shift) + half;
+            dst[x] = (((msrc[x] - half) * (int64_t)asrc[x]) >> shift) + half;
         }
 
         dst  += dlinesize / 2;
@@ -212,7 +212,7 @@ static void premultiply16offset(const uint8_t *mmsrc, const 
uint8_t *aasrc,
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((((msrc[x] - offset) * (int64_t)(((asrc[x] >> 1) & 1) + 
asrc[x])) + half) >> shift) + offset;
+            dst[x] = ((((msrc[x] - offset) * (int64_t)asrc[x]) + half) >> 
shift) + offset;
         }
 
         dst  += dlinesize / 2;

_______________________________________________
ffmpeg-cvslog mailing list
ffmpeg-cvslog@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog

To unsubscribe, visit link above, or email
ffmpeg-cvslog-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to