1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/*!
************************************************************************
* \file
* ifunctions.h
*
* \brief
* define some inline functions that are used within the encoder.
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Karsten Sühring <suehring@hhi.de>
* - Alexis Tourapis <alexismt@ieee.org>
*
************************************************************************
*/
#ifndef _IFUNCTIONS_H_
#define _IFUNCTIONS_H_
# if !defined(WIN32) && (__STDC_VERSION__ < 199901L)
#define static
#define inline
#endif
#include <math.h>
#include <limits.h>
static inline short smin(short a, short b)
{
return (short) (((a) < (b)) ? (a) : (b));
}
static inline short smax(short a, short b)
{
return (short) (((a) > (b)) ? (a) : (b));
}
static inline int imin(int a, int b)
{/*
int retu;
_asm
{
mov eax, a
mov edx, b
cmp edx, eax
cmovle eax, edx
mov retu, eax
}
return retu;*/
return ((a) < (b)) ? (a) : (b);
}
static inline int imax(int a, int b)
{
return ((a) > (b)) ? (a) : (b);
}
static inline double dmin(double a, double b)
{
return ((a) < (b)) ? (a) : (b);
}
static inline double dmax(double a, double b)
{
return ((a) > (b)) ? (a) : (b);
}
static inline int64 i64min(int64 a, int64 b)
{
return ((a) < (b)) ? (a) : (b);
}
static inline int64 i64max(int64 a, int64 b)
{
return ((a) > (b)) ? (a) : (b);
}
static inline short sabs(short x)
{
static const short SHORT_BITS = (sizeof(short) * CHAR_BIT) - 1;
short y = (short) (x >> SHORT_BITS);
return (short) ((x ^ y) - y);
}
static inline int iabs(int x)
{
static const int INT_BITS = (sizeof(int) * CHAR_BIT) - 1;
int y = x >> INT_BITS;
return (x ^ y) - y;
}
static inline double dabs(double x)
{
return ((x) < 0) ? -(x) : (x);
}
static inline int64 i64abs(int64 x)
{
static const int64 INT64_BITS = (sizeof(int64) * CHAR_BIT) - 1;
int64 y = x >> INT64_BITS;
return (x ^ y) - y;
}
static inline double dabs2(double x)
{
return (x) * (x);
}
static inline int iabs2(int x)
{
return (x) * (x);
}
static inline int64 i64abs2(int64 x)
{
return (x) * (x);
}
static inline int isign(int x)
{
return ( (x > 0) - (x < 0));
}
static inline int isignab(int a, int b)
{
return ((b) < 0) ? -iabs(a) : iabs(a);
}
static inline int rshift_rnd(int x, int a)
{
return (a > 0) ? ((x + (1 << (a-1) )) >> a) : (x << (-a));
}
static inline int rshift_rnd_pos(int x, int a)
{
return (x + (1 << (a-1) )) >> a;
}
// flip a before calling
static inline int rshift_rnd_nonpos(int x, int a)
{
return (x << a);
}
static inline int rshift_rnd_sign(int x, int a)
{
return (x > 0) ? ( ( x + (1 << (a-1)) ) >> a ) : (-( ( iabs(x) + (1 << (a-1)) ) >> a ));
}
static inline unsigned int rshift_rnd_us(unsigned int x, unsigned int a)
{
return (a > 0) ? ((x + (1 << (a-1))) >> a) : x;
}
static inline int rshift_rnd_sf(int x, int a)
{
return ((x + (1 << (a-1) )) >> a);
}
static inline unsigned int rshift_rnd_us_sf(unsigned int x, unsigned int a)
{
return ((x + (1 << (a-1))) >> a);
}
static inline int iClip1(int high, int x)
{
if (x < 0)
return 0;
if (x > high)
return high;
return x;
/* old:
x = imax(x, 0);
x = imin(x, high);
return x;*/
}
static inline int iClip3(int low, int high, int x)
{
if (x < low)
return low;
if (x > high)
return high;
return x;
/* old:
x = imax(x, low);
x = imin(x, high);
return x;*/
}
static inline short sClip3(short low, short high, short x)
{
x = smax(x, low);
x = smin(x, high);
return x;
}
static inline double dClip3(double low, double high, double x)
{
x = dmax(x, low);
x = dmin(x, high);
return x;
}
static inline int weighted_cost(int factor, int bits)
{
return (((factor)*(bits))>>LAMBDA_ACCURACY_BITS);
}
static inline int RSD(int x)
{
return ((x&2)?(x|1):(x&(~1)));
}
static inline int power2(int x)
{
return 1 << (x);
}
static inline int float2int (float x)
{
return (int)((x < 0) ? (x - 0.5f) : (x + 0.5f));
}
#if ZEROSNR
static inline float psnr(int max_sample_sq, int samples, float sse_distortion )
{
return (float) (10.0 * log10(max_sample_sq * (double) ((double) samples / (sse_distortion < 1.0 ? 1.0 : sse_distortion))));
}
#else
static inline float psnr(int max_sample_sq, int samples, float sse_distortion )
{
return (float) (sse_distortion == 0.0 ? 0.0 : (10.0 * log10(max_sample_sq * (double) ((double) samples / sse_distortion))));
}
#endif
# if !defined(WIN32) && (__STDC_VERSION__ < 199901L)
#undef static
#undef inline
#endif
#endif
|