ref: 21687726d05641bd8d627128e6ce564df3a56aa2
parent: 1e744b1933ffe37c15d1ce426f923083124cdd5b
author: Martin Storsjö <martin@martin.st>
date: Wed Jan 29 09:30:04 EST 2014
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.
--- a/codec/common/macros.h
+++ b/codec/common/macros.h
@@ -234,34 +234,6 @@
#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
--- a/codec/encoder/core/inc/svc_enc_golomb.h
+++ b/codec/encoder/core/inc/svc_enc_golomb.h
@@ -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 @@
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 @@
} 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 @@
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