~2x faster SSE2 RGB24toY, BGR24toY, ARGBToY|UV

global effect is ~2% faster encoding from JPG source
and ~8% faster lossless-webp source decoding to PGM (e.g.)

Also revamped the YUVA case to first accumulate R/G/B value into 16b
temporary buffer, and then doing the UV conversion.
-> New function: WebPConvertRGBA32ToUV

Change-Id: I1d7d0c4003aa02966ad33490ce0fcdc7925cf9f5
This commit is contained in:
Pascal Massimino 2015-11-04 08:51:24 +01:00 committed by James Zern
parent 96f5b4237d
commit bfd3fc02df
4 changed files with 409 additions and 69 deletions

View File

@ -335,10 +335,20 @@ extern void (*WebPConvertARGBToY)(const uint32_t* argb, uint8_t* y, int width);
extern void (*WebPConvertARGBToUV)(const uint32_t* argb, uint8_t* u, uint8_t* v,
int src_width, int do_store);
// Convert a row of accumulated (four-values) of rgba32 toward U/V
extern void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
uint8_t* u, uint8_t* v, int width);
// Convert RGB or BGR to Y
extern void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
extern void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
// used for plain-C fallback.
extern void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
int src_width, int do_store);
extern void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
uint8_t* u, uint8_t* v, int width);
// Must be called before using the above.
void WebPInitConvertARGBToYUV(void);

View File

