From 69b7f9f4c384673eeb89a4c6b7a0a0a2bf105c08 Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Date: Mon, 29 Dec 2025 08:52:33 +0100 Subject: [PATCH] fix(mp4): Use fixed-width integer types in bswap functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change bswap16 and bswap32 to use int16_t and int32_t instead of short and long for consistent behavior across platforms. On Windows x64, `long` is 4 bytes (LLP64 model), while on Linux x64 `long` is 8 bytes (LP64 model). This difference could cause inconsistent NAL unit length parsing in MP4/MOV files, potentially affecting timestamp calculations. This fix ensures the byte-swapping functions work identically on both platforms by using fixed-width integer types from . 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/lib_ccx/mp4.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib_ccx/mp4.c b/src/lib_ccx/mp4.c index 6f8392a4b..c9b7970d1 100644 --- a/src/lib_ccx/mp4.c +++ b/src/lib_ccx/mp4.c @@ -31,15 +31,17 @@ #define GF_ISOM_SUBTYPE_MPEG4 GF_4CC('M', 'P', 'E', 'G') #endif -static short bswap16(short v) +static int16_t bswap16(int16_t v) { return ((v >> 8) & 0x00FF) | ((v << 8) & 0xFF00); } -static long bswap32(long v) +static int32_t bswap32(int32_t v) { // For 0x12345678 returns 78563412 - long swapped = ((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24); + // Use int32_t instead of long for consistent behavior across platforms + // (long is 4 bytes on Windows x64 but 8 bytes on Linux x64) + int32_t swapped = ((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24); return swapped; } static struct @@ -82,10 +84,10 @@ static int process_avc_sample(struct lib_ccx_ctx *ctx, u32 timescale, GF_AVCConf nal_length = s->data[i]; break; case 2: - nal_length = bswap16(*(short *)&s->data[i]); + nal_length = bswap16(*(int16_t *)&s->data[i]); break; case 4: - nal_length = bswap32(*(long *)&s->data[i]); + nal_length = bswap32(*(int32_t *)&s->data[i]); break; } const u32 previous_index = i; @@ -151,10 +153,10 @@ static int process_hevc_sample(struct lib_ccx_ctx *ctx, u32 timescale, GF_HEVCCo nal_length = s->data[i]; break; case 2: - nal_length = bswap16(*(short *)&s->data[i]); + nal_length = bswap16(*(int16_t *)&s->data[i]); break; case 4: - nal_length = bswap32(*(long *)&s->data[i]); + nal_length = bswap32(*(int32_t *)&s->data[i]); break; default: mprint("Unexpected nal_unit_size %u in HEVC config\n", c->nal_unit_size);