Skip to content

Commit 69b7f9f

Browse files
cfsmp3claude
andcommitted
fix(mp4): Use fixed-width integer types in bswap functions
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 <stdint.h>. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 63dde6f commit 69b7f9f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/lib_ccx/mp4.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@
3131
#define GF_ISOM_SUBTYPE_MPEG4 GF_4CC('M', 'P', 'E', 'G')
3232
#endif
3333

34-
static short bswap16(short v)
34+
static int16_t bswap16(int16_t v)
3535
{
3636
return ((v >> 8) & 0x00FF) | ((v << 8) & 0xFF00);
3737
}
3838

39-
static long bswap32(long v)
39+
static int32_t bswap32(int32_t v)
4040
{
4141
// For 0x12345678 returns 78563412
42-
long swapped = ((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24);
42+
// Use int32_t instead of long for consistent behavior across platforms
43+
// (long is 4 bytes on Windows x64 but 8 bytes on Linux x64)
44+
int32_t swapped = ((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24);
4345
return swapped;
4446
}
4547
static struct
@@ -82,10 +84,10 @@ static int process_avc_sample(struct lib_ccx_ctx *ctx, u32 timescale, GF_AVCConf
8284
nal_length = s->data[i];
8385
break;
8486
case 2:
85-
nal_length = bswap16(*(short *)&s->data[i]);
87+
nal_length = bswap16(*(int16_t *)&s->data[i]);
8688
break;
8789
case 4:
88-
nal_length = bswap32(*(long *)&s->data[i]);
90+
nal_length = bswap32(*(int32_t *)&s->data[i]);
8991
break;
9092
}
9193
const u32 previous_index = i;
@@ -151,10 +153,10 @@ static int process_hevc_sample(struct lib_ccx_ctx *ctx, u32 timescale, GF_HEVCCo
151153
nal_length = s->data[i];
152154
break;
153155
case 2:
154-
nal_length = bswap16(*(short *)&s->data[i]);
156+
nal_length = bswap16(*(int16_t *)&s->data[i]);
155157
break;
156158
case 4:
157-
nal_length = bswap32(*(long *)&s->data[i]);
159+
nal_length = bswap32(*(int32_t *)&s->data[i]);
158160
break;
159161
default:
160162
mprint("Unexpected nal_unit_size %u in HEVC config\n", c->nal_unit_size);

0 commit comments

Comments
 (0)