aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 26399f4ba67f25110cbdf194b743211cfab9855e
parent 10c3c153852d922743929b41e1e2065a5a2c320d
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 19:00:59 +0000

Decode JPEG blocks with a readable integer inverse transform

Diffstat:
Msrc/Makefile.linux | 8++++++++
Msrc/Makefile.wasm | 4++++
Msrc/game/bbr.dll/lifted-0042.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/tests/jpeg_idct.c | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 182 insertions(+), 0 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -257,6 +257,8 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/texture-resampling ./build/linux/texture-resampling-paged ./build/linux/loading-fade + ./build/linux/jpeg-idct + ./build/linux/jpeg-idct-paged ./build/linux/png-filter ./build/linux/png-filter-paged ./build/linux/archive-string @@ -609,3 +611,9 @@ build/linux/png-filter build/linux/png-filter-paged: tests/png_filter.c game/bbr $(CC) $(CPPFLAGS) $(CFLAGS) $(if $(filter %-paged,$@),-U__linux__) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/png-filter build/linux/png-filter-paged + +build/linux/jpeg-idct build/linux/jpeg-idct-paged: tests/jpeg_idct.c game/bbr.dll/lifted-0042.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(if $(filter %-paged,$@),-U__linux__) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/jpeg-idct build/linux/jpeg-idct-paged diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -175,3 +175,7 @@ build/wasm/web/stdio-concurrency-test.html: tests/stdio_concurrency.c runtime/gu build/wasm/png-filter.js: tests/png_filter.c game/bbr.dll/lifted-0047.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + +build/wasm/jpeg-idct.js: tests/jpeg_idct.c game/bbr.dll/lifted-0042.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ diff --git a/src/game/bbr.dll/lifted-0042.c b/src/game/bbr.dll/lifted-0042.c @@ -17297,6 +17297,89 @@ L_250BB3D0: RECOMP_REGS_OUT(); } +/* Integer inverse transform used by bbr.dll!sub_250BB3F0 (14.9.1). + * Unsigned arithmetic preserves x86 wrapping before signed right shifts. */ +static void jpeg_idct_pass(const uint32_t input[8], uint32_t output[8]) +{ + /* Even frequencies. */ + uint32_t even = (input[2] + input[6]) * 4433; + uint32_t even_low = even - input[6] * 15137; + uint32_t even_high = even + input[2] * 6270; + uint32_t dc_sum = (input[0] + input[4]) << 13; + uint32_t dc_difference = (input[0] - input[4]) << 13; + uint32_t sum0 = dc_sum + even_high; + uint32_t sum3 = dc_sum - even_high; + uint32_t sum1 = dc_difference + even_low; + uint32_t sum2 = dc_difference - even_low; + + /* Odd frequencies. */ + uint32_t odd = (input[1] + input[3] + input[5] + input[7]) * 9633; + uint32_t odd17 = 0u - (input[1] + input[7]) * 7373; + uint32_t odd35 = 0u - (input[3] + input[5]) * 20995; + uint32_t odd37 = odd - (input[3] + input[7]) * 16069; + uint32_t odd15 = odd - (input[1] + input[5]) * 3196; + uint32_t part7 = input[7] * 2446 + odd17 + odd37; + uint32_t part5 = input[5] * 16819 + odd35 + odd15; + uint32_t part3 = input[3] * 25172 + odd35 + odd37; + uint32_t part1 = input[1] * 12299 + odd17 + odd15; + + output[0] = sum0 + part1; + output[7] = sum0 - part1; + output[1] = sum1 + part3; + output[6] = sum1 - part3; + output[2] = sum2 + part5; + output[5] = sum2 - part5; + output[3] = sum3 + part7; + output[4] = sum3 - part7; +} + +static void jpeg_idct_block(uint32_t decoder, uint32_t component, + uint32_t coefficients, uint32_t rows, uint32_t column) +{ + uint32_t workspace[64], input[8], output[8]; + uint32_t quantization = MEM32(component + 0x50); + uint32_t range = MEM32(decoder + 0x120) + 0x80; + + /* Dequantize and transform each column into the intermediate workspace. */ + for (uint32_t x = 0; x < 8; ++x) { + uint32_t ac = 0; + for (uint32_t y = 0; y < 8; ++y) { + uint32_t index = y * 8 + x; + uint32_t coefficient = (int32_t)(int16_t)MEM16(coefficients + index * 2); + input[y] = coefficient * MEM32(quantization + index * 4); + if (y) ac |= coefficient; + } + if (!ac) { + for (uint32_t y = 0; y < 8; ++y) + workspace[y * 8 + x] = input[0] << 2; + } else { + jpeg_idct_pass(input, output); + for (uint32_t y = 0; y < 8; ++y) + workspace[y * 8 + x] = (int32_t)(output[y] + 1024) >> 11; + } + } + + /* Transform each row, round, and use the decoder's sample-range table. */ + for (uint32_t y = 0; y < 8; ++y) { + uint32_t destination = MEM32(rows + y * 4) + column; + uint32_t ac = 0; + for (uint32_t x = 0; x < 8; ++x) { + input[x] = workspace[y * 8 + x]; + if (x) ac |= input[x]; + } + if (!ac) { + uint32_t index = ((int32_t)(input[0] + 16) >> 5) & 1023; + uint8_t sample = MEM8(range + index); + for (uint32_t x = 0; x < 8; ++x) MEM8(destination + x) = sample; + } else { + jpeg_idct_pass(input, output); + for (uint32_t x = 0; x < 8; ++x) { + uint32_t index = ((int32_t)(output[x] + 131072) >> 18) & 1023; + MEM8(destination + x) = MEM8(range + index); + } + } + } +} void sub_250BB3F0(void) { int _fpu_cmp = 0; uint32_t _cf = 0; @@ -17306,6 +17389,22 @@ void sub_250BB3F0(void) { uint32_t _itail_tgt = 0; RECOMP_REGS_LOCALS RECOMP_ENTER(0x250BB3F0u); + /* The native cooperative scheduler observes the emulated scratch frame. + * Retain the instruction path whenever that observer is installed. */ + if (!recomp_yield_hook) { + uint32_t rows = MEM32(esp + 16); + jpeg_idct_block(MEM32(esp + 4), MEM32(esp + 8), MEM32(esp + 12), + rows, MEM32(esp + 20)); + g_backedges += 16; + eax = 0; + ecx = rows + 32; + edx = esp - 0x30; + _flag_k = FK_DEC; _flag_a = 1; _flag_b = 1; _cf = edx < 32; + RECOMP_FLAGS_OUT(); + esp += 4; + RECOMP_REGS_OUT(); + return; + } L_250BB3F0: PUSH32(esp, ebp); /* 0x250BB3F0: push ebp */ diff --git a/src/tests/jpeg_idct.c b/src/tests/jpeg_idct.c @@ -0,0 +1,71 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_250BB3F0(void); +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +recomp_func_t recomp_lookup(uint32_t address) { abort(); } +static unsigned yields; +static void observe_yield(void) { ++yields; } + +int main(void) +{ + assert(guest_map("JPEG inverse transform", 0x50000000, 0x40000)); + MEM32(0x50001120) = 0x50003f80; + MEM32(0x50002050) = 0x50006000; + for (unsigned i = 0; i < 1024; ++i) { + int value = (i < 512 ? (int)i : (int)i - 1024) + 128; + MEM8(0x50004000 + i) = value < 0 ? 0 : value > 255 ? 255 : value; + } + for (unsigned i = 0; i < 8; ++i) MEM32(0x50005000 + i * 4) = 0x5000fffd + i * 64; + const unsigned char expected[64] = { + 138,136,133,129,126,121,119,117, + 138,138,133,132,124,124,118,118, + 141,137,138,129,131,122,123,119, + 142,143,136,138,127,129,122,123, + 146,142,144,134,136,127,128,124, + 148,148,142,143,133,134,128,128, + 151,148,147,140,140,132,132,129, + 151,150,146,144,138,136,132,131 + }; + for (unsigned trial = 0; trial < 8; ++trial) { + memset((void *)ADDR(0x50003000), 0, 128); + for (unsigned i = 0; i < 64; ++i) MEM32(0x50006000 + i * 4) = 1; + if (trial % 4 == 1) MEM16(0x50003000) = 8; + if (trial % 4 == 2) MEM16(0x50003000) = -8; + if (trial % 4 == 3) { + MEM16(0x50003000) = 48; + MEM16(0x50003002) = 60; + MEM16(0x50003010) = -40; + MEM16(0x5000307e) = 13; + } + memset((void *)ADDR(0x5000fffd), 0xa5, 512); + g_esp = 0x50030008; + g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456; + MEM32(g_esp + 4) = 0x50001000; + MEM32(g_esp + 8) = 0x50002000; + MEM32(g_esp + 12) = 0x50003000; + MEM32(g_esp + 16) = 0x50005000; + MEM32(g_esp + 20) = 3; + recomp_yield_hook = trial < 4 ? NULL : observe_yield; + g_backedges = RECOMP_YIELD_EVERY - 8; + yields = 0; + + sub_250BB3F0(); + + for (unsigned y = 0; y < 8; ++y) { + for (unsigned x = 0; x < 8; ++x) { + unsigned sample = trial % 4 == 3 ? expected[y * 8 + x] : + trial % 4 == 1 ? 129 : trial % 4 == 2 ? 127 : 128; + assert(MEM8(0x50010000 + y * 64 + x) == sample); + } + assert(MEM8(0x5000ffff + y * 64) == 0xa5); + assert(MEM8(0x50010008 + y * 64) == 0xa5); + } + assert(g_esp == 0x5003000c && g_ebp == 123); + assert(g_ebx == 234 && g_esi == 345 && g_edi == 456); + assert(g_eax == 0 && g_ecx == 0x50005020 && g_edx == 0x5002ffd8); + assert(g_backedges == (trial < 4 ? RECOMP_YIELD_EVERY + 8 : 8)); + assert(yields == (trial < 4 ? 0 : 1)); + } +}