Libav 0.7.1
|
00001 /* 00002 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> 00003 * 00004 * This file is part of Libav. 00005 * 00006 * Libav is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * Libav is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with Libav; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00026 #ifndef AVUTIL_COMMON_H 00027 #define AVUTIL_COMMON_H 00028 00029 #include <ctype.h> 00030 #include <errno.h> 00031 #include <inttypes.h> 00032 #include <limits.h> 00033 #include <math.h> 00034 #include <stdio.h> 00035 #include <stdlib.h> 00036 #include <string.h> 00037 #include "attributes.h" 00038 #include "libavutil/avconfig.h" 00039 00040 #if AV_HAVE_BIGENDIAN 00041 # define AV_NE(be, le) (be) 00042 #else 00043 # define AV_NE(be, le) (le) 00044 #endif 00045 00046 //rounded division & shift 00047 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b)) 00048 /* assume b>0 */ 00049 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) 00050 #define FFABS(a) ((a) >= 0 ? (a) : (-(a))) 00051 #define FFSIGN(a) ((a) > 0 ? 1 : -1) 00052 00053 #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) 00054 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c) 00055 #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) 00056 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c) 00057 00058 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0) 00059 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0])) 00060 #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1)) 00061 00062 /* misc math functions */ 00063 extern const uint8_t ff_log2_tab[256]; 00064 00065 extern const uint8_t av_reverse[256]; 00066 00067 static av_always_inline av_const int av_log2_c(unsigned int v) 00068 { 00069 int n = 0; 00070 if (v & 0xffff0000) { 00071 v >>= 16; 00072 n += 16; 00073 } 00074 if (v & 0xff00) { 00075 v >>= 8; 00076 n += 8; 00077 } 00078 n += ff_log2_tab[v]; 00079 00080 return n; 00081 } 00082 00083 static av_always_inline av_const int av_log2_16bit_c(unsigned int v) 00084 { 00085 int n = 0; 00086 if (v & 0xff00) { 00087 v >>= 8; 00088 n += 8; 00089 } 00090 n += ff_log2_tab[v]; 00091 00092 return n; 00093 } 00094 00095 #ifdef HAVE_AV_CONFIG_H 00096 # include "config.h" 00097 # include "intmath.h" 00098 #endif 00099 00100 /* Pull in unguarded fallback defines at the end of this file. */ 00101 #include "common.h" 00102 00110 static av_always_inline av_const int av_clip_c(int a, int amin, int amax) 00111 { 00112 if (a < amin) return amin; 00113 else if (a > amax) return amax; 00114 else return a; 00115 } 00116 00122 static av_always_inline av_const uint8_t av_clip_uint8_c(int a) 00123 { 00124 if (a&(~0xFF)) return (-a)>>31; 00125 else return a; 00126 } 00127 00133 static av_always_inline av_const int8_t av_clip_int8_c(int a) 00134 { 00135 if ((a+0x80) & ~0xFF) return (a>>31) ^ 0x7F; 00136 else return a; 00137 } 00138 00144 static av_always_inline av_const uint16_t av_clip_uint16_c(int a) 00145 { 00146 if (a&(~0xFFFF)) return (-a)>>31; 00147 else return a; 00148 } 00149 00155 static av_always_inline av_const int16_t av_clip_int16_c(int a) 00156 { 00157 if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF; 00158 else return a; 00159 } 00160 00166 static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a) 00167 { 00168 if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF; 00169 else return a; 00170 } 00171 00178 static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p) 00179 { 00180 if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1); 00181 else return a; 00182 } 00183 00191 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax) 00192 { 00193 if (a < amin) return amin; 00194 else if (a > amax) return amax; 00195 else return a; 00196 } 00197 00202 static av_always_inline av_const int av_ceil_log2_c(int x) 00203 { 00204 return av_log2((x - 1) << 1); 00205 } 00206 00212 static av_always_inline av_const int av_popcount_c(uint32_t x) 00213 { 00214 x -= (x >> 1) & 0x55555555; 00215 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); 00216 x = (x + (x >> 4)) & 0x0F0F0F0F; 00217 x += x >> 8; 00218 return (x + (x >> 16)) & 0x3F; 00219 } 00220 00221 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24)) 00222 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((a) << 24)) 00223 00235 #define GET_UTF8(val, GET_BYTE, ERROR)\ 00236 val= GET_BYTE;\ 00237 {\ 00238 int ones= 7 - av_log2(val ^ 255);\ 00239 if(ones==1)\ 00240 ERROR\ 00241 val&= 127>>ones;\ 00242 while(--ones > 0){\ 00243 int tmp= GET_BYTE - 128;\ 00244 if(tmp>>6)\ 00245 ERROR\ 00246 val= (val<<6) + tmp;\ 00247 }\ 00248 } 00249 00259 #define GET_UTF16(val, GET_16BIT, ERROR)\ 00260 val = GET_16BIT;\ 00261 {\ 00262 unsigned int hi = val - 0xD800;\ 00263 if (hi < 0x800) {\ 00264 val = GET_16BIT - 0xDC00;\ 00265 if (val > 0x3FFU || hi > 0x3FFU)\ 00266 ERROR\ 00267 val += (hi<<10) + 0x10000;\ 00268 }\ 00269 }\ 00270 00271 00287 #define PUT_UTF8(val, tmp, PUT_BYTE)\ 00288 {\ 00289 int bytes, shift;\ 00290 uint32_t in = val;\ 00291 if (in < 0x80) {\ 00292 tmp = in;\ 00293 PUT_BYTE\ 00294 } else {\ 00295 bytes = (av_log2(in) + 4) / 5;\ 00296 shift = (bytes - 1) * 6;\ 00297 tmp = (256 - (256 >> bytes)) | (in >> shift);\ 00298 PUT_BYTE\ 00299 while (shift >= 6) {\ 00300 shift -= 6;\ 00301 tmp = 0x80 | ((in >> shift) & 0x3f);\ 00302 PUT_BYTE\ 00303 }\ 00304 }\ 00305 } 00306 00321 #define PUT_UTF16(val, tmp, PUT_16BIT)\ 00322 {\ 00323 uint32_t in = val;\ 00324 if (in < 0x10000) {\ 00325 tmp = in;\ 00326 PUT_16BIT\ 00327 } else {\ 00328 tmp = 0xD800 | ((in - 0x10000) >> 10);\ 00329 PUT_16BIT\ 00330 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\ 00331 PUT_16BIT\ 00332 }\ 00333 }\ 00334 00335 00336 00337 #include "mem.h" 00338 00339 #ifdef HAVE_AV_CONFIG_H 00340 # include "internal.h" 00341 #endif /* HAVE_AV_CONFIG_H */ 00342 00343 #endif /* AVUTIL_COMMON_H */ 00344 00345 /* 00346 * The following definitions are outside the multiple inclusion guard 00347 * to ensure they are immediately available in intmath.h. 00348 */ 00349 00350 #ifndef av_log2 00351 # define av_log2 av_log2_c 00352 #endif 00353 #ifndef av_log2_16bit 00354 # define av_log2_16bit av_log2_16bit_c 00355 #endif 00356 #ifndef av_ceil_log2 00357 # define av_ceil_log2 av_ceil_log2_c 00358 #endif 00359 #ifndef av_clip 00360 # define av_clip av_clip_c 00361 #endif 00362 #ifndef av_clip_uint8 00363 # define av_clip_uint8 av_clip_uint8_c 00364 #endif 00365 #ifndef av_clip_int8 00366 # define av_clip_int8 av_clip_int8_c 00367 #endif 00368 #ifndef av_clip_uint16 00369 # define av_clip_uint16 av_clip_uint16_c 00370 #endif 00371 #ifndef av_clip_int16 00372 # define av_clip_int16 av_clip_int16_c 00373 #endif 00374 #ifndef av_clipl_int32 00375 # define av_clipl_int32 av_clipl_int32_c 00376 #endif 00377 #ifndef av_clip_uintp2 00378 # define av_clip_uintp2 av_clip_uintp2_c 00379 #endif 00380 #ifndef av_clipf 00381 # define av_clipf av_clipf_c 00382 #endif 00383 #ifndef av_popcount 00384 # define av_popcount av_popcount_c 00385 #endif