From f0f9eda40653b937b14a90a9ababbcaa36cd5684 Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 11 Aug 2022 17:18:49 -0700 Subject: [PATCH 1/6] sharpyuv.h: remove this hasn't been needed since: 93c54371 sharpyuv: add support for 10/12/16 bit rgb and 10/12 bit yuv. Change-Id: I02051e8b576d7fd27824e13be0d087dc4e18209e --- sharpyuv/sharpyuv.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/sharpyuv/sharpyuv.h b/sharpyuv/sharpyuv.h index 7ce92f9b..9bfd5fe6 100644 --- a/sharpyuv/sharpyuv.h +++ b/sharpyuv/sharpyuv.h @@ -12,8 +12,6 @@ #ifndef WEBP_SHARPYUV_SHARPYUV_H_ #define WEBP_SHARPYUV_SHARPYUV_H_ -#include - #ifdef __cplusplus extern "C" { #endif From ef70ee06fa47d34835995f46dd6160170fed106b Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 11 Aug 2022 17:39:48 -0700 Subject: [PATCH 2/6] add a few missing includes for NULL and remove unused includes in sharpyuv/ Change-Id: If10538a994bd5dc55126f1485f2b163933ad8e91 --- sharpyuv/sharpyuv.c | 2 +- sharpyuv/sharpyuv_csp.c | 2 +- src/dsp/cpu.h | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sharpyuv/sharpyuv.c b/sharpyuv/sharpyuv.c index 6497fb02..3fc8496c 100644 --- a/sharpyuv/sharpyuv.c +++ b/sharpyuv/sharpyuv.c @@ -15,7 +15,7 @@ #include #include -#include +#include #include #include diff --git a/sharpyuv/sharpyuv_csp.c b/sharpyuv/sharpyuv_csp.c index 5334fa64..0ad22be9 100644 --- a/sharpyuv/sharpyuv_csp.c +++ b/sharpyuv/sharpyuv_csp.c @@ -13,7 +13,7 @@ #include #include -#include +#include static int ToFixed16(float f) { return (int)floor(f * (1 << 16) + 0.5f); } diff --git a/src/dsp/cpu.h b/src/dsp/cpu.h index 57a40d87..be80727c 100644 --- a/src/dsp/cpu.h +++ b/src/dsp/cpu.h @@ -14,6 +14,8 @@ #ifndef WEBP_DSP_CPU_H_ #define WEBP_DSP_CPU_H_ +#include + #ifdef HAVE_CONFIG_H #include "src/webp/config.h" #endif From cad0d5adb6de76487139d09fdcdde74b0c1e9984 Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 11 Aug 2022 17:48:11 -0700 Subject: [PATCH 3/6] sharyuv_{neon,sse2}.c: merge WEBP_USE_* sections Change-Id: I686e6740717902d632fb01f9151e47fd3cb2cf79 --- sharpyuv/sharpyuv_neon.c | 9 ++++----- sharpyuv/sharpyuv_sse2.c | 7 ++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/sharpyuv/sharpyuv_neon.c b/sharpyuv/sharpyuv_neon.c index 5cf6aaff..58409148 100644 --- a/sharpyuv/sharpyuv_neon.c +++ b/sharpyuv/sharpyuv_neon.c @@ -17,11 +17,6 @@ #include #include #include -#endif - -extern void InitSharpYuvNEON(void); - -#if defined(WEBP_USE_NEON) static uint16_t clip_NEON(int v, int max) { return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v; @@ -169,6 +164,8 @@ static void SharpYuvFilterRow_NEON(const int16_t* A, const int16_t* B, int len, //------------------------------------------------------------------------------ +extern void InitSharpYuvNEON(void); + WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvNEON(void) { SharpYuvUpdateY = SharpYuvUpdateY_NEON; SharpYuvUpdateRGB = SharpYuvUpdateRGB_NEON; @@ -177,6 +174,8 @@ WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvNEON(void) { #else // !WEBP_USE_NEON +extern void InitSharpYuvNEON(void); + void InitSharpYuvNEON(void) {} #endif // WEBP_USE_NEON diff --git a/sharpyuv/sharpyuv_sse2.c b/sharpyuv/sharpyuv_sse2.c index 19438737..9744d1bb 100644 --- a/sharpyuv/sharpyuv_sse2.c +++ b/sharpyuv/sharpyuv_sse2.c @@ -16,11 +16,6 @@ #if defined(WEBP_USE_SSE2) #include #include -#endif - -extern void InitSharpYuvSSE2(void); - -#if defined(WEBP_USE_SSE2) static uint16_t clip_SSE2(int v, int max) { return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v; @@ -199,6 +194,8 @@ WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvSSE2(void) { } #else // !WEBP_USE_SSE2 +extern void InitSharpYuvSSE2(void); + void InitSharpYuvSSE2(void) {} #endif // WEBP_USE_SSE2 From 782ed48cd314b3b09232ea2bf3b54df99f155046 Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 11 Aug 2022 18:58:40 -0700 Subject: [PATCH 4/6] sharpyuv,SharpYuvInit: add mutex protection when available this is similar to WEBP_DSP_INIT and avoids the potential for a race in initializing the function pointers and gamma table Change-Id: I15af7d1a3e92c42eaa735751ca33eba0d1e83283 --- sharpyuv/sharpyuv.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/sharpyuv/sharpyuv.c b/sharpyuv/sharpyuv.c index 3fc8496c..5ff02225 100644 --- a/sharpyuv/sharpyuv.c +++ b/sharpyuv/sharpyuv.c @@ -414,6 +414,22 @@ static int DoSharpArgbToYuv(const uint8_t* r_ptr, const uint8_t* g_ptr, } #undef SAFE_ALLOC +#if defined(WEBP_USE_THREAD) && !defined(_WIN32) +#include // NOLINT + +#define LOCK_ACCESS \ + static pthread_mutex_t sharpyuv_lock = PTHREAD_MUTEX_INITIALIZER; \ + if (pthread_mutex_lock(&sharpyuv_lock)) return +#define UNLOCK_ACCESS_AND_RETURN \ + do { \ + (void)pthread_mutex_unlock(&sharpyuv_lock); \ + return; \ + } while (0) +#else // !(defined(WEBP_USE_THREAD) && !defined(_WIN32)) +#define LOCK_ACCESS do {} while (0) +#define UNLOCK_ACCESS_AND_RETURN return +#endif // defined(WEBP_USE_THREAD) && !defined(_WIN32) + // Hidden exported init function. // By default SharpYuvConvert calls it with NULL. If needed, users can declare // it as extern and call it with a VP8CPUInfo function. @@ -421,17 +437,21 @@ SHARPYUV_EXTERN void SharpYuvInit(VP8CPUInfo cpu_info_func); void SharpYuvInit(VP8CPUInfo cpu_info_func) { static volatile VP8CPUInfo sharpyuv_last_cpuinfo_used = (VP8CPUInfo)&sharpyuv_last_cpuinfo_used; - const int initialized = + LOCK_ACCESS; + { + const int initialized = (sharpyuv_last_cpuinfo_used != (VP8CPUInfo)&sharpyuv_last_cpuinfo_used); - if (cpu_info_func == NULL && initialized) return; - if (sharpyuv_last_cpuinfo_used == cpu_info_func) return; + if (cpu_info_func == NULL && initialized) UNLOCK_ACCESS_AND_RETURN; + if (sharpyuv_last_cpuinfo_used == cpu_info_func) UNLOCK_ACCESS_AND_RETURN; - SharpYuvInitDsp(cpu_info_func); - if (!initialized) { - SharpYuvInitGammaTables(); + SharpYuvInitDsp(cpu_info_func); + if (!initialized) { + SharpYuvInitGammaTables(); + } + + sharpyuv_last_cpuinfo_used = cpu_info_func; } - - sharpyuv_last_cpuinfo_used = cpu_info_func; + UNLOCK_ACCESS_AND_RETURN; } int SharpYuvConvert(const void* r_ptr, const void* g_ptr, From 6af8845a8fe872940d38226ae4c3ba5cb43a9fe1 Mon Sep 17 00:00:00 2001 From: James Zern Date: Fri, 12 Aug 2022 10:55:39 -0700 Subject: [PATCH 5/6] sharpyuv: prefer webp/types.h this provides better compatibility for older Visual Studio versions; stdint.h is a part of C99. this is consistent with the core libwebp library: 8fbb9188 prefer webp/types.h over stdint.h Change-Id: I777cad70d9a407646c4193db9ca0f09e65d88735 --- sharpyuv/Makefile.am | 1 + sharpyuv/sharpyuv_dsp.h | 3 +-- sharpyuv/sharpyuv_gamma.c | 1 - sharpyuv/sharpyuv_gamma.h | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/sharpyuv/Makefile.am b/sharpyuv/Makefile.am index eda07187..ac5862b6 100644 --- a/sharpyuv/Makefile.am +++ b/sharpyuv/Makefile.am @@ -12,6 +12,7 @@ libsharpyuvinclude_HEADERS += sharpyuv.h libsharpyuvinclude_HEADERS += sharpyuv_csp.h noinst_HEADERS = noinst_HEADERS += ../src/dsp/cpu.h +noinst_HEADERS += ../src/webp/types.h libsharpyuv_sse2_la_SOURCES = libsharpyuv_sse2_la_SOURCES += sharpyuv_sse2.c diff --git a/sharpyuv/sharpyuv_dsp.h b/sharpyuv/sharpyuv_dsp.h index e561d8d3..c7755b97 100644 --- a/sharpyuv/sharpyuv_dsp.h +++ b/sharpyuv/sharpyuv_dsp.h @@ -12,9 +12,8 @@ #ifndef WEBP_SHARPYUV_SHARPYUV_DSP_H_ #define WEBP_SHARPYUV_SHARPYUV_DSP_H_ -#include - #include "src/dsp/cpu.h" +#include "src/webp/types.h" extern uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref, uint16_t* dst, int len, int bit_depth); diff --git a/sharpyuv/sharpyuv_gamma.c b/sharpyuv/sharpyuv_gamma.c index 05b5436f..20ab2da6 100644 --- a/sharpyuv/sharpyuv_gamma.c +++ b/sharpyuv/sharpyuv_gamma.c @@ -13,7 +13,6 @@ #include #include -#include #include "src/webp/types.h" diff --git a/sharpyuv/sharpyuv_gamma.h b/sharpyuv/sharpyuv_gamma.h index 2f1a3ff4..d13aff59 100644 --- a/sharpyuv/sharpyuv_gamma.h +++ b/sharpyuv/sharpyuv_gamma.h @@ -12,7 +12,7 @@ #ifndef WEBP_SHARPYUV_SHARPYUV_GAMMA_H_ #define WEBP_SHARPYUV_SHARPYUV_GAMMA_H_ -#include +#include "src/webp/types.h" #ifdef __cplusplus extern "C" { From e7c805cfadb139222789f9b8366f2086bab86576 Mon Sep 17 00:00:00 2001 From: James Zern Date: Fri, 12 Aug 2022 11:05:57 -0700 Subject: [PATCH 6/6] picture_csp_enc.c: remove SafeInitSharpYuv thread safety was internalized in: 782ed48c sharpyuv,SharpYuvInit: add mutex protection when available Change-Id: I2877e7bb78bf89ad7504c33d04b22952c0c0a968 --- src/enc/picture_csp_enc.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/enc/picture_csp_enc.c b/src/enc/picture_csp_enc.c index 8a3a86c4..78c8ca47 100644 --- a/src/enc/picture_csp_enc.c +++ b/src/enc/picture_csp_enc.c @@ -172,21 +172,6 @@ static const int kMinDimensionIterativeConversion = 4; //------------------------------------------------------------------------------ // Main function -extern void SharpYuvInit(VP8CPUInfo cpu_info_func); - -static void SafeInitSharpYuv(void) { -#if defined(WEBP_USE_THREAD) && !defined(_WIN32) - static pthread_mutex_t initsharpyuv_lock = PTHREAD_MUTEX_INITIALIZER; - if (pthread_mutex_lock(&initsharpyuv_lock)) return; -#endif - - SharpYuvInit(VP8GetCPUInfo); - -#if defined(WEBP_USE_THREAD) && !defined(_WIN32) - (void)pthread_mutex_unlock(&initsharpyuv_lock); -#endif -} - static int PreprocessARGB(const uint8_t* r_ptr, const uint8_t* g_ptr, const uint8_t* b_ptr, @@ -483,6 +468,8 @@ static WEBP_INLINE void ConvertRowsToUV(const uint16_t* rgb, } } +extern void SharpYuvInit(VP8CPUInfo cpu_info_func); + static int ImportYUVAFromRGBA(const uint8_t* r_ptr, const uint8_t* g_ptr, const uint8_t* b_ptr, @@ -518,7 +505,7 @@ static int ImportYUVAFromRGBA(const uint8_t* r_ptr, } if (use_iterative_conversion) { - SafeInitSharpYuv(); + SharpYuvInit(VP8GetCPUInfo); if (!PreprocessARGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, picture)) { return 0; }