Go to the documentation of this file.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 FFABS(a) ((a) >= 0 ? (a) : (-(a)))
00051 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
00052
00053 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
00054 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
00055 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
00056 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
00057
00058 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
00059 #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
00060 #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
00061
00062
00063 extern const uint8_t ff_log2_tab[256];
00064
00065 extern const uint8_t av_reverse[256];
00066
00067 static av_always_inline av_const int av_log2_c(unsigned int v)
00068 {
00069 int n = 0;
00070 if (v & 0xffff0000) {
00071 v >>= 16;
00072 n += 16;
00073 }
00074 if (v & 0xff00) {
00075 v >>= 8;
00076 n += 8;
00077 }
00078 n += ff_log2_tab[v];
00079
00080 return n;
00081 }
00082
00083 static av_always_inline av_const int av_log2_16bit_c(unsigned int v)
00084 {
00085 int n = 0;
00086 if (v & 0xff00) {
00087 v >>= 8;
00088 n += 8;
00089 }
00090 n += ff_log2_tab[v];
00091
00092 return n;
00093 }
00094
00095 #ifdef HAVE_AV_CONFIG_H
00096 # include "config.h"
00097 # include "intmath.h"
00098 #endif
00099
00100
00101 #include "common.h"
00102
00110 static av_always_inline av_const int av_clip_c(int a, int amin, int amax)
00111 {
00112 if (a < amin) return amin;
00113 else if (a > amax) return amax;
00114 else return a;
00115 }
00116
00122 static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
00123 {
00124 if (a&(~0xFF)) return (-a)>>31;
00125 else return a;
00126 }
00127
00133 static av_always_inline av_const int8_t av_clip_int8_c(int a)
00134 {
00135 if ((a+0x80) & ~0xFF) return (a>>31) ^ 0x7F;
00136 else return a;
00137 }
00138
00144 static av_always_inline av_const uint16_t av_clip_uint16_c(int a)
00145 {
00146 if (a&(~0xFFFF)) return (-a)>>31;
00147 else return a;
00148 }
00149
00155 static av_always_inline av_const int16_t av_clip_int16_c(int a)
00156 {
00157 if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
00158 else return a;
00159 }
00160
00166 static av_always_inline av_const int32_t av_clipl_int32_c(int64_t a)
00167 {
00168 if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF;
00169 else return a;
00170 }
00171
00178 static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
00179 {
00180 if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1);
00181 else return a;
00182 }
00183
00191 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
00192 {
00193 if (a < amin) return amin;
00194 else if (a > amax) return amax;
00195 else return a;
00196 }
00197
00202 static av_always_inline av_const int av_ceil_log2_c(int x)
00203 {
00204 return av_log2((x - 1) << 1);
00205 }
00206
00212 static av_always_inline av_const int av_popcount_c(uint32_t x)
00213 {
00214 x -= (x >> 1) & 0x55555555;
00215 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
00216 x = (x + (x >> 4)) & 0x0F0F0F0F;
00217 x += x >> 8;
00218 return (x + (x >> 16)) & 0x3F;
00219 }
00220
00226 static av_always_inline av_const int av_popcount64_c(uint64_t x)
00227 {
00228 return av_popcount(x) + av_popcount(x >> 32);
00229 }
00230
00231 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((unsigned)(d) << 24))
00232 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((unsigned)(a) << 24))
00233
00245 #define GET_UTF8(val, GET_BYTE, ERROR)\
00246 val= GET_BYTE;\
00247 {\
00248 int ones= 7 - av_log2(val ^ 255);\
00249 if(ones==1)\
00250 ERROR\
00251 val&= 127>>ones;\
00252 while(--ones > 0){\
00253 int tmp= GET_BYTE - 128;\
00254 if(tmp>>6)\
00255 ERROR\
00256 val= (val<<6) + tmp;\
00257 }\
00258 }
00259
00269 #define GET_UTF16(val, GET_16BIT, ERROR)\
00270 val = GET_16BIT;\
00271 {\
00272 unsigned int hi = val - 0xD800;\
00273 if (hi < 0x800) {\
00274 val = GET_16BIT - 0xDC00;\
00275 if (val > 0x3FFU || hi > 0x3FFU)\
00276 ERROR\
00277 val += (hi<<10) + 0x10000;\
00278 }\
00279 }\
00280
00281
00297 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00298 {\
00299 int bytes, shift;\
00300 uint32_t in = val;\
00301 if (in < 0x80) {\
00302 tmp = in;\
00303 PUT_BYTE\
00304 } else {\
00305 bytes = (av_log2(in) + 4) / 5;\
00306 shift = (bytes - 1) * 6;\
00307 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00308 PUT_BYTE\
00309 while (shift >= 6) {\
00310 shift -= 6;\
00311 tmp = 0x80 | ((in >> shift) & 0x3f);\
00312 PUT_BYTE\
00313 }\
00314 }\
00315 }
00316
00331 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00332 {\
00333 uint32_t in = val;\
00334 if (in < 0x10000) {\
00335 tmp = in;\
00336 PUT_16BIT\
00337 } else {\
00338 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00339 PUT_16BIT\
00340 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00341 PUT_16BIT\
00342 }\
00343 }\
00344
00345
00346
00347 #include "mem.h"
00348
00349 #ifdef HAVE_AV_CONFIG_H
00350 # include "internal.h"
00351 #endif
00352
00353 #endif
00354
00355
00356
00357
00358
00359
00360 #ifndef av_log2
00361 # define av_log2 av_log2_c
00362 #endif
00363 #ifndef av_log2_16bit
00364 # define av_log2_16bit av_log2_16bit_c
00365 #endif
00366 #ifndef av_ceil_log2
00367 # define av_ceil_log2 av_ceil_log2_c
00368 #endif
00369 #ifndef av_clip
00370 # define av_clip av_clip_c
00371 #endif
00372 #ifndef av_clip_uint8
00373 # define av_clip_uint8 av_clip_uint8_c
00374 #endif
00375 #ifndef av_clip_int8
00376 # define av_clip_int8 av_clip_int8_c
00377 #endif
00378 #ifndef av_clip_uint16
00379 # define av_clip_uint16 av_clip_uint16_c
00380 #endif
00381 #ifndef av_clip_int16
00382 # define av_clip_int16 av_clip_int16_c
00383 #endif
00384 #ifndef av_clipl_int32
00385 # define av_clipl_int32 av_clipl_int32_c
00386 #endif
00387 #ifndef av_clip_uintp2
00388 # define av_clip_uintp2 av_clip_uintp2_c
00389 #endif
00390 #ifndef av_clipf
00391 # define av_clipf av_clipf_c
00392 #endif
00393 #ifndef av_popcount
00394 # define av_popcount av_popcount_c
00395 #endif
00396 #ifndef av_popcount64
00397 # define av_popcount64 av_popcount64_c
00398 #endif