Coverage Report

Created: 2025-06-13 06:48

/src/libwebp/src/dsp/enc.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2011 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 encoding functions.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <stdlib.h>  // for abs()
16
#include <string.h>
17
18
#include "src/dsp/cpu.h"
19
#include "src/dsp/dsp.h"
20
#include "src/enc/vp8i_enc.h"
21
#include "src/utils/utils.h"
22
#include "src/webp/types.h"
23
24
0
static WEBP_INLINE uint8_t clip_8b(int v) {
25
0
  return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
26
0
}
27
28
#if !WEBP_NEON_OMIT_C_CODE
29
0
static WEBP_INLINE int clip_max(int v, int max) {
30
0
  return (v > max) ? max : v;
31
0
}
32
#endif  // !WEBP_NEON_OMIT_C_CODE
33
34
//------------------------------------------------------------------------------
35
// Compute susceptibility based on DCT-coeff histograms:
36
// the higher, the "easier" the macroblock is to compress.
37
38
const int VP8DspScan[16 + 4 + 4] = {
39
  // Luma
40
  0 +  0 * BPS,  4 +  0 * BPS, 8 +  0 * BPS, 12 +  0 * BPS,
41
  0 +  4 * BPS,  4 +  4 * BPS, 8 +  4 * BPS, 12 +  4 * BPS,
42
  0 +  8 * BPS,  4 +  8 * BPS, 8 +  8 * BPS, 12 +  8 * BPS,
43
  0 + 12 * BPS,  4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS,
44
45
  0 + 0 * BPS,   4 + 0 * BPS, 0 + 4 * BPS,  4 + 4 * BPS,    // U
46
  8 + 0 * BPS,  12 + 0 * BPS, 8 + 4 * BPS, 12 + 4 * BPS     // V
47
};
48
49
// general-purpose util function
50
void VP8SetHistogramData(const int distribution[MAX_COEFF_THRESH + 1],
51
0
                         VP8Histogram* const histo) {
52
0
  int max_value = 0, last_non_zero = 1;
53
0
  int k;
54
0
  for (k = 0; k <= MAX_COEFF_THRESH; ++k) {
55
0
    const int value = distribution[k];
56
0
    if (value > 0) {
57
0
      if (value > max_value) max_value = value;
58
0
      last_non_zero = k;
59
0
    }
60
0
  }
61
0
  histo->max_value = max_value;
62
0
  histo->last_non_zero = last_non_zero;
63
0
}
64
65
#if !WEBP_NEON_OMIT_C_CODE
66
static void CollectHistogram_C(const uint8_t* WEBP_RESTRICT ref,
67
                               const uint8_t* WEBP_RESTRICT pred,
68
                               int start_block, int end_block,
69
0
                               VP8Histogram* WEBP_RESTRICT const histo) {
70
0
  int j;
71
0
  int distribution[MAX_COEFF_THRESH + 1] = { 0 };
72
0
  for (j = start_block; j < end_block; ++j) {
73
0
    int k;
74
0
    int16_t out[16];
75
76
0
    VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
77
78
    // Convert coefficients to bin.
79
0
    for (k = 0; k < 16; ++k) {
80
0
      const int v = abs(out[k]) >> 3;
81
0
      const int clipped_value = clip_max(v, MAX_COEFF_THRESH);
82
0
      ++distribution[clipped_value];
83
0
    }
84
0
  }
85
0
  VP8SetHistogramData(distribution, histo);
86
0
}
87
#endif  // !WEBP_NEON_OMIT_C_CODE
88
89
//------------------------------------------------------------------------------
90
// run-time tables (~4k)
91
92
static uint8_t clip1[255 + 510 + 1];    // clips [-255,510] to [0,255]
93
94
// We declare this variable 'volatile' to prevent instruction reordering
95
// and make sure it's set to true _last_ (so as to be thread-safe)
96
static volatile int tables_ok = 0;
97
98
0
static WEBP_TSAN_IGNORE_FUNCTION void InitTables(void) {
99
0
  if (!tables_ok) {
100
0
    int i;
101
0
    for (i = -255; i <= 255 + 255; ++i) {
102
0
      clip1[255 + i] = clip_8b(i);
103
0
    }
104
0
    tables_ok = 1;
105
0
  }
106
0
}
107
108
109
//------------------------------------------------------------------------------
110
// Transforms (Paragraph 14.4)
111
112
#if !WEBP_NEON_OMIT_C_CODE
113
114
#define STORE(x, y, v) \
115
0
  dst[(x) + (y) * BPS] = clip_8b(ref[(x) + (y) * BPS] + ((v) >> 3))
