SDL_image: webp needs to load symbols from libwebp and libwebpdemux

From 778889fdf89c2de2ce6600607738faeebae89f28 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Tue, 1 Nov 2022 17:13:52 +0100
Subject: [PATCH] webp needs to load symbols from libwebp and libwebpdemux

---
 CMakeLists.txt       |  17 ++-
 IMG_webp.c           |  54 ++++++----
 Makefile.in          |   2 +
 cmake/Findwebp.cmake |  26 ++++-
 configure            | 249 ++++++++++++++++++++++++++++++++++++++-----
 configure.ac         |  46 +++++---
 test/Makefile.in     |   2 +
 7 files changed, 328 insertions(+), 68 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e89d0a63..87f2b767 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -622,7 +622,8 @@ if(SDL2IMAGE_WEBP)
         add_subdirectory(external/libwebp EXCLUDE_FROM_ALL)
         target_include_directories(SDL2_image PRIVATE external/libwebp/src)
         add_library(WebP::webp ALIAS webp)
-        list(APPEND INSTALL_EXTRA_TARGETS webp)
+        add_library(WebP::webpdemux ALIAS webpdemux)
+        list(APPEND INSTALL_EXTRA_TARGETS webp webpdemux)
         set_target_properties(webp PROPERTIES EXPORT_NAME "external_libwebp")
         add_library(SDL2_image::external_libwebp ALIAS webp)
     else()
@@ -635,15 +636,21 @@ if(SDL2IMAGE_WEBP)
             $<TARGET_PROPERTY:WebP::webp,INCLUDE_DIRECTORIES>
             $<TARGET_PROPERTY:WebP::webp,INTERFACE_INCLUDE_DIRECTORIES>
             $<TARGET_PROPERTY:WebP::webp,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
+            $<TARGET_PROPERTY:WebP::webpdemux,INCLUDE_DIRECTORIES>
+            $<TARGET_PROPERTY:WebP::webpdemux,INTERFACE_INCLUDE_DIRECTORIES>
+            $<TARGET_PROPERTY:WebP::webpdemux,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
         )
-        target_get_dynamic_library(dynamic_webp WebP::webp)
-        message(STATUS "Dynamic libwebp: ${dynamic_webp}")
+        target_get_dynamic_library(dynamic_webpdemux WebP::webpdemux)
+        message(STATUS "Dynamic libwebpdemux: ${dynamic_webpdemux}")
+        target_compile_definitions(SDL2_image PRIVATE "LOAD_WEBPDEMUX_DYNAMIC=\"${dynamic_webpdemux}\"")
+        target_get_dynamic_library(dynamic_webpdemux WebP::webp)
+        message(STATUS "Dynamic libwebpdemux: ${dynamic_webp}")
         target_compile_definitions(SDL2_image PRIVATE "LOAD_WEBP_DYNAMIC=\"${dynamic_webp}\"")
         if(SDL2IMAGE_WEBP_VENDORED)
-            add_dependencies(SDL2_image WebP::webp)
+            add_dependencies(SDL2_image WebP::webp WebP::webpdemux)
         endif()
     else()
-        target_link_libraries(SDL2_image PRIVATE WebP::webp)
+        target_link_libraries(SDL2_image PRIVATE WebP::webp WebP::webpdemux)
     endif()
 endif()
 
diff --git a/IMG_webp.c b/IMG_webp.c
index e881501a..2905535e 100644
--- a/IMG_webp.c
+++ b/IMG_webp.c
@@ -44,7 +44,8 @@
 
 static struct {
     int loaded;
-    void *handle;
+    void *handle_libwebpdemux;
+    void *handle_libwebp;
 #if WEBP_DECODER_ABI_VERSION < 0x0100
     VP8StatusCode (*WebPGetFeaturesInternal) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version);
     uint8_t*    (*WebPDecodeRGBInto) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
@@ -61,38 +62,48 @@ static struct {
     void (*WebPDemuxDelete)(WebPDemuxer* dmux);
 } lib;
 
