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 int av_sat_add32_c(int a, int b)
00197 {
00198 return av_clipl_int32((int64_t)a + b);
00199 }
00200
00208 static av_always_inline int av_sat_dadd32_c(int a, int b)
00209 {
00210 return av_sat_add32(a, av_sat_add32(b, b));
00211 }
00212
00220 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
00221 {
00222 if (a < amin) return amin;
00223 else if (a > amax) return amax;
00224 else return a;
00225 }
00226
00231 static av_always_inline av_const int av_ceil_log2_c(int x)
00232 {
00233 return av_log2((x - 1) << 1);
00234 }
00235
00241 static av_always_inline av_const int av_popcount_c(uint32_t x)
00242 {
00243 x -= (x >> 1) & 0x55555555;
00244 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
00245 x = (x + (x >> 4)) & 0x0F0F0F0F;
00246 x += x >> 8;
00247 return (x + (x >> 16)) & 0x3F;
00248 }
00249
00255 static av_always_inline av_const int av_popcount64_c(uint64_t x)
00256 {
00257 return av_popcount((uint32_t)x) + av_popcount(x >> 32);
00258 }
00259
00260 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
00261 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
00262
00274 #define GET_UTF8(val, GET_BYTE, ERROR)\
00275 val= GET_BYTE;\
00276 {\
00277 int ones= 7 - av_log2(val ^ 255);\
00278 if(ones==1)\
00279 ERROR\
00280 val&= 127>>ones;\
00281 while(--ones > 0){\
00282 int tmp= GET_BYTE - 128;\
00283 if(tmp>>6)\
00284 ERROR\
00285 val= (val<<6) + tmp;\
00286 }\
00287 }
00288
00298 #define GET_UTF16(val, GET_16BIT, ERROR)\
00299 val = GET_16BIT;\
00300 {\
00301 unsigned int hi = val - 0xD800;\
00302 if (hi < 0x800) {\
00303 val = GET_16BIT - 0xDC00;\
00304 if (val > 0x3FFU || hi > 0x3FFU)\
00305 ERROR\
00306 val += (hi<<10) + 0x10000;\
00307 }\
00308 }\
00309
00310
00326 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00327 {\
00328 int bytes, shift;\
00329 uint32_t in = val;\
00330 if (in < 0x80) {\
00331 tmp = in;\
00332 PUT_BYTE\
00333 } else {\
00334 bytes = (av_log2(in) + 4) / 5;\
00335 shift = (bytes - 1) * 6;\
00336 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00337 PUT_BYTE\
00338 while (shift >= 6) {\
00339 shift -= 6;\
00340 tmp = 0x80 | ((in >> shift) & 0x3f);\
00341 PUT_BYTE\
00342 }\
00343 }\
00344 }
00345
00360 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00361 {\
00362 uint32_t in = val;\
00363 if (in < 0x10000) {\
00364 tmp = in;\
00365 PUT_16BIT\
00366 } else {\
00367 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00368 PUT_16BIT\
00369 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00370 PUT_16BIT\
00371 }\
00372 }\
00373
00374
00375
00376 #include "mem.h"
00377
00378 #ifdef HAVE_AV_CONFIG_H
00379 # include "internal.h"
00380 #endif
00381
00382 #endif
00383
00384
00385
00386
00387
00388
00389 #ifndef av_log2
00390 # define av_log2 av_log2_c
00391 #endif
00392 #ifndef av_log2_16bit
00393 # define av_log2_16bit av_log2_16bit_c
00394 #endif
00395 #ifndef av_ceil_log2
00396 # define av_ceil_log2 av_ceil_log2_c
00397 #endif
00398 #ifndef av_clip
00399 # define av_clip av_clip_c
00400 #endif
00401 #ifndef av_clip_uint8
00402 # define av_clip_uint8 av_clip_uint8_c
00403 #endif
00404 #ifndef av_clip_int8
00405 # define av_clip_int8 av_clip_int8_c
00406 #endif
00407 #ifndef av_clip_uint16
00408 # define av_clip_uint16 av_clip_uint16_c
00409 #endif
00410 #ifndef av_clip_int16
00411 # define av_clip_int16 av_clip_int16_c
00412 #endif
00413 #ifndef av_clipl_int32
00414 # define av_clipl_int32 av_clipl_int32_c
00415 #endif
00416 #ifndef av_clip_uintp2
00417 # define av_clip_uintp2 av_clip_uintp2_c
00418 #endif
00419 #ifndef av_sat_add32
00420 # define av_sat_add32 av_sat_add32_c
00421 #endif
00422 #ifndef av_sat_dadd32
00423 # define av_sat_dadd32 av_sat_dadd32_c
00424 #endif
00425 #ifndef av_clipf
00426 # define av_clipf av_clipf_c
00427 #endif
00428 #ifndef av_popcount
00429 # define av_popcount av_popcount_c
00430 #endif
00431 #ifndef av_popcount64
00432 # define av_popcount64 av_popcount64_c
00433 #endif