116
117
static WEBP_INLINE void ITransformOne(const uint8_t* WEBP_RESTRICT ref,
118
                                      const int16_t* WEBP_RESTRICT in,
119
0
                                      uint8_t* WEBP_RESTRICT dst) {
120
0
  int C[4 * 4], *tmp;
121
0
  int i;
122
0
  tmp = C;
123
0
  for (i = 0; i < 4; ++i) {    // vertical pass
124
0
    const int a = in[0] + in[8];
125
0
    const int b = in[0] - in[8];
126
0
    const int c =
127
0
        WEBP_TRANSFORM_AC3_MUL2(in[4]) - WEBP_TRANSFORM_AC3_MUL1(in[12]);
128
0
    const int d =
129
0
        WEBP_TRANSFORM_AC3_MUL1(in[4]) + WEBP_TRANSFORM_AC3_MUL2(in[12]);
130
0
    tmp[0] = a + d;
131
0
    tmp[1] = b + c;
132
0
    tmp[2] = b - c;
133
0
    tmp[3] = a - d;
134
0
    tmp += 4;
135
0
    in++;
136
0
  }
137
138
0
  tmp = C;
139
0
  for (i = 0; i < 4; ++i) {    // horizontal pass
140
0
    const int dc = tmp[0] + 4;
141
0
    const int a = dc + tmp[8];
142
0
    const int b = dc - tmp[8];
143
0
    const int c =
144
0
        WEBP_TRANSFORM_AC3_MUL2(tmp[4]) - WEBP_TRANSFORM_AC3_MUL1(tmp[12]);
145
0
    const int d =
146
0
        WEBP_TRANSFORM_AC3_MUL1(tmp[4]) + WEBP_TRANSFORM_AC3_MUL2(tmp[12]);
147
0
    STORE(0, i, a + d);
148
0
    STORE(1, i, b + c);
149
0
    STORE(2, i, b - c);
150
0
    STORE(3, i, a - d);
151
0
    tmp++;
152
0
  }
153
0
}
154
155
static void ITransform_C(const uint8_t* WEBP_RESTRICT ref,
156
                         const int16_t* WEBP_RESTRICT in,
157
                         uint8_t* WEBP_RESTRICT dst,
158
0
                         int do_two) {
159
0
  ITransformOne(ref, in, dst);
160
0
  if (do_two) {
161
0
    ITransformOne(ref + 4, in + 16, dst + 4);
162
0
  }
163
0
}
164
165
static void FTransform_C(const uint8_t* WEBP_RESTRICT src,
166
                         const uint8_t* WEBP_RESTRICT ref,
167
0
                         int16_t* WEBP_RESTRICT out) {
168
0
  int i;
169
0
  int tmp[16];
170
0
  for (i = 0; i < 4; ++i, src += BPS, ref += BPS) {
171
0
    const int d0 = src[0] - ref[0];   // 9bit dynamic range ([-255,255])
172
0
    const int d1 = src[1] - ref[1];
173
0
    const int d2 = src[2] - ref[2];
174
0
    const int d3 = src[3] - ref[3];
175
0
    const int a0 = (d0 + d3);         // 10b                      [-510,510]
176
0
    const int a1 = (d1 + d2);
177
0
    const int a2 = (d1 - d2);
178
0
    const int a3 = (d0 - d3);
179
0
    tmp[0 + i * 4] = (a0 + a1) * 8;   // 14b                      [-8160,8160]
180
0
    tmp[1 + i * 4] = (a2 * 2217 + a3 * 5352 + 1812) >> 9;      // [-7536,7542]
181
0
    tmp[2 + i * 4] = (a0 - a1) * 8;
182
0
    tmp[3 + i * 4] = (a3 * 2217 - a2 * 5352 +  937) >> 9;
183
0
  }
184
0
  for (i = 0; i < 4; ++i) {
185
0
    const int a0 = (tmp[0 + i] + tmp[12 + i]);  // 15b
186
0
    const int a1 = (tmp[4 + i] + tmp[ 8 + i]);
187
0
    const int a2 = (tmp[4 + i] - tmp[ 8 + i]);
188
0
    const int a3 = (tmp[0 + i] - tmp[12 + i]);
189
0
    out[0 + i] = (a0 + a1 + 7) >> 4;            // 12b
190
0
    out[4 + i] = ((a2 * 2217 + a3 * 5352 + 12000) >> 16) + (a3 != 0);
191
0
    out[8 + i] = (a0 - a1 + 7) >> 4;
192
0
    out[12+ i] = ((a3 * 2217 - a2 * 5352 + 51000) >> 16);
193
0
  }
194
0
}
195
#endif  // !WEBP_NEON_OMIT_C_CODE
196
197
static void FTransform2_C(const uint8_t* WEBP_RESTRICT src,
198
                          const uint8_t* WEBP_RESTRICT ref,
199
0
                          int16_t* WEBP_RESTRICT out) {
200
0
  VP8FTransform(src, ref, out);
201
0
  VP8FTransform(src + 4, ref + 4, out + 16);
202
0
}
203
204
#if !WEBP_NEON_OMIT_C_CODE
205
static void FTransformWHT_C(const int16_t* WEBP_RESTRICT in,
206
0
                            int16_t* WEBP_RESTRICT out) {
207
  // input is 12b signed
208
0
  int32_t tmp[16];
209
0
  int i;
210
0
  for (i = 0; i < 4; ++i, in += 64) {
211
0
    const int a0 = (in[0 * 16] + in[2 * 16]);  // 13b
212
0
    const int a1 = (in[1 * 16] + in[3 * 16]);
213
0
    const int a2 = (in[1 * 16] - in[3 * 16]);
214
0
    const int a3 = (in[0 * 16] - in[2 * 16]);
215
0
    tmp[0 + i * 4] = a0 + a1;   // 14b
216
0
    tmp[1 + i * 4] = a3 + a2;
217
0
    tmp[2 + i * 4] = a3 - a2;
218
0
    tmp[3 + i * 4] = a0 - a1;
219
0
  }
220
0
  for (i = 0; i < 4; ++i) {
221
0
    const int a0 = (tmp[0 + i] + tmp[8 + i]);  // 15b
222
0
    const int a1 = (tmp[4 + i] + tmp[12+ i]);
223
0
    const int a2 = (tmp[4 + i] - tmp[12+ i]);
224
0
    const int a3 = (tmp[0 + i] - tmp[8 + i]);
225
0
    const int b0 = a0 + a1;    // 16b
226
0
    const int b1 = a3 + a2;
227
0
    const int b2 = a3 - a2;
228
0
    const int b3 = a0 - a1;
229
0
    out[ 0 + i] = b0 >> 1;     // 15b
230
0
    out[ 4 + i] = b1 >> 1;
231
0
    out[ 8 + i] = b2 >> 1;
232
0
    out[12 + i] = b3 >> 1;
233
0
  }
234
0
}
235
#endif  // !WEBP_NEON_OMIT_C_CODE
236
237
#undef STORE
238
239
//------------------------------------------------------------------------------
240
// Intra predictions
241
242
0
static WEBP_INLINE void Fill(uint8_t* dst, int value, int size) {
243
0
  int j;
244
0
  for (j = 0; j < size; ++j) {
245
0
    memset(dst + j * BPS, value, size);
246
0
  }
247
0
}
248
249
static WEBP_INLINE void VerticalPred(uint8_t* WEBP_RESTRICT dst,
250
                                     const uint8_t* WEBP_RESTRICT top,
251
0
                                     int size) {
252
0
  int j;
253
0
  if (top != NULL) {
254
0
    for (j = 0; j < size; ++j) memcpy(dst + j * BPS, top, size);
255
0
  } else {
256
0
    Fill(dst, 127, size);
257
0
  }
258
0
}
259
260
static WEBP_INLINE void HorizontalPred(uint8_t* WEBP_RESTRICT dst,
261
                                       const uint8_t* WEBP_RESTRICT left,
262
0
                                       int size) {
263
0
  if (left != NULL) {
264
0
    int j;
265
0
    for (j = 0; j < size; ++j) {
266
0
      memset(dst + j * BPS, left[j], size);
267
0
    }
268
0
  } else {
269
0
    Fill(dst, 129, size);
270
0
  }
271
0
}
272
273
static WEBP_INLINE void TrueMotion(uint8_t* WEBP_RESTRICT dst,
274
                                   const uint8_t* WEBP_RESTRICT left,
275
0
                                   const uint8_t* WEBP_RESTRICT top, int size) {
276
0
  int y;
277
0
  if (left != NULL) {
278
0
    if (top != NULL) {
279
0
      const uint8_t* const clip = clip1 + 255 - left[-1];
280
0
      for (y = 0; y < size; ++y) {
281
0
        const uint8_t* const clip_table = clip + left[y];
282
0
        int x;
283
0
        for (x = 0; x < size; ++x) {
284
0
          dst[x] = clip_table[top[x]];
285
0
        }
286
0
        dst += BPS;
287
0
      }
288
0
    } else {
289
0
      HorizontalPred(dst, left, size);
290
0
    }
291
0
  } else {
292
    // true motion without left samples (hence: with default 129 value)
293
    // is equivalent to VE prediction where you just copy the top samples.
294
    // Note that if top samples are not available, the default value is
295
    // then 129, and not 127 as in the VerticalPred case.
296
0
    if (top != NULL) {
297
0
      VerticalPred(dst, top, size);
298
0
    } else {
299
0
      Fill(dst, 129, size);
300
0
    }
301
0
  }
302
0
}
303
304
static WEBP_INLINE void DCMode(uint8_t* WEBP_RESTRICT dst,
305
                               const uint8_t* WEBP_RESTRICT left,
306
                               const uint8_t* WEBP_RESTRICT top,
307
0
                               int size, int round, int shift) {
308
0
  int DC = 0;
309
0
  int j;
310
0
  if (top != NULL) {
311
0
    for (j = 0; j < size; ++j) DC += top[j];
312
0
    if (left != NULL) {   // top and left present
313
0
      for (j = 0; j < size; ++j) DC += left[j];
314
0
    } else {      // top, but no left
315
0
      DC += DC;
316
0
    }
317
0
    DC = (DC + round) >> shift;
318
0
  } else if (left != NULL) {   // left but no top
319
0
    for (j = 0; j < size; ++j) DC += left[j];
320
0
    DC += DC;
321
0
    DC = (DC + round) >> shift;
322
0
  } else {   // no top, no left, nothing.
323
0
    DC = 0x80;
324
0
  }
325
0
  Fill(dst, DC, size);
326
0
}
327
328
//------------------------------------------------------------------------------
329
// Chroma 8x8 prediction (paragraph 12.2)
330
331
static void IntraChromaPreds_C(uint8_t* WEBP_RESTRICT dst,
332
                               const uint8_t* WEBP_RESTRICT left,
333
0
                               const uint8_t* WEBP_RESTRICT top) {
334
  // U block
335
0
  DCMode(C8DC8 + dst, left, top, 8, 8, 4);
336
0
  VerticalPred(C8VE8 + dst, top, 8);
337
0
  HorizontalPred(C8HE8 + dst, left, 8);
338
0
  TrueMotion(C8TM8 + dst, left, top, 8);
339
  // V block
340
0
  dst += 8;
341
0
  if (top != NULL) top += 8;
342
0
  if (left != NULL) left += 16;
343
0
  DCMode(C8DC8 + dst, left, top, 8, 8, 4);
344
0
  VerticalPred(C8VE8 + dst, top, 8);
345
0
  HorizontalPred(C8HE8 + dst, left, 8);
346
0
  TrueMotion(C8TM8 + dst, left, top, 8);
347
0
}
348
349
//------------------------------------------------------------------------------
350
// luma 16x16 prediction (paragraph 12.3)
351
352
#if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64
353
static void Intra16Preds_C(uint8_t* WEBP_RESTRICT dst,
354
                           const uint8_t* WEBP_RESTRICT left,
355
0
                           const uint8_t* WEBP_RESTRICT top) {
356
0
  DCMode(I16DC16 + dst, left, top, 16, 16, 5);
357
0
  VerticalPred(I16VE16 + dst, top, 16);
358
0
  HorizontalPred(I16HE16 + dst, left, 16);
359
0
  TrueMotion(I16TM16 + dst, left, top, 16);
360
0
}
361
#endif  // !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64
362
363
//------------------------------------------------------------------------------
364
// luma 4x4 prediction
365
366
#if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 || BPS != 32
367
368
0
#define DST(x, y) dst[(x) + (y) * BPS]
369
0
#define AVG3(a, b, c) ((uint8_t)(((a) + 2 * (b) + (c) + 2) >> 2))
370
0
#define AVG2(a, b) (((a) + (b) + 1) >> 1)
371
372
// vertical
373
0
static void VE4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
374
0
  const uint8_t vals[4] = {
375
0
    AVG3(top[-1], top[0], top[1]),
376
0
    AVG3(top[ 0], top[1], top[2]),
377
0
    AVG3(top[ 1], top[2], top[3]),
378
0
    AVG3(top[ 2], top[3], top[4])
379
0
  };