-#ifdef LOAD_WEBP_DYNAMIC
-#define FUNCTION_LOADER(FUNC, SIG) \
-    lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
-    if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
+#if defined(LOAD_WEBP_DYNAMIC) && defined(LOAD_WEBPDEMUX_DYNAMIC)
+#define FUNCTION_LOADER_LIBWEBP(FUNC, SIG) \
+    lib.FUNC = (SIG) SDL_LoadFunction(lib.handle_libwebp, #FUNC); \
+    if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle_libwebp); return -1; }
+#define FUNCTION_LOADER_LIBWEBPDEMUX(FUNC, SIG) \
+    lib.FUNC = (SIG) SDL_LoadFunction(lib.handle_libwebpdemux, #FUNC); \
+    if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle_libwebpdemux); return -1; }
 #else
-#define FUNCTION_LOADER(FUNC, SIG) \
+#define FUNCTION_LOADER_LIBWEBP(FUNC, SIG) \
     lib.FUNC = FUNC; \
     if (lib.FUNC == NULL) { IMG_SetError("Missing webp.framework"); return -1; }
+#define FUNCTION_LOADER_LIBWEBPDEMUX(FUNC, SIG) \
+    lib.FUNC = FUNC; \
+    if (lib.FUNC == NULL) { IMG_SetError("Missing webpdemux.framework"); return -1; }
 #endif
 
 int IMG_InitWEBP()
 {
     if (lib.loaded == 0) {
-#ifdef LOAD_WEBP_DYNAMIC
-        lib.handle = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
-        if (lib.handle == NULL) {
+#if defined(LOAD_WEBP_DYNAMIC) && defined(LOAD_WEBPDEMUX_DYNAMIC)
+        lib.handle_libwebpdemux = SDL_LoadObject(LOAD_WEBPDEMUX_DYNAMIC);
+        if (lib.handle_libwebpdemux == NULL) {
+            return -1;
+        }
+        lib.handle_libwebp = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
+        if (lib.handle_libwebp == NULL) {
             return -1;
         }
 #endif
 #if WEBP_DECODER_ABI_VERSION < 0x0100
-        FUNCTION_LOADER(WebPGetFeaturesInternal, VP8StatusCode (*) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version))
-        FUNCTION_LOADER(WebPDecodeRGBInto, uint8_t * (*) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride))
-        FUNCTION_LOADER(WebPDecodeRGBAInto, uint8_t * (*) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride))
+        FUNCTION_LOADER_LIBWEBP(WebPGetFeaturesInternal, VP8StatusCode (*) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version))
+        FUNCTION_LOADER_LIBWEBP(WebPDecodeRGBInto, uint8_t * (*) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride))
+        FUNCTION_LOADER_LIBWEBP(WebPDecodeRGBAInto, uint8_t * (*) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride))
 #else
-        FUNCTION_LOADER(WebPGetFeaturesInternal, VP8StatusCode (*) (const uint8_t *data, size_t data_size, WebPBitstreamFeatures* features, int decoder_abi_version))
-        FUNCTION_LOADER(WebPDecodeRGBInto, uint8_t * (*) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride))
-        FUNCTION_LOADER(WebPDecodeRGBAInto, uint8_t * (*) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride))
+        FUNCTION_LOADER_LIBWEBP(WebPGetFeaturesInternal, VP8StatusCode (*) (const uint8_t *data, size_t data_size, WebPBitstreamFeatures* features, int decoder_abi_version))
+        FUNCTION_LOADER_LIBWEBP(WebPDecodeRGBInto, uint8_t * (*) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride))
+        FUNCTION_LOADER_LIBWEBP(WebPDecodeRGBAInto, uint8_t * (*) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride))
 #endif
-        FUNCTION_LOADER(WebPDemuxInternal, WebPDemuxer* (*)(const WebPData*, int, WebPDemuxState*, int))
-        FUNCTION_LOADER(WebPDemuxGetFrame, int (*)(const WebPDemuxer* dmux, int frame_number, WebPIterator* iter))
-        FUNCTION_LOADER(WebPDemuxGetI, uint32_t (*)(const WebPDemuxer* dmux, WebPFormatFeature feature));
-        FUNCTION_LOADER(WebPDemuxDelete, void (*)(WebPDemuxer* dmux))
+        FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxInternal, WebPDemuxer* (*)(const WebPData*, int, WebPDemuxState*, int))
+        FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxGetFrame, int (*)(const WebPDemuxer* dmux, int frame_number, WebPIterator* iter))
+        FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxGetI, uint32_t (*)(const WebPDemuxer* dmux, WebPFormatFeature feature));
+        FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxDelete, void (*)(WebPDemuxer* dmux))
     }
     ++lib.loaded;
 
