xref: /aosp_15_r20/external/webp/src/dsp/dec.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
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 ([email protected])
13 
14 #include <assert.h>
15 
16 #include "src/dsp/dsp.h"
17 #include "src/dec/vp8i_dec.h"
18 #include "src/utils/utils.h"
19 
20 //------------------------------------------------------------------------------
21 
clip_8b(int v)22 static WEBP_INLINE uint8_t clip_8b(int v) {
23   return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
24 }
25 
26 //------------------------------------------------------------------------------
27 // Transforms (Paragraph 14.4)
28 
29 #define STORE(x, y, v) \
30   dst[(x) + (y) * BPS] = clip_8b(dst[(x) + (y) * BPS] + ((v) >> 3))
31 
32 #define STORE2(y, dc, d, c) do {    \
33   const int DC = (dc);              \
34   STORE(0, y, DC + (d));            \
35   STORE(1, y, DC + (c));            \
36   STORE(2, y, DC - (c));            \
37   STORE(3, y, DC - (d));            \
38 } while (0)
39 
40 #if !WEBP_NEON_OMIT_C_CODE
TransformOne_C(const int16_t * in,uint8_t * dst)41 static void TransformOne_C(const int16_t* in, uint8_t* dst) {
42   int C[4 * 4], *tmp;
43   int i;
44   tmp = C;
45   for (i = 0; i < 4; ++i) {    // vertical pass
46     const int a = in[0] + in[8];    // [-4096, 4094]
47     const int b = in[0] - in[8];    // [-4095, 4095]
48     const int c = WEBP_TRANSFORM_AC3_MUL2(in[4]) -
49                   WEBP_TRANSFORM_AC3_MUL1(in[12]);  // [-3783, 3783]
50     const int d = WEBP_TRANSFORM_AC3_MUL1(in[4]) +
51                   WEBP_TRANSFORM_AC3_MUL2(in[12]);  // [-3785, 3781]
52     tmp[0] = a + d;   // [-7881, 7875]
53     tmp[1] = b + c;   // [-7878, 7878]
54     tmp[2] = b - c;   // [-7878, 7878]
55     tmp[3] = a - d;   // [-7877, 7879]
56     tmp += 4;
57     in++;
58   }
59   // Each pass is expanding the dynamic range by ~3.85 (upper bound).
60   // The exact value is (2. + (20091 + 35468) / 65536).
61   // After the second pass, maximum interval is [-3794, 3794], assuming
62   // an input in [-2048, 2047] interval. We then need to add a dst value
63   // in the [0, 255] range.
64   // In the worst case scenario, the input to clip_8b() can be as large as
65   // [-60713, 60968].
66   tmp = C;
67   for (i = 0; i < 4; ++i) {    // horizontal pass
68     const int dc = tmp[0] + 4;
69     const int a =  dc +  tmp[8];
70     const int b =  dc -  tmp[8];
71     const int c =
72         WEBP_TRANSFORM_AC3_MUL2(tmp[4]) - WEBP_TRANSFORM_AC3_MUL1(tmp[12]);
73     const int d =
74         WEBP_TRANSFORM_AC3_MUL1(tmp[4]) + WEBP_TRANSFORM_AC3_MUL2(tmp[12]);
75     STORE(0, 0, a + d);
76     STORE(1, 0, b + c);
77     STORE(2, 0, b - c);
78     STORE(3, 0, a - d);
79     tmp++;
80     dst += BPS;
81   }
82 }
83 
84 // Simplified transform when only in[0], in[1] and in[4] are non-zero
TransformAC3_C(const int16_t * in,uint8_t * dst)85 static void TransformAC3_C(const int16_t* in, uint8_t* dst) {
86   const int a = in[0] + 4;
87   const int c4 = WEBP_TRANSFORM_AC3_MUL2(in[4]);
88   const int d4 = WEBP_TRANSFORM_AC3_MUL1(in[4]);
89   const int c1 = WEBP_TRANSFORM_AC3_MUL2(in[1]);
90   const int d1 = WEBP_TRANSFORM_AC3_MUL1(in[1]);
91   STORE2(0, a + d4, d1, c1);
92   STORE2(1, a + c4, d1, c1);
93   STORE2(2, a - c4, d1, c1);
94   STORE2(3, a - d4, d1, c1);
95 }
96 #undef STORE2
97 
TransformTwo_C(const int16_t * in,uint8_t * dst,int do_two)98 static void TransformTwo_C(const int16_t* in, uint8_t* dst, int do_two) {
99   TransformOne_C(in, dst);
100   if (do_two) {
101     TransformOne_C(in + 16, dst + 4);
102   }
103 }
104 #endif  // !WEBP_NEON_OMIT_C_CODE
105 
TransformUV_C(const int16_t * in,uint8_t * dst)106 static void TransformUV_C(const int16_t* in, uint8_t* dst) {
107   VP8Transform(in + 0 * 16, dst, 1);
108   VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
109 }
110 
111 #if !WEBP_NEON_OMIT_C_CODE
TransformDC_C(const int16_t * in,uint8_t * dst)112 static void TransformDC_C(const int16_t* in, uint8_t* dst) {
113   const int DC = in[0] + 4;
114   int i, j;
115   for (j = 0; j < 4; ++j) {
116     for (i = 0; i < 4; ++i) {
117       STORE(i, j, DC);
118     }
119   }
120 }
121 #endif  // !WEBP_NEON_OMIT_C_CODE
122 
TransformDCUV_C(const int16_t * in,uint8_t * dst)123 static void TransformDCUV_C(const int16_t* in, uint8_t* dst) {
124   if (in[0 * 16]) VP8TransformDC(in + 0 * 16, dst);
125   if (in[1 * 16]) VP8TransformDC(in + 1 * 16, dst + 4);
126   if (in[2 * 16]) VP8TransformDC(in + 2 * 16, dst + 4 * BPS);
127   if (in[3 * 16]) VP8TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
128 }
129 
130 #undef STORE
131 
132 //------------------------------------------------------------------------------
133 // Paragraph 14.3
134 
135 #if !WEBP_NEON_OMIT_C_CODE
TransformWHT_C(const int16_t * in,int16_t * out)136 static void TransformWHT_C(const int16_t* in, int16_t* out) {
137   int tmp[16];
138   int i;
139   for (i = 0; i < 4; ++i) {
140     const int a0 = in[0 + i] + in[12 + i];
141     const int a1 = in[4 + i] + in[ 8 + i];
142     const int a2 = in[4 + i] - in[ 8 + i];
143     const int a3 = in[0 + i] - in[12 + i];
144     tmp[0  + i] = a0 + a1;
145     tmp[8  + i] = a0 - a1;
146     tmp[4  + i] = a3 + a2;
147     tmp[12 + i] = a3 - a2;
148   }
149   for (i = 0; i < 4; ++i) {
150     const int dc = tmp[0 + i * 4] + 3;    // w/ rounder
151     const int a0 = dc             + tmp[3 + i * 4];
152     const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
153     const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
154     const int a3 = dc             - tmp[3 + i * 4];
155     out[ 0] = (a0 + a1) >> 3;
156     out[16] = (a3 + a2) >> 3;
157     out[32] = (a0 - a1) >> 3;
158     out[48] = (a3 - a2) >> 3;
159     out += 64;
160   }
161 }
162 #endif  // !WEBP_NEON_OMIT_C_CODE
163 
164 void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
165 
166 //------------------------------------------------------------------------------
167 // Intra predictions
168 
169 #define DST(x, y) dst[(x) + (y) * BPS]
170 
171 #if !WEBP_NEON_OMIT_C_CODE
TrueMotion(uint8_t * dst,int size)172 static WEBP_INLINE void TrueMotion(uint8_t* dst, int size) {
173   const uint8_t* top = dst - BPS;
174   const uint8_t* const clip0 = VP8kclip1 - top[-1];
175   int y;
176   for (y = 0; y < size; ++y) {
177     const uint8_t* const clip = clip0 + dst[-1];
178     int x;
179     for (x = 0; x < size; ++x) {
180       dst[x] = clip[top[x]];
181     }
182     dst += BPS;
183   }
184 }
TM4_C(uint8_t * dst)185 static void TM4_C(uint8_t* dst)   { TrueMotion(dst, 4); }
TM8uv_C(uint8_t * dst)186 static void TM8uv_C(uint8_t* dst) { TrueMotion(dst, 8); }
TM16_C(uint8_t * dst)187 static void TM16_C(uint8_t* dst)  { TrueMotion(dst, 16); }
188 
189 //------------------------------------------------------------------------------
190 // 16x16
191 
VE16_C(uint8_t * dst)192 static void VE16_C(uint8_t* dst) {     // vertical
193   int j;
194   for (j = 0; j < 16; ++j) {
195     memcpy(dst + j * BPS, dst - BPS, 16);
196   }
197 }
198 
HE16_C(uint8_t * dst)199 static void HE16_C(uint8_t* dst) {     // horizontal
200   int j;
201   for (j = 16; j > 0; --j) {
202     memset(dst, dst[-1], 16);
203     dst += BPS;
204   }
205 }
206 
Put16(int v,uint8_t * dst)207 static WEBP_INLINE void Put16(int v, uint8_t* dst) {
208   int j;
209   for (j = 0; j < 16; ++j) {
210     memset(dst + j * BPS, v, 16);
211   }
212 }
213 
DC16_C(uint8_t * dst)214 static void DC16_C(uint8_t* dst) {    // DC
215   int DC = 16;
216   int j;
217   for (j = 0; j < 16; ++j) {
218     DC += dst[-1 + j * BPS] + dst[j - BPS];
219   }
220   Put16(DC >> 5, dst);
221 }
222 
DC16NoTop_C(uint8_t * dst)223 static void DC16NoTop_C(uint8_t* dst) {   // DC with top samples not available
224   int DC = 8;
225   int j;
226   for (j = 0; j < 16; ++j) {
227     DC += dst[-1 + j * BPS];
228   }
229   Put16(DC >> 4, dst);
230 }
231 
DC16NoLeft_C(uint8_t * dst)232 static void DC16NoLeft_C(uint8_t* dst) {  // DC with left samples not available
233   int DC = 8;
234   int i;
235   for (i = 0; i < 16; ++i) {
236     DC += dst[i - BPS];
237   }
238   Put16(DC >> 4, dst);
239 }
240 
DC16NoTopLeft_C(uint8_t * dst)241 static void DC16NoTopLeft_C(uint8_t* dst) {  // DC with no top and left samples
242   Put16(0x80, dst);
243 }
244 #endif  // !WEBP_NEON_OMIT_C_CODE
245 
246 VP8PredFunc VP8PredLuma16[NUM_B_DC_MODES];
247 
248 //------------------------------------------------------------------------------
249 // 4x4
250 
251 #define AVG3(a, b, c) ((uint8_t)(((a) + 2 * (b) + (c) + 2) >> 2))
252 #define AVG2(a, b) (((a) + (b) + 1) >> 1)
253 
254 #if !WEBP_NEON_OMIT_C_CODE
VE4_C(uint8_t * dst)255 static void VE4_C(uint8_t* dst) {    // vertical
256   const uint8_t* top = dst - BPS;
257   const uint8_t vals[4] = {
258     AVG3(top[-1], top[0], top[1]),
259     AVG3(top[ 0], top[1], top[2]),
260     AVG3(top[ 1], top[2], top[3]),
261     AVG3(top[ 2], top[3], top[4])
262   };
263   int i;
264   for (i = 0; i < 4; ++i) {
265     memcpy(dst + i * BPS, vals, sizeof(vals));
266   }
267 }
268 #endif  // !WEBP_NEON_OMIT_C_CODE
269 
HE4_C(uint8_t * dst)270 static void HE4_C(uint8_t* dst) {    // horizontal
271   const int A = dst[-1 - BPS];
272   const int B = dst[-1];
273   const int C = dst[-1 + BPS];
274   const int D = dst[-1 + 2 * BPS];
275   const int E = dst[-1 + 3 * BPS];
276   WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(A, B, C));
277   WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(B, C, D));
278   WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(C, D, E));
279   WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(D, E, E));
280 }
281 
282 #if !WEBP_NEON_OMIT_C_CODE
DC4_C(uint8_t * dst)283 static void DC4_C(uint8_t* dst) {   // DC
284   uint32_t dc = 4;
285   int i;
286   for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
287   dc >>= 3;
288   for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
289 }
290 
RD4_C(uint8_t * dst)291 static void RD4_C(uint8_t* dst) {   // Down-right
292   const int I = dst[-1 + 0 * BPS];
293   const int J = dst[-1 + 1 * BPS];
294   const int K = dst[-1 + 2 * BPS];
295   const int L = dst[-1 + 3 * BPS];
296   const int X = dst[-1 - BPS];
297   const int A = dst[0 - BPS];
298   const int B = dst[1 - BPS];
299   const int C = dst[2 - BPS];
300   const int D = dst[3 - BPS];
301   DST(0, 3)                                     = AVG3(J, K, L);
302   DST(1, 3) = DST(0, 2)                         = AVG3(I, J, K);
303   DST(2, 3) = DST(1, 2) = DST(0, 1)             = AVG3(X, I, J);
304   DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
305               DST(3, 2) = DST(2, 1) = DST(1, 0) = AVG3(B, A, X);
306                           DST(3, 1) = DST(2, 0) = AVG3(C, B, A);
307                                       DST(3, 0) = AVG3(D, C, B);
308 }
309 
LD4_C(uint8_t * dst)310 static void LD4_C(uint8_t* dst) {   // Down-Left
311   const int A = dst[0 - BPS];
312   const int B = dst[1 - BPS];
313   const int C = dst[2 - BPS];
314   const int D = dst[3 - BPS];
315   const int E = dst[4 - BPS];
316   const int F = dst[5 - BPS];
317   const int G = dst[6 - BPS];
318   const int H = dst[7 - BPS];
319   DST(0, 0)                                     = AVG3(A, B, C);
320   DST(1, 0) = DST(0, 1)                         = AVG3(B, C, D);
321   DST(2, 0) = DST(1, 1) = DST(0, 2)             = AVG3(C, D, E);
322   DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
323               DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
324                           DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
325                                       DST(3, 3) = AVG3(G, H, H);
326 }
327 #endif  // !WEBP_NEON_OMIT_C_CODE
328 
VR4_C(uint8_t * dst)329 static void VR4_C(uint8_t* dst) {   // Vertical-Right
330   const int I = dst[-1 + 0 * BPS];
331   const int J = dst[-1 + 1 * BPS];
332   const int K = dst[-1 + 2 * BPS];
333   const int X = dst[-1 - BPS];
334   const int A = dst[0 - BPS];
335   const int B = dst[1 - BPS];
336   const int C = dst[2 - BPS];
337   const int D = dst[3 - BPS];
338   DST(0, 0) = DST(1, 2) = AVG2(X, A);
339   DST(1, 0) = DST(2, 2) = AVG2(A, B);
340   DST(2, 0) = DST(3, 2) = AVG2(B, C);
341   DST(3, 0)             = AVG2(C, D);
342 
343   DST(0, 3) =             AVG3(K, J, I);
344   DST(0, 2) =             AVG3(J, I, X);
345   DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
346   DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
347   DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
348   DST(3, 1) =             AVG3(B, C, D);
349 }
350 
VL4_C(uint8_t * dst)351 static void VL4_C(uint8_t* dst) {   // Vertical-Left
352   const int A = dst[0 - BPS];
353   const int B = dst[1 - BPS];
354   const int C = dst[2 - BPS];
355   const int D = dst[3 - BPS];
356   const int E = dst[4 - BPS];
357   const int F = dst[5 - BPS];
358   const int G = dst[6 - BPS];
359   const int H = dst[7 - BPS];
360   DST(0, 0) =             AVG2(A, B);
361   DST(1, 0) = DST(0, 2) = AVG2(B, C);
362   DST(2, 0) = DST(1, 2) = AVG2(C, D);
363   DST(3, 0) = DST(2, 2) = AVG2(D, E);
364 
365   DST(0, 1) =             AVG3(A, B, C);
366   DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
367   DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
368   DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
369               DST(3, 2) = AVG3(E, F, G);
370               DST(3, 3) = AVG3(F, G, H);
371 }
372 
HU4_C(uint8_t * dst)373 static void HU4_C(uint8_t* dst) {   // Horizontal-Up
374   const int I = dst[-1 + 0 * BPS];
375   const int J = dst[-1 + 1 * BPS];
376   const int K = dst[-1 + 2 * BPS];
377   const int L = dst[-1 + 3 * BPS];
378   DST(0, 0) =             AVG2(I, J);
379   DST(2, 0) = DST(0, 1) = AVG2(J, K);
380   DST(2, 1) = DST(0, 2) = AVG2(K, L);
381   DST(1, 0) =             AVG3(I, J, K);
382   DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
383   DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
384   DST(3, 2) = DST(2, 2) =
385     DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
386 }
387 
HD4_C(uint8_t * dst)388 static void HD4_C(uint8_t* dst) {  // Horizontal-Down
389   const int I = dst[-1 + 0 * BPS];
390   const int J = dst[-1 + 1 * BPS];
391   const int K = dst[-1 + 2 * BPS];
392   const int L = dst[-1 + 3 * BPS];
393   const int X = dst[-1 - BPS];
394   const int A = dst[0 - BPS];
395   const int B = dst[1 - BPS];
396   const int C = dst[2 - BPS];
397 
398   DST(0, 0) = DST(2, 1) = AVG2(I, X);
399   DST(0, 1) = DST(2, 2) = AVG2(J, I);
400   DST(0, 2) = DST(2, 3) = AVG2(K, J);
401   DST(0, 3)             = AVG2(L, K);
402 
403   DST(3, 0)             = AVG3(A, B, C);
404   DST(2, 0)             = AVG3(X, A, B);
405   DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
406   DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
407   DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
408   DST(1, 3)             = AVG3(L, K, J);
409 }
410 
411 #undef DST
412 #undef AVG3
413 #undef AVG2
414 
415 VP8PredFunc VP8PredLuma4[NUM_BMODES];
416 
417 //------------------------------------------------------------------------------
418 // Chroma
419 
420 #if !WEBP_NEON_OMIT_C_CODE
VE8uv_C(uint8_t * dst)421 static void VE8uv_C(uint8_t* dst) {    // vertical
422   int j;
423   for (j = 0; j < 8; ++j) {
424     memcpy(dst + j * BPS, dst - BPS, 8);
425   }
426 }
427 
HE8uv_C(uint8_t * dst)428 static void HE8uv_C(uint8_t* dst) {    // horizontal
429   int j;
430   for (j = 0; j < 8; ++j) {
431     memset(dst, dst[-1], 8);
432     dst += BPS;
433   }
434 }
435 
436 // helper for chroma-DC predictions
Put8x8uv(uint8_t value,uint8_t * dst)437 static WEBP_INLINE void Put8x8uv(uint8_t value, uint8_t* dst) {
438   int j;
439   for (j = 0; j < 8; ++j) {
440     memset(dst + j * BPS, value, 8);
441   }
442 }
443 
DC8uv_C(uint8_t * dst)444 static void DC8uv_C(uint8_t* dst) {     // DC
445   int dc0 = 8;
446   int i;
447   for (i = 0; i < 8; ++i) {
448     dc0 += dst[i - BPS] + dst[-1 + i * BPS];
449   }
450   Put8x8uv(dc0 >> 4, dst);
451 }
452 
DC8uvNoLeft_C(uint8_t * dst)453 static void DC8uvNoLeft_C(uint8_t* dst) {   // DC with no left samples
454   int dc0 = 4;
455   int i;
456   for (i = 0; i < 8; ++i) {
457     dc0 += dst[i - BPS];
458   }
459   Put8x8uv(dc0 >> 3, dst);
460 }
461 
DC8uvNoTop_C(uint8_t * dst)462 static void DC8uvNoTop_C(uint8_t* dst) {  // DC with no top samples
463   int dc0 = 4;
464   int i;
465   for (i = 0; i < 8; ++i) {
466     dc0 += dst[-1 + i * BPS];
467   }
468   Put8x8uv(dc0 >> 3, dst);
469 }
470 
DC8uvNoTopLeft_C(uint8_t * dst)471 static void DC8uvNoTopLeft_C(uint8_t* dst) {    // DC with nothing
472   Put8x8uv(0x80, dst);
473 }
474 #endif  // !WEBP_NEON_OMIT_C_CODE
475 
476 VP8PredFunc VP8PredChroma8[NUM_B_DC_MODES];
477 
478 //------------------------------------------------------------------------------
479 // Edge filtering functions
480 
481 #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
482 // 4 pixels in, 2 pixels out
DoFilter2_C(uint8_t * p,int step)483 static WEBP_INLINE void DoFilter2_C(uint8_t* p, int step) {
484   const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
485   const int a = 3 * (q0 - p0) + VP8ksclip1[p1 - q1];  // in [-893,892]
486   const int a1 = VP8ksclip2[(a + 4) >> 3];            // in [-16,15]
487   const int a2 = VP8ksclip2[(a + 3) >> 3];
488   p[-step] = VP8kclip1[p0 + a2];
489   p[    0] = VP8kclip1[q0 - a1];
490 }
491 
492 // 4 pixels in, 4 pixels out
DoFilter4_C(uint8_t * p,int step)493 static WEBP_INLINE void DoFilter4_C(uint8_t* p, int step) {
494   const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
495   const int a = 3 * (q0 - p0);
496   const int a1 = VP8ksclip2[(a + 4) >> 3];
497   const int a2 = VP8ksclip2[(a + 3) >> 3];
498   const int a3 = (a1 + 1) >> 1;
499   p[-2*step] = VP8kclip1[p1 + a3];
500   p[-  step] = VP8kclip1[p0 + a2];
501   p[      0] = VP8kclip1[q0 - a1];
502   p[   step] = VP8kclip1[q1 - a3];
503 }
504 
505 // 6 pixels in, 6 pixels out
DoFilter6_C(uint8_t * p,int step)506 static WEBP_INLINE void DoFilter6_C(uint8_t* p, int step) {
507   const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
508   const int q0 = p[0], q1 = p[step], q2 = p[2*step];
509   const int a = VP8ksclip1[3 * (q0 - p0) + VP8ksclip1[p1 - q1]];
510   // a is in [-128,127], a1 in [-27,27], a2 in [-18,18] and a3 in [-9,9]
511   const int a1 = (27 * a + 63) >> 7;  // eq. to ((3 * a + 7) * 9) >> 7
512   const int a2 = (18 * a + 63) >> 7;  // eq. to ((2 * a + 7) * 9) >> 7
513   const int a3 = (9  * a + 63) >> 7;  // eq. to ((1 * a + 7) * 9) >> 7
514   p[-3*step] = VP8kclip1[p2 + a3];
515   p[-2*step] = VP8kclip1[p1 + a2];
516   p[-  step] = VP8kclip1[p0 + a1];
517   p[      0] = VP8kclip1[q0 - a1];
518   p[   step] = VP8kclip1[q1 - a2];
519   p[ 2*step] = VP8kclip1[q2 - a3];
520 }
521 
Hev(const uint8_t * p,int step,int thresh)522 static WEBP_INLINE int Hev(const uint8_t* p, int step, int thresh) {
523   const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
524   return (VP8kabs0[p1 - p0] > thresh) || (VP8kabs0[q1 - q0] > thresh);
525 }
526 #endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
527 
528 #if !WEBP_NEON_OMIT_C_CODE
NeedsFilter_C(const uint8_t * p,int step,int t)529 static WEBP_INLINE int NeedsFilter_C(const uint8_t* p, int step, int t) {
530   const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
531   return ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) <= t);
532 }
533 #endif  // !WEBP_NEON_OMIT_C_CODE
534 
535 #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
NeedsFilter2_C(const uint8_t * p,int step,int t,int it)536 static WEBP_INLINE int NeedsFilter2_C(const uint8_t* p,
537                                       int step, int t, int it) {
538   const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step];
539   const int p0 = p[-step], q0 = p[0];
540   const int q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
541   if ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) > t) return 0;
542   return VP8kabs0[p3 - p2] <= it && VP8kabs0[p2 - p1] <= it &&
543          VP8kabs0[p1 - p0] <= it && VP8kabs0[q3 - q2] <= it &&
544          VP8kabs0[q2 - q1] <= it && VP8kabs0[q1 - q0] <= it;
545 }
546 #endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
547 
548 //------------------------------------------------------------------------------
549 // Simple In-loop filtering (Paragraph 15.2)
550 
551 #if !WEBP_NEON_OMIT_C_CODE
SimpleVFilter16_C(uint8_t * p,int stride,int thresh)552 static void SimpleVFilter16_C(uint8_t* p, int stride, int thresh) {
553   int i;
554   const int thresh2 = 2 * thresh + 1;
555   for (i = 0; i < 16; ++i) {
556     if (NeedsFilter_C(p + i, stride, thresh2)) {
557       DoFilter2_C(p + i, stride);
558     }
559   }
560 }
561 
SimpleHFilter16_C(uint8_t * p,int stride,int thresh)562 static void SimpleHFilter16_C(uint8_t* p, int stride, int thresh) {
563   int i;
564   const int thresh2 = 2 * thresh + 1;
565   for (i = 0; i < 16; ++i) {
566     if (NeedsFilter_C(p + i * stride, 1, thresh2)) {
567       DoFilter2_C(p + i * stride, 1);
568     }
569   }
570 }
571 
SimpleVFilter16i_C(uint8_t * p,int stride,int thresh)572 static void SimpleVFilter16i_C(uint8_t* p, int stride, int thresh) {
573   int k;
574   for (k = 3; k > 0; --k) {
575     p += 4 * stride;
576     SimpleVFilter16_C(p, stride, thresh);
577   }
578 }
579 
SimpleHFilter16i_C(uint8_t * p,int stride,int thresh)580 static void SimpleHFilter16i_C(uint8_t* p, int stride, int thresh) {
581   int k;
582   for (k = 3; k > 0; --k) {
583     p += 4;
584     SimpleHFilter16_C(p, stride, thresh);
585   }
586 }
587 #endif  // !WEBP_NEON_OMIT_C_CODE
588 
589 //------------------------------------------------------------------------------
590 // Complex In-loop filtering (Paragraph 15.3)
591 
592 #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
FilterLoop26_C(uint8_t * p,int hstride,int vstride,int size,int thresh,int ithresh,int hev_thresh)593 static WEBP_INLINE void FilterLoop26_C(uint8_t* p,
594                                        int hstride, int vstride, int size,
595                                        int thresh, int ithresh,
596                                        int hev_thresh) {
597   const int thresh2 = 2 * thresh + 1;
598   while (size-- > 0) {
599     if (NeedsFilter2_C(p, hstride, thresh2, ithresh)) {
600       if (Hev(p, hstride, hev_thresh)) {
601         DoFilter2_C(p, hstride);
602       } else {
603         DoFilter6_C(p, hstride);
604       }
605     }
606     p += vstride;
607   }
608 }
609 
FilterLoop24_C(uint8_t * p,int hstride,int vstride,int size,int thresh,int ithresh,int hev_thresh)610 static WEBP_INLINE void FilterLoop24_C(uint8_t* p,
611                                        int hstride, int vstride, int size,
612                                        int thresh, int ithresh,
613                                        int hev_thresh) {
614   const int thresh2 = 2 * thresh + 1;
615   while (size-- > 0) {
616     if (NeedsFilter2_C(p, hstride, thresh2, ithresh)) {
617       if (Hev(p, hstride, hev_thresh)) {
618         DoFilter2_C(p, hstride);
619       } else {
620         DoFilter4_C(p, hstride);
621       }
622     }
623     p += vstride;
624   }
625 }
626 #endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
627 
628 #if !WEBP_NEON_OMIT_C_CODE
629 // on macroblock edges
VFilter16_C(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)630 static void VFilter16_C(uint8_t* p, int stride,
631                         int thresh, int ithresh, int hev_thresh) {
632   FilterLoop26_C(p, stride, 1, 16, thresh, ithresh, hev_thresh);
633 }
634 
HFilter16_C(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)635 static void HFilter16_C(uint8_t* p, int stride,
636                         int thresh, int ithresh, int hev_thresh) {
637   FilterLoop26_C(p, 1, stride, 16, thresh, ithresh, hev_thresh);
638 }
639 
640 // on three inner edges
VFilter16i_C(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)641 static void VFilter16i_C(uint8_t* p, int stride,
642                          int thresh, int ithresh, int hev_thresh) {
643   int k;
644   for (k = 3; k > 0; --k) {
645     p += 4 * stride;
646     FilterLoop24_C(p, stride, 1, 16, thresh, ithresh, hev_thresh);
647   }
648 }
649 #endif  // !WEBP_NEON_OMIT_C_CODE
650 
651 #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
HFilter16i_C(uint8_t * p,int stride,int thresh,int ithresh,int hev_thresh)652 static void HFilter16i_C(uint8_t* p, int stride,
653                          int thresh, int ithresh, int hev_thresh) {
654   int k;
655   for (k = 3; k > 0; --k) {
656     p += 4;
657     FilterLoop24_C(p, 1, stride, 16, thresh, ithresh, hev_thresh);
658   }
659 }
660 #endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
661 
662 #if !WEBP_NEON_OMIT_C_CODE
663 // 8-pixels wide variant, for chroma filtering
VFilter8_C(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)664 static void VFilter8_C(uint8_t* u, uint8_t* v, int stride,
665                        int thresh, int ithresh, int hev_thresh) {
666   FilterLoop26_C(u, stride, 1, 8, thresh, ithresh, hev_thresh);
667   FilterLoop26_C(v, stride, 1, 8, thresh, ithresh, hev_thresh);
668 }
669 #endif  // !WEBP_NEON_OMIT_C_CODE
670 
671 #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
HFilter8_C(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)672 static void HFilter8_C(uint8_t* u, uint8_t* v, int stride,
673                        int thresh, int ithresh, int hev_thresh) {
674   FilterLoop26_C(u, 1, stride, 8, thresh, ithresh, hev_thresh);
675   FilterLoop26_C(v, 1, stride, 8, thresh, ithresh, hev_thresh);
676 }
677 #endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
678 
679 #if !WEBP_NEON_OMIT_C_CODE
VFilter8i_C(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)680 static void VFilter8i_C(uint8_t* u, uint8_t* v, int stride,
681                         int thresh, int ithresh, int hev_thresh) {
682   FilterLoop24_C(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
683   FilterLoop24_C(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
684 }
685 #endif  // !WEBP_NEON_OMIT_C_CODE
686 
687 #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
HFilter8i_C(uint8_t * u,uint8_t * v,int stride,int thresh,int ithresh,int hev_thresh)688 static void HFilter8i_C(uint8_t* u, uint8_t* v, int stride,
689                         int thresh, int ithresh, int hev_thresh) {
690   FilterLoop24_C(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
691   FilterLoop24_C(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
692 }
693 #endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
694 
695 //------------------------------------------------------------------------------
696 
DitherCombine8x8_C(const uint8_t * dither,uint8_t * dst,int dst_stride)697 static void DitherCombine8x8_C(const uint8_t* dither, uint8_t* dst,
698                                int dst_stride) {
699   int i, j;
700   for (j = 0; j < 8; ++j) {
701     for (i = 0; i < 8; ++i) {
702       const int delta0 = dither[i] - VP8_DITHER_AMP_CENTER;
703       const int delta1 =
704           (delta0 + VP8_DITHER_DESCALE_ROUNDER) >> VP8_DITHER_DESCALE;
705       dst[i] = clip_8b((int)dst[i] + delta1);
706     }
707     dst += dst_stride;
708     dither += 8;
709   }
710 }
711 
712 //------------------------------------------------------------------------------
713 
714 VP8DecIdct2 VP8Transform;
715 VP8DecIdct VP8TransformAC3;
716 VP8DecIdct VP8TransformUV;
717 VP8DecIdct VP8TransformDC;
718 VP8DecIdct VP8TransformDCUV;
719 
720 VP8LumaFilterFunc VP8VFilter16;
721 VP8LumaFilterFunc VP8HFilter16;
722 VP8ChromaFilterFunc VP8VFilter8;
723 VP8ChromaFilterFunc VP8HFilter8;
724 VP8LumaFilterFunc VP8VFilter16i;
725 VP8LumaFilterFunc VP8HFilter16i;
726 VP8ChromaFilterFunc VP8VFilter8i;
727 VP8ChromaFilterFunc VP8HFilter8i;
728 VP8SimpleFilterFunc VP8SimpleVFilter16;
729 VP8SimpleFilterFunc VP8SimpleHFilter16;
730 VP8SimpleFilterFunc VP8SimpleVFilter16i;
731 VP8SimpleFilterFunc VP8SimpleHFilter16i;
732 
733 void (*VP8DitherCombine8x8)(const uint8_t* dither, uint8_t* dst,
734                             int dst_stride);
735 
736 extern VP8CPUInfo VP8GetCPUInfo;
737 extern void VP8DspInitSSE2(void);
738 extern void VP8DspInitSSE41(void);
739 extern void VP8DspInitNEON(void);
740 extern void VP8DspInitMIPS32(void);
741 extern void VP8DspInitMIPSdspR2(void);
742 extern void VP8DspInitMSA(void);
743 
WEBP_DSP_INIT_FUNC(VP8DspInit)744 WEBP_DSP_INIT_FUNC(VP8DspInit) {
745   VP8InitClipTables();
746 
747 #if !WEBP_NEON_OMIT_C_CODE
748   VP8TransformWHT = TransformWHT_C;
749   VP8Transform = TransformTwo_C;
750   VP8TransformDC = TransformDC_C;
751   VP8TransformAC3 = TransformAC3_C;
752 #endif
753   VP8TransformUV = TransformUV_C;
754   VP8TransformDCUV = TransformDCUV_C;
755 
756 #if !WEBP_NEON_OMIT_C_CODE
757   VP8VFilter16 = VFilter16_C;
758   VP8VFilter16i = VFilter16i_C;
759   VP8HFilter16 = HFilter16_C;
760   VP8VFilter8 = VFilter8_C;
761   VP8VFilter8i = VFilter8i_C;
762   VP8SimpleVFilter16 = SimpleVFilter16_C;
763   VP8SimpleHFilter16 = SimpleHFilter16_C;
764   VP8SimpleVFilter16i = SimpleVFilter16i_C;
765   VP8SimpleHFilter16i = SimpleHFilter16i_C;
766 #endif
767 
768 #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
769   VP8HFilter16i = HFilter16i_C;
770   VP8HFilter8 = HFilter8_C;
771   VP8HFilter8i = HFilter8i_C;
772 #endif
773 
774 #if !WEBP_NEON_OMIT_C_CODE
775   VP8PredLuma4[0] = DC4_C;
776   VP8PredLuma4[1] = TM4_C;
777   VP8PredLuma4[2] = VE4_C;
778   VP8PredLuma4[4] = RD4_C;
779   VP8PredLuma4[6] = LD4_C;
780 #endif
781 
782   VP8PredLuma4[3] = HE4_C;
783   VP8PredLuma4[5] = VR4_C;
784   VP8PredLuma4[7] = VL4_C;
785   VP8PredLuma4[8] = HD4_C;
786   VP8PredLuma4[9] = HU4_C;
787 
788 #if !WEBP_NEON_OMIT_C_CODE
789   VP8PredLuma16[0] = DC16_C;
790   VP8PredLuma16[1] = TM16_C;
791   VP8PredLuma16[2] = VE16_C;
792   VP8PredLuma16[3] = HE16_C;
793   VP8PredLuma16[4] = DC16NoTop_C;
794   VP8PredLuma16[5] = DC16NoLeft_C;
795   VP8PredLuma16[6] = DC16NoTopLeft_C;
796 
797   VP8PredChroma8[0] = DC8uv_C;
798   VP8PredChroma8[1] = TM8uv_C;
799   VP8PredChroma8[2] = VE8uv_C;
800   VP8PredChroma8[3] = HE8uv_C;
801   VP8PredChroma8[4] = DC8uvNoTop_C;
802   VP8PredChroma8[5] = DC8uvNoLeft_C;
803   VP8PredChroma8[6] = DC8uvNoTopLeft_C;
804 #endif
805 
806   VP8DitherCombine8x8 = DitherCombine8x8_C;
807 
808   // If defined, use CPUInfo() to overwrite some pointers with faster versions.
809   if (VP8GetCPUInfo != NULL) {
810 #if defined(WEBP_HAVE_SSE2)
811     if (VP8GetCPUInfo(kSSE2)) {
812       VP8DspInitSSE2();
813 #if defined(WEBP_HAVE_SSE41)
814       if (VP8GetCPUInfo(kSSE4_1)) {
815         VP8DspInitSSE41();
816       }
817 #endif
818     }
819 #endif
820 #if defined(WEBP_USE_MIPS32)
821     if (VP8GetCPUInfo(kMIPS32)) {
822       VP8DspInitMIPS32();
823     }
824 #endif
825 #if defined(WEBP_USE_MIPS_DSP_R2)
826     if (VP8GetCPUInfo(kMIPSdspR2)) {
827       VP8DspInitMIPSdspR2();
828     }
829 #endif
830 #if defined(WEBP_USE_MSA)
831     if (VP8GetCPUInfo(kMSA)) {
832       VP8DspInitMSA();
833     }
834 #endif
835   }
836 
837 #if defined(WEBP_HAVE_NEON)
838   if (WEBP_NEON_OMIT_C_CODE ||
839       (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
840     VP8DspInitNEON();
841   }
842 #endif
843 
844   assert(VP8TransformWHT != NULL);
845   assert(VP8Transform != NULL);
846   assert(VP8TransformDC != NULL);
847   assert(VP8TransformAC3 != NULL);
848   assert(VP8TransformUV != NULL);
849   assert(VP8TransformDCUV != NULL);
850   assert(VP8VFilter16 != NULL);
851   assert(VP8HFilter16 != NULL);
852   assert(VP8VFilter8 != NULL);
853   assert(VP8HFilter8 != NULL);
854   assert(VP8VFilter16i != NULL);
855   assert(VP8HFilter16i != NULL);
856   assert(VP8VFilter8i != NULL);
857   assert(VP8HFilter8i != NULL);
858   assert(VP8SimpleVFilter16 != NULL);
859   assert(VP8SimpleHFilter16 != NULL);
860   assert(VP8SimpleVFilter16i != NULL);
861   assert(VP8SimpleHFilter16i != NULL);
862   assert(VP8PredLuma4[0] != NULL);
863   assert(VP8PredLuma4[1] != NULL);
864   assert(VP8PredLuma4[2] != NULL);
865   assert(VP8PredLuma4[3] != NULL);
866   assert(VP8PredLuma4[4] != NULL);
867   assert(VP8PredLuma4[5] != NULL);
868   assert(VP8PredLuma4[6] != NULL);
869   assert(VP8PredLuma4[7] != NULL);
870   assert(VP8PredLuma4[8] != NULL);
871   assert(VP8PredLuma4[9] != NULL);
872   assert(VP8PredLuma16[0] != NULL);
873   assert(VP8PredLuma16[1] != NULL);
874   assert(VP8PredLuma16[2] != NULL);
875   assert(VP8PredLuma16[3] != NULL);
876   assert(VP8PredLuma16[4] != NULL);
877   assert(VP8PredLuma16[5] != NULL);
878   assert(VP8PredLuma16[6] != NULL);
879   assert(VP8PredChroma8[0] != NULL);
880   assert(VP8PredChroma8[1] != NULL);
881   assert(VP8PredChroma8[2] != NULL);
882   assert(VP8PredChroma8[3] != NULL);
883   assert(VP8PredChroma8[4] != NULL);
884   assert(VP8PredChroma8[5] != NULL);
885   assert(VP8PredChroma8[6] != NULL);
886   assert(VP8DitherCombine8x8 != NULL);
887 }
888