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.

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