@@ -104,8 +115,9 @@ void IMG_QuitWEBP()
         return;
     }
     if (lib.loaded == 1) {
-#ifdef LOAD_WEBP_DYNAMIC
-        SDL_UnloadObject(lib.handle);
+#if defined(LOAD_WEBP_DYNAMIC) && defined(LOAD_WEBPDEMUX_DYNAMIC)
+        SDL_UnloadObject(lib.handle_libwebp);
+        SDL_UnloadObject(lib.handle_libwebpdemux);
 #endif
     }
     --lib.loaded;
diff --git a/Makefile.in b/Makefile.in
index a5a12017..6694f99c 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -386,6 +386,8 @@ LIBTIFF_LIBS = @LIBTIFF_LIBS@
 LIBTOOL = @LIBTOOL@
 LIBWEBPDEMUX_CFLAGS = @LIBWEBPDEMUX_CFLAGS@
 LIBWEBPDEMUX_LIBS = @LIBWEBPDEMUX_LIBS@
+LIBWEBP_CFLAGS = @LIBWEBP_CFLAGS@
+LIBWEBP_LIBS = @LIBWEBP_LIBS@
 LIPO = @LIPO@
 LN_S = @LN_S@
 LOAD_AVIF = @LOAD_AVIF@
diff --git a/cmake/Findwebp.cmake b/cmake/Findwebp.cmake
index 23982ddb..58891318 100644
--- a/cmake/Findwebp.cmake
+++ b/cmake/Findwebp.cmake
@@ -14,8 +14,22 @@ set(webp_LINK_LIBRARIES "" CACHE STRING "Extra link libraries of webp")
 
 set(webp_LINK_FLAGS "" CACHE STRING "Extra link flags of webp")
 
+find_library(webpdemux_LIBRARY
+    NAMES webpdemux
+)
+
+find_path(webpdemux_INCLUDE_PATH
+    NAMES webp/demux.h
+)
+
+set(webpdemux_COMPILE_OPTIONS "" CACHE STRING "Extra compile options of webpdemux")
+
+set(webpdemux_LINK_LIBRARIES "" CACHE STRING "Extra link libraries of webpdemux")
+
+set(webpdemux_LINK_FLAGS "" CACHE STRING "Extra link flags of webpdemux")
+
 find_package_handle_standard_args(webp
-    REQUIRED_VARS webp_LIBRARY webp_INCLUDE_PATH
+    REQUIRED_VARS webp_LIBRARY webp_INCLUDE_PATH webpdemux_LIBRARY webpdemux_INCLUDE_PATH
 )
 
 if (webp_FOUND)
@@ -29,4 +43,14 @@ if (webp_FOUND)
             INTERFACE_LINK_FLAGS "${webp_LINK_FLAGS}"
         )
     endif()
+    if (NOT TARGET WEBP::webpdemux)
+        add_library(WebP::webpdemux UNKNOWN IMPORTED)
+        set_target_properties(WebP::webpdemux PROPERTIES
+            IMPORTED_LOCATION "${webpdemux_LIBRARY}"
+            INTERFACE_INCLUDE_DIRECTORIES "${webpdemux_INCLUDE_PATH}"
+            INTERFACE_COMPILE_OPTIONS "${webpdemux_COMPILE_FLAGS}"
+            INTERFACE_LINK_LIBRARIES "${webwebpdemux_LINK_LIBRARIES}"
+            INTERFACE_LINK_FLAGS "${webpdemux_LINK_FLAGS}"
+        )
+    endif()
 endif()
diff --git a/configure b/configure
index 6bfcfa00..e289ca19 100755
--- a/configure
+++ b/configure
@@ -824,6 +824,8 @@ LOAD_LBM
 LOAD_GIF
 LOAD_BMP
 LOAD_WEBP
+LIBWEBP_LIBS
+LIBWEBP_CFLAGS
 LIBWEBPDEMUX_LIBS
 LIBWEBPDEMUX_CFLAGS
 LOAD_TIF
@@ -1056,7 +1058,9 @@ LIBPNG_LIBS
 LIBTIFF_CFLAGS
 LIBTIFF_LIBS
 LIBWEBPDEMUX_CFLAGS
-LIBWEBPDEMUX_LIBS'
+LIBWEBPDEMUX_LIBS
+LIBWEBP_CFLAGS
+LIBWEBP_LIBS'
 
 
 # Initialize some variables set by options.
