00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef MPD_PCM_UTILS_H
00021 #define MPD_PCM_UTILS_H
00022
00023 #include <glib.h>
00024
00025 #include <stdint.h>
00026
00032 static inline const void *
00033 pcm_end_pointer(const void *p, size_t size)
00034 {
00035 return (const char *)p + size;
00036 }
00037
00042 static inline int32_t
00043 pcm_range(int32_t sample, unsigned bits)
00044 {
00045 if (G_UNLIKELY(sample < (-1 << (bits - 1))))
00046 return -1 << (bits - 1);
00047 if (G_UNLIKELY(sample >= (1 << (bits - 1))))
00048 return (1 << (bits - 1)) - 1;
00049 return sample;
00050 }
00051
00056 static inline int64_t
00057 pcm_range_64(int64_t sample, unsigned bits)
00058 {
00059 if (G_UNLIKELY(sample < ((int64_t)-1 << (bits - 1))))
00060 return (int64_t)-1 << (bits - 1);
00061 if (G_UNLIKELY(sample >= ((int64_t)1 << (bits - 1))))
00062 return ((int64_t)1 << (bits - 1)) - 1;
00063 return sample;
00064 }
00065
00066 G_GNUC_CONST
00067 static inline int16_t
00068 pcm_clamp_16(int x)
00069 {
00070 static const int32_t MIN_VALUE = G_MININT16;
00071 static const int32_t MAX_VALUE = G_MAXINT16;
00072
00073 if (G_UNLIKELY(x < MIN_VALUE))
00074 return MIN_VALUE;
00075 if (G_UNLIKELY(x > MAX_VALUE))
00076 return MAX_VALUE;
00077 return x;
00078 }
00079
00080 G_GNUC_CONST
00081 static inline int32_t
00082 pcm_clamp_24(int x)
00083 {
00084 static const int32_t MIN_VALUE = -(1 << 23);
00085 static const int32_t MAX_VALUE = (1 << 23) - 1;
00086
00087 if (G_UNLIKELY(x < MIN_VALUE))
00088 return MIN_VALUE;
00089 if (G_UNLIKELY(x > MAX_VALUE))
00090 return MAX_VALUE;
00091 return x;
00092 }
00093
00094 #endif