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