123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- #ifndef AVUTIL_COMMON_H
- #define AVUTIL_COMMON_H
- #include <ctype.h>
- #include <errno.h>
- #include <inttypes.h>
- #include <limits.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "attributes.h"
- #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
- #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
- #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
- #define FFSIGN(a) ((a) > 0 ? 1 : -1)
- #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
- #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
- #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
- #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
- #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
- #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
- #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
- extern const uint8_t ff_log2_tab[256];
- extern const uint8_t av_reverse[256];
- static inline av_const int av_log2_c(unsigned int v)
- {
- int n = 0;
- if (v & 0xffff0000) {
- v >>= 16;
- n += 16;
- }
- if (v & 0xff00) {
- v >>= 8;
- n += 8;
- }
- n += ff_log2_tab[v];
- return n;
- }
- static inline av_const int av_log2_16bit_c(unsigned int v)
- {
- int n = 0;
- if (v & 0xff00) {
- v >>= 8;
- n += 8;
- }
- n += ff_log2_tab[v];
- return n;
- }
- #ifdef HAVE_AV_CONFIG_H
- # include "config.h"
- # include "intmath.h"
- #endif
- #ifndef av_log2
- # define av_log2 av_log2_c
- #endif
- #ifndef av_log2_16bit
- # define av_log2_16bit av_log2_16bit_c
- #endif
- static inline av_const int av_clip(int a, int amin, int amax)
- {
- if (a < amin) return amin;
- else if (a > amax) return amax;
- else return a;
- }
- static inline av_const uint8_t av_clip_uint8(int a)
- {
- if (a&(~0xFF)) return (-a)>>31;
- else return a;
- }
- static inline av_const uint16_t av_clip_uint16(int a)
- {
- if (a&(~0xFFFF)) return (-a)>>31;
- else return a;
- }
- static inline av_const int16_t av_clip_int16(int a)
- {
- if ((a+0x8000) & ~0xFFFF) return (a>>31) ^ 0x7FFF;
- else return a;
- }
- static inline av_const int32_t av_clipl_int32(int64_t a)
- {
- if ((a+0x80000000u) & ~UINT64_C(0xFFFFFFFF)) return (a>>63) ^ 0x7FFFFFFF;
- else return a;
- }
- static inline av_const float av_clipf(float a, float amin, float amax)
- {
- if (a < amin) return amin;
- else if (a > amax) return amax;
- else return a;
- }
- static inline av_const int av_ceil_log2(int x)
- {
- return av_log2((x - 1) << 1);
- }
- #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
- #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
- #define GET_UTF8(val, GET_BYTE, ERROR)\
- val= GET_BYTE;\
- {\
- int ones= 7 - av_log2(val ^ 255);\
- if(ones==1)\
- ERROR\
- val&= 127>>ones;\
- while(--ones > 0){\
- int tmp= GET_BYTE - 128;\
- if(tmp>>6)\
- ERROR\
- val= (val<<6) + tmp;\
- }\
- }
- #define GET_UTF16(val, GET_16BIT, ERROR)\
- val = GET_16BIT;\
- {\
- unsigned int hi = val - 0xD800;\
- if (hi < 0x800) {\
- val = GET_16BIT - 0xDC00;\
- if (val > 0x3FFU || hi > 0x3FFU)\
- ERROR\
- val += (hi<<10) + 0x10000;\
- }\
- }\
- #define PUT_UTF8(val, tmp, PUT_BYTE)\
- {\
- int bytes, shift;\
- uint32_t in = val;\
- if (in < 0x80) {\
- tmp = in;\
- PUT_BYTE\
- } else {\
- bytes = (av_log2(in) + 4) / 5;\
- shift = (bytes - 1) * 6;\
- tmp = (256 - (256 >> bytes)) | (in >> shift);\
- PUT_BYTE\
- while (shift >= 6) {\
- shift -= 6;\
- tmp = 0x80 | ((in >> shift) & 0x3f);\
- PUT_BYTE\
- }\
- }\
- }
- #define PUT_UTF16(val, tmp, PUT_16BIT)\
- {\
- uint32_t in = val;\
- if (in < 0x10000) {\
- tmp = in;\
- PUT_16BIT\
- } else {\
- tmp = 0xD800 | ((in - 0x10000) >> 10);\
- PUT_16BIT\
- tmp = 0xDC00 | ((in - 0x10000) & 0x3FF);\
- PUT_16BIT\
- }\
- }\
- #include "mem.h"
- #ifdef HAVE_AV_CONFIG_H
- # include "internal.h"
- #endif
- #endif
|