1
2 /*!
3 ************************************************************************
4 * \file
5 * ifunctions.h
6 *
7 * \brief
8 * define some inline functions that are used within the encoder.
9 *
10 * \author
11 * Main contributors (see contributors.h for copyright, address and affiliation details)
12 * - Karsten Suehring
13 * - Alexis Tourapis <alexismt@ieee.org>
14 *
15 ************************************************************************
16 */
17 #ifndef _IFUNCTIONS_H_
18 #define _IFUNCTIONS_H_
19
20 # if !(defined(WIN32) || defined(WIN64)) && (__STDC_VERSION__ < 199901L)
21 #define static
22 #define inline
23 #endif
24 #include <math.h>
25 #include <limits.h>
26
27
smin(short a,short b)28 static inline short smin(short a, short b)
29 {
30 return (short) (((a) < (b)) ? (a) : (b));
31 }
32
smax(short a,short b)33 static inline short smax(short a, short b)
34 {
35 return (short) (((a) > (b)) ? (a) : (b));
36 }
37
imin(int a,int b)38 static inline int imin(int a, int b)
39 {
40 return ((a) < (b)) ? (a) : (b);
41 }
42
imin3(int a,int b,int c)43 static inline int imin3(int a, int b, int c)
44 {
45 return ((a) < (b)) ? imin(a, c) : imin(b, c);
46 }
47
imax(int a,int b)48 static inline int imax(int a, int b)
49 {
50 return ((a) > (b)) ? (a) : (b);
51 }
52
imedian(int a,int b,int c)53 static inline int imedian(int a,int b,int c)
54 {
55 if (a > b) // a > b
56 {
57 if (b > c)
58 return(b); // a > b > c
59 else if (a > c)
60 return(c); // a > c > b
61 else
62 return(a); // c > a > b
63 }
64 else // b > a
65 {
66 if (a > c)
67 return(a); // b > a > c
68 else if (b > c)
69 return(c); // b > c > a
70 else
71 return(b); // c > b > a
72 }
73 }
74
imedian_old(int a,int b,int c)75 static inline int imedian_old(int a, int b, int c)
76 {
77 return (a + b + c - imin(a, imin(b, c)) - imax(a, imax(b ,c)));
78 }
79
dmin(double a,double b)80 static inline double dmin(double a, double b)
81 {
82 return ((a) < (b)) ? (a) : (b);
83 }
84
dmax(double a,double b)85 static inline double dmax(double a, double b)
86 {
87 return ((a) > (b)) ? (a) : (b);
88 }
89
i64min(int64 a,int64 b)90 static inline int64 i64min(int64 a, int64 b)
91 {
92 return ((a) < (b)) ? (a) : (b);
93 }
94
i64max(int64 a,int64 b)95 static inline int64 i64max(int64 a, int64 b)
96 {
97 return ((a) > (b)) ? (a) : (b);
98 }
99
distblkmin(distblk a,distblk b)100 static inline distblk distblkmin(distblk a, distblk b)
101 {
102 return ((a) < (b)) ? (a) : (b);
103 }
104
distblkmax(distblk a,distblk b)105 static inline distblk distblkmax(distblk a, distblk b)
106 {
107 return ((a) > (b)) ? (a) : (b);
108 }
109
sabs(short x)110 static inline short sabs(short x)
111 {
112 static const short SHORT_BITS = (sizeof(short) * CHAR_BIT) - 1;
113 short y = (short) (x >> SHORT_BITS);
114 return (short) ((x ^ y) - y);
115 }
116
iabs(int x)117 static inline int iabs(int x)
118 {
119 static const int INT_BITS = (sizeof(int) * CHAR_BIT) - 1;
120 int y = x >> INT_BITS;
121 return (x ^ y) - y;
122 }
123
dabs(double x)124 static inline double dabs(double x)
125 {
126 return ((x) < 0) ? -(x) : (x);
127 }
128
i64abs(int64 x)129 static inline int64 i64abs(int64 x)
130 {
131 static const int64 INT64_BITS = (sizeof(int64) * CHAR_BIT) - 1;
132 int64 y = x >> INT64_BITS;
133 return (x ^ y) - y;
134 }
135
dabs2(double x)136 static inline double dabs2(double x)
137 {
138 return (x) * (x);
139 }
140
iabs2(int x)141 static inline int iabs2(int x)
142 {
143 return (x) * (x);
144 }
145
i64abs2(int64 x)146 static inline int64 i64abs2(int64 x)
147 {
148 return (x) * (x);
149 }
150
isign(int x)151 static inline int isign(int x)
152 {
153 return ( (x > 0) - (x < 0));
154 }
155
isignab(int a,int b)156 static inline int isignab(int a, int b)
157 {
158 return ((b) < 0) ? -iabs(a) : iabs(a);
159 }
160
rshift_rnd(int x,int a)161 static inline int rshift_rnd(int x, int a)
162 {
163 return (a > 0) ? ((x + (1 << (a-1) )) >> a) : (x << (-a));
164 }
165
rshift_rnd_sign(int x,int a)166 static inline int rshift_rnd_sign(int x, int a)
167 {
168 return (x > 0) ? ( ( x + (1 << (a-1)) ) >> a ) : (-( ( iabs(x) + (1 << (a-1)) ) >> a ));
169 }
170
rshift_rnd_us(unsigned int x,unsigned int a)171 static inline unsigned int rshift_rnd_us(unsigned int x, unsigned int a)
172 {
173 return (a > 0) ? ((x + (1 << (a-1))) >> a) : x;
174 }
175
rshift_rnd_sf(int x,int a)176 static inline int rshift_rnd_sf(int x, int a)
177 {
178 return ((x + (1 << (a-1) )) >> a);
179 }
180
shift_off_sf(int x,int o,int a)181 static inline int shift_off_sf(int x, int o, int a)
182 {
183 return ((x + o) >> a);
184 }
185
rshift_rnd_us_sf(unsigned int x,unsigned int a)186 static inline unsigned int rshift_rnd_us_sf(unsigned int x, unsigned int a)
187 {
188 return ((x + (1 << (a-1))) >> a);
189 }
190
iClip1(int high,int x)191 static inline int iClip1(int high, int x)
192 {
193 x = imax(x, 0);
194 x = imin(x, high);
195
196 return x;
197 }
198
iClip3(int low,int high,int x)199 static inline int iClip3(int low, int high, int x)
200 {
201 x = imax(x, low);
202 x = imin(x, high);
203
204 return x;
205 }
206
sClip3(short low,short high,short x)207 static inline short sClip3(short low, short high, short x)
208 {
209 x = smax(x, low);
210 x = smin(x, high);
211
212 return x;
213 }
214
dClip3(double low,double high,double x)215 static inline double dClip3(double low, double high, double x)
216 {
217 x = dmax(x, low);
218 x = dmin(x, high);
219
220 return x;
221 }
222
223
weighted_cost(int factor,int bits)224 static inline distblk weighted_cost(int factor, int bits)
225 {
226 #if JCOST_CALC_SCALEUP
227 return (((distblk)(factor))*((distblk)(bits)));
228 #else
229 #if (USE_RND_COST)
230 return (rshift_rnd_sf((lambda) * (bits), LAMBDA_ACCURACY_BITS));
231 #else
232 return (((factor)*(bits))>>LAMBDA_ACCURACY_BITS);
233 #endif
234 #endif
235 }
236
RSD(int x)237 static inline int RSD(int x)
238 {
239 return ((x&2)?(x|1):(x&(~1)));
240 }
241
power2(int x)242 static inline int power2(int x)
243 {
244 return 1 << (x);
245 }
246
247
248 static const int64 po2[64] = {0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000,
249 0x10000,0x20000,0x40000,0x80000,0x100000,0x200000,0x400000,0x800000,0x1000000,0x2000000,0x4000000,0x8000000,
250 0x10000000,0x20000000,0x40000000,0x80000000,0x100000000,0x200000000,0x400000000,0x800000000,
251 0x1000000000,0x2000000000,0x4000000000,0x8000000000,0x10000000000,0x20000000000,0x40000000000,0x80000000000,
252 0x100000000000,0x200000000000,0x400000000000,0x800000000000,
253 0x1000000000000,0x2000000000000,0x4000000000000,0x8000000000000,
254 0x10000000000000,0x20000000000000,0x40000000000000,0x80000000000000,
255 0x100000000000000,0x200000000000000,0x400000000000000,0x800000000000000,
256 0x1000000000000000,0x2000000000000000,0x4000000000000000,0x8000000000000000};
257
i64_power2(int x)258 static inline int64 i64_power2(int x)
259 {
260 return((x > 63) ? 0 : po2[x]);
261 }
262
float2int(float x)263 static inline int float2int (float x)
264 {
265 return (int)((x < 0) ? (x - 0.5f) : (x + 0.5f));
266 }
267
get_bit(int64 x,int n)268 static inline int get_bit(int64 x,int n)
269 {
270 return (int)(((x >> n) & 1));
271 }
272
273 #if ZEROSNR
psnr(int max_sample_sq,int samples,float sse_distortion)274 static inline float psnr(int max_sample_sq, int samples, float sse_distortion )
275 {
276 return (float) (10.0 * log10(max_sample_sq * (double) ((double) samples / (sse_distortion < 1.0 ? 1.0 : sse_distortion))));
277 }
278 #else
psnr(int max_sample_sq,int samples,float sse_distortion)279 static inline float psnr(int max_sample_sq, int samples, float sse_distortion )
280 {
281 return (float) (sse_distortion == 0.0 ? 0.0 : (10.0 * log10(max_sample_sq * (double) ((double) samples / sse_distortion))));
282 }
283 #endif
284
CheckCost_Shift(int64 mcost,int64 min_mcost)285 static inline int CheckCost_Shift(int64 mcost, int64 min_mcost)
286 {
287 if((mcost<<LAMBDA_ACCURACY_BITS) >= min_mcost)
288 return 1;
289 else
290 return 0;
291 }
292
CheckCost(int64 mcost,int64 min_mcost)293 static inline int CheckCost(int64 mcost, int64 min_mcost)
294 {
295 return ((mcost) >= (min_mcost));
296 }
297
down_scale(distblk * pblkdistCost)298 static inline void down_scale(distblk *pblkdistCost)
299 {
300 #if JCOST_CALC_SCALEUP
301 *pblkdistCost = (*pblkdistCost)>>LAMBDA_ACCURACY_BITS;
302 #endif
303 }
304
up_scale(distblk * pblkdistCost)305 static inline void up_scale(distblk *pblkdistCost)
306 {
307 #if JCOST_CALC_SCALEUP
308 *pblkdistCost = (*pblkdistCost)<<LAMBDA_ACCURACY_BITS;
309 #endif
310 }
311
dist_scale(distblk blkdistCost)312 static inline distblk dist_scale(distblk blkdistCost)
313 {
314 #if JCOST_CALC_SCALEUP
315 return ((blkdistCost)<<LAMBDA_ACCURACY_BITS);
316 #else
317 return (blkdistCost);
318 #endif
319 }
320
dist_down(distblk blkdistCost)321 static inline int dist_down(distblk blkdistCost)
322 {
323 #if JCOST_CALC_SCALEUP
324 return ((int)((blkdistCost)>>LAMBDA_ACCURACY_BITS));
325 #else
326 return ((int)blkdistCost);
327 #endif
328 }
329
330 /*!
331 ************************************************************************
332 * \brief
333 * calculate RoundLog2(uiVal)
334 ************************************************************************
335 */
RoundLog2(int iValue)336 static inline int RoundLog2 (int iValue)
337 {
338 int iRet = 0;
339 int iValue_square = iValue * iValue;
340 while ((1 << (iRet + 1)) <= iValue_square)
341 ++iRet;
342
343 iRet = (iRet + 1) >> 1;
344 return iRet;
345 }
346
free_pointer(void * pointer)347 static inline void free_pointer(void *pointer)
348 {
349 if (pointer != NULL)
350 {
351 free(pointer);
352 // pointer = NULL; // we would only set the copy of the pointer to zero
353 }
354 }
355
i32_swap(int * x,int * y)356 static inline void i32_swap(int *x, int *y)
357 {
358 int temp = *x;
359 *x = *y;
360 *y = temp;
361 }
362
i64_swap(int64 * x,int64 * y)363 static inline void i64_swap(int64 *x, int64 *y)
364 {
365 int64 temp = *x;
366 *x = *y;
367 *y = temp;
368 }
369
is_intra_mb(short mb_type)370 static inline int is_intra_mb(short mb_type)
371 {
372 return (mb_type==SI4MB || mb_type==I4MB || mb_type==I16MB || mb_type==I8MB || mb_type==IPCM);
373 }
374
375 # if !(defined(WIN32) || defined(WIN64)) && (__STDC_VERSION__ < 199901L)
376 #undef static
377 #undef inline
378 #endif
379
380 #endif
381
382