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
00228 static av_always_inline av_const int av_popcount64_c(uint64_t x)
00229 {
00230 return av_popcount(x) + av_popcount(x >> 32);
00231 }
00232
00233 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
00234 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
00235
00247 #define GET_UTF8(val, GET_BYTE, ERROR)\
00248 val= GET_BYTE;\
00249 {\
00250 int ones= 7 - av_log2(val ^ 255);\
00251 if(ones==1)\
00252 ERROR\
00253 val&= 127>>ones;\
00254 while(--ones > 0){\
00255 int tmp= GET_BYTE - 128;\
00256 if(tmp>>6)\
00257 ERROR\
00258 val= (val<<6) + tmp;\
00259 }\
00260 }
00261
00271 #define GET_UTF16(val, GET_16BIT, ERROR)\
00272 val = GET_16BIT;\
00273 {\
00274 unsigned int hi = val - 0xD800;\
00275 if (hi < 0x800) {\
00276 val = GET_16BIT - 0xDC00;\
00277 if (val > 0x3FFU || hi > 0x3FFU)\
00278 ERROR\
00279 val += (hi<<10) + 0x10000;\
00280 }\
00281 }\
00282
00283
00299 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00300 {\
00301 int bytes, shift;\
00302 uint32_t in = val;\
00303 if (in < 0x80) {\
00304 tmp = in;\
00305 PUT_BYTE\
00306 } else {\
00307 bytes = (av_log2(in) + 4) / 5;\
00308 shift = (bytes - 1) * 6;\
00309 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00310 PUT_BYTE\
00311 while (shift >= 6) {\
00312 shift -= 6;\
00313 tmp = 0x80 | ((in >> shift) & 0x3f);\
00314 PUT_BYTE\
00315 }\
00316 }\
00317 }
00318
00333 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00334 {\
00335 uint32_t in = val;\
00336 if (in < 0x10000) {\
00337 tmp = in;\
00338 PUT_16BIT\
00339 } else {\
00340 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00341 PUT_16BIT\
00342 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00343 PUT_16BIT\
00344 }\
00345 }\
00346
00347
00348
00349 #include "mem.h"
00350
00351 #ifdef HAVE_AV_CONFIG_H
00352 # include "internal.h"
00353 #endif
00354
00355 #endif
00356
00357
00358
00359
00360
00361
00362 #ifndef av_log2
00363 # define av_log2 av_log2_c
00364 #endif
00365 #ifndef av_log2_16bit
00366 # define av_log2_16bit av_log2_16bit_c
00367 #endif
00368 #ifndef av_ceil_log2
00369 # define av_ceil_log2 av_ceil_log2_c
00370 #endif
00371 #ifndef av_clip
00372 # define av_clip av_clip_c
00373 #endif
00374 #ifndef av_clip_uint8
00375 # define av_clip_uint8 av_clip_uint8_c
00376 #endif
00377 #ifndef av_clip_int8
00378 # define av_clip_int8 av_clip_int8_c
00379 #endif
00380 #ifndef av_clip_uint16
00381 # define av_clip_uint16 av_clip_uint16_c
00382 #endif
00383 #ifndef av_clip_int16
00384 # define av_clip_int16 av_clip_int16_c
00385 #endif
00386 #ifndef av_clipl_int32
00387 # define av_clipl_int32 av_clipl_int32_c
00388 #endif
00389 #ifndef av_clip_uintp2
00390 # define av_clip_uintp2 av_clip_uintp2_c
00391 #endif
00392 #ifndef av_clipf
00393 # define av_clipf av_clipf_c
00394 #endif
00395 #ifndef av_popcount
00396 # define av_popcount av_popcount_c
00397 #endif
00398 #ifndef av_popcount64
00399 # define av_popcount64 av_popcount64_c
00400 #endif