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