internal.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 internal.h
  22. * common internal api header.
  23. */
  24. #ifndef AVUTIL_INTERNAL_H
  25. #define AVUTIL_INTERNAL_H
  26. #if !defined(DEBUG) && !defined(NDEBUG)
  27. # define NDEBUG
  28. #endif
  29. #include <stdint.h>
  30. #include <stddef.h>
  31. #include <assert.h>
  32. #include "common.h"
  33. #ifndef attribute_align_arg
  34. #if AV_GCC_VERSION_AT_LEAST(4,2)
  35. # define attribute_align_arg __attribute__((force_align_arg_pointer))
  36. #else
  37. # define attribute_align_arg
  38. #endif
  39. #endif
  40. #ifndef attribute_used
  41. #if AV_GCC_VERSION_AT_LEAST(3,1)
  42. # define attribute_used __attribute__((used))
  43. #else
  44. # define attribute_used
  45. #endif
  46. #endif
  47. #ifndef INT16_MIN
  48. #define INT16_MIN (-0x7fff-1)
  49. #endif
  50. #ifndef INT16_MAX
  51. #define INT16_MAX 0x7fff
  52. #endif
  53. #ifndef INT32_MIN
  54. #define INT32_MIN (-0x7fffffff-1)
  55. #endif
  56. #ifndef INT32_MAX
  57. #define INT32_MAX 0x7fffffff
  58. #endif
  59. #ifndef UINT32_MAX
  60. #define UINT32_MAX 0xffffffff
  61. #endif
  62. #ifndef INT64_MIN
  63. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  64. #endif
  65. #ifndef INT64_MAX
  66. #define INT64_MAX INT64_C(9223372036854775807)
  67. #endif
  68. #ifndef UINT64_MAX
  69. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  70. #endif
  71. #ifndef INT_BIT
  72. # if INT_MAX != 2147483647
  73. # define INT_BIT 64
  74. # else
  75. # define INT_BIT 32
  76. # endif
  77. #endif
  78. #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
  79. # define PIC
  80. #endif
  81. #include "config.h"
  82. #include "intreadwrite.h"
  83. #include "bswap.h"
  84. #ifndef offsetof
  85. # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
  86. #endif
  87. // Use rip-relative addressing if compiling PIC code on x86-64.
  88. #if defined(ARCH_X86_64) && defined(PIC)
  89. # define LOCAL_MANGLE(a) #a "(%%rip)"
  90. #else
  91. # define LOCAL_MANGLE(a) #a
  92. #endif
  93. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  94. /* debug stuff */
  95. /* dprintf macros */
  96. #ifdef DEBUG
  97. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  98. #else
  99. # define dprintf(pctx, ...)
  100. #endif
  101. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  102. /* math */
  103. extern const uint32_t ff_inverse[256];
  104. #if defined(ARCH_X86)
  105. # define FASTDIV(a,b) \
  106. ({\
  107. int ret,dmy;\
  108. __asm__ volatile(\
  109. "mull %3"\
  110. :"=d"(ret),"=a"(dmy)\
  111. :"1"(a),"g"(ff_inverse[b])\
  112. );\
  113. ret;\
  114. })
  115. #elif defined(HAVE_ARMV6)
  116. static inline av_const int FASTDIV(int a, int b)
  117. {
  118. int r, t;
  119. __asm__ volatile("cmp %3, #2 \n\t"
  120. "ldr %1, [%4, %3, lsl #2] \n\t"
  121. "lsrle %0, %2, #1 \n\t"
  122. "smmulgt %0, %1, %2 \n\t"
  123. : "=&r"(r), "=&r"(t) : "r"(a), "r"(b), "r"(ff_inverse));
  124. return r;
  125. }
  126. #elif defined(ARCH_ARM)
  127. static inline av_const int FASTDIV(int a, int b)
  128. {
  129. int r, t;
  130. __asm__ volatile ("umull %1, %0, %2, %3"
  131. : "=&r"(r), "=&r"(t) : "r"(a), "r"(ff_inverse[b]));
  132. return r;
  133. }
  134. #elif defined(CONFIG_FASTDIV)
  135. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  136. #else
  137. # define FASTDIV(a,b) ((a)/(b))
  138. #endif
  139. extern const uint8_t ff_sqrt_tab[256];
  140. static inline int av_log2_16bit(unsigned int v);
  141. static inline av_const unsigned int ff_sqrt(unsigned int a)
  142. {
  143. unsigned int b;
  144. if(a<255) return (ff_sqrt_tab[a+1]-1)>>4;
  145. else if(a<(1<<12)) b= ff_sqrt_tab[a>>4 ]>>2;
  146. #ifndef CONFIG_SMALL
  147. else if(a<(1<<14)) b= ff_sqrt_tab[a>>6 ]>>1;
  148. else if(a<(1<<16)) b= ff_sqrt_tab[a>>8 ] ;
  149. #endif
  150. else{
  151. int s= av_log2_16bit(a>>16)>>1;
  152. unsigned int c= a>>(s+2);
  153. b= ff_sqrt_tab[c>>(s+8)];
  154. b= FASTDIV(c,b) + (b<<s);
  155. }
  156. return b - (a<b*b);
  157. }
  158. #if defined(ARCH_X86)
  159. #define MASK_ABS(mask, level)\
  160. __asm__ volatile(\
  161. "cltd \n\t"\
  162. "xorl %1, %0 \n\t"\
  163. "subl %1, %0 \n\t"\
  164. : "+a" (level), "=&d" (mask)\
  165. );
  166. #else
  167. #define MASK_ABS(mask, level)\
  168. mask= level>>31;\
  169. level= (level^mask)-mask;
  170. #endif
  171. #ifdef HAVE_CMOV
  172. #define COPY3_IF_LT(x,y,a,b,c,d)\
  173. __asm__ volatile (\
  174. "cmpl %0, %3 \n\t"\
  175. "cmovl %3, %0 \n\t"\
  176. "cmovl %4, %1 \n\t"\
  177. "cmovl %5, %2 \n\t"\
  178. : "+&r" (x), "+&r" (a), "+r" (c)\
  179. : "r" (y), "r" (b), "r" (d)\
  180. );
  181. #else
  182. #define COPY3_IF_LT(x,y,a,b,c,d)\
  183. if((y)<(x)){\
  184. (x)=(y);\
  185. (a)=(b);\
  186. (c)=(d);\
  187. }
  188. #endif
  189. /* avoid usage of various functions */
  190. #undef malloc
  191. #define malloc please_use_av_malloc
  192. #undef free
  193. #define free please_use_av_free
  194. #undef realloc
  195. #define realloc please_use_av_realloc
  196. #undef time
  197. #define time time_is_forbidden_due_to_security_issues
  198. #undef rand
  199. #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
  200. #undef srand
  201. #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
  202. #undef random
  203. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  204. #undef sprintf
  205. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  206. #undef strcat
  207. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  208. #undef exit
  209. #define exit exit_is_forbidden
  210. #ifndef LIBAVFORMAT_BUILD
  211. #undef printf
  212. #define printf please_use_av_log
  213. #undef fprintf
  214. #define fprintf please_use_av_log
  215. #undef puts
  216. #define puts please_use_av_log
  217. #undef perror
  218. #define perror please_use_av_log_instead_of_perror
  219. #endif
  220. #define CHECKED_ALLOCZ(p, size)\
  221. {\
  222. p= av_mallocz(size);\
  223. if(p==NULL && (size)!=0){\
  224. av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
  225. goto fail;\
  226. }\
  227. }
  228. #ifndef HAVE_LLRINT
  229. static av_always_inline av_const long long llrint(double x)
  230. {
  231. return rint(x);
  232. }
  233. #endif /* HAVE_LLRINT */
  234. #ifndef HAVE_LRINT
  235. static av_always_inline av_const long int lrint(double x)
  236. {
  237. return rint(x);
  238. }
  239. #endif /* HAVE_LRINT */
  240. #ifndef HAVE_LRINTF
  241. static av_always_inline av_const long int lrintf(float x)
  242. {
  243. return (int)(rint(x));
  244. }
  245. #endif /* HAVE_LRINTF */
  246. #ifndef HAVE_ROUND
  247. static av_always_inline av_const double round(double x)
  248. {
  249. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  250. }
  251. #endif /* HAVE_ROUND */
  252. #ifndef HAVE_ROUNDF
  253. static av_always_inline av_const float roundf(float x)
  254. {
  255. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  256. }
  257. #endif /* HAVE_ROUNDF */
  258. #endif /* AVUTIL_INTERNAL_H */