You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
8.4KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file common.h
  22. * common internal and external API header
  23. */
  24. #ifndef AVUTIL_COMMON_H
  25. #define AVUTIL_COMMON_H
  26. #include <inttypes.h>
  27. #ifdef HAVE_AV_CONFIG_H
  28. /* only include the following when compiling package */
  29. # include "config.h"
  30. # include <stdlib.h>
  31. # include <stdio.h>
  32. # include <string.h>
  33. # include <ctype.h>
  34. # include <limits.h>
  35. # include <errno.h>
  36. # include <math.h>
  37. #endif /* HAVE_AV_CONFIG_H */
  38. #define AV_GCC_VERSION_AT_LEAST(x,y) (defined(__GNUC__) && (__GNUC__ > x || __GNUC__ == x && __GNUC_MINOR__ >= y))
  39. #ifndef av_always_inline
  40. #if AV_GCC_VERSION_AT_LEAST(3,1)
  41. # define av_always_inline __attribute__((always_inline)) inline
  42. #else
  43. # define av_always_inline inline
  44. #endif
  45. #endif
  46. #ifndef av_noinline
  47. #if AV_GCC_VERSION_AT_LEAST(3,1)
  48. # define av_noinline __attribute__((noinline))
  49. #else
  50. # define av_noinline
  51. #endif
  52. #endif
  53. #ifndef av_pure
  54. #if AV_GCC_VERSION_AT_LEAST(3,1)
  55. # define av_pure __attribute__((pure))
  56. #else
  57. # define av_pure
  58. #endif
  59. #endif
  60. #ifndef av_const
  61. #if AV_GCC_VERSION_AT_LEAST(2,6)
  62. # define av_const __attribute__((const))
  63. #else
  64. # define av_const
  65. #endif
  66. #endif
  67. #ifndef av_cold
  68. #if (!defined(__ICC) || __ICC > 1100) && AV_GCC_VERSION_AT_LEAST(4,3)
  69. # define av_cold __attribute__((cold))
  70. #else
  71. # define av_cold
  72. #endif
  73. #endif
  74. #ifdef HAVE_AV_CONFIG_H
  75. # include "internal.h"
  76. #endif /* HAVE_AV_CONFIG_H */
  77. #ifndef attribute_deprecated
  78. #if AV_GCC_VERSION_AT_LEAST(3,1)
  79. # define attribute_deprecated __attribute__((deprecated))
  80. #else
  81. # define attribute_deprecated
  82. #endif
  83. #endif
  84. #ifndef av_unused
  85. #if defined(__GNUC__)
  86. # define av_unused __attribute__((unused))
  87. #else
  88. # define av_unused
  89. #endif
  90. #endif
  91. #include "mem.h"
  92. //rounded divison & shift
  93. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  94. /* assume b>0 */
  95. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  96. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  97. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  98. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  99. #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
  100. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  101. #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
  102. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  103. #define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
  104. /* misc math functions */
  105. extern const uint8_t ff_log2_tab[256];
  106. static inline av_const int av_log2(unsigned int v)
  107. {
  108. int n = 0;
  109. if (v & 0xffff0000) {
  110. v >>= 16;
  111. n += 16;
  112. }
  113. if (v & 0xff00) {
  114. v >>= 8;
  115. n += 8;
  116. }
  117. n += ff_log2_tab[v];
  118. return n;
  119. }
  120. static inline av_const int av_log2_16bit(unsigned int v)
  121. {
  122. int n = 0;
  123. if (v & 0xff00) {
  124. v >>= 8;
  125. n += 8;
  126. }
  127. n += ff_log2_tab[v];
  128. return n;
  129. }
  130. /* median of 3 */
  131. static inline av_const int mid_pred(int a, int b, int c)
  132. {
  133. #if HAVE_CMOV
  134. int i=b;
  135. __asm__ volatile(
  136. "cmp %2, %1 \n\t"
  137. "cmovg %1, %0 \n\t"
  138. "cmovg %2, %1 \n\t"
  139. "cmp %3, %1 \n\t"
  140. "cmovl %3, %1 \n\t"
  141. "cmp %1, %0 \n\t"
  142. "cmovg %1, %0 \n\t"
  143. :"+&r"(i), "+&r"(a)
  144. :"r"(b), "r"(c)
  145. );
  146. return i;
  147. #elif 0
  148. int t= (a-b)&((a-b)>>31);
  149. a-=t;
  150. b+=t;
  151. b-= (b-c)&((b-c)>>31);
  152. b+= (a-b)&((a-b)>>31);
  153. return b;
  154. #else
  155. if(a>b){
  156. if(c>b){
  157. if(c>a) b=a;
  158. else b=c;
  159. }
  160. }else{
  161. if(b>c){
  162. if(c>a) b=c;
  163. else b=a;
  164. }
  165. }
  166. return b;
  167. #endif
  168. }
  169. /**
  170. * clip a signed integer value into the amin-amax range
  171. * @param a value to clip
  172. * @param amin minimum value of the clip range
  173. * @param amax maximum value of the clip range
  174. * @return clipped value
  175. */
  176. static inline av_const int av_clip(int a, int amin, int amax)
  177. {
  178. if (a < amin) return amin;
  179. else if (a > amax) return amax;
  180. else return a;
  181. }
  182. /**
  183. * clip a signed integer value into the 0-255 range
  184. * @param a value to clip
  185. * @return clipped value
  186. */
  187. static inline av_const uint8_t av_clip_uint8(int a)
  188. {
  189. if (a&(~255)) return (-a)>>31;
  190. else return a;
  191. }
  192. /**
  193. * clip a signed integer value into the -32768,32767 range
  194. * @param a value to clip
  195. * @return clipped value
  196. */
  197. static inline av_const int16_t av_clip_int16(int a)
  198. {
  199. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  200. else return a;
  201. }
  202. /**
  203. * clip a float value into the amin-amax range
  204. * @param a value to clip
  205. * @param amin minimum value of the clip range
  206. * @param amax maximum value of the clip range
  207. * @return clipped value
  208. */
  209. static inline av_const float av_clipf(float a, float amin, float amax)
  210. {
  211. if (a < amin) return amin;
  212. else if (a > amax) return amax;
  213. else return a;
  214. }
  215. /* math */
  216. int64_t av_const ff_gcd(int64_t a, int64_t b);
  217. /**
  218. * converts fourcc string to int
  219. */
  220. static inline av_pure int ff_get_fourcc(const char *s){
  221. #ifdef HAVE_AV_CONFIG_H
  222. assert( strlen(s)==4 );
  223. #endif
  224. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  225. }
  226. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  227. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  228. /*!
  229. * \def GET_UTF8(val, GET_BYTE, ERROR)
  230. * converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  231. * \param val is the output and should be of type uint32_t. It holds the converted
  232. * UCS-4 character and should be a left value.
  233. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  234. * a function or a statement whose return value or evaluated value is of type
  235. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  236. * and up to 7 times in the general case.
  237. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  238. * from GET_BYTE. It should be a statement that jumps out of the macro,
  239. * like exit(), goto, return, break, or continue.
  240. */
  241. #define GET_UTF8(val, GET_BYTE, ERROR)\
  242. val= GET_BYTE;\
  243. {\
  244. int ones= 7 - av_log2(val ^ 255);\
  245. if(ones==1)\
  246. ERROR\
  247. val&= 127>>ones;\
  248. while(--ones > 0){\
  249. int tmp= GET_BYTE - 128;\
  250. if(tmp>>6)\
  251. ERROR\
  252. val= (val<<6) + tmp;\
  253. }\
  254. }
  255. /*!
  256. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  257. * converts a 32-bit unicode character to its UTF-8 encoded form (up to 4 bytes long).
  258. * \param val is an input only argument and should be of type uint32_t. It holds
  259. * a ucs4 encoded unicode character that is to be converted to UTF-8. If
  260. * val is given as a function it's executed only once.
  261. * \param tmp is a temporary variable and should be of type uint8_t. It
  262. * represents an intermediate value during conversion that is to be
  263. * outputted by PUT_BYTE.
  264. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  265. * It could be a function or a statement, and uses tmp as the input byte.
  266. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  267. * executed up to 4 times for values in the valid UTF-8 range and up to
  268. * 7 times in the general case, depending on the length of the converted
  269. * unicode character.
  270. */
  271. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  272. {\
  273. int bytes, shift;\
  274. uint32_t in = val;\
  275. if (in < 0x80) {\
  276. tmp = in;\
  277. PUT_BYTE\
  278. } else {\
  279. bytes = (av_log2(in) + 4) / 5;\
  280. shift = (bytes - 1) * 6;\
  281. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  282. PUT_BYTE\
  283. while (shift >= 6) {\
  284. shift -= 6;\
  285. tmp = 0x80 | ((in >> shift) & 0x3f);\
  286. PUT_BYTE\
  287. }\
  288. }\
  289. }
  290. #endif /* AVUTIL_COMMON_H */