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.

252 lines
6.6KB

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