| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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 "interp_mips.h" |
| 16 | |
| 17 | #if __mips_msa |
| 18 | #include <msa.h> |
| 19 | #endif // __mips_msa |
| 20 | |
| 21 | #include "mips_usability.h" |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | #include "interp_bicubic.h" |
| 26 | #include "interp_bilinear.h" |
| 27 | |
| 28 | #if __mips_msa |
| 29 | #include "interp_bicubic_pack4.h" |
| 30 | #include "interp_bilinear_pack4.h" |
| 31 | #endif |
| 32 | |
| 33 | Interp_mips::Interp_mips() |
| 34 | { |
| 35 | #if __mips_msa |
| 36 | support_packing = true; |
| 37 | #endif // __mips_msa |
| 38 | } |
| 39 | |
| 40 | int Interp_mips::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 41 | { |
| 42 | const Mat& bottom_blob = bottom_blobs[0]; |
| 43 | const Mat& reference_blob = bottom_blobs[1]; |
| 44 | Mat& top_blob = top_blobs[0]; |
| 45 | |
| 46 | int h = bottom_blob.h; |
| 47 | int w = bottom_blob.w; |
| 48 | int channels = bottom_blob.c; |
| 49 | int dims = bottom_blob.dims; |
| 50 | size_t elemsize = bottom_blob.elemsize; |
| 51 | int elempack = bottom_blob.elempack; |
| 52 | |
| 53 | int outw = reference_blob.w; |
| 54 | int outh = reference_blob.h; |
| 55 | |
| 56 | if (dims == 1) |
| 57 | { |
| 58 | top_blob.create(outw, outh, w, elemsize, elempack, opt.blob_allocator); |
| 59 | if (top_blob.empty()) |
| 60 | return -100; |
| 61 | |
| 62 | #if __mips_msa |
| 63 | if (elempack == 4) |
| 64 | { |
| 65 | #pragma omp parallel for num_threads(opt.num_threads) |
| 66 | for (int q = 0; q < w; q++) |
| 67 | { |
| 68 | Mat top_blob_c = top_blob.channel(q); |
| 69 | v4f32 _v = (v4f32)__msa_ld_w((const float*)bottom_blob + q * 4, 0); |
| 70 | top_blob_c.fill(_v); |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | #endif // __mips_msa |
| 76 | |
| 77 | #pragma omp parallel for num_threads(opt.num_threads) |
| 78 | for (int q = 0; q < w; q++) |
| 79 | { |
| 80 | Mat top_blob_c = top_blob.channel(q); |
| 81 | const float v = bottom_blob[q]; |
| 82 | top_blob_c.fill(v); |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | if (dims == 2) |
| 89 | { |
| 90 | if (outw == w) |
| 91 | { |
| 92 | top_blob = bottom_blob; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | top_blob.create(outw, h, elemsize, elempack, opt.blob_allocator); |
| 97 | if (top_blob.empty()) |
| 98 | return -100; |
| 99 | |
| 100 | #if __mips_msa |
| 101 | if (elempack == 4) |
| 102 | { |
| 103 | if (resize_type == 1) // nearest |
| 104 | { |
| 105 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 106 | |
| 107 | #pragma omp parallel for num_threads(opt.num_threads) |
| 108 | for (int y = 0; y < h; y++) |
| 109 | { |
| 110 | const float* ptr = bottom_blob.row(y); |
| 111 | float* outptr = top_blob.row(y); |
| 112 | for (int x = 0; x < outw; x++) |
| 113 | { |
| 114 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 115 | |
| 116 | v4f32 _p = (v4f32)__msa_ld_w(ptr + in_x * 4, 0); |
| 117 | __msa_st_w((v4i32)_p, outptr, 0); |
| 118 | |
| 119 | outptr += 4; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (resize_type == 2) // bilinear |
| 125 | { |
| 126 | int* buf = new int[outw + outw * 2]; |
| 127 | |
| 128 | int* xofs = buf; |
| 129 | float* alpha = (float*)(buf + outw); |
| 130 | |
| 131 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 132 | |
| 133 | #pragma omp parallel for num_threads(opt.num_threads) |
| 134 | for (int y = 0; y < h; y++) |
| 135 | { |
| 136 | const float* ptr = bottom_blob.row(y); |
| 137 | float* outptr = top_blob.row(y); |
| 138 | const float* alphap = alpha; |
| 139 | |
| 140 | for (int x = 0; x < outw; x++) |
| 141 | { |
| 142 | int sx = xofs[x] * 4; |
| 143 | const float* Sp = ptr + sx; |
| 144 | |
| 145 | v4f32 _a0 = __msa_fill_w_f32(alphap[0]); |
| 146 | v4f32 _a1 = __msa_fill_w_f32(alphap[1]); |
| 147 | |
| 148 | v4f32 _S0 = (v4f32)__msa_ld_w(Sp, 0); |
| 149 | v4f32 _S1 = (v4f32)__msa_ld_w(Sp + 4, 0); |
| 150 | v4f32 _p = __msa_fmul_w(_S0, _a0); |
| 151 | _p = __msa_fmadd_w(_p, _S1, _a1); |
| 152 | __msa_st_w((v4i32)_p, outptr, 0); |
| 153 | |
| 154 | alphap += 2; |
| 155 | outptr += 4; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | delete[] buf; |
| 160 | } |
| 161 | |
| 162 | if (resize_type == 3) // bicubic |
| 163 | { |
| 164 | int* buf = new int[outw + outw * 4]; |
| 165 | |
| 166 | int* xofs = buf; |
| 167 | float* alpha = (float*)(buf + outw); |
| 168 | |
| 169 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 170 | |
| 171 | #pragma omp parallel for num_threads(opt.num_threads) |
| 172 | for (int y = 0; y < h; y++) |
| 173 | { |
| 174 | const float* ptr = bottom_blob.row(y); |
| 175 | float* outptr = top_blob.row(y); |
| 176 | const float* alphap = alpha; |
| 177 | |
| 178 | for (int x = 0; x < outw; x++) |
| 179 | { |
| 180 | int sx = xofs[x] * 4; |
| 181 | const float* Sp = ptr + sx; |
| 182 | |
| 183 | v4f32 _a0 = __msa_fill_w_f32(alphap[0]); |
| 184 | v4f32 _a1 = __msa_fill_w_f32(alphap[1]); |
| 185 | v4f32 _a2 = __msa_fill_w_f32(alphap[2]); |
| 186 | v4f32 _a3 = __msa_fill_w_f32(alphap[3]); |
| 187 | |
| 188 | v4f32 _S0 = (v4f32)__msa_ld_w(Sp - 4, 0); |
| 189 | v4f32 _S1 = (v4f32)__msa_ld_w(Sp + 0, 0); |
| 190 | v4f32 _S2 = (v4f32)__msa_ld_w(Sp + 4, 0); |
| 191 | v4f32 _S3 = (v4f32)__msa_ld_w(Sp + 8, 0); |
| 192 | v4f32 _p = __msa_fmul_w(_S0, _a0); |
| 193 | _p = __msa_fmadd_w(_p, _S1, _a1); |
| 194 | _p = __msa_fmadd_w(_p, _S2, _a2); |
| 195 | _p = __msa_fmadd_w(_p, _S3, _a3); |
| 196 | __msa_st_w((v4i32)_p, outptr, 0); |
| 197 | |
| 198 | alphap += 4; |
| 199 | outptr += 4; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | delete[] buf; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | #endif // __mips_msa |
| 209 | |
| 210 | if (resize_type == 1) // nearest |
| 211 | { |
| 212 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 213 | |
| 214 | #pragma omp parallel for num_threads(opt.num_threads) |
| 215 | for (int y = 0; y < h; y++) |
| 216 | { |
| 217 | const float* ptr = bottom_blob.row(y); |
| 218 | float* outptr = top_blob.row(y); |
| 219 | for (int x = 0; x < outw; x++) |
| 220 | { |
| 221 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 222 | *outptr++ = ptr[in_x]; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (resize_type == 2) // bilinear |
| 228 | { |
| 229 | int* buf = new int[outw + outw * 2]; |
| 230 | |
| 231 | int* xofs = buf; |
| 232 | float* alpha = (float*)(buf + outw); |
| 233 | |
| 234 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 235 | |
| 236 | #pragma omp parallel for num_threads(opt.num_threads) |
| 237 | for (int y = 0; y < h; y++) |
| 238 | { |
| 239 | const float* ptr = bottom_blob.row(y); |
| 240 | float* outptr = top_blob.row(y); |
| 241 | const float* alphap = alpha; |
| 242 | |
| 243 | for (int x = 0; x < outw; x++) |
| 244 | { |
| 245 | int sx = xofs[x]; |
| 246 | const float* Sp = ptr + sx; |
| 247 | float a0 = alphap[0]; |
| 248 | float a1 = alphap[1]; |
| 249 | *outptr++ = Sp[0] * a0 + Sp[1] * a1; |
| 250 | alphap += 2; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | delete[] buf; |
| 255 | } |
| 256 | |
| 257 | if (resize_type == 3) // bicubic |
| 258 | { |
| 259 | int* buf = new int[outw + outw * 4]; |
| 260 | |
| 261 | int* xofs = buf; |
| 262 | float* alpha = (float*)(buf + outw); |
| 263 | |
| 264 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 265 | |
| 266 | #pragma omp parallel for num_threads(opt.num_threads) |
| 267 | for (int y = 0; y < h; y++) |
| 268 | { |
| 269 | const float* ptr = bottom_blob.row(y); |
| 270 | float* outptr = top_blob.row(y); |
| 271 | const float* alphap = alpha; |
| 272 | |
| 273 | for (int x = 0; x < outw; x++) |
| 274 | { |
| 275 | int sx = xofs[x]; |
| 276 | const float* Sp = ptr + sx; |
| 277 | float a0 = alphap[0]; |
| 278 | float a1 = alphap[1]; |
| 279 | float a2 = alphap[2]; |
| 280 | float a3 = alphap[3]; |
| 281 | *outptr++ = Sp[-1] * a0 + Sp[0] * a1 + Sp[1] * a2 + Sp[2] * a3; |
| 282 | alphap += 4; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | delete[] buf; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | if (outw == w && outh == h) |
| 293 | { |
| 294 | top_blob = bottom_blob; |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator); |
| 299 | if (top_blob.empty()) |
| 300 | return -100; |
| 301 | |
| 302 | #if __mips_msa |
| 303 | if (elempack == 4) |
| 304 | { |
| 305 | if (resize_type == 1) // nearest |
| 306 | { |
| 307 | const float hs = output_height ? h / (float)outh : 1.f / height_scale; |
| 308 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 309 | |
| 310 | #pragma omp parallel for num_threads(opt.num_threads) |
| 311 | for (int q = 0; q < channels; q++) |
| 312 | { |
| 313 | const Mat src = bottom_blob.channel(q); |
| 314 | Mat dst = top_blob.channel(q); |
| 315 | |
| 316 | for (int y = 0; y < outh; y++) |
| 317 | { |
| 318 | int in_y = std::min((int)(y * hs), (h - 1)); |
| 319 | |
| 320 | const float* ptr = src.row(in_y); |
| 321 | float* outptr = dst.row(y); |
| 322 | for (int x = 0; x < outw; x++) |
| 323 | { |
| 324 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 325 | |
| 326 | v4f32 _p = (v4f32)__msa_ld_w(ptr + in_x * 4, 0); |
| 327 | __msa_st_w((v4i32)_p, outptr, 0); |
| 328 | |
| 329 | outptr += 4; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (resize_type == 2) // bilinear |
| 336 | { |
| 337 | int* buf = new int[outw + outh + outw * 2 + outh * 2]; |
| 338 | |
| 339 | int* xofs = buf; //new int[outw]; |
| 340 | int* yofs = buf + outw; //new int[outh]; |
| 341 | |
| 342 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 2]; |
| 343 | float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2]; |
| 344 | |
| 345 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 346 | linear_coeffs(h, outh, yofs, beta, align_corner); |
| 347 | |
| 348 | #pragma omp parallel for num_threads(opt.num_threads) |
| 349 | for (int q = 0; q < channels; q++) |
| 350 | { |
| 351 | const Mat src = bottom_blob.channel(q); |
| 352 | Mat dst = top_blob.channel(q); |
| 353 | |
| 354 | resize_bilinear_image_pack4(src, dst, alpha, xofs, beta, yofs); |
| 355 | } |
| 356 | |
| 357 | delete[] buf; |
| 358 | } |
| 359 | |
| 360 | if (resize_type == 3) // bicubic |
| 361 | { |
| 362 | int* buf = new int[outw + outh + outw * 4 + outh * 4]; |
| 363 | |
| 364 | int* xofs = buf; //new int[outw]; |
| 365 | int* yofs = buf + outw; //new int[outh]; |
| 366 | |
| 367 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 4]; |
| 368 | float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4]; |
| 369 | |
| 370 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 371 | cubic_coeffs(h, outh, yofs, beta, align_corner); |
| 372 | |
| 373 | #pragma omp parallel for num_threads(opt.num_threads) |
| 374 | for (int q = 0; q < channels; q++) |
| 375 | { |
| 376 | const Mat src = bottom_blob.channel(q); |
| 377 | Mat dst = top_blob.channel(q); |
| 378 | |
| 379 | resize_bicubic_image_pack4(src, dst, alpha, xofs, beta, yofs); |
| 380 | } |
| 381 | |
| 382 | delete[] buf; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | #endif // __mips_msa |
| 388 | |
| 389 | if (resize_type == 1) // nearest |
| 390 | { |
| 391 | const float hs = output_height ? h / (float)outh : 1.f / height_scale; |
| 392 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 393 | |
| 394 | #pragma omp parallel for num_threads(opt.num_threads) |
| 395 | for (int q = 0; q < channels; q++) |
| 396 | { |
| 397 | const Mat src = bottom_blob.channel(q); |
| 398 | Mat dst = top_blob.channel(q); |
| 399 | |
| 400 | for (int y = 0; y < outh; y++) |
| 401 | { |
| 402 | int in_y = std::min((int)(y * hs), (h - 1)); |
| 403 | |
| 404 | const float* ptr = src.row(in_y); |
| 405 | float* outptr = dst.row(y); |
| 406 | for (int x = 0; x < outw; x++) |
| 407 | { |
| 408 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 409 | *outptr++ = ptr[in_x]; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (resize_type == 2) // bilinear |
| 416 | { |
| 417 | int* buf = new int[outw + outh + outw * 2 + outh * 2]; |
| 418 | |
| 419 | int* xofs = buf; //new int[outw]; |
| 420 | int* yofs = buf + outw; //new int[outh]; |
| 421 | |
| 422 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 2]; |
| 423 | float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2]; |
| 424 | |
| 425 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 426 | linear_coeffs(h, outh, yofs, beta, align_corner); |
| 427 | |
| 428 | #pragma omp parallel for num_threads(opt.num_threads) |
| 429 | for (int q = 0; q < channels; q++) |
| 430 | { |
| 431 | const Mat src = bottom_blob.channel(q); |
| 432 | Mat dst = top_blob.channel(q); |
| 433 | |
| 434 | resize_bilinear_image(src, dst, alpha, xofs, beta, yofs); |
| 435 | } |
| 436 | |
| 437 | delete[] buf; |
| 438 | } |
| 439 | |
| 440 | if (resize_type == 3) // bicubic |
| 441 | { |
| 442 | int* buf = new int[outw + outh + outw * 4 + outh * 4]; |
| 443 | |
| 444 | int* xofs = buf; //new int[outw]; |
| 445 | int* yofs = buf + outw; //new int[outh]; |
| 446 | |
| 447 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 4]; |
| 448 | float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4]; |
| 449 | |
| 450 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 451 | cubic_coeffs(h, outh, yofs, beta, align_corner); |
| 452 | |
| 453 | #pragma omp parallel for num_threads(opt.num_threads) |
| 454 | for (int q = 0; q < channels; q++) |
| 455 | { |
| 456 | const Mat src = bottom_blob.channel(q); |
| 457 | Mat dst = top_blob.channel(q); |
| 458 | |
| 459 | resize_bicubic_image(src, dst, alpha, xofs, beta, yofs); |
| 460 | } |
| 461 | |
| 462 | delete[] buf; |
| 463 | } |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | } // namespace ncnn |