MPD 0.17~git
src/pcm_utils.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2011 The Music Player Daemon Project
00003  * http://www.musicpd.org
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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