[FFmpeg-devel] [PATCH 1/2] avutil/common: add av_rint64_clip

Michael Niedermayer michael at niedermayer.cc
Fri Nov 13 12:40:20 CET 2015


On Thu, Nov 12, 2015 at 09:46:04PM -0500, Ganesh Ajjanagadde wrote:
> The rationale for this function is reflected in the documentation for
> it, and is copied here:
> 
> Clip a double value into the long long amin-amax range.
> This function is needed because conversion of floating point to integers when
> it does not fit in the integer's representation does not necessarily saturate
> correctly (usually converted to a cvttsd2si on x86) which saturates numbers
> > INT64_MAX to INT64_MIN. The standard marks such conversions as undefined
> behavior, allowing this sort of mathematically bogus conversions. This provides
> a safe alternative that is slower obviously but assures safety and better
> mathematical behavior.
> API:
> @param a value to clip
> @param amin minimum value of the clip range
> @param amax maximum value of the clip range
> @return clipped value
> 
> Note that a priori if one can guarantee from the calling side that the
> double is in range, it is safe to simply do an explicit/implicit cast,
> and that will be far faster. However, otherwise this function should be
> used.
> 
> avutil minor version is bumped.
> 
> Reviewed-by: Ronald S. Bultje <rsbultje at gmail.com>
> Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>
> ---
>  libavutil/common.h  | 31 +++++++++++++++++++++++++++++++
>  libavutil/version.h |  4 ++--
>  2 files changed, 33 insertions(+), 2 deletions(-)
> 
> diff --git a/libavutil/common.h b/libavutil/common.h
> index 6f0f582..4f60e72 100644
> --- a/libavutil/common.h
> +++ b/libavutil/common.h
> @@ -298,6 +298,34 @@ static av_always_inline av_const double av_clipd_c(double a, double amin, double
>      else               return a;
>  }
>  
> +/**
> + * Clip and convert a double value into the long long amin-amax range.
> + * This function is needed because conversion of floating point to integers when
> + * it does not fit in the integer's representation does not necessarily saturate
> + * correctly (usually converted to a cvttsd2si on x86) which saturates numbers
> + * > INT64_MAX to INT64_MIN. The standard marks such conversions as undefined
> + * behavior, allowing this sort of mathematically bogus conversions. This provides
> + * a safe alternative that is slower obviously but assures safety and better
> + * mathematical behavior.
> + * @param a value to clip
> + * @param amin minimum value of the clip range
> + * @param amax maximum value of the clip range
> + * @return clipped value
> + */
> +static av_always_inline av_const int64_t av_rint64_clip_c(double a, int64_t amin, int64_t amax)
> +{
> +#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
> +    if (amin > amax) abort();
> +#endif
> +    // INT64_MAX+1,INT64_MIN are exactly representable as IEEE doubles
> +    if (a >= 9223372036854775808.0)
> +        return INT64_MAX;
> +    if (a <= INT64_MIN)
> +        return INT64_MIN;
> +    // Finally safe to call av_clipd_c
> +    return (int64_t)av_clipd_c(a, amin, amax);
> +}

this fails to build here:

CXX     libavcodec/libutvideodec.o
cc1plus: warning: command line option ‘-Wdeclaration-after-statement’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wmissing-prototypes’ is valid for Ada/C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wno-pointer-to-int-cast’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for Ada/C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wno-format-zero-length’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-Wno-pointer-sign’ is valid for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option ‘-std=c99’ is valid for C/ObjC but not for C++ [enabled by default]
libavcodec/libutvideodec.cpp:59:18: warning: multi-character character constant [-Wmultichar]
libavcodec/libutvideodec.cpp:64:18: warning: multi-character character constant [-Wmultichar]
libavcodec/libutvideodec.cpp:69:18: warning: multi-character character constant [-Wmultichar]
libavcodec/libutvideodec.cpp:73:18: warning: multi-character character constant [-Wmultichar]
libavcodec/libutvideodec.cpp:86:18: warning: multi-character character constant [-Wmultichar]
libavcodec/libutvideodec.cpp:98:19: warning: multi-character character constant [-Wmultichar]
In file included from ./libavutil/avutil.h:288:0,
                 from ./libavutil/samplefmt.h:24,
                 from libavcodec/avcodec.h:31,
                 from libavcodec/libutvideodec.cpp:29:
./libavutil/common.h: In function ‘int64_t av_rint64_clip_c(double, int64_t, int64_t)’:
./libavutil/common.h:322:16: error: ‘INT64_MAX’ was not declared in this scope
./libavutil/common.h:323:14: error: ‘INT64_MIN’ was not declared in this scope
libavcodec/libutvideodec.cpp: In function ‘int utvideo_decode_init(AVCodecContext*)’:
libavcodec/libutvideodec.cpp:96:21: warning: ‘int avpicture_get_size(AVPixelFormat, int, int)’ is deprecated (declared at libavcodec/avcodec.h:4814) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:96:83: warning: ‘int avpicture_get_size(AVPixelFormat, int, int)’ is deprecated (declared at libavcodec/avcodec.h:4814) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:109:12: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:109:12: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:115:12: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:115:12: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:118:12: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:118:12: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp: In function ‘int utvideo_decode_frame(AVCodecContext*, void*, int*, AVPacket*)’:
libavcodec/libutvideodec.cpp:144:27: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:144:27: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp: In function ‘int utvideo_decode_close(AVCodecContext*)’:
libavcodec/libutvideodec.cpp:236:27: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
libavcodec/libutvideodec.cpp:236:27: warning: ‘AVCodecContext::coded_frame’ is deprecated (declared at libavcodec/avcodec.h:2892) [-Wdeprecated-declarations]
make: *** [libavcodec/libutvideodec.o] Error 1


[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Into a blind darkness they enter who follow after the Ignorance,
they as if into a greater darkness enter who devote themselves
to the Knowledge alone. -- Isha Upanishad
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20151113/e97b4e69/attachment.sig>


More information about the ffmpeg-devel mailing list