00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00040 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
00041
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
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&(~255)) 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&(~65535)) 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+32768) & ~65535) return (a>>31) ^ 32767;
00144 else return a;
00145 }
00146
00154 static inline av_const float av_clipf(float a, float amin, float amax)
00155 {
00156 if (a < amin) return amin;
00157 else if (a > amax) return amax;
00158 else return a;
00159 }
00160
00165 static inline av_const int av_ceil_log2(int x)
00166 {
00167 return av_log2((x - 1) << 1);
00168 }
00169
00170 #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
00171 #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
00172
00186 #define GET_UTF8(val, GET_BYTE, ERROR)\
00187 val= GET_BYTE;\
00188 {\
00189 int ones= 7 - av_log2(val ^ 255);\
00190 if(ones==1)\
00191 ERROR\
00192 val&= 127>>ones;\
00193 while(--ones > 0){\
00194 int tmp= GET_BYTE - 128;\
00195 if(tmp>>6)\
00196 ERROR\
00197 val= (val<<6) + tmp;\
00198 }\
00199 }
00200
00213 #define GET_UTF16(val, GET_16BIT, ERROR)\
00214 val = GET_16BIT;\
00215 {\
00216 unsigned int hi = val - 0xD800;\
00217 if (hi < 0x800) {\
00218 val = GET_16BIT - 0xDC00;\
00219 if (val > 0x3FFU || hi > 0x3FFU)\
00220 ERROR\
00221 val += (hi<<10) + 0x10000;\
00222 }\
00223 }\
00224
00225
00241 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00242 {\
00243 int bytes, shift;\
00244 uint32_t in = val;\
00245 if (in < 0x80) {\
00246 tmp = in;\
00247 PUT_BYTE\
00248 } else {\
00249 bytes = (av_log2(in) + 4) / 5;\
00250 shift = (bytes - 1) * 6;\
00251 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00252 PUT_BYTE\
00253 while (shift >= 6) {\
00254 shift -= 6;\
00255 tmp = 0x80 | ((in >> shift) & 0x3f);\
00256 PUT_BYTE\
00257 }\
00258 }\
00259 }
00260
00275 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00276 {\
00277 uint32_t in = val;\
00278 if (in < 0x10000) {\
00279 tmp = in;\
00280 PUT_16BIT\
00281 } else {\
00282 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00283 PUT_16BIT\
00284 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00285 PUT_16BIT\
00286 }\
00287 }\
00288
00289
00290
00291 #include "mem.h"
00292
00293 #ifdef HAVE_AV_CONFIG_H
00294 # include "internal.h"
00295 #endif
00296
00297 #endif