Libav
|
00001 /* 00002 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 00039 //rounded division & shift 00040 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b)) 00041 /* assume b>0 */ 00042 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b)) 00043 #define FFABS(a) ((a) >= 0 ? (a) : (-(a))) 00044 #define FFSIGN(a) ((a) > 0 ? 1 : -1) 00045 00046 #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) 00047 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c) 00048 #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) 00049 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c) 00050 00051 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0) 00052 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0])) 00053 #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1)) 00054 00055 /* misc math functions */ 00056 extern const uint8_t ff_log2_tab[256]; 00057 00058 extern const uint8_t av_reverse[256]; 00059 00060 static inline av_const int av_log2_c(unsigned int v) 00061 { 00062 int n = 0; 00063 if (v & 0xffff0000) { 00064 v >>= 16; 00065 n += 16; 00066 } 00067 if (v & 0xff00) { 00068 v >>= 8; 00069 n += 8; 00070 } 00071 n += ff_log2_tab[v]; 00072 00073 return n; 00074 } 00075 00076 static inline av_const int av_log2_16bit_c(unsigned int v) 00077 { 00078 int n = 0; 00079 if (v & 0xff00) { 00080 v >>= 8; 00081 n += 8; 00082 } 00083 n += ff_log2_tab[v]; 00084 00085 return n; 00086 } 00087 00088 #ifdef HAVE_AV_CONFIG_H 00089 # include "config.h" 00090 # include "intmath.h" 00091 #endif 00092 00093 #ifndef av_log2 00094 # define av_log2 av_log2_c 00095 #endif 00096 #ifndef av_log2_16bit 00097 # define av_log2_16bit av_log2_16bit_c 00098 #endif 00099 00107 static inline av_const int av_clip(int a, int amin, int amax) 00108 { 00109 if (a < amin) return amin; 00110 else if (a > amax) return amax; 00111 else return a; 00112 } 00113 00119 static inline av_const uint8_t av_clip_uint8(int a) 00120 { 00121 if (a&(~0xFF)) return (-a)>>31; 00122 else return a; 00123 } 00124 00130 static inline av_const uint16_t av_clip_uint16(int a) 00131 { 00132 if (a&(~0xFFFF)) return (-a)>>31; 00133 else return a; 00134 } 00135 00141 static inline av_const int16_t av_clip_int16(int a) 00142 { 00143 if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF; 00144 else return a; 00145 } 00146 00152 static inline av_const int32_t av_clipl_int32(int64_t a) 00153 { 00154 if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF; 00155 else return a; 00156 } 00157 00165 static inline av_const float av_clipf(float a, float amin, float amax) 00166 { 00167 if (a < amin) return amin; 00168 else if (a > amax) return amax; 00169 else return a; 00170 } 00171 00176 static inline av_const int av_ceil_log2(int x) 00177 { 00178 return av_log2((x - 1) << 1); 00179 } 00180 00181 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24)) 00182 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24)) 00183 00197 #define GET_UTF8(val, GET_BYTE, ERROR)\ 00198 val= GET_BYTE;\ 00199 {\ 00200 int ones= 7 - av_log2(val ^ 255);\ 00201 if(ones==1)\ 00202 ERROR\ 00203 val&= 127>>ones;\ 00204 while(--ones > 0){\ 00205 int tmp= GET_BYTE - 128;\ 00206 if(tmp>>6)\ 00207 ERROR\ 00208 val= (val<<6) + tmp;\ 00209 }\ 00210 } 00211 00224 #define GET_UTF16(val, GET_16BIT, ERROR)\ 00225 val = GET_16BIT;\ 00226 {\ 00227 unsigned int hi = val - 0xD800;\ 00228 if (hi < 0x800) {\ 00229 val = GET_16BIT - 0xDC00;\ 00230 if (val > 0x3FFU || hi > 0x3FFU)\ 00231 ERROR\ 00232 val += (hi<<10) + 0x10000;\ 00233 }\ 00234 }\ 00235 00236 00252 #define PUT_UTF8(val, tmp, PUT_BYTE)\ 00253 {\ 00254 int bytes, shift;\ 00255 uint32_t in = val;\ 00256 if (in < 0x80) {\ 00257 tmp = in;\ 00258 PUT_BYTE\ 00259 } else {\ 00260 bytes = (av_log2(in) + 4) / 5;\ 00261 shift = (bytes - 1) * 6;\ 00262 tmp = (256 - (256 >> bytes)) | (in >> shift);\ 00263 PUT_BYTE\ 00264 while (shift >= 6) {\ 00265 shift -= 6;\ 00266 tmp = 0x80 | ((in >> shift) & 0x3f);\ 00267 PUT_BYTE\ 00268 }\ 00269 }\ 00270 } 00271 00286 #define PUT_UTF16(val, tmp, PUT_16BIT)\ 00287 {\ 00288 uint32_t in = val;\ 00289 if (in < 0x10000) {\ 00290 tmp = in;\ 00291 PUT_16BIT\ 00292 } else {\ 00293 tmp = 0xD800 | ((in - 0x10000) >> 10);\ 00294 PUT_16BIT\ 00295 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\ 00296 PUT_16BIT\ 00297 }\ 00298 }\ 00299 00300 00301 00302 #include "mem.h" 00303 00304 #ifdef HAVE_AV_CONFIG_H 00305 # include "internal.h" 00306 #endif /* HAVE_AV_CONFIG_H */ 00307 00308 #endif /* AVUTIL_COMMON_H */