1 // Copyright 2010 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Speed-critical decoding functions, default plain-C implementations.
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13
14 #include "./dsp.h"
15 #include "../dec/vp8i.h"
16
17 //------------------------------------------------------------------------------
18
clip_8b(int v)19 static WEBP_INLINE uint8_t clip_8b(int v) {
20 return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
21 }
22
23 //------------------------------------------------------------------------------
24 // Transforms (Paragraph 14.4)
25
26 #define STORE(x, y, v) \
27 dst[x + y * BPS] = clip_8b(dst[x + y * BPS] + ((v) >> 3))
28
29 #define STORE2(y, dc, d, c) do { \
30 const int DC = (dc); \
31 STORE(0, y, DC + (d)); \
32 STORE(1, y, DC + (c)); \
33 STORE(2, y, DC - (c)); \
34 STORE(3, y, DC - (d)); \
35 } while (0)
36
37 #define MUL1(a) ((((a) * 20091) >> 16) + (a))
38 #define MUL2(a) (((a) * 35468) >> 16)
39
TransformOne(const int16_t * in,uint8_t * dst)40 static void TransformOne(const int16_t* in, uint8_t* dst) {
41 int C[4 * 4], *tmp;
42 int i;
43 tmp = C;
44 for (i = 0; i < 4; ++i) { // vertical pass
45 const int a = in[0] + in[8]; // [-4096, 4094]
46 const int b = in[0] - in[8]; // [-4095, 4095]
47 const int c = MUL2(in[4]) - MUL1(in[12]); // [-3783, 3783]
48 const int d = MUL1(in[4]) + MUL2(in[12]); // [-3785, 3781]
49 tmp[0] = a + d; // [-7881, 7875]
50 tmp[1] = b + c; // [-7878, 7878]
51 tmp[2] = b - c; // [-7878, 7878]
52 tmp[3] = a - d; // [-7877, 7879]
53 tmp += 4;
54 in++;
55 }
56 // Each pass is expanding the dynamic range by ~3.85 (upper bound).
57 // The exact value is (2. + (20091 + 35468) / 65536).
58 // After the second pass, maximum interval is [-3794, 3794], assuming
59 // an input in [-2048, 2047] interval. We then need to add a dst value
60 // in the [0, 255] range.
61 // In the worst case scenario, the input to clip_8b() can be as large as
62 // [-60713, 60968].
63 tmp = C;
64 for (i = 0; i < 4; ++i) { // horizontal pass
65 const int dc = tmp[0] + 4;
66 const int a = dc + tmp[8];
67 const int b = dc - tmp[8];
68 const int c = MUL2(tmp[4]) - MUL1(tmp[12]);
69 const int d = MUL1(tmp[4]) + MUL2(tmp[12]);
70 STORE(0, 0, a + d);
71 STORE(1, 0, b + c);
72 STORE(2, 0, b - c);
73 STORE(3, 0, a - d);
74 tmp++;
75 dst += BPS;
76 }
77 }
78
79 // Simplified transform when only in[0], in[1] and in[4] are non-zero
TransformAC3(const int16_t * in,uint8_t * dst)80 static void TransformAC3(const int16_t* in, uint8_t* dst) {
81 const int a = in[0] + 4;
82 const int c4 = MUL2(in[4]);
83 const int d4 = MUL1(in[4]);
84 const int c1 = MUL2(in[1]);
85 const int d1 = MUL1(in[1]);
86 STORE2(0, a + d4, d1, c1);
87 STORE2(1, a + c4, d1, c1);
88 STORE2(2, a - c4, d1, c1);
89 STORE2(3, a - d4, d1, c1);
90 }
91 #undef MUL1
92 #undef MUL2
93 #undef STORE2
94
TransformTwo(const int16_t * in,uint8_t * dst,int do_two)95 static void TransformTwo(const int16_t* in, uint8_t* dst, int do_two) {
96 TransformOne(in, dst);
97 if (do_two) {
98 TransformOne(in + 16, dst + 4);
99 }
100 }
101
TransformUV(const int16_t * in,uint8_t * dst)102 static void TransformUV(const int16_t* in, uint8_t* dst) {
103 VP8Transform(in + 0 * 16, dst, 1);
104 VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
105 }
106
TransformDC(const int16_t * in,uint8_t * dst)107 static void TransformDC(const int16_t* in, uint8_t* dst) {
108 const int DC = in[0] + 4;
109 int i, j;
110 for (j = 0; j < 4; ++j) {
111 for (i = 0; i < 4; ++i) {
112 STORE(i, j, DC);
113 }
114 }
115 }
116
TransformDCUV(const int16_t * in,uint8_t * dst)117 static void TransformDCUV(const int16_t* in, uint8_t* dst) {
118 if (in[0 * 16]) VP8TransformDC(in + 0 * 16, dst);
119 if (in[1 * 16]) VP8TransformDC(in + 1 * 16, dst + 4);
120 if (in[2 * 16]) VP8TransformDC(in + 2 * 16, dst + 4 * BPS);
121 if (in[3 * 16]) VP8TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
122 }
123
124 #undef STORE
125
126 //------------------------------------------------------------------------------
127 // Paragraph 14.3
128
TransformWHT(const int16_t * in,int16_t * out)129 static void TransformWHT(const int16_t* in, int16_t* out) {
130 int tmp[16];
131 int i;
132 for (i = 0; i < 4; ++i) {
133 const int a0 = in[0 + i] + in[12 + i];
134 const int a1 = in[4 + i] + in[ 8 + i];
135 const int a2 = in[4 + i] - in[ 8 + i];
136 const int a3 = in[0 + i] - in[12 + i];
137 tmp[0 + i] = a0 + a1;
138 tmp[8 + i] = a0 - a1;
139 tmp[4 + i] = a3 + a2;
140 tmp[12 + i] = a3 - a2;
141 }
142 for (i = 0; i < 4; ++i) {
143 const int dc = tmp[0 + i * 4] + 3; // w/ rounder
144 const int a0 = dc + tmp[3 + i * 4];
145 const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
146 const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
147 const int a3 = dc - tmp[3 + i * 4];
148 out[ 0] = (a0 + a1) >> 3;
149 out[16] = (a3 + a2) >> 3;
150 out[32] = (a0 - a1) >> 3;
151 out[48] = (a3 - a2) >> 3;
152 out += 64;
153 }
154 }
155
156 void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
157
158 //------------------------------------------------------------------------------
159 // Intra predictions
160
161 #define DST(x, y) dst[(x) + (y) * BPS]
162
TrueMotion(uint8_t * dst,int size)163 static WEBP_INLINE void TrueMotion(uint8_t* dst, int size) {
164 const uint8_t* top = dst - BPS;
165 const uint8_t* const clip0 = VP8kclip1 - top[-1];
166 int y;
167 for (y = 0; y < size; ++y) {
168 const uint8_t* const clip = clip0 + dst[-1];
169 int x;
170 for (x = 0; x < size; ++x) {
171 dst[x] = clip[top[x]];
172 }
173 dst += BPS;
174 }
175 }
TM4(uint8_t * dst)176 static void TM4(uint8_t* dst) { TrueMotion(dst, 4); }
TM8uv(uint8_t * dst)177 static void TM8uv(uint8_t* dst) { TrueMotion(dst, 8); }
TM16(uint8_t * dst)178 static void TM16(uint8_t* dst) { TrueMotion(dst, 16); }
179
180 //------------------------------------------------------------------------------
181 // 16x16
182
VE16(uint8_t * dst)183 static void VE16(uint8_t* dst) { // vertical
184 int j;
185 for (j = 0; j < 16; ++j) {
186 memcpy(dst + j * BPS, dst - BPS, 16);
187 }
188 }
189
HE16(uint8_t * dst)190 static void HE16(uint8_t* dst) { // horizontal
191 int j;
192 for (j = 16; j > 0; --j) {
193 memset(dst, dst[-1], 16);
194 dst += BPS;
195 }
196 }
197
Put16(int v,uint8_t * dst)198 static WEBP_INLINE void Put16(int v, uint8_t* dst) {
199 int j;
200 for (j = 0; j < 16; ++j) {
201 memset(dst + j * BPS, v, 16);
202 }
203 }
204
DC16(uint8_t * dst)205 static void DC16(uint8_t* dst) { // DC
206 int DC = 16;
207 int j;
208 for (j = 0; j < 16; ++j) {
209 DC += dst[-1 + j * BPS] + dst[j - BPS];
210 }
211 Put16(DC >> 5, dst);
212 }
213
DC16NoTop(uint8_t * dst)214 static void DC16NoTop(uint8_t* dst) { // DC with top samples not available
215 int DC = 8;
216 int j;
217 for (j = 0; j < 16; ++j) {
218 DC += dst[-1 + j * BPS];
219 }
220 Put16(DC >> 4, dst);
221 }
222
DC16NoLeft(uint8_t * dst)223 static void DC16NoLeft(uint8_t* dst) { // DC with left samples not available
224 int DC = 8;
225 int i;
226 for (i = 0; i < 16; ++i) {
227 DC += dst[i - BPS];
228 }
229 Put16(DC >> 4, dst);
230 }
231
DC16NoTopLeft(uint8_t * dst)232 static void DC16NoTopLeft(uint8_t* dst) { // DC with no top and left samples
233 Put16(0x80, dst);
234 }
235
236 VP8PredFunc VP8PredLuma16[NUM_B_DC_MODES];
237
238 //------------------------------------------------------------------------------
239 // 4x4
240
241 #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
242 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
243
VE4(uint8_t * dst)244 static void VE4(uint8_t* dst) { // vertical
245 const uint8_t* top = dst - BPS;
246 const uint8_t vals[4] = {
247 AVG3(top[-1], top[0], top[1]),
248 AVG3(top[ 0], top[1], top[2]),
249 AVG3(top[ 1], top[2], top[3]),
250 AVG3(top[ 2], top[3], top[4])
251 };
252 int i;
253 for (i = 0; i < 4; ++i) {
254 memcpy(dst + i * BPS, vals, sizeof(vals));
255 }
256 }
257
HE4(uint8_t * dst)258 static void HE4(uint8_t* dst) { // horizontal
259 const int A = dst[-1 - BPS];
260 const int B = dst[-1];
261 const int C = dst[-1 + BPS];
262 const int D = dst[-1 + 2 * BPS];
263 const int E = dst[-1 + 3 * BPS];
264 WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(A, B, C));
265 WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(B, C, D));
266 WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(C, D, E));
267 WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(D, E, E));
268 }
269
DC4(uint8_t * dst)270 static void DC4(uint8_t* dst) { // DC
271 uint32_t dc = 4;
272 int i;
273 for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
274 dc >>= 3;
275 for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
276 }
277
RD4(uint8_t * dst)278 static void RD4(uint8_t* dst) { // Down-right
279 const int I = dst[-1 + 0 * BPS];
280 const int J = dst[-1 + 1 * BPS];
281 const int K = dst[-1 + 2 * BPS];
282 const int L = dst[-1 + 3 * BPS];
283 const int X = dst[-1 - BPS];
284 const int A = dst[0 - BPS];
285 const int B = dst[1 - BPS];
286 const int C = dst[2 - BPS];
287 const int D = dst[3 - BPS];
288 DST(0, 3) = AVG3(J, K, L);
289 DST(1, 3) = DST(0, 2) = AVG3(I, J, K);
290 DST(2, 3) = DST(1, 2) = DST(0, 1) = AVG3(X, I, J);
291 DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
292 DST(3, 2) = DST(2, 1) = DST(1, 0) = AVG3(B, A, X);
293 DST(3, 1) = DST(2, 0) = AVG3(C, B, A);
294 DST(3, 0) = AVG3(D, C, B);
295 }
296
LD4(uint8_t * dst)297 static void LD4(uint8_t* dst) { // Down-Left
298 const int A = dst[0 - BPS];
299 const int B = dst[1 - BPS];
300 const int C = dst[2 - BPS];
301 const int D = dst[3 - BPS];
302 const int E = dst[4 - BPS];
303 const int F = dst[5 - BPS];
304 const int G = dst[6 - BPS];
305 const int H = dst[7 - BPS];
306 DST(0, 0) = AVG3(A, B, C);
307 DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
308 DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
309 DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
310 DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
311 DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
312 DST(3, 3) = AVG3(G, H, H);
313 }
314
VR4(uint8_t * dst)315 static void VR4(uint8_t* dst) { // Vertical-Right
316 const int I = dst[-1 + 0 * BPS];
317 const int J = dst[-1 + 1 * BPS];
318 const int K = dst[-1 + 2 * BPS];
319 const int X = dst[-1 - BPS];
320 const int A = dst[0 - BPS];
321 const int B = dst[1 - BPS];
322 const int C = dst[2 - BPS];
323 const int D = dst[3 - BPS];
324 DST(0, 0) = DST(1, 2) = AVG2(X, A);
325 DST(1, 0) = DST(2, 2) = AVG2(A, B);
326 DST(2, 0) = DST(3, 2) = AVG2(B, C);
327 DST(3, 0) = AVG2(C, D);
328
329 DST(0, 3) = AVG3(K, J, I);
330 DST(0, 2) = AVG3(J, I, X);
331 DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
332 DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
333 DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
334 DST(3, 1) = AVG3(B, C, D);
335 }
336
VL4(uint8_t * dst)337 static void VL4(uint8_t* dst) { // Vertical-Left
338 const int A = dst[0 - BPS];
339 const int B = dst[1 - BPS];
340 const int C = dst[2 - BPS];
341 const int D = dst[3 - BPS];
342 const int E = dst[4 - BPS];
343 const int F = dst[5 - BPS];
344 const int G = dst[6 - BPS];
345 const int H = dst[7 - BPS];
346 DST(0, 0) = AVG2(A, B);
347 DST(1, 0) = DST(0, 2) = AVG2(B, C);
348 DST(2, 0) = DST(1, 2) = AVG2(C, D);
349 DST(3, 0) = DST(2, 2) = AVG2(D, E);
350
351 DST(0, 1) = AVG3(A, B, C);
352 DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
353 DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
354 DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
355 DST(3, 2) = AVG3(E, F, G);
356 DST(3, 3) = AVG3(F, G, H);
357 }
358
HU4(uint8_t * dst)359 static void HU4(uint8_t* dst) { // Horizontal-Up
360 const int I = dst[-1 + 0 * BPS];
361 const int J = dst[-1 + 1 * BPS];
362 const int K = dst[-1 + 2 * BPS];
363 const int L = dst[-1 + 3 * BPS];
364 DST(0, 0) = AVG2(I, J);
365 DST(2, 0) = DST(0, 1) = AVG2(J, K);
366 DST(2, 1) = DST(0, 2) = AVG2(K, L);
367 DST(1, 0) = AVG3(I, J, K);
368 DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
369 DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
370 DST(3, 2) = DST(2, 2) =
371 DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
372 }
373
HD4(uint8_t * dst)374 static void HD4(uint8_t* dst) { // Horizontal-Down
375 const int I = dst[-1 + 0 * BPS];
376 const int J = dst[-1 + 1 * BPS];
377 const int K = dst[-1 + 2 * BPS];
378 const int L = dst[-1 + 3 * BPS];
379 const int X = dst[-1 - BPS];
380 const int A = dst[0 - BPS];
381 const int B = dst[1 - BPS];
382 const int C = dst[2 - BPS];
383
384 DST(0, 0) = DST(2, 1) = AVG2(I, X);
385 DST(0, 1) = DST(2, 2) = AVG2(J, I);
386 DST(0, 2) = DST(2, 3) = AVG2(K, J);
387 DST(0, 3) = AVG2(L, K);
388
389 DST(3, 0) = AVG3(A, B, C);
390 DST(2, 0) = AVG3(X, A, B);
391 DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
392 DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
393 DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
394 DST(1, 3) = AVG3(L, K, J);
395 }
396
397 #undef DST
398 #undef AVG3
399 #undef AVG2
400
401 VP8PredFunc VP8PredLuma4[NUM_BMODES];
402
403 //------------------------------------------------------------------------------
404 // Chroma
405
VE8uv(uint8_t * dst)406 static void VE8uv(uint8_t* dst) { // vertical
407 int j;
408 for (j = 0; j < 8; ++j) {
409 memcpy(dst + j * BPS, dst - BPS, 8);
410 }
411 }
412
HE8uv(uint8_t * dst)413 static void HE8uv(uint8_t* dst) { // horizontal
414 int j;
415 for (j = 0; j < 8; ++j) {
416 memset(dst, dst[-1], 8);
417 dst += BPS;
418 }
419 }
420
421 // helper for chroma-DC predictions
Put8x8uv(uint8_t value,uint8_t * dst)422 static WEBP_INLINE void Put8x8uv(uint8_t value, uint8_t* dst) {
423 int j;
424 for (j = 0; j < 8; ++j) {
425 memset(dst + j * BPS, value, 8);
426 }
427 }
428
DC8uv(uint8_t * dst)429 static void DC8uv(uint8_t* dst) { // DC
430 int dc0 = 8;
431 int i;
432 for (i = 0; i < 8; ++i) {
433 dc0 += dst[i - BPS] + dst[-1 + i * BPS];
434 }
435 Put8x8uv(dc0 >> 4, dst);
436 }
437
DC8uvNoLeft(uint8_t * dst)438 static void DC8uvNoLeft(uint8_t* dst) { // DC with no left samples
439 int dc0 = 4;
440 int i;
441 for (i = 0; i < 8; ++i) {
442 dc0 += dst[i - BPS];
443 }
444 Put8x8uv(dc0 >> 3, dst);
445 }
446
DC8uvNoTop(uint8_t * dst)447 static void DC8uvNoTop(uint8_t* dst) { // DC with no top samples
448 int dc0 = 4;
449 int i;
450 for (i = 0; i < 8; ++i) {
451 dc0 += dst[-1 + i * BPS];
452 }
453 Put8x8uv(dc0 >> 3, dst);
454 }
455
DC8uvNoTopLeft(uint8_t * dst)456 static void DC8uvNoTopLeft(uint8_t* dst) { // DC with nothing
457 Put8x8uv(0x80, dst);
458 }
459
460 VP8PredFunc VP8PredChroma8[NUM_B_DC_MODES];
461
462 //------------------------------------------------------------------------------
463 // Edge filtering functions
464
465 // 4 pixels in, 2 pixels out
do_filter2(uint8_t * p,int step)466 static WEBP_INLINE void do_filter2(uint8_t* p, int step) {
467 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
468 const int a = 3 * (q0 - p0) + VP8ksclip1[p1 - q1]; // in [-893,892]
469 const int a1 = VP8ksclip2[(a + 4) >> 3]; // in [-16,15]
470 const int a2 = VP8ksclip2[(a + 3) >> 3];
471 p[-step] = VP8kclip1[p0 + a2];
472 p[ 0] = VP8kclip1[q0 - a1];
473 }
474
475 // 4 pixels in, 4 pixels out
do_filter4(uint8_t * p,int step)476 static WEBP_INLINE void do_filter4(uint8_t* p, int step) {
477 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
478 const int a = 3 * (q0 - p0);
479 const int a1 = VP8ksclip2[(a + 4) >> 3];
480 const int a2 = VP8ksclip2[(a + 3) >> 3];
481 const int a3 = (a1 + 1) >> 1;
482 p[-2*step] = VP8kclip1[p1 + a3];
483 p[- step] = VP8kclip1[p0 + a2];
484 p[ 0] = VP8kclip1[q0 - a1];
485 p[ step] = VP8kclip1[q1 - a3];
486 }
487
488 // 6 pixels in, 6 pixels out
do_filter6(uint8_t * p,int step)489 static WEBP_INLINE void do_filter6(uint8_t* p, int step) {
490 const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
491 const int q0 = p[0], q1 = p[step], q2 = p[2*step];
492 const int a = VP8ksclip1[3 * (q0 - p0) + VP8ksclip1[p1 - q1]];
493 // a is in [-128,127], a1 in [-27,27], a2 in [-18,18] and a3 in [-9,9]
494 const int a1 = (27 * a + 63) >> 7; // eq. to ((3 * a + 7) * 9) >> 7
495 const int a2 = (18 * a + 63) >> 7; // eq. to ((2 * a + 7) * 9) >> 7
496 const int a3 = (9 * a + 63) >> 7; // eq. to ((1 * a + 7) * 9) >> 7
497 p[-3*step] = VP8kclip1[p2 + a3];
498 p[-2*step] = VP8kclip1[p1 + a2];
499 p[- step] = VP8kclip1[p0 + a1];
500 p[ 0] = VP8kclip1[q0 - a1];
501 p[ step] = VP8kclip1[q1 - a2];
502 p[ 2*step] = VP8kclip1[q2 - a3];
503 }
504
hev(const uint8_t * p,int step,int thresh)505 static WEBP_INLINE int hev(const uint8_t* p, int step, int thresh) {
506 const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
507 return (VP8kabs0[p1 - p0] > thresh) || (VP8kabs0[q1 - q0] > thresh);
508 }
509
needs_filter(const uint8_t * p,int step,int t)510 static WEBP_INLINE int needs_filter(const uint8_t* p, int step, int t) {
511 const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
512 return ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) <= t);
513 }
514
needs_filter2(const uint8_t * p,int step,int t,int it)515 static WEBP_INLINE int needs_filter2(const uint8_t* p,
516 int step, int t, int it) {
517 const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step];
518 const int p0 = p[-step], q0 = p[0];
519 const int q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
520 if ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) > t) return 0;
521 return VP8kabs0[p3 - p2] <= it && VP8kabs0[p2 - p1] <= it &&
522 VP8kabs0[p1 - p0] <= it && VP8kabs0[q3 - q2] <= it &&
523 VP8kabs0[q2 - q1] <= it && VP8kabs0[q1 - q0] <= it;
524 }
525
526 //------------------------------------------------------------------------------
527 // Simple In-loop filtering (Paragraph 15.2)
528
SimpleVFilter16(uint8_t * p,int stride,int thresh)529 static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
530 int i;
531 const int thresh2 = 2 * thresh + 1;
532 for (i = 0; i < 16; ++i) {
533 if (needs_filter(p + i, stride, thresh2)) {
534 do_filter2(p + i, stride);
535 }
536 }
537 }
538
SimpleHFilter16(uint8_t * p,int stride,int thresh)539 static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
540 int i;
541 const int thresh2 = 2 * thresh + 1;
542 for (i = 0; i < 16; ++i) {
543 if (needs_filter(p + i * stride, 1, thresh2)) {
544 do_filter2(p + i * stride, 1);
545 }
546 }
547 }
548
SimpleVFilter16i(uint8_t * p,int stride,int thresh)549 static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
550 int k;
551 for (k = 3; k > 0; --k) {
552 p += 4 * stride;
553 SimpleVFilter16(p, stride, thresh);
554 }
555 }
556
SimpleHFilter16i(uint8_t * p,int stride,int thresh)557 static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
558 int k;
559 for (k = 3; k > 0; --k) {
560 p += 4;
561 SimpleHFilter16(p, stride, thresh);
562 }
563 }
564
565 //------------------------------------------------------------------------------
566 // Complex In-loop filtering (Paragraph 15.3)
567
FilterLoop26(uint8_t * p,int hstride,int vstride,int size,int thresh,int ithresh,int hev_thresh)568 static WEBP_INLINE void FilterLoop26(uint8_t* p,
569 int hstride, int vstride, int size,
570 int thresh, int ithresh, int hev_thresh) {
571 const int thresh2 = 2 * thresh + 1;
572 while (size-- > 0) {
573 if (needs_filter2(p, hstride, thresh2, ithresh)) {
574 if (hev(p, hstride, hev_thresh)) {
575 do_filter2(p, hstride);
576 } else {
577 do_filter6(p, hstride);
578 }
579 }
580 p += vstride;
581 }
582 }
583
FilterLoop24(uint8_t * p,int hstride,int vstride,int size,int thresh,int ithresh,int hev_thresh)584 static WEBP_INLINE void FilterLoop24(uint8_t* p,
585 int hstride, int vstride, int size,
586 int thresh, int ithresh, int hev_thresh) {
587 const int thresh2 = 2 * thresh + 1;
588 while (size-- > 0) {
589 if (needs_filter2(p, hstride, thresh2, ithresh)) {
590 if (hev(p, hstride, hev_thresh)) {
591 do_filter2(p, hstride);
592 } else {
593 do_filter4(p, hstride);
594 }
595 }
596 p += vstride;
597 }
598 }
599
600 // on macroblock edges
VFilter16(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)601 static void VFilter16(uint8_t* p, int stride,
602 int thresh, int ithresh, int hev_thresh) {
603 FilterLoop26(p, stride, 1, 16, thresh, ithresh, hev_thresh);
604 }
605
HFilter16(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)606 static void HFilter16(uint8_t* p, int stride,
607 int thresh, int ithresh, int hev_thresh) {
608 FilterLoop26(p, 1, stride, 16, thresh, ithresh, hev_thresh);
609 }
610
611 // on three inner edges
VFilter16i(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)612 static void VFilter16i(uint8_t* p, int stride,
613 int thresh, int ithresh, int hev_thresh) {
614 int k;
615 for (k = 3; k > 0; --k) {
616 p += 4 * stride;
617 FilterLoop24(p, stride, 1, 16, thresh, ithresh, hev_thresh);
618 }
619 }
620
HFilter16i(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)621 static void HFilter16i(uint8_t* p, int stride,
622 int thresh, int ithresh, int hev_thresh) {
623 int k;
624 for (k = 3; k > 0; --k) {
625 p += 4;
626 FilterLoop24(p, 1, stride, 16, thresh, ithresh, hev_thresh);
627 }
628 }
629
630 // 8-pixels wide variant, for chroma filtering
VFilter8(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)631 static void VFilter8(uint8_t* u, uint8_t* v, int stride,
632 int thresh, int ithresh, int hev_thresh) {
633 FilterLoop26(u, stride, 1, 8, thresh, ithresh, hev_thresh);
634 FilterLoop26(v, stride, 1, 8, thresh, ithresh, hev_thresh);
635 }
636
HFilter8(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)637 static void HFilter8(uint8_t* u, uint8_t* v, int stride,
638 int thresh, int ithresh, int hev_thresh) {
639 FilterLoop26(u, 1, stride, 8, thresh, ithresh, hev_thresh);
640 FilterLoop26(v, 1, stride, 8, thresh, ithresh, hev_thresh);
641 }
642
VFilter8i(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)643 static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
644 int thresh, int ithresh, int hev_thresh) {
645 FilterLoop24(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
646 FilterLoop24(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
647 }
648
HFilter8i(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)649 static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
650 int thresh, int ithresh, int hev_thresh) {
651 FilterLoop24(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
652 FilterLoop24(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
653 }
654
655 //------------------------------------------------------------------------------
656
657 VP8DecIdct2 VP8Transform;
658 VP8DecIdct VP8TransformAC3;
659 VP8DecIdct VP8TransformUV;
660 VP8DecIdct VP8TransformDC;
661 VP8DecIdct VP8TransformDCUV;
662
663 VP8LumaFilterFunc VP8VFilter16;
664 VP8LumaFilterFunc VP8HFilter16;
665 VP8ChromaFilterFunc VP8VFilter8;
666 VP8ChromaFilterFunc VP8HFilter8;
667 VP8LumaFilterFunc VP8VFilter16i;
668 VP8LumaFilterFunc VP8HFilter16i;
669 VP8ChromaFilterFunc VP8VFilter8i;
670 VP8ChromaFilterFunc VP8HFilter8i;
671 VP8SimpleFilterFunc VP8SimpleVFilter16;
672 VP8SimpleFilterFunc VP8SimpleHFilter16;
673 VP8SimpleFilterFunc VP8SimpleVFilter16i;
674 VP8SimpleFilterFunc VP8SimpleHFilter16i;
675
676 extern void VP8DspInitSSE2(void);
677 extern void VP8DspInitSSE41(void);
678 extern void VP8DspInitNEON(void);
679 extern void VP8DspInitMIPS32(void);
680 extern void VP8DspInitMIPSdspR2(void);
681
682 static volatile VP8CPUInfo dec_last_cpuinfo_used =
683 (VP8CPUInfo)&dec_last_cpuinfo_used;
684
VP8DspInit(void)685 WEBP_TSAN_IGNORE_FUNCTION void VP8DspInit(void) {
686 if (dec_last_cpuinfo_used == VP8GetCPUInfo) return;
687
688 VP8InitClipTables();
689
690 VP8TransformWHT = TransformWHT;
691 VP8Transform = TransformTwo;
692 VP8TransformUV = TransformUV;
693 VP8TransformDC = TransformDC;
694 VP8TransformDCUV = TransformDCUV;
695 VP8TransformAC3 = TransformAC3;
696
697 VP8VFilter16 = VFilter16;
698 VP8HFilter16 = HFilter16;
699 VP8VFilter8 = VFilter8;
700 VP8HFilter8 = HFilter8;
701 VP8VFilter16i = VFilter16i;
702 VP8HFilter16i = HFilter16i;
703 VP8VFilter8i = VFilter8i;
704 VP8HFilter8i = HFilter8i;
705 VP8SimpleVFilter16 = SimpleVFilter16;
706 VP8SimpleHFilter16 = SimpleHFilter16;
707 VP8SimpleVFilter16i = SimpleVFilter16i;
708 VP8SimpleHFilter16i = SimpleHFilter16i;
709
710 VP8PredLuma4[0] = DC4;
711 VP8PredLuma4[1] = TM4;
712 VP8PredLuma4[2] = VE4;
713 VP8PredLuma4[3] = HE4;
714 VP8PredLuma4[4] = RD4;
715 VP8PredLuma4[5] = VR4;
716 VP8PredLuma4[6] = LD4;
717 VP8PredLuma4[7] = VL4;
718 VP8PredLuma4[8] = HD4;
719 VP8PredLuma4[9] = HU4;
720
721 VP8PredLuma16[0] = DC16;
722 VP8PredLuma16[1] = TM16;
723 VP8PredLuma16[2] = VE16;
724 VP8PredLuma16[3] = HE16;
725 VP8PredLuma16[4] = DC16NoTop;
726 VP8PredLuma16[5] = DC16NoLeft;
727 VP8PredLuma16[6] = DC16NoTopLeft;
728
729 VP8PredChroma8[0] = DC8uv;
730 VP8PredChroma8[1] = TM8uv;
731 VP8PredChroma8[2] = VE8uv;
732 VP8PredChroma8[3] = HE8uv;
733 VP8PredChroma8[4] = DC8uvNoTop;
734 VP8PredChroma8[5] = DC8uvNoLeft;
735 VP8PredChroma8[6] = DC8uvNoTopLeft;
736
737 // If defined, use CPUInfo() to overwrite some pointers with faster versions.
738 if (VP8GetCPUInfo != NULL) {
739 #if defined(WEBP_USE_SSE2)
740 if (VP8GetCPUInfo(kSSE2)) {
741 VP8DspInitSSE2();
742 #if defined(WEBP_USE_SSE41)
743 if (VP8GetCPUInfo(kSSE4_1)) {
744 VP8DspInitSSE41();
745 }
746 #endif
747 }
748 #endif
749 #if defined(WEBP_USE_NEON)
750 if (VP8GetCPUInfo(kNEON)) {
751 VP8DspInitNEON();
752 }
753 #endif
754 #if defined(WEBP_USE_MIPS32)
755 if (VP8GetCPUInfo(kMIPS32)) {
756 VP8DspInitMIPS32();
757 }
758 #endif
759 #if defined(WEBP_USE_MIPS_DSP_R2)
760 if (VP8GetCPUInfo(kMIPSdspR2)) {
761 VP8DspInitMIPSdspR2();
762 }
763 #endif
764 }
765 dec_last_cpuinfo_used = VP8GetCPUInfo;
766 }
767