upsampling_neon.c (16440B)
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 // NEON version of YUV to RGB upsampling functions. 11 // 12 // Author: mans@mansr.com (Mans Rullgard) 13 // Based on SSE code by: somnath@google.com (Somnath Banerjee) 14 15 #include "src/dsp/dsp.h" 16 17 #if defined(WEBP_USE_NEON) 18 19 #include <assert.h> 20 #include <arm_neon.h> 21 #include <string.h> 22 #include "src/dsp/neon.h" 23 #include "src/dsp/yuv.h" 24 25 #ifdef FANCY_UPSAMPLING 26 27 //----------------------------------------------------------------------------- 28 // U/V upsampling 29 30 // Loads 9 pixels each from rows r1 and r2 and generates 16 pixels. 31 #define UPSAMPLE_16PIXELS(r1, r2, out) do { \ 32 const uint8x8_t a = vld1_u8(r1 + 0); \ 33 const uint8x8_t b = vld1_u8(r1 + 1); \ 34 const uint8x8_t c = vld1_u8(r2 + 0); \ 35 const uint8x8_t d = vld1_u8(r2 + 1); \ 36 /* a + b + c + d */ \ 37 const uint16x8_t ad = vaddl_u8(a, d); \ 38 const uint16x8_t bc = vaddl_u8(b, c); \ 39 const uint16x8_t abcd = vaddq_u16(ad, bc); \ 40 /* 3a + b + c + 3d */ \ 41 const uint16x8_t al = vaddq_u16(abcd, vshlq_n_u16(ad, 1)); \ 42 /* a + 3b + 3c + d */ \ 43 const uint16x8_t bl = vaddq_u16(abcd, vshlq_n_u16(bc, 1)); \ 44 \ 45 const uint8x8_t diag2 = vshrn_n_u16(al, 3); \ 46 const uint8x8_t diag1 = vshrn_n_u16(bl, 3); \ 47 \ 48 const uint8x8_t A = vrhadd_u8(a, diag1); \ 49 const uint8x8_t B = vrhadd_u8(b, diag2); \ 50 const uint8x8_t C = vrhadd_u8(c, diag2); \ 51 const uint8x8_t D = vrhadd_u8(d, diag1); \ 52 \ 53 uint8x8x2_t A_B, C_D; \ 54 INIT_VECTOR2(A_B, A, B); \ 55 INIT_VECTOR2(C_D, C, D); \ 56 vst2_u8(out + 0, A_B); \ 57 vst2_u8(out + 32, C_D); \ 58 } while (0) 59 60 // Turn the macro into a function for reducing code-size when non-critical 61 static void Upsample16Pixels_NEON(const uint8_t* WEBP_RESTRICT const r1, 62 const uint8_t* WEBP_RESTRICT const r2, 63 uint8_t* WEBP_RESTRICT const out) { 64 UPSAMPLE_16PIXELS(r1, r2, out); 65 } 66 67 #define UPSAMPLE_LAST_BLOCK(tb, bb, num_pixels, out) { \ 68 uint8_t r1[9], r2[9]; \ 69 memcpy(r1, (tb), (num_pixels)); \ 70 memcpy(r2, (bb), (num_pixels)); \ 71 /* replicate last byte */ \ 72 memset(r1 + (num_pixels), r1[(num_pixels) - 1], 9 - (num_pixels)); \ 73 memset(r2 + (num_pixels), r2[(num_pixels) - 1], 9 - (num_pixels)); \ 74 Upsample16Pixels_NEON(r1, r2, out); \ 75 } 76 77 //----------------------------------------------------------------------------- 78 // YUV->RGB conversion 79 80 // note: we represent the 33050 large constant as 32768 + 282 81 static const int16_t kCoeffs1[4] = { 19077, 26149, 6419, 13320 }; 82 83 #define v255 vdup_n_u8(255) 84 85 #define STORE_Rgb(out, r, g, b) do { \ 86 uint8x8x3_t r_g_b; \ 87 INIT_VECTOR3(r_g_b, r, g, b); \ 88 vst3_u8(out, r_g_b); \ 89 } while (0) 90 91 #define STORE_Bgr(out, r, g, b) do { \ 92 uint8x8x3_t b_g_r; \ 93 INIT_VECTOR3(b_g_r, b, g, r); \ 94 vst3_u8(out, b_g_r); \ 95 } while (0) 96 97 #define STORE_Rgba(out, r, g, b) do { \ 98 uint8x8x4_t r_g_b_v255; \ 99 INIT_VECTOR4(r_g_b_v255, r, g, b, v255); \ 100 vst4_u8(out, r_g_b_v255); \ 101 } while (0) 102 103 #define STORE_Bgra(out, r, g, b) do { \ 104 uint8x8x4_t b_g_r_v255; \ 105 INIT_VECTOR4(b_g_r_v255, b, g, r, v255); \ 106 vst4_u8(out, b_g_r_v255); \ 107 } while (0) 108 109 #define STORE_Argb(out, r, g, b) do { \ 110 uint8x8x4_t v255_r_g_b; \ 111 INIT_VECTOR4(v255_r_g_b, v255, r, g, b); \ 112 vst4_u8(out, v255_r_g_b); \ 113 } while (0) 114 115 #if (WEBP_SWAP_16BIT_CSP == 0) 116 #define ZIP_U8(lo, hi) vzip_u8((lo), (hi)) 117 #else 118 #define ZIP_U8(lo, hi) vzip_u8((hi), (lo)) 119 #endif 120 121 #define STORE_Rgba4444(out, r, g, b) do { \ 122 const uint8x8_t rg = vsri_n_u8(r, g, 4); /* shift g, insert r */ \ 123 const uint8x8_t ba = vsri_n_u8(b, v255, 4); /* shift a, insert b */ \ 124 const uint8x8x2_t rgba4444 = ZIP_U8(rg, ba); \ 125 vst1q_u8(out, vcombine_u8(rgba4444.val[0], rgba4444.val[1])); \ 126 } while (0) 127 128 #define STORE_Rgb565(out, r, g, b) do { \ 129 const uint8x8_t rg = vsri_n_u8(r, g, 5); /* shift g and insert r */ \ 130 const uint8x8_t g1 = vshl_n_u8(g, 3); /* pre-shift g: 3bits */ \ 131 const uint8x8_t gb = vsri_n_u8(g1, b, 3); /* shift b and insert g */ \ 132 const uint8x8x2_t rgb565 = ZIP_U8(rg, gb); \ 133 vst1q_u8(out, vcombine_u8(rgb565.val[0], rgb565.val[1])); \ 134 } while (0) 135 136 #define CONVERT8(FMT, XSTEP, N, src_y, src_uv, out, cur_x) do { \ 137 int i; \ 138 for (i = 0; i < N; i += 8) { \ 139 const int off = ((cur_x) + i) * XSTEP; \ 140 const uint8x8_t y = vld1_u8((src_y) + (cur_x) + i); \ 141 const uint8x8_t u = vld1_u8((src_uv) + i + 0); \ 142 const uint8x8_t v = vld1_u8((src_uv) + i + 16); \ 143 const int16x8_t Y0 = vreinterpretq_s16_u16(vshll_n_u8(y, 7)); \ 144 const int16x8_t U0 = vreinterpretq_s16_u16(vshll_n_u8(u, 7)); \ 145 const int16x8_t V0 = vreinterpretq_s16_u16(vshll_n_u8(v, 7)); \ 146 const int16x8_t Y1 = vqdmulhq_lane_s16(Y0, coeff1, 0); \ 147 const int16x8_t R0 = vqdmulhq_lane_s16(V0, coeff1, 1); \ 148 const int16x8_t G0 = vqdmulhq_lane_s16(U0, coeff1, 2); \ 149 const int16x8_t G1 = vqdmulhq_lane_s16(V0, coeff1, 3); \ 150 const int16x8_t B0 = vqdmulhq_n_s16(U0, 282); \ 151 const int16x8_t R1 = vqaddq_s16(Y1, R_Rounder); \ 152 const int16x8_t G2 = vqaddq_s16(Y1, G_Rounder); \ 153 const int16x8_t B1 = vqaddq_s16(Y1, B_Rounder); \ 154 const int16x8_t R2 = vqaddq_s16(R0, R1); \ 155 const int16x8_t G3 = vqaddq_s16(G0, G1); \ 156 const int16x8_t B2 = vqaddq_s16(B0, B1); \ 157 const int16x8_t G4 = vqsubq_s16(G2, G3); \ 158 const int16x8_t B3 = vqaddq_s16(B2, U0); \ 159 const uint8x8_t R = vqshrun_n_s16(R2, YUV_FIX2); \ 160 const uint8x8_t G = vqshrun_n_s16(G4, YUV_FIX2); \ 161 const uint8x8_t B = vqshrun_n_s16(B3, YUV_FIX2); \ 162 STORE_ ## FMT(out + off, R, G, B); \ 163 } \ 164 } while (0) 165 166 #define CONVERT1(FUNC, XSTEP, N, src_y, src_uv, rgb, cur_x) { \ 167 int i; \ 168 for (i = 0; i < N; i++) { \ 169 const int off = ((cur_x) + i) * XSTEP; \ 170 const int y = src_y[(cur_x) + i]; \ 171 const int u = (src_uv)[i]; \ 172 const int v = (src_uv)[i + 16]; \ 173 FUNC(y, u, v, rgb + off); \ 174 } \ 175 } 176 177 #define CONVERT2RGB_8(FMT, XSTEP, top_y, bottom_y, uv, \ 178 top_dst, bottom_dst, cur_x, len) { \ 179 CONVERT8(FMT, XSTEP, len, top_y, uv, top_dst, cur_x); \ 180 if (bottom_y != NULL) { \ 181 CONVERT8(FMT, XSTEP, len, bottom_y, (uv) + 32, bottom_dst, cur_x); \ 182 } \ 183 } 184 185 #define CONVERT2RGB_1(FUNC, XSTEP, top_y, bottom_y, uv, \ 186 top_dst, bottom_dst, cur_x, len) { \ 187 CONVERT1(FUNC, XSTEP, len, top_y, uv, top_dst, cur_x); \ 188 if (bottom_y != NULL) { \ 189 CONVERT1(FUNC, XSTEP, len, bottom_y, (uv) + 32, bottom_dst, cur_x); \ 190 } \ 191 } 192 193 #define NEON_UPSAMPLE_FUNC(FUNC_NAME, FMT, XSTEP) \ 194 static void FUNC_NAME(const uint8_t* WEBP_RESTRICT top_y, \ 195 const uint8_t* WEBP_RESTRICT bottom_y, \ 196 const uint8_t* WEBP_RESTRICT top_u, \ 197 const uint8_t* WEBP_RESTRICT top_v, \ 198 const uint8_t* WEBP_RESTRICT cur_u, \ 199 const uint8_t* WEBP_RESTRICT cur_v, \ 200 uint8_t* WEBP_RESTRICT top_dst, \ 201 uint8_t* WEBP_RESTRICT bottom_dst, int len) { \ 202 int block; \ 203 /* 16 byte aligned array to cache reconstructed u and v */ \ 204 uint8_t uv_buf[2 * 32 + 15]; \ 205 uint8_t* const r_uv = (uint8_t*)((uintptr_t)(uv_buf + 15) & ~(uintptr_t)15); \ 206 const int uv_len = (len + 1) >> 1; \ 207 /* 9 pixels must be read-able for each block */ \ 208 const int num_blocks = (uv_len - 1) >> 3; \ 209 const int leftover = uv_len - num_blocks * 8; \ 210 const int last_pos = 1 + 16 * num_blocks; \ 211 \ 212 const int u_diag = ((top_u[0] + cur_u[0]) >> 1) + 1; \ 213 const int v_diag = ((top_v[0] + cur_v[0]) >> 1) + 1; \ 214 \ 215 const int16x4_t coeff1 = vld1_s16(kCoeffs1); \ 216 const int16x8_t R_Rounder = vdupq_n_s16(-14234); \ 217 const int16x8_t G_Rounder = vdupq_n_s16(8708); \ 218 const int16x8_t B_Rounder = vdupq_n_s16(-17685); \ 219 \ 220 /* Treat the first pixel in regular way */ \ 221 assert(top_y != NULL); \ 222 { \ 223 const int u0 = (top_u[0] + u_diag) >> 1; \ 224 const int v0 = (top_v[0] + v_diag) >> 1; \ 225 VP8YuvTo ## FMT(top_y[0], u0, v0, top_dst); \ 226 } \ 227 if (bottom_y != NULL) { \ 228 const int u0 = (cur_u[0] + u_diag) >> 1; \ 229 const int v0 = (cur_v[0] + v_diag) >> 1; \ 230 VP8YuvTo ## FMT(bottom_y[0], u0, v0, bottom_dst); \ 231 } \ 232 \ 233 for (block = 0; block < num_blocks; ++block) { \ 234 UPSAMPLE_16PIXELS(top_u, cur_u, r_uv); \ 235 UPSAMPLE_16PIXELS(top_v, cur_v, r_uv + 16); \ 236 CONVERT2RGB_8(FMT, XSTEP, top_y, bottom_y, r_uv, \ 237 top_dst, bottom_dst, 16 * block + 1, 16); \ 238 top_u += 8; \ 239 cur_u += 8; \ 240 top_v += 8; \ 241 cur_v += 8; \ 242 } \ 243 \ 244 UPSAMPLE_LAST_BLOCK(top_u, cur_u, leftover, r_uv); \ 245 UPSAMPLE_LAST_BLOCK(top_v, cur_v, leftover, r_uv + 16); \ 246 CONVERT2RGB_1(VP8YuvTo ## FMT, XSTEP, top_y, bottom_y, r_uv, \ 247 top_dst, bottom_dst, last_pos, len - last_pos); \ 248 } 249 250 // NEON variants of the fancy upsampler. 251 NEON_UPSAMPLE_FUNC(UpsampleRgbaLinePair_NEON, Rgba, 4) 252 NEON_UPSAMPLE_FUNC(UpsampleBgraLinePair_NEON, Bgra, 4) 253 #if !defined(WEBP_REDUCE_CSP) 254 NEON_UPSAMPLE_FUNC(UpsampleRgbLinePair_NEON, Rgb, 3) 255 NEON_UPSAMPLE_FUNC(UpsampleBgrLinePair_NEON, Bgr, 3) 256 NEON_UPSAMPLE_FUNC(UpsampleArgbLinePair_NEON, Argb, 4) 257 NEON_UPSAMPLE_FUNC(UpsampleRgba4444LinePair_NEON, Rgba4444, 2) 258 NEON_UPSAMPLE_FUNC(UpsampleRgb565LinePair_NEON, Rgb565, 2) 259 #endif // WEBP_REDUCE_CSP 260 261 //------------------------------------------------------------------------------ 262 // Entry point 263 264 extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */]; 265 266 extern void WebPInitUpsamplersNEON(void); 267 268 WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersNEON(void) { 269 WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair_NEON; 270 WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair_NEON; 271 WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair_NEON; 272 WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair_NEON; 273 #if !defined(WEBP_REDUCE_CSP) 274 WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair_NEON; 275 WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair_NEON; 276 WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair_NEON; 277 WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair_NEON; 278 WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair_NEON; 279 WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair_NEON; 280 WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair_NEON; 281 #endif // WEBP_REDUCE_CSP 282 } 283 284 #endif // FANCY_UPSAMPLING 285 286 #endif // WEBP_USE_NEON 287 288 #if !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_NEON)) 289 WEBP_DSP_INIT_STUB(WebPInitUpsamplersNEON) 290 #endif