Coverage Report

Created: 2024-07-27 06:27

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