@@ -1777,6 +1781,10 @@ Some influential environment variables:
               C compiler flags for LIBWEBPDEMUX, overriding pkg-config
   LIBWEBPDEMUX_LIBS
               linker flags for LIBWEBPDEMUX, overriding pkg-config
+  LIBWEBP_CFLAGS
+              C compiler flags for LIBWEBP, overriding pkg-config
+  LIBWEBP_LIBS
+              linker flags for LIBWEBP, overriding pkg-config
 
 Use these variables to override the choices made by `configure' or to help
 it to find libraries and programs with nonstandard names/locations.
@@ -4705,13 +4713,13 @@ then :
 else $as_nop
   lt_cv_nm_interface="BSD nm"
   echo "int some_variable = 0;" > conftest.$ac_ext
-  (eval echo "\"\$as_me:4708: $ac_compile\"" >&5)
+  (eval echo "\"\$as_me:4716: $ac_compile\"" >&5)
   (eval "$ac_compile" 2>conftest.err)
   cat conftest.err >&5
-  (eval echo "\"\$as_me:4711: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
+  (eval echo "\"\$as_me:4719: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
   (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
   cat conftest.err >&5
-  (eval echo "\"\$as_me:4714: output\"" >&5)
+  (eval echo "\"\$as_me:4722: output\"" >&5)
   cat conftest.out >&5
   if $GREP 'External.*some_variable' conftest.out > /dev/null; then
     lt_cv_nm_interface="MS dumpbin"
@@ -5970,7 +5978,7 @@ ia64-*-hpux*)
   ;;
 *-*-irix6*)
   # Find out which ABI we are using.
-  echo '#line 5973 "configure"' > conftest.$ac_ext
+  echo '#line 5981 "configure"' > conftest.$ac_ext
   if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
   (eval $ac_compile) 2>&5
   ac_status=$?
@@ -7643,11 +7651,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:7646: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7654: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:7650: \$? = $ac_status" >&5
+   echo "$as_me:7658: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s "$ac_outfile"; then
      # The compiler can only warn and ignore the option if not recognized
      # So say no if there are warnings other than the usual output.
@@ -7993,11 +8001,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:7996: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:8004: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:8000: \$? = $ac_status" >&5
+   echo "$as_me:8008: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s "$ac_outfile"; then
      # The compiler can only warn and ignore the option if not recognized
      # So say no if there are warnings other than the usual output.
@@ -8100,11 +8108,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:8103: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:8111: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:8107: \$? = $ac_status" >&5
+   echo "$as_me:8115: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s out/conftest2.$ac_objext
    then
      # The compiler can only warn and ignore the option if not recognized
@@ -8156,11 +8164,11 @@ else $as_nop
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:8159: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:8167: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:8163: \$? = $ac_status" >&5
+   echo "$as_me:8171: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s out/conftest2.$ac_objext
    then
      # The compiler can only warn and ignore the option if not recognized
@@ -10599,7 +10607,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 10602 "configure"
+#line 10610 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -10696,7 +10704,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 10699 "configure"
+#line 10707 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -15376,7 +15384,7 @@ fi
 if test "x$ac_cv_header_webp_demux_h" = xyes
 then :
 
-            have_webp_hdr=yes
+            have_webpdemux_hdr=yes
             LIBWEBPDEMUX_CFLAGS=""
 
 fi
@@ -15419,7 +15427,7 @@ printf "%s\n" "$ac_cv_lib_webpdemux_WebPDemuxGetFrame" >&6; }
 if test "x$ac_cv_lib_webpdemux_WebPDemuxGetFrame" = xyes
 then :
 
-            have_webp_lib=yes
+            have_webpdemux_lib=yes
             LIBWEBPDEMUX_LIBS="-lwebpdemux"
 
 fi
@@ -15432,7 +15440,7 @@ printf "%s\n" "no" >&6; }
 if test "x$ac_cv_header_webp_demux_h" = xyes
 then :
 
-            have_webp_hdr=yes
+            have_webpdemux_hdr=yes
             LIBWEBPDEMUX_CFLAGS=""
 
 fi
@@ -15475,7 +15483,7 @@ printf "%s\n" "$ac_cv_lib_webpdemux_WebPDemuxGetFrame" >&6; }
 if test "x$ac_cv_lib_webpdemux_WebPDemuxGetFrame" = xyes
 then :
 
-            have_webp_lib=yes
+            have_webpdemux_lib=yes
             LIBWEBPDEMUX_LIBS="-lwebpdemux"
 
 fi
@@ -15485,28 +15493,210 @@ else
 	LIBWEBPDEMUX_CFLAGS=$pkg_cv_LIBWEBPDEMUX_CFLAGS
 	LIBWEBPDEMUX_LIBS=$pkg_cv_LIBWEBPDEMUX_LIBS
         { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+printf "%s\n" "yes" >&6; }
+	        have_webpdemux_hdr=yes
+        have_webpdemux_lib=yes
+        have_webpdemux_pc=yes
+
+fi
+
+pkg_failed=no
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for libwebp" >&5
+printf %s "checking for libwebp... " >&6; }
+
+if test -n "$LIBWEBP_CFLAGS"; then
+    pkg_cv_LIBWEBP_CFLAGS="$LIBWEBP_CFLAGS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libwebp\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libwebp") 2>&5
+  ac_status=$?
+  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_LIBWEBP_CFLAGS=`$PKG_CONFIG --cflags "libwebp" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+if test -n "$LIBWEBP_LIBS"; then
+    pkg_cv_LIBWEBP_LIBS="$LIBWEBP_LIBS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libwebp\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libwebp") 2>&5
+  ac_status=$?
+  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_LIBWEBP_LIBS=`$PKG_CONFIG --libs "libwebp" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+
+
+
+if test $pkg_failed = yes; then
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
+fi
+        if test $_pkg_short_errors_supported = yes; then
+	        LIBWEBP_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libwebp" 2>&1`
+        else
+	        LIBWEBP_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libwebp" 2>&1`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$LIBWEBP_PKG_ERRORS" >&5
+
+	        ac_fn_c_check_header_compile "$LINENO" "webp/decode.h" "ac_cv_header_webp_decode_h" "$ac_includes_default"
+if test "x$ac_cv_header_webp_decode_h" = xyes
+then :
+
+            have_webp_hdr=yes
+            LIBWEBP_CFLAGS=""
+
+fi
+
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for WebPGetDecoderVersion in -lwebp" >&5
+printf %s "checking for WebPGetDecoderVersion in -lwebp... " >&6; }
+if test ${ac_cv_lib_webp_WebPGetDecoderVersion+y}
+then :
+  printf %s "(cached) " >&6
+else $as_nop
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lwebp -lm $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+char WebPGetDecoderVersion ();
+int
+main (void)
+{
+return WebPGetDecoderVersion ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  ac_cv_lib_webp_WebPGetDecoderVersion=yes
+else $as_nop
+  ac_cv_lib_webp_WebPGetDecoderVersion=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_webp_WebPGetDecoderVersion" >&5
+printf "%s\n" "$ac_cv_lib_webp_WebPGetDecoderVersion" >&6; }
+if test "x$ac_cv_lib_webp_WebPGetDecoderVersion" = xyes
+then :
+
+            have_webp_lib=yes
+            LIBWEBP_LIBS="-lwebp"
+
+fi
+
+
+elif test $pkg_failed = untried; then
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
+printf "%s\n" "no" >&6; }
+	        ac_fn_c_check_header_compile "$LINENO" "webp/decode.h" "ac_cv_header_webp_decode_h" "$ac_includes_default"
+if test "x$ac_cv_header_webp_decode_h" = xyes
+then :
+
+            have_webp_hdr=yes
+            LIBWEBP_CFLAGS=""
+
+fi
+
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for WebPGetDecoderVersion in -lwebp" >&5
+printf %s "checking for WebPGetDecoderVersion in -lwebp... " >&6; }
+if test ${ac_cv_lib_webp_WebPGetDecoderVersion+y}
+then :
+  printf %s "(cached) " >&6
+else $as_nop
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lwebp -lm $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+char WebPGetDecoderVersion ();
+int
+main (void)
+{
+return WebPGetDecoderVersion ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"
+then :
+  ac_cv_lib_webp_WebPGetDecoderVersion=yes
+else $as_nop
+  ac_cv_lib_webp_WebPGetDecoderVersion=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_webp_WebPGetDecoderVersion" >&5
+printf "%s\n" "$ac_cv_lib_webp_WebPGetDecoderVersion" >&6; }
+if test "x$ac_cv_lib_webp_WebPGetDecoderVersion" = xyes
+then :
+
+            have_webp_lib=yes
+            LIBWEBP_LIBS="-lwebp"
+
+fi
+
+
+else
+	LIBWEBP_CFLAGS=$pkg_cv_LIBWEBP_CFLAGS
+	LIBWEBP_LIBS=$pkg_cv_LIBWEBP_LIBS
+        { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 printf "%s\n" "yes" >&6; }
 	        have_webp_hdr=yes
         have_webp_lib=yes
         have_webp_pc=yes
 
 fi
