| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "mat.h" |
| 16 | |
| 17 | #include <limits.h> |
| 18 | |
| 19 | #if __ARM_NEON |
| 20 | #include <arm_neon.h> |
| 21 | #endif // __ARM_NEON |
| 22 | #include "platform.h" |
| 23 | |
| 24 | namespace ncnn { |
| 25 | |
| 26 | #if NCNN_PIXEL |
| 27 | static void vresize_two(const short* rows0p, const short* rows1p, int wsize, unsigned char* Dp0, unsigned char* Dp1, short b0, short b1, short b2, short b3) |
| 28 | { |
| 29 | int dx = 0; |
| 30 | #if __ARM_NEON |
| 31 | int16x8_t _b0 = vdupq_n_s16(b0); |
| 32 | int16x8_t _b1 = vdupq_n_s16(b1); |
| 33 | int16x8_t _b2 = vdupq_n_s16(b2); |
| 34 | int16x8_t _b3 = vdupq_n_s16(b3); |
| 35 | for (; dx + 15 < wsize; dx += 16) |
| 36 | { |
| 37 | int16x8_t _r00 = vld1q_s16(rows0p); |
| 38 | int16x8_t _r01 = vld1q_s16(rows0p + 8); |
| 39 | int16x8_t _r10 = vld1q_s16(rows1p); |
| 40 | int16x8_t _r11 = vld1q_s16(rows1p + 8); |
| 41 | int16x8_t _acc00 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r00, _b0), 1), vqdmulhq_s16(_r10, _b1), 1); |
| 42 | int16x8_t _acc01 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r01, _b0), 1), vqdmulhq_s16(_r11, _b1), 1); |
| 43 | int16x8_t _acc10 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r00, _b2), 1), vqdmulhq_s16(_r10, _b3), 1); |
| 44 | int16x8_t _acc11 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r01, _b2), 1), vqdmulhq_s16(_r11, _b3), 1); |
| 45 | uint8x16_t _Dp0 = vcombine_u8(vqrshrun_n_s16(_acc00, 2), vqrshrun_n_s16(_acc01, 2)); |
| 46 | uint8x16_t _Dp1 = vcombine_u8(vqrshrun_n_s16(_acc10, 2), vqrshrun_n_s16(_acc11, 2)); |
| 47 | vst1q_u8(Dp0, _Dp0); |
| 48 | vst1q_u8(Dp1, _Dp1); |
| 49 | Dp0 += 16; |
| 50 | Dp1 += 16; |
| 51 | rows0p += 16; |
| 52 | rows1p += 16; |
| 53 | } |
| 54 | for (; dx + 7 < wsize; dx += 8) |
| 55 | { |
| 56 | int16x8_t _r0 = vld1q_s16(rows0p); |
| 57 | int16x8_t _r1 = vld1q_s16(rows1p); |
| 58 | int16x8_t _acc0 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r0, _b0), 1), vqdmulhq_s16(_r1, _b1), 1); |
| 59 | int16x8_t _acc1 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r0, _b2), 1), vqdmulhq_s16(_r1, _b3), 1); |
| 60 | uint8x8_t _Dp0 = vqrshrun_n_s16(_acc0, 2); |
| 61 | uint8x8_t _Dp1 = vqrshrun_n_s16(_acc1, 2); |
| 62 | vst1_u8(Dp0, _Dp0); |
| 63 | vst1_u8(Dp1, _Dp1); |
| 64 | Dp0 += 8; |
| 65 | Dp1 += 8; |
| 66 | rows0p += 8; |
| 67 | rows1p += 8; |
| 68 | } |
| 69 | #endif // __ARM_NEON |
| 70 | #if __SSE2__ |
| 71 | __m128i _b0 = _mm_set1_epi16(b0); |
| 72 | __m128i _b1 = _mm_set1_epi16(b1); |
| 73 | __m128i _b2 = _mm_set1_epi16(b2); |
| 74 | __m128i _b3 = _mm_set1_epi16(b3); |
| 75 | __m128i _v2 = _mm_set1_epi16(2); |
| 76 | for (; dx + 15 < wsize; dx += 16) |
| 77 | { |
| 78 | __m128i _r00 = _mm_loadu_si128((const __m128i*)rows0p); |
| 79 | __m128i _r01 = _mm_loadu_si128((const __m128i*)(rows0p + 8)); |
| 80 | __m128i _r10 = _mm_loadu_si128((const __m128i*)rows1p); |
| 81 | __m128i _r11 = _mm_loadu_si128((const __m128i*)(rows1p + 8)); |
| 82 | __m128i _acc00 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b0), _mm_mulhi_epi16(_r10, _b1)); |
| 83 | __m128i _acc01 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b0), _mm_mulhi_epi16(_r11, _b1)); |
| 84 | __m128i _acc10 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b2), _mm_mulhi_epi16(_r10, _b3)); |
| 85 | __m128i _acc11 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b2), _mm_mulhi_epi16(_r11, _b3)); |
| 86 | _acc00 = _mm_srai_epi16(_mm_add_epi16(_acc00, _v2), 2); |
| 87 | _acc01 = _mm_srai_epi16(_mm_add_epi16(_acc01, _v2), 2); |
| 88 | _acc10 = _mm_srai_epi16(_mm_add_epi16(_acc10, _v2), 2); |
| 89 | _acc11 = _mm_srai_epi16(_mm_add_epi16(_acc11, _v2), 2); |
| 90 | __m128i _Dp0 = _mm_packus_epi16(_acc00, _acc01); |
| 91 | __m128i _Dp1 = _mm_packus_epi16(_acc10, _acc11); |
| 92 | _mm_storeu_si128((__m128i*)Dp0, _Dp0); |
| 93 | _mm_storeu_si128((__m128i*)Dp1, _Dp1); |
| 94 | Dp0 += 16; |
| 95 | Dp1 += 16; |
| 96 | rows0p += 16; |
| 97 | rows1p += 16; |
| 98 | } |
| 99 | for (; dx + 7 < wsize; dx += 8) |
| 100 | { |
| 101 | __m128i _r0 = _mm_loadu_si128((const __m128i*)rows0p); |
| 102 | __m128i _r1 = _mm_loadu_si128((const __m128i*)rows1p); |
| 103 | __m128i _acc0 = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b0), _mm_mulhi_epi16(_r1, _b1)); |
| 104 | __m128i _acc1 = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b2), _mm_mulhi_epi16(_r1, _b3)); |
| 105 | _acc0 = _mm_srai_epi16(_mm_add_epi16(_acc0, _v2), 2); |
| 106 | _acc1 = _mm_srai_epi16(_mm_add_epi16(_acc1, _v2), 2); |
| 107 | __m128i _Dp0 = _mm_packus_epi16(_acc0, _acc0); |
| 108 | __m128i _Dp1 = _mm_packus_epi16(_acc1, _acc1); |
| 109 | _mm_storel_epi64((__m128i*)Dp0, _Dp0); |
| 110 | _mm_storel_epi64((__m128i*)Dp1, _Dp1); |
| 111 | Dp0 += 8; |
| 112 | Dp1 += 8; |
| 113 | rows0p += 8; |
| 114 | rows1p += 8; |
| 115 | } |
| 116 | #endif // __SSE2__ |
| 117 | for (; dx < wsize; dx++) |
| 118 | { |
| 119 | short s0 = *rows0p++; |
| 120 | short s1 = *rows1p++; |
| 121 | |
| 122 | *Dp0++ = (unsigned char)(((short)((b0 * s0) >> 16) + (short)((b1 * s1) >> 16) + 2) >> 2); |
| 123 | *Dp1++ = (unsigned char)(((short)((b2 * s0) >> 16) + (short)((b3 * s1) >> 16) + 2) >> 2); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static void vresize_one(const short* rows0p, const short* rows1p, int wsize, unsigned char* Dp, short b0, short b1) |
| 128 | { |
| 129 | int dx = 0; |
| 130 | #if __ARM_NEON |
| 131 | int16x8_t _b0 = vdupq_n_s16(b0); |
| 132 | int16x8_t _b1 = vdupq_n_s16(b1); |
| 133 | for (; dx + 15 < wsize; dx += 16) |
| 134 | { |
| 135 | int16x8_t _r00 = vld1q_s16(rows0p); |
| 136 | int16x8_t _r01 = vld1q_s16(rows0p + 8); |
| 137 | int16x8_t _r10 = vld1q_s16(rows1p); |
| 138 | int16x8_t _r11 = vld1q_s16(rows1p + 8); |
| 139 | int16x8_t _acc0 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r00, _b0), 1), vqdmulhq_s16(_r10, _b1), 1); |
| 140 | int16x8_t _acc1 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r01, _b0), 1), vqdmulhq_s16(_r11, _b1), 1); |
| 141 | uint8x16_t _Dp = vcombine_u8(vqrshrun_n_s16(_acc0, 2), vqrshrun_n_s16(_acc1, 2)); |
| 142 | vst1q_u8(Dp, _Dp); |
| 143 | Dp += 16; |
| 144 | rows0p += 16; |
| 145 | rows1p += 16; |
| 146 | } |
| 147 | for (; dx + 7 < wsize; dx += 8) |
| 148 | { |
| 149 | int16x8_t _r0 = vld1q_s16(rows0p); |
| 150 | int16x8_t _r1 = vld1q_s16(rows1p); |
| 151 | int16x8_t _acc = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r0, _b0), 1), vqdmulhq_s16(_r1, _b1), 1); |
| 152 | uint8x8_t _Dp = vqrshrun_n_s16(_acc, 2); |
| 153 | vst1_u8(Dp, _Dp); |
| 154 | Dp += 8; |
| 155 | rows0p += 8; |
| 156 | rows1p += 8; |
| 157 | } |
| 158 | #endif // __ARM_NEON |
| 159 | #if __SSE2__ |
| 160 | __m128i _b0 = _mm_set1_epi16(b0); |
| 161 | __m128i _b1 = _mm_set1_epi16(b1); |
| 162 | __m128i _v2 = _mm_set1_epi16(2); |
| 163 | for (; dx + 15 < wsize; dx += 16) |
| 164 | { |
| 165 | __m128i _r00 = _mm_loadu_si128((const __m128i*)rows0p); |
| 166 | __m128i _r01 = _mm_loadu_si128((const __m128i*)(rows0p + 8)); |
| 167 | __m128i _r10 = _mm_loadu_si128((const __m128i*)rows1p); |
| 168 | __m128i _r11 = _mm_loadu_si128((const __m128i*)(rows1p + 8)); |
| 169 | __m128i _acc0 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b0), _mm_mulhi_epi16(_r10, _b1)); |
| 170 | __m128i _acc1 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b0), _mm_mulhi_epi16(_r11, _b1)); |
| 171 | _acc0 = _mm_srai_epi16(_mm_add_epi16(_acc0, _v2), 2); |
| 172 | _acc1 = _mm_srai_epi16(_mm_add_epi16(_acc1, _v2), 2); |
| 173 | __m128i _Dp = _mm_packus_epi16(_acc0, _acc1); |
| 174 | _mm_storeu_si128((__m128i*)Dp, _Dp); |
| 175 | Dp += 16; |
| 176 | rows0p += 16; |
| 177 | rows1p += 16; |
| 178 | } |
| 179 | for (; dx + 7 < wsize; dx += 8) |
| 180 | { |
| 181 | __m128i _r0 = _mm_loadu_si128((const __m128i*)rows0p); |
| 182 | __m128i _r1 = _mm_loadu_si128((const __m128i*)rows1p); |
| 183 | __m128i _acc = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b0), _mm_mulhi_epi16(_r1, _b1)); |
| 184 | _acc = _mm_srai_epi16(_mm_add_epi16(_acc, _v2), 2); |
| 185 | __m128i _Dp = _mm_packus_epi16(_acc, _acc); |
| 186 | _mm_storel_epi64((__m128i*)Dp, _Dp); |
| 187 | Dp += 8; |
| 188 | rows0p += 8; |
| 189 | rows1p += 8; |
| 190 | } |
| 191 | #endif // __SSE2__ |
| 192 | for (; dx < wsize; dx++) |
| 193 | { |
| 194 | short s0 = *rows0p++; |
| 195 | short s1 = *rows1p++; |
| 196 | |
| 197 | *Dp++ = (unsigned char)(((short)((b0 * s0) >> 16) + (short)((b1 * s1) >> 16) + 2) >> 2); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h) |
| 202 | { |
| 203 | return resize_bilinear_c1(src, srcw, srch, srcw, dst, w, h, w); |
| 204 | } |
| 205 | |
| 206 | void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h) |
| 207 | { |
| 208 | return resize_bilinear_c2(src, srcw, srch, srcw * 2, dst, w, h, w * 2); |
| 209 | } |
| 210 | |
| 211 | void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h) |
| 212 | { |
| 213 | return resize_bilinear_c3(src, srcw, srch, srcw * 3, dst, w, h, w * 3); |
| 214 | } |
| 215 | |
| 216 | void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h) |
| 217 | { |
| 218 | return resize_bilinear_c4(src, srcw, srch, srcw * 4, dst, w, h, w * 4); |
| 219 | } |
| 220 | |
| 221 | void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride) |
| 222 | { |
| 223 | const int INTER_RESIZE_COEF_BITS = 11; |
| 224 | const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS; |
| 225 | |
| 226 | double scale_x = (double)srcw / w; |
| 227 | double scale_y = (double)srch / h; |
| 228 | |
| 229 | int* buf = new int[w + h + w + h]; |
| 230 | |
| 231 | int* xofs = buf; //new int[w]; |
| 232 | int* yofs = buf + w; //new int[h]; |
| 233 | |
| 234 | short* ialpha = (short*)(buf + w + h); //new short[w * 2]; |
| 235 | short* ibeta = (short*)(buf + w + h + w); //new short[h * 2]; |
| 236 | |
| 237 | float fx; |
| 238 | float fy; |
| 239 | int sx; |
| 240 | int sy; |
| 241 | |
| 242 | #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX); |
| 243 | |
| 244 | for (int dx = 0; dx < w; dx++) |
| 245 | { |
| 246 | fx = (float)((dx + 0.5) * scale_x - 0.5); |
| 247 | sx = static_cast<int>(floor(fx)); |
| 248 | fx -= sx; |
| 249 | |
| 250 | if (sx < 0) |
| 251 | { |
| 252 | sx = 0; |
| 253 | fx = 0.f; |
| 254 | } |
| 255 | if (sx >= srcw - 1) |
| 256 | { |
| 257 | sx = srcw - 2; |
| 258 | fx = 1.f; |
| 259 | } |
| 260 | |
| 261 | xofs[dx] = sx; |
| 262 | |
| 263 | float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE; |
| 264 | float a1 = fx * INTER_RESIZE_COEF_SCALE; |
| 265 | |
| 266 | ialpha[dx * 2] = SATURATE_CAST_SHORT(a0); |
| 267 | ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1); |
| 268 | } |
| 269 | |
| 270 | for (int dy = 0; dy < h; dy++) |
| 271 | { |
| 272 | fy = (float)((dy + 0.5) * scale_y - 0.5); |
| 273 | sy = static_cast<int>(floor(fy)); |
| 274 | fy -= sy; |
| 275 | |
| 276 | if (sy < 0) |
| 277 | { |
| 278 | sy = 0; |
| 279 | fy = 0.f; |
| 280 | } |
| 281 | if (sy >= srch - 1) |
| 282 | { |
| 283 | sy = srch - 2; |
| 284 | fy = 1.f; |
| 285 | } |
| 286 | |
| 287 | yofs[dy] = sy; |
| 288 | |
| 289 | float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE; |
| 290 | float b1 = fy * INTER_RESIZE_COEF_SCALE; |
| 291 | |
| 292 | ibeta[dy * 2] = SATURATE_CAST_SHORT(b0); |
| 293 | ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1); |
| 294 | } |
| 295 | |
| 296 | #undef SATURATE_CAST_SHORT |
| 297 | |
| 298 | // loop body |
| 299 | Mat rowsbuf0(w, (size_t)2u); |
| 300 | Mat rowsbuf1(w, (size_t)2u); |
| 301 | short* rows0 = (short*)rowsbuf0.data; |
| 302 | short* rows1 = (short*)rowsbuf1.data; |
| 303 | |
| 304 | int prev_sy1 = -2; |
| 305 | |
| 306 | for (int dy = 0; dy < h; dy++) |
| 307 | { |
| 308 | sy = yofs[dy]; |
| 309 | |
| 310 | if (sy == prev_sy1) |
| 311 | { |
| 312 | // reuse all rows |
| 313 | } |
| 314 | else if (sy == prev_sy1 + 1) |
| 315 | { |
| 316 | // hresize one row |
| 317 | short* rows0_old = rows0; |
| 318 | rows0 = rows1; |
| 319 | rows1 = rows0_old; |
| 320 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 321 | |
| 322 | const short* ialphap = ialpha; |
| 323 | short* rows1p = rows1; |
| 324 | for (int dx = 0; dx < w; dx++) |
| 325 | { |
| 326 | sx = xofs[dx]; |
| 327 | short a0 = ialphap[0]; |
| 328 | short a1 = ialphap[1]; |
| 329 | |
| 330 | const unsigned char* S1p = S1 + sx; |
| 331 | rows1p[dx] = (S1p[0] * a0 + S1p[1] * a1) >> 4; |
| 332 | |
| 333 | ialphap += 2; |
| 334 | } |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | // hresize two rows |
| 339 | const unsigned char* S0 = src + srcstride * (sy); |
| 340 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 341 | |
| 342 | const short* ialphap = ialpha; |
| 343 | short* rows0p = rows0; |
| 344 | short* rows1p = rows1; |
| 345 | for (int dx = 0; dx < w; dx++) |
| 346 | { |
| 347 | sx = xofs[dx]; |
| 348 | short a0 = ialphap[0]; |
| 349 | short a1 = ialphap[1]; |
| 350 | |
| 351 | const unsigned char* S0p = S0 + sx; |
| 352 | const unsigned char* S1p = S1 + sx; |
| 353 | rows0p[dx] = (S0p[0] * a0 + S0p[1] * a1) >> 4; |
| 354 | rows1p[dx] = (S1p[0] * a0 + S1p[1] * a1) >> 4; |
| 355 | |
| 356 | ialphap += 2; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | prev_sy1 = sy; |
| 361 | |
| 362 | if (dy + 1 < h && yofs[dy + 1] == sy) |
| 363 | { |
| 364 | // vresize for two rows |
| 365 | unsigned char* Dp0 = dst + stride * dy; |
| 366 | unsigned char* Dp1 = dst + stride * (dy + 1); |
| 367 | |
| 368 | vresize_two(rows0, rows1, w, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]); |
| 369 | |
| 370 | ibeta += 4; |
| 371 | dy += 1; |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | // vresize |
| 376 | unsigned char* Dp = dst + stride * dy; |
| 377 | |
| 378 | vresize_one(rows0, rows1, w, Dp, ibeta[0], ibeta[1]); |
| 379 | |
| 380 | ibeta += 2; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | delete[] buf; |
| 385 | } |
| 386 | |
| 387 | void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride) |
| 388 | { |
| 389 | const int INTER_RESIZE_COEF_BITS = 11; |
| 390 | const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS; |
| 391 | |
| 392 | double scale_x = (double)srcw / w; |
| 393 | double scale_y = (double)srch / h; |
| 394 | |
| 395 | int* buf = new int[w + h + w + h]; |
| 396 | |
| 397 | int* xofs = buf; //new int[w]; |
| 398 | int* yofs = buf + w; //new int[h]; |
| 399 | |
| 400 | short* ialpha = (short*)(buf + w + h); //new short[w * 2]; |
| 401 | short* ibeta = (short*)(buf + w + h + w); //new short[h * 2]; |
| 402 | |
| 403 | float fx; |
| 404 | float fy; |
| 405 | int sx; |
| 406 | int sy; |
| 407 | |
| 408 | #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX); |
| 409 | |
| 410 | for (int dx = 0; dx < w; dx++) |
| 411 | { |
| 412 | fx = (float)((dx + 0.5) * scale_x - 0.5); |
| 413 | sx = static_cast<int>(floor(fx)); |
| 414 | fx -= sx; |
| 415 | |
| 416 | if (sx < 0) |
| 417 | { |
| 418 | sx = 0; |
| 419 | fx = 0.f; |
| 420 | } |
| 421 | if (sx >= srcw - 1) |
| 422 | { |
| 423 | sx = srcw - 2; |
| 424 | fx = 1.f; |
| 425 | } |
| 426 | |
| 427 | xofs[dx] = sx * 2; |
| 428 | |
| 429 | float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE; |
| 430 | float a1 = fx * INTER_RESIZE_COEF_SCALE; |
| 431 | |
| 432 | ialpha[dx * 2] = SATURATE_CAST_SHORT(a0); |
| 433 | ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1); |
| 434 | } |
| 435 | |
| 436 | for (int dy = 0; dy < h; dy++) |
| 437 | { |
| 438 | fy = (float)((dy + 0.5) * scale_y - 0.5); |
| 439 | sy = static_cast<int>(floor(fy)); |
| 440 | fy -= sy; |
| 441 | |
| 442 | if (sy < 0) |
| 443 | { |
| 444 | sy = 0; |
| 445 | fy = 0.f; |
| 446 | } |
| 447 | if (sy >= srch - 1) |
| 448 | { |
| 449 | sy = srch - 2; |
| 450 | fy = 1.f; |
| 451 | } |
| 452 | |
| 453 | yofs[dy] = sy; |
| 454 | |
| 455 | float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE; |
| 456 | float b1 = fy * INTER_RESIZE_COEF_SCALE; |
| 457 | |
| 458 | ibeta[dy * 2] = SATURATE_CAST_SHORT(b0); |
| 459 | ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1); |
| 460 | } |
| 461 | |
| 462 | #undef SATURATE_CAST_SHORT |
| 463 | |
| 464 | // loop body |
| 465 | Mat rowsbuf0(w * 2 + 2, (size_t)2u); |
| 466 | Mat rowsbuf1(w * 2 + 2, (size_t)2u); |
| 467 | short* rows0 = (short*)rowsbuf0.data; |
| 468 | short* rows1 = (short*)rowsbuf1.data; |
| 469 | |
| 470 | int prev_sy1 = -2; |
| 471 | |
| 472 | for (int dy = 0; dy < h; dy++) |
| 473 | { |
| 474 | sy = yofs[dy]; |
| 475 | |
| 476 | if (sy == prev_sy1) |
| 477 | { |
| 478 | // reuse all rows |
| 479 | } |
| 480 | else if (sy == prev_sy1 + 1) |
| 481 | { |
| 482 | // hresize one row |
| 483 | short* rows0_old = rows0; |
| 484 | rows0 = rows1; |
| 485 | rows1 = rows0_old; |
| 486 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 487 | |
| 488 | const short* ialphap = ialpha; |
| 489 | short* rows1p = rows1; |
| 490 | for (int dx = 0; dx < w; dx++) |
| 491 | { |
| 492 | sx = xofs[dx]; |
| 493 | |
| 494 | const unsigned char* S1p = S1 + sx; |
| 495 | #if __ARM_NEON |
| 496 | int16x4_t _a0a1XX = vld1_s16(ialphap); |
| 497 | int16x4_t _a0a0a1a1 = vzip_s16(_a0a1XX, _a0a1XX).val[0]; |
| 498 | uint8x8_t _S1 = uint8x8_t(); |
| 499 | |
| 500 | _S1 = vld1_lane_u8(S1p, _S1, 0); |
| 501 | _S1 = vld1_lane_u8(S1p + 1, _S1, 1); |
| 502 | _S1 = vld1_lane_u8(S1p + 2, _S1, 2); |
| 503 | _S1 = vld1_lane_u8(S1p + 3, _S1, 3); |
| 504 | |
| 505 | int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1)); |
| 506 | int16x4_t _S1lowhigh = vget_low_s16(_S116); |
| 507 | int32x4_t _S1ma0a1 = vmull_s16(_S1lowhigh, _a0a0a1a1); |
| 508 | int32x2_t _rows1low = vadd_s32(vget_low_s32(_S1ma0a1), vget_high_s32(_S1ma0a1)); |
| 509 | int32x4_t _rows1 = vcombine_s32(_rows1low, vget_high_s32(_S1ma0a1)); |
| 510 | int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4); |
| 511 | vst1_s16(rows1p, _rows1_sr4); |
| 512 | #else |
| 513 | short a0 = ialphap[0]; |
| 514 | short a1 = ialphap[1]; |
| 515 | |
| 516 | rows1p[0] = (S1p[0] * a0 + S1p[2] * a1) >> 4; |
| 517 | rows1p[1] = (S1p[1] * a0 + S1p[3] * a1) >> 4; |
| 518 | #endif // __ARM_NEON |
| 519 | |
| 520 | ialphap += 2; |
| 521 | rows1p += 2; |
| 522 | } |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | // hresize two rows |
| 527 | const unsigned char* S0 = src + srcstride * (sy); |
| 528 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 529 | |
| 530 | const short* ialphap = ialpha; |
| 531 | short* rows0p = rows0; |
| 532 | short* rows1p = rows1; |
| 533 | for (int dx = 0; dx < w; dx++) |
| 534 | { |
| 535 | sx = xofs[dx]; |
| 536 | short a0 = ialphap[0]; |
| 537 | short a1 = ialphap[1]; |
| 538 | |
| 539 | const unsigned char* S0p = S0 + sx; |
| 540 | const unsigned char* S1p = S1 + sx; |
| 541 | #if __ARM_NEON |
| 542 | int16x4_t _a0 = vdup_n_s16(a0); |
| 543 | int16x4_t _a1 = vdup_n_s16(a1); |
| 544 | uint8x8_t _S0 = uint8x8_t(); |
| 545 | uint8x8_t _S1 = uint8x8_t(); |
| 546 | |
| 547 | _S0 = vld1_lane_u8(S0p, _S0, 0); |
| 548 | _S0 = vld1_lane_u8(S0p + 1, _S0, 1); |
| 549 | _S0 = vld1_lane_u8(S0p + 2, _S0, 2); |
| 550 | _S0 = vld1_lane_u8(S0p + 3, _S0, 3); |
| 551 | |
| 552 | _S1 = vld1_lane_u8(S1p, _S1, 0); |
| 553 | _S1 = vld1_lane_u8(S1p + 1, _S1, 1); |
| 554 | _S1 = vld1_lane_u8(S1p + 2, _S1, 2); |
| 555 | _S1 = vld1_lane_u8(S1p + 3, _S1, 3); |
| 556 | |
| 557 | int16x8_t _S016 = vreinterpretq_s16_u16(vmovl_u8(_S0)); |
| 558 | int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1)); |
| 559 | int16x4_t _S0lowhigh = vget_low_s16(_S016); |
| 560 | int16x4_t _S1lowhigh = vget_low_s16(_S116); |
| 561 | int32x2x2_t _S0S1low_S0S1high = vtrn_s32(vreinterpret_s32_s16(_S0lowhigh), vreinterpret_s32_s16(_S1lowhigh)); |
| 562 | int32x4_t _rows01 = vmull_s16(vreinterpret_s16_s32(_S0S1low_S0S1high.val[0]), _a0); |
| 563 | _rows01 = vmlal_s16(_rows01, vreinterpret_s16_s32(_S0S1low_S0S1high.val[1]), _a1); |
| 564 | int16x4_t _rows01_sr4 = vshrn_n_s32(_rows01, 4); |
| 565 | int16x4_t _rows1_sr4 = vext_s16(_rows01_sr4, _rows01_sr4, 2); |
| 566 | vst1_s16(rows0p, _rows01_sr4); |
| 567 | vst1_s16(rows1p, _rows1_sr4); |
| 568 | #else |
| 569 | rows0p[0] = (S0p[0] * a0 + S0p[2] * a1) >> 4; |
| 570 | rows0p[1] = (S0p[1] * a0 + S0p[3] * a1) >> 4; |
| 571 | rows1p[0] = (S1p[0] * a0 + S1p[2] * a1) >> 4; |
| 572 | rows1p[1] = (S1p[1] * a0 + S1p[3] * a1) >> 4; |
| 573 | #endif // __ARM_NEON |
| 574 | |
| 575 | ialphap += 2; |
| 576 | rows0p += 2; |
| 577 | rows1p += 2; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | prev_sy1 = sy; |
| 582 | |
| 583 | if (dy + 1 < h && yofs[dy + 1] == sy) |
| 584 | { |
| 585 | // vresize for two rows |
| 586 | unsigned char* Dp0 = dst + stride * dy; |
| 587 | unsigned char* Dp1 = dst + stride * (dy + 1); |
| 588 | |
| 589 | vresize_two(rows0, rows1, w * 2, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]); |
| 590 | |
| 591 | ibeta += 4; |
| 592 | dy += 1; |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | // vresize |
| 597 | unsigned char* Dp = dst + stride * dy; |
| 598 | |
| 599 | vresize_one(rows0, rows1, w * 2, Dp, ibeta[0], ibeta[1]); |
| 600 | |
| 601 | ibeta += 2; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | delete[] buf; |
| 606 | } |
| 607 | |
| 608 | void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride) |
| 609 | { |
| 610 | const int INTER_RESIZE_COEF_BITS = 11; |
| 611 | const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS; |
| 612 | |
| 613 | double scale_x = (double)srcw / w; |
| 614 | double scale_y = (double)srch / h; |
| 615 | |
| 616 | int* buf = new int[w + h + w + h]; |
| 617 | |
| 618 | int* xofs = buf; //new int[w]; |
| 619 | int* yofs = buf + w; //new int[h]; |
| 620 | |
| 621 | short* ialpha = (short*)(buf + w + h); //new short[w * 2]; |
| 622 | short* ibeta = (short*)(buf + w + h + w); //new short[h * 2]; |
| 623 | |
| 624 | float fx; |
| 625 | float fy; |
| 626 | int sx; |
| 627 | int sy; |
| 628 | |
| 629 | #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX); |
| 630 | |
| 631 | for (int dx = 0; dx < w; dx++) |
| 632 | { |
| 633 | fx = (float)((dx + 0.5) * scale_x - 0.5); |
| 634 | sx = static_cast<int>(floor(fx)); |
| 635 | fx -= sx; |
| 636 | |
| 637 | if (sx < 0) |
| 638 | { |
| 639 | sx = 0; |
| 640 | fx = 0.f; |
| 641 | } |
| 642 | if (sx >= srcw - 1) |
| 643 | { |
| 644 | sx = srcw - 2; |
| 645 | fx = 1.f; |
| 646 | } |
| 647 | |
| 648 | xofs[dx] = sx * 3; |
| 649 | |
| 650 | float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE; |
| 651 | float a1 = fx * INTER_RESIZE_COEF_SCALE; |
| 652 | |
| 653 | ialpha[dx * 2] = SATURATE_CAST_SHORT(a0); |
| 654 | ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1); |
| 655 | } |
| 656 | |
| 657 | for (int dy = 0; dy < h; dy++) |
| 658 | { |
| 659 | fy = (float)((dy + 0.5) * scale_y - 0.5); |
| 660 | sy = static_cast<int>(floor(fy)); |
| 661 | fy -= sy; |
| 662 | |
| 663 | if (sy < 0) |
| 664 | { |
| 665 | sy = 0; |
| 666 | fy = 0.f; |
| 667 | } |
| 668 | if (sy >= srch - 1) |
| 669 | { |
| 670 | sy = srch - 2; |
| 671 | fy = 1.f; |
| 672 | } |
| 673 | |
| 674 | yofs[dy] = sy; |
| 675 | |
| 676 | float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE; |
| 677 | float b1 = fy * INTER_RESIZE_COEF_SCALE; |
| 678 | |
| 679 | ibeta[dy * 2] = SATURATE_CAST_SHORT(b0); |
| 680 | ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1); |
| 681 | } |
| 682 | |
| 683 | #undef SATURATE_CAST_SHORT |
| 684 | |
| 685 | // loop body |
| 686 | Mat rowsbuf0(w * 3 + 1, (size_t)2u); |
| 687 | Mat rowsbuf1(w * 3 + 1, (size_t)2u); |
| 688 | short* rows0 = (short*)rowsbuf0.data; |
| 689 | short* rows1 = (short*)rowsbuf1.data; |
| 690 | |
| 691 | int prev_sy1 = -2; |
| 692 | |
| 693 | for (int dy = 0; dy < h; dy++) |
| 694 | { |
| 695 | sy = yofs[dy]; |
| 696 | |
| 697 | if (sy == prev_sy1) |
| 698 | { |
| 699 | // reuse all rows |
| 700 | } |
| 701 | else if (sy == prev_sy1 + 1) |
| 702 | { |
| 703 | // hresize one row |
| 704 | short* rows0_old = rows0; |
| 705 | rows0 = rows1; |
| 706 | rows1 = rows0_old; |
| 707 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 708 | |
| 709 | const short* ialphap = ialpha; |
| 710 | short* rows1p = rows1; |
| 711 | for (int dx = 0; dx < w; dx++) |
| 712 | { |
| 713 | sx = xofs[dx]; |
| 714 | short a0 = ialphap[0]; |
| 715 | short a1 = ialphap[1]; |
| 716 | |
| 717 | const unsigned char* S1p = S1 + sx; |
| 718 | #if __ARM_NEON |
| 719 | int16x4_t _a0 = vdup_n_s16(a0); |
| 720 | int16x4_t _a1 = vdup_n_s16(a1); |
| 721 | uint8x8_t _S1 = uint8x8_t(); |
| 722 | |
| 723 | _S1 = vld1_lane_u8(S1p, _S1, 0); |
| 724 | _S1 = vld1_lane_u8(S1p + 1, _S1, 1); |
| 725 | _S1 = vld1_lane_u8(S1p + 2, _S1, 2); |
| 726 | _S1 = vld1_lane_u8(S1p + 3, _S1, 3); |
| 727 | _S1 = vld1_lane_u8(S1p + 4, _S1, 4); |
| 728 | _S1 = vld1_lane_u8(S1p + 5, _S1, 5); |
| 729 | |
| 730 | int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1)); |
| 731 | int16x4_t _S1low = vget_low_s16(_S116); |
| 732 | int16x4_t _S1high = vext_s16(_S1low, vget_high_s16(_S116), 3); |
| 733 | int32x4_t _rows1 = vmull_s16(_S1low, _a0); |
| 734 | _rows1 = vmlal_s16(_rows1, _S1high, _a1); |
| 735 | int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4); |
| 736 | vst1_s16(rows1p, _rows1_sr4); |
| 737 | #else |
| 738 | rows1p[0] = (S1p[0] * a0 + S1p[3] * a1) >> 4; |
| 739 | rows1p[1] = (S1p[1] * a0 + S1p[4] * a1) >> 4; |
| 740 | rows1p[2] = (S1p[2] * a0 + S1p[5] * a1) >> 4; |
| 741 | #endif // __ARM_NEON |
| 742 | |
| 743 | ialphap += 2; |
| 744 | rows1p += 3; |
| 745 | } |
| 746 | } |
| 747 | else |
| 748 | { |
| 749 | // hresize two rows |
| 750 | const unsigned char* S0 = src + srcstride * (sy); |
| 751 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 752 | |
| 753 | const short* ialphap = ialpha; |
| 754 | short* rows0p = rows0; |
| 755 | short* rows1p = rows1; |
| 756 | for (int dx = 0; dx < w; dx++) |
| 757 | { |
| 758 | sx = xofs[dx]; |
| 759 | short a0 = ialphap[0]; |
| 760 | short a1 = ialphap[1]; |
| 761 | |
| 762 | const unsigned char* S0p = S0 + sx; |
| 763 | const unsigned char* S1p = S1 + sx; |
| 764 | #if __ARM_NEON |
| 765 | int16x4_t _a0 = vdup_n_s16(a0); |
| 766 | int16x4_t _a1 = vdup_n_s16(a1); |
| 767 | uint8x8_t _S0 = uint8x8_t(); |
| 768 | uint8x8_t _S1 = uint8x8_t(); |
| 769 | |
| 770 | _S0 = vld1_lane_u8(S0p, _S0, 0); |
| 771 | _S0 = vld1_lane_u8(S0p + 1, _S0, 1); |
| 772 | _S0 = vld1_lane_u8(S0p + 2, _S0, 2); |
| 773 | _S0 = vld1_lane_u8(S0p + 3, _S0, 3); |
| 774 | _S0 = vld1_lane_u8(S0p + 4, _S0, 4); |
| 775 | _S0 = vld1_lane_u8(S0p + 5, _S0, 5); |
| 776 | |
| 777 | _S1 = vld1_lane_u8(S1p, _S1, 0); |
| 778 | _S1 = vld1_lane_u8(S1p + 1, _S1, 1); |
| 779 | _S1 = vld1_lane_u8(S1p + 2, _S1, 2); |
| 780 | _S1 = vld1_lane_u8(S1p + 3, _S1, 3); |
| 781 | _S1 = vld1_lane_u8(S1p + 4, _S1, 4); |
| 782 | _S1 = vld1_lane_u8(S1p + 5, _S1, 5); |
| 783 | |
| 784 | int16x8_t _S016 = vreinterpretq_s16_u16(vmovl_u8(_S0)); |
| 785 | int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1)); |
| 786 | int16x4_t _S0low = vget_low_s16(_S016); |
| 787 | int16x4_t _S1low = vget_low_s16(_S116); |
| 788 | int16x4_t _S0high = vext_s16(_S0low, vget_high_s16(_S016), 3); |
| 789 | int16x4_t _S1high = vext_s16(_S1low, vget_high_s16(_S116), 3); |
| 790 | int32x4_t _rows0 = vmull_s16(_S0low, _a0); |
| 791 | int32x4_t _rows1 = vmull_s16(_S1low, _a0); |
| 792 | _rows0 = vmlal_s16(_rows0, _S0high, _a1); |
| 793 | _rows1 = vmlal_s16(_rows1, _S1high, _a1); |
| 794 | int16x4_t _rows0_sr4 = vshrn_n_s32(_rows0, 4); |
| 795 | int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4); |
| 796 | vst1_s16(rows0p, _rows0_sr4); |
| 797 | vst1_s16(rows1p, _rows1_sr4); |
| 798 | #else |
| 799 | rows0p[0] = (S0p[0] * a0 + S0p[3] * a1) >> 4; |
| 800 | rows0p[1] = (S0p[1] * a0 + S0p[4] * a1) >> 4; |
| 801 | rows0p[2] = (S0p[2] * a0 + S0p[5] * a1) >> 4; |
| 802 | rows1p[0] = (S1p[0] * a0 + S1p[3] * a1) >> 4; |
| 803 | rows1p[1] = (S1p[1] * a0 + S1p[4] * a1) >> 4; |
| 804 | rows1p[2] = (S1p[2] * a0 + S1p[5] * a1) >> 4; |
| 805 | #endif // __ARM_NEON |
| 806 | |
| 807 | ialphap += 2; |
| 808 | rows0p += 3; |
| 809 | rows1p += 3; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | prev_sy1 = sy; |
| 814 | |
| 815 | if (dy + 1 < h && yofs[dy + 1] == sy) |
| 816 | { |
| 817 | // vresize for two rows |
| 818 | unsigned char* Dp0 = dst + stride * dy; |
| 819 | unsigned char* Dp1 = dst + stride * (dy + 1); |
| 820 | |
| 821 | vresize_two(rows0, rows1, w * 3, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]); |
| 822 | |
| 823 | ibeta += 4; |
| 824 | dy += 1; |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | // vresize |
| 829 | unsigned char* Dp = dst + stride * dy; |
| 830 | |
| 831 | vresize_one(rows0, rows1, w * 3, Dp, ibeta[0], ibeta[1]); |
| 832 | |
| 833 | ibeta += 2; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | delete[] buf; |
| 838 | } |
| 839 | |
| 840 | void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride) |
| 841 | { |
| 842 | const int INTER_RESIZE_COEF_BITS = 11; |
| 843 | const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS; |
| 844 | |
| 845 | double scale_x = (double)srcw / w; |
| 846 | double scale_y = (double)srch / h; |
| 847 | |
| 848 | int* buf = new int[w + h + w + h]; |
| 849 | |
| 850 | int* xofs = buf; //new int[w]; |
| 851 | int* yofs = buf + w; //new int[h]; |
| 852 | |
| 853 | short* ialpha = (short*)(buf + w + h); //new short[w * 2]; |
| 854 | short* ibeta = (short*)(buf + w + h + w); //new short[h * 2]; |
| 855 | |
| 856 | float fx; |
| 857 | float fy; |
| 858 | int sx; |
| 859 | int sy; |
| 860 | |
| 861 | #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX); |
| 862 | |
| 863 | for (int dx = 0; dx < w; dx++) |
| 864 | { |
| 865 | fx = (float)((dx + 0.5) * scale_x - 0.5); |
| 866 | sx = static_cast<int>(floor(fx)); |
| 867 | fx -= sx; |
| 868 | |
| 869 | if (sx < 0) |
| 870 | { |
| 871 | sx = 0; |
| 872 | fx = 0.f; |
| 873 | } |
| 874 | if (sx >= srcw - 1) |
| 875 | { |
| 876 | sx = srcw - 2; |
| 877 | fx = 1.f; |
| 878 | } |
| 879 | |
| 880 | xofs[dx] = sx * 4; |
| 881 | |
| 882 | float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE; |
| 883 | float a1 = fx * INTER_RESIZE_COEF_SCALE; |
| 884 | |
| 885 | ialpha[dx * 2] = SATURATE_CAST_SHORT(a0); |
| 886 | ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1); |
| 887 | } |
| 888 | |
| 889 | for (int dy = 0; dy < h; dy++) |
| 890 | { |
| 891 | fy = (float)((dy + 0.5) * scale_y - 0.5); |
| 892 | sy = static_cast<int>(floor(fy)); |
| 893 | fy -= sy; |
| 894 | |
| 895 | if (sy < 0) |
| 896 | { |
| 897 | sy = 0; |
| 898 | fy = 0.f; |
| 899 | } |
| 900 | if (sy >= srch - 1) |
| 901 | { |
| 902 | sy = srch - 2; |
| 903 | fy = 1.f; |
| 904 | } |
| 905 | |
| 906 | yofs[dy] = sy; |
| 907 | |
| 908 | float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE; |
| 909 | float b1 = fy * INTER_RESIZE_COEF_SCALE; |
| 910 | |
| 911 | ibeta[dy * 2] = SATURATE_CAST_SHORT(b0); |
| 912 | ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1); |
| 913 | } |
| 914 | |
| 915 | #undef SATURATE_CAST_SHORT |
| 916 | |
| 917 | // loop body |
| 918 | Mat rowsbuf0(w * 4, (size_t)2u); |
| 919 | Mat rowsbuf1(w * 4, (size_t)2u); |
| 920 | short* rows0 = (short*)rowsbuf0.data; |
| 921 | short* rows1 = (short*)rowsbuf1.data; |
| 922 | |
| 923 | int prev_sy1 = -2; |
| 924 | |
| 925 | for (int dy = 0; dy < h; dy++) |
| 926 | { |
| 927 | sy = yofs[dy]; |
| 928 | |
| 929 | if (sy == prev_sy1) |
| 930 | { |
| 931 | // reuse all rows |
| 932 | } |
| 933 | else if (sy == prev_sy1 + 1) |
| 934 | { |
| 935 | // hresize one row |
| 936 | short* rows0_old = rows0; |
| 937 | rows0 = rows1; |
| 938 | rows1 = rows0_old; |
| 939 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 940 | |
| 941 | const short* ialphap = ialpha; |
| 942 | short* rows1p = rows1; |
| 943 | for (int dx = 0; dx < w; dx++) |
| 944 | { |
| 945 | sx = xofs[dx]; |
| 946 | short a0 = ialphap[0]; |
| 947 | short a1 = ialphap[1]; |
| 948 | |
| 949 | const unsigned char* S1p = S1 + sx; |
| 950 | #if __ARM_NEON |
| 951 | int16x4_t _a0 = vdup_n_s16(a0); |
| 952 | int16x4_t _a1 = vdup_n_s16(a1); |
| 953 | uint8x8_t _S1 = vld1_u8(S1p); |
| 954 | int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1)); |
| 955 | int16x4_t _S1low = vget_low_s16(_S116); |
| 956 | int16x4_t _S1high = vget_high_s16(_S116); |
| 957 | int32x4_t _rows1 = vmull_s16(_S1low, _a0); |
| 958 | _rows1 = vmlal_s16(_rows1, _S1high, _a1); |
| 959 | int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4); |
| 960 | vst1_s16(rows1p, _rows1_sr4); |
| 961 | #else |
| 962 | rows1p[0] = (S1p[0] * a0 + S1p[4] * a1) >> 4; |
| 963 | rows1p[1] = (S1p[1] * a0 + S1p[5] * a1) >> 4; |
| 964 | rows1p[2] = (S1p[2] * a0 + S1p[6] * a1) >> 4; |
| 965 | rows1p[3] = (S1p[3] * a0 + S1p[7] * a1) >> 4; |
| 966 | #endif // __ARM_NEON |
| 967 | |
| 968 | ialphap += 2; |
| 969 | rows1p += 4; |
| 970 | } |
| 971 | } |
| 972 | else |
| 973 | { |
| 974 | // hresize two rows |
| 975 | const unsigned char* S0 = src + srcstride * (sy); |
| 976 | const unsigned char* S1 = src + srcstride * (sy + 1); |
| 977 | |
| 978 | const short* ialphap = ialpha; |
| 979 | short* rows0p = rows0; |
| 980 | short* rows1p = rows1; |
| 981 | for (int dx = 0; dx < w; dx++) |
| 982 | { |
| 983 | sx = xofs[dx]; |
| 984 | short a0 = ialphap[0]; |
| 985 | short a1 = ialphap[1]; |
| 986 | |
| 987 | const unsigned char* S0p = S0 + sx; |
| 988 | const unsigned char* S1p = S1 + sx; |
| 989 | #if __ARM_NEON |
| 990 | int16x4_t _a0 = vdup_n_s16(a0); |
| 991 | int16x4_t _a1 = vdup_n_s16(a1); |
| 992 | uint8x8_t _S0 = vld1_u8(S0p); |
| 993 | uint8x8_t _S1 = vld1_u8(S1p); |
| 994 | int16x8_t _S016 = vreinterpretq_s16_u16(vmovl_u8(_S0)); |
| 995 | int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1)); |
| 996 | int16x4_t _S0low = vget_low_s16(_S016); |
| 997 | int16x4_t _S1low = vget_low_s16(_S116); |
| 998 | int16x4_t _S0high = vget_high_s16(_S016); |
| 999 | int16x4_t _S1high = vget_high_s16(_S116); |
| 1000 | int32x4_t _rows0 = vmull_s16(_S0low, _a0); |
| 1001 | int32x4_t _rows1 = vmull_s16(_S1low, _a0); |
| 1002 | _rows0 = vmlal_s16(_rows0, _S0high, _a1); |
| 1003 | _rows1 = vmlal_s16(_rows1, _S1high, _a1); |
| 1004 | int16x4_t _rows0_sr4 = vshrn_n_s32(_rows0, 4); |
| 1005 | int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4); |
| 1006 | vst1_s16(rows0p, _rows0_sr4); |
| 1007 | vst1_s16(rows1p, _rows1_sr4); |
| 1008 | #else |
| 1009 | rows0p[0] = (S0p[0] * a0 + S0p[4] * a1) >> 4; |
| 1010 | rows0p[1] = (S0p[1] * a0 + S0p[5] * a1) >> 4; |
| 1011 | rows0p[2] = (S0p[2] * a0 + S0p[6] * a1) >> 4; |
| 1012 | rows0p[3] = (S0p[3] * a0 + S0p[7] * a1) >> 4; |
| 1013 | rows1p[0] = (S1p[0] * a0 + S1p[4] * a1) >> 4; |
| 1014 | rows1p[1] = (S1p[1] * a0 + S1p[5] * a1) >> 4; |
| 1015 | rows1p[2] = (S1p[2] * a0 + S1p[6] * a1) >> 4; |
| 1016 | rows1p[3] = (S1p[3] * a0 + S1p[7] * a1) >> 4; |
| 1017 | #endif // __ARM_NEON |
| 1018 | |
| 1019 | ialphap += 2; |
| 1020 | rows0p += 4; |
| 1021 | rows1p += 4; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | prev_sy1 = sy; |
| 1026 | |
| 1027 | if (dy + 1 < h && yofs[dy + 1] == sy) |
| 1028 | { |
| 1029 | // vresize for two rows |
| 1030 | unsigned char* Dp0 = dst + stride * dy; |
| 1031 | unsigned char* Dp1 = dst + stride * (dy + 1); |
| 1032 | |
| 1033 | vresize_two(rows0, rows1, w * 4, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]); |
| 1034 | |
| 1035 | ibeta += 4; |
| 1036 | dy += 1; |
| 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | // vresize |
| 1041 | unsigned char* Dp = dst + stride * dy; |
| 1042 | |
| 1043 | vresize_one(rows0, rows1, w * 4, Dp, ibeta[0], ibeta[1]); |
| 1044 | |
| 1045 | ibeta += 2; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | delete[] buf; |
| 1050 | } |
| 1051 | |
| 1052 | void resize_bilinear_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h) |
| 1053 | { |
| 1054 | // assert srcw % 2 == 0 |
| 1055 | // assert srch % 2 == 0 |
| 1056 | // assert w % 2 == 0 |
| 1057 | // assert h % 2 == 0 |
| 1058 | |
| 1059 | const unsigned char* srcY = src; |
| 1060 | unsigned char* dstY = dst; |
| 1061 | resize_bilinear_c1(srcY, srcw, srch, dstY, w, h); |
| 1062 | |
| 1063 | const unsigned char* srcUV = src + srcw * srch; |
| 1064 | unsigned char* dstUV = dst + w * h; |
| 1065 | resize_bilinear_c2(srcUV, srcw / 2, srch / 2, dstUV, w / 2, h / 2); |
| 1066 | } |
| 1067 | #endif // NCNN_PIXEL |
| 1068 | |
| 1069 | } // namespace ncnn |