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
00179 static av_always_inline av_const float av_clipf_c(float a, float amin, float amax)
00180 {
00181 if (a < amin) return amin;
00182 else if (a > amax) return amax;
00183 else return a;
00184 }
00185
00190 static av_always_inline av_const int av_ceil_log2_c(int x)
00191 {
00192 return av_log2((x - 1) << 1);
00193 }
00194
00200 static av_always_inline av_const int av_popcount_c(uint32_t x)
00201 {
00202 x -= (x >> 1) & 0x55555555;
00203 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
00204 x = (x + (x >> 4)) & 0x0F0F0F0F;
00205 x += x >> 8;
00206 return (x + (x >> 16)) & 0x3F;
00207 }
00208
00209 #define MKTAG(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
00210 #define MKBETAG(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((a) << 24))
00211
00223 #define GET_UTF8(val, GET_BYTE, ERROR)\
00224 val= GET_BYTE;\
00225 {\
00226 int ones= 7 - av_log2(val ^ 255);\
00227 if(ones==1)\
00228 ERROR\
00229 val&= 127>>ones;\
00230 while(--ones > 0){\
00231 int tmp= GET_BYTE - 128;\
00232 if(tmp>>6)\
00233 ERROR\
00234 val= (val<<6) + tmp;\
00235 }\
00236 }
00237
00247 #define GET_UTF16(val, GET_16BIT, ERROR)\
00248 val = GET_16BIT;\
00249 {\
00250 unsigned int hi = val - 0xD800;\
00251 if (hi < 0x800) {\
00252 val = GET_16BIT - 0xDC00;\
00253 if (val > 0x3FFU || hi > 0x3FFU)\
00254 ERROR\
00255 val += (hi<<10) + 0x10000;\
00256 }\
00257 }\
00258
00259
00275 #define PUT_UTF8(val, tmp, PUT_BYTE)\
00276 {\
00277 int bytes, shift;\
00278 uint32_t in = val;\
00279 if (in < 0x80) {\
00280 tmp = in;\
00281 PUT_BYTE\
00282 } else {\
00283 bytes = (av_log2(in) + 4) / 5;\
00284 shift = (bytes - 1) * 6;\
00285 tmp = (256 - (256 >> bytes)) | (in >> shift);\
00286 PUT_BYTE\
00287 while (shift >= 6) {\
00288 shift -= 6;\
00289 tmp = 0x80 | ((in >> shift) & 0x3f);\
00290 PUT_BYTE\
00291 }\
00292 }\
00293 }
00294
00309 #define PUT_UTF16(val, tmp, PUT_16BIT)\
00310 {\
00311 uint32_t in = val;\
00312 if (in < 0x10000) {\
00313 tmp = in;\
00314 PUT_16BIT\
00315 } else {\
00316 tmp = 0xD800 | ((in - 0x10000) >> 10);\
00317 PUT_16BIT\
00318 tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
00319 PUT_16BIT\
00320 }\
00321 }\
00322
00323
00324
00325 #include "mem.h"
00326
00327 #ifdef HAVE_AV_CONFIG_H
00328 # include "internal.h"
00329 #endif
00330
00331 #endif
00332
00333
00334
00335
00336
00337
00338 #ifndef av_log2
00339 # define av_log2 av_log2_c
00340 #endif
00341 #ifndef av_log2_16bit
00342 # define av_log2_16bit av_log2_16bit_c
00343 #endif
00344 #ifndef av_ceil_log2
00345 # define av_ceil_log2 av_ceil_log2_c
00346 #endif
00347 #ifndef av_clip
00348 # define av_clip av_clip_c
00349 #endif
00350 #ifndef av_clip_uint8
00351 # define av_clip_uint8 av_clip_uint8_c
00352 #endif
00353 #ifndef av_clip_int8
00354 # define av_clip_int8 av_clip_int8_c
00355 #endif
00356 #ifndef av_clip_uint16
00357 # define av_clip_uint16 av_clip_uint16_c
00358 #endif
00359 #ifndef av_clip_int16
00360 # define av_clip_int16 av_clip_int16_c
00361 #endif
00362 #ifndef av_clipl_int32
00363 # define av_clipl_int32 av_clipl_int32_c
00364 #endif
00365 #ifndef av_clipf
00366 # define av_clipf av_clipf_c
00367 #endif
00368 #ifndef av_popcount
00369 # define av_popcount av_popcount_c
00370 #endif