380
0
  int i;
381
0
  for (i = 0; i < 4; ++i) {
382
0
    memcpy(dst + i * BPS, vals, 4);
383
0
  }
384
0
}
385
386
// horizontal
387
0
static void HE4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
388
0
  const int X = top[-1];
389
0
  const int I = top[-2];
390
0
  const int J = top[-3];
391
0
  const int K = top[-4];
392
0
  const int L = top[-5];
393
0
  WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J));
394
0
  WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K));
395
0
  WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L));
396
0
  WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L));
397
0
}
398
399
0
static void DC4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
400
0
  uint32_t dc = 4;
401
0
  int i;
402
0
  for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];
403
0
  Fill(dst, dc >> 3, 4);
404
0
}
405
406
0
static void RD4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
407
0
  const int X = top[-1];
408
0
  const int I = top[-2];
409
0
  const int J = top[-3];
410
0
  const int K = top[-4];
411
0
  const int L = top[-5];
412
0
  const int A = top[0];
413
0
  const int B = top[1];
414
0
  const int C = top[2];
415
0
  const int D = top[3];
416
0
  DST(0, 3)                                     = AVG3(J, K, L);
417
0
  DST(0, 2) = DST(1, 3)                         = AVG3(I, J, K);
418
0
  DST(0, 1) = DST(1, 2) = DST(2, 3)             = AVG3(X, I, J);
