Skip to content

Commit 1567ac5

Browse files
committed
Added endianness detection and byte order conversion functions
- Detected platform endianness (Big/Little Endian) in CMake configuration - Added conditional compilation for endianness-specific byte order conversion in oapv_app_enc.c - Introduced helper macros (bswap16, bswap32, bswap64) and endian conversion functions (be2ne, le2ne, ne2be, ne2le) in oapv_app_util.h - Updated metadata serialization functions to use new byte order conversion utilities for consistent cross-platform behavior Signed-off-by: Dawid Kozinski <[email protected]>
1 parent 5b79915 commit 1567ac5

File tree

3 files changed

+91
-16
lines changed

3 files changed

+91
-16
lines changed

app/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ include_directories(${CMAKE_BINARY_DIR}/include)
33
set(EXE_ENC oapv_app_enc)
44
set(EXE_DEC oapv_app_dec)
55

6+
if(CMAKE_C_BYTE_ORDER STREQUAL "BIG_ENDIAN")
7+
set(WORDS_BIGENDIAN TRUE)
8+
message(STATUS "Platform is Big Endian")
9+
elseif(CMAKE_C_BYTE_ORDER STREQUAL "LITTLE_ENDIAN")
10+
set(WORDS_BIGENDIAN FALSE)
11+
message(STATUS "Platform is Little Endian")
12+
else()
13+
set(WORDS_BIGENDIAN FALSE)
14+
message(WARNING "Endianness could not be determined, assuming Little Endian")
15+
endif()
16+
617
file(GLOB SRC_ENC "oapv_app_enc.c")
718
file(GLOB INC_ENC "*.h")
819
file(GLOB SRC_DEC "oapv_app_dec.c")
@@ -37,6 +48,11 @@ elseif(UNIX OR MINGW)
3748
target_link_libraries(${EXE_DEC} m)
3849
endif()
3950

51+
if(WORDS_BIGENDIAN)
52+
target_compile_definitions(${EXE_ENC} PRIVATE HAVE_BIGENDIAN ANY)
53+
target_compile_definitions(${EXE_DEC} PRIVATE HAVE_BIGENDIAN ANY)
54+
endif()
55+
4056
# Install rules
4157
#
4258
# Install executable to <prefix>/bin

app/oapv_app_enc.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@
3535
#include "oapv_app_y4m.h"
3636
#include "oapv_port.h"
3737

38-
#if defined(_WIN64) || defined(_WIN32)
39-
#include <winsock2.h>
40-
#pragma comment(lib, "ws2_32.lib")
41-
#else
42-
#include <arpa/inet.h>
43-
#endif
4438

4539
#define MAX_BS_BUF (128 * 1024 * 1024)
4640
#define MAX_NUM_FRMS (1) // supports only 1-frame in an access unit
@@ -856,29 +850,28 @@ static void serialize_metadata_mdcv(const md_mdcv_t* mdcv, uint8_t* buffer) {
856850
uint32_t beu32_val;
857851

858852
for (i = 0; i < 3; i++) {
859-
// convert Host Byte Order na Network Byte Order (Big Endian).
860-
beu16_val = htons(mdcv->primary_chromaticity_x[i]);
853+
beu16_val = ne2be16(mdcv->primary_chromaticity_x[i]);
861854
memcpy(current_ptr, &beu16_val, sizeof(uint16_t));
862855
current_ptr += sizeof(uint16_t);
863856

864-
beu16_val = htons(mdcv->primary_chromaticity_y[i]);
857+
beu16_val = ne2be16(mdcv->primary_chromaticity_y[i]);
865858
memcpy(current_ptr, &beu16_val, sizeof(uint16_t));
866859
current_ptr += sizeof(uint16_t);
867860
}
868861

869-
beu16_val = htons(mdcv->white_point_chromaticity_x);
862+
beu16_val = ne2be16(mdcv->white_point_chromaticity_x);
870863
memcpy(current_ptr, &beu16_val, sizeof(uint16_t));
871864
current_ptr += sizeof(uint16_t);
872865

873-
beu16_val = htons(mdcv->white_point_chromaticity_y);
874-
memcpy(current_ptr, &beu32_val, sizeof(uint16_t));
866+
beu16_val = ne2be16(mdcv->white_point_chromaticity_y);
867+
memcpy(current_ptr, &beu16_val, sizeof(uint16_t));
875868
current_ptr += sizeof(uint16_t);
876869

877-
beu32_val = htonl(mdcv->max_mastering_luminance);
870+
beu32_val = ne2be32(mdcv->max_mastering_luminance);
878871
memcpy(current_ptr, &beu32_val, sizeof(uint32_t));
879872
current_ptr += sizeof(uint32_t);
880873

881-
beu32_val = htonl(mdcv->min_mastering_luminance);
874+
beu32_val = ne2be32(mdcv->min_mastering_luminance);
882875
memcpy(current_ptr, &beu32_val, sizeof(uint32_t));
883876
current_ptr += sizeof(uint32_t);
884877
}
@@ -887,11 +880,11 @@ static void serialize_metadata_cll(const md_cll_t* cll, uint8_t* buffer) {
887880
uint8_t* current_ptr = buffer;
888881
uint16_t beu16_val;
889882

890-
beu16_val = htons(cll->max_cll);
883+
beu16_val = ne2be16(cll->max_cll);
891884
memcpy(current_ptr, &beu16_val, sizeof(uint16_t));
892885
current_ptr += sizeof(uint16_t);
893886

894-
beu16_val = htons(cll->max_fall);
887+
beu16_val = ne2be16(cll->max_fall);
895888
memcpy(current_ptr, &beu16_val, sizeof(uint16_t));
896889
current_ptr += sizeof(uint16_t);
897890
}

