1 
2 #ifndef __SCHRO_UTILS_H__
3 #define __SCHRO_UTILS_H__
4 
5 #if defined(_MSC_VER)
6 #ifndef SCHRO_NO_STDINT_TYPEDEFS
7 typedef __int8 int8_t;
8 typedef __int16 int16_t;
9 typedef __int32 int32_t;
10 typedef unsigned __int8 uint8_t;
11 typedef unsigned __int16 uint16_t;
12 typedef unsigned __int32 uint32_t;
13 #endif
14 #else
15 #include <stdint.h>
16 #endif
17 
18 #ifndef TRUE
19 #define TRUE 1
20 #endif
21 #ifndef FALSE
22 #define FALSE 0
23 #endif
24 
25 typedef uint32_t SchroPictureNumber;
26 typedef unsigned int schro_bool;
27 
28 /* This must match the CUDA stream type, as we don't want to include the CUDA
29    includes in any of the Schroedinger include files.
30  */
31 typedef int SchroCUDAStream;
32 
33 #ifdef SCHRO_ENABLE_UNSTABLE_API
34 
35 #if defined(_MSC_VER)
36 #define inline __inline
37 #endif
38 
39 #define SCHRO_PICTURE_NUMBER_INVALID (-1)
40 
41 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
42 #define DIVIDE_ROUND_UP(a,b) (((a) + (b) - 1)/(b))
43 #ifndef MIN
44 #define MIN(a,b) ((a)<(b) ? (a) : (b))
45 #endif
46 #ifndef MAX
47 #define MAX(a,b) ((a)>(b) ? (a) : (b))
48 #endif
49 #ifndef CLAMP
50 #define CLAMP(x,a,b) ((x)<(a) ? (a) : ((x)>(b) ? (b) : (x)))
51 #endif
52 #define NEED_CLAMP(x,y,a,b) ((x) < (a) || (y) > (b))
53 #define ROUND_UP_SHIFT(x,y) (((x) + (1<<(y)) - 1)>>(y))
54 #define ROUND_UP_POW2(x,y) (((x) + (1<<(y)) - 1)&((~0)<<(y)))
55 #define ROUND_UP_2(x) ROUND_UP_POW2(x,1)
56 #define ROUND_UP_4(x) ROUND_UP_POW2(x,2)
57 #define ROUND_UP_8(x) ROUND_UP_POW2(x,3)
58 #define ROUND_UP_16(x) ROUND_UP_POW2(x,4)
59 #define ROUND_UP_64(x) ROUND_UP_POW2(x,6)
60 #define OFFSET(ptr,offset) ((void *)(((uint8_t *)(ptr)) + (offset)))
61 #define ROUND_SHIFT(x,y) (((x) + (1<<((y)-1)))>>(y))
62 
63 #define schro_divide(a,b) (((a)<0)?(((a) - (b) + 1)/(b)):((a)/(b)))
64 #define schro_divide3(a) (((a)*21845 + 10922)>>16)
65 
66 #endif
67 
68 #define SCHRO_OFFSET(ptr,offset) ((void *)(((uint8_t *)(ptr)) + (offset)))
69 #define SCHRO_GET(ptr, offset, type) (*(type *)((uint8_t *)(ptr) + (offset)) )
70 
71 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
72 #define SCHRO_GNUC_PREREQ(maj, min) \
73   ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
74 #else
75 #define SCHRO_GNUC_PREREQ(maj, min) 0
76 #endif
77 
78 #if SCHRO_GNUC_PREREQ(3,3) && defined(__ELF__)
79 #define SCHRO_INTERNAL __attribute__ ((visibility ("internal")))
80 #else
81 #define SCHRO_INTERNAL
82 #endif
83 
84 #if SCHRO_GNUC_PREREQ(3,3)
85 #define SCHRO_DEPRECATED __attribute__ ((deprecated))
86 #else
87 #define SCHRO_DEPRECATED
88 #endif
89 
90 #if SCHRO_GNUC_PREREQ(3,3)
91 #define SCHRO_UNUSED __attribute__ ((unused))
92 #else
93 #define SCHRO_UNUSED
94 #endif
95 
96 #ifdef __cplusplus
97 #define SCHRO_BEGIN_DECLS extern "C" {
98 #define SCHRO_END_DECLS }
99 #else
100 #define SCHRO_BEGIN_DECLS
101 #define SCHRO_END_DECLS
102 #endif
103 
104 #if SCHRO_GNUC_PREREQ(3,0) && defined(__OPTIMIZE__)
105 #define SCHRO_LIKELY(expr) (__builtin_expect ((expr), 1))
106 #define SCHRO_UNLIKELY(expr) (__builtin_expect ((expr), 0))
107 #else
108 #define SCHRO_LIKELY(expr) (expr)
109 #define SCHRO_UNLIKELY(expr) (expr)
110 #endif
111 
112 #ifndef M_PI
113 #define M_PI 3.14159265358979323846
114 #endif
115 #ifndef M_LN10
116 #define M_LN10 2.30258509299404568402
117 #endif
118 
119 #if defined(_MSC_VER)
120 #define rint(x) (floor((x) + 0.5))
121 #endif
122 
123 SCHRO_BEGIN_DECLS
124 
125 #ifdef SCHRO_ENABLE_UNSTABLE_API
126 
127 void * schro_malloc (int size);
128 void * schro_malloc0 (int size);
129 void * schro_realloc (void *ptr, int size);
130 void schro_free (void *ptr);
131 
132 int muldiv64 (int a, int b, int c);
133 int schro_utils_multiplier_to_quant_index (double x);
134 int schro_dequantise (int q, int quant_factor, int quant_offset);
135 int schro_quantise (int value, int quant_factor, int quant_offset);
136 void schro_quantise_s16 (int16_t *dest, int16_t *src, int quant_factor,
137     int quant_offset, int n);
138 void schro_quantise_s32 (int32_t *dest, int32_t *src, int quant_factor,
139     int quant_offset, int n);
140 void schro_quantise_s16_table (int16_t *dest, int16_t *src, int quant_index,
141     schro_bool is_intra, int n);
142 void schro_dequantise_s16 (int16_t *dest, int16_t *src, int quant_factor,
143     int quant_offset, int n);
144 void schro_dequantise_s16_table (int16_t *dest, int16_t *src, int quant_index,
145     schro_bool is_intra, int n);
146 double schro_utils_probability_to_entropy (double x);
147 double schro_utils_entropy (double a, double total);
148 void schro_utils_reduce_fraction (int *n, int *d);
149 double schro_utils_get_time (void);
150 
151 #endif
152 
153 SCHRO_END_DECLS
154 
155 #endif
156 
157