From ce92ee4b4f3ef13aba551897f54d794bcba5dd62 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Fri, 5 Apr 2019 11:31:43 +0300 Subject: [PATCH] swscale/ppc: VSX-optimize non-full-chroma yuv2rgb_2 ./ffmpeg -f lavfi -i yuvtestsrc=duration=1:size=1200x1440 -sws_flags fast_bilinear \ -s 1200x720 -f null -vframes 100 -pix_fmt $i -nostats \ -cpuflags 0 -v error - 32-bit mul, power8 only. ~2x speedup: rgb24 24431 UNITS in yuv2packed2, 16384 runs, 0 skips 13783 UNITS in yuv2packed2, 16383 runs, 1 skips bgr24 24396 UNITS in yuv2packed2, 16384 runs, 0 skips 14059 UNITS in yuv2packed2, 16384 runs, 0 skips rgba 26815 UNITS in yuv2packed2, 16383 runs, 1 skips 12797 UNITS in yuv2packed2, 16383 runs, 1 skips bgra 27060 UNITS in yuv2packed2, 16384 runs, 0 skips 13138 UNITS in yuv2packed2, 16384 runs, 0 skips argb 26998 UNITS in yuv2packed2, 16384 runs, 0 skips 12728 UNITS in yuv2packed2, 16381 runs, 3 skips bgra 26651 UNITS in yuv2packed2, 16384 runs, 0 skips 13124 UNITS in yuv2packed2, 16384 runs, 0 skips This is a low speedup, but the x86 mmx version also gets only ~2x. The mmx version is also heavily inaccurate, while the vsx version has high accuracy. --- libswscale/ppc/swscale_vsx.c | 188 +++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/libswscale/ppc/swscale_vsx.c b/libswscale/ppc/swscale_vsx.c index e05f9ecdd3..ba00791dcb 100644 --- a/libswscale/ppc/swscale_vsx.c +++ b/libswscale/ppc/swscale_vsx.c @@ -793,6 +793,180 @@ yuv2rgb_full_2_vsx_template(SwsContext *c, const int16_t *buf[2], } } +static av_always_inline void +yuv2rgb_2_vsx_template(SwsContext *c, const int16_t *buf[2], + const int16_t *ubuf[2], const int16_t *vbuf[2], + const int16_t *abuf[2], uint8_t *dest, int dstW, + int yalpha, int uvalpha, int y, + enum AVPixelFormat target, int hasAlpha) +{ + const int16_t *buf0 = buf[0], *buf1 = buf[1], + *ubuf0 = ubuf[0], *ubuf1 = ubuf[1], + *vbuf0 = vbuf[0], *vbuf1 = vbuf[1], + *abuf0 = hasAlpha ? abuf[0] : NULL, + *abuf1 = hasAlpha ? abuf[1] : NULL; + const int16_t yalpha1 = 4096 - yalpha; + const int16_t uvalpha1 = 4096 - uvalpha; + vector int16_t vy, vu, vv, A = vec_splat_s16(0); + vector int32_t vy32_l, vy32_r, vu32_l, vu32_r, vv32_l, vv32_r, tmp32; + vector int32_t R_l, R_r, G_l, G_r, B_l, B_r, vud32_l, vud32_r, vvd32_l, vvd32_r; + vector int32_t tmp, tmp2, tmp3, tmp4, tmp5, tmp6; + vector uint16_t rd16, gd16, bd16; + vector uint8_t rd, bd, gd, ad, out0, out1, tmp8; + const vector int16_t vyalpha1 = vec_splats(yalpha1); + const vector int16_t vuvalpha1 = vec_splats(uvalpha1); + const vector int16_t vyalpha = vec_splats((int16_t) yalpha); + const vector int16_t vuvalpha = vec_splats((int16_t) uvalpha); + const vector uint16_t zero16 = vec_splat_u16(0); + const vector int32_t y_offset = vec_splats(c->yuv2rgb_y_offset); + const vector int32_t y_coeff = vec_splats(c->yuv2rgb_y_coeff); + const vector int32_t y_add = vec_splats(1 << 21); + const vector int32_t v2r_coeff = vec_splats(c->yuv2rgb_v2r_coeff); + const vector int32_t v2g_coeff = vec_splats(c->yuv2rgb_v2g_coeff); + const vector int32_t u2g_coeff = vec_splats(c->yuv2rgb_u2g_coeff); + const vector int32_t u2b_coeff = vec_splats(c->yuv2rgb_u2b_coeff); + const vector int32_t rgbclip = vec_splats(1 << 30); + const vector int32_t zero32 = vec_splat_s32(0); + const vector uint32_t shift19 = vec_splats(19U); + const vector uint32_t shift22 = vec_splats(22U); + const vector uint32_t shift10 = vec_splat_u32(10); + const vector int32_t dec128 = vec_splats(128 << 19); + const vector int32_t add18 = vec_splats(1 << 18); + int i; + + // Various permutations + const vector uint8_t doubleleft = (vector uint8_t) {0, 1, 2, 3, + 0, 1, 2, 3, + 4, 5, 6, 7, + 4, 5, 6, 7 }; + const vector uint8_t doubleright = (vector uint8_t) {8, 9, 10, 11, + 8, 9, 10, 11, + 12, 13, 14, 15, + 12, 13, 14, 15 }; + const vector uint8_t perm3rg0 = (vector uint8_t) {0x0, 0x10, 0, + 0x1, 0x11, 0, + 0x2, 0x12, 0, + 0x3, 0x13, 0, + 0x4, 0x14, 0, + 0x5 }; + const vector uint8_t perm3rg1 = (vector uint8_t) { 0x15, 0, + 0x6, 0x16, 0, + 0x7, 0x17, 0 }; + const vector uint8_t perm3tb0 = (vector uint8_t) {0x0, 0x1, 0x10, + 0x3, 0x4, 0x11, + 0x6, 0x7, 0x12, + 0x9, 0xa, 0x13, + 0xc, 0xd, 0x14, + 0xf }; + const vector uint8_t perm3tb1 = (vector uint8_t) { 0x0, 0x15, + 0x2, 0x3, 0x16, + 0x5, 0x6, 0x17 }; + + av_assert2(yalpha <= 4096U); + av_assert2(uvalpha <= 4096U); + + for (i = 0; i < (dstW + 1) >> 1; i += 8) { + SETUP(vy, &buf0[i * 2], vyalpha1, &buf1[i * 2], vyalpha); + vy32_l = vec_sra(tmp3, shift10); + vy32_r = vec_sra(tmp4, shift10); + + SETUP(vu, &ubuf0[i], vuvalpha1, &ubuf1[i], vuvalpha); + tmp3 = vec_sub(tmp3, dec128); + tmp4 = vec_sub(tmp4, dec128); + vu32_l = vec_sra(tmp3, shift10); + vu32_r = vec_sra(tmp4, shift10); + + SETUP(vv, &vbuf0[i], vuvalpha1, &vbuf1[i], vuvalpha); + tmp3 = vec_sub(tmp3, dec128); + tmp4 = vec_sub(tmp4, dec128); + vv32_l = vec_sra(tmp3, shift10); + vv32_r = vec_sra(tmp4, shift10); + + if (hasAlpha) { + SETUP(A, &abuf0[i], vyalpha1, &abuf1[i], vyalpha); + tmp3 = vec_add(tmp3, add18); + tmp4 = vec_add(tmp4, add18); + tmp3 = vec_sra(tmp3, shift19); + tmp4 = vec_sra(tmp4, shift19); + A = vec_packs(tmp3, tmp4); + ad = vec_packsu(A, (vector int16_t) zero16); + } else { + ad = vec_splats((uint8_t) 255); + } + + vy32_l = vec_sub(vy32_l, y_offset); + vy32_r = vec_sub(vy32_r, y_offset); + vy32_l = vec_mul(vy32_l, y_coeff); + vy32_r = vec_mul(vy32_r, y_coeff); + vy32_l = vec_add(vy32_l, y_add); + vy32_r = vec_add(vy32_r, y_add); + + // Use the first UV half + vud32_l = vec_perm(vu32_l, vu32_l, doubleleft); + vud32_r = vec_perm(vu32_l, vu32_l, doubleright); + vvd32_l = vec_perm(vv32_l, vv32_l, doubleleft); + vvd32_r = vec_perm(vv32_l, vv32_l, doubleright); + + R_l = vec_mul(vvd32_l, v2r_coeff); + R_l = vec_add(R_l, vy32_l); + R_r = vec_mul(vvd32_r, v2r_coeff); + R_r = vec_add(R_r, vy32_r); + G_l = vec_mul(vvd32_l, v2g_coeff); + tmp32 = vec_mul(vud32_l, u2g_coeff); + G_l = vec_add(G_l, vy32_l); + G_l = vec_add(G_l, tmp32); + G_r = vec_mul(vvd32_r, v2g_coeff); + tmp32 = vec_mul(vud32_r, u2g_coeff); + G_r = vec_add(G_r, vy32_r); + G_r = vec_add(G_r, tmp32); + + B_l = vec_mul(vud32_l, u2b_coeff); + B_l = vec_add(B_l, vy32_l); + B_r = vec_mul(vud32_r, u2b_coeff); + B_r = vec_add(B_r, vy32_r); + + WRITERGB + + // New Y for the second half + SETUP(vy, &buf0[i * 2 + 8], vyalpha1, &buf1[i * 2 + 8], vyalpha); + vy32_l = vec_sra(tmp3, shift10); + vy32_r = vec_sra(tmp4, shift10); + + vy32_l = vec_sub(vy32_l, y_offset); + vy32_r = vec_sub(vy32_r, y_offset); + vy32_l = vec_mul(vy32_l, y_coeff); + vy32_r = vec_mul(vy32_r, y_coeff); + vy32_l = vec_add(vy32_l, y_add); + vy32_r = vec_add(vy32_r, y_add); + + // Second UV half + vud32_l = vec_perm(vu32_r, vu32_r, doubleleft); + vud32_r = vec_perm(vu32_r, vu32_r, doubleright); + vvd32_l = vec_perm(vv32_r, vv32_r, doubleleft); + vvd32_r = vec_perm(vv32_r, vv32_r, doubleright); + + R_l = vec_mul(vvd32_l, v2r_coeff); + R_l = vec_add(R_l, vy32_l); + R_r = vec_mul(vvd32_r, v2r_coeff); + R_r = vec_add(R_r, vy32_r); + G_l = vec_mul(vvd32_l, v2g_coeff); + tmp32 = vec_mul(vud32_l, u2g_coeff); + G_l = vec_add(G_l, vy32_l); + G_l = vec_add(G_l, tmp32); + G_r = vec_mul(vvd32_r, v2g_coeff); + tmp32 = vec_mul(vud32_r, u2g_coeff); + G_r = vec_add(G_r, vy32_r); + G_r = vec_add(G_r, tmp32); + + B_l = vec_mul(vud32_l, u2b_coeff); + B_l = vec_add(B_l, vy32_l); + B_r = vec_mul(vud32_r, u2b_coeff); + B_r = vec_add(B_r, vy32_r); + + WRITERGB + } +} + #undef SETUP static av_always_inline void @@ -1151,6 +1325,14 @@ YUV2RGBWRAPPER(yuv2, rgb, xbgr32, AV_PIX_FMT_ABGR, 0) YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0) YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0) +YUV2RGBWRAPPERX2(yuv2, rgb, bgrx32, AV_PIX_FMT_BGRA, 0) +YUV2RGBWRAPPERX2(yuv2, rgb, rgbx32, AV_PIX_FMT_RGBA, 0) +YUV2RGBWRAPPERX2(yuv2, rgb, xrgb32, AV_PIX_FMT_ARGB, 0) +YUV2RGBWRAPPERX2(yuv2, rgb, xbgr32, AV_PIX_FMT_ABGR, 0) + +YUV2RGBWRAPPERX2(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0) +YUV2RGBWRAPPERX2(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0) + YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0) YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0) YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0) @@ -1616,6 +1798,7 @@ av_cold void ff_sws_init_swscale_vsx(SwsContext *c) if (HAVE_POWER8 && cpu_flags & AV_CPU_FLAG_POWER8) { if (!c->needAlpha) { c->yuv2packed1 = yuv2bgrx32_1_vsx; + c->yuv2packed2 = yuv2bgrx32_2_vsx; } } break; @@ -1623,6 +1806,7 @@ av_cold void ff_sws_init_swscale_vsx(SwsContext *c) if (HAVE_POWER8 && cpu_flags & AV_CPU_FLAG_POWER8) { if (!c->needAlpha) { c->yuv2packed1 = yuv2rgbx32_1_vsx; + c->yuv2packed2 = yuv2rgbx32_2_vsx; } } break; @@ -1630,6 +1814,7 @@ av_cold void ff_sws_init_swscale_vsx(SwsContext *c) if (HAVE_POWER8 && cpu_flags & AV_CPU_FLAG_POWER8) { if (!c->needAlpha) { c->yuv2packed1 = yuv2xrgb32_1_vsx; + c->yuv2packed2 = yuv2xrgb32_2_vsx; } } break; @@ -1637,17 +1822,20 @@ av_cold void ff_sws_init_swscale_vsx(SwsContext *c) if (HAVE_POWER8 && cpu_flags & AV_CPU_FLAG_POWER8) { if (!c->needAlpha) { c->yuv2packed1 = yuv2xbgr32_1_vsx; + c->yuv2packed2 = yuv2xbgr32_2_vsx; } } break; case AV_PIX_FMT_RGB24: if (HAVE_POWER8 && cpu_flags & AV_CPU_FLAG_POWER8) { c->yuv2packed1 = yuv2rgb24_1_vsx; + c->yuv2packed2 = yuv2rgb24_2_vsx; } break; case AV_PIX_FMT_BGR24: if (HAVE_POWER8 && cpu_flags & AV_CPU_FLAG_POWER8) { c->yuv2packed1 = yuv2bgr24_1_vsx; + c->yuv2packed2 = yuv2bgr24_2_vsx; } break; }