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.

242 lines
6.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
  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 <limits.h>
  30. #include <stdint.h>
  31. #include <stddef.h>
  32. #include <assert.h>
  33. #include "config.h"
  34. #include "attributes.h"
  35. #include "timer.h"
  36. #include "cpu.h"
  37. #ifndef attribute_align_arg
  38. #if ARCH_X86_32 && AV_GCC_VERSION_AT_LEAST(4,2)
  39. # define attribute_align_arg __attribute__((force_align_arg_pointer))
  40. #else
  41. # define attribute_align_arg
  42. #endif
  43. #endif
  44. #ifndef INT16_MIN
  45. #define INT16_MIN (-0x7fff - 1)
  46. #endif
  47. #ifndef INT16_MAX
  48. #define INT16_MAX 0x7fff
  49. #endif
  50. #ifndef INT32_MIN
  51. #define INT32_MIN (-0x7fffffff - 1)
  52. #endif
  53. #ifndef INT32_MAX
  54. #define INT32_MAX 0x7fffffff
  55. #endif
  56. #ifndef UINT32_MAX
  57. #define UINT32_MAX 0xffffffff
  58. #endif
  59. #ifndef INT64_MIN
  60. #define INT64_MIN (-0x7fffffffffffffffLL - 1)
  61. #endif
  62. #ifndef INT64_MAX
  63. #define INT64_MAX INT64_C(9223372036854775807)
  64. #endif
  65. #ifndef UINT64_MAX
  66. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  67. #endif
  68. #ifndef INT_BIT
  69. # define INT_BIT (CHAR_BIT * sizeof(int))
  70. #endif
  71. #ifndef offsetof
  72. # define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
  73. #endif
  74. /* Use to export labels from asm. */
  75. #define LABEL_MANGLE(a) EXTERN_PREFIX #a
  76. // Use rip-relative addressing if compiling PIC code on x86-64.
  77. #if ARCH_X86_64 && defined(PIC)
  78. # define LOCAL_MANGLE(a) #a "(%%rip)"
  79. #else
  80. # define LOCAL_MANGLE(a) #a
  81. #endif
  82. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  83. /* debug stuff */
  84. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  85. /* math */
  86. #if ARCH_X86
  87. #define MASK_ABS(mask, level)\
  88. __asm__ volatile(\
  89. "cltd \n\t"\
  90. "xorl %1, %0 \n\t"\
  91. "subl %1, %0 \n\t"\
  92. : "+a" (level), "=&d" (mask)\
  93. );
  94. #else
  95. #define MASK_ABS(mask, level)\
  96. mask = level >> 31;\
  97. level = (level ^ mask) - mask;
  98. #endif
  99. /* avoid usage of dangerous/inappropriate system functions */
  100. #undef malloc
  101. #define malloc please_use_av_malloc
  102. #undef free
  103. #define free please_use_av_free
  104. #undef realloc
  105. #define realloc please_use_av_realloc
  106. #undef time
  107. #define time time_is_forbidden_due_to_security_issues
  108. #undef rand
  109. #define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
  110. #undef srand
  111. #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
  112. #undef random
  113. #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
  114. #undef sprintf
  115. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  116. #undef strcat
  117. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  118. #undef strncpy
  119. #define strncpy strncpy_is_forbidden_due_to_security_issues_use_av_strlcpy
  120. #undef exit
  121. #define exit exit_is_forbidden
  122. #ifndef LIBAVFORMAT_BUILD
  123. #undef printf
  124. #define printf please_use_av_log_instead_of_printf
  125. #undef fprintf
  126. #define fprintf please_use_av_log_instead_of_fprintf
  127. #undef puts
  128. #define puts please_use_av_log_instead_of_puts
  129. #undef perror
  130. #define perror please_use_av_log_instead_of_perror
  131. #endif
  132. #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
  133. {\
  134. p = av_malloc(size);\
  135. if (p == NULL && (size) != 0) {\
  136. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  137. goto label;\
  138. }\
  139. }
  140. #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
  141. {\
  142. p = av_mallocz(size);\
  143. if (p == NULL && (size) != 0) {\
  144. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  145. goto label;\
  146. }\
  147. }
  148. #include "libm.h"
  149. /**
  150. * Return NULL if CONFIG_SMALL is true, otherwise the argument
  151. * without modification. Used to disable the definition of strings
  152. * (for example AVCodec long_names).
  153. */
  154. #if CONFIG_SMALL
  155. # define NULL_IF_CONFIG_SMALL(x) NULL
  156. #else
  157. # define NULL_IF_CONFIG_SMALL(x) x
  158. #endif
  159. /**
  160. * Define a function with only the non-default version specified.
  161. *
  162. * On systems with ELF shared libraries, all symbols exported from
  163. * FFmpeg libraries are tagged with the name and major version of the
  164. * library to which they belong. If a function is moved from one
  165. * library to another, a wrapper must be retained in the original
  166. * location to preserve binary compatibility.
  167. *
  168. * Functions defined with this macro will never be used to resolve
  169. * symbols by the build-time linker.
  170. *
  171. * @param type return type of function
  172. * @param name name of function
  173. * @param args argument list of function
  174. * @param ver version tag to assign function
  175. */
  176. #if HAVE_SYMVER_ASM_LABEL
  177. # define FF_SYMVER(type, name, args, ver) \
  178. type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver); \
  179. type ff_##name args
  180. #elif HAVE_SYMVER_GNU_ASM
  181. # define FF_SYMVER(type, name, args, ver) \
  182. __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver); \
  183. type ff_##name args; \
  184. type ff_##name args
  185. #endif
  186. /**
  187. * Returns NULL if a threading library has not been enabled.
  188. * Used to disable threading functions in AVCodec definitions
  189. * when not needed.
  190. */
  191. #if HAVE_THREADS
  192. # define ONLY_IF_THREADS_ENABLED(x) x
  193. #else
  194. # define ONLY_IF_THREADS_ENABLED(x) NULL
  195. #endif
  196. #if HAVE_MMX
  197. /**
  198. * Empty mmx state.
  199. * this must be called between any dsp function and float/double code.
  200. * for example sin(); dsp->idct_put(); emms_c(); cos()
  201. */
  202. static av_always_inline void emms_c(void)
  203. {
  204. if(av_get_cpu_flags() & AV_CPU_FLAG_MMX)
  205. __asm__ volatile ("emms" ::: "memory");
  206. }
  207. #else /* HAVE_MMX */
  208. #define emms_c()
  209. #endif /* HAVE_MMX */
  210. #endif /* AVUTIL_INTERNAL_H */