internal.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 libavutil/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 <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. #ifndef attribute_align_arg
  37. #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,2)
  38. # define attribute_align_arg __attribute__((force_align_arg_pointer))
  39. #else
  40. # define attribute_align_arg
  41. #endif
  42. #endif
  43. #ifndef attribute_used
  44. #if AV_GCC_VERSION_AT_LEAST(3,1)
  45. # define attribute_used __attribute__((used))
  46. #else
  47. # define attribute_used
  48. #endif
  49. #endif
  50. #ifndef av_alias
  51. #if HAVE_ATTRIBUTE_MAY_ALIAS && (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(3,3)
  52. # define av_alias __attribute__((may_alias))
  53. #else
  54. # define av_alias
  55. #endif
  56. #endif
  57. #ifndef INT16_MIN
  58. #define INT16_MIN (-0x7fff - 1)
  59. #endif
  60. #ifndef INT16_MAX
  61. #define INT16_MAX 0x7fff
  62. #endif
  63. #ifndef INT32_MIN
  64. #define INT32_MIN (-0x7fffffff - 1)
  65. #endif
  66. #ifndef INT32_MAX
  67. #define INT32_MAX 0x7fffffff
  68. #endif
  69. #ifndef UINT32_MAX
  70. #define UINT32_MAX 0xffffffff
  71. #endif
  72. #ifndef INT64_MIN
  73. #define INT64_MIN (-0x7fffffffffffffffLL - 1)
  74. #endif
  75. #ifndef INT64_MAX
  76. #define INT64_MAX INT64_C(9223372036854775807)
  77. #endif
  78. #ifndef UINT64_MAX
  79. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  80. #endif
  81. #ifndef INT_BIT
  82. # define INT_BIT (CHAR_BIT * sizeof(int))
  83. #endif
  84. #ifndef offsetof
  85. # define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
  86. #endif
  87. /* Use to export labels from asm. */
  88. #define LABEL_MANGLE(a) EXTERN_PREFIX #a
  89. // Use rip-relative addressing if compiling PIC code on x86-64.
  90. #if ARCH_X86_64 && defined(PIC)
  91. # define LOCAL_MANGLE(a) #a "(%%rip)"
  92. #else
  93. # define LOCAL_MANGLE(a) #a
  94. #endif
  95. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  96. /* debug stuff */
  97. /* dprintf macros */
  98. #ifdef DEBUG
  99. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  100. #else
  101. # define dprintf(pctx, ...)
  102. #endif
  103. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  104. /* math */
  105. #if ARCH_X86
  106. #define MASK_ABS(mask, level)\
  107. __asm__ volatile(\
  108. "cltd \n\t"\
  109. "xorl %1, %0 \n\t"\
  110. "subl %1, %0 \n\t"\
  111. : "+a" (level), "=&d" (mask)\
  112. );
  113. #else
  114. #define MASK_ABS(mask, level)\
  115. mask = level >> 31;\
  116. level = (level ^ mask) - mask;
  117. #endif
  118. /* avoid usage of dangerous/inappropriate system functions */
  119. #undef malloc
  120. #define malloc please_use_av_malloc
  121. #undef free
  122. #define free please_use_av_free
  123. #undef realloc
  124. #define realloc please_use_av_realloc
  125. #undef time
  126. #define time time_is_forbidden_due_to_security_issues
  127. #undef rand
  128. #define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
  129. #undef srand
  130. #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
  131. #undef random
  132. #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
  133. #undef sprintf
  134. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  135. #undef strcat
  136. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  137. #undef exit
  138. #define exit exit_is_forbidden
  139. #ifndef LIBAVFORMAT_BUILD
  140. #undef printf
  141. #define printf please_use_av_log_instead_of_printf
  142. #undef fprintf
  143. #define fprintf please_use_av_log_instead_of_fprintf
  144. #undef puts
  145. #define puts please_use_av_log_instead_of_puts
  146. #undef perror
  147. #define perror please_use_av_log_instead_of_perror
  148. #endif
  149. #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
  150. {\
  151. p = av_malloc(size);\
  152. if (p == NULL && (size) != 0) {\
  153. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  154. goto label;\
  155. }\
  156. }
  157. #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
  158. {\
  159. p = av_mallocz(size);\
  160. if (p == NULL && (size) != 0) {\
  161. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  162. goto label;\
  163. }\
  164. }
  165. #include "libm.h"
  166. /**
  167. * Returns NULL if CONFIG_SMALL is true, otherwise the argument
  168. * without modification. Used to disable the definition of strings
  169. * (for example AVCodec long_names).
  170. */
  171. #if CONFIG_SMALL
  172. # define NULL_IF_CONFIG_SMALL(x) NULL
  173. #else
  174. # define NULL_IF_CONFIG_SMALL(x) x
  175. #endif
  176. #endif /* AVUTIL_INTERNAL_H */