[Ffmpeg-devel] clip_uint8

Panagiotis Issaris takis.issaris
Fri Apr 28 17:58:56 CEST 2006


The clip function clips between the amin and amax values given as parameters, 
the clip_uint8 function, actually behaves a bit differently. The lower bound
is indeed 0 as the uint8 postfix in the name implies, but upper values (values 
higher then 255) are returned as -1. This works if the value which stores the 
returned value is uint8, but in other cases will cause strange behavior imho.

430 static inline int clip(int a, int amin, int amax)
431 {
432     if (a < amin)
433         return amin;
434     else if (a > amax)
435         return amax;
436     else
437         return a;
438 }
440 static inline int clip_uint8(int a)
441 {
442     if (a&(~255)) return (-a)>>31;
443     else          return a;
444 }

Changing the return type to uint8_t fixes this, or was the -1 
return value intentional?

diff --git a/libavutil/common.h b/libavutil/common.h
index d8f2c40..92c0d08 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -437,7 +437,7 @@ static inline int clip(int a, int amin,
         return a;

-static inline int clip_uint8(int a)
+static inline uint8_t clip_uint8(int a)
     if (a&(~255)) return (-a)>>31;
     else          return a;

With friendly regards,

More information about the ffmpeg-devel mailing list