Avoid requiring byteswapping functions

Instead of byteswapping a 32 bit word and writing it out as a
whole (which could even possibly lead to crashes due to
incorrect alignment on some platforms), write it out explicitly
in the intended byte order.

This avoids having to set a define indicating the endianness.
This commit is contained in:
Martin Storsjö 2014-01-29 14:30:04 +02:00
parent 1e744b1933
commit 21687726d0
2 changed files with 9 additions and 31 deletions

View File

@ -234,34 +234,6 @@ return r;
#define CALC_BI_STRIDE(width,bitcount) ((((width * bitcount) + 31) & ~31) >> 3)
#ifdef WORDS_BIGENDIAN
static inline uint32_t ENDIAN_FIX (uint32_t x) {
return x;
}
#else //!WORDS_BIGENDIAN
#if defined(_MSC_VER) && defined(_M_IX86)
static inline uint32_t ENDIAN_FIX (uint32_t x) {
__asm {
mov eax, x
bswap eax
mov x, eax
}
return x;
}
#else // GCC
static inline uint32_t ENDIAN_FIX (uint32_t x) {
#ifdef X86_ARCH
__asm__ __volatile__ ("bswap %0":"+r" (x));
#else
x = ((x & 0xff000000) >> 24) | ((x & 0xff0000) >> 8) |
((x & 0xff00) << 8) | ((x & 0xff) << 24);
#endif
return x;
}
#endif//GCC
#endif//!WORDS_BIGENDIAN
#ifndef BUTTERFLY1x2

View File

@ -46,6 +46,12 @@
namespace WelsSVCEnc {
#define WRITE_BE_32(ptr, val) do { \
(ptr)[0] = (val) >> 24; \
(ptr)[1] = (val) >> 16; \
(ptr)[2] = (val) >> 8; \
(ptr)[3] = (val) >> 0; \
} while (0)
/************************************************************************/
/* GOLOMB CODIMG FOR WELS ENCODER */
/************************************************************************/
@ -73,7 +79,7 @@ namespace WelsSVCEnc {
else {\
(n) -= iLeftBits;\
uiCurBits = (uiCurBits<<iLeftBits) | ((v)>>(n));\
*((uint32_t*)pBufPtr) = ENDIAN_FIX(uiCurBits);\
WRITE_BE_32(pBufPtr, uiCurBits);\
pBufPtr += 4;\
uiCurBits = (v) & ((1<<(n))-1);\
iLeftBits = 32 - (n);\
@ -141,7 +147,7 @@ if (n < pBs->iLeftBits) {
} else {
n -= pBs->iLeftBits;
pBs->uiCurBits = (pBs->uiCurBits << pBs->iLeftBits) | (kuiValue >> n);
* ((uint32_t*)pBs->pBufPtr) = ENDIAN_FIX (pBs->uiCurBits);
WRITE_BE_32(pBs->pBufPtr, pBs->uiCurBits);
pBs->pBufPtr += 4;
pBs->uiCurBits = kuiValue & ((1 << n) - 1);
pBs->iLeftBits = 32 - n;
@ -160,7 +166,7 @@ return 0;
static inline void BsFlush (SBitStringAux* pBs) {
* (uint32_t*)pBs->pBufPtr = ENDIAN_FIX (pBs->uiCurBits << pBs->iLeftBits);
WRITE_BE_32(pBs->pBufPtr, pBs->uiCurBits << pBs->iLeftBits);
pBs->pBufPtr += 4 - pBs->iLeftBits / 8;
pBs->iLeftBits = 32;
pBs->uiCurBits = 0; // for future writing safe, 5/19/2010