419
0
  DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
420
0
  DST(1, 0) = DST(2, 1) = DST(3, 2)             = AVG3(B, A, X);
421
0
  DST(2, 0) = DST(3, 1)                         = AVG3(C, B, A);
422
0
  DST(3, 0)                                     = AVG3(D, C, B);
423
0
}
424
425
0
static void LD4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
426
0
  const int A = top[0];
427
0
  const int B = top[1];
428
0
  const int C = top[2];
429
0
  const int D = top[3];
430
0
  const int E = top[4];
431
0
  const int F = top[5];
432
0
  const int G = top[6];
433
0
  const int H = top[7];
434
0
  DST(0, 0)                                     = AVG3(A, B, C);
435
0
  DST(1, 0) = DST(0, 1)                         = AVG3(B, C, D);
436
0
  DST(2, 0) = DST(1, 1) = DST(0, 2)             = AVG3(C, D, E);
437
0
  DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
438
0
  DST(3, 1) = DST(2, 2) = DST(1, 3)             = AVG3(E, F, G);
439
0
  DST(3, 2) = DST(2, 3)                         = AVG3(F, G, H);
440
0
  DST(3, 3)                                     = AVG3(G, H, H);
441
0
}
442
443
0
static void VR4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
444
0
  const int X = top[-1];
