|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058 |
- // Copyright 2018 Tencent
- // SPDX-License-Identifier: BSD-3-Clause
-
- #include "mat.h"
-
- #include <limits.h>
-
- #if __ARM_NEON
- #include <arm_neon.h>
- #endif // __ARM_NEON
- #include "platform.h"
-
- namespace ncnn {
-
- #if NCNN_PIXEL
- 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)
- {
- int dx = 0;
- #if __ARM_NEON
- int16x8_t _b0 = vdupq_n_s16(b0);
- int16x8_t _b1 = vdupq_n_s16(b1);
- int16x8_t _b2 = vdupq_n_s16(b2);
- int16x8_t _b3 = vdupq_n_s16(b3);
- for (; dx + 15 < wsize; dx += 16)
- {
- int16x8_t _r00 = vld1q_s16(rows0p);
- int16x8_t _r01 = vld1q_s16(rows0p + 8);
- int16x8_t _r10 = vld1q_s16(rows1p);
- int16x8_t _r11 = vld1q_s16(rows1p + 8);
- int16x8_t _acc00 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r00, _b0), 1), vqdmulhq_s16(_r10, _b1), 1);
- int16x8_t _acc01 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r01, _b0), 1), vqdmulhq_s16(_r11, _b1), 1);
- int16x8_t _acc10 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r00, _b2), 1), vqdmulhq_s16(_r10, _b3), 1);
- int16x8_t _acc11 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r01, _b2), 1), vqdmulhq_s16(_r11, _b3), 1);
- uint8x16_t _Dp0 = vcombine_u8(vqrshrun_n_s16(_acc00, 2), vqrshrun_n_s16(_acc01, 2));
- uint8x16_t _Dp1 = vcombine_u8(vqrshrun_n_s16(_acc10, 2), vqrshrun_n_s16(_acc11, 2));
- vst1q_u8(Dp0, _Dp0);
- vst1q_u8(Dp1, _Dp1);
- Dp0 += 16;
- Dp1 += 16;
- rows0p += 16;
- rows1p += 16;
- }
- for (; dx + 7 < wsize; dx += 8)
- {
- int16x8_t _r0 = vld1q_s16(rows0p);
- int16x8_t _r1 = vld1q_s16(rows1p);
- int16x8_t _acc0 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r0, _b0), 1), vqdmulhq_s16(_r1, _b1), 1);
- int16x8_t _acc1 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r0, _b2), 1), vqdmulhq_s16(_r1, _b3), 1);
- uint8x8_t _Dp0 = vqrshrun_n_s16(_acc0, 2);
- uint8x8_t _Dp1 = vqrshrun_n_s16(_acc1, 2);
- vst1_u8(Dp0, _Dp0);
- vst1_u8(Dp1, _Dp1);
- Dp0 += 8;
- Dp1 += 8;
- rows0p += 8;
- rows1p += 8;
- }
- #endif // __ARM_NEON
- #if __SSE2__
- __m128i _b0 = _mm_set1_epi16(b0);
- __m128i _b1 = _mm_set1_epi16(b1);
- __m128i _b2 = _mm_set1_epi16(b2);
- __m128i _b3 = _mm_set1_epi16(b3);
- __m128i _v2 = _mm_set1_epi16(2);
- for (; dx + 15 < wsize; dx += 16)
- {
- __m128i _r00 = _mm_loadu_si128((const __m128i*)rows0p);
- __m128i _r01 = _mm_loadu_si128((const __m128i*)(rows0p + 8));
- __m128i _r10 = _mm_loadu_si128((const __m128i*)rows1p);
- __m128i _r11 = _mm_loadu_si128((const __m128i*)(rows1p + 8));
- __m128i _acc00 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b0), _mm_mulhi_epi16(_r10, _b1));
- __m128i _acc01 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b0), _mm_mulhi_epi16(_r11, _b1));
- __m128i _acc10 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b2), _mm_mulhi_epi16(_r10, _b3));
- __m128i _acc11 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b2), _mm_mulhi_epi16(_r11, _b3));
- _acc00 = _mm_srai_epi16(_mm_add_epi16(_acc00, _v2), 2);
- _acc01 = _mm_srai_epi16(_mm_add_epi16(_acc01, _v2), 2);
- _acc10 = _mm_srai_epi16(_mm_add_epi16(_acc10, _v2), 2);
- _acc11 = _mm_srai_epi16(_mm_add_epi16(_acc11, _v2), 2);
- __m128i _Dp0 = _mm_packus_epi16(_acc00, _acc01);
- __m128i _Dp1 = _mm_packus_epi16(_acc10, _acc11);
- _mm_storeu_si128((__m128i*)Dp0, _Dp0);
- _mm_storeu_si128((__m128i*)Dp1, _Dp1);
- Dp0 += 16;
- Dp1 += 16;
- rows0p += 16;
- rows1p += 16;
- }
- for (; dx + 7 < wsize; dx += 8)
- {
- __m128i _r0 = _mm_loadu_si128((const __m128i*)rows0p);
- __m128i _r1 = _mm_loadu_si128((const __m128i*)rows1p);
- __m128i _acc0 = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b0), _mm_mulhi_epi16(_r1, _b1));
- __m128i _acc1 = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b2), _mm_mulhi_epi16(_r1, _b3));
- _acc0 = _mm_srai_epi16(_mm_add_epi16(_acc0, _v2), 2);
- _acc1 = _mm_srai_epi16(_mm_add_epi16(_acc1, _v2), 2);
- __m128i _Dp0 = _mm_packus_epi16(_acc0, _acc0);
- __m128i _Dp1 = _mm_packus_epi16(_acc1, _acc1);
- _mm_storel_epi64((__m128i*)Dp0, _Dp0);
- _mm_storel_epi64((__m128i*)Dp1, _Dp1);
- Dp0 += 8;
- Dp1 += 8;
- rows0p += 8;
- rows1p += 8;
- }
- #endif // __SSE2__
- for (; dx < wsize; dx++)
- {
- short s0 = *rows0p++;
- short s1 = *rows1p++;
-
- *Dp0++ = (unsigned char)(((short)((b0 * s0) >> 16) + (short)((b1 * s1) >> 16) + 2) >> 2);
- *Dp1++ = (unsigned char)(((short)((b2 * s0) >> 16) + (short)((b3 * s1) >> 16) + 2) >> 2);
- }
- }
-
- static void vresize_one(const short* rows0p, const short* rows1p, int wsize, unsigned char* Dp, short b0, short b1)
- {
- int dx = 0;
- #if __ARM_NEON
- int16x8_t _b0 = vdupq_n_s16(b0);
- int16x8_t _b1 = vdupq_n_s16(b1);
- for (; dx + 15 < wsize; dx += 16)
- {
- int16x8_t _r00 = vld1q_s16(rows0p);
- int16x8_t _r01 = vld1q_s16(rows0p + 8);
- int16x8_t _r10 = vld1q_s16(rows1p);
- int16x8_t _r11 = vld1q_s16(rows1p + 8);
- int16x8_t _acc0 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r00, _b0), 1), vqdmulhq_s16(_r10, _b1), 1);
- int16x8_t _acc1 = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r01, _b0), 1), vqdmulhq_s16(_r11, _b1), 1);
- uint8x16_t _Dp = vcombine_u8(vqrshrun_n_s16(_acc0, 2), vqrshrun_n_s16(_acc1, 2));
- vst1q_u8(Dp, _Dp);
- Dp += 16;
- rows0p += 16;
- rows1p += 16;
- }
- for (; dx + 7 < wsize; dx += 8)
- {
- int16x8_t _r0 = vld1q_s16(rows0p);
- int16x8_t _r1 = vld1q_s16(rows1p);
- int16x8_t _acc = vsraq_n_s16(vshrq_n_s16(vqdmulhq_s16(_r0, _b0), 1), vqdmulhq_s16(_r1, _b1), 1);
- uint8x8_t _Dp = vqrshrun_n_s16(_acc, 2);
- vst1_u8(Dp, _Dp);
- Dp += 8;
- rows0p += 8;
- rows1p += 8;
- }
- #endif // __ARM_NEON
- #if __SSE2__
- __m128i _b0 = _mm_set1_epi16(b0);
- __m128i _b1 = _mm_set1_epi16(b1);
- __m128i _v2 = _mm_set1_epi16(2);
- for (; dx + 15 < wsize; dx += 16)
- {
- __m128i _r00 = _mm_loadu_si128((const __m128i*)rows0p);
- __m128i _r01 = _mm_loadu_si128((const __m128i*)(rows0p + 8));
- __m128i _r10 = _mm_loadu_si128((const __m128i*)rows1p);
- __m128i _r11 = _mm_loadu_si128((const __m128i*)(rows1p + 8));
- __m128i _acc0 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b0), _mm_mulhi_epi16(_r10, _b1));
- __m128i _acc1 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b0), _mm_mulhi_epi16(_r11, _b1));
- _acc0 = _mm_srai_epi16(_mm_add_epi16(_acc0, _v2), 2);
- _acc1 = _mm_srai_epi16(_mm_add_epi16(_acc1, _v2), 2);
- __m128i _Dp = _mm_packus_epi16(_acc0, _acc1);
- _mm_storeu_si128((__m128i*)Dp, _Dp);
- Dp += 16;
- rows0p += 16;
- rows1p += 16;
- }
- for (; dx + 7 < wsize; dx += 8)
- {
- __m128i _r0 = _mm_loadu_si128((const __m128i*)rows0p);
- __m128i _r1 = _mm_loadu_si128((const __m128i*)rows1p);
- __m128i _acc = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b0), _mm_mulhi_epi16(_r1, _b1));
- _acc = _mm_srai_epi16(_mm_add_epi16(_acc, _v2), 2);
- __m128i _Dp = _mm_packus_epi16(_acc, _acc);
- _mm_storel_epi64((__m128i*)Dp, _Dp);
- Dp += 8;
- rows0p += 8;
- rows1p += 8;
- }
- #endif // __SSE2__
- for (; dx < wsize; dx++)
- {
- short s0 = *rows0p++;
- short s1 = *rows1p++;
-
- *Dp++ = (unsigned char)(((short)((b0 * s0) >> 16) + (short)((b1 * s1) >> 16) + 2) >> 2);
- }
- }
-
- void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h)
- {
- return resize_bilinear_c1(src, srcw, srch, srcw, dst, w, h, w);
- }
-
- void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h)
- {
- return resize_bilinear_c2(src, srcw, srch, srcw * 2, dst, w, h, w * 2);
- }
-
- void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h)
- {
- return resize_bilinear_c3(src, srcw, srch, srcw * 3, dst, w, h, w * 3);
- }
-
- void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h)
- {
- return resize_bilinear_c4(src, srcw, srch, srcw * 4, dst, w, h, w * 4);
- }
-
- void resize_bilinear_c1(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride)
- {
- const int INTER_RESIZE_COEF_BITS = 11;
- const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS;
-
- double scale_x = (double)srcw / w;
- double scale_y = (double)srch / h;
-
- int* buf = new int[w + h + w + h];
-
- int* xofs = buf; //new int[w];
- int* yofs = buf + w; //new int[h];
-
- short* ialpha = (short*)(buf + w + h); //new short[w * 2];
- short* ibeta = (short*)(buf + w + h + w); //new short[h * 2];
-
- float fx;
- float fy;
- int sx;
- int sy;
-
- #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX);
-
- for (int dx = 0; dx < w; dx++)
- {
- fx = (float)((dx + 0.5) * scale_x - 0.5);
- sx = static_cast<int>(floor(fx));
- fx -= sx;
-
- if (sx < 0)
- {
- sx = 0;
- fx = 0.f;
- }
- if (sx >= srcw - 1)
- {
- sx = srcw - 2;
- fx = 1.f;
- }
-
- xofs[dx] = sx;
-
- float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE;
- float a1 = fx * INTER_RESIZE_COEF_SCALE;
-
- ialpha[dx * 2] = SATURATE_CAST_SHORT(a0);
- ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1);
- }
-
- for (int dy = 0; dy < h; dy++)
- {
- fy = (float)((dy + 0.5) * scale_y - 0.5);
- sy = static_cast<int>(floor(fy));
- fy -= sy;
-
- if (sy < 0)
- {
- sy = 0;
- fy = 0.f;
- }
- if (sy >= srch - 1)
- {
- sy = srch - 2;
- fy = 1.f;
- }
-
- yofs[dy] = sy;
-
- float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE;
- float b1 = fy * INTER_RESIZE_COEF_SCALE;
-
- ibeta[dy * 2] = SATURATE_CAST_SHORT(b0);
- ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1);
- }
-
- #undef SATURATE_CAST_SHORT
-
- // loop body
- Mat rowsbuf0(w, (size_t)2u);
- Mat rowsbuf1(w, (size_t)2u);
- short* rows0 = (short*)rowsbuf0.data;
- short* rows1 = (short*)rowsbuf1.data;
-
- int prev_sy1 = -2;
-
- for (int dy = 0; dy < h; dy++)
- {
- sy = yofs[dy];
-
- if (sy == prev_sy1)
- {
- // reuse all rows
- }
- else if (sy == prev_sy1 + 1)
- {
- // hresize one row
- short* rows0_old = rows0;
- rows0 = rows1;
- rows1 = rows0_old;
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- const unsigned char* S1p = S1 + sx;
- rows1p[dx] = (S1p[0] * a0 + S1p[1] * a1) >> 4;
-
- ialphap += 2;
- }
- }
- else
- {
- // hresize two rows
- const unsigned char* S0 = src + srcstride * (sy);
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows0p = rows0;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- const unsigned char* S0p = S0 + sx;
- const unsigned char* S1p = S1 + sx;
- rows0p[dx] = (S0p[0] * a0 + S0p[1] * a1) >> 4;
- rows1p[dx] = (S1p[0] * a0 + S1p[1] * a1) >> 4;
-
- ialphap += 2;
- }
- }
-
- prev_sy1 = sy;
-
- if (dy + 1 < h && yofs[dy + 1] == sy)
- {
- // vresize for two rows
- unsigned char* Dp0 = dst + stride * dy;
- unsigned char* Dp1 = dst + stride * (dy + 1);
-
- vresize_two(rows0, rows1, w, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]);
-
- ibeta += 4;
- dy += 1;
- }
- else
- {
- // vresize
- unsigned char* Dp = dst + stride * dy;
-
- vresize_one(rows0, rows1, w, Dp, ibeta[0], ibeta[1]);
-
- ibeta += 2;
- }
- }
-
- delete[] buf;
- }
-
- void resize_bilinear_c2(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride)
- {
- const int INTER_RESIZE_COEF_BITS = 11;
- const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS;
-
- double scale_x = (double)srcw / w;
- double scale_y = (double)srch / h;
-
- int* buf = new int[w + h + w + h];
-
- int* xofs = buf; //new int[w];
- int* yofs = buf + w; //new int[h];
-
- short* ialpha = (short*)(buf + w + h); //new short[w * 2];
- short* ibeta = (short*)(buf + w + h + w); //new short[h * 2];
-
- float fx;
- float fy;
- int sx;
- int sy;
-
- #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX);
-
- for (int dx = 0; dx < w; dx++)
- {
- fx = (float)((dx + 0.5) * scale_x - 0.5);
- sx = static_cast<int>(floor(fx));
- fx -= sx;
-
- if (sx < 0)
- {
- sx = 0;
- fx = 0.f;
- }
- if (sx >= srcw - 1)
- {
- sx = srcw - 2;
- fx = 1.f;
- }
-
- xofs[dx] = sx * 2;
-
- float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE;
- float a1 = fx * INTER_RESIZE_COEF_SCALE;
-
- ialpha[dx * 2] = SATURATE_CAST_SHORT(a0);
- ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1);
- }
-
- for (int dy = 0; dy < h; dy++)
- {
- fy = (float)((dy + 0.5) * scale_y - 0.5);
- sy = static_cast<int>(floor(fy));
- fy -= sy;
-
- if (sy < 0)
- {
- sy = 0;
- fy = 0.f;
- }
- if (sy >= srch - 1)
- {
- sy = srch - 2;
- fy = 1.f;
- }
-
- yofs[dy] = sy;
-
- float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE;
- float b1 = fy * INTER_RESIZE_COEF_SCALE;
-
- ibeta[dy * 2] = SATURATE_CAST_SHORT(b0);
- ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1);
- }
-
- #undef SATURATE_CAST_SHORT
-
- // loop body
- Mat rowsbuf0(w * 2 + 2, (size_t)2u);
- Mat rowsbuf1(w * 2 + 2, (size_t)2u);
- short* rows0 = (short*)rowsbuf0.data;
- short* rows1 = (short*)rowsbuf1.data;
-
- int prev_sy1 = -2;
-
- for (int dy = 0; dy < h; dy++)
- {
- sy = yofs[dy];
-
- if (sy == prev_sy1)
- {
- // reuse all rows
- }
- else if (sy == prev_sy1 + 1)
- {
- // hresize one row
- short* rows0_old = rows0;
- rows0 = rows1;
- rows1 = rows0_old;
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
-
- const unsigned char* S1p = S1 + sx;
- #if __ARM_NEON
- int16x4_t _a0a1XX = vld1_s16(ialphap);
- int16x4_t _a0a0a1a1 = vzip_s16(_a0a1XX, _a0a1XX).val[0];
- uint8x8_t _S1 = uint8x8_t();
-
- _S1 = vld1_lane_u8(S1p, _S1, 0);
- _S1 = vld1_lane_u8(S1p + 1, _S1, 1);
- _S1 = vld1_lane_u8(S1p + 2, _S1, 2);
- _S1 = vld1_lane_u8(S1p + 3, _S1, 3);
-
- int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1));
- int16x4_t _S1lowhigh = vget_low_s16(_S116);
- int32x4_t _S1ma0a1 = vmull_s16(_S1lowhigh, _a0a0a1a1);
- int32x2_t _rows1low = vadd_s32(vget_low_s32(_S1ma0a1), vget_high_s32(_S1ma0a1));
- int32x4_t _rows1 = vcombine_s32(_rows1low, vget_high_s32(_S1ma0a1));
- int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4);
- vst1_s16(rows1p, _rows1_sr4);
- #else
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- rows1p[0] = (S1p[0] * a0 + S1p[2] * a1) >> 4;
- rows1p[1] = (S1p[1] * a0 + S1p[3] * a1) >> 4;
- #endif // __ARM_NEON
-
- ialphap += 2;
- rows1p += 2;
- }
- }
- else
- {
- // hresize two rows
- const unsigned char* S0 = src + srcstride * (sy);
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows0p = rows0;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- const unsigned char* S0p = S0 + sx;
- const unsigned char* S1p = S1 + sx;
- #if __ARM_NEON
- int16x4_t _a0 = vdup_n_s16(a0);
- int16x4_t _a1 = vdup_n_s16(a1);
- uint8x8_t _S0 = uint8x8_t();
- uint8x8_t _S1 = uint8x8_t();
-
- _S0 = vld1_lane_u8(S0p, _S0, 0);
- _S0 = vld1_lane_u8(S0p + 1, _S0, 1);
- _S0 = vld1_lane_u8(S0p + 2, _S0, 2);
- _S0 = vld1_lane_u8(S0p + 3, _S0, 3);
-
- _S1 = vld1_lane_u8(S1p, _S1, 0);
- _S1 = vld1_lane_u8(S1p + 1, _S1, 1);
- _S1 = vld1_lane_u8(S1p + 2, _S1, 2);
- _S1 = vld1_lane_u8(S1p + 3, _S1, 3);
-
- int16x8_t _S016 = vreinterpretq_s16_u16(vmovl_u8(_S0));
- int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1));
- int16x4_t _S0lowhigh = vget_low_s16(_S016);
- int16x4_t _S1lowhigh = vget_low_s16(_S116);
- int32x2x2_t _S0S1low_S0S1high = vtrn_s32(vreinterpret_s32_s16(_S0lowhigh), vreinterpret_s32_s16(_S1lowhigh));
- int32x4_t _rows01 = vmull_s16(vreinterpret_s16_s32(_S0S1low_S0S1high.val[0]), _a0);
- _rows01 = vmlal_s16(_rows01, vreinterpret_s16_s32(_S0S1low_S0S1high.val[1]), _a1);
- int16x4_t _rows01_sr4 = vshrn_n_s32(_rows01, 4);
- int16x4_t _rows1_sr4 = vext_s16(_rows01_sr4, _rows01_sr4, 2);
- vst1_s16(rows0p, _rows01_sr4);
- vst1_s16(rows1p, _rows1_sr4);
- #else
- rows0p[0] = (S0p[0] * a0 + S0p[2] * a1) >> 4;
- rows0p[1] = (S0p[1] * a0 + S0p[3] * a1) >> 4;
- rows1p[0] = (S1p[0] * a0 + S1p[2] * a1) >> 4;
- rows1p[1] = (S1p[1] * a0 + S1p[3] * a1) >> 4;
- #endif // __ARM_NEON
-
- ialphap += 2;
- rows0p += 2;
- rows1p += 2;
- }
- }
-
- prev_sy1 = sy;
-
- if (dy + 1 < h && yofs[dy + 1] == sy)
- {
- // vresize for two rows
- unsigned char* Dp0 = dst + stride * dy;
- unsigned char* Dp1 = dst + stride * (dy + 1);
-
- vresize_two(rows0, rows1, w * 2, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]);
-
- ibeta += 4;
- dy += 1;
- }
- else
- {
- // vresize
- unsigned char* Dp = dst + stride * dy;
-
- vresize_one(rows0, rows1, w * 2, Dp, ibeta[0], ibeta[1]);
-
- ibeta += 2;
- }
- }
-
- delete[] buf;
- }
-
- void resize_bilinear_c3(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride)
- {
- const int INTER_RESIZE_COEF_BITS = 11;
- const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS;
-
- double scale_x = (double)srcw / w;
- double scale_y = (double)srch / h;
-
- int* buf = new int[w + h + w + h];
-
- int* xofs = buf; //new int[w];
- int* yofs = buf + w; //new int[h];
-
- short* ialpha = (short*)(buf + w + h); //new short[w * 2];
- short* ibeta = (short*)(buf + w + h + w); //new short[h * 2];
-
- float fx;
- float fy;
- int sx;
- int sy;
-
- #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX);
-
- for (int dx = 0; dx < w; dx++)
- {
- fx = (float)((dx + 0.5) * scale_x - 0.5);
- sx = static_cast<int>(floor(fx));
- fx -= sx;
-
- if (sx < 0)
- {
- sx = 0;
- fx = 0.f;
- }
- if (sx >= srcw - 1)
- {
- sx = srcw - 2;
- fx = 1.f;
- }
-
- xofs[dx] = sx * 3;
-
- float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE;
- float a1 = fx * INTER_RESIZE_COEF_SCALE;
-
- ialpha[dx * 2] = SATURATE_CAST_SHORT(a0);
- ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1);
- }
-
- for (int dy = 0; dy < h; dy++)
- {
- fy = (float)((dy + 0.5) * scale_y - 0.5);
- sy = static_cast<int>(floor(fy));
- fy -= sy;
-
- if (sy < 0)
- {
- sy = 0;
- fy = 0.f;
- }
- if (sy >= srch - 1)
- {
- sy = srch - 2;
- fy = 1.f;
- }
-
- yofs[dy] = sy;
-
- float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE;
- float b1 = fy * INTER_RESIZE_COEF_SCALE;
-
- ibeta[dy * 2] = SATURATE_CAST_SHORT(b0);
- ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1);
- }
-
- #undef SATURATE_CAST_SHORT
-
- // loop body
- Mat rowsbuf0(w * 3 + 1, (size_t)2u);
- Mat rowsbuf1(w * 3 + 1, (size_t)2u);
- short* rows0 = (short*)rowsbuf0.data;
- short* rows1 = (short*)rowsbuf1.data;
-
- int prev_sy1 = -2;
-
- for (int dy = 0; dy < h; dy++)
- {
- sy = yofs[dy];
-
- if (sy == prev_sy1)
- {
- // reuse all rows
- }
- else if (sy == prev_sy1 + 1)
- {
- // hresize one row
- short* rows0_old = rows0;
- rows0 = rows1;
- rows1 = rows0_old;
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- const unsigned char* S1p = S1 + sx;
- #if __ARM_NEON
- int16x4_t _a0 = vdup_n_s16(a0);
- int16x4_t _a1 = vdup_n_s16(a1);
- uint8x8_t _S1 = uint8x8_t();
-
- _S1 = vld1_lane_u8(S1p, _S1, 0);
- _S1 = vld1_lane_u8(S1p + 1, _S1, 1);
- _S1 = vld1_lane_u8(S1p + 2, _S1, 2);
- _S1 = vld1_lane_u8(S1p + 3, _S1, 3);
- _S1 = vld1_lane_u8(S1p + 4, _S1, 4);
- _S1 = vld1_lane_u8(S1p + 5, _S1, 5);
-
- int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1));
- int16x4_t _S1low = vget_low_s16(_S116);
- int16x4_t _S1high = vext_s16(_S1low, vget_high_s16(_S116), 3);
- int32x4_t _rows1 = vmull_s16(_S1low, _a0);
- _rows1 = vmlal_s16(_rows1, _S1high, _a1);
- int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4);
- vst1_s16(rows1p, _rows1_sr4);
- #else
- rows1p[0] = (S1p[0] * a0 + S1p[3] * a1) >> 4;
- rows1p[1] = (S1p[1] * a0 + S1p[4] * a1) >> 4;
- rows1p[2] = (S1p[2] * a0 + S1p[5] * a1) >> 4;
- #endif // __ARM_NEON
-
- ialphap += 2;
- rows1p += 3;
- }
- }
- else
- {
- // hresize two rows
- const unsigned char* S0 = src + srcstride * (sy);
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows0p = rows0;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- const unsigned char* S0p = S0 + sx;
- const unsigned char* S1p = S1 + sx;
- #if __ARM_NEON
- int16x4_t _a0 = vdup_n_s16(a0);
- int16x4_t _a1 = vdup_n_s16(a1);
- uint8x8_t _S0 = uint8x8_t();
- uint8x8_t _S1 = uint8x8_t();
-
- _S0 = vld1_lane_u8(S0p, _S0, 0);
- _S0 = vld1_lane_u8(S0p + 1, _S0, 1);
- _S0 = vld1_lane_u8(S0p + 2, _S0, 2);
- _S0 = vld1_lane_u8(S0p + 3, _S0, 3);
- _S0 = vld1_lane_u8(S0p + 4, _S0, 4);
- _S0 = vld1_lane_u8(S0p + 5, _S0, 5);
-
- _S1 = vld1_lane_u8(S1p, _S1, 0);
- _S1 = vld1_lane_u8(S1p + 1, _S1, 1);
- _S1 = vld1_lane_u8(S1p + 2, _S1, 2);
- _S1 = vld1_lane_u8(S1p + 3, _S1, 3);
- _S1 = vld1_lane_u8(S1p + 4, _S1, 4);
- _S1 = vld1_lane_u8(S1p + 5, _S1, 5);
-
- int16x8_t _S016 = vreinterpretq_s16_u16(vmovl_u8(_S0));
- int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1));
- int16x4_t _S0low = vget_low_s16(_S016);
- int16x4_t _S1low = vget_low_s16(_S116);
- int16x4_t _S0high = vext_s16(_S0low, vget_high_s16(_S016), 3);
- int16x4_t _S1high = vext_s16(_S1low, vget_high_s16(_S116), 3);
- int32x4_t _rows0 = vmull_s16(_S0low, _a0);
- int32x4_t _rows1 = vmull_s16(_S1low, _a0);
- _rows0 = vmlal_s16(_rows0, _S0high, _a1);
- _rows1 = vmlal_s16(_rows1, _S1high, _a1);
- int16x4_t _rows0_sr4 = vshrn_n_s32(_rows0, 4);
- int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4);
- vst1_s16(rows0p, _rows0_sr4);
- vst1_s16(rows1p, _rows1_sr4);
- #else
- rows0p[0] = (S0p[0] * a0 + S0p[3] * a1) >> 4;
- rows0p[1] = (S0p[1] * a0 + S0p[4] * a1) >> 4;
- rows0p[2] = (S0p[2] * a0 + S0p[5] * a1) >> 4;
- rows1p[0] = (S1p[0] * a0 + S1p[3] * a1) >> 4;
- rows1p[1] = (S1p[1] * a0 + S1p[4] * a1) >> 4;
- rows1p[2] = (S1p[2] * a0 + S1p[5] * a1) >> 4;
- #endif // __ARM_NEON
-
- ialphap += 2;
- rows0p += 3;
- rows1p += 3;
- }
- }
-
- prev_sy1 = sy;
-
- if (dy + 1 < h && yofs[dy + 1] == sy)
- {
- // vresize for two rows
- unsigned char* Dp0 = dst + stride * dy;
- unsigned char* Dp1 = dst + stride * (dy + 1);
-
- vresize_two(rows0, rows1, w * 3, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]);
-
- ibeta += 4;
- dy += 1;
- }
- else
- {
- // vresize
- unsigned char* Dp = dst + stride * dy;
-
- vresize_one(rows0, rows1, w * 3, Dp, ibeta[0], ibeta[1]);
-
- ibeta += 2;
- }
- }
-
- delete[] buf;
- }
-
- void resize_bilinear_c4(const unsigned char* src, int srcw, int srch, int srcstride, unsigned char* dst, int w, int h, int stride)
- {
- const int INTER_RESIZE_COEF_BITS = 11;
- const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS;
-
- double scale_x = (double)srcw / w;
- double scale_y = (double)srch / h;
-
- int* buf = new int[w + h + w + h];
-
- int* xofs = buf; //new int[w];
- int* yofs = buf + w; //new int[h];
-
- short* ialpha = (short*)(buf + w + h); //new short[w * 2];
- short* ibeta = (short*)(buf + w + h + w); //new short[h * 2];
-
- float fx;
- float fy;
- int sx;
- int sy;
-
- #define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX);
-
- for (int dx = 0; dx < w; dx++)
- {
- fx = (float)((dx + 0.5) * scale_x - 0.5);
- sx = static_cast<int>(floor(fx));
- fx -= sx;
-
- if (sx < 0)
- {
- sx = 0;
- fx = 0.f;
- }
- if (sx >= srcw - 1)
- {
- sx = srcw - 2;
- fx = 1.f;
- }
-
- xofs[dx] = sx * 4;
-
- float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE;
- float a1 = fx * INTER_RESIZE_COEF_SCALE;
-
- ialpha[dx * 2] = SATURATE_CAST_SHORT(a0);
- ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1);
- }
-
- for (int dy = 0; dy < h; dy++)
- {
- fy = (float)((dy + 0.5) * scale_y - 0.5);
- sy = static_cast<int>(floor(fy));
- fy -= sy;
-
- if (sy < 0)
- {
- sy = 0;
- fy = 0.f;
- }
- if (sy >= srch - 1)
- {
- sy = srch - 2;
- fy = 1.f;
- }
-
- yofs[dy] = sy;
-
- float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE;
- float b1 = fy * INTER_RESIZE_COEF_SCALE;
-
- ibeta[dy * 2] = SATURATE_CAST_SHORT(b0);
- ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1);
- }
-
- #undef SATURATE_CAST_SHORT
-
- // loop body
- Mat rowsbuf0(w * 4, (size_t)2u);
- Mat rowsbuf1(w * 4, (size_t)2u);
- short* rows0 = (short*)rowsbuf0.data;
- short* rows1 = (short*)rowsbuf1.data;
-
- int prev_sy1 = -2;
-
- for (int dy = 0; dy < h; dy++)
- {
- sy = yofs[dy];
-
- if (sy == prev_sy1)
- {
- // reuse all rows
- }
- else if (sy == prev_sy1 + 1)
- {
- // hresize one row
- short* rows0_old = rows0;
- rows0 = rows1;
- rows1 = rows0_old;
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- const unsigned char* S1p = S1 + sx;
- #if __ARM_NEON
- int16x4_t _a0 = vdup_n_s16(a0);
- int16x4_t _a1 = vdup_n_s16(a1);
- uint8x8_t _S1 = vld1_u8(S1p);
- int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1));
- int16x4_t _S1low = vget_low_s16(_S116);
- int16x4_t _S1high = vget_high_s16(_S116);
- int32x4_t _rows1 = vmull_s16(_S1low, _a0);
- _rows1 = vmlal_s16(_rows1, _S1high, _a1);
- int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4);
- vst1_s16(rows1p, _rows1_sr4);
- #else
- rows1p[0] = (S1p[0] * a0 + S1p[4] * a1) >> 4;
- rows1p[1] = (S1p[1] * a0 + S1p[5] * a1) >> 4;
- rows1p[2] = (S1p[2] * a0 + S1p[6] * a1) >> 4;
- rows1p[3] = (S1p[3] * a0 + S1p[7] * a1) >> 4;
- #endif // __ARM_NEON
-
- ialphap += 2;
- rows1p += 4;
- }
- }
- else
- {
- // hresize two rows
- const unsigned char* S0 = src + srcstride * (sy);
- const unsigned char* S1 = src + srcstride * (sy + 1);
-
- const short* ialphap = ialpha;
- short* rows0p = rows0;
- short* rows1p = rows1;
- for (int dx = 0; dx < w; dx++)
- {
- sx = xofs[dx];
- short a0 = ialphap[0];
- short a1 = ialphap[1];
-
- const unsigned char* S0p = S0 + sx;
- const unsigned char* S1p = S1 + sx;
- #if __ARM_NEON
- int16x4_t _a0 = vdup_n_s16(a0);
- int16x4_t _a1 = vdup_n_s16(a1);
- uint8x8_t _S0 = vld1_u8(S0p);
- uint8x8_t _S1 = vld1_u8(S1p);
- int16x8_t _S016 = vreinterpretq_s16_u16(vmovl_u8(_S0));
- int16x8_t _S116 = vreinterpretq_s16_u16(vmovl_u8(_S1));
- int16x4_t _S0low = vget_low_s16(_S016);
- int16x4_t _S1low = vget_low_s16(_S116);
- int16x4_t _S0high = vget_high_s16(_S016);
- int16x4_t _S1high = vget_high_s16(_S116);
- int32x4_t _rows0 = vmull_s16(_S0low, _a0);
- int32x4_t _rows1 = vmull_s16(_S1low, _a0);
- _rows0 = vmlal_s16(_rows0, _S0high, _a1);
- _rows1 = vmlal_s16(_rows1, _S1high, _a1);
- int16x4_t _rows0_sr4 = vshrn_n_s32(_rows0, 4);
- int16x4_t _rows1_sr4 = vshrn_n_s32(_rows1, 4);
- vst1_s16(rows0p, _rows0_sr4);
- vst1_s16(rows1p, _rows1_sr4);
- #else
- rows0p[0] = (S0p[0] * a0 + S0p[4] * a1) >> 4;
- rows0p[1] = (S0p[1] * a0 + S0p[5] * a1) >> 4;
- rows0p[2] = (S0p[2] * a0 + S0p[6] * a1) >> 4;
- rows0p[3] = (S0p[3] * a0 + S0p[7] * a1) >> 4;
- rows1p[0] = (S1p[0] * a0 + S1p[4] * a1) >> 4;
- rows1p[1] = (S1p[1] * a0 + S1p[5] * a1) >> 4;
- rows1p[2] = (S1p[2] * a0 + S1p[6] * a1) >> 4;
- rows1p[3] = (S1p[3] * a0 + S1p[7] * a1) >> 4;
- #endif // __ARM_NEON
-
- ialphap += 2;
- rows0p += 4;
- rows1p += 4;
- }
- }
-
- prev_sy1 = sy;
-
- if (dy + 1 < h && yofs[dy + 1] == sy)
- {
- // vresize for two rows
- unsigned char* Dp0 = dst + stride * dy;
- unsigned char* Dp1 = dst + stride * (dy + 1);
-
- vresize_two(rows0, rows1, w * 4, Dp0, Dp1, ibeta[0], ibeta[1], ibeta[2], ibeta[3]);
-
- ibeta += 4;
- dy += 1;
- }
- else
- {
- // vresize
- unsigned char* Dp = dst + stride * dy;
-
- vresize_one(rows0, rows1, w * 4, Dp, ibeta[0], ibeta[1]);
-
- ibeta += 2;
- }
- }
-
- delete[] buf;
- }
-
- void resize_bilinear_yuv420sp(const unsigned char* src, int srcw, int srch, unsigned char* dst, int w, int h)
- {
- // assert srcw % 2 == 0
- // assert srch % 2 == 0
- // assert w % 2 == 0
- // assert h % 2 == 0
-
- const unsigned char* srcY = src;
- unsigned char* dstY = dst;
- resize_bilinear_c1(srcY, srcw, srch, dstY, w, h);
-
- const unsigned char* srcUV = src + srcw * srch;
- unsigned char* dstUV = dst + w * h;
- resize_bilinear_c2(srcUV, srcw / 2, srch / 2, dstUV, w / 2, h / 2);
- }
- #endif // NCNN_PIXEL
-
- } // namespace ncnn
|