1// Copyright 2022 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 functions for Sharp YUV.
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include "sharpyuv/sharpyuv_dsp.h"
15
16#include <assert.h>
17#include <stdlib.h>
18
19#include "sharpyuv/sharpyuv_cpu.h"
20
21//-----------------------------------------------------------------------------
22
23#if !WEBP_NEON_OMIT_C_CODE
24static uint16_t clip(int v, int max) {
25 return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
26}
27
28static uint64_t SharpYuvUpdateY_C(const uint16_t* ref, const uint16_t* src,
29 uint16_t* dst, int len, int bit_depth) {
30 uint64_t diff = 0;
31 int i;
32 const int max_y = (1 << bit_depth) - 1;
33 for (i = 0; i < len; ++i) {
34 const int diff_y = ref[i] - src[i];
35 const int new_y = (int)dst[i] + diff_y;
36 dst[i] = clip(new_y, max_y);
37 diff += (uint64_t)abs(diff_y);
38 }
39 return diff;
40}
41
42static void SharpYuvUpdateRGB_C(const int16_t* ref, const int16_t* src,
43 int16_t* dst, int len) {
44 int i;
45 for (i = 0; i < len; ++i) {
46 const int diff_uv = ref[i] - src[i];
47 dst[i] += diff_uv;
48 }
49}
50
51static void SharpYuvFilterRow_C(const int16_t* A, const int16_t* B, int len,
52 const uint16_t* best_y, uint16_t* out,
53 int bit_depth) {
54 int i;
55 const int max_y = (1 << bit_depth) - 1;
56 for (i = 0; i < len; ++i, ++A, ++B) {
57 const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4;
58 const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4;
59 out[2 * i + 0] = clip(best_y[2 * i + 0] + v0, max_y);
60 out[2 * i + 1] = clip(best_y[2 * i + 1] + v1, max_y);
61 }
62}
63#endif // !WEBP_NEON_OMIT_C_CODE
64
65//-----------------------------------------------------------------------------
66
67uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref,
68 uint16_t* dst, int len, int bit_depth);
69void (*SharpYuvUpdateRGB)(const int16_t* src, const int16_t* ref, int16_t* dst,
70 int len);
71void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len,
72 const uint16_t* best_y, uint16_t* out,
73 int bit_depth);
74
75extern VP8CPUInfo SharpYuvGetCPUInfo;
76extern void InitSharpYuvSSE2(void);
77extern void InitSharpYuvNEON(void);
78
79void SharpYuvInitDsp(void) {
80#if !WEBP_NEON_OMIT_C_CODE
81 SharpYuvUpdateY = SharpYuvUpdateY_C;
82 SharpYuvUpdateRGB = SharpYuvUpdateRGB_C;
83 SharpYuvFilterRow = SharpYuvFilterRow_C;
84#endif
85
86 if (SharpYuvGetCPUInfo != NULL) {
87#if defined(WEBP_HAVE_SSE2)
88 if (SharpYuvGetCPUInfo(kSSE2)) {
89 InitSharpYuvSSE2();
90 }
91#endif // WEBP_HAVE_SSE2
92 }
93
94#if defined(WEBP_HAVE_NEON)
95 if (WEBP_NEON_OMIT_C_CODE ||
96 (SharpYuvGetCPUInfo != NULL && SharpYuvGetCPUInfo(kNEON))) {
97 InitSharpYuvNEON();
98 }
99#endif // WEBP_HAVE_NEON
100
101 assert(SharpYuvUpdateY != NULL);
102 assert(SharpYuvUpdateRGB != NULL);
103 assert(SharpYuvFilterRow != NULL);
104}
105