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.

390 lines
9.9KB

  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 common.h
  22. * common internal and external API header
  23. */
  24. #ifndef FFMPEG_COMMON_H
  25. #define FFMPEG_COMMON_H
  26. #include <inttypes.h>
  27. #ifdef HAVE_AV_CONFIG_H
  28. /* only include the following when compiling package */
  29. # include "config.h"
  30. # include <stdlib.h>
  31. # include <stdio.h>
  32. # include <string.h>
  33. # include <ctype.h>
  34. # include <limits.h>
  35. # include <errno.h>
  36. # include <math.h>
  37. #endif /* HAVE_AV_CONFIG_H */
  38. #ifndef av_always_inline
  39. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  40. # define av_always_inline __attribute__((always_inline)) inline
  41. #else
  42. # define av_always_inline inline
  43. #endif
  44. #endif
  45. #ifndef av_noinline
  46. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  47. # define av_noinline __attribute__((noinline))
  48. #else
  49. # define av_noinline
  50. #endif
  51. #endif
  52. #ifndef av_pure
  53. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  54. # define av_pure __attribute__((pure))
  55. #else
  56. # define av_pure
  57. #endif
  58. #endif
  59. #ifndef av_const
  60. #if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ > 5)
  61. # define av_const __attribute__((const))
  62. #else
  63. # define av_const
  64. #endif
  65. #endif
  66. #ifndef av_cold
  67. #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 2)
  68. # define av_cold __attribute__((cold))
  69. #else
  70. # define av_cold
  71. #endif
  72. #endif
  73. #ifdef HAVE_AV_CONFIG_H
  74. # include "internal.h"
  75. #endif /* HAVE_AV_CONFIG_H */
  76. #ifndef attribute_deprecated
  77. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  78. # define attribute_deprecated __attribute__((deprecated))
  79. #else
  80. # define attribute_deprecated
  81. #endif
  82. #endif
  83. #ifndef av_unused
  84. #if defined(__GNUC__)
  85. # define av_unused __attribute__((unused))
  86. #else
  87. # define av_unused
  88. #endif
  89. #endif
  90. #include "mem.h"
  91. //rounded divison & shift
  92. #define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
  93. /* assume b>0 */
  94. #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
  95. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  96. #define FFSIGN(a) ((a) > 0 ? 1 : -1)
  97. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  98. #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
  99. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  100. #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
  101. /* misc math functions */
  102. extern const uint8_t ff_log2_tab[256];
  103. static inline av_const int av_log2(unsigned int v)
  104. {
  105. int n = 0;
  106. if (v & 0xffff0000) {
  107. v >>= 16;
  108. n += 16;
  109. }
  110. if (v & 0xff00) {
  111. v >>= 8;
  112. n += 8;
  113. }
  114. n += ff_log2_tab[v];
  115. return n;
  116. }
  117. static inline av_const int av_log2_16bit(unsigned int v)
  118. {
  119. int n = 0;
  120. if (v & 0xff00) {
  121. v >>= 8;
  122. n += 8;
  123. }
  124. n += ff_log2_tab[v];
  125. return n;
  126. }
  127. /* median of 3 */
  128. static inline av_const int mid_pred(int a, int b, int c)
  129. {
  130. #ifdef HAVE_CMOV
  131. int i=b;
  132. asm volatile(
  133. "cmp %2, %1 \n\t"
  134. "cmovg %1, %0 \n\t"
  135. "cmovg %2, %1 \n\t"
  136. "cmp %3, %1 \n\t"
  137. "cmovl %3, %1 \n\t"
  138. "cmp %1, %0 \n\t"
  139. "cmovg %1, %0 \n\t"
  140. :"+&r"(i), "+&r"(a)
  141. :"r"(b), "r"(c)
  142. );
  143. return i;
  144. #elif 0
  145. int t= (a-b)&((a-b)>>31);
  146. a-=t;
  147. b+=t;
  148. b-= (b-c)&((b-c)>>31);
  149. b+= (a-b)&((a-b)>>31);
  150. return b;
  151. #else
  152. if(a>b){
  153. if(c>b){
  154. if(c>a) b=a;
  155. else b=c;
  156. }
  157. }else{
  158. if(b>c){
  159. if(c>a) b=c;
  160. else b=a;
  161. }
  162. }
  163. return b;
  164. #endif
  165. }
  166. /**
  167. * clip a signed integer value into the amin-amax range
  168. * @param a value to clip
  169. * @param amin minimum value of the clip range
  170. * @param amax maximum value of the clip range
  171. * @return clipped value
  172. */
  173. static inline av_const int av_clip(int a, int amin, int amax)
  174. {
  175. if (a < amin) return amin;
  176. else if (a > amax) return amax;
  177. else return a;
  178. }
  179. /**
  180. * clip a signed integer value into the 0-255 range
  181. * @param a value to clip
  182. * @return clipped value
  183. */
  184. static inline av_const uint8_t av_clip_uint8(int a)
  185. {
  186. if (a&(~255)) return (-a)>>31;
  187. else return a;
  188. }
  189. /**
  190. * clip a signed integer value into the -32768,32767 range
  191. * @param a value to clip
  192. * @return clipped value
  193. */
  194. static inline av_const int16_t av_clip_int16(int a)
  195. {
  196. if ((a+32768) & ~65535) return (a>>31) ^ 32767;
  197. else return a;
  198. }
  199. /* math */
  200. int64_t av_const ff_gcd(int64_t a, int64_t b);
  201. /**
  202. * converts fourcc string to int
  203. */
  204. static inline av_pure int ff_get_fourcc(const char *s){
  205. #ifdef HAVE_AV_CONFIG_H
  206. assert( strlen(s)==4 );
  207. #endif
  208. return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
  209. }
  210. #define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
  211. #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
  212. /*!
  213. * \def GET_UTF8(val, GET_BYTE, ERROR)
  214. * converts a UTF-8 character (up to 4 bytes long) to its 32-bit UCS-4 encoded form
  215. * \param val is the output and should be of type uint32_t. It holds the converted
  216. * UCS-4 character and should be a left value.
  217. * \param GET_BYTE gets UTF-8 encoded bytes from any proper source. It can be
  218. * a function or a statement whose return value or evaluated value is of type
  219. * uint8_t. It will be executed up to 4 times for values in the valid UTF-8 range,
  220. * and up to 7 times in the general case.
  221. * \param ERROR action that should be taken when an invalid UTF-8 byte is returned
  222. * from GET_BYTE. It should be a statement that jumps out of the macro,
  223. * like exit(), goto, return, break, or continue.
  224. */
  225. #define GET_UTF8(val, GET_BYTE, ERROR)\
  226. val= GET_BYTE;\
  227. {\
  228. int ones= 7 - av_log2(val ^ 255);\
  229. if(ones==1)\
  230. ERROR\
  231. val&= 127>>ones;\
  232. while(--ones > 0){\
  233. int tmp= GET_BYTE - 128;\
  234. if(tmp>>6)\
  235. ERROR\
  236. val= (val<<6) + tmp;\
  237. }\
  238. }
  239. /*!
  240. * \def PUT_UTF8(val, tmp, PUT_BYTE)
  241. * converts a 32-bit unicode character to its UTF-8 encoded form (up to 4 bytes long).
  242. * \param val is an input only argument and should be of type uint32_t. It holds
  243. * a ucs4 encoded unicode character that is to be converted to UTF-8. If
  244. * val is given as a function it's executed only once.
  245. * \param tmp is a temporary variable and should be of type uint8_t. It
  246. * represents an intermediate value during conversion that is to be
  247. * outputted by PUT_BYTE.
  248. * \param PUT_BYTE writes the converted UTF-8 bytes to any proper destination.
  249. * It could be a function or a statement, and uses tmp as the input byte.
  250. * For example, PUT_BYTE could be "*output++ = tmp;" PUT_BYTE will be
  251. * executed up to 4 times for values in the valid UTF-8 range and up to
  252. * 7 times in the general case, depending on the length of the converted
  253. * unicode character.
  254. */
  255. #define PUT_UTF8(val, tmp, PUT_BYTE)\
  256. {\
  257. int bytes, shift;\
  258. uint32_t in = val;\
  259. if (in < 0x80) {\
  260. tmp = in;\
  261. PUT_BYTE\
  262. } else {\
  263. bytes = (av_log2(in) + 4) / 5;\
  264. shift = (bytes - 1) * 6;\
  265. tmp = (256 - (256 >> bytes)) | (in >> shift);\
  266. PUT_BYTE\
  267. while (shift >= 6) {\
  268. shift -= 6;\
  269. tmp = 0x80 | ((in >> shift) & 0x3f);\
  270. PUT_BYTE\
  271. }\
  272. }\
  273. }
  274. #if defined(ARCH_X86) || defined(ARCH_POWERPC) || defined(ARCH_BFIN)
  275. #define AV_READ_TIME read_time
  276. #if defined(ARCH_X86_64)
  277. static inline uint64_t read_time(void)
  278. {
  279. uint64_t a, d;
  280. asm volatile("rdtsc\n\t"
  281. : "=a" (a), "=d" (d));
  282. return (d << 32) | (a & 0xffffffff);
  283. }
  284. #elif defined(ARCH_X86_32)
  285. static inline long long read_time(void)
  286. {
  287. long long l;
  288. asm volatile("rdtsc\n\t"
  289. : "=A" (l));
  290. return l;
  291. }
  292. #elif ARCH_BFIN
  293. static inline uint64_t read_time(void)
  294. {
  295. union {
  296. struct {
  297. unsigned lo;
  298. unsigned hi;
  299. } p;
  300. unsigned long long c;
  301. } t;
  302. asm volatile ("%0=cycles; %1=cycles2;" : "=d" (t.p.lo), "=d" (t.p.hi));
  303. return t.c;
  304. }
  305. #else //FIXME check ppc64
  306. static inline uint64_t read_time(void)
  307. {
  308. uint32_t tbu, tbl, temp;
  309. /* from section 2.2.1 of the 32-bit PowerPC PEM */
  310. asm volatile(
  311. "1:\n"
  312. "mftbu %2\n"
  313. "mftb %0\n"
  314. "mftbu %1\n"
  315. "cmpw %2,%1\n"
  316. "bne 1b\n"
  317. : "=r"(tbl), "=r"(tbu), "=r"(temp)
  318. :
  319. : "cc");
  320. return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
  321. }
  322. #endif
  323. #elif defined(HAVE_GETHRTIME)
  324. #define AV_READ_TIME gethrtime
  325. #endif
  326. #ifdef AV_READ_TIME
  327. #define START_TIMER \
  328. uint64_t tend;\
  329. uint64_t tstart= AV_READ_TIME();\
  330. #define STOP_TIMER(id) \
  331. tend= AV_READ_TIME();\
  332. {\
  333. static uint64_t tsum=0;\
  334. static int tcount=0;\
  335. static int tskip_count=0;\
  336. if(tcount<2 || tend - tstart < FFMAX(8*tsum/tcount, 2000)){\
  337. tsum+= tend - tstart;\
  338. tcount++;\
  339. }else\
  340. tskip_count++;\
  341. if(((tcount+tskip_count)&(tcount+tskip_count-1))==0){\
  342. av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" dezicycles in %s, %d runs, %d skips\n",\
  343. tsum*10/tcount, id, tcount, tskip_count);\
  344. }\
  345. }
  346. #else
  347. #define START_TIMER
  348. #define STOP_TIMER(id) {}
  349. #endif
  350. #endif /* FFMPEG_COMMON_H */