aboutsummaryrefslogtreecommitdiffstats
path: root/libavutil/internal.h
diff options
context:
space:
mode:
authorMichael Niedermayer <michael@niedermayer.cc>2015-11-15 02:35:02 +0100
committerMichael Niedermayer <michael@niedermayer.cc>2015-11-15 03:47:09 +0100
commit5c3dee7dadf8f3f554648bdce30844d5958e3572 (patch)
tree05cb85e9690d275f80013260c4bb005157a396bf /libavutil/internal.h
parent08b520636e96ba6888b669b9b3f4c414631ea1d2 (diff)
downloadffmpeg-5c3dee7dadf8f3f554648bdce30844d5958e3572.tar.gz
avutil: Move av_rint64_clip_* to internal.h
The function is renamed to ff_rint64_clip() This should avoid build failures on VS2012 Feel free to changes this to a different solution Reviewed-by: James Almer <jamrial@gmail.com> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
Diffstat (limited to 'libavutil/internal.h')
-rw-r--r--libavutil/internal.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/libavutil/internal.h b/libavutil/internal.h
index 5c2cd99dcc..8d2d8759bf 100644
--- a/libavutil/internal.h
+++ b/libavutil/internal.h
@@ -257,6 +257,43 @@ void avpriv_request_sample(void *avc,
#endif
/**
+ * 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 ff_rint64_clip(double a, int64_t amin, int64_t amax)
+{
+ int64_t res;
+#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
+ // do range checks first
+ if (a >= 9223372036854775808.0)
+ return amax;
+ if (a <= -9223372036854775808.0)
+ return amin;
+
+ // safe to call llrint and clip accordingly
+ res = llrint(a);
+ if (res > amax)
+ return amax;
+ if (res < amin)
+ return amin;
+ return res;
+}
+
+
+/**
* A wrapper for open() setting O_CLOEXEC.
*/
av_warn_unused_result