-    if test x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
+    if test x$have_webpdemux_hdr = xyes -a x$have_webpdemux_lib = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
         printf "%s\n" "#define LOAD_WEBP 1" >>confdefs.h
 
         load_webp=1
 
         case "$host" in
             *-*-darwin*)
-                webp_lib=`find_lib libwebpdemux.dylib`
+                webpdemux_lib=`find_lib libwebpdemux.dylib`
+                webp_lib=`find_lib libwebp.dylib`
                 ;;
             *-*-cygwin* | *-*-mingw*)
-                webp_lib=`find_lib "libwebpdemux-*.dll"`
+                webpdemux_lib=`find_lib "libwebpdemux-*.dll"`
+                webp_lib=`find_lib "libwebp-*.dll"`
                 ;;
             *)
-                webp_lib=`find_lib "libwebpdemux[0-9]*.so.*"`
+                webpdemux_lib=`find_lib "libwebpdemux[0-9]*.so.*"`
+                webp_lib=`find_lib "libwebp[0-9]*.so.*"`
                 if test x$webp_lib = x; then
-                    webp_lib=`find_lib "libwebpdemux.so.*"`
+                    webpdemux_lib=`find_lib "libwebpdemux.so.*"`
+                    webp_lib=`find_lib "libwebp.so.*"`
                 fi
                 ;;
         esac
