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