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
00038 #include "attributes.h"
00039 #include "version.h"
00040 #include "libavutil/avconfig.h"
00041
00042 #if AV_HAVE_BIGENDIAN
00043 # define AV_NE(be, le) (be)
00044 #else
00045 # define AV_NE(be, le) (le)
00046 #endif
00047
00048
00049 #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
00050
00051 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
00052 #define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
00053 #define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
00054 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
00055 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
00056
00057 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
00058 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
00059 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
00060 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
00061
00062 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
00063 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
00064 #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
00065
00066
00067
00071 #if FF_API_AV_REVERSE
00072 extern attribute_deprecated const uint8_t av_reverse[256];
00073 #endif
00074
00075 #ifdef HAVE_AV_CONFIG_H
00076 # include "config.h"
00077 # include "intmath.h"
00078 #endif
00079
00080
00081 #include "common.h"
00082
00083 #ifndef av_log2
00084 av_const int av_log2(unsigned v);
00085 #endif
00086
00087 #ifndef av_log2_16bit
00088 av_const int av_log2_16bit(unsigned v);
00089 #endif
00090
00098 static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
00099 {
00100 if (a < amin) return amin;
00101 else if (a > amax) return amax;
00102 else return a;
00103 }
00104
00112 static av_always_inline av_const int64_t av_clip64_c(int64_t a, int64_t amin, int64_t 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 (int32_t)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 int av_sat_add32_c(int a, int b)
00194 {
00195 return av_clipl_int32((int64_t)a + b);
00196 }
00197
00205 static av_always_inline int av_sat_dadd32_c(int a, int b)
00206 {
00207 return av_sat_add32(a, av_sat_add32(b, b));
00208 }
00209
00217 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
00218 {
00219 if (a < amin) return amin;
00220 else if (a > amax) return amax;
00221 else return a;
00222 }
00223
00228 static av_always_inline av_const int av_ceil_log2_c(int x)
00229 {
00230 return av_log2((x - 1) << 1);
00231 }
00232
00238 static av_always_inline av_const int av_popcount_c(uint32_t x)
00239 {
00240 x -= (x >> 1) & 0x55555555;
00241 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
00242 x = (x + (x >> 4)) & 0x0F0F0F0F;
00243 x += x >> 8;
00244 return (x + (x >> 16)) & 0x3F;
00245 }
00246
00252 static av_always_inline av_const int av_popcount64_c(uint64_t x)
00253 {
00254 return av_popcount((uint32_t)x) + av_popcount(x >> 32);
00255 }
00256
00257 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
00258 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
00259
00271 #define GET_UTF8(val, GET_BYTE, ERROR)\
00272 val= GET_BYTE;\
00273 {\
00274 uint32_t top = (val & 128) >> 1;\
00275 if ((val & 0xc0) == 0x80)\
00276 ERROR\
00277 while (val & top) {\
00278 int tmp= GET_BYTE - 128;\
00279 if(tmp>>6)\
00280 ERROR\
00281 val= (val<<6) + tmp;\
00282 top <<= 5;\
00283 }\
00284 val &= (top << 1) - 1;\
00285 }
00286
00296 #define GET_UTF16(val, GET_16BIT, ERROR)\
00297 val = GET_16BIT;\
00298 {\
00299 unsigned int hi = val - 0xD800;\
00300 if (hi < 0x800) {\
00301 val = GET_16BIT - 0xDC00;\
00302 if (val > 0x3FFU || hi > 0x3FFU)\
00303 ERROR\
00304 val += (hi<<10) + 0x10000;\
00305 }\
00306 }\
00307
00308
00324 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00325 {\
00326 int bytes, shift;\
00327 uint32_t in = val;\
00328 if (in < 0x80) {\
00329 tmp = in;\
00330 PUT_BYTE\
00331 } else {\
00332 bytes = (av_log2(in) + 4) / 5;\
00333 shift = (bytes - 1) * 6;\
00334 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00335 PUT_BYTE\
00336 while (shift >= 6) {\
00337 shift -= 6;\
00338 tmp = 0x80 | ((in >> shift) & 0x3f);\
00339 PUT_BYTE\
00340 }\
00341 }\
00342 }
00343
00358 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00359 {\
00360 uint32_t in = val;\
00361 if (in < 0x10000) {\
00362 tmp = in;\
00363 PUT_16BIT\
00364 } else {\
00365 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00366 PUT_16BIT\
00367 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00368 PUT_16BIT\
00369 }\
00370 }\
00371
00372
00373
00374 #include "mem.h"
00375
00376 #ifdef HAVE_AV_CONFIG_H
00377 # include "internal.h"
00378 #endif
00379
00380 #endif
00381
00382
00383
00384
00385
00386
00387 #ifndef av_ceil_log2
00388 # define av_ceil_log2 av_ceil_log2_c
00389 #endif
00390 #ifndef av_clip
00391 # define av_clip av_clip_c
00392 #endif
00393 #ifndef av_clip64
00394 # define av_clip64 av_clip64_c
00395 #endif
00396 #ifndef av_clip_uint8
00397 # define av_clip_uint8 av_clip_uint8_c
00398 #endif
00399 #ifndef av_clip_int8
00400 # define av_clip_int8 av_clip_int8_c
00401 #endif
00402 #ifndef av_clip_uint16
00403 # define av_clip_uint16 av_clip_uint16_c
00404 #endif
00405 #ifndef av_clip_int16
00406 # define av_clip_int16 av_clip_int16_c
00407 #endif
00408 #ifndef av_clipl_int32
00409 # define av_clipl_int32 av_clipl_int32_c
00410 #endif
00411 #ifndef av_clip_uintp2
00412 # define av_clip_uintp2 av_clip_uintp2_c
00413 #endif
00414 #ifndef av_sat_add32
00415 # define av_sat_add32 av_sat_add32_c
00416 #endif
00417 #ifndef av_sat_dadd32
00418 # define av_sat_dadd32 av_sat_dadd32_c
00419 #endif
00420 #ifndef av_clipf
00421 # define av_clipf av_clipf_c
00422 #endif
00423 #ifndef av_popcount
00424 # define av_popcount av_popcount_c
00425 #endif
00426 #ifndef av_popcount64
00427 # define av_popcount64 av_popcount64_c
00428 #endif