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 #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
00047 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
00048
00049 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
00050 #define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
00051 #define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
00052 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
00053 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
00054
00055 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
00056 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
00057 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
00058 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
00059
00060 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
00061 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
00062 #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
00063
00064
00065 extern const uint8_t ff_log2_tab[256];
00066
00067 extern const uint8_t av_reverse[256];
00068
00069 static av_always_inline av_const int av_log2_c(unsigned int v)
00070 {
00071 int n = 0;
00072 if (v & 0xffff0000) {
00073 v >>= 16;
00074 n += 16;
00075 }
00076 if (v & 0xff00) {
00077 v >>= 8;
00078 n += 8;
00079 }
00080 n += ff_log2_tab[v];
00081
00082 return n;
00083 }
00084
00085 static av_always_inline av_const int av_log2_16bit_c(unsigned int v)
00086 {
00087 int n = 0;
00088 if (v & 0xff00) {
00089 v >>= 8;
00090 n += 8;
00091 }
00092 n += ff_log2_tab[v];
00093
00094 return n;
00095 }
00096
00097 #ifdef HAVE_AV_CONFIG_H
00098 # include "config.h"
00099 # include "intmath.h"
00100 #endif
00101
00102
00103 #include "common.h"
00104
00112 static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
00113 {
00114 if (a < amin) return amin;
00115 else if (a > amax) return amax;
00116 else return a;
00117 }
00118
00124 static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
00125 {
00126 if (a&(~0xFF)) return (-a)>>31;
00127 else return a;
00128 }
00129
00135 static av_always_inline av_const int8_t av_clip_int8_c(int a)
00136 {
00137 if ((a+0x80) & ~0xFF) return (a>>31) ^ 0x7F;
00138 else return a;
00139 }
00140
00146 static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
00147 {
00148 if (a&(~0xFFFF)) return (-a)>>31;
00149 else return a;
00150 }
00151
00157 static av_always_inline av_const int16_t av_clip_int16_c(int a)
00158 {
00159 if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
00160 else return a;
00161 }
00162
00168 static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
00169 {
00170 if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF;
00171 else return a;
00172 }
00173
00180 static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
00181 {
00182 if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1);
00183 else return a;
00184 }
00185
00193 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
00194 {
00195 if (a < amin) return amin;
00196 else if (a > amax) return amax;
00197 else return a;
00198 }
00199
00204 static av_always_inline av_const int av_ceil_log2_c(int x)
00205 {
00206 return av_log2((x - 1) << 1);
00207 }
00208
00214 static av_always_inline av_const int av_popcount_c(uint32_t x)
00215 {
00216 x -= (x >> 1) & 0x55555555;
00217 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
00218 x = (x + (x >> 4)) & 0x0F0F0F0F;
00219 x += x >> 8;
00220 return (x + (x >> 16)) & 0x3F;
00221 }
00222
00223 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
00224 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
00225
00237 #define GET_UTF8(val, GET_BYTE, ERROR)\
00238 val= GET_BYTE;\
00239 {\
00240 int ones= 7 - av_log2(val ^ 255);\
00241 if(ones==1)\
00242 ERROR\
00243 val&= 127>>ones;\
00244 while(--ones > 0){\
00245 int tmp= GET_BYTE - 128;\
00246 if(tmp>>6)\
00247 ERROR\
00248 val= (val<<6) + tmp;\
00249 }\
00250 }
00251
00261 #define GET_UTF16(val, GET_16BIT, ERROR)\
00262 val = GET_16BIT;\
00263 {\
00264 unsigned int hi = val - 0xD800;\
00265 if (hi < 0x800) {\
00266 val = GET_16BIT - 0xDC00;\
00267 if (val > 0x3FFU || hi > 0x3FFU)\
00268 ERROR\
00269 val += (hi<<10) + 0x10000;\
00270 }\
00271 }\
00272
00273
00289 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00290 {\
00291 int bytes, shift;\
00292 uint32_t in = val;\
00293 if (in < 0x80) {\
00294 tmp = in;\
00295 PUT_BYTE\
00296 } else {\
00297 bytes = (av_log2(in) + 4) / 5;\
00298 shift = (bytes - 1) * 6;\
00299 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00300 PUT_BYTE\
00301 while (shift >= 6) {\
00302 shift -= 6;\
00303 tmp = 0x80 | ((in >> shift) & 0x3f);\
00304 PUT_BYTE\
00305 }\
00306 }\
00307 }
00308
00323 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00324 {\
00325 uint32_t in = val;\
00326 if (in < 0x10000) {\
00327 tmp = in;\
00328 PUT_16BIT\
00329 } else {\
00330 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00331 PUT_16BIT\
00332 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00333 PUT_16BIT\
00334 }\
00335 }\
00336
00337
00338
00339 #include "mem.h"
00340
00341 #ifdef HAVE_AV_CONFIG_H
00342 # include "internal.h"
00343 #endif
00344
00345 #endif
00346
00347
00348
00349
00350
00351
00352 #ifndef av_log2
00353 # define av_log2 av_log2_c
00354 #endif
00355 #ifndef av_log2_16bit
00356 # define av_log2_16bit av_log2_16bit_c
00357 #endif
00358 #ifndef av_ceil_log2
00359 # define av_ceil_log2 av_ceil_log2_c
00360 #endif
00361 #ifndef av_clip
00362 # define av_clip av_clip_c
00363 #endif
00364 #ifndef av_clip_uint8
00365 # define av_clip_uint8 av_clip_uint8_c
00366 #endif
00367 #ifndef av_clip_int8
00368 # define av_clip_int8 av_clip_int8_c
00369 #endif
00370 #ifndef av_clip_uint16
00371 # define av_clip_uint16 av_clip_uint16_c
00372 #endif
00373 #ifndef av_clip_int16
00374 # define av_clip_int16 av_clip_int16_c
00375 #endif
00376 #ifndef av_clipl_int32
00377 # define av_clipl_int32 av_clipl_int32_c
00378 #endif
00379 #ifndef av_clip_uintp2
00380 # define av_clip_uintp2 av_clip_uintp2_c
00381 #endif
00382 #ifndef av_clipf
00383 # define av_clipf av_clipf_c
00384 #endif
00385 #ifndef av_popcount
00386 # define av_popcount av_popcount_c
00387 #endif