xref: /aosp_15_r20/external/webp/sharpyuv/sharpyuv_dsp.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2022 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Speed-critical functions for Sharp YUV.
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li 
14*b2055c35SXin Li #include "sharpyuv/sharpyuv_dsp.h"
15*b2055c35SXin Li 
16*b2055c35SXin Li #include <assert.h>
17*b2055c35SXin Li #include <stdlib.h>
18*b2055c35SXin Li 
19*b2055c35SXin Li #include "sharpyuv/sharpyuv_cpu.h"
20*b2055c35SXin Li #include "src/webp/types.h"
21*b2055c35SXin Li 
22*b2055c35SXin Li //-----------------------------------------------------------------------------
23*b2055c35SXin Li 
24*b2055c35SXin Li #if !WEBP_NEON_OMIT_C_CODE
clip(int v,int max)25*b2055c35SXin Li static uint16_t clip(int v, int max) {
26*b2055c35SXin Li   return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
27*b2055c35SXin Li }
28*b2055c35SXin Li 
SharpYuvUpdateY_C(const uint16_t * ref,const uint16_t * src,uint16_t * dst,int len,int bit_depth)29*b2055c35SXin Li static uint64_t SharpYuvUpdateY_C(const uint16_t* ref, const uint16_t* src,
30*b2055c35SXin Li                                   uint16_t* dst, int len, int bit_depth) {
31*b2055c35SXin Li   uint64_t diff = 0;
32*b2055c35SXin Li   int i;
33*b2055c35SXin Li   const int max_y = (1 << bit_depth) - 1;
34*b2055c35SXin Li   for (i = 0; i < len; ++i) {
35*b2055c35SXin Li     const int diff_y = ref[i] - src[i];
36*b2055c35SXin Li     const int new_y = (int)dst[i] + diff_y;
37*b2055c35SXin Li     dst[i] = clip(new_y, max_y);
38*b2055c35SXin Li     diff += (uint64_t)abs(diff_y);
39*b2055c35SXin Li   }
40*b2055c35SXin Li   return diff;
41*b2055c35SXin Li }
42*b2055c35SXin Li 
SharpYuvUpdateRGB_C(const int16_t * ref,const int16_t * src,int16_t * dst,int len)43*b2055c35SXin Li static void SharpYuvUpdateRGB_C(const int16_t* ref, const int16_t* src,
44*b2055c35SXin Li                                 int16_t* dst, int len) {
45*b2055c35SXin Li   int i;
46*b2055c35SXin Li   for (i = 0; i < len; ++i) {
47*b2055c35SXin Li     const int diff_uv = ref[i] - src[i];
48*b2055c35SXin Li     dst[i] += diff_uv;
49*b2055c35SXin Li   }
50*b2055c35SXin Li }
51*b2055c35SXin Li 
SharpYuvFilterRow_C(const int16_t * A,const int16_t * B,int len,const uint16_t * best_y,uint16_t * out,int bit_depth)52*b2055c35SXin Li static void SharpYuvFilterRow_C(const int16_t* A, const int16_t* B, int len,
53*b2055c35SXin Li                                 const uint16_t* best_y, uint16_t* out,
54*b2055c35SXin Li                                 int bit_depth) {
55*b2055c35SXin Li   int i;
56*b2055c35SXin Li   const int max_y = (1 << bit_depth) - 1;
57*b2055c35SXin Li   for (i = 0; i < len; ++i, ++A, ++B) {
58*b2055c35SXin Li     const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4;
59*b2055c35SXin Li     const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4;
60*b2055c35SXin Li     out[2 * i + 0] = clip(best_y[2 * i + 0] + v0, max_y);
61*b2055c35SXin Li     out[2 * i + 1] = clip(best_y[2 * i + 1] + v1, max_y);
62*b2055c35SXin Li   }
63*b2055c35SXin Li }
64*b2055c35SXin Li #endif  // !WEBP_NEON_OMIT_C_CODE
65*b2055c35SXin Li 
66*b2055c35SXin Li //-----------------------------------------------------------------------------
67*b2055c35SXin Li 
68*b2055c35SXin Li uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref,
69*b2055c35SXin Li                             uint16_t* dst, int len, int bit_depth);
70*b2055c35SXin Li void (*SharpYuvUpdateRGB)(const int16_t* src, const int16_t* ref, int16_t* dst,
71*b2055c35SXin Li                           int len);
72*b2055c35SXin Li void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len,
73*b2055c35SXin Li                           const uint16_t* best_y, uint16_t* out, int bit_depth);
74*b2055c35SXin Li 
75*b2055c35SXin Li extern VP8CPUInfo SharpYuvGetCPUInfo;
76*b2055c35SXin Li extern void InitSharpYuvSSE2(void);
77*b2055c35SXin Li extern void InitSharpYuvNEON(void);
78*b2055c35SXin Li 
SharpYuvInitDsp(void)79*b2055c35SXin Li void SharpYuvInitDsp(void) {
80*b2055c35SXin Li #if !WEBP_NEON_OMIT_C_CODE
81*b2055c35SXin Li   SharpYuvUpdateY = SharpYuvUpdateY_C;
82*b2055c35SXin Li   SharpYuvUpdateRGB = SharpYuvUpdateRGB_C;
83*b2055c35SXin Li   SharpYuvFilterRow = SharpYuvFilterRow_C;
84*b2055c35SXin Li #endif
85*b2055c35SXin Li 
86*b2055c35SXin Li   if (SharpYuvGetCPUInfo != NULL) {
87*b2055c35SXin Li #if defined(WEBP_HAVE_SSE2)
88*b2055c35SXin Li     if (SharpYuvGetCPUInfo(kSSE2)) {
89*b2055c35SXin Li       InitSharpYuvSSE2();
90*b2055c35SXin Li     }
91*b2055c35SXin Li #endif  // WEBP_HAVE_SSE2
92*b2055c35SXin Li   }
93*b2055c35SXin Li 
94*b2055c35SXin Li #if defined(WEBP_HAVE_NEON)
95*b2055c35SXin Li   if (WEBP_NEON_OMIT_C_CODE ||
96*b2055c35SXin Li       (SharpYuvGetCPUInfo != NULL && SharpYuvGetCPUInfo(kNEON))) {
97*b2055c35SXin Li     InitSharpYuvNEON();
98*b2055c35SXin Li   }
99*b2055c35SXin Li #endif  // WEBP_HAVE_NEON
100*b2055c35SXin Li 
101*b2055c35SXin Li   assert(SharpYuvUpdateY != NULL);
102*b2055c35SXin Li   assert(SharpYuvUpdateRGB != NULL);
103*b2055c35SXin Li   assert(SharpYuvFilterRow != NULL);
104*b2055c35SXin Li }
105