@@ -15619,18 +15809,21 @@ fi
 LOAD_QOI=$load_qoi
 
 
-if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
+if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib -a x$have_webpdemux_hdr = xyes -a x$have_webpdemux_lib = xyes; then
     CFLAGS="$LIBWEBP_CFLAGS $CFLAGS"
     if test x$enable_webp_shared = xyes && test x$webp_lib != x; then
         echo "-- dynamic libwebp -> $webp_lib"
         printf "%s\n" "#define LOAD_WEBP_DYNAMIC \"$webp_lib\"" >>confdefs.h
 
+        echo "-- dynamic libwebpdemux -> $webpdemux_lib"
+        printf "%s\n" "#define LOAD_WEBPDEMUX_DYNAMIC \"$webpdemux_lib\"" >>confdefs.h
+
     else
-        IMG_LIBS="$LIBWEBPDEMUX_LIBS $IMG_LIBS"
+        IMG_LIBS="$LIBWEBPDEMUX_LIBS $LIBWEBP_LIBS $IMG_LIBS"
         if test x$have_webp_pc = xyes; then
-            PC_REQUIRES="libwebpdemux $PC_REQUIRES"
+            PC_REQUIRES="libwebpdemux libwebp $PC_REQUIRES"
         else
-            PC_LIBS="$LIBWEBPDEMUX_LIBS $PC_LIBS"
+            PC_LIBS="$LIBWEBPDEMUX_LIBS $LIBWEBP_LIBS $PC_LIBS"
         fi
     fi
 fi
diff --git a/configure.ac b/configure.ac
index 9b0be17c..c2584c0e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -534,34 +534,52 @@ AC_SUBST([LOAD_TIF], $load_tif)
 
 if test x$enable_webp = xyes; then
     PKG_CHECK_MODULES([LIBWEBPDEMUX], [libwebpdemux], [dnl
+        have_webpdemux_hdr=yes
+        have_webpdemux_lib=yes
+        have_webpdemux_pc=yes
+      ], [dnl
+        AC_CHECK_HEADER([webp/demux.h], [
+            have_webpdemux_hdr=yes
+            LIBWEBPDEMUX_CFLAGS=""
+        ])
+        AC_CHECK_LIB([webpdemux], [WebPDemuxGetFrame], [
+            have_webpdemux_lib=yes
+            LIBWEBPDEMUX_LIBS="-lwebpdemux"
+        ], [], [-lm])
+      ])
+    PKG_CHECK_MODULES([LIBWEBP], [libwebp], [dnl
         have_webp_hdr=yes
         have_webp_lib=yes
         have_webp_pc=yes
       ], [dnl
-        AC_CHECK_HEADER([webp/demux.h], [
+        AC_CHECK_HEADER([webp/decode.h], [
             have_webp_hdr=yes
-            LIBWEBPDEMUX_CFLAGS=""
+            LIBWEBP_CFLAGS=""
         ])
-        AC_CHECK_LIB([webpdemux], [WebPDemuxGetFrame], [
+        AC_CHECK_LIB([webp], [WebPGetDecoderVersion], [
             have_webp_lib=yes
-            LIBWEBPDEMUX_LIBS="-lwebpdemux"
+            LIBWEBP_LIBS="-lwebp"
         ], [], [-lm])
       ])
-    if test x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
+    if test x$have_webpdemux_hdr = xyes -a x$have_webpdemux_lib = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
         AC_DEFINE([LOAD_WEBP])
         load_webp=1
 
         case "$host" in
             *-*-darwin*)
-                webp_lib=[`find_lib libwebpdemux.dylib`]
+                webpdemux_lib=[`find_lib libwebpdemux.dylib`]
+                webp_lib=[`find_lib libwebp.dylib`]
                 ;;
             *-*-cygwin* | *-*-mingw*)
-                webp_lib=[`find_lib "libwebpdemux-*.dll"`]
+                webpdemux_lib=[`find_lib "libwebpdemux-*.dll"`]
+                webp_lib=[`find_lib "libwebp-*.dll"`]
                 ;;
             *)
-                webp_lib=[`find_lib "libwebpdemux[0-9]*.so.*"`]
+                webpdemux_lib=[`find_lib "libwebpdemux[0-9]*.so.*"`]
+                webp_lib=[`find_lib "libwebp[0-9]*.so.*"`]
                 if test x$webp_lib = x; then
-                    webp_lib=[`find_lib "libwebpdemux.so.*"`]
+                    webpdemux_lib=[`find_lib "libwebpdemux.so.*"`]
+                    webp_lib=[`find_lib "libwebp.so.*"`]
                 fi
                 ;;
         esac
@@ -649,17 +667,19 @@ if test x$enable_qoi = xyes; then
 fi
 AC_SUBST([LOAD_QOI], $load_qoi)
 
-if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib = xyes; then
+if test x$enable_webp = xyes -a x$have_webp_hdr = xyes -a x$have_webp_lib -a x$have_webpdemux_hdr = xyes -a x$have_webpdemux_lib = xyes; then
     CFLAGS="$LIBWEBP_CFLAGS $CFLAGS"
     if test x$enable_webp_shared = xyes && test x$webp_lib != x; then
         echo "-- dynamic libwebp -> $webp_lib"
         AC_DEFINE_UNQUOTED(LOAD_WEBP_DYNAMIC, "$webp_lib")
+        echo "-- dynamic libwebpdemux -> $webpdemux_lib"
+        AC_DEFINE_UNQUOTED(LOAD_WEBPDEMUX_DYNAMIC, "$webpdemux_lib")
     else
-        IMG_LIBS="$LIBWEBPDEMUX_LIBS $IMG_LIBS"
+        IMG_LIBS="$LIBWEBPDEMUX_LIBS $LIBWEBP_LIBS $IMG_LIBS"
         if test x$have_webp_pc = xyes; then
-            PC_REQUIRES="libwebpdemux $PC_REQUIRES"
+            PC_REQUIRES="libwebpdemux libwebp $PC_REQUIRES"
         else
-            PC_LIBS="$LIBWEBPDEMUX_LIBS $PC_LIBS"
+            PC_LIBS="$LIBWEBPDEMUX_LIBS $LIBWEBP_LIBS $PC_LIBS"
         fi
     fi
 fi
diff --git a/test/Makefile.in b/test/Makefile.in
index c06a884a..84eb19c5 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -449,6 +449,8 @@ LIBTIFF_LIBS = @LIBTIFF_LIBS@
 LIBTOOL = @LIBTOOL@
 LIBWEBPDEMUX_CFLAGS = @LIBWEBPDEMUX_CFLAGS@
 LIBWEBPDEMUX_LIBS = @LIBWEBPDEMUX_LIBS@
+LIBWEBP_CFLAGS = @LIBWEBP_CFLAGS@
+LIBWEBP_LIBS = @LIBWEBP_LIBS@
 LIPO = @LIPO@
 LN_S = @LN_S@
 LOAD_AVIF = @LOAD_AVIF@