@ -164,7 +164,7 @@ WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplers(void) {
}
//-----------------------------------------------------------------------------
// ARGB -> YUV converters (for lossless decoding)
// ARGB -> YUV converters
static void ConvertARGBToY(const uint32_t* argb, uint8_t* y, int width) {
int i;
@ -175,7 +175,7 @@ static void ConvertARGBToY(const uint32_t* argb, uint8_t* y, int width) {
}
}
static void ConvertARGBToUV(const uint32_t* argb, uint8_t* u, uint8_t* v,
void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
int src_width, int do_store) {
// No rounding. Last pixel is dealt with separately.
const int uv_width = src_width >> 1;
@ -232,29 +232,47 @@ static void ConvertBGR24ToY(const uint8_t* bgr, uint8_t* y, int width) {
}
}
void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
uint8_t* u, uint8_t* v, int width) {
int i;
for (i = 0; i < width; i += 1, rgb += 4) {
const int r = rgb[0], g = rgb[1], b = rgb[2];
u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
}
}
//-----------------------------------------------------------------------------
void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
uint8_t* u, uint8_t* v, int width);
void (*WebPConvertARGBToY)(const uint32_t* argb, uint8_t* y, int width);
void (*WebPConvertARGBToUV)(const uint32_t* argb, uint8_t* u, uint8_t* v,
int src_width, int do_store);
static volatile VP8CPUInfo rgba_to_yuv_last_cpuinfo_used =
(VP8CPUInfo)&rgba_to_yuv_last_cpuinfo_used;
extern void WebPInitConvertARGBToYUVSSE2(void);
WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUV(void) {
if (rgba_to_yuv_last_cpuinfo_used == VP8GetCPUInfo) return;
WebPConvertARGBToY = ConvertARGBToY;
WebPConvertARGBToUV = ConvertARGBToUV;
WebPConvertARGBToUV = WebPConvertARGBToUV_C;
WebPConvertRGB24ToY = ConvertRGB24ToY;
WebPConvertBGR24ToY = ConvertBGR24ToY;
WebPConvertRGBA32ToUV = WebPConvertRGBA32ToUV_C;
if (VP8GetCPUInfo != NULL) {
#if defined(WEBP_USE_SSE2)
if (VP8GetCPUInfo(kSSE2)) {
WebPInitConvertARGBToYUVSSE2();
}
#endif // WEBP_USE_SSE2
}

View File

@ -187,8 +187,7 @@ void VP8YuvToBgr32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
//-----------------------------------------------------------------------------
// Arbitrary-length row conversion functions
static void YuvToRgbaRowSSE2(const uint8_t* y,
const uint8_t* u, const uint8_t* v,
static void YuvToRgbaRow(const uint8_t* y, const uint8_t* u, const uint8_t* v,
uint8_t* dst, int len) {
int n;
for (n = 0; n + 4 <= len; n += 4) {
@ -218,8 +217,7 @@ static void YuvToRgbaRowSSE2(const uint8_t* y,
}
}
static void YuvToBgraRowSSE2(const uint8_t* y,
const uint8_t* u, const uint8_t* v,
static void YuvToBgraRow(const uint8_t* y, const uint8_t* u, const uint8_t* v,
uint8_t* dst, int len) {
int n;
for (n = 0; n + 2 <= len; n += 2) {
@ -242,8 +240,7 @@ static void YuvToBgraRowSSE2(const uint8_t* y,
}
}
static void YuvToArgbRowSSE2(const uint8_t* y,
const uint8_t* u, const uint8_t* v,
static void YuvToArgbRow(const uint8_t* y, const uint8_t* u, const uint8_t* v,
uint8_t* dst, int len) {
int n;
for (n = 0; n + 2 <= len; n += 2) {
@ -266,8 +263,7 @@ static void YuvToArgbRowSSE2(const uint8_t* y,
}
}
static void YuvToRgbRowSSE2(const uint8_t* y,
const uint8_t* u, const uint8_t* v,
static void YuvToRgbRow(const uint8_t* y, const uint8_t* u, const uint8_t* v,
uint8_t* dst, int len) {
int n;
for (n = 0; n + 2 < len; ++n) { // we directly stomp the *dst memory
@ -283,8 +279,7 @@ static void YuvToRgbRowSSE2(const uint8_t* y,
}
}
static void YuvToBgrRowSSE2(const uint8_t* y,
const uint8_t* u, const uint8_t* v,
static void YuvToBgrRow(const uint8_t* y, const uint8_t* u, const uint8_t* v,
uint8_t* dst, int len) {
int n;
for (n = 0; n + 2 < len; ++n) { // we directly stomp the *dst memory
@ -306,15 +301,306 @@ static void YuvToBgrRowSSE2(const uint8_t* y,
extern void WebPInitSamplersSSE2(void);
WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplersSSE2(void) {
WebPSamplers[MODE_RGB] = YuvToRgbRowSSE2;
WebPSamplers[MODE_RGBA] = YuvToRgbaRowSSE2;
WebPSamplers[MODE_BGR] = YuvToBgrRowSSE2;
WebPSamplers[MODE_BGRA] = YuvToBgraRowSSE2;
WebPSamplers[MODE_ARGB] = YuvToArgbRowSSE2;
WebPSamplers[MODE_RGB] = YuvToRgbRow;
WebPSamplers[MODE_RGBA] = YuvToRgbaRow;
WebPSamplers[MODE_BGR] = YuvToBgrRow;
WebPSamplers[MODE_BGRA] = YuvToBgraRow;
WebPSamplers[MODE_ARGB] = YuvToArgbRow;
}
//------------------------------------------------------------------------------
// RGB24/32 -> YUV converters
// Load eight 16b-words from *src.
#define LOAD_16(src) _mm_loadu_si128((const __m128i*)(src))
// Store either 16b-words into *dst
#define STORE_16(V, dst) _mm_storeu_si128((__m128i*)(dst), (V))
// Convert 8 packed RGB or BGR samples to r[], g[], b[]
static WEBP_INLINE void RGB24PackedToPlanar(const uint8_t* const rgb,
__m128i* const r,
__m128i* const g,
__m128i* const b,
int input_is_bgr) {
const __m128i zero = _mm_setzero_si128();
// in0: r0 g0 b0 r1 | g1 b1 r2 g2 | b2 r3 g3 b3 | r4 g4 b4 r5
// in1: b2 r3 g3 b3 | r4 g4 b4 r5 | g5 b5 r6 g6 | b6 r7 g7 b7
const __m128i in0 = LOAD_16(rgb + 0);
const __m128i in1 = LOAD_16(rgb + 8);
// A0: | r2 g2 b2 r3 | g3 b3 r4 g4 | b4 r5 ...
// A1: ... b2 r3 | g3 b3 r4 g4 | b4 r5 g5 b5 |
const __m128i A0 = _mm_srli_si128(in0, 6);
const __m128i A1 = _mm_slli_si128(in1, 6);
// B0: r0 r2 g0 g2 | b0 b2 r1 r3 | g1 g3 b1 b3 | r2 r4 b2 b4
// B1: g3 g5 b3 b5 | r4 r6 g4 g6 | b4 b6 r5 r7 | g5 g7 b5 b7
const __m128i B0 = _mm_unpacklo_epi8(in0, A0);
const __m128i B1 = _mm_unpackhi_epi8(A1, in1);
// C0: r1 r3 g1 g3 | b1 b3 r2 r4 | b2 b4 ...
// C1: ... g3 g5 | b3 b5 r4 r6 | g4 g6 b4 b6
const __m128i C0 = _mm_srli_si128(B0, 6);
const __m128i C1 = _mm_slli_si128(B1, 6);
// D0: r0 r1 r2 r3 | g0 g1 g2 g3 | b0 b1 b2 b3 | r1 r2 r3 r4
// D1: b3 b4 b5 b6 | r4 r5 r6 r7 | g4 g5 g6 g7 | b4 b5 b6 b7 |
const __m128i D0 = _mm_unpacklo_epi8(B0, C0);
const __m128i D1 = _mm_unpackhi_epi8(C1, B1);
// r4 r5 r6 r7 | g4 g5 g6 g7 | b4 b5 b6 b7 | 0
const __m128i D2 = _mm_srli_si128(D1, 4);
// r0 r1 r2 r3 | r4 r5 r6 r7 | g0 g1 g2 g3 | g4 g5 g6 g7
const __m128i E0 = _mm_unpacklo_epi32(D0, D2);
// b0 b1 b2 b3 | b4 b5 b6 b7 | r1 r2 r3 r4 | 0
const __m128i E1 = _mm_unpackhi_epi32(D0, D2);
// g0 g1 g2 g3 | g4 g5 g6 g7 | 0
const __m128i E2 = _mm_srli_si128(E0, 8);
const __m128i F0 = _mm_unpacklo_epi8(E0, zero); // -> R
const __m128i F1 = _mm_unpacklo_epi8(E1, zero); // -> B
const __m128i F2 = _mm_unpacklo_epi8(E2, zero); // -> G
*g = F2;
if (input_is_bgr) {
*r = F1;
*b = F0;
} else {
*r = F0;
*b = F1;
}
}
// Convert 8 packed ARGB to r[], g[], b[]
static WEBP_INLINE void RGB32PackedToPlanar(const uint32_t* const argb,
__m128i* const r,
__m128i* const g,
__m128i* const b) {
const __m128i zero = _mm_setzero_si128();
const __m128i in0 = LOAD_16(argb + 0); // argb3 | argb2 | argb1 | argb0
const __m128i in1 = LOAD_16(argb + 4); // argb7 | argb6 | argb5 | argb4
// column-wise transpose
const __m128i A0 = _mm_unpacklo_epi8(in0, in1);
const __m128i A1 = _mm_unpackhi_epi8(in0, in1);
const __m128i B0 = _mm_unpacklo_epi8(A0, A1);
const __m128i B1 = _mm_unpackhi_epi8(A0, A1);
// C0 = g7 g6 ... g1 g0 | b7 b6 ... b1 b0
// C1 = a7 a6 ... a1 a0 | r7 r6 ... r1 r0
const __m128i C0 = _mm_unpacklo_epi8(B0, B1);
const __m128i C1 = _mm_unpackhi_epi8(B0, B1);
// store 16b
*r = _mm_unpacklo_epi8(C1, zero);
*g = _mm_unpackhi_epi8(C0, zero);
*b = _mm_unpacklo_epi8(C0, zero);
}
// This macro computes (RG * MULT_RG + GB * MULT_GB + ROUNDER) >> DESCALE_FIX
// It's a macro and not a function because we need to use immediate values with
// srai_epi32, e.g.
#define TRANSFORM(RG_LO, RG_HI, GB_LO, GB_HI, MULT_RG, MULT_GB, \
ROUNDER, DESCALE_FIX, OUT) do { \
const __m128i V0_lo = _mm_madd_epi16(RG_LO, MULT_RG); \
const __m128i V0_hi = _mm_madd_epi16(RG_HI, MULT_RG); \
const __m128i V1_lo = _mm_madd_epi16(GB_LO, MULT_GB); \
const __m128i V1_hi = _mm_madd_epi16(GB_HI, MULT_GB); \
const __m128i V2_lo = _mm_add_epi32(V0_lo, V1_lo); \
const __m128i V2_hi = _mm_add_epi32(V0_hi, V1_hi); \
const __m128i V3_lo = _mm_add_epi32(V2_lo, ROUNDER); \
const __m128i V3_hi = _mm_add_epi32(V2_hi, ROUNDER); \
const __m128i V5_lo = _mm_srai_epi32(V3_lo, DESCALE_FIX); \
const __m128i V5_hi = _mm_srai_epi32(V3_hi, DESCALE_FIX); \
(OUT) = _mm_packs_epi32(V5_lo, V5_hi); \
} while (0)
#define MK_CST_16(A, B) _mm_set_epi16((B), (A), (B), (A), (B), (A), (B), (A))
static WEBP_INLINE void ConvertRGBToY(const __m128i* const R,
const __m128i* const G,
const __m128i* const B,
__m128i* const Y) {
const __m128i kRG_y = MK_CST_16(16839, 33059 - 16384);
const __m128i kGB_y = MK_CST_16(16384, 6420);
const __m128i kHALF_Y = _mm_set1_epi32((16 << YUV_FIX) + YUV_HALF);
const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_y, kGB_y, kHALF_Y, YUV_FIX, *Y);
}
static WEBP_INLINE void ConvertRGBToUV(const __m128i* const R,
const __m128i* const G,
const __m128i* const B,
__m128i* const U, __m128i* const V) {
const __m128i kRG_u = MK_CST_16(-9719, -19081);
const __m128i kGB_u = MK_CST_16(0, 28800);
const __m128i kRG_v = MK_CST_16(28800, 0);
const __m128i kGB_v = MK_CST_16(-24116, -4684);
const __m128i kHALF_UV = _mm_set1_epi32(((128 << YUV_FIX) + YUV_HALF) << 2);
const __m128i RG_lo = _mm_unpacklo_epi16(*R, *G);
const __m128i RG_hi = _mm_unpackhi_epi16(*R, *G);
const __m128i GB_lo = _mm_unpacklo_epi16(*G, *B);
const __m128i GB_hi = _mm_unpackhi_epi16(*G, *B);
TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_u, kGB_u,
kHALF_UV, YUV_FIX + 2, *U);
TRANSFORM(RG_lo, RG_hi, GB_lo, GB_hi, kRG_v, kGB_v,
kHALF_UV, YUV_FIX + 2, *V);
}
#undef MK_CST_16
#undef TRANSFORM
static void ConvertRGB24ToY(const uint8_t* rgb, uint8_t* y, int width) {
const int max_width = width & ~15;
int i;
for (i = 0; i < max_width; i += 16, rgb += 3 * 16) {
__m128i r, g, b, Y0, Y1;
RGB24PackedToPlanar(rgb + 0 * 8, &r, &g, &b, 0);
ConvertRGBToY(&r, &g, &b, &Y0);
RGB24PackedToPlanar(rgb + 3 * 8, &r, &g, &b, 0);
ConvertRGBToY(&r, &g, &b, &Y1);
STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
}
for (; i < width; ++i, rgb += 3) { // left-over
y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
}
}
static void ConvertBGR24ToY(const uint8_t* bgr, uint8_t* y, int width) {
int i;
const int max_width = width & ~15;
for (i = 0; i < max_width; i += 16, bgr += 3 * 16) {
__m128i r, g, b, Y0, Y1;
RGB24PackedToPlanar(bgr + 0 * 8, &r, &g, &b, 1);
ConvertRGBToY(&r, &g, &b, &Y0);
RGB24PackedToPlanar(bgr + 3 * 8, &r, &g, &b, 1);
ConvertRGBToY(&r, &g, &b, &Y1);
STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
}
for (; i < width; ++i, bgr += 3) { // left-over
y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
}
}
static void ConvertARGBToY(const uint32_t* argb, uint8_t* y, int width) {
const int max_width = width & ~15;
int i;
for (i = 0; i < max_width; i += 16) {
__m128i r, g, b, Y0, Y1;
RGB32PackedToPlanar(&argb[i + 0], &r, &g, &b);
ConvertRGBToY(&r, &g, &b, &Y0);
RGB32PackedToPlanar(&argb[i + 8], &r, &g, &b);
ConvertRGBToY(&r, &g, &b, &Y1);
STORE_16(_mm_packus_epi16(Y0, Y1), y + i);
}
for (; i < width; ++i) { // left-over
const uint32_t p = argb[i];
y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff,
YUV_HALF);
}
}
// Horizontal add (doubled) of two 16b values, result is 16b.
// in: A | B | C | D | ... -> out: 2*(A+B) | 2*(C+D) | ...
static void HorizontalAddPack(const __m128i* const A, const __m128i* const B,
__m128i* const out) {
const __m128i k2 = _mm_set1_epi16(2);
const __m128i C = _mm_madd_epi16(*A, k2);
const __m128i D = _mm_madd_epi16(*B, k2);
*out = _mm_packs_epi32(C, D);
}
static void ConvertARGBToUV(const uint32_t* argb, uint8_t* u, uint8_t* v,
int src_width, int do_store) {
const int max_width = src_width & ~31;
int i;
for (i = 0; i < max_width; i += 32, u += 16, v += 16) {
__m128i r0, g0, b0, r1, g1, b1, U0, V0, U1, V1;
RGB32PackedToPlanar(&argb[i + 0], &r0, &g0, &b0);
RGB32PackedToPlanar(&argb[i + 8], &r1, &g1, &b1);
HorizontalAddPack(&r0, &r1, &r0);
HorizontalAddPack(&g0, &g1, &g0);
HorizontalAddPack(&b0, &b1, &b0);
ConvertRGBToUV(&r0, &g0, &b0, &U0, &V0);
RGB32PackedToPlanar(&argb[i + 16], &r0, &g0, &b0);
RGB32PackedToPlanar(&argb[i + 24], &r1, &g1, &b1);
HorizontalAddPack(&r0, &r1, &r0);
HorizontalAddPack(&g0, &g1, &g0);
HorizontalAddPack(&b0, &b1, &b0);
ConvertRGBToUV(&r0, &g0, &b0, &U1, &V1);
U0 = _mm_packus_epi16(U0, U1);
V0 = _mm_packus_epi16(V0, V1);
if (!do_store) {
const __m128i prev_u = LOAD_16(u);
const __m128i prev_v = LOAD_16(v);
U0 = _mm_avg_epu8(U0, prev_u);
V0 = _mm_avg_epu8(V0, prev_v);
}
STORE_16(U0, u);
STORE_16(V0, v);
}
if (i < src_width) { // left-over
WebPConvertARGBToUV_C(argb + i, u, v, src_width - i, do_store);
}
}
// Convert 16 packed ARGB 16b-values to r[], g[], b[]
static WEBP_INLINE void RGBA32PackedToPlanar_16b(const uint16_t* const rgbx,
__m128i* const r,
__m128i* const g,
__m128i* const b) {
const __m128i in0 = LOAD_16(rgbx + 0); // r0 | g0 | b0 |x| r1 | g1 | b1 |x
const __m128i in1 = LOAD_16(rgbx + 8); // r2 | g2 | b2 |x| r3 | g3 | b3 |x
const __m128i in2 = LOAD_16(rgbx + 16); // r4 | ...
const __m128i in3 = LOAD_16(rgbx + 24); // r6 | ...
// column-wise transpose
const __m128i A0 = _mm_unpacklo_epi16(in0, in1);
const __m128i A1 = _mm_unpackhi_epi16(in0, in1);
const __m128i A2 = _mm_unpacklo_epi16(in2, in3);
const __m128i A3 = _mm_unpackhi_epi16(in2, in3);
const __m128i B0 = _mm_unpacklo_epi16(A0, A1); // r0 r1 r2 r3 | g0 g1 ..
const __m128i B1 = _mm_unpackhi_epi16(A0, A1); // b0 b1 b2 b3 | x x x x
const __m128i B2 = _mm_unpacklo_epi16(A2, A3); // r4 r5 r6 r7 | g4 g5 ..
const __m128i B3 = _mm_unpackhi_epi16(A2, A3); // b4 b5 b6 b7 | x x x x
*r = _mm_unpacklo_epi64(B0, B2);
*g = _mm_unpackhi_epi64(B0, B2);
*b = _mm_unpacklo_epi64(B1, B3);
}
static void ConvertRGBA32ToUV(const uint16_t* rgb,
uint8_t* u, uint8_t* v, int width) {
const int max_width = width & ~15;
const uint16_t* const last_rgb = rgb + 4 * max_width;
while (rgb < last_rgb) {
__m128i r, g, b, U0, V0, U1, V1;
RGBA32PackedToPlanar_16b(rgb + 0, &r, &g, &b);
ConvertRGBToUV(&r, &g, &b, &U0, &V0);
RGBA32PackedToPlanar_16b(rgb + 32, &r, &g, &b);
ConvertRGBToUV(&r, &g, &b, &U1, &V1);
STORE_16(_mm_packus_epi16(U0, U1), u);
STORE_16(_mm_packus_epi16(V0, V1), v);
u += 16;
v += 16;
rgb += 2 * 32;
}
if (max_width < width) { // left-over
WebPConvertRGBA32ToUV_C(rgb, u, v, width - max_width);
}
}
//------------------------------------------------------------------------------
extern void WebPInitConvertARGBToYUVSSE2(void);
WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUVSSE2(void) {
WebPConvertARGBToY = ConvertARGBToY;
WebPConvertARGBToUV = ConvertARGBToUV;
WebPConvertRGB24ToY = ConvertRGB24ToY;
WebPConvertBGR24ToY = ConvertBGR24ToY;
WebPConvertRGBA32ToUV = ConvertRGBA32ToUV;
}
#else // !WEBP_USE_SSE2
WEBP_DSP_INIT_STUB(WebPInitSamplersSSE2)
WEBP_DSP_INIT_STUB(WebPInitConvertARGBToYUVSSE2)
#endif // WEBP_USE_SSE2

View File

@ -768,23 +768,20 @@ static WEBP_INLINE void ConvertRowToY(const uint8_t* const r_ptr,
int width,
VP8Random* const rg) {
int i, j;
for (i = 0, j = 0; i < width; ++i, j += step) {
for (i = 0, j = 0; i < width; i += 1, j += step) {
dst_y[i] = RGBToY(r_ptr[j], g_ptr[j], b_ptr[j], rg);
}
}
static WEBP_INLINE void ConvertRowsToUVWithAlpha(const uint8_t* const r_ptr,
static WEBP_INLINE void AccumulateRGBA(const uint8_t* const r_ptr,
const uint8_t* const g_ptr,
const uint8_t* const b_ptr,
const uint8_t* const a_ptr,
int rgb_stride,
uint8_t* const dst_u,
uint8_t* const dst_v,
int width,
VP8Random* const rg) {
uint16_t* dst, int width) {
int i, j;
// we loop over 2x2 blocks and produce one U/V value for each.
for (i = 0, j = 0; i < (width >> 1); ++i, j += 2 * sizeof(uint32_t)) {
// we loop over 2x2 blocks and produce one R/G/B/A value for each.
for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * 4, dst += 4) {
const uint32_t a = SUM4ALPHA(a_ptr + j);
int r, g, b;
if (a == 4 * 0xff || a == 0) {
@ -796,8 +793,10 @@ static WEBP_INLINE void ConvertRowsToUVWithAlpha(const uint8_t* const r_ptr,
g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 4, rgb_stride);
b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 4, rgb_stride);
}
dst_u[i] = RGBToU(r, g, b, rg);
dst_v[i] = RGBToV(r, g, b, rg);
dst[0] = r;
dst[1] = g;
dst[2] = b;
dst[3] = a;
}
if (width & 1) {
const uint32_t a = 2u * SUM2ALPHA(a_ptr + j);
@ -811,31 +810,39 @@ static WEBP_INLINE void ConvertRowsToUVWithAlpha(const uint8_t* const r_ptr,
g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 0, rgb_stride);
b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 0, rgb_stride);
}
dst_u[i] = RGBToU(r, g, b, rg);
dst_v[i] = RGBToV(r, g, b, rg);
dst[0] = r;
dst[1] = g;
dst[2] = b;
dst[3] = a;
}
}
static WEBP_INLINE void ConvertRowsToUV(const uint8_t* const r_ptr,
static WEBP_INLINE void AccumulateRGB(const uint8_t* const r_ptr,
const uint8_t* const g_ptr,
const uint8_t* const b_ptr,
int step, int rgb_stride,
uint16_t* dst, int width) {
int i, j;
for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * step, dst += 4) {
dst[0] = SUM4(r_ptr + j, step);
dst[1] = SUM4(g_ptr + j, step);
dst[2] = SUM4(b_ptr + j, step);
}
if (width & 1) {
dst[0] = SUM2(r_ptr + j);
dst[1] = SUM2(g_ptr + j);
dst[2] = SUM2(b_ptr + j);
}
}
static WEBP_INLINE void ConvertRowsToUV(const uint16_t* rgb,
uint8_t* const dst_u,
uint8_t* const dst_v,
int width,
VP8Random* const rg) {
int i, j;
for (i = 0, j = 0; i < (width >> 1); ++i, j += 2 * step) {
const int r = SUM4(r_ptr + j, step);
const int g = SUM4(g_ptr + j, step);
const int b = SUM4(b_ptr + j, step);
dst_u[i] = RGBToU(r, g, b, rg);
dst_v[i] = RGBToV(r, g, b, rg);
}
if (width & 1) {
const int r = SUM2(r_ptr + j);
const int g = SUM2(g_ptr + j);
const int b = SUM2(b_ptr + j);
int i;
for (i = 0; i < width; i += 1, rgb += 4) {
const int r = rgb[0], g = rgb[1], b = rgb[2];
dst_u[i] = RGBToU(r, g, b, rg);
dst_v[i] = RGBToV(r, g, b, rg);
}
@ -854,6 +861,7 @@ static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
const int width = picture->width;
const int height = picture->height;
const int has_alpha = CheckNonOpaque(a_ptr, width, height, step, rgb_stride);
const int is_rgb = (r_ptr < b_ptr); // otherwise it's bgr
picture->colorspace = has_alpha ? WEBP_YUV420A : WEBP_YUV420;
picture->use_argb = 0;
@ -885,7 +893,11 @@ static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
picture->a, picture->a_stride);
}
} else {
const int uv_width = (width + 1) >> 1;
int use_dsp = (step == 3); // use special function in this case
// temporary storage for accumulated R/G/B values during conversion to U/V
uint16_t* const tmp_rgb =
(uint16_t*)WebPSafeMalloc(4 * uv_width, sizeof(*tmp_rgb));
uint8_t* dst_y = picture->y;
uint8_t* dst_u = picture->u;
uint8_t* dst_v = picture->v;
@ -898,17 +910,18 @@ static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
rg = &base_rg;
use_dsp = 0; // can't use dsp in this case
}
if (use_dsp) WebPInitConvertARGBToYUV();
WebPInitConvertARGBToYUV();
InitGammaTables();
if (tmp_rgb == NULL) return 0; // malloc error
// Downsample Y/U/V planes, two rows at a time
for (y = 0; y < (height >> 1); ++y) {
int rows_have_alpha = has_alpha;
const int off1 = (2 * y + 0) * rgb_stride;
const int off2 = (2 * y + 1) * rgb_stride;
if (use_dsp) {
if (r_ptr < b_ptr) {
if (is_rgb) {
WebPConvertRGB24ToY(r_ptr + off1, dst_y, width);
WebPConvertRGB24ToY(r_ptr + off2, dst_y + picture->y_stride, width);
} else {
@ -928,13 +941,19 @@ static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
dst_a, picture->a_stride);
dst_a += 2 * picture->a_stride;
}
// Collect averaged R/G/B(/A)
if (!rows_have_alpha) {
ConvertRowsToUV(r_ptr + off1, g_ptr + off1, b_ptr + off1,
step, rgb_stride, dst_u, dst_v, width, rg);
AccumulateRGB(r_ptr + off1, g_ptr + off1, b_ptr + off1,
step, rgb_stride, tmp_rgb, width);
} else {
ConvertRowsToUVWithAlpha(r_ptr + off1, g_ptr + off1, b_ptr + off1,
a_ptr + off1, rgb_stride,
dst_u, dst_v, width, rg);
AccumulateRGBA(r_ptr + off1, g_ptr + off1, b_ptr + off1, a_ptr + off1,
rgb_stride, tmp_rgb, width);
}
// Convert to U/V
if (rg == NULL) {
WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);
} else {
ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);
}
dst_u += picture->uv_stride;
dst_v += picture->uv_stride;
@ -955,15 +974,22 @@ static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
if (row_has_alpha) {
row_has_alpha &= !WebPExtractAlpha(a_ptr + off, 0, width, 1, dst_a, 0);
}
// Collect averaged R/G/B(/A)
if (!row_has_alpha) {
ConvertRowsToUV(r_ptr + off, g_ptr + off, b_ptr + off,
step, 0, dst_u, dst_v, width, rg);
// Collect averaged R/G/B
AccumulateRGB(r_ptr + off, g_ptr + off, b_ptr + off,
step, /* rgb_stride = */ 0, tmp_rgb, width);
} else {
ConvertRowsToUVWithAlpha(r_ptr + off, g_ptr + off, b_ptr + off,
a_ptr + off, 0,
dst_u, dst_v, width, rg);
AccumulateRGBA(r_ptr + off, g_ptr + off, b_ptr + off, a_ptr + off,
/* rgb_stride = */ 0, tmp_rgb, width);
}
if (rg == NULL) {
WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);
} else {
ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);
}
}
WebPSafeFree(tmp_rgb);
}
return 1;
}