app/oapv_app_util.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <math.h>
4545
#include <stdarg.h>
4646
#include <ctype.h>
47+
#include <stdint.h>
4748
#if LINUX
4849
#include <signal.h>
4950
#include <unistd.h>
@@ -181,6 +182,71 @@ static void log_line(char *pre)
181182

182183
static int op_verbose = VERBOSE_SIMPLE;
183184

185+
/* Endiannes */
186+
#define BSWAP16(x) (((x) << 8 & 0xff00) | ((x) >> 8 & 0x00ff))
187+
#define BSWAP32(x) (BSWAP16(x) << 16 | BSWAP16((x) >> 16))
188+
#define BSWAP64(x) (BSWAP32(x) << 32 | BSWAP32((x) >> 32))
189+
190+
#ifndef bswap16
191+
static inline uint16_t bswap16(uint16_t x)
192+
{
193+
x= (x>>8) | (x<<8);
194+
return x;
195+
}
196+
#endif
197+
198+
#ifndef bswap32
199+
static inline uint32_t bswap32(uint32_t x)
200+
{
201+
return BSWAP32(x);
202+
}
203+
#endif
204+
205+
#ifndef bswap64
206+
static inline uint64_t bswap64(uint64_t x)
207+
{
208+
return (uint64_t)bswap32(x) << 32 | bswap32(x >> 32);
209+
}
210+
#endif
211+
212+
// be2ne - big-endian to native-endian
213+
// le2ne - little-endian to native-endian
214+
// ne2be - native-endian to big-endian
215+
// ne2le - native-endian to little-endian
216+
217+
#if HAVE_BIGENDIAN
218+
#define be2ne16(x) (x)
219+
#define be2ne32(x) (x)
220+
#define be2ne64(x) (x)
221+
#define le2ne16(x) bswap16(x)
222+
#define le2ne32(x) bswap32(x)
223+
#define le2ne64(x) bswap64(x)
224+
225+
#define ne2be16(x) (x)
226+
#define ne2be32(x) (x)
227+
#define ne2be64(x) (x)
228+
#define ne2le16(x) bswap16(x)
229+
#define ne2le32(x) bswap32(x)
230+
#define ne2le64(x) bswap64(x)
231+
232+
#else
233+
234+
#define be2ne16(x) bswap16(x)
235+
#define be2ne32(x) bswap32(x)
236+
#define be2ne64(x) bswap64(x)
237+
#define le2ne16(x) (x)
238+
#define le2ne32(x) (x)
239+
#define le2ne64(x) (x)
240+
241+
#define ne2be16(x) bswap16(x)
242+
#define ne2be32(x) bswap32(x)
243+
#define ne2be64(x) bswap64(x)
244+
#define ne2le16(x) (x)
245+
#define ne2le32(x) (x)
246+
#define ne2le64(x) (x)
247+
248+
#endif
249+
184250
/* Clocks */
185251
#if defined(_WIN64) || defined(_WIN32)
186252
#include <windows.h>

0 commit comments

Comments
 (0)