loader/webp: Remove WEBP_FORCE_ALIGNED and use memcpy() instead

The google's libwebp source uses aligned memory access.
This patch is that applies the two commits below to our static lib code.

refer to:
[Remove WEBP_FORCE_ALIGNED and use memcpy() instead]
3884972e3f
[bit_reader.c: s/VP8L_USE_UNALIGNED_LOAD/VP8L_USE_FAST_LOAD/]
ac49e4e4dc
source :
https://chromium.googlesource.com/webm/libwebp/+/refs/heads/main/src/utils/bit_reader_inl_utils.h#80

related issue: https://github.com/thorvg/thorvg/issues/2006
This commit is contained in:
JunsuChoi 2024-03-08 14:37:07 +09:00 committed by Hermet Park
parent 3c5e9e1e7c
commit fc4f452362
3 changed files with 20 additions and 12 deletions

View file

@ -16,6 +16,7 @@
#endif #endif
#include "./bit_reader_inl.h" #include "./bit_reader_inl.h"
#include "./utils.h"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// VP8BitReader // VP8BitReader
@ -109,11 +110,10 @@ int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) {
#define VP8L_LOG8_WBITS 4 // Number of bytes needed to store VP8L_WBITS bits. #define VP8L_LOG8_WBITS 4 // Number of bytes needed to store VP8L_WBITS bits.
#if !defined(WEBP_FORCE_ALIGNED) && \ #if (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \
(defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \
defined(__i386__) || defined(_M_IX86) || \ defined(__i386__) || defined(_M_IX86) || \
defined(__x86_64__) || defined(_M_X64)) defined(__x86_64__) || defined(_M_X64))
#define VP8L_USE_UNALIGNED_LOAD #define VP8L_USE_FAST_LOAD
#endif #endif
static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = { static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = {
@ -183,13 +183,13 @@ void VP8LDoFillBitWindow(VP8LBitReader* const br) {
assert(br->bit_pos_ >= VP8L_WBITS); assert(br->bit_pos_ >= VP8L_WBITS);
// TODO(jzern): given the fixed read size it may be possible to force // TODO(jzern): given the fixed read size it may be possible to force
// alignment in this block. // alignment in this block.
#if defined(VP8L_USE_UNALIGNED_LOAD) #if defined(VP8L_USE_FAST_LOAD)
if (br->pos_ + sizeof(br->val_) < br->len_) { if (br->pos_ + sizeof(br->val_) < br->len_) {
br->val_ >>= VP8L_WBITS; br->val_ >>= VP8L_WBITS;
br->bit_pos_ -= VP8L_WBITS; br->bit_pos_ -= VP8L_WBITS;
// The expression below needs a little-endian arch to work correctly. // The expression below needs a little-endian arch to work correctly.
// This gives a large speedup for decoding speed. // This gives a large speedup for decoding speed.
br->val_ |= (vp8l_val_t)*(const uint32_t*)(br->buf_ + br->pos_) << br->val_ |= (vp8l_val_t)HToLE32(WebPMemToUint32(br->buf_ + br->pos_)) <<
(VP8L_LBITS - VP8L_WBITS); (VP8L_LBITS - VP8L_WBITS);
br->pos_ += VP8L_LOG8_WBITS; br->pos_ += VP8L_LOG8_WBITS;
return; return;

View file

@ -20,9 +20,7 @@
#include "../webp/config.h" #include "../webp/config.h"
#endif #endif
#ifdef WEBP_FORCE_ALIGNED
#include <string.h> // memcpy #include <string.h> // memcpy
#endif
#include "../dsp/dsp.h" #include "../dsp/dsp.h"
#include "./bit_reader.h" #include "./bit_reader.h"
@ -61,10 +59,7 @@ static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) {
if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) { if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) {
// convert memory type to register type (with some zero'ing!) // convert memory type to register type (with some zero'ing!)
bit_t bits; bit_t bits;
#if defined(WEBP_FORCE_ALIGNED) #if defined(WEBP_USE_MIPS32)
lbit_t in_bits;
memcpy(&in_bits, br->buf_, sizeof(in_bits));
#elif defined(WEBP_USE_MIPS32)
// This is needed because of un-aligned read. // This is needed because of un-aligned read.
lbit_t in_bits; lbit_t in_bits;
lbit_t* p_buf_ = (lbit_t*)br->buf_; lbit_t* p_buf_ = (lbit_t*)br->buf_;
@ -79,7 +74,8 @@ static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) {
: "memory", "at" : "memory", "at"
); );
#else #else
const lbit_t in_bits = *(const lbit_t*)br->buf_; lbit_t in_bits;
memcpy(&in_bits, br->buf_, sizeof(in_bits));
#endif #endif
br->buf_ += BITS >> 3; br->buf_ += BITS >> 3;
#if !defined(WORDS_BIGENDIAN) #if !defined(WORDS_BIGENDIAN)

View file

@ -59,6 +59,18 @@ static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
} }
#endif #endif
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Alignment
#include <string.h>
// memcpy() is the safe way of moving potentially unaligned 32b memory.
static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
uint32_t A;
memcpy(&A, ptr, sizeof(A));
return A;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#ifdef __cplusplus #ifdef __cplusplus