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.

306 lines
7.2KB

  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 FFMPEG_INTERNAL_H
  25. #define FFMPEG_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. #ifdef CONFIG_FASTMEMCPY
  87. # include "libvo/fastmemcpy.h"
  88. # define memcpy(a,b,c) fast_memcpy(a,b,c)
  89. #endif
  90. // Use rip-relative addressing if compiling PIC code on x86-64.
  91. #if defined(ARCH_X86_64) && defined(PIC)
  92. # define LOCAL_MANGLE(a) #a "(%%rip)"
  93. #else
  94. # define LOCAL_MANGLE(a) #a
  95. #endif
  96. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  97. /* debug stuff */
  98. /* dprintf macros */
  99. #ifdef DEBUG
  100. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  101. #else
  102. # define dprintf(pctx, ...)
  103. #endif
  104. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  105. /* math */
  106. extern const uint32_t ff_inverse[256];
  107. #if defined(ARCH_X86)
  108. # define FASTDIV(a,b) \
  109. ({\
  110. int ret,dmy;\
  111. asm volatile(\
  112. "mull %3"\
  113. :"=d"(ret),"=a"(dmy)\
  114. :"1"(a),"g"(ff_inverse[b])\
  115. );\
  116. ret;\
  117. })
  118. #elif defined(HAVE_ARMV6)
  119. static inline av_const int FASTDIV(int a, int b)
  120. {
  121. int r;
  122. asm volatile("cmp %2, #0 \n\t"
  123. "smmul %0, %1, %2 \n\t"
  124. "rsblt %0, %0, #0 \n\t"
  125. : "=r"(r) : "r"(a), "r"(ff_inverse[b]));
  126. return r;
  127. }
  128. #elif defined(ARCH_ARMV4L)
  129. # define FASTDIV(a,b) \
  130. ({\
  131. int ret,dmy;\
  132. asm volatile(\
  133. "umull %1, %0, %2, %3"\
  134. :"=&r"(ret),"=&r"(dmy)\
  135. :"r"(a),"r"(ff_inverse[b])\
  136. );\
  137. ret;\
  138. })
  139. #elif defined(CONFIG_FASTDIV)
  140. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  141. #else
  142. # define FASTDIV(a,b) ((a)/(b))
  143. #endif
  144. extern const uint8_t ff_sqrt_tab[256];
  145. static inline int av_log2_16bit(unsigned int v);
  146. static inline av_const unsigned int ff_sqrt(unsigned int a)
  147. {
  148. unsigned int b;
  149. if(a<255) return (ff_sqrt_tab[a+1]-1)>>4;
  150. else if(a<(1<<12)) b= ff_sqrt_tab[a>>4 ]>>2;
  151. #ifndef CONFIG_SMALL
  152. else if(a<(1<<14)) b= ff_sqrt_tab[a>>6 ]>>1;
  153. else if(a<(1<<16)) b= ff_sqrt_tab[a>>8 ] ;
  154. #endif
  155. else{
  156. int s= av_log2_16bit(a>>16)>>1;
  157. unsigned int c= a>>(s+2);
  158. b= ff_sqrt_tab[c>>(s+8)];
  159. b= FASTDIV(c,b) + (b<<s);
  160. }
  161. return b - (a<b*b);
  162. }
  163. #if defined(ARCH_X86)
  164. #define MASK_ABS(mask, level)\
  165. asm volatile(\
  166. "cltd \n\t"\
  167. "xorl %1, %0 \n\t"\
  168. "subl %1, %0 \n\t"\
  169. : "+a" (level), "=&d" (mask)\
  170. );
  171. #else
  172. #define MASK_ABS(mask, level)\
  173. mask= level>>31;\
  174. level= (level^mask)-mask;
  175. #endif
  176. #ifdef HAVE_CMOV
  177. #define COPY3_IF_LT(x,y,a,b,c,d)\
  178. asm volatile (\
  179. "cmpl %0, %3 \n\t"\
  180. "cmovl %3, %0 \n\t"\
  181. "cmovl %4, %1 \n\t"\
  182. "cmovl %5, %2 \n\t"\
  183. : "+&r" (x), "+&r" (a), "+r" (c)\
  184. : "r" (y), "r" (b), "r" (d)\
  185. );
  186. #else
  187. #define COPY3_IF_LT(x,y,a,b,c,d)\
  188. if((y)<(x)){\
  189. (x)=(y);\
  190. (a)=(b);\
  191. (c)=(d);\
  192. }
  193. #endif
  194. /* avoid usage of various functions */
  195. #undef malloc
  196. #define malloc please_use_av_malloc
  197. #undef free
  198. #define free please_use_av_free
  199. #undef realloc
  200. #define realloc please_use_av_realloc
  201. #undef time
  202. #define time time_is_forbidden_due_to_security_issues
  203. #undef rand
  204. #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
  205. #undef srand
  206. #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
  207. #undef random
  208. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  209. #undef sprintf
  210. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  211. #undef strcat
  212. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  213. #undef exit
  214. #define exit exit_is_forbidden
  215. #if !(defined(LIBAVFORMAT_BUILD) || defined(FFMPEG_FRAMEHOOK_H))
  216. #undef printf
  217. #define printf please_use_av_log
  218. #undef fprintf
  219. #define fprintf please_use_av_log
  220. #undef puts
  221. #define puts please_use_av_log
  222. #undef perror
  223. #define perror please_use_av_log_instead_of_perror
  224. #endif
  225. #define CHECKED_ALLOCZ(p, size)\
  226. {\
  227. p= av_mallocz(size);\
  228. if(p==NULL && (size)!=0){\
  229. av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
  230. goto fail;\
  231. }\
  232. }
  233. #ifndef HAVE_LLRINT
  234. static av_always_inline av_const long long llrint(double x)
  235. {
  236. return rint(x);
  237. }
  238. #endif /* HAVE_LLRINT */
  239. #ifndef HAVE_LRINT
  240. static av_always_inline av_const long int lrint(double x)
  241. {
  242. return rint(x);
  243. }
  244. #endif /* HAVE_LRINT */
  245. #ifndef HAVE_LRINTF
  246. static av_always_inline av_const long int lrintf(float x)
  247. {
  248. return (int)(rint(x));
  249. }
  250. #endif /* HAVE_LRINTF */
  251. #ifndef HAVE_ROUND
  252. static av_always_inline av_const double round(double x)
  253. {
  254. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  255. }
  256. #endif /* HAVE_ROUND */
  257. #ifndef HAVE_ROUNDF
  258. static av_always_inline av_const float roundf(float x)
  259. {
  260. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  261. }
  262. #endif /* HAVE_ROUNDF */
  263. #endif /* FFMPEG_INTERNAL_H */