From ffaea7eee38e593a3e63553ffa90e554ba81fe30 Mon Sep 17 00:00:00 2001 From: "fbarchard@google.com" Date: Fri, 18 May 2012 19:43:59 +0000 Subject: [PATCH] ARGBGray function for convertion a rectangle of ARGB to gray scale BUG=none TEST=none Review URL: https://webrtc-codereview.appspot.com/582007 git-svn-id: http://libyuv.googlecode.com/svn/trunk@269 16f28f9a-4ce2-e073-06de-1de4eb20be90 --- README.chromium | 2 +- include/libyuv/planar_functions.h | 5 +++ include/libyuv/scale_argb.h | 8 +---- include/libyuv/version.h | 2 +- source/planar_functions.cc | 22 +++++++++++++ source/row.h | 4 +++ source/row_common.cc | 15 +++++++++ source/row_posix.cc | 52 ++++++++++++++++++++++++++++++ source/row_win.cc | 50 ++++++++++++++++++++++++++++- unit_test/planar_test.cc | 53 +++++++++++++++++++++++++++++++ 10 files changed, 203 insertions(+), 10 deletions(-) diff --git a/README.chromium b/README.chromium index 49b8465ce..27dbfd4db 100644 --- a/README.chromium +++ b/README.chromium @@ -1,6 +1,6 @@ Name: libyuv URL: http://code.google.com/p/libyuv/ -Version: 268 +Version: 269 License: BSD License File: LICENSE diff --git a/include/libyuv/planar_functions.h b/include/libyuv/planar_functions.h index 6f8a9208a..b30ca3e2b 100644 --- a/include/libyuv/planar_functions.h +++ b/include/libyuv/planar_functions.h @@ -133,6 +133,11 @@ int ARGBRect(uint8* dst_argb, int dst_stride_argb, int width, int height, uint32 value); +// Make a rectangle of ARGB gray scale. +int ARGBGray(uint8* dst_argb, int dst_stride_argb, + int x, int y, + int width, int height); + // Copy ARGB to ARGB. int ARGBCopy(const uint8* src_argb, int src_stride_argb, uint8* dst_argb, int dst_stride_argb, diff --git a/include/libyuv/scale_argb.h b/include/libyuv/scale_argb.h index 225f77258..958ac96a3 100644 --- a/include/libyuv/scale_argb.h +++ b/include/libyuv/scale_argb.h @@ -12,19 +12,13 @@ #define INCLUDE_LIBYUV_SCALE_ARGB_H_ #include "libyuv/basic_types.h" +#include "libyuv/scale.h" // For FilterMode #ifdef __cplusplus namespace libyuv { extern "C" { #endif -// Supported filtering -enum FilterMode { - kFilterNone = 0, // Point sample; Fastest - kFilterBilinear = 1, // Faster than box, but lower quality scaling down. - kFilterBox = 2 // Highest quality (not supported for ARGB) -}; - int ARGBScale(const uint8* src_argb, int src_stride_argb, int src_width, int src_height, uint8* dst_argb, int dst_stride_argb, diff --git a/include/libyuv/version.h b/include/libyuv/version.h index 287e00f99..8a5df622f 100644 --- a/include/libyuv/version.h +++ b/include/libyuv/version.h @@ -11,7 +11,7 @@ #ifndef INCLUDE_LIBYUV_VERSION_H_ #define INCLUDE_LIBYUV_VERSION_H_ -#define LIBYUV_VERSION 268 +#define LIBYUV_VERSION 269 #endif // INCLUDE_LIBYUV_VERSION_H_ diff --git a/source/planar_functions.cc b/source/planar_functions.cc index 8efe68508..d2791ca94 100644 --- a/source/planar_functions.cc +++ b/source/planar_functions.cc @@ -1037,6 +1037,28 @@ AddRow GetSubRow(uint16* dst, int width) { return SubRowF; } +// Make a rectangle of ARGB gray scale. +int ARGBGray(uint8* dst_argb, int dst_stride_argb, + int dst_x, int dst_y, + int width, int height) { + if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) { + return -1; + } + void (*ARGBGrayRow)(uint8* dst_argb, int width) = ARGBGrayRow_C; +#if defined(HAS_ARGBGRAYROW_SSSE3) + if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) && + IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) { + ARGBGrayRow = ARGBGrayRow_SSSE3; + } +#endif + uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4; + for (int y = 0; y < height; ++y) { + ARGBGrayRow(dst, width); + dst += dst_stride_argb; + } + return 0; +} + #ifdef __cplusplus } // extern "C" } // namespace libyuv diff --git a/source/row.h b/source/row.h index 012815f93..d8604f745 100644 --- a/source/row.h +++ b/source/row.h @@ -71,6 +71,7 @@ extern "C" { #define HAS_YTOARGBROW_SSE2 #define HAS_YUY2TOUVROW_SSE2 #define HAS_YUY2TOYROW_SSE2 +#define HAS_ARGBGRAYROW_SSSE3 #endif // The following are available only useful when SSSE3 is unavailable. @@ -399,6 +400,9 @@ extern uint32 fixed_invtbl8[256]; void ARGBUnattenuateRow_C(const uint8* src_argb, uint8* dst_argb, int width); void ARGBUnattenuateRow_SSE2(const uint8* src_argb, uint8* dst_argb, int width); +void ARGBGrayRow_C(uint8* dst_argb, int width); +void ARGBGrayRow_SSSE3(uint8* dst_argb, int width); + #ifdef __cplusplus } // extern "C" } // namespace libyuv diff --git a/source/row_common.cc b/source/row_common.cc index cb3b31d96..45fe9f8af 100644 --- a/source/row_common.cc +++ b/source/row_common.cc @@ -234,6 +234,21 @@ MAKEROWY(ARGB, 2, 1, 0) MAKEROWY(BGRA, 1, 2, 3) MAKEROWY(ABGR, 0, 1, 2) +// http://en.wikipedia.org/wiki/Grayscale. +// 0.11 * B + 0.59 * G + 0.30 * R +// Coefficients rounded to multiple of 2 for consistency with SSSE3 version. +static __inline int RGBToGray(uint8 r, uint8 g, uint8 b) { + return (( 76 * r + 152 * g + 28 * b) >> 8); +} + +void ARGBGrayRow_C(uint8* dst_argb, int width) { + for (int x = 0; x < width; ++x) { + uint8 y = RGBToGray(dst_argb[2], dst_argb[1], dst_argb[0]); + dst_argb[2] = dst_argb[1] = dst_argb[0] = y; + dst_argb += 4; + } +} + void I400ToARGBRow_C(const uint8* src_y, uint8* dst_argb, int width) { // Copy a Y to RGB. for (int x = 0; x < width; ++x) { diff --git a/source/row_posix.cc b/source/row_posix.cc index 9d9dbc507..340dd651f 100644 --- a/source/row_posix.cc +++ b/source/row_posix.cc @@ -108,6 +108,11 @@ CONST uvec8 kShuffleMaskARGBToRAW = { 2u, 1u, 0u, 6u, 5u, 4u, 10u, 9u, 8u, 14u, 13u, 12u, 128u, 128u, 128u, 128u }; +// Constant for ARGB color to gray scale. 0.11 * B + 0.59 * G + 0.30 * R +CONST vec8 kARGBToGray = { + 14, 76, 38, 0, 14, 76, 38, 0, 14, 76, 38, 0, 14, 76, 38, 0 +}; + void I400ToARGBRow_SSE2(const uint8* src_y, uint8* dst_argb, int pix) { asm volatile ( "pcmpeqb %%xmm5,%%xmm5 \n" @@ -2527,6 +2532,53 @@ void ARGBUnattenuateRow_SSE2(const uint8* src_argb, uint8* dst_argb, } #endif // HAS_ARGBUNATTENUATE_SSE2 +#ifdef HAS_ARGBGRAYROW_SSSE3 +// Convert 8 ARGB pixels (64 bytes) to 8 Gray ARGB pixels +void ARGBGrayRow_SSSE3(uint8* dst_argb, int width) { + asm volatile ( + "movdqa %2,%%xmm4 \n" + "pcmpeqb %%xmm5,%%xmm5 \n" + "pslld $0x18,%%xmm5 \n" + "pcmpeqb %%xmm3,%%xmm3 \n" + "psrld $0x8,%%xmm3 \n" + + // 8 pixel loop \n" + ".p2align 4 \n" + "1: \n" + "movdqa (%0),%%xmm0 \n" + "movdqa 0x10(%0),%%xmm1 \n" + "pmaddubsw %%xmm4,%%xmm0 \n" + "pmaddubsw %%xmm4,%%xmm1 \n" + "movdqa (%0),%%xmm6 \n" + "movdqa 0x10(%0),%%xmm7 \n" + "pand %%xmm5,%%xmm6 \n" + "pand %%xmm5,%%xmm7 \n" + "phaddw %%xmm1,%%xmm0 \n" + "psrlw $0x7,%%xmm0 \n" + "packuswb %%xmm0,%%xmm0 \n" + "punpcklbw %%xmm0,%%xmm0 \n" + "movdqa %%xmm0,%%xmm1 \n" + "punpcklwd %%xmm0,%%xmm0 \n" + "punpckhwd %%xmm1,%%xmm1 \n" + "pand %%xmm3,%%xmm0 \n" + "pand %%xmm3,%%xmm1 \n" + "por %%xmm6,%%xmm0 \n" + "por %%xmm7,%%xmm1 \n" + "sub $0x8,%1 \n" + "movdqa %%xmm0,(%0) \n" + "movdqa %%xmm1,0x10(%0) \n" + "lea 0x20(%0),%0 \n" + "jg 1b \n" + : "+r"(dst_argb), // %0 + "+r"(width) // %1 + : "m"(kARGBToGray) // %2 + : "memory", "cc" +#if defined(__SSE2__) + , "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7" +#endif + ); +} +#endif // HAS_ARGBGRAYROW_SSSE3 #endif // defined(__x86_64__) || defined(__i386__) #ifdef __cplusplus diff --git a/source/row_win.cc b/source/row_win.cc index d4d5d8661..eb49ac46c 100644 --- a/source/row_win.cc +++ b/source/row_win.cc @@ -93,12 +93,16 @@ static const uvec8 kShuffleMaskARGBToRGB24 = { 0u, 1u, 2u, 4u, 5u, 6u, 8u, 9u, 10u, 12u, 13u, 14u, 128u, 128u, 128u, 128u }; - // Shuffle table for converting ARGB to RAW. static const uvec8 kShuffleMaskARGBToRAW = { 2u, 1u, 0u, 6u, 5u, 4u, 10u, 9u, 8u, 14u, 13u, 12u, 128u, 128u, 128u, 128u }; +// Constant for ARGB color to gray scale. 0.11 * B + 0.59 * G + 0.30 * R +static const vec8 kARGBToGray = { + 14, 76, 38, 0, 14, 76, 38, 0, 14, 76, 38, 0, 14, 76, 38, 0 +}; + __declspec(naked) __declspec(align(16)) void I400ToARGBRow_SSE2(const uint8* src_y, uint8* dst_argb, int pix) { __asm { @@ -2553,6 +2557,50 @@ void ARGBUnattenuateRow_SSE2(const uint8* src_argb, uint8* dst_argb, } #endif // HAS_ARGBUNATTENUATE_SSE2 +#ifdef HAS_ARGBGRAYROW_SSSE3 +// Convert 8 ARGB pixels (64 bytes) to 8 Gray ARGB pixels +__declspec(naked) __declspec(align(16)) +void ARGBGrayRow_SSSE3(uint8* dst_argb, int width) { + __asm { + mov eax, [esp + 4] /* dst_argb */ + mov ecx, [esp + 8] /* width */ + movdqa xmm4, kARGBToGray + pcmpeqb xmm5, xmm5 // generate mask 0xff000000 + pslld xmm5, 24 + pcmpeqb xmm3, xmm3 // generate mask 0x00ffffff + psrld xmm3, 8 + + align 16 + convertloop: + movdqa xmm0, [eax] + movdqa xmm1, [eax + 16] + pmaddubsw xmm0, xmm4 + pmaddubsw xmm1, xmm4 + movdqa xmm6, [eax] // preserve alpha + movdqa xmm7, [eax + 16] + pand xmm6, xmm5 + pand xmm7, xmm5 + phaddw xmm0, xmm1 + psrlw xmm0, 7 + packuswb xmm0, xmm0 // 8 Y values + + punpcklbw xmm0, xmm0 + movdqa xmm1, xmm0 + punpcklwd xmm0, xmm0 + punpckhwd xmm1, xmm1 + pand xmm0, xmm3 // mask in alpha + pand xmm1, xmm3 + por xmm0, xmm6 + por xmm1, xmm7 + sub ecx, 8 + movdqa [eax], xmm0 + movdqa [eax + 16], xmm1 + lea eax, [eax + 32] + jg convertloop + ret + } +} +#endif // HAS_ARGBGRAYROW_SSSE3 #endif // _M_IX86 #ifdef __cplusplus diff --git a/unit_test/planar_test.cc b/unit_test/planar_test.cc index cb9f736c4..86ada07f8 100644 --- a/unit_test/planar_test.cc +++ b/unit_test/planar_test.cc @@ -220,4 +220,57 @@ TEST_F(libyuvTest, TestAddRow) { AddRow(orig_pixels, added_pixels, 256); } } + +TEST_F(libyuvTest, TestARGBGray) { + SIMD_ALIGNED(uint8 orig_pixels[256][4]); + + // Test blue + orig_pixels[0][0] = 255u; + orig_pixels[0][1] = 0u; + orig_pixels[0][2] = 0u; + orig_pixels[0][3] = 128u; + // Test green + orig_pixels[1][0] = 0u; + orig_pixels[1][1] = 255u; + orig_pixels[1][2] = 0u; + orig_pixels[1][3] = 0u; + // Test red + orig_pixels[2][0] = 0u; + orig_pixels[2][1] = 0u; + orig_pixels[2][2] = 255u; + orig_pixels[2][3] = 255u; + // Test color + orig_pixels[3][0] = 16u; + orig_pixels[3][1] = 64u; + orig_pixels[3][2] = 192u; + orig_pixels[3][3] = 224u; + ARGBGray(&orig_pixels[0][0], 0, 0, 0, 4, 1); + EXPECT_EQ(27u, orig_pixels[0][0]); + EXPECT_EQ(27u, orig_pixels[0][1]); + EXPECT_EQ(27u, orig_pixels[0][2]); + EXPECT_EQ(128u, orig_pixels[0][3]); + EXPECT_EQ(151u, orig_pixels[1][0]); + EXPECT_EQ(151u, orig_pixels[1][1]); + EXPECT_EQ(151u, orig_pixels[1][2]); + EXPECT_EQ(0u, orig_pixels[1][3]); + EXPECT_EQ(75u, orig_pixels[2][0]); + EXPECT_EQ(75u, orig_pixels[2][1]); + EXPECT_EQ(75u, orig_pixels[2][2]); + EXPECT_EQ(255u, orig_pixels[2][3]); + EXPECT_EQ(96u, orig_pixels[3][0]); + EXPECT_EQ(96u, orig_pixels[3][1]); + EXPECT_EQ(96u, orig_pixels[3][2]); + EXPECT_EQ(224u, orig_pixels[3][3]); + + for (int i = 0; i < 256; ++i) { + orig_pixels[i][0] = i; + orig_pixels[i][1] = i / 2; + orig_pixels[i][2] = i / 3; + orig_pixels[i][3] = i; + } + + for (int i = 0; i < 1000 * 1280 * 720 / 256; ++i) { + ARGBGray(&orig_pixels[0][0], 0, 0, 0, 256, 1); + } } +} // namespace libyuv