445
0
  const int I = top[-2];
446
0
  const int J = top[-3];
447
0
  const int K = top[-4];
448
0
  const int A = top[0];
449
0
  const int B = top[1];
450
0
  const int C = top[2];
451
0
  const int D = top[3];
452
0
  DST(0, 0) = DST(1, 2) = AVG2(X, A);
453
0
  DST(1, 0) = DST(2, 2) = AVG2(A, B);
454
0
  DST(2, 0) = DST(3, 2) = AVG2(B, C);
455
0
  DST(3, 0)             = AVG2(C, D);
456
457
0
  DST(0, 3) =             AVG3(K, J, I);
458
0
  DST(0, 2) =             AVG3(J, I, X);
459
0
  DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
460
0
  DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
461
0
  DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
462
0
  DST(3, 1) =             AVG3(B, C, D);
463
0
}
464
465
0
static void VL4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
466
0
  const int A = top[0];
467
0
  const int B = top[1];
468
0
  const int C = top[2];
469
0
  const int D = top[3];
470
0
  const int E = top[4];
471
0
  const int F = top[5];
472
0
  const int G = top[6];
473
0
  const int H = top[7];
474
0
  DST(0, 0) =             AVG2(A, B);
475
0
  DST(1, 0) = DST(0, 2) = AVG2(B, C);
476
0
  DST(2, 0) = DST(1, 2) = AVG2(C, D);
477
0
  DST(3, 0) = DST(2, 2) = AVG2(D, E);
478
479
0
  DST(0, 1) =             AVG3(A, B, C);
480
0
  DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
481
0
  DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
482
0
  DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
483
0
              DST(3, 2) = AVG3(E, F, G);
484
0
              DST(3, 3) = AVG3(F, G, H);
485
0
}
486
487
0
static void HU4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
488
0
  const int I = top[-2];
489
0
  const int J = top[-3];
490
0
  const int K = top[-4];
491
0
  const int L = top[-5];
492
0
  DST(0, 0) =             AVG2(I, J);
493
0
  DST(2, 0) = DST(0, 1) = AVG2(J, K);
494
0
  DST(2, 1) = DST(0, 2) = AVG2(K, L);
495
0
  DST(1, 0) =             AVG3(I, J, K);
496
0
  DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
497
0
  DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
498
0
  DST(3, 2) = DST(2, 2) =
499
0
  DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
500
0
}
501
502
0
static void HD4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
503
0
  const int X = top[-1];
504
0
  const int I = top[-2];
505
0
  const int J = top[-3];
506
0
  const int K = top[-4];
507
0
  const int L = top[-5];
508
0
  const int A = top[0];
509
0
  const int B = top[1];
510
0
  const int C = top[2];
511
512
0
  DST(0, 0) = DST(2, 1) = AVG2(I, X);
513
0
  DST(0, 1) = DST(2, 2) = AVG2(J, I);
514
0
  DST(0, 2) = DST(2, 3) = AVG2(K, J);
515
0
  DST(0, 3)             = AVG2(L, K);
516
517
0
  DST(3, 0)             = AVG3(A, B, C);
518
0
  DST(2, 0)             = AVG3(X, A, B);
519
0
  DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
520
0
  DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
521
0
  DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
522
0
  DST(1, 3)             = AVG3(L, K, J);
523
0
}
524
525
0
static void TM4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) {
526
0
  int x, y;
527
0
  const uint8_t* const clip = clip1 + 255 - top[-1];
528
0
  for (y = 0; y < 4; ++y) {
529
0
    const uint8_t* const clip_table = clip + top[-2 - y];
530
0
    for (x = 0; x < 4; ++x) {
531
0
      dst[x] = clip_table[top[x]];
532
0
    }
533
0
    dst += BPS;
534
0
  }
535
0
}
536
537
#undef DST
538
#undef AVG3
539
#undef AVG2
540
541
// Left samples are top[-5 .. -2], top_left is top[-1], top are
542
// located at top[0..3], and top right is top[4..7]
543
static void Intra4Preds_C(uint8_t* WEBP_RESTRICT dst,
544
0
                          const uint8_t* WEBP_RESTRICT top) {
545
0
  DC4(I4DC4 + dst, top);
546
0
  TM4(I4TM4 + dst, top);
547
0
  VE4(I4VE4 + dst, top);
548
0
  HE4(I4HE4 + dst, top);
549
0
  RD4(I4RD4 + dst, top);
550
0
  VR4(I4VR4 + dst, top);
551
0
  LD4(I4LD4 + dst, top);
552
0
  VL4(I4VL4 + dst, top);
553
0
  HD4(I4HD4 + dst, top);
554
0
  HU4(I4HU4 + dst, top);
555
0
}
556
557
#endif  // !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 || BPS != 32
558
559
//------------------------------------------------------------------------------
560
// Metric
561
562
#if !WEBP_NEON_OMIT_C_CODE
563
static WEBP_INLINE int GetSSE(const uint8_t* WEBP_RESTRICT a,
564
                              const uint8_t* WEBP_RESTRICT b,
565
0
                              int w, int h) {
566
0
  int count = 0;
567
0
  int y, x;
568
0
  for (y = 0; y < h; ++y) {
569
0
    for (x = 0; x < w; ++x) {
570
0
      const int diff = (int)a[x] - b[x];
571
0
      count += diff * diff;
572
0
    }
573
0
    a += BPS;
574
0
    b += BPS;
575
0
  }
576
0
  return count;
577
0
}
578
579
static int SSE16x16_C(const uint8_t* WEBP_RESTRICT a,
580
0
                      const uint8_t* WEBP_RESTRICT b) {
581
0
  return GetSSE(a, b, 16, 16);
582
0
}
583
static int SSE16x8_C(const uint8_t* WEBP_RESTRICT a,
584
0
                     const uint8_t* WEBP_RESTRICT b) {
585
0
  return GetSSE(a, b, 16, 8);
586
0
}
587
static int SSE8x8_C(const uint8_t* WEBP_RESTRICT a,
588
0
                    const uint8_t* WEBP_RESTRICT b) {
589
0
  return GetSSE(a, b, 8, 8);
590
0
}
591
static int SSE4x4_C(const uint8_t* WEBP_RESTRICT a,
592
0
                    const uint8_t* WEBP_RESTRICT b) {
593
0
  return GetSSE(a, b, 4, 4);
594
0
}
595
#endif  // !WEBP_NEON_OMIT_C_CODE
596
597
0
static void Mean16x4_C(const uint8_t* WEBP_RESTRICT ref, uint32_t dc[4]) {
598
0
  int k, x, y;
599
0
  for (k = 0; k < 4; ++k) {
600
0
    uint32_t avg = 0;
601
0
    for (y = 0; y < 4; ++y) {
602
0
      for (x = 0; x < 4; ++x) {
603
0
        avg += ref[x + y * BPS];
604
0
      }
605
0
    }
606
0
    dc[k] = avg;
607
0
    ref += 4;   // go to next 4x4 block.
608
0
  }
609
0
}
610
611
//------------------------------------------------------------------------------
612
// Texture distortion
613
//
614
// We try to match the spectral content (weighted) between source and
615
// reconstructed samples.
616
617
#if !WEBP_NEON_OMIT_C_CODE
618
// Hadamard transform
619
// Returns the weighted sum of the absolute value of transformed coefficients.
620
// w[] contains a row-major 4 by 4 symmetric matrix.
621
static int TTransform(const uint8_t* WEBP_RESTRICT in,
622
0
                      const uint16_t* WEBP_RESTRICT w) {
623
0
  int sum = 0;
624
0
  int tmp[16];
625
0
  int i;
626
  // horizontal pass
627
0
  for (i = 0; i < 4; ++i, in += BPS) {
628
0
    const int a0 = in[0] + in[2];
629
0
    const int a1 = in[1] + in[3];
630
0
    const int a2 = in[1] - in[3];
631
0
    const int a3 = in[0] - in[2];
632
0
    tmp[0 + i * 4] = a0 + a1;
633
0
    tmp[1 + i * 4] = a3 + a2;
634
0
    tmp[2 + i * 4] = a3 - a2;
635
0
    tmp[3 + i * 4] = a0 - a1;
636
0
  }
637
  // vertical pass
638
0
  for (i = 0; i < 4; ++i, ++w) {
639
0
    const int a0 = tmp[0 + i] + tmp[8 + i];
640
0
    const int a1 = tmp[4 + i] + tmp[12+ i];
641
0
    const int a2 = tmp[4 + i] - tmp[12+ i];
642
0
    const int a3 = tmp[0 + i] - tmp[8 + i];
643
0
    const int b0 = a0 + a1;
644
0
    const int b1 = a3 + a2;
645
0
    const int b2 = a3 - a2;
646
0
    const int b3 = a0 - a1;
647
648
0
    sum += w[ 0] * abs(b0);
649
0
    sum += w[ 4] * abs(b1);
650
0
    sum += w[ 8] * abs(b2);
651
0
    sum += w[12] * abs(b3);
652
0
  }
653
0
  return sum;
654
0
}
655
656
static int Disto4x4_C(const uint8_t* WEBP_RESTRICT const a,
657
                      const uint8_t* WEBP_RESTRICT const b,
658
0
                      const uint16_t* WEBP_RESTRICT const w) {
659
0
  const int sum1 = TTransform(a, w);
660
0
  const int sum2 = TTransform(b, w);
661
0
  return abs(sum2 - sum1) >> 5;
662
0
}
663
664
static int Disto16x16_C(const uint8_t* WEBP_RESTRICT const a,
665
                        const uint8_t* WEBP_RESTRICT const b,
666
0
                        const uint16_t* WEBP_RESTRICT const w) {
667
0
  int D = 0;
668
0
  int x, y;
669
0
  for (y = 0; y < 16 * BPS; y += 4 * BPS) {
670
0
    for (x = 0; x < 16; x += 4) {
671
0
      D += Disto4x4_C(a + x + y, b + x + y, w);
672
0
    }
673
0
  }
674
0
  return D;
675
0
}
676
#endif  // !WEBP_NEON_OMIT_C_CODE
677
678
//------------------------------------------------------------------------------
679
// Quantization
680
//
681
682
#if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
683
static const uint8_t kZigzag[16] = {
684
  0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
685
};
686
687
// Simple quantization
688
static int QuantizeBlock_C(int16_t in[16], int16_t out[16],
689
0
                           const VP8Matrix* WEBP_RESTRICT const mtx) {
690
0
  int last = -1;
691
0
  int n;
692
0
  for (n = 0; n < 16; ++n) {
693
0
    const int j = kZigzag[n];
694
0
    const int sign = (in[j] < 0);
695
0
    const uint32_t coeff = (sign ? -in[j] : in[j]) + mtx->sharpen[j];
696
0
    if (coeff > mtx->zthresh[j]) {
697
0
      const uint32_t Q = mtx->q[j];
698
0
      const uint32_t iQ = mtx->iq[j];
699
0
      const uint32_t B = mtx->bias[j];
700
0
      int level = QUANTDIV(coeff, iQ, B);
701
0
      if (level > MAX_LEVEL) level = MAX_LEVEL;
702
0
      if (sign) level = -level;
703
0
      in[j] = level * (int)Q;
704
0
      out[n] = level;
705
0
      if (level) last = n;
706
0
    } else {
707
0
      out[n] = 0;
708
0
      in[j] = 0;
709
0
    }
710
0
  }
711
0
  return (last >= 0);
712
0
}
713
714
static int Quantize2Blocks_C(int16_t in[32], int16_t out[32],
715
0
                             const VP8Matrix* WEBP_RESTRICT const mtx) {
716
0
  int nz;
717
0
  nz  = VP8EncQuantizeBlock(in + 0 * 16, out + 0 * 16, mtx) << 0;
718
0
  nz |= VP8EncQuantizeBlock(in + 1 * 16, out + 1 * 16, mtx) << 1;
719
0
  return nz;
720
0
}
721
#endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
722
723
//------------------------------------------------------------------------------
724
// Block copy
725
726
static WEBP_INLINE void Copy(const uint8_t* WEBP_RESTRICT src,
727
0
                             uint8_t* WEBP_RESTRICT dst, int w, int h) {
728
0
  int y;
729
0
  for (y = 0; y < h; ++y) {
730
0
    memcpy(dst, src, w);
731
0
    src += BPS;
732
0
    dst += BPS;
733
0
  }
734
0
}
735
736
static void Copy4x4_C(const uint8_t* WEBP_RESTRICT src,
737
0
                      uint8_t* WEBP_RESTRICT dst) {
738
0
  Copy(src, dst, 4, 4);
739
0
}
740
741
static void Copy16x8_C(const uint8_t* WEBP_RESTRICT src,
742
0
                       uint8_t* WEBP_RESTRICT dst) {
743
0
  Copy(src, dst, 16, 8);
744
0
}
745
746
//------------------------------------------------------------------------------
747
// Initialization
748
749
// Speed-critical function pointers. We have to initialize them to the default
750
// implementations within VP8EncDspInit().
751
VP8CHisto VP8CollectHistogram;
752
VP8Idct VP8ITransform;
753
VP8Fdct VP8FTransform;
754
VP8Fdct VP8FTransform2;
755
VP8WHT VP8FTransformWHT;
756
VP8Intra4Preds VP8EncPredLuma4;
757
VP8IntraPreds VP8EncPredLuma16;
758
VP8IntraPreds VP8EncPredChroma8;
759
VP8Metric VP8SSE16x16;
760
VP8Metric VP8SSE8x8;
761
VP8Metric VP8SSE16x8;
762
VP8Metric VP8SSE4x4;
763
VP8WMetric VP8TDisto4x4;
764
VP8WMetric VP8TDisto16x16;
765
VP8MeanMetric VP8Mean16x4;
766
VP8QuantizeBlock VP8EncQuantizeBlock;
767
VP8Quantize2Blocks VP8EncQuantize2Blocks;
768
VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
769
VP8BlockCopy VP8Copy4x4;
770
VP8BlockCopy VP8Copy16x8;
771
772
extern VP8CPUInfo VP8GetCPUInfo;
773
extern void VP8EncDspInitSSE2(void);
774
extern void VP8EncDspInitSSE41(void);
775
extern void VP8EncDspInitNEON(void);
776
extern void VP8EncDspInitMIPS32(void);
777
extern void VP8EncDspInitMIPSdspR2(void);
778
extern void VP8EncDspInitMSA(void);
779
780
0
WEBP_DSP_INIT_FUNC(VP8EncDspInit) {
781
0
  VP8DspInit();  // common inverse transforms
782
0
  InitTables();
783
784
  // default C implementations
785
0
#if !WEBP_NEON_OMIT_C_CODE
786
0
  VP8ITransform = ITransform_C;
787
0
  VP8FTransform = FTransform_C;
788
0
  VP8FTransformWHT = FTransformWHT_C;
789
0
  VP8TDisto4x4 = Disto4x4_C;
790
0
  VP8TDisto16x16 = Disto16x16_C;
791
0
  VP8CollectHistogram = CollectHistogram_C;
792
0
  VP8SSE16x16 = SSE16x16_C;
793
0
  VP8SSE16x8 = SSE16x8_C;
794
0
  VP8SSE8x8 = SSE8x8_C;
795
0
  VP8SSE4x4 = SSE4x4_C;
796
0
#endif
797
798
0
#if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
799
0
  VP8EncQuantizeBlock = QuantizeBlock_C;
800
0
  VP8EncQuantize2Blocks = Quantize2Blocks_C;
801
0
  VP8EncQuantizeBlockWHT = QuantizeBlock_C;
802
0
#endif
803
804
0
#if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 || BPS != 32
805
0
  VP8EncPredLuma4 = Intra4Preds_C;
806
0
#endif
807
0
#if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64
808
0
  VP8EncPredLuma16 = Intra16Preds_C;
809
0
#endif
810
811
0
  VP8FTransform2 = FTransform2_C;
812
0
  VP8EncPredChroma8 = IntraChromaPreds_C;
813
0
  VP8Mean16x4 = Mean16x4_C;
814
0
  VP8Copy4x4 = Copy4x4_C;
815
0
  VP8Copy16x8 = Copy16x8_C;
816
817
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
818
0
  if (VP8GetCPUInfo != NULL) {
819
0
#if defined(WEBP_HAVE_SSE2)
820
0
    if (VP8GetCPUInfo(kSSE2)) {
821
0
      VP8EncDspInitSSE2();
822
0
#if defined(WEBP_HAVE_SSE41)
823
0
      if (VP8GetCPUInfo(kSSE4_1)) {
824
0
        VP8EncDspInitSSE41();
825
0
      }
826
0
#endif
827
0
    }
828
0
#endif
829
#if defined(WEBP_USE_MIPS32)
830
    if (VP8GetCPUInfo(kMIPS32)) {
831
      VP8EncDspInitMIPS32();
832
    }
833
#endif
834
#if defined(WEBP_USE_MIPS_DSP_R2)
835
    if (VP8GetCPUInfo(kMIPSdspR2)) {
836
      VP8EncDspInitMIPSdspR2();
837
    }
838
#endif
839
#if defined(WEBP_USE_MSA)
840
    if (VP8GetCPUInfo(kMSA)) {
841
      VP8EncDspInitMSA();
842
    }
843
#endif
844
0
  }
845
846
#if defined(WEBP_HAVE_NEON)
847
  if (WEBP_NEON_OMIT_C_CODE ||
848
      (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
849
    VP8EncDspInitNEON();
850
  }
851
#endif
852
853
0
  assert(VP8ITransform != NULL);
854
0
  assert(VP8FTransform != NULL);
855
0
  assert(VP8FTransformWHT != NULL);
856
0
  assert(VP8TDisto4x4 != NULL);
857
0
  assert(VP8TDisto16x16 != NULL);
858
0
  assert(VP8CollectHistogram != NULL);
859
0
  assert(VP8SSE16x16 != NULL);
860
0
  assert(VP8SSE16x8 != NULL);
861
0
  assert(VP8SSE8x8 != NULL);
862
0
  assert(VP8SSE4x4 != NULL);
863
0
  assert(VP8EncQuantizeBlock != NULL);
864
0
  assert(VP8EncQuantize2Blocks != NULL);
865
0
  assert(VP8FTransform2 != NULL);
866
0
  assert(VP8EncPredLuma4 != NULL);
867
0
  assert(VP8EncPredLuma16 != NULL);
868
0
  assert(VP8EncPredChroma8 != NULL);
869
0
  assert(VP8Mean16x4 != NULL);
870
0
  assert(VP8EncQuantizeBlockWHT != NULL);
871
0
  assert(VP8Copy4x4 != NULL);
872
0
  assert(VP8Copy16x8 != NULL);
873
0
}