xref: /aosp_15_r20/external/webp/src/dsp/rescaler_neon.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2015 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 // NEON version of rescaling functions
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li 
14*b2055c35SXin Li #include "src/dsp/dsp.h"
15*b2055c35SXin Li 
16*b2055c35SXin Li #if defined(WEBP_USE_NEON) && !defined(WEBP_REDUCE_SIZE)
17*b2055c35SXin Li 
18*b2055c35SXin Li #include <arm_neon.h>
19*b2055c35SXin Li #include <assert.h>
20*b2055c35SXin Li #include "src/dsp/neon.h"
21*b2055c35SXin Li #include "src/utils/rescaler_utils.h"
22*b2055c35SXin Li 
23*b2055c35SXin Li #define ROUNDER (WEBP_RESCALER_ONE >> 1)
24*b2055c35SXin Li #define MULT_FIX_C(x, y) (((uint64_t)(x) * (y) + ROUNDER) >> WEBP_RESCALER_RFIX)
25*b2055c35SXin Li #define MULT_FIX_FLOOR_C(x, y) (((uint64_t)(x) * (y)) >> WEBP_RESCALER_RFIX)
26*b2055c35SXin Li 
27*b2055c35SXin Li #define LOAD_32x4(SRC, DST) const uint32x4_t DST = vld1q_u32((SRC))
28*b2055c35SXin Li #define LOAD_32x8(SRC, DST0, DST1)                                    \
29*b2055c35SXin Li     LOAD_32x4(SRC + 0, DST0);                                         \
30*b2055c35SXin Li     LOAD_32x4(SRC + 4, DST1)
31*b2055c35SXin Li 
32*b2055c35SXin Li #define STORE_32x8(SRC0, SRC1, DST) do {                              \
33*b2055c35SXin Li     vst1q_u32((DST) + 0, SRC0);                                       \
34*b2055c35SXin Li     vst1q_u32((DST) + 4, SRC1);                                       \
35*b2055c35SXin Li } while (0)
36*b2055c35SXin Li 
37*b2055c35SXin Li #if (WEBP_RESCALER_RFIX == 32)
38*b2055c35SXin Li #define MAKE_HALF_CST(C) vdupq_n_s32((int32_t)((C) >> 1))
39*b2055c35SXin Li // note: B is actualy scale>>1. See MAKE_HALF_CST
40*b2055c35SXin Li #define MULT_FIX(A, B) \
41*b2055c35SXin Li     vreinterpretq_u32_s32(vqrdmulhq_s32(vreinterpretq_s32_u32((A)), (B)))
42*b2055c35SXin Li #define MULT_FIX_FLOOR(A, B) \
43*b2055c35SXin Li     vreinterpretq_u32_s32(vqdmulhq_s32(vreinterpretq_s32_u32((A)), (B)))
44*b2055c35SXin Li #else
45*b2055c35SXin Li #error "MULT_FIX/WEBP_RESCALER_RFIX need some more work"
46*b2055c35SXin Li #endif
47*b2055c35SXin Li 
Interpolate_NEON(const rescaler_t * const frow,const rescaler_t * const irow,uint32_t A,uint32_t B)48*b2055c35SXin Li static uint32x4_t Interpolate_NEON(const rescaler_t* const frow,
49*b2055c35SXin Li                                    const rescaler_t* const irow,
50*b2055c35SXin Li                                    uint32_t A, uint32_t B) {
51*b2055c35SXin Li   LOAD_32x4(frow, A0);
52*b2055c35SXin Li   LOAD_32x4(irow, B0);
53*b2055c35SXin Li   const uint64x2_t C0 = vmull_n_u32(vget_low_u32(A0), A);
54*b2055c35SXin Li   const uint64x2_t C1 = vmull_n_u32(vget_high_u32(A0), A);
55*b2055c35SXin Li   const uint64x2_t D0 = vmlal_n_u32(C0, vget_low_u32(B0), B);
56*b2055c35SXin Li   const uint64x2_t D1 = vmlal_n_u32(C1, vget_high_u32(B0), B);
57*b2055c35SXin Li   const uint32x4_t E = vcombine_u32(
58*b2055c35SXin Li       vrshrn_n_u64(D0, WEBP_RESCALER_RFIX),
59*b2055c35SXin Li       vrshrn_n_u64(D1, WEBP_RESCALER_RFIX));
60*b2055c35SXin Li   return E;
61*b2055c35SXin Li }
62*b2055c35SXin Li 
RescalerExportRowExpand_NEON(WebPRescaler * const wrk)63*b2055c35SXin Li static void RescalerExportRowExpand_NEON(WebPRescaler* const wrk) {
64*b2055c35SXin Li   int x_out;
65*b2055c35SXin Li   uint8_t* const dst = wrk->dst;
66*b2055c35SXin Li   rescaler_t* const irow = wrk->irow;
67*b2055c35SXin Li   const int x_out_max = wrk->dst_width * wrk->num_channels;
68*b2055c35SXin Li   const int max_span = x_out_max & ~7;
69*b2055c35SXin Li   const rescaler_t* const frow = wrk->frow;
70*b2055c35SXin Li   const uint32_t fy_scale = wrk->fy_scale;
71*b2055c35SXin Li   const int32x4_t fy_scale_half = MAKE_HALF_CST(fy_scale);
72*b2055c35SXin Li   assert(!WebPRescalerOutputDone(wrk));
73*b2055c35SXin Li   assert(wrk->y_accum <= 0);
74*b2055c35SXin Li   assert(wrk->y_expand);
75*b2055c35SXin Li   assert(wrk->y_sub != 0);
76*b2055c35SXin Li   if (wrk->y_accum == 0) {
77*b2055c35SXin Li     for (x_out = 0; x_out < max_span; x_out += 8) {
78*b2055c35SXin Li       LOAD_32x4(frow + x_out + 0, A0);
79*b2055c35SXin Li       LOAD_32x4(frow + x_out + 4, A1);
80*b2055c35SXin Li       const uint32x4_t B0 = MULT_FIX(A0, fy_scale_half);
81*b2055c35SXin Li       const uint32x4_t B1 = MULT_FIX(A1, fy_scale_half);
82*b2055c35SXin Li       const uint16x4_t C0 = vmovn_u32(B0);
83*b2055c35SXin Li       const uint16x4_t C1 = vmovn_u32(B1);
84*b2055c35SXin Li       const uint8x8_t D = vqmovn_u16(vcombine_u16(C0, C1));
85*b2055c35SXin Li       vst1_u8(dst + x_out, D);
86*b2055c35SXin Li     }
87*b2055c35SXin Li     for (; x_out < x_out_max; ++x_out) {
88*b2055c35SXin Li       const uint32_t J = frow[x_out];
89*b2055c35SXin Li       const int v = (int)MULT_FIX_C(J, fy_scale);
90*b2055c35SXin Li       dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
91*b2055c35SXin Li     }
92*b2055c35SXin Li   } else {
93*b2055c35SXin Li     const uint32_t B = WEBP_RESCALER_FRAC(-wrk->y_accum, wrk->y_sub);
94*b2055c35SXin Li     const uint32_t A = (uint32_t)(WEBP_RESCALER_ONE - B);
95*b2055c35SXin Li     for (x_out = 0; x_out < max_span; x_out += 8) {
96*b2055c35SXin Li       const uint32x4_t C0 =
97*b2055c35SXin Li           Interpolate_NEON(frow + x_out + 0, irow + x_out + 0, A, B);
98*b2055c35SXin Li       const uint32x4_t C1 =
99*b2055c35SXin Li           Interpolate_NEON(frow + x_out + 4, irow + x_out + 4, A, B);
100*b2055c35SXin Li       const uint32x4_t D0 = MULT_FIX(C0, fy_scale_half);
101*b2055c35SXin Li       const uint32x4_t D1 = MULT_FIX(C1, fy_scale_half);
102*b2055c35SXin Li       const uint16x4_t E0 = vmovn_u32(D0);
103*b2055c35SXin Li       const uint16x4_t E1 = vmovn_u32(D1);
104*b2055c35SXin Li       const uint8x8_t F = vqmovn_u16(vcombine_u16(E0, E1));
105*b2055c35SXin Li       vst1_u8(dst + x_out, F);
106*b2055c35SXin Li     }
107*b2055c35SXin Li     for (; x_out < x_out_max; ++x_out) {
108*b2055c35SXin Li       const uint64_t I = (uint64_t)A * frow[x_out]
109*b2055c35SXin Li                        + (uint64_t)B * irow[x_out];
110*b2055c35SXin Li       const uint32_t J = (uint32_t)((I + ROUNDER) >> WEBP_RESCALER_RFIX);
111*b2055c35SXin Li       const int v = (int)MULT_FIX_C(J, fy_scale);
112*b2055c35SXin Li       dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
113*b2055c35SXin Li     }
114*b2055c35SXin Li   }
115*b2055c35SXin Li }
116*b2055c35SXin Li 
RescalerExportRowShrink_NEON(WebPRescaler * const wrk)117*b2055c35SXin Li static void RescalerExportRowShrink_NEON(WebPRescaler* const wrk) {
118*b2055c35SXin Li   int x_out;
119*b2055c35SXin Li   uint8_t* const dst = wrk->dst;
120*b2055c35SXin Li   rescaler_t* const irow = wrk->irow;
121*b2055c35SXin Li   const int x_out_max = wrk->dst_width * wrk->num_channels;
122*b2055c35SXin Li   const int max_span = x_out_max & ~7;
123*b2055c35SXin Li   const rescaler_t* const frow = wrk->frow;
124*b2055c35SXin Li   const uint32_t yscale = wrk->fy_scale * (-wrk->y_accum);
125*b2055c35SXin Li   const uint32_t fxy_scale = wrk->fxy_scale;
126*b2055c35SXin Li   const uint32x4_t zero = vdupq_n_u32(0);
127*b2055c35SXin Li   const int32x4_t yscale_half = MAKE_HALF_CST(yscale);
128*b2055c35SXin Li   const int32x4_t fxy_scale_half = MAKE_HALF_CST(fxy_scale);
129*b2055c35SXin Li   assert(!WebPRescalerOutputDone(wrk));
130*b2055c35SXin Li   assert(wrk->y_accum <= 0);
131*b2055c35SXin Li   assert(!wrk->y_expand);
132*b2055c35SXin Li   if (yscale) {
133*b2055c35SXin Li     for (x_out = 0; x_out < max_span; x_out += 8) {
134*b2055c35SXin Li       LOAD_32x8(frow + x_out, in0, in1);
135*b2055c35SXin Li       LOAD_32x8(irow + x_out, in2, in3);
136*b2055c35SXin Li       const uint32x4_t A0 = MULT_FIX_FLOOR(in0, yscale_half);
137*b2055c35SXin Li       const uint32x4_t A1 = MULT_FIX_FLOOR(in1, yscale_half);
138*b2055c35SXin Li       const uint32x4_t B0 = vqsubq_u32(in2, A0);
139*b2055c35SXin Li       const uint32x4_t B1 = vqsubq_u32(in3, A1);
140*b2055c35SXin Li       const uint32x4_t C0 = MULT_FIX(B0, fxy_scale_half);
141*b2055c35SXin Li       const uint32x4_t C1 = MULT_FIX(B1, fxy_scale_half);
142*b2055c35SXin Li       const uint16x4_t D0 = vmovn_u32(C0);
143*b2055c35SXin Li       const uint16x4_t D1 = vmovn_u32(C1);
144*b2055c35SXin Li       const uint8x8_t E = vqmovn_u16(vcombine_u16(D0, D1));
145*b2055c35SXin Li       vst1_u8(dst + x_out, E);
146*b2055c35SXin Li       STORE_32x8(A0, A1, irow + x_out);
147*b2055c35SXin Li     }
148*b2055c35SXin Li     for (; x_out < x_out_max; ++x_out) {
149*b2055c35SXin Li       const uint32_t frac = (uint32_t)MULT_FIX_FLOOR_C(frow[x_out], yscale);
150*b2055c35SXin Li       const int v = (int)MULT_FIX_C(irow[x_out] - frac, fxy_scale);
151*b2055c35SXin Li       dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
152*b2055c35SXin Li       irow[x_out] = frac;   // new fractional start
153*b2055c35SXin Li     }
154*b2055c35SXin Li   } else {
155*b2055c35SXin Li     for (x_out = 0; x_out < max_span; x_out += 8) {
156*b2055c35SXin Li       LOAD_32x8(irow + x_out, in0, in1);
157*b2055c35SXin Li       const uint32x4_t A0 = MULT_FIX(in0, fxy_scale_half);
158*b2055c35SXin Li       const uint32x4_t A1 = MULT_FIX(in1, fxy_scale_half);
159*b2055c35SXin Li       const uint16x4_t B0 = vmovn_u32(A0);
160*b2055c35SXin Li       const uint16x4_t B1 = vmovn_u32(A1);
161*b2055c35SXin Li       const uint8x8_t C = vqmovn_u16(vcombine_u16(B0, B1));
162*b2055c35SXin Li       vst1_u8(dst + x_out, C);
163*b2055c35SXin Li       STORE_32x8(zero, zero, irow + x_out);
164*b2055c35SXin Li     }
165*b2055c35SXin Li     for (; x_out < x_out_max; ++x_out) {
166*b2055c35SXin Li       const int v = (int)MULT_FIX_C(irow[x_out], fxy_scale);
167*b2055c35SXin Li       dst[x_out] = (v > 255) ? 255u : (uint8_t)v;
168*b2055c35SXin Li       irow[x_out] = 0;
169*b2055c35SXin Li     }
170*b2055c35SXin Li   }
171*b2055c35SXin Li }
172*b2055c35SXin Li 
173*b2055c35SXin Li #undef MULT_FIX_FLOOR_C
174*b2055c35SXin Li #undef MULT_FIX_C
175*b2055c35SXin Li #undef MULT_FIX_FLOOR
176*b2055c35SXin Li #undef MULT_FIX
177*b2055c35SXin Li #undef ROUNDER
178*b2055c35SXin Li 
179*b2055c35SXin Li //------------------------------------------------------------------------------
180*b2055c35SXin Li 
181*b2055c35SXin Li extern void WebPRescalerDspInitNEON(void);
182*b2055c35SXin Li 
WebPRescalerDspInitNEON(void)183*b2055c35SXin Li WEBP_TSAN_IGNORE_FUNCTION void WebPRescalerDspInitNEON(void) {
184*b2055c35SXin Li   WebPRescalerExportRowExpand = RescalerExportRowExpand_NEON;
185*b2055c35SXin Li   WebPRescalerExportRowShrink = RescalerExportRowShrink_NEON;
186*b2055c35SXin Li }
187*b2055c35SXin Li 
188*b2055c35SXin Li #else     // !WEBP_USE_NEON
189*b2055c35SXin Li 
190*b2055c35SXin Li WEBP_DSP_INIT_STUB(WebPRescalerDspInitNEON)
191*b2055c35SXin Li 
192*b2055c35SXin Li #endif    // WEBP_USE_NEON
193