internal.h 7.1 KB

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