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
00070 extern const uint8_t av_reverse[256];
00071
00072 static av_always_inline av_const int av_log2_c(unsigned int v)
00073 {
00074 int n = 0;
00075 if (v & 0xffff0000) {
00076 v >>= 16;
00077 n += 16;
00078 }
00079 if (v & 0xff00) {
00080 v >>= 8;
00081 n += 8;
00082 }
00083 n += ff_log2_tab[v];
00084
00085 return n;
00086 }
00087
00088 static av_always_inline av_const int av_log2_16bit_c(unsigned int v)
00089 {
00090 int n = 0;
00091 if (v & 0xff00) {
00092 v >>= 8;
00093 n += 8;
00094 }
00095 n += ff_log2_tab[v];
00096
00097 return n;
00098 }
00099
00100 #ifdef HAVE_AV_CONFIG_H
00101 # include "config.h"
00102 # include "intmath.h"
00103 #endif
00104
00105
00106 #include "common.h"
00107
00115 static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
00116 {
00117 if (a < amin) return amin;
00118 else if (a > amax) return amax;
00119 else return a;
00120 }
00121
00127 static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
00128 {
00129 if (a&(~0xFF)) return (-a)>>31;
00130 else return a;
00131 }
00132
00138 static av_always_inline av_const int8_t av_clip_int8_c(int a)
00139 {
00140 if ((a+0x80) & ~0xFF) return (a>>31) ^ 0x7F;
00141 else return a;
00142 }
00143
00149 static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
00150 {
00151 if (a&(~0xFFFF)) return (-a)>>31;
00152 else return a;
00153 }
00154
00160 static av_always_inline av_const int16_t av_clip_int16_c(int a)
00161 {
00162 if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
00163 else return a;
00164 }
00165
00171 static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
00172 {
00173 if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF;
00174 else return (int32_t)a;
00175 }
00176
00183 static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
00184 {
00185 if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1);
00186 else return a;
00187 }
00188
00196 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
00197 {
00198 if (a < amin) return amin;
00199 else if (a > amax) return amax;
00200 else return a;
00201 }
00202
00207 static av_always_inline av_const int av_ceil_log2_c(int x)
00208 {
00209 return av_log2((x - 1) << 1);
00210 }
00211
00217 static av_always_inline av_const int av_popcount_c(uint32_t x)
00218 {
00219 x -= (x >> 1) & 0x55555555;
00220 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
00221 x = (x + (x >> 4)) & 0x0F0F0F0F;
00222 x += x >> 8;
00223 return (x + (x >> 16)) & 0x3F;
00224 }
00225
00231 static av_always_inline av_const int av_popcount64_c(uint64_t x)
00232 {
00233 return av_popcount((uint32_t)x) + av_popcount(x >> 32);
00234 }
00235
00236 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
00237 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
00238
00250 #define GET_UTF8(val, GET_BYTE, ERROR)\
00251 val= GET_BYTE;\
00252 {\
00253 int ones= 7 - av_log2(val ^ 255);\
00254 if(ones==1)\
00255 ERROR\
00256 val&= 127>>ones;\
00257 while(--ones > 0){\
00258 int tmp= GET_BYTE - 128;\
00259 if(tmp>>6)\
00260 ERROR\
00261 val= (val<<6) + tmp;\
00262 }\
00263 }
00264
00274 #define GET_UTF16(val, GET_16BIT, ERROR)\
00275 val = GET_16BIT;\
00276 {\
00277 unsigned int hi = val - 0xD800;\
00278 if (hi < 0x800) {\
00279 val = GET_16BIT - 0xDC00;\
00280 if (val > 0x3FFU || hi > 0x3FFU)\
00281 ERROR\
00282 val += (hi<<10) + 0x10000;\
00283 }\
00284 }\
00285
00286
00302 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00303 {\
00304 int bytes, shift;\
00305 uint32_t in = val;\
00306 if (in < 0x80) {\
00307 tmp = in;\
00308 PUT_BYTE\
00309 } else {\
00310 bytes = (av_log2(in) + 4) / 5;\
00311 shift = (bytes - 1) * 6;\
00312 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00313 PUT_BYTE\
00314 while (shift >= 6) {\
00315 shift -= 6;\
00316 tmp = 0x80 | ((in >> shift) & 0x3f);\
00317 PUT_BYTE\
00318 }\
00319 }\
00320 }
00321
00336 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00337 {\
00338 uint32_t in = val;\
00339 if (in < 0x10000) {\
00340 tmp = in;\
00341 PUT_16BIT\
00342 } else {\
00343 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00344 PUT_16BIT\
00345 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00346 PUT_16BIT\
00347 }\
00348 }\
00349
00350
00351
00352 #include "mem.h"
00353
00354 #ifdef HAVE_AV_CONFIG_H
00355 # include "internal.h"
00356 #endif
00357
00358 #endif
00359
00360
00361
00362
00363
00364
00365 #ifndef av_log2
00366 # define av_log2 av_log2_c
00367 #endif
00368 #ifndef av_log2_16bit
00369 # define av_log2_16bit av_log2_16bit_c
00370 #endif
00371 #ifndef av_ceil_log2
00372 # define av_ceil_log2 av_ceil_log2_c
00373 #endif
00374 #ifndef av_clip
00375 # define av_clip av_clip_c
00376 #endif
00377 #ifndef av_clip_uint8
00378 # define av_clip_uint8 av_clip_uint8_c
00379 #endif
00380 #ifndef av_clip_int8
00381 # define av_clip_int8 av_clip_int8_c
00382 #endif
00383 #ifndef av_clip_uint16
00384 # define av_clip_uint16 av_clip_uint16_c
00385 #endif
00386 #ifndef av_clip_int16
00387 # define av_clip_int16 av_clip_int16_c
00388 #endif
00389 #ifndef av_clipl_int32
00390 # define av_clipl_int32 av_clipl_int32_c
00391 #endif
00392 #ifndef av_clip_uintp2
00393 # define av_clip_uintp2 av_clip_uintp2_c
00394 #endif
00395 #ifndef av_clipf
00396 # define av_clipf av_clipf_c
00397 #endif
00398 #ifndef av_popcount
00399 # define av_popcount av_popcount_c
00400 #endif
00401 #ifndef av_popcount64
00402 # define av_popcount64 av_popcount